| Bsoft 2.0.5 Bernard's software package

rwGROMACS.cpp
Go to the documentation of this file.
1
9 #include "rwGROMACS.h"
10 #include "utilities.h"
11 #include <fstream>
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
31 int readGROMACS(Bstring& filename, Bmolgroup *molgroup)
32 {
33 if ( verbose & VERB_DEBUG )
34 cout << "DEBUG readGROMACS: filename=" << filename << endl;
35
36 ifstream fgro(filename.c_str());
37 if ( fgro.fail() ) {
38 cerr << "Error: File " << filename << " not opened!" << endl;
39 return -1;
40 }
41
42 size_t i;
43 long j;
44 int readflag(0), atomnum, resnum, prev_resnum = -1;
45 int n(0), natom, nmol(0);
46 char aline[MAXLINELEN];
47 char restype[20], atomtype[20];
48 Vector3<double> coord, vel;
49
50 fgro.getline(aline, MAXLINELEN);
51 aline[MAXLINELEN-1] = 0;
52
53 Bmolecule* mol = molecule_add(&molgroup->mol, aline);
54
55 if ( verbose )
56 cout << "Title: " << mol->id << endl;
57
58 fgro.getline(aline, MAXLINELEN);
59 sscanf(aline, "%d", &natom);
60
61 if ( verbose )
62 cout << "Number of atoms: " << natom << endl;
63
64 Bresidue* res = NULL;
65 Batom* atom = NULL;
66 int linenum = 2;
67
68 while ( !fgro.eof() && n < natom ) {
69 fgro.getline(aline, MAXLINELEN);
70 linenum++;
71 i = strcspn(aline,"\n");
72 if ( i > 0 ) aline[i] = ' ';
73 if ( i > 40 ) readflag = 1;
74 if ( strstr(aline, "nan") || strstr(aline, "NAN") ) {
75 cerr << "Warning: NAN at line number " << linenum << endl;
76 readflag = 0;
77 }
78 if ( readflag ) {
79 coord = 0;
80 vel = 0;
81 sscanf(aline, "%5d%5c%5c%5d%8lf%8lf%8lf%8lf%8lf%8lf",
82 &resnum, restype, atomtype, &atomnum,
83 &coord[0], &coord[1], &coord[2], &vel[0], &vel[1], &vel[2]);
84 if ( resnum < 1 ) resnum = n + 1;
85 if ( atomnum < 1 ) atomnum = n + 1;
86 restype[5] = 0; // Residue names only 5 characters
87 if ( resnum != prev_resnum ) {
88 if ( mol->res ) res = residue_add(&res, restype);
89 else res = residue_add(&mol->res, restype);
90 res->num = prev_resnum = resnum;
91 }
92 atomtype[5] = 0; // Atom names only 5 characters
93 for ( i=0, j=0; i<strlen(atomtype); i++ )
94 if ( !isspace(atomtype[i]) ) atomtype[j++] = atomtype[i];
95 atomtype[j] = 0; // Atom type string shifted to left
96 if ( !res->atom ) atom = atom_add(&res->atom, atomtype);
97 else atom = atom_add(&atom, atomtype);
98 atom->num = atomnum;
99 atom->coord = coord * 10;
100 atom->vel = vel * 10;
101 atom->q = 1;
102 atom->b = 10;
103 if ( verbose & VERB_DEBUG )
104 cout << atom->num << " " << res->num << " " << atom->coord << endl;
105
106 atom->sel = 1; /* All atoms read selected */
107 n++;
108 }
109 }
110
111 if ( n ) nmol = 1;
112 else nmol = -1;
113
114 fgro.getline(aline, MAXLINELEN);
115
116 Vector3<double> box;
117 sscanf( aline, "%lf %lf %lf", &box[0], &box[1], &box[2]);
118 if ( verbose & VERB_PROCESS )
119 cout << "Box size: " << box << " nm" << endl;
120
121 molgroup->box = box * 10;
122
123 fgro.close();
124
125 return nmol;
126 }
127
143 int writeGROMACS(Bstring& filename, Bmolgroup *molgroup)
144 {
145 if ( verbose & VERB_DEBUG )
146 cout << "DEBUG writeGROMACS: filename=" << filename << endl;
147
148 if ( !molgroup ) return -1;
149
150 ofstream fgro(filename.c_str());
151 if ( fgro.fail() ) {
152 cerr << "Error: File " << filename << " not opened!" << endl;
153 return -1;
154 }
155
156 long i, j, nw(0), natom(0);
157 char atomtype[8];
158 memset(atomtype, 0, 8);
159
160 if ( verbose & VERB_DEBUG )
161 cout << "New file name: " << filename << endl;
162
163 Bmolecule* mol;
164 Bresidue* res;
165 Batom* atom;
166
167 for ( mol = molgroup->mol; mol; mol = mol->next )
168 for ( res = mol->res; res; res = res->next )
169 for ( atom = res->atom; atom; atom = atom->next )
170 natom++;
171
172 // time_t t = time(NULL);
173 // fgro << "Gromacs file written by Bsoft: " << asctime(localtime(&t)) << endl;
174 char comment[MAXLINELEN];
175 for ( i=0; i<molgroup->comment.length() && molgroup->comment[i] != '\n' && i<80; i++ )
176 comment[i] = molgroup->comment[i];
177 comment[i++] = '\n';
178 comment[i] = 0;
179 fgro << comment;
180 fgro << " " << natom << endl;
181
182 int nmol = 0;
183 for ( mol = molgroup->mol; mol; mol = mol->next ) {
184 nw = 0;
185 for ( res = mol->res; res; res = res->next ) {
186 for ( atom = res->atom; atom; atom = atom->next ) {
187 if ( atom->sel ) {
188 memset(atomtype, ' ', 5);
189 for ( i=(long)strlen(atom->type)-1, j=4; i>=0 && j>=0; i-- )
190 if ( !isspace(atom->type[i]) ) atomtype[j--] = atom->type[i];
191 fgro << setw(5) << res->num << setw(5) << res->type <<
192 setw(5) << atomtype << setw(5) << atom->num <<
193 fixed << setprecision(3) << setw(8) << 0.1*atom->coord[0] <<
194 setw(8) << 0.1*atom->coord[1] << setw(8) << 0.1*atom->coord[2] <<
195 setprecision(4) << setw(8) << 0.1*atom->vel[0] <<
196 setw(8) << 0.1*atom->vel[1] << setw(8) << 0.1*atom->vel[2] << endl;
197 nw++;
198 }
199 }
200 }
201 if ( nw > 0 ) nmol++;
202 }
203
204 fgro << fixed << setprecision(2) << setw(9) << 0.1*molgroup->box[0] <<
205 setw(9) << 0.1*molgroup->box[1] << setw(9) << 0.1*molgroup->box[2] << endl;
206
207 fgro.close();
208
209 return nmol;
210 }
211
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
Bstring::length
long length() const
Definition: Bstring.cpp:263
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Bresidue::type
char type[6]
Definition: rwmolecule.h:53
readGROMACS
int readGROMACS(Bstring &filename, Bmolgroup *molgroup)
Reads a Gromacs coordinate file.
Definition: rwGROMACS.cpp:31
writeGROMACS
int writeGROMACS(Bstring &filename, Bmolgroup *molgroup)
Writes a Gromacs coordinate file.
Definition: rwGROMACS.cpp:143
Batom::q
double q
Definition: rwmolecule.h:31
residue_add
Bresidue * residue_add(Bresidue **res, const char *type)
Adds a residue to a linked list.
Definition: rwmolecule.cpp:113
Batom::sel
int sel
Definition: rwmolecule.h:34
Bmolgroup::box
Vector3< double > box
Definition: rwmolecule.h:184
Batom::type
char type[8]
Definition: rwmolecule.h:28
Batom::num
int num
Definition: rwmolecule.h:26
Bmolgroup
Definition: rwmolecule.h:170
Bmolgroup::comment
Bstring comment
Definition: rwmolecule.h:175
verbose
int verbose
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
molecule_add
Bmolecule * molecule_add(Bmolecule **mol, char *name)
Adds a molecule to a linked list.
Definition: rwmolecule.cpp:76
Batom::b
double b
Definition: rwmolecule.h:31
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmolecule
Definition: rwmolecule.h:107
atom_add
Batom * atom_add(Batom **atom, const char *type)
Adds an atom to a linked list.
Definition: rwmolecule.cpp:152
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Bresidue
Definition: rwmolecule.h:49
Bstring
Definition: Bstring.h:29
Batom
Definition: rwmolecule.h:24
Vector3< double >
Bresidue::num
int num
Definition: rwmolecule.h:51
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
rwGROMACS.h
Header file for reading and writing GROMACS coordinate files.
Batom::next
Batom * next
Definition: rwmolecule.h:25
Batom::vel
Vector3< double > vel
Definition: rwmolecule.h:35