00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include "DrawMolecule.h"
00023 #include "Timestep.h"
00024 #include "Residue.h"
00025 #include "Inform.h"
00026 #include "Stride.h"
00027 #include "utilities.h"
00028 #include "VMDDir.h"
00029
00030
00031
00032
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;
00049 int atomcount = 0;
00050
00051 for (int i=0; i < mol->nAtoms; i++) {
00052 MolAtom *atom = mol->atom(i);
00053
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
00080
00081
00082
00083
00084
00085
00086
00087
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:
00128 mol->residueList[uniq_resid]->sstruct = SS_HELIX_ALPHA;
00129 break;
00130 case 1:
00131 mol->residueList[uniq_resid]->sstruct = SS_HELIX_3_10;
00132 break;
00133 case 2:
00134 mol->residueList[uniq_resid]->sstruct = SS_HELIX_PI;
00135 break;
00136 case 3:
00137 mol->residueList[uniq_resid]->sstruct = SS_BETA;
00138 break;
00139 case 4:
00140 case 5:
00141 mol->residueList[uniq_resid]->sstruct = SS_BRIDGE;
00142 break;
00143 case 6:
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
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 }