# include # include # include # include # include # define MAXCHAR 256 # define COLORTEXT "YES" # define PI 3.1415926 # define debug 0 typedef struct { int flag; int id; char name[10]; char type[10]; double mass; double pol; } ATOM; typedef struct { int id; int flag; char at1[10]; char at2[10]; double len; double force; } BOND; typedef struct { int id; int flag; char at1[10]; char at2[10]; char at3[10]; double angle; double force; } ANGLE; typedef struct { int id; int type; int flag; char at1[10]; char at2[10]; char at3[10]; char at4[10]; double periodicity; double phase; double force; } TOR; /* for vdw parameters */ typedef struct { char type[10]; double e; double r; } VDW; /* for atom-atom pairs*/ typedef struct { int index; int ati; int atj; double a; double b; } VDWPAIR; /* for mixed information of ATOM and VDW*/ typedef struct { char type[10]; double mass; double pol; double e; double r; } VDW2; int i,j,k=0; int atomnum = 0; int typenum = 0; /* vdw type number */ int pairnum = 0; int btnum = 0; int atnum = 0; int ttnum = 0; int btnum_count = 0; int atnum_count = 0; int ttnum_count = 0; int improper_flag = 0; ATOM *atom; BOND *bond; ANGLE *angle; TOR *tor; VDW *vdw; VDWPAIR *vdwpair; BOND *bond2; ANGLE *angle2; TOR *tor2; VDW2 *vdw2; int atomtypenum = 0; /*true atom type number */ int btnum2 = 0; /* ture bond type number */ int atnum2 = 0; /* true angle type number*/ int ttnum2 = 0; /* true torsional type number */ int nbonh = 0; int nbona = 0; int ntheth = 0; int ntheta = 0; int nphih = 0; int nphia = 0; char pfilename[MAXCHAR]; char ofilename[MAXCHAR]; FILE *fptop, *fpout ; char line[MAXCHAR]; char tmpchar1[MAXCHAR], tmpchar2[MAXCHAR], tmpchar3[MAXCHAR]; int tmpint1, tmpint2; void rtopparm() { int tmpint1, tmpint2, tmpint3, tmpint4, tmpint5; for(;;) { line[7] = ' '; line[15] = ' '; line[23] = ' '; line[31] = ' '; line[39] = ' '; line[47] = ' '; line[55] = ' '; line[63] = ' '; line[71] = ' '; line[79] = ' '; if(fgets(line, MAXCHAR, fptop)==NULL) break; sscanf(line, "%s%s", tmpchar1, tmpchar2); if (strcmp("%FLAG", tmpchar1) == 0 &&strcmp("POINTERS", tmpchar2) == 0) { fgets(line, MAXCHAR, fptop); fgets(line, MAXCHAR, fptop); sscanf(line, "%d%d%d%d%d%d%d%d", &atomnum, &typenum, &nbonh, &nbona, &ntheth, &ntheta, &nphih, &nphia); fgets(line, MAXCHAR, fptop); sscanf(line, "%d%d%d%d%d%d%d%d", &tmpint1, &tmpint2, &tmpint3, &tmpint4, &tmpint5, &btnum, &atnum, &ttnum); } } } void rtopatom() { char str[10]; double v[5]; int index[10]; int numatom; int tmpint; int flag = -999; for(;;) { line[7] = ' '; line[15] = ' '; line[23] = ' '; line[31] = ' '; line[39] = ' '; line[47] = ' '; line[55] = ' '; line[63] = ' '; line[71] = ' '; line[79] = ' '; if(fgets(line, MAXCHAR, fptop)==NULL) break; sscanf(line, "%s%s", tmpchar1, tmpchar2); if (strcmp("%FLAG", tmpchar1) == 0 &&strcmp("ATOM_NAME", tmpchar2) == 0) { flag = 0; continue; } if (strcmp("%FLAG", tmpchar1) == 0 &&strcmp("MASS", tmpchar2) == 0) { flag = 2; continue; } if (strcmp("%FLAG", tmpchar1) == 0 &&strcmp("ATOM_TYPE_INDEX", tmpchar2) == 0) { flag = 4; continue; } if (strcmp("%FLAG", tmpchar1) == 0 &&strcmp("AMBER_ATOM_TYPE", tmpchar2) == 0) { flag = 6; continue; } if (strcmp("%FLAG", tmpchar1) == 0 &&strcmp("POLARIZABILITY", tmpchar2) == 0) { flag = 8; continue; } if(flag==0) {flag = 1; i= 0; continue; } if(flag==2) {flag = 3; i =0 ; continue; } if(flag==4) {flag = 5; i =0 ; continue; } if(flag==6) {flag = 7; i =0 ; continue; } if(flag==8) {flag = 9; i =0 ; continue; } if(flag ==1) { for(j=0;j<20;j++) { strcpy(str, ""); str[0] = '\0'; str[0]=line[j*4]; if(line[j*4+1] == ' ' || line[j*4+1] == '\0' || line[j*4+1] == '\n') str[1] = '\0'; else { str[1]=line[j*4+1]; if(line[j*4+2] == ' ' || line[j*4+2] == '\0' || line[j*4+2] == '\n') str[2] = '\0'; else { str[2]=line[j*4+2]; if(line[j*4+3] == ' ' || line[j*4+3] == '\0' || line[j*4+3] == '\n') str[3] = '\0'; else { str[3]=line[j*4+3]; str[4]= '\0'; } } } strcpy(atom[i].name, str); i++; if(i>=atomnum) { flag = -999; break; } } } if(flag ==3) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { atom[i].mass = v[j]; i++; if(i>=atomnum) { flag = -999; break; } } } if(flag ==5) { sscanf(line, "%d%d%d%d%d%d%d%d%d%d", &index[0], &index[1], &index[2], &index[3], &index[4], &index[5], &index[6], &index[7], &index[8], &index[9]); for(j=0;j<10;j++) { atom[i].id = index[j]; i++; if(i>=atomnum) { flag = -999; break; } } } if(flag ==7) { for(j=0;j<20;j++) { strcpy(str, ""); str[0] = '\0'; str[0]=line[j*4]; if(line[j*4+1] == ' ' || line[j*4+1] == '\0' || line[j*4+1] == '\n') str[1] = '\0'; else { str[1]=line[j*4+1]; if(line[j*4+2] == ' ' || line[j*4+2] == '\0' || line[j*4+2] == '\n') str[2] = '\0'; else { str[2]=line[j*4+2]; if(line[j*4+3] == ' ' || line[j*4+3] == '\0' || line[j*4+3] == '\n') str[3] = '\0'; else { str[3]=line[j*4+3]; str[4]= '\0'; } } } strcpy(atom[i].type, str); i++; if(i>=atomnum) { flag = -999; break; } } } if(flag ==9) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { atom[i].pol = v[j]; i++; if(i>=atomnum) { flag = -999; break; } } } } if(debug == 1) { for(i=0;i=btnum) { flag = -999; break; } } } if(flag ==3) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { bond[i].len = v[j]; bond[i].flag = 0; i++; if(i>=btnum) { flag = -999; break; } } } if(flag ==5) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { angle[i].force = v[j]; i++; if(i>=atnum) { flag = -999; break; } } } if(flag ==7) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { angle[i].angle = v[j]; angle[i].flag = 0; i++; if(i>=atnum) { flag = -999; break; } } } if(flag ==9) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { tor[i].force = v[j]; i++; if(i>=ttnum) { flag = -999; break; } } } if(flag ==11) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { tor[i].periodicity = v[j]; i++; if(i>=ttnum) { flag = -999; break; } } } if(flag ==13) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { tor[i].phase = v[j]; tor[i].flag = -1; i++; if(i>=ttnum) { flag = -999; break; } } } if(flag ==15 || flag == 17) { j = 0; while(j <= 10) { j++; if(line[j*8-1] <='9' && line[j*8-1]>='0') sscanf(&line[8*j-8], "%d", &tmpint); else break; if(k==0) { at1 = tmpint/3; k=1; continue; } if(k==1) { at2 = tmpint/3; k=2; continue; } if(k==2) { k=0; strcpy(bond2[btnum2].at1, atom[at1].type); strcpy(bond2[btnum2].at2, atom[at2].type); bond2[btnum2].id = tmpint - 1; bond2[btnum2].flag = 0; btnum2++; if(btnum2>=(nbonh + nbona)) { flag = -999; break; } continue; } } } if(flag ==19 || flag == 21) { j = 0; while(j <= 10) { j++; if(line[j*8-1] <='9' && line[j*8-1]>='0') sscanf(&line[8*j-8], "%d", &tmpint); else break; if(k==0) { at1 = tmpint/3; k=1; continue; } if(k==1) { at2 = tmpint/3; k=2; continue; } if(k==2) { at3 = tmpint/3; k=3; continue; } if(k==3) { k=0; strcpy(angle2[atnum2].at1, atom[at1].type); strcpy(angle2[atnum2].at2, atom[at2].type); strcpy(angle2[atnum2].at3, atom[at3].type); angle2[atnum2].id = tmpint -1; angle2[atnum2].flag = 0; atnum2 ++; if(atnum2>=(ntheth + ntheta)) { flag = -999; break; } } } } if(flag ==23 || flag == 25) { j = 0; tortype = 0; while(j <= 10) { j++; if(line[j*8-1] <='9' && line[j*8-1]>='0') sscanf(&line[8*j-8], "%d", &tmpint); else break; if(k==0) { at1 = fabs(tmpint)/3; k=1; continue; } if(k==1) { at2 = fabs(tmpint)/3; k=2; continue; } if(k==2) { at3 = fabs(tmpint)/3; k=3; continue; } if(k==3) { if(tmpint < 0) tortype = 1; at4 = fabs(tmpint)/3; k=4; continue; } if(k==4) { k=0; strcpy(tor2[ttnum2].at1, atom[at1].type); strcpy(tor2[ttnum2].at2, atom[at2].type); strcpy(tor2[ttnum2].at3, atom[at3].type); strcpy(tor2[ttnum2].at4, atom[at4].type); tor2[ttnum2].type = tortype; tor2[ttnum2].id = tmpint - 1; tor2[ttnum2].flag = 0; ttnum2 ++; if(ttnum2>=(nphih + nphia)) { flag = -999; break; } continue; } } } if(flag ==27) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { vdwpair[i].a = v[j]; i++; if(i>=pairnum) { flag = -999; break; } } } if(flag ==29) { sscanf(line, "%lf%lf%lf%lf%lf", &v[0], &v[1], &v[2], &v[3], &v[4]); for(j=0;j<5;j++) { vdwpair[i].b = v[j]; i++; if(i>=pairnum) { flag = -999; break; } } } } } void write(void) { int i,j; int suc; fprintf(fpout, "remark goes here\n"); fprintf(fpout, "MASS\n"); for(i=0;i 0) fprintf(fpout, "%-2s-%-2s%10.3lf%10.3lf\n", bond2[i].at2, bond2[i].at1, bond2[i].force, bond2[i].len); else fprintf(fpout, "%-2s-%-2s%10.3lf%10.3lf\n", bond2[i].at1, bond2[i].at2, bond2[i].force, bond2[i].len); } fprintf(fpout, "\n"); fprintf(fpout, "ANGLE\n"); for(i=0;i 0) fprintf(fpout, "%-2s-%-2s-%-2s%10.3lf%10.3lf\n", angle2[i].at3, angle2[i].at2, angle2[i].at1, angle2[i].force, angle2[i].angle * 180.0/PI); else fprintf(fpout, "%-2s-%-2s-%-2s%10.3lf%10.3lf\n", angle2[i].at1, angle2[i].at2, angle2[i].at3, angle2[i].force, angle2[i].angle * 180.0/PI); } fprintf(fpout, "\n"); fprintf(fpout, "DIHE\n"); for(i=0;i 0) { if(suc == 1) fprintf(fpout, "%-2s-%-2s-%-2s-%-2s%4d%15.9lf%15.3lf%15.3lf\n", tor2[i].at4, tor2[i].at3, tor2[i].at2, tor2[i].at1, 1, tor2[i].force, tor2[i].phase*180/PI, -tor2[i].periodicity); else fprintf(fpout, "%-2s-%-2s-%-2s-%-2s%4d%15.9lf%15.3lf%15.3lf\n", tor2[i].at4, tor2[i].at3, tor2[i].at2, tor2[i].at1, 1, tor2[i].force, tor2[i].phase*180/PI, tor2[i].periodicity); } else { if(suc == 1) fprintf(fpout, "%-2s-%-2s-%-2s-%-2s%4d%15.9lf%15.3lf%15.3lf\n", tor2[i].at1, tor2[i].at2, tor2[i].at3, tor2[i].at4, 1, tor2[i].force, tor2[i].phase*180/PI, -tor2[i].periodicity); else fprintf(fpout, "%-2s-%-2s-%-2s-%-2s%4d%15.9lf%15.3lf%15.3lf\n", tor2[i].at1, tor2[i].at2, tor2[i].at3, tor2[i].at4, 1, tor2[i].force, tor2[i].phase*180/PI, tor2[i].periodicity); } } fprintf(fpout, "\n"); fprintf(fpout, "IMPROPER\n"); if(improper_flag == 1) { for(i=0;i= ttnum2) break; suc2 = 0; if(strcmp(tor2[i].at1, tor2[i+1].at1) == 0 && strcmp(tor2[i].at2, tor2[i+1].at2) == 0 && strcmp(tor2[i].at3, tor2[i+1].at3) == 0 && strcmp(tor2[i].at4, tor2[i+1].at4) == 0) suc2 = 1; if(suc2 == 0) { if(strcmp(tor2[i].at1, tor2[i+1].at4) == 0 && strcmp(tor2[i].at2, tor2[i+1].at3) == 0 && strcmp(tor2[i].at3, tor2[i+1].at2) == 0 && strcmp(tor2[i].at4, tor2[i+1].at1) == 0) suc2 = 1; } /* as i and i+1 belong to the same torsional angle, their periodicity must be different*/ for(j= begin; j<= i; j++) if(tor2[j].periodicity == tor[tor2[i+1].id].periodicity) { suc2 = 0; break; } if(suc2 == 0) break; i++; tor2[i].flag = 1; tor2[i].force = tor[tor2[i].id].force; tor2[i].phase = tor[tor2[i].id].phase; tor2[i].periodicity= tor[tor2[i].id].periodicity; } } } /* for(i=0;i 0) { strcpy(tmpchar, atseq[m]); strcpy(atseq[m], atseq[n]); strcpy(atseq[n], tmpchar); } strcpy(tor2[i].at1, atseq[0]); strcpy(tor2[i].at2, atseq[1]); strcpy(tor2[i].at4, atseq[2]); suc = 1; for(j=0;j