Bsoft  2.1.0
Bernard's software package
rwmodel_cmm.cpp
Go to the documentation of this file.
1 
9 #include "rwmodel.h"
10 #include "file_util.h"
11 #include "linked_list.h"
12 #include "utilities.h"
13 #include <fstream>
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 struct TagValue {
22 } ;
23 
24 TagValue* tag_value_parse(char* string)
25 {
26  TagValue* tvlist = NULL;
27  TagValue* tv = NULL;
28 
29  Bstring s(string);
30  s = s.within('<', '>');
31 
32  Bstring* slist = s.split();
33  Bstring* sp;
34 
35  for ( sp = slist; sp; sp = sp->next ) {
36  tv = (TagValue *) add_item((char **) &tv, sizeof(TagValue));
37  if ( !tvlist ) tvlist = tv;
38 // cout << "sp = " << *sp << endl;
39  if ( sp->contains("=") ) {
40  tv->tag = sp->pre('=');
41  tv->value = sp->post('\"');
42  while ( tv->value.index('\"', 0) < 0 ) { sp = sp->next; tv->value += " " + *sp; }
43  tv->value = tv->value.pre('\"');
44  } else tv->tag = *sp;
45  }
46 
47  string_kill(slist);
48 
49  return tvlist;
50 }
51 
53 {
54  TagValue* tv2;
55 
56  while ( tv ) {
57  tv2 = tv->next;
58  tv->tag = 0;
59  tv->value = 0;
60  delete[] (char *)tv;
61  tv = tv2;
62  }
63 
64  return 0;
65 }
66 
73 {
74  int i;
75  Bmodel* model = NULL;
76  Bmodel* mp = NULL;
77  Bcomptype* ct = NULL;
78  Bcomponent* comp = NULL;
79  Bcomponent* comp1 = NULL;
80  Bcomponent* comp2 = NULL;
81  Blink* link = NULL;
82  Bstring id, path;
83  Bstring* filename;
84  ifstream fmod;
85  char aline[1024];
86  TagValue* tvlist = NULL;
87  TagValue* tv = NULL;
88 
89  for ( i=1, filename = file_list; filename; filename = filename->next, i++ ) {
90  if ( verbose & VERB_LABEL )
91  cout << "Reading file: " << *filename << endl;
92  fmod.open(filename->c_str());
93  if ( fmod.fail() ) return NULL;
94  path = filename->pre_rev('/');
95  while ( fmod.getline(aline, 1024) ) {
96  if ( verbose & VERB_DEBUG )
97  cout << "DEBUG read_model_chimera: " << aline << endl;
98  tvlist = tag_value_parse(aline);
99  if ( verbose & VERB_DEBUG )
100  cout << "DEBUG read_model_chimera: tag=" << tvlist->tag << endl;
101  if ( tvlist->tag == "marker_set" ) {
102  link = NULL;
103  comp = NULL;
104  mp = model_add(&mp, "1");
105  if ( !model ) model = mp;
106  for ( tv = tvlist; tv; tv = tv->next ) if ( tv->value.length() ) {
107  if ( verbose & VERB_DEBUG )
108  cout << "DEBUG read_model_chimera: tag=" << tvlist->tag << " value=" << tv->value << endl;
109  if ( tv->tag == "name" ) mp->id = tv->value;
110  if ( tv->tag == "type" ) mp->type_id = tv->value;
111  if ( tv->tag == "hand" ) mp->hand = tv->value.integer();
112  if ( tv->tag == "symmetry" ) mp->symmetry = tv->value;
113  if ( tv->tag == "file" ) mp->fmap = find_file(tv->value, path);
114  if ( tv->tag == "img_num" ) mp->img_num = tv->value.integer();
115  if ( tv->tag == "fom" ) mp->fom = tv->value.real();
116  if ( tv->tag == "select" ) mp->sel = tv->value.integer();
117  }
118  if ( verbose & VERB_DEBUG )
119  cout << "DEBUG read_model_chimera: model id = " << mp->id << endl;
120  } else if ( tvlist->tag == "comment" ) {
121  while ( fmod.getline(aline, 1024) && strncmp(aline, "</comment>", 10) ) {
122  mp->comment += aline;
123  mp->comment += "\n";
124  }
125  } else if ( tvlist->tag == "type" ) {
126  for ( tv = tvlist; tv; tv = tv->next )
127  if ( tv->tag == "id" ) id = tv->value;
128  ct = model_add_type_by_id(mp, id);
129  if ( !mp->type ) mp->type = ct;
130  for ( tv = tvlist; tv; tv = tv->next ) {
131  if ( tv->tag == "file" ) ct->fmod = tv->value;
132  if ( tv->tag == "num" ) ct->num = tv->value.integer();
133  if ( tv->tag == "mass" ) ct->mass = tv->value.real();
134  if ( tv->tag == "fom" ) ct->fom = tv->value.real();
135  if ( tv->tag == "select" ) ct->sel = tv->value.integer();
136  }
137  } else if ( tvlist->tag == "marker" ) {
138  for ( tv = tvlist; tv; tv = tv->next )
139  if ( tv->tag == "id" ) id = tv->value;
140  comp = component_add(&comp, id);
141  if ( !mp->comp ) mp->comp = comp;
142  for ( tv = tvlist; tv; tv = tv->next ) {
143  if ( tv->tag == "type" )
144  comp->type = model_add_type_by_id(mp, tv->value);
145  if ( tv->tag == "x" ) comp->loc[0] = tv->value.real();
146  if ( tv->tag == "y" ) comp->loc[1] = tv->value.real();
147  if ( tv->tag == "z" ) comp->loc[2] = tv->value.real();
148  if ( tv->tag == "vx" ) comp->view[0] = tv->value.real();
149  if ( tv->tag == "vy" ) comp->view[1] = tv->value.real();
150  if ( tv->tag == "vz" ) comp->view[2] = tv->value.real();
151  if ( tv->tag == "va" ) comp->view[3] = tv->value.real()*M_PI/180.0;
152  if ( tv->tag == "radius" ) comp->radius = tv->value.real();
153  if ( tv->tag == "r" ) comp->color[0] = tv->value.real();
154  if ( tv->tag == "g" ) comp->color[1] = tv->value.real();
155  if ( tv->tag == "b" ) comp->color[2] = tv->value.real();
156  if ( tv->tag == "a" ) comp->color[3] = tv->value.real();
157  if ( tv->tag == "density" ) comp->density = tv->value.real();
158  if ( tv->tag == "fom" ) comp->fom = tv->value.real();
159  if ( tv->tag == "select" ) comp->sel = tv->value.integer();
160  }
161 // if ( comp->type.length() ) model_add_type_by_id(mp, comp->type);
162  } else if ( tvlist->tag == "link" ) {
163  comp1 = comp2 = NULL;
164  for ( tv = tvlist; tv; tv = tv->next ) {
165  if ( tv->tag == "id1" )
166  for ( comp = mp->comp; comp; comp = comp->next )
167  if ( comp->id == tv->value ) comp1 = comp;
168  if ( tv->tag == "id2" )
169  for ( comp = mp->comp; comp; comp = comp->next )
170  if ( comp->id == tv->value ) comp2 = comp;
171  }
172  link = link_add(&link, comp1, comp2, 0, 1);
173  if ( !mp->link ) mp->link = link;
174  for ( tv = tvlist; tv; tv = tv->next ) {
175  if ( tv->tag == "radius" ) link->radius = tv->value.real();
176  if ( tv->tag == "r" ) link->color[0] = tv->value.real();
177  if ( tv->tag == "g" ) link->color[1] = tv->value.real();
178  if ( tv->tag == "b" ) link->color[2] = tv->value.real();
179  if ( tv->tag == "a" ) link->color[3] = tv->value.real();
180  if ( tv->tag == "fom" ) link->fom = tv->value.real();
181  if ( tv->tag == "select" ) link->sel = tv->value.integer();
182  }
183  }
184  tag_value_kill(tvlist);
185  }
186  fmod.close();
187  }
188 
189  return model;
190 }
191 
199 {
200  int n;
201  Bmodel* mp = NULL;
202  Bcomptype* ct = NULL;
203  Bcomponent* comp = NULL;
204  Blink* link = NULL;
205  Bstring onename;
206 
207  ofstream fmod;
208 
209  for ( n=0, mp = model; mp; mp = mp->next, n++ ) {
210  if ( model->next )
211  onename = filename.pre_rev('.') + Bstring(n+1, "_%04d.") + filename.post_rev('.');
212  else
213  onename = filename;
214  fmod.open(onename.c_str());
215  if ( fmod.fail() ) return -1;
216  fmod << "<?xml version=\"1.0\"?>" << endl;
217  fmod << "<marker_set name=\"" << mp->id << "\" type=\"" << mp->type_id
218  << "\" hand=\"" << mp->hand << "\" symmetry=\"" << mp->symmetry
219  << "\" file=\"" << mp->fmap << "\" img_num=\"" << mp->img_num
220  << "\" fom=\"" << mp->fom << "\" select=\"" << mp->sel << "\">" << endl;
221  fmod << "<comment>" << endl << mp->comment << endl << "</comment>" << endl;
222  for ( ct = mp->type; ct; ct = ct->next ) {
223  fmod << "<type id=\"" << ct->id << "\" file=\"" << ct->fmod
224  << "\" num=\"" << ct->num << "\" mass=\"" << ct->mass
225  << "\" fom=\"" << ct->fom << "\" select=\"" << ct->sel << "\"/>" << endl;
226  }
227  for ( comp = mp->comp; comp; comp = comp->next ) {
228  fmod << "<marker id=\"" << comp->id << "\" type=\"" << comp->type->id
229  << "\" x=\"" << comp->loc[0] << "\" y=\"" << comp->loc[1]
230  << "\" z=\"" << comp->loc[2] << "\" vx=\"" << comp->view[0]
231  << "\" vy=\"" << comp->view[1] << "\" vz=\"" << comp->view[2]
232  << "\" va=\"" << comp->view.angle()*180.0/M_PI
233  << "\" r=\"" << comp->color[0] << "\" g=\"" << comp->color[1]
234  << "\" b=\"" << comp->color[2] << "\" a=\"" << comp->color[3]
235  << "\" radius=\"" << comp->radius << "\" density=\"" << comp->density
236  << "\" fom=\"" << comp->fom << "\" select=\"" << comp->sel << "\"/>" << endl;
237  }
238  for ( link = mp->link; link; link = link->next ) {
239  fmod << "<link id1=\"" << link->comp[0]->id << "\" id2=\"" << link->comp[1]->id
240  << "\" r=\"" << link->color[0] << "\" g=\"" << link->color[1]
241  << "\" b=\"" << link->color[2] << "\" a=\"" << link->color[3]
242  << "\" radius=\"" << link->radius << "\" fom=\"" << link->fom
243  << "\" select=\"" << link->sel << "\"/>" << endl;
244  }
245  fmod << "</marker_set>" << endl;
246  fmod.close();
247  }
248 
249  return n;
250 }
251 
252 
tag_value_parse
TagValue * tag_value_parse(char *string)
Definition: rwmodel_cmm.cpp:24
Bmodel::fom
float fom
Definition: rwmodel.h:128
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Bcomponent
Definition: rwmodel.h:51
Bmodel::hand
int hand
Definition: rwmodel.h:123
TagValue::tag
Bstring tag
Definition: rwmodel_cmm.cpp:20
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
rwmodel.h
Header file for reading and writing atomic model files.
Bstring::index
long index(const char c)
Definition: Bstring.h:102
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
file_util.h
Header file for file checking functions.
Bcomptype::sel
int sel
Definition: rwmodel.h:34
Bstring
Definition: Bstring.h:29
TagValue::value
Bstring value
Definition: rwmodel_cmm.cpp:21
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
tag_value_kill
int tag_value_kill(TagValue *tv)
Definition: rwmodel_cmm.cpp:52
Bstring::integer
long integer()
Definition: Bstring.cpp:547
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:270
TagValue::next
TagValue * next
Definition: rwmodel_cmm.cpp:19
Bcomptype
Definition: rwmodel.h:25
link_add
Blink * link_add(Blink **link, Bcomponent *comp1, Bcomponent *comp2, double length, double radius)
Adds a component link to a linked list.
Definition: rwmodel.cpp:318
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
Bstring::real
double real()
Definition: Bstring.cpp:553
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Bcomptype::mass
float mass
Definition: rwmodel.h:32
Bmodel::link
Blink * link
Definition: rwmodel.h:133
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Bmodel::id
Bstring id
Definition: rwmodel.h:121
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bmodel::sel
int sel
Definition: rwmodel.h:129
Bmodel
Definition: rwmodel.h:119
Bcomponent::color
RGBA< float > color
Definition: rwmodel.h:63
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bstring::within
Bstring within(char c1, char c2)
Definition: Bstring.cpp:450
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bstring::length
long length() const
Definition: Bstring.cpp:263
write_model_chimera
int write_model_chimera(Bstring &filename, Bmodel *model)
Writes Chimera marker model parameters.
Definition: rwmodel_cmm.cpp:198
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomptype::num
int num
Definition: rwmodel.h:30
Bcomponent::fom
float fom
Definition: rwmodel.h:65
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Bcomptype::id
Bstring id
Definition: rwmodel.h:27
model
Bmodel * model
Definition: bshow.cpp:34
read_model_chimera
Bmodel * read_model_chimera(Bstring *file_list)
Reads Chimera marker model parameters.
Definition: rwmodel_cmm.cpp:72
Bstring::next
Bstring * next
Definition: Bstring.h:31
find_file
Bstring find_file(Bstring filename, Bstring path, int flag=0)
Searches for a file within or without the given path.
Definition: file_util.cpp:54
linked_list.h
Header file for generalized linked list functions.
Bstring::split
Bstring * split()
Definition: Bstring.cpp:605
model_add
Bmodel * model_add(Bmodel **model, Bstring &id)
Adds a model to a linked list.
Definition: rwmodel.cpp:181
model_add_type_by_id
Bcomptype * model_add_type_by_id(Bmodel *model, Bstring &id)
Add a type to a model if the identifier does not exist already.
Definition: rwmodel.cpp:414
Bmodel::type
Bcomptype * type
Definition: rwmodel.h:131
Bcomptype::next
Bcomptype * next
Definition: rwmodel.h:26
Bcomponent::radius
float radius
Definition: rwmodel.h:62
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bcomptype::fom
float fom
Definition: rwmodel.h:33
Bcomponent::density
float density
Definition: rwmodel.h:64
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
View::angle
double angle()
Definition: View.h:98
TagValue
Definition: rwmodel_cmm.cpp:18
Bmodel::comment
Bstring comment
Definition: rwmodel.h:126
Bmodel::symmetry
Bstring symmetry
Definition: rwmodel.h:124
Bmodel::type_id
Bstring type_id
Definition: rwmodel.h:122
Bcomponent::sel
int sel
Definition: rwmodel.h:66
verbose
int verbose