Bsoft  2.1.0
Bernard's software package
rwatomprop.cpp
Go to the documentation of this file.
1 
9 #include "rwatomprop.h"
10 #include "rwstar.h"
11 #include "mol_tags.h"
12 #include "linked_list.h"
13 #include "utilities.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 // Internal function prototypes
21 int write_atom_prop_star(Bstring& filename, Batomtype* at_first);
22 
23 /* Atomic parameters */
24 static struct atom_type {
25  char name[8]; // Atom name/symbol
26  float mass; // Atom mass
27  float oxid; // Oxidation state
28  float bond; // Bond length
29  float vdw; // VdW radius
30  float red,grn,blu; // RGB colour
31  float scat[9]; // Atom scattering factor parameters (a1,a2,a3,a4,b1,b2,b3,b4,c)
32 } atom_data[] = {
33  {"H", 1.007, 0.0, 1.0, 1.100, 1.0,1.0,1.0, {0.307,0.202,0.0,0.0,22.97,3.98,0.0,0.0,0.0}},
34  {"C", 12.0110, 0.0, 1.5, 1.548, 0.7,0.7,0.7, {1.494,0.937,0.0,0.0,23.22,3.79,0.0,0.0,0.0}},
35  {"N", 14.0067, 0.0, 1.5, 1.400, 0.0,0.0,1.0, {1.263,0.872,0.0,0.0,17.65,2.94,0.0,0.0,0.0}},
36  {"O", 15.9994, 0.0, 1.5, 1.348, 1.0,0.0,0.0, {1.085,0.822,0.0,0.0,14.17,2.40,0.0,0.0,0.0}},
37  {"P", 30.9738, 0.0, 1.5, 1.880, 1.0,0.0,1.0, {3.590,1.696,0.0,0.0,28.40,3.65,0.0,0.0,0.0}},
38  {"S", 32.0600, 0.0, 1.5, 1.808, 1.0,1.0,0.0, {3.319,1.649,0.0,0.0,23.06,3.22,0.0,0.0,0.0}},
39  {" ", 0, 0, 0, 0, 0, 0, 0, {0.0 ,0.0 ,0.0,0.0, 0.0, 0.0, 0.0,0.0,0.0}}
40 } ;
41 
48 {
49  if ( verbose & VERB_DEBUG )
50  cout << "DEBUG get_atom_properties: Initializing atomic parameters from " << filename << endl;
51 
52  Batomtype* at_curr = NULL;
53  Batomtype* at_first = NULL;
54 
55  int i, j;
56 
57  // Atom parameter file
58  Bstring atfile;
59  Bstring propfile;
60  if ( filename.length() ) atfile = filename;
61  else atfile = "atom_prop.star";
62 
63  if ( access(atfile.c_str(), R_OK) == 0 )
64  propfile = atfile;
65  else
66  propfile = parameter_file_path(atfile);
67 
68  filename = propfile;
69 
70  Bstring ext = atfile.extension();
71  if ( ext.length() ) {
72  if ( ext.contains("star") )
73  at_first = read_atom_prop_star(propfile);
74  else
75  at_first = read_atom_properties(propfile);
76  }
77 
78  if ( !at_first ) {
79  if ( verbose )
80  cout << "Atom property file " << atfile << " not opened! Using default properties" << endl;
81  for ( i=0; atom_data[i].mass && atom_data[i].vdw; i++ ) {
82  at_curr = atom_type_add(&at_curr, atom_data[i].name);
83  if ( i == 0 ) at_first = at_curr;
84  at_curr->mass = atom_data[i].mass;
85  at_curr->oxid = atom_data[i].oxid;
86  at_curr->bond = atom_data[i].bond;
87  at_curr->vdw = atom_data[i].vdw;
88  at_curr->color[0] = (char) (255*atom_data[i].red);
89  at_curr->color[1] = (char) (255*atom_data[i].grn);
90  at_curr->color[2] = (char) (255*atom_data[i].blu);
91  for ( j=0; j<4; j++ ) {
92  at_curr->sfa[j] = atom_data[i].scat[j];
93  at_curr->sfb[j] = atom_data[i].scat[j+4];
94  }
95  at_curr->sfc = atom_data[i].scat[8];
96  }
97  }
98 
99  for ( i=0, at_curr = at_first; at_curr; at_curr = at_curr->next, i++ ) ;
100 
101  if ( verbose & VERB_DEBUG )
102  cout << "DEBUG get_atom_properties: Number of atom types = " << i << endl;
103 
104  return at_first;
105 }
106 
114 {
115  int err(0);
116  Bstring ext = filename.extension();
117 
118  if ( ext.contains("star") )
119  err = write_atom_prop_star(filename, at);
120  else {
121  cerr << "Error: Extension \"" << ext << "\" not valid for parameter files!" << endl;
122  cerr << " Parameter file \"" << filename << "\" not written." << endl;
123  err = -1;
124  }
125 
126  if ( err < 0 )
127  error_show(filename.c_str(), __FILE__, __LINE__);
128 
129  return err;
130 }
131 
144 Batomtype* atom_type_add(Batomtype** atype, const char* aname)
145 {
146  Batomtype* this_atype = *atype;
147  Batomtype* new_atype = new Batomtype;
148  memset(new_atype, 0, sizeof(Batomtype));
149 
150  if ( aname ) strncpy(new_atype->name, aname, 8);
151  if ( aname ) {
152  while ( isspace(aname[0]) ) aname++;
153  strncpy(new_atype->name, aname, 6);
154  if ( isalpha(aname[0]) ) {
155  new_atype->el[0] = toupper(aname[0]);
156  if ( isalpha(aname[1]) ) {
157  if ( islower(aname[1]) ) new_atype->el[1] = aname[1];
158  }
159  }
160  }
161 
162  if ( !this_atype )
163  *atype = new_atype;
164  else {
165  while ( this_atype->next ) this_atype = this_atype->next;
166  this_atype->next = new_atype;
167  }
168 
169  return new_atype;
170 }
171 
178 {
179  if ( !atype ) return 0;
180 
181  Batomtype* atype2;
182 
183  for ( ; atype; ) {
184  atype2 = atype->next;
185  delete atype;
186  atype = atype2;
187  }
188 
189  return 0;
190 }
191 
192 
194 {
195  Bstar* star = init_star();
196 
197  if ( read_star(filename.c_str(), star) < 0 ) {
198  error_show(filename.c_str(), __FILE__, __LINE__);
199  return NULL;
200  }
201 
202  Bstar_block* block = star->block;
203  Bstar_item* item = NULL;
204  Bstring* data;
205 
206  // Find the number of atoms
207  int n = item_get_number(star, ATOM_TYPE_SYMBOL);
208 
209  if ( n < 1 ) {
210  cerr << "Error: Star to atom properties conversion unsuccessful!" << endl;
211  return NULL;
212  }
213 
214  Batomtype* at = NULL;
215  Batomtype* at_first = NULL;
216 
217  if ( verbose & VERB_DEBUG )
218  cout << "DEBUG read_atom_prop_star: Reading " << filename << endl;
219 
220  // Convert the atomic data
221  if ( n ) {
222  item = item_find(block, ATOM_TYPE_SYMBOL);
223  if ( item )
224  for ( data=item->data; data; data=data->next )
225  at = atom_type_add(&at_first, data->c_str());
226  item = item_find(block, ATOM_TYPE_NUMBER);
227  if ( item )
228  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
229  at->z = data->integer();
230  item = item_find(block, ATOM_TYPE_MASS);
231  if ( item )
232  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
233  at->mass = data->real();
234  item = item_find(block, ATOM_TYPE_OXIDATION);
235  if ( item )
236  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
237  at->oxid = data->real();
238  item = item_find(block, ATOM_TYPE_RADIUS_BOND);
239  if ( item )
240  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
241  at->bond = data->real();
242  item = item_find(block, ATOM_TYPE_RADIUS_VDW);
243  if ( item )
244  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
245  at->vdw = data->real();
246  item = item_find(block, ATOM_TYPE_SCAT_A1);
247  if ( item )
248  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
249  at->sfa[0] = data->real();
250  item = item_find(block, ATOM_TYPE_SCAT_A2);
251  if ( item )
252  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
253  at->sfa[1] = data->real();
254  item = item_find(block, ATOM_TYPE_SCAT_A3);
255  if ( item )
256  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
257  at->sfa[2] = data->real();
258  item = item_find(block, ATOM_TYPE_SCAT_A4);
259  if ( item )
260  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
261  at->sfa[3] = data->real();
262  item = item_find(block, ATOM_TYPE_SCAT_A5);
263  if ( item )
264  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
265  at->sfa[4] = data->real();
266  item = item_find(block, ATOM_TYPE_SCAT_B1);
267  if ( item )
268  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
269  at->sfb[0] = data->real();
270  item = item_find(block, ATOM_TYPE_SCAT_B2);
271  if ( item )
272  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
273  at->sfb[1] = data->real();
274  item = item_find(block, ATOM_TYPE_SCAT_B3);
275  if ( item )
276  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
277  at->sfb[2] = data->real();
278  item = item_find(block, ATOM_TYPE_SCAT_B4);
279  if ( item )
280  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
281  at->sfb[3] = data->real();
282  item = item_find(block, ATOM_TYPE_SCAT_B5);
283  if ( item )
284  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
285  at->sfb[4] = data->real();
286  item = item_find(block, ATOM_TYPE_SCAT_C);
287  if ( item )
288  for ( data=item->data, at=at_first; data && at; data=data->next, at=at->next )
289  at->sfc = data->real();
290  }
291 
292  kill_star(star);
293 
294  return at_first;
295 }
296 
297 int write_atom_prop_star(Bstring& filename, Batomtype* at_first)
298 {
299  Bstar* star = init_star();
300  Bstar_block* block;
301 
302  Batomtype* at;
303 
304  star->line_length = 120;
305 
306  // Count the number of atom types
307  int n, loop, err(0);
308  for ( n=0, at = at_first; at; at = at->next, n++ ) ;
309 
310  if ( n ) {
311  block = (Bstar_block *) add_item((char **)&star->block, sizeof(Bstar_block));
312  item_put_list(block, ATOM_TYPE_SYMBOL, (char *) at_first,
313  (char *)&at_first->name - (char *)at_first, "%s");
314  item_put_list(block, ATOM_TYPE_NUMBER, (char *) at_first,
315  (char *)&at_first->z - (char *)at_first, "%4d");
316  item_put_list(block, ATOM_TYPE_MASS, (char *) at_first,
317  (char *)&at_first->mass - (char *)at_first, "%7.3f");
318  item_put_list(block, ATOM_TYPE_OXIDATION, (char *) at_first,
319  (char *)&at_first->oxid - (char *)at_first, "%7.3f");
320  item_put_list(block, ATOM_TYPE_RADIUS_BOND, (char *) at_first,
321  (char *)&at_first->bond - (char *)at_first, "%7.3f");
322  item_put_list(block, ATOM_TYPE_RADIUS_VDW, (char *) at_first,
323  (char *)&at_first->vdw - (char *)at_first, "%7.3f");
324  item_put_list(block, ATOM_TYPE_SCAT_A1, (char *) at_first,
325  (char *)&at_first->sfa[0] - (char *)at_first, "%7.4f");
326  item_put_list(block, ATOM_TYPE_SCAT_A2, (char *) at_first,
327  (char *)&at_first->sfa[1] - (char *)at_first, "%7.4f");
328  item_put_list(block, ATOM_TYPE_SCAT_A3, (char *) at_first,
329  (char *)&at_first->sfa[2] - (char *)at_first, "%7.4f");
330  item_put_list(block, ATOM_TYPE_SCAT_A4, (char *) at_first,
331  (char *)&at_first->sfa[3] - (char *)at_first, "%7.4f");
332  item_put_list(block, ATOM_TYPE_SCAT_A5, (char *) at_first,
333  (char *)&at_first->sfa[4] - (char *)at_first, "%7.4f");
334  item_put_list(block, ATOM_TYPE_SCAT_B1, (char *) at_first,
335  (char *)&at_first->sfb[0] - (char *)at_first, "%7.4f");
336  item_put_list(block, ATOM_TYPE_SCAT_B2, (char *) at_first,
337  (char *)&at_first->sfb[1] - (char *)at_first, "%7.4f");
338  item_put_list(block, ATOM_TYPE_SCAT_B3, (char *) at_first,
339  (char *)&at_first->sfb[2] - (char *)at_first, "%7.4f");
340  item_put_list(block, ATOM_TYPE_SCAT_B4, (char *) at_first,
341  (char *)&at_first->sfb[3] - (char *)at_first, "%7.4f");
342  item_put_list(block, ATOM_TYPE_SCAT_B5, (char *) at_first,
343  (char *)&at_first->sfb[4] - (char *)at_first, "%7.4f");
344  item_put_list(block, ATOM_TYPE_SCAT_C, (char *) at_first,
345  (char *)&at_first->sfc - (char *)at_first, "%7.4f");
346  loop = item_index(block, ATOM_TYPE_SYMBOL);
347  loop_set_identifier(block, loop, 1, "atom_type");
348  }
349 
350  err = write_star(filename.c_str(), star);
351 
352  kill_star(star);
353 
354  if ( err < 0 ) return err;
355 
356  return n;
357 }
358 
360 {
361  return NULL;
362 }
363 
Bstar_item::data
Bstring * data
Definition: rwstar.h:43
Bstar_item
Definition: rwstar.h:36
Batomtype::vdw
float vdw
Definition: rwatomprop.h:27
ATOM_TYPE_RADIUS_BOND
#define ATOM_TYPE_RADIUS_BOND
Definition: mol_tags.h:26
read_atom_prop_star
Batomtype * read_atom_prop_star(Bstring &filename)
Definition: rwatomprop.cpp:193
item_put_list
int item_put_list(Bstar_block *block, const char *tag, char *list, size_t offset, const char *format)
Writes a list of string or numeric values into a data item associated with a specific data block and ...
Definition: rwstar.cpp:1436
Batomtype::name
char name[8]
Definition: rwatomprop.h:23
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
ATOM_TYPE_SCAT_A2
#define ATOM_TYPE_SCAT_A2
Definition: mol_tags.h:29
Batomtype::el
char el[4]
Definition: rwatomprop.h:22
item_index
int item_index(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1029
Bstring
Definition: Bstring.h:29
write_atom_properties
int write_atom_properties(Bstring &filename, Batomtype *at)
Writing atomic properties from parameter files.
Definition: rwatomprop.cpp:113
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Bstring::extension
Bstring extension()
Definition: Bstring.cpp:523
ATOM_TYPE_SCAT_B4
#define ATOM_TYPE_SCAT_B4
Definition: mol_tags.h:36
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Batomtype::mass
float mass
Definition: rwatomprop.h:24
ATOM_TYPE_SCAT_B1
#define ATOM_TYPE_SCAT_B1
Definition: mol_tags.h:33
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Batomtype
Definition: rwatomprop.h:19
parameter_file_path
Bstring parameter_file_path(Bstring filename)
Finds the parameter file path.
Definition: Bstring.cpp:795
loop_set_identifier
int loop_set_identifier(Bstar_block *block, int loop, int n,...)
Assigns a loop identification number to items.
Definition: rwstar.cpp:1803
Bstring::real
double real()
Definition: Bstring.cpp:553
atom_type_add
Batomtype * atom_type_add(Batomtype **atype, const char *aname)
Adds an atom type structure to a linked list.
Definition: rwatomprop.cpp:144
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Batomtype::sfa
float sfa[5]
Definition: rwatomprop.h:29
write_star
int write_star(const char *filename, Bstar *star)
Writes a STAR data base to one or more STAR format files.
Definition: rwstar.cpp:666
mol_tags.h
All STAR file format tags for molecules and molecular parameters.
write_atom_prop_star
int write_atom_prop_star(Bstring &filename, Batomtype *at_first)
Definition: rwatomprop.cpp:297
rwatomprop.h
Header file for reading atom property files.
read_atom_properties
Batomtype * read_atom_properties(Bstring &filename)
Definition: rwatomprop.cpp:359
ATOM_TYPE_RADIUS_VDW
#define ATOM_TYPE_RADIUS_VDW
Definition: mol_tags.h:27
ATOM_TYPE_SCAT_A3
#define ATOM_TYPE_SCAT_A3
Definition: mol_tags.h:30
Batomtype::z
int z
Definition: rwatomprop.h:21
ATOM_TYPE_SCAT_B5
#define ATOM_TYPE_SCAT_B5
Definition: mol_tags.h:37
utilities.h
Header file for general utilities.
Batomtype::next
Batomtype * next
Definition: rwatomprop.h:20
ATOM_TYPE_SCAT_C
#define ATOM_TYPE_SCAT_C
Definition: mol_tags.h:38
Bstring::length
long length() const
Definition: Bstring.cpp:263
Batomtype::sfb
float sfb[5]
Definition: rwatomprop.h:30
read_star
int read_star(const char *filename, Bstar *star)
Reads paramaters and data into a STAR data base from a list of files.
Definition: rwstar.cpp:224
ATOM_TYPE_SCAT_B3
#define ATOM_TYPE_SCAT_B3
Definition: mol_tags.h:35
kill_star
int kill_star(Bstar *star)
Destroys a STAR data base.
Definition: rwstar.cpp:132
Bstring::next
Bstring * next
Definition: Bstring.h:31
Batomtype::sfc
float sfc
Definition: rwatomprop.h:31
Bstar::line_length
int line_length
Definition: rwstar.h:80
Batomtype::oxid
float oxid
Definition: rwatomprop.h:25
linked_list.h
Header file for generalized linked list functions.
atom_type_kill
int atom_type_kill(Batomtype *atype)
Destroys an atom type.
Definition: rwatomprop.cpp:177
item_get_number
long item_get_number(Bstar *star, const char *tag)
Gets the number of values associated with a tag in a STAR data base.
Definition: rwstar.cpp:1134
item_find
Bstar_item * item_find(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1083
ATOM_TYPE_SYMBOL
#define ATOM_TYPE_SYMBOL
Definition: mol_tags.h:22
ATOM_TYPE_SCAT_A4
#define ATOM_TYPE_SCAT_A4
Definition: mol_tags.h:31
rwstar.h
A header file for STAR format files.
ATOM_TYPE_SCAT_A1
#define ATOM_TYPE_SCAT_A1
Definition: mol_tags.h:28
ATOM_TYPE_SCAT_B2
#define ATOM_TYPE_SCAT_B2
Definition: mol_tags.h:34
Bstar::block
Bstar_block * block
Definition: rwstar.h:82
ATOM_TYPE_SCAT_A5
#define ATOM_TYPE_SCAT_A5
Definition: mol_tags.h:32
Batomtype::color
unsigned char color[4]
Definition: rwatomprop.h:28
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
get_atom_properties
Batomtype * get_atom_properties(Bstring &filename)
Reading atomic properties from parameter files.
Definition: rwatomprop.cpp:47
ATOM_TYPE_OXIDATION
#define ATOM_TYPE_OXIDATION
Definition: mol_tags.h:24
Bstar_block
Definition: rwstar.h:55
ATOM_TYPE_MASS
#define ATOM_TYPE_MASS
Definition: mol_tags.h:25
init_star
Bstar * init_star()
Creates a STAR data base.
Definition: rwstar.cpp:111
Batomtype::bond
float bond
Definition: rwatomprop.h:26
ATOM_TYPE_NUMBER
#define ATOM_TYPE_NUMBER
Definition: mol_tags.h:23
Bstar
Definition: rwstar.h:78
verbose
int verbose