Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Stride.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: Stride.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.32 $       $Date: 2008/06/02 08:03:09 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *  Stride interface class.
00019  ***************************************************************************/
00020 
00021 #include <stdio.h>      // for tmpfile
00022 #include "DrawMolecule.h"
00023 #include "Timestep.h"
00024 #include "Residue.h"
00025 #include "Inform.h"
00026 #include "Stride.h"
00027 #include "utilities.h"  // for vmd_delete_file
00028 #include "VMDDir.h"
00029 
00030 // Write a pdb of the given timestep.
00031 // Write the uniq_resid of the residues written to the stride input file
00032 // into the residues argument.
00033 static int write_stride_record( DrawMolecule *mol, 
00034                                 const char *inputfilename,
00035                                 ResizeArray<int>& residues ) {
00036   const float *pos = mol->current()->pos;
00037   char name[6], resname[5];
00038  
00039   residues.clear();
00040 
00041   FILE *inputfile = fopen(inputfilename,"w");
00042   if (!inputfile) {
00043     msgErr << "Unable to open input file '" << inputfilename 
00044            << "' for writing." << sendmsg;
00045     return 1;
00046   }
00047 
00048   int prev = -1; // previous uniq_resid
00049   int atomcount = 0;
00050 
00051   for (int i=0; i < mol->nAtoms; i++) {
00052     MolAtom *atom = mol->atom(i);
00053     // skip if this atom isn't part of protein
00054     if (atom->residueType != RESPROTEIN)
00055       continue;
00056     const float *loc=pos + 3*i;
00057     strncpy(name, (mol->atomNames).name(atom->nameindex), 4);
00058     name[4]='\0';
00059     strncpy(resname,(mol->resNames).name(atom->resnameindex),4);  
00060     resname[4] = '\0';
00061 
00062     if (atom->uniq_resid != prev) {
00063       prev = atom->uniq_resid;
00064       residues.append(prev);
00065     }
00066 
00067     if (fprintf(inputfile,
00068       "ATOM  %5d %-4s %-4s %4d    %8.3f%8.3f%8.3f\n",
00069       ++atomcount,name,resname,residues.num()-1,loc[0],loc[1],loc[2]) < 0) {
00070       msgErr << "Error writing line in Stride input file for atom " << i 
00071              << sendmsg;
00072       return 1;
00073     }
00074   }
00075   fclose(inputfile);
00076   return 0;
00077 }
00078 
00079 /* Here's the sprintf command stride uses on output in report.c:
00080 
00081       sprintf(Tmp,"ASG  %3s %c %4s %4d    %c   %11s   %7.2f   %7.2f   %7.1f",
00082         p->ResType,SpaceToDash(Chain[Cn]->Id),p->PDB_ResNumb,i+1,
00083         p->Prop->Asn,Translate(p->Prop->Asn),p->Prop->Phi,
00084         p->Prop->Psi,p->Prop->Solv);
00085 
00086    Here's a typical line:
00087 ASG  THR -    9    9    B        Bridge   -113.85    157.34      21.9      ~~~~
00088 
00089 */
00090 
00091 const int BUF_LEN = 90;
00092 const char sstypes[] = "HGIEBbT";
00093 static int read_stride_record( DrawMolecule *mol, 
00094                                const char *stridefile,
00095                                const ResizeArray<int> &residues ) { 
00096   FILE *in = fopen(stridefile, "rt");
00097   if (!in) {
00098     msgErr << "Unable to find Stride output file: "
00099            << stridefile << sendmsg;
00100     return 1;
00101   }
00102   char buf[BUF_LEN], resname[4], chain[1], uniq_residstr[5], ss[1];
00103   const char *stype;
00104   const char *sstypes_start = &sstypes[0];
00105   int resid[1];
00106   while (fgets(buf, BUF_LEN, in)) {
00107     if (strncmp(buf, "ASG", 3)) continue;
00108     sscanf(buf,"ASG  %3s %c %4s %4d    %c",
00109       resname, chain, uniq_residstr, resid, ss);
00110     int index = atoi(uniq_residstr);
00111     if (index < 0 || index >= residues.num()) {
00112       msgErr << "invalid resid found in stride output file!" << sendmsg;
00113       msgErr << "Error found in the following line: " << sendmsg;
00114       msgErr << buf << sendmsg;
00115       fclose(in);
00116       return -1;
00117     }
00118     int uniq_resid = residues[index];
00119 
00120     stype = strchr(sstypes,ss[0]);
00121 
00122     if (stype == NULL) {
00123       mol->residueList[uniq_resid]->sstruct = SS_COIL;
00124       continue;
00125     }
00126     switch ((int)(stype - sstypes_start)) {
00127       case 0: // H
00128         mol->residueList[uniq_resid]->sstruct = SS_HELIX_ALPHA;
00129         break;
00130       case 1: // G
00131         mol->residueList[uniq_resid]->sstruct = SS_HELIX_3_10;
00132         break; 
00133       case 2: // I
00134         mol->residueList[uniq_resid]->sstruct = SS_HELIX_PI;
00135         break;
00136       case 3: // E
00137         mol->residueList[uniq_resid]->sstruct = SS_BETA;
00138         break;
00139       case 4: // B
00140       case 5: // b
00141         mol->residueList[uniq_resid]->sstruct = SS_BRIDGE;
00142         break;
00143       case 6: // T
00144         mol->residueList[uniq_resid]->sstruct = SS_TURN;
00145         break;
00146       default:
00147         msgErr << "Internal error in read_stride_record\n" << sendmsg;
00148         mol->residueList[uniq_resid]->sstruct = SS_COIL;
00149     }
00150   }
00151   fclose(in);
00152   return 0;
00153 }  
00154 
00155 int ss_from_stride(DrawMolecule *mol) {
00156   int rc = 0;
00157   char *stridebin   = getenv("STRIDE_BIN");
00158   char *infilename; 
00159   char *outfilename;
00160   
00161   if (!stridebin) {
00162     msgErr << "No STRIDE binary found; please set STRIDE_BIN environment variable" << sendmsg;
00163     msgErr << "to the location of the STRIDE binary." << sendmsg;
00164     return 1;
00165   }
00166 
00167   // check to see if the executable exists
00168   if (!vmd_file_is_executable(stridebin)) {
00169     msgErr << "STRIDE binary " << stridebin << " cannot be run; check permissions." << sendmsg;
00170     return 1;
00171   }
00172 
00173 #if defined(ARCH_MACOSXX86) || defined(ARCH_MACOSXX86_64)
00174   infilename = tmpnam(NULL);
00175   outfilename = tmpnam(NULL);
00176   char *tmpstr;
00177   tmpstr  = (char *) malloc(strlen(infilename));
00178   strcpy(tmpstr, infilename);
00179   infilename = tmpstr;
00180   tmpstr = (char *) malloc(strlen(outfilename));
00181   strcpy(tmpstr, outfilename);
00182   outfilename = tmpstr;
00183 #else
00184   infilename  = tempnam(NULL, NULL);
00185   outfilename = tempnam(NULL, NULL);
00186 #endif
00187   if (infilename == NULL || outfilename == NULL) {
00188     msgErr << "Unable to create temporary files for STRIDE." << sendmsg;
00189     return 1;
00190   }
00191 
00192   char *stridecall = new char[strlen(stridebin)
00193                               +strlen(infilename)
00194                               +strlen(outfilename)
00195                               +16];
00196 
00197   sprintf(stridecall,"\"%s\" %s -f%s", stridebin, infilename, outfilename);
00198 
00199   ResizeArray<int> residues;
00200 
00201   if (write_stride_record(mol,infilename,residues)) {
00202     msgErr << "Stride::write_stride_record: unable "
00203            << "to write input file for Stride\n" << sendmsg;
00204     rc = 1;
00205   }
00206 
00207   if (!rc) {
00208     vmd_system(stridecall);
00209 
00210     if (read_stride_record(mol,outfilename,residues)) {
00211       msgErr << "Stride::read_stride_record: unable "
00212              << "to read output file from Stride\n" << sendmsg;
00213       rc = 1;
00214     }
00215   }
00216 
00217   delete [] stridecall;
00218   vmd_delete_file(outfilename);
00219   vmd_delete_file(infilename);
00220   free(outfilename);
00221   free(infilename);
00222 
00223   return rc;
00224 } 

Generated on Sat Aug 30 01:27:04 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002