GLYLIB
0.3.0b
|
00001 /* File read_amber_prmtop.c begun on 20080425 by BLFoley 00002 Purpose: read amber prmtop file info into an assembly 00003 See also: amber_prmtop_structs.h */ 00004 //#include "molecules.h" 00005 //#include "general.h" 00006 #include "AMBER/amber_prmtop.h" 00007 //#include "amber_prmtop_structs.h" 00008 //#include "declarations.h" 00009 //void read_amber_prmtop_asis(fileset F,amber_prmtop *P); 00010 // the amber_prmtop *P should be -already- allocated and initialized 00011 00012 00013 /********** assembly *read_amber_prmtop(fileset F) **********/ 00014 /* Main function for reading the prmtop file 00015 * the amber_prmtop *P should be -already- allocated and initialized 00016 * BUT, it should be otherwise empty */ 00017 void read_amber_prmtop_asis(fileset F,amber_prmtop *P){ 00018 int aa=0,anl=0,sec=0,ent=0,fld=0; // generic, line, section, entry & field counters 00019 int newflag=0,newline=0,iseof=1; // utility flags 00020 char line[501],over[501],tmp[81],*s1,*s2,*fgets_check; 00021 fpos_t line_current; 00022 00023 00024 // Open files and initialize some variables 00025 //F.F=myfreopen(F.N,"r",F.F); // don't depend on calling function to have opened 00026 F.F=myfopen(F.N,"r"); // 00027 P[0].FN=strdup(F.N); // the name of the file from which these data were taken 00028 P[0].nS=0; // the prmtop structure should be empty 00029 P[0].S=(amber_prmtop_section*)calloc(1,sizeof(amber_prmtop_section)); 00030 P[0].SN=(char**)calloc(1,sizeof(char*)); 00031 00032 // 00033 // Get the first line, which should be the "VERSION" 00034 // 00035 if(fgets(line,500,F.F)==NULL){read_fneek("Problem in read_amber_prmtop",1,0,F.N);} 00036 // See if the first 500 chars includes a newline... 00037 if(strchr(line,'\n')==NULL){ // if we didn't get the whole line 00038 fld=500; 00039 fprintf(stderr,"Unusually long VERSION line.\n"); 00040 fprintf(stderr,"-- Ignoring characters beyond 500.\n"); 00041 if(fgets(over,500,F.F)==NULL){// try to get rest of line 00042 read_fneek("Problem reading VERSION line in read_amber_prmtop.",1,fld,F.N);} 00043 while(strchr(over,'\n')==NULL){// try to get rest of line 00044 fld+=500; 00045 if(fgets(over,500,F.F)==NULL){ 00046 read_fneek("Problem readin VERSION line in read_amber_prmtop.",1,fld,F.N);} 00047 } 00048 } // close lengthy line check... 00049 if(strncmp("\%VERSION",line,8)!=0){ 00050 read_eek("First line not \%VERSION.",F.N);} 00051 P[0].VERSION=strdup(line); 00052 anl=2; 00053 fld=0; 00054 // 00055 // grab next line, which should be a flag line 00056 // 00057 if(fgets(line,500,F.F)==NULL){ 00058 read_fneek("Problem in read_amber_prmtop",anl,fld,F.N);} 00059 // Check the line -- it should be a short-ish FLAG line 00060 00061 //printf("FLAG line read from top is >>>%s<<<\n",line); 00062 00063 if(strchr(line,'\n')==NULL){// die if line too long 00064 read_eek("FLAG line over 500 chars... \n",F.N);} 00065 // 00066 // Read the FLAG entry 00067 // 00068 if(strncmp("\%FLAG",line,5)!=0){ // must start with %FLAG 00069 read_fneek("Entry not FLAG in read_amber_prmtop",anl,fld,F.N);} 00070 // Parse the FLAG line 00071 if(sscanf(line,"%s %s",over,tmp)!=2){mywhine("Problem in read_amber_prmtop with 1st FLAG read.");} 00072 newflag=0; 00073 // 00074 // Start main read of topology file 00075 // 00076 while(newflag==0){// while we have found another flag entry 00077 // We have a FLAG entry, so increment the number of sections 00078 //printf("line (FLAG) is >>%s<<\n",line); 00079 sec=P[0].nS; 00080 P[0].nS++; 00081 // Allocate memory 00082 P[0].S=(amber_prmtop_section*)realloc(P[0].S,P[0].nS*sizeof(amber_prmtop_section)); 00083 P[0].SN=(char**)realloc(P[0].SN,P[0].nS*sizeof(char*)); 00084 P[0].SN[sec]=strdup(tmp); // record FLAG entry in 00085 P[0].S[sec].N=strdup(tmp); // both locations 00086 // clear the other entries in the new structure 00087 P[0].S[sec].FORMAT=NULL; 00088 P[0].S[sec].TYPE=NULL; 00089 P[0].S[sec].desc=NULL; 00090 P[0].S[sec].is_standard=0; 00091 P[0].S[sec].nc=0; 00092 P[0].S[sec].npl=0; 00093 P[0].S[sec].nt=0; 00094 P[0].S[sec].ns=0; 00095 P[0].S[sec].nu=0; 00096 P[0].S[sec].D=NULL; 00097 // 00098 // Read the FORMAT entry 00099 // 00100 anl++; 00101 if(fgets(line,500,F.F)==NULL){ // if we can't read the line, die 00102 read_fneek("Problem in read_amber_prmtop.",anl,fld,F.N);} 00103 //printf("FORMAT line read from top is >>>%s<<<\n",line); 00104 if(strchr(line,'\n')==NULL){ // FORMAT lines should be short-ish 00105 read_fneek("FORMAT line over 500 chars... \n",anl,fld,F.N);} 00106 if(strncmp("\%FORMAT",line,7)!=0){// must start with %FORMAT 00107 read_fneek("Entry not FORMAT in read_amber_prmtop",anl,fld,F.N);} 00108 //printf("line (FORMAT) is >>%s<<\n",line); 00109 // split line by parentheses as tokens 00110 s1=strdup(strtok(line,"()"));// toss out the "%FORMAT" part 00111 P[0].S[sec].FORMAT=strdup(strtok(NULL,"()")); // save next 00112 s1=strdup(P[0].S[sec].FORMAT); // save a char string to tear apart 00113 // parse the format string 00114 s2=strdup(strtok(s1,"aAiIfFeE")); // up to the format identifier 00115 sscanf(s2,"%d",&P[0].S[sec].npl); // max entries expected per line 00116 s2=strdup(strtok(NULL,".")); // up to any periods 00117 if((strchr(s2,'S')!=NULL)||(strchr(s2,'N')!=NULL)){over[0]='0';} // EN or ES 00118 sscanf(s2,"%d",&P[0].S[sec].nc); // number of chars in format 00119 //printf("parsed format: npl is %d ; nc is %d \n",P[0].S[sec].npl,P[0].S[sec].nc); 00120 // 00121 // Read the section 00122 // 00123 P[0].S[sec].D=(char**)calloc(1,sizeof(char*)); // initially allocate **D 00124 P[0].S[sec].nt=0; // total pieces of data found 00125 ent=0; 00126 if(fgetpos(F.F,&line_current)!=0){mywhine("Problem getting line_start position in read_amber_prmtop.");} 00127 fgets_check=fgets(line,6,F.F); 00128 while(fgets_check!=NULL){ 00129 //printf("the new section fgets's line is >>%s<<\n",line); 00130 if(strcspn(line,"\0")==0){ //printf("found empty line\n"); 00131 if(fgetpos(F.F,&line_current)!=0){mywhine("Problem getting line_current position in read_amber_prmtop.");} 00132 fgets_check=fgets(line,6,F.F); 00133 continue; 00134 } 00135 anl++; // we have a new line 00136 fld=0; // start field count over 00137 // check to see if first 5 chars match "%FLAG" 00138 if(strncmp(line,"\%FLAG",5)==0){ 00139 //printf("found a FLAG line, resetting line start\n"); 00140 if(fsetpos(F.F,&line_current)!=0){mywhine("Problem resetting line_start position in read_amber_prmtop.");} 00141 break;} // if so, stop this 00142 if(strcspn(line,"\n")==0){ 00143 fprintf(stderr,"Unexpected blank line in read_amber_prmtop\n"); 00144 fprintf(stderr," at line number %d in file %s. Ignoring.\n",anl,F.N); 00145 fprintf(stderr,"The section (%d) FLAG is: %s\n",sec,P[0].SN[sec]); 00146 if(fgetpos(F.F,&line_current)!=0){mywhine("Problem getting line_current position in read_amber_prmtop.");} 00147 fgets_check=fgets(line,6,F.F); 00148 continue; 00149 } 00150 // 00151 // Now read the rest of the line... 00152 // 00153 //printf("reading the rest of the line.\n"); 00154 if(fsetpos(F.F,&line_current)!=0){mywhine("Problem resetting line_start position in read_amber_prmtop.");} 00155 newline=1; 00156 while(newline!=0){ 00157 if(fgets(line,(P[0].S[sec].nc+1),F.F)==NULL){ 00158 read_fneek("problem reading data line in read_amber_prmtop",anl,ent,F.N);} 00159 //printf("the line-bit for P[0].S[sec].nc=%d is >>>%s<<<\n",P[0].S[sec].nc,line); 00160 //printf("in ascii decimal: "); 00161 //for(ae=0;ae<P[0].S[sec].nc+1;ae++){printf(" %d ",line[ae]);} 00162 //printf("\n"); 00163 if(strcspn(line,"\0")==0){ //printf("found empty string\n"); 00164 newline=0; 00165 break;} 00166 if(strcspn(line,"\n")==0){ // at end of line, no new data 00167 newline=0; 00168 break;} 00169 if(strchr(line,'\n')!=NULL){ // this is the end of a line, but with data 00170 for(aa=0;aa<strlen(line);aa++){ //check for all whitespace 00171 if(isspace(line[aa])==0) newline=-1; // something isn't white 00172 } 00173 if(newline==-1){ 00174 line[strcspn(line,"\n")]='\0'; 00175 ent=P[0].S[sec].nt; // for convenience 00176 // allocate space for the next entry 00177 P[0].S[sec].nt++; 00178 P[0].S[sec].D=(char**)realloc(P[0].S[sec].D,P[0].S[sec].nt*sizeof(char*)); 00179 P[0].S[sec].D[ent]=(char*)calloc(P[0].S[sec].nc+1,sizeof(char)); 00180 strncpy(P[0].S[sec].D[ent],line,P[0].S[sec].nc+1); 00181 //printf("the entry for P[0].S[%d].D[%d] is -now- >>>%s<<< ; nt is %d\n",sec,ent,P[0].S[sec].D[ent],P[0].S[sec].nt); 00182 } 00183 newline=0; 00184 break; 00185 } 00186 ent=P[0].S[sec].nt; // for convenience 00187 // allocate space for the next entry 00188 P[0].S[sec].nt++; 00189 P[0].S[sec].D=(char**)realloc(P[0].S[sec].D,P[0].S[sec].nt*sizeof(char*)); 00190 P[0].S[sec].D[ent]=(char*)calloc(P[0].S[sec].nc+1,sizeof(char)); 00191 strncpy(P[0].S[sec].D[ent],line,P[0].S[sec].nc+1); 00192 //printf("the entry for P[0].S[%d].D[%d] is -now- >>>%s<<< ; nt is %d\n",sec,ent,P[0].S[sec].D[ent],P[0].S[sec].nt); 00193 //printf("the entry for P[0].S[%d].D[%d] is -now- >>>%s<<<\n",sec,ent,P[0].S[sec].D[ent]); 00194 } // close while not a new line 00195 if(fgetpos(F.F,&line_current)!=0){mywhine("Problem getting line_current position in read_amber_prmtop.");} 00196 fgets_check=fgets(line,6,F.F); 00197 }// close fgets within a section 00198 00199 if(newflag==-1){break;} 00200 if(fgets(line,500,F.F)==NULL){ 00201 if(fgetc(F.F)==EOF){ 00202 iseof=1; 00203 newflag=-1; 00204 break;} 00205 else{read_fneek("Problem in FLAG line read read_amber_prmtop",anl,0,F.N);} 00206 } 00207 if(newflag==-1){break;} 00208 // If here, then we found a FLAG, read it in, exit loop and start new 00209 // Check the line -- it should be a short-ish FLAG line 00210 if(strchr(line,'\n')==NULL){// die if line too long 00211 read_eek("FLAG line over 500 chars... \n",F.N);} 00212 // 00213 // Read the FLAG entry 00214 // 00215 // Parse the FLAG line 00216 //printf("Flag line is >>>%s<<<\n",line); 00217 if(sscanf(line,"%s %s",over,tmp)!=2){ 00218 read_fneek("Problem FLAG entry scan, read_amber_prmtop.",anl,1,F.N);} 00219 } // close fgets for reading file 00220 00221 fclose(F.F); 00222 00223 /* 00224 F.F=myfopen("test_rewrite_of_prmtop","w"); 00225 fprintf(F.F,"%s",P[0].VERSION); 00226 for(sec=0;sec<P[0].nS;sec++){ // for each section found 00227 fprintf(F.F,"%%FLAG %s\n",P[0].S[sec].N); 00228 fprintf(F.F,"%%FORMAT(%s)\n",P[0].S[sec].FORMAT); 00229 for(aa=0;aa<P[0].S[sec].nt;aa++){ 00230 fprintf(F.F,"%s",P[0].S[sec].D[aa]); 00231 //fprintf(F.F,"D[%d] is >>>%s<<<\n",aa,P[0].S[sec].D[aa]); 00232 fflush(F.F); 00233 //printf("\naa is %d ; P[0].S[%d].npl is %d ; (aa+1)%%npl is %d\n",aa,sec,P[0].S[sec].npl,); 00234 if((((aa+1)%P[0].S[sec].npl))==0){fprintf(F.F,"\n");} 00235 //if((((aa+1)*P[0].S[sec].nc))%80==0){printf("\n");} 00236 } 00237 if(((aa)*P[0].S[sec].nc)%80!=0){fprintf(F.F,"\n");} 00238 } 00239 fclose(F.F); 00240 */ 00241 00242 return; 00243 }