Bsoft  2.1.0
Bernard's software package
rwWAH.cpp
Go to the documentation of this file.
1 
9 #include "rwWAH.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 
28 int readWAH(Bstring& filename, Bmolgroup *molgroup)
29 {
30  if ( verbose & VERB_DEBUG )
31  cout << "DEBUG readWAH: filename=" << filename << endl;
32 
33  ifstream fwah(filename.c_str());
34  if ( fwah.fail() ) {
35  cerr << "Error: File " << filename << " not opened!" << endl;
36  return -1;
37  }
38 
39  int atomnum=0, resnum, prev_resnum = -1, n = 0, nmol = 0;
40  char aline[MAXLINELEN] = "A";
41  char restype[20], atomtype[20];
42 
43  Bmolecule* mol = molecule_add(&molgroup->mol, aline);
44  Bresidue* res = NULL;
45  Batom* atom = NULL;
46 
47  if ( verbose )
48  cout << "Title: " << mol->id << endl;
49 
50  while ( !fwah.eof() ) {
51  fwah.getline(aline, MAXLINELEN);
52  strncpy(restype, aline + 3, 4);
53  resnum = get_integer(aline + 7, 4);
54  strncpy(atomtype, aline + 11, 4);
55  restype[4] = 0; // Residue names only 4 characters
56  atomtype[4] = 0; // Atom names only 4 characters
57  if ( n < 1 || resnum != prev_resnum ) {
58  if ( verbose & VERB_DEBUG )
59  cout << "DEBUG readWAH: resnum=" << resnum << " restype=" << restype << endl;
60  if ( mol->res ) res = residue_add(&res, restype);
61  else res = residue_add(&mol->res, restype);
62  res->num = resnum;
63  prev_resnum = resnum;
64  }
65  if ( verbose & VERB_DEBUG )
66  cout << "DEBUG readWAH: atomnum=" << atomnum << " atomtype=" << atomtype << endl;
67  if ( !res->atom ) atom = atom_add(&res->atom, atomtype);
68  else atom = atom_add(&atom, atomtype);
69  atom->num = ++atomnum;
70  atom->coord[0] = get_float(aline + 15, 10);
71  atom->coord[1] = get_float(aline + 25, 10);
72  atom->coord[2] = get_float(aline + 35, 10);
73  atom->q = get_float(aline + 45, 10);
74  atom->b = get_float(aline + 55, 10);
75  atom->sel = 1; /* All atoms read selected */
76  n++;
77  }
78 
79  if ( n ) nmol = 1;
80  else nmol = -1;
81 
82  fwah.close();
83 
84  return nmol;
85 }
86 
99 int writeWAH(Bstring& filename, Bmolgroup *molgroup)
100 {
101  if ( verbose & VERB_DEBUG )
102  cout << "DEBUG writeWAH: filename=" << filename << endl;
103 
104  if ( !molgroup ) return -1;
105 
106  ofstream fwah(filename.c_str());
107  if ( fwah.fail() ) {
108  cerr << "Error: File " << filename << " not opened!" << endl;
109  return -1;
110  }
111 
112  long nw = 0;
113  int nmol = 0;
114  char string[MAXLINELEN];
115 
116  if ( verbose & VERB_DEBUG )
117  cout << "New file name: " << filename << endl;
118 
119  Bmolecule* mol;
120  Bresidue* res;
121  Batom* atom;
122 
123  for ( mol = molgroup->mol; mol; mol = mol->next ) {
124  nw = 0;
125  for ( res = mol->res; res; res = res->next ) {
126  for ( atom = res->atom; atom; atom = atom->next ) {
127  if ( atom->sel ) {
128  snprintf(string, MAXLINELEN, " %-4s%4d%-4s%10.5f%10.5f%10.5f%10.5f%10.5f\n",
129  res->type, res->num, atom->type,
130  atom->coord[0], atom->coord[1], atom->coord[2], atom->q, atom->b);
131  fwah << string;
132  nw++;
133  }
134  }
135  }
136  if ( nw > 0 ) nmol++;
137  }
138 
139  fwah.close();
140 
141  return nmol;
142 }
143 
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
atom_add
Batom * atom_add(Batom **atom, const char *type)
Adds an atom to a linked list.
Definition: rwmolecule.cpp:152
residue_add
Bresidue * residue_add(Bresidue **res, const char *type)
Adds a residue to a linked list.
Definition: rwmolecule.cpp:113
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
writeWAH
int writeWAH(Bstring &filename, Bmolgroup *molgroup)
Writes a Wayne Hendrickson coordinate file.
Definition: rwWAH.cpp:99
Bstring
Definition: Bstring.h:29
verbose
int verbose
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Batom::num
int num
Definition: rwmolecule.h:26
get_integer
int get_integer(char *ptr, int len)
Converts a defined length string into an integer.
Definition: utilities.cpp:423
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Batom::q
double q
Definition: rwmolecule.h:31
Batom::next
Batom * next
Definition: rwmolecule.h:25
Bresidue::num
int num
Definition: rwmolecule.h:51
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
rwWAH.h
Header file for reading and writing Wayne Hendrickson coordinate files.
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
Batom::type
char type[8]
Definition: rwmolecule.h:28
Batom::b
double b
Definition: rwmolecule.h:31
utilities.h
Header file for general utilities.
Bresidue::type
char type[6]
Definition: rwmolecule.h:53
get_float
float get_float(char *ptr, int len)
Converts a defined length string into a floating point number.
Definition: utilities.cpp:446
Batom::sel
int sel
Definition: rwmolecule.h:34
Bresidue
Definition: rwmolecule.h:49
Bmolgroup
Definition: rwmolecule.h:170
molecule_add
Bmolecule * molecule_add(Bmolecule **mol, char *name)
Adds a molecule to a linked list.
Definition: rwmolecule.cpp:76
readWAH
int readWAH(Bstring &filename, Bmolgroup *molgroup)
Reads a Wayne Hendrickson coordinate file.
Definition: rwWAH.cpp:28
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
Batom
Definition: rwmolecule.h:24
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110