-
Notifications
You must be signed in to change notification settings - Fork 0
/
psf_file.c
178 lines (149 loc) · 3.88 KB
/
psf_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdlib.h>
#include <string.h>
#include "psf_file.h"
#define PSF_RECORD_LENGTH 160
/*
* Read in the beginning of the bond/angle/dihed/etc information,
* but don't read in the data itself. Returns the number of the record type
* for the molecule. If error, returns (-1).
*/
int psf_start_block(FILE *file, const char *blockname) {
char inbuf[PSF_RECORD_LENGTH+2];
int nrec = -1;
/* keep reading the next line until a line with blockname appears */
do {
if(inbuf != fgets(inbuf, PSF_RECORD_LENGTH+1, file)) {
/* EOF encountered with no blockname line found ==> error, return (-1) */
return (-1);
}
if(strlen(inbuf) > 0 && strstr(inbuf, blockname))
nrec = atoi(inbuf);
} while (nrec == -1);
return nrec;
}
/* return # of atoms, or negative if error */
int psf_start_atoms(FILE *file) {
char inbuf[PSF_RECORD_LENGTH+2];
int natom = 0;
/* skip comments; get number of atoms */
/* Taken from VMD's ReadPSF */
do {
if (inbuf != fgets(inbuf, PSF_RECORD_LENGTH+1, file)) {
/* EOF with no NATOM */
return -1;
}
if (strlen(inbuf) > 0) {
if (!strstr(inbuf, "REMARKS")) {
if (strstr(inbuf, "NATOM")) {
natom = atoi(inbuf);
}
}
}
} while (!natom);
return natom;
}
int psf_get_atom(FILE *f, char *name, char *atype, char *resname,
char *segname, char *resid, double *q, double *m) {
char inbuf[PSF_RECORD_LENGTH+2];
int i,num, read_count;
if(inbuf != fgets(inbuf, PSF_RECORD_LENGTH+1, f)) {
return(-1);
}
read_count = sscanf(inbuf, "%d %s %s %s %s %s %lf %lf",
&num, segname, resid, resname, name, atype, q, m);
if (read_count != 8) {
fprintf(stderr,"BAD ATOM LINE IN PSF FILE:\n: %s\n", inbuf);
return -1;
}
if (sscanf(atype, "%d", &i) > 0) {
fprintf(stderr, "PSF file is in CHARMM format; XPLOR format required.\n");
return -1;
}
return num;
}
int psf_get_bonds(FILE *f, int n, int *bonds) {
char inbuf[PSF_RECORD_LENGTH+2];
char *bondptr = NULL;
int i=0;
while (i<n) {
if((i % 4) == 0) {
/* must read next line */
if(!fgets(inbuf,PSF_RECORD_LENGTH+2,f)) {
/* early EOF encountered */
break;
}
bondptr = inbuf;
}
if((bonds[2*i] = atoi(bondptr)) < 1)
break;
bondptr += 8;
if((bonds[2*i+1] = atoi(bondptr)) < 1)
break;
bondptr += 8;
i++;
}
return (i != n);
}
int psf_get_angles(FILE *f, int n, int *angles) {
char inbuf[PSF_RECORD_LENGTH+2];
char *bondptr = NULL;
int i=0;
while (i<n) {
if((i % 3) == 0) {
/* must read next line */
if(!fgets(inbuf,PSF_RECORD_LENGTH+2,f)) {
/* early EOF encountered */
break;
}
bondptr = inbuf;
}
if((angles[3*i] = atoi(bondptr)) < 1)
break;
bondptr += 8;
if((angles[3*i+1] = atoi(bondptr)) < 1)
break;
bondptr += 8;
if((angles[3*i+2] = atoi(bondptr)) < 1)
break;
bondptr += 8;
i++;
}
return (i != n);
}
int psf_get_dihedrals(FILE *f, int n, int *dihedrals) {
char inbuf[PSF_RECORD_LENGTH+2];
char *bondptr = NULL;
int i=0;
while (i<n) {
if((i % 2) == 0) {
/* must read next line */
if(!fgets(inbuf,PSF_RECORD_LENGTH+2,f)) {
/* early EOF encountered */
break;
}
bondptr = inbuf;
}
if((dihedrals[4*i] = atoi(bondptr)) < 1)
break;
bondptr += 8;
if((dihedrals[4*i+1] = atoi(bondptr)) < 1)
break;
bondptr += 8;
if((dihedrals[4*i+2] = atoi(bondptr)) < 1)
break;
bondptr += 8;
if((dihedrals[4*i+3] = atoi(bondptr)) < 1)
break;
bondptr += 8;
i++;
}
return (i != n);
}
int psf_get_impropers(FILE *f, int n, int *impropers) {
/* Same format */
return psf_get_dihedrals(f, n, impropers);
}
int psf_get_cmaps(FILE *f, int n, int *cmaps) {
/* Same format */
return psf_get_dihedrals(f, 2*n, cmaps);
}