Bsoft  2.1.0
Bernard's software package
rwmodel_xml.cpp
Go to the documentation of this file.
1 
9 #ifdef HAVE_XML
10 #include "rwxml.h"
11 #endif
12 
13 #include "rwmodel.h"
14 #include "model_links.h"
15 #include "model_util.h"
16 #include "model_tags.h"
17 #include "file_util.h"
18 #include "utilities.h"
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
28 #if defined(HAVE_XML)
29 Bmodel* read_model_xml(Bstring* file_list)
30 {
31  if ( !file_list ) {
32  error_show("No file names found!", __FILE__, __LINE__);
33  return NULL;
34  }
35 
36  // Get the list of filenames
37  long i;
38  Bstring* thisfile = NULL;
39 
40  Bmodel* model = NULL;
41  Bmodel* mp = NULL;
42  Bcomptype* comptype = NULL;
43  Bcomponent* comp = NULL;
44  Bcomponent* comp2 = NULL;
45  Blink* link = NULL;
46  Bstring id, path;
47 
48  xmlDocPtr doc;
49  xmlNodePtr root_node;
50  xmlNodePtr comment_node;
51  xmlNodePtr model_node;
52  xmlNodePtr node;
53 
54  if ( verbose & VERB_DEBUG_XML ) {
55  cout << "DEBUG read_xml: XML filenames: " << endl;
56  for ( thisfile = file_list; thisfile; thisfile = thisfile->next )
57  cout << " " << *thisfile;
58  cout << endl;
59  }
60 
61  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
63  doc = xmlParseFile(thisfile->c_str());
64  if ( doc == NULL ) return NULL;
65 
66  path = thisfile->pre_rev('/');
67 
68  if ( verbose & VERB_PROCESS )
69  cout << "# Reading XML file: " << *thisfile << endl;
70 
71  root_node = xmlDocGetRootElement(doc);
72  if ( root_node == NULL ) {
73  error_show(thisfile->c_str(), __FILE__, __LINE__);
74  xmlFreeDoc(doc);
75  return NULL;
76  }
77 
78 // cout << "XML root name: " << root_node->name << endl;
79 
80  if ( xmlStrcmp(root_node->name, BAD_CAST "model_file") ) {
81  cerr <<"Error: The document " << *thisfile << " is not a model file!" << endl;
82  xmlFreeDoc(doc);
83  return NULL;
84  }
85 
86 // xmlDocDump(stderr, doc);
87 // xmlElemDump(stderr, doc, node);
88 
89  for ( comment_node = root_node->xmlChildrenNode; comment_node &&
90  xmlStrcmp(comment_node->name, BAD_CAST COMMENT); comment_node = comment_node->next ) ;
91 
92  for ( i=0, model_node = root_node->xmlChildrenNode; model_node; model_node = model_node->next )
93  if ( !xmlStrcmp(model_node->name, BAD_CAST MODEL) ) {
94  id = 0;
95  id = (char *) xmlGetProp(model_node, BAD_CAST ID);
96  if ( id.length() < 1 ) id = Bstring(i, "%d");
97  mp = model_add(&mp, id);
98  if ( !model ) model = mp;
99  if ( !xmlStrcmp(model_node->name, BAD_CAST COMMENT) )
100  mp->comment = (char *) xmlNodeGetContent(model_node);
101  else if ( comment_node )
102  mp->comment = (char *) xmlNodeGetContent(comment_node);
103  if ( mp->id.length() < 1 ) mp->id = Bstring(i, "%d");
104  mp->type_id = xml_get_string(model_node, MODEL_TYPE);
105  mp->symmetry = xml_get_string(model_node, MODEL_SYM);
106  mp->fmap = find_file(xml_get_string(model_node, MODEL_MAP_FILENAME), path);
107  mp->hand = xml_get_integer(model_node, MODEL_HAND);
108  mp->img_num = xml_get_integer(model_node, MODEL_MAP_NUMBER);
109  mp->fom = xml_get_real(model_node, MODEL_FOM);
110  mp->sel = xml_get_integer(model_node, MODEL_SELECT);
111  comptype = NULL;
112  comp = NULL;
113  for ( node = model_node->xmlChildrenNode; node; node = node->next ) {
114  if ( !xmlStrcmp(node->name, BAD_CAST COMPTYPE) ) {
115  id = (char *) xmlGetProp(node, BAD_CAST ID);
116  comptype = model_add_type_by_id(mp, id);
117  if ( !mp->type ) mp->type = comptype;
118  comptype->fmod = xml_get_string(node, COMPTYPE_FILENAME);
119  comptype->num = xml_get_integer(node, COMPTYPE_NUMBER);
120  comptype->mass = xml_get_real(node, COMPTYPE_MASS);
121  comptype->fom = xml_get_real(node, COMPTYPE_FOM);
122  comptype->sel = xml_get_integer(node, COMPTYPE_SELECT);
123  }
124  if ( !xmlStrcmp(node->name, BAD_CAST COMPONENT) ) {
125  id = (char *) xmlGetProp(node, BAD_CAST ID);
126  comp = component_add(&comp, id);
127  if ( !mp->comp ) mp->comp = comp;
128  id = xml_get_string(node, COMPONENT_TYPE_ID);
129  comp->type = model_add_type_by_id(mp, id);
130  comp->loc[0] = xml_get_real(node, COMPONENT_X);
131  comp->loc[1] = xml_get_real(node, COMPONENT_Y);
132  comp->loc[2] = xml_get_real(node, COMPONENT_Z);
133  comp->view[0] = xml_get_real(node, COMPONENT_VIEW_X);
134  comp->view[1] = xml_get_real(node, COMPONENT_VIEW_Y);
135  comp->view[2] = xml_get_real(node, COMPONENT_VIEW_Z);
136  comp->view[3] = xml_get_real(node, COMPONENT_VIEW_ANGLE) * M_PI/180.0;
137  comp->radius = xml_get_real(node, COMPONENT_RADIUS);
138  comp->color[0] = xml_get_real(node, COMPONENT_RED);
139  comp->color[1] = xml_get_real(node, COMPONENT_GREEN);
140  comp->color[2] = xml_get_real(node, COMPONENT_BLUE);
141  comp->color[3] = xml_get_real(node, COMPONENT_ALPHA);
142  comp->density = xml_get_real(node, COMPONENT_DENSITY);
143  comp->fom = xml_get_real(node, COMPONENT_FOM);
144  comp->sel = xml_get_integer(node, COMPONENT_SELECT);
145  }
146  }
147  link = NULL;
148  for ( node = model_node->xmlChildrenNode; node; node = node->next ) {
149  if ( !xmlStrcmp(node->name, BAD_CAST COMPLINK) ) {
150  id = xml_get_string(node, COMPLINK_1);
151  for ( comp = mp->comp; comp && comp->id != id; comp = comp->next ) ;
152  if ( !comp )
153  cerr << "Error: Component " << id << " not found! (1)" << endl;
154  id = xml_get_string(node, COMPLINK_2);
155  for ( comp2 = mp->comp; comp2 && comp2->id != id; comp2 = comp2->next ) ;
156  if ( !comp2 )
157  cerr << "Error: Component " << id << " not found! (2)" << endl;
158  link = link_add(&link, comp, comp2, 1, 1);
159  if ( !mp->link ) mp->link = link;
160  link->angle = xml_get_real(node, COMPLINK_ANGLE) * M_PI/180.0;
161  link->radius = xml_get_real(node, COMPLINK_RADIUS);
162  link->color[0] = xml_get_real(node, COMPLINK_RED);
163  link->color[1] = xml_get_real(node, COMPLINK_GREEN);
164  link->color[2] = xml_get_real(node, COMPLINK_BLUE);
165  link->color[3] = xml_get_real(node, COMPLINK_ALPHA);
166  link->fom = xml_get_real(node, COMPLINK_FOM);
167  link->sel = xml_get_integer(node, COMPLINK_SELECT);
168  }
169  }
170  for ( link=mp->link; link; link=link->next )
171  link->length = link->comp[0]->loc.distance(link->comp[1]->loc);
172  }
173 
174  xmlFreeDoc(doc);
175 
176  xmlCleanupParser();
177  }
178 
179 // model_list_setup_links(model);
181 
182  return model;
183 }
184 #else
186 {
187  cerr << "Error: XML files are not supported!" << endl << endl;
188 
189  return NULL;
190 }
191 #endif
192 
199 #if defined(HAVE_XML)
200 int write_model_xml(Bstring& filename, Bmodel* model)
201 {
202  int i, err = 0;
203 
204  xmlNodePtr comment_node;
205  xmlNodePtr model_node;
206  xmlNodePtr type_node;
207  xmlNodePtr comp_node;
208  xmlNodePtr link_node;
209 
210  Bmodel* mp = NULL;
211  Bcomptype* comptype = NULL;
212  Bcomponent* comp = NULL;
213  Blink* link = NULL;
214 
215  if ( verbose & VERB_PROCESS )
216  cout << "# Writing file: " << filename << endl;
217 
218  xmlDocPtr doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
219  if ( doc == NULL ) {
220  cerr << "Error: The XML document tree was not created!" << endl;
221  return -1;
222  }
223 
224  xmlNodePtr root_node = xmlNewDocPI(doc, BAD_CAST "xml-stylesheet",
225  BAD_CAST "href=\"Bsoft_model.xsl\" type=\"text/xsl\"");
226 
227  root_node = xmlNewNode(NULL, BAD_CAST "model_file");
228  xmlDocSetRootElement(doc, root_node);
229 
230  comment_node = xmlNewChild(root_node, NULL, BAD_CAST COMMENT, NULL);
231  xmlNodeSetContent(comment_node, BAD_CAST model->comment.c_str());
232 
233  for ( i=1, mp = model; mp; mp = mp->next, i++ ) {
234  model_node = xmlNewChild(root_node, NULL, BAD_CAST MODEL, NULL);
235  if ( mp->id.length() < 1 ) mp->id = Bstring(i, "%d");
236  xmlNewProp(model_node, BAD_CAST ID, BAD_CAST mp->id.c_str());
237  if ( mp->type_id.length() > 0 )
238  xmlNewChild(model_node, NULL, BAD_CAST MODEL_TYPE, BAD_CAST mp->type_id.c_str());
239  if ( mp->symmetry.length() > 0 )
240  xmlNewChild(model_node, NULL, BAD_CAST MODEL_SYM, BAD_CAST mp->symmetry.c_str());
241  xml_set_integer(model_node, MODEL_HAND, mp->hand, "%d");
242  if ( mp->fmap.length() > 0 )
243  xmlNewChild(model_node, NULL, BAD_CAST MODEL_MAP_FILENAME, BAD_CAST mp->fmap.c_str());
244  xml_set_integer(model_node, MODEL_MAP_NUMBER, mp->img_num, "%d");
245  xml_set_real(model_node, MODEL_FOM, mp->fom, "%g");
246  xml_set_integer(model_node, MODEL_SELECT, mp->sel, "%d");
247  for ( comptype = mp->type; comptype; comptype = comptype->next ) {
248  type_node = xmlNewChild(model_node, NULL, BAD_CAST COMPTYPE, NULL);
249  xmlNewProp(type_node, BAD_CAST ID, BAD_CAST comptype->id.c_str());
250  xmlNewChild(type_node, NULL, BAD_CAST COMPTYPE_FILENAME, BAD_CAST comptype->fmod.c_str());
251  xml_set_integer(type_node, COMPTYPE_NUMBER, comptype->num, "%4d");
252  xml_set_real(type_node, COMPTYPE_MASS, comptype->mass, "%12.2f");
253  xml_set_real(type_node, COMPTYPE_FOM, comptype->fom, "%7.4f");
254  xml_set_integer(type_node, COMPTYPE_SELECT, comptype->sel, "%4d");
255  }
256  for ( comp = mp->comp; comp; comp = comp->next ) {
257  comp_node = xmlNewChild(model_node, NULL, BAD_CAST COMPONENT, NULL);
258  xmlNewProp(comp_node, BAD_CAST ID, BAD_CAST comp->id.c_str());
259  xmlNewChild(comp_node, NULL, BAD_CAST COMPONENT_TYPE_ID, BAD_CAST comp->type->id.c_str());
260  xml_set_real(comp_node, COMPONENT_X, comp->loc[0], "%8.3f");
261  xml_set_real(comp_node, COMPONENT_Y, comp->loc[1], "%8.3f");
262  xml_set_real(comp_node, COMPONENT_Z, comp->loc[2], "%8.3f");
263  xml_set_real(comp_node, COMPONENT_VIEW_X, comp->view[0], "%7.4lf");
264  xml_set_real(comp_node, COMPONENT_VIEW_Y, comp->view[1], "%7.4lf");
265  xml_set_real(comp_node, COMPONENT_VIEW_Z, comp->view[2], "%7.4lf");
266  xml_set_real(comp_node, COMPONENT_VIEW_ANGLE, comp->view[3] * 180.0/M_PI, "%7.2lf");
267  xml_set_real(comp_node, COMPONENT_RADIUS, comp->radius, "%8.3f");
268  xml_set_real(comp_node, COMPONENT_RED, comp->color[0], "%5.3f");
269  xml_set_real(comp_node, COMPONENT_GREEN, comp->color[1], "%5.3f");
270  xml_set_real(comp_node, COMPONENT_BLUE, comp->color[2], "%5.3f");
271  xml_set_real(comp_node, COMPONENT_ALPHA, comp->color[3], "%5.3f");
272  xml_set_real(comp_node, COMPONENT_DENSITY, comp->density, "%8.3f");
273  xml_set_real(comp_node, COMPONENT_FOM, comp->fom, "%8.3f");
274  xml_set_integer(comp_node, COMPONENT_SELECT, comp->sel, "%4d");
275  }
276  for ( link = mp->link; link; link = link->next ) {
277  link_node = xmlNewChild(model_node, NULL, BAD_CAST COMPLINK, NULL);
278  xmlNewChild(link_node, NULL, BAD_CAST COMPLINK_1, BAD_CAST link->comp[0]->id.c_str());
279  xmlNewChild(link_node, NULL, BAD_CAST COMPLINK_2, BAD_CAST link->comp[1]->id.c_str());
280  xml_set_real(link_node, COMPLINK_ANGLE, link->angle * 180.0/M_PI, "%7.2f");
281  xml_set_real(link_node, COMPLINK_RADIUS, link->radius, "%8.3f");
282  xml_set_real(link_node, COMPLINK_RED, link->color[0], "%5.3f");
283  xml_set_real(link_node, COMPLINK_GREEN, link->color[1], "%5.3f");
284  xml_set_real(link_node, COMPLINK_BLUE, link->color[2], "%5.3f");
285  xml_set_real(link_node, COMPLINK_ALPHA, link->color[3], "%5.3f");
286  xml_set_real(link_node, COMPLINK_FOM, link->fom, "%8.3f");
287  xml_set_integer(link_node, COMPLINK_SELECT, link->sel, "%4d");
288  }
289  }
290 
291  if ( err == 0 ) err = xmlSaveFormatFile(filename.c_str(), doc, 1);
292 
293  xmlFreeDoc(doc);
294 
295  return err;
296 }
297 #else
299 {
300  cerr << "Error: XML files are not supported!" << endl << endl;
301 
302  return 0;
303 }
304 #endif
305 
xml_get_string
string xml_get_string(xmlNodePtr parent, const char *tag)
MODEL_SELECT
#define MODEL_SELECT
Definition: model_tags.h:25
Bmodel::fom
float fom
Definition: rwmodel.h:128
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
COMPLINK_ANGLE
#define COMPLINK_ANGLE
Definition: model_tags.h:62
Bcomponent
Definition: rwmodel.h:51
rwxml.h
Reads and writes micrograph XML files.
COMPLINK
#define COMPLINK
Definition: model_tags.h:58
Bmodel::hand
int hand
Definition: rwmodel.h:123
COMPONENT_DENSITY
#define COMPONENT_DENSITY
Definition: model_tags.h:43
COMPONENT_ALPHA
#define COMPONENT_ALPHA
Definition: model_tags.h:42
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
rwmodel.h
Header file for reading and writing atomic model files.
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
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:270
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
COMPLINK_1
#define COMPLINK_1
Definition: model_tags.h:60
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
xml_set_integer
xmlNodePtr xml_set_integer(xmlNodePtr parent, const char *tag, long value, const char *format)
xml_get_real
double xml_get_real(xmlNodePtr parent, const char *tag)
write_model_xml
int write_model_xml(Bstring &filename, Bmodel *model)
Writes XML model parameters.
Definition: rwmodel_xml.cpp:298
COMPTYPE
#define COMPTYPE
Definition: model_tags.h:48
Bcomptype::mass
float mass
Definition: rwmodel.h:32
Bmodel::link
Blink * link
Definition: rwmodel.h:133
length
double length(vector< T > v)
Definition: Vector3.h:530
COMPONENT_VIEW_X
#define COMPONENT_VIEW_X
Definition: model_tags.h:34
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
COMPONENT_SELECT
#define COMPONENT_SELECT
Definition: model_tags.h:45
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
models_process
long models_process(Bmodel *model, long(modfunc)(Bmodel *))
Process a list of models using the specified function.
Definition: model_util.cpp:36
MODEL_SYM
#define MODEL_SYM
Definition: model_tags.h:21
COMPONENT_VIEW_ANGLE
#define COMPONENT_VIEW_ANGLE
Definition: model_tags.h:37
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
COMMENT
#define COMMENT
Definition: mg_tags.h:21
COMPONENT_Z
#define COMPONENT_Z
Definition: model_tags.h:33
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
MODEL
#define MODEL
Definition: model_tags.h:17
COMPLINK_RED
#define COMPLINK_RED
Definition: model_tags.h:64
COMPTYPE_SELECT
#define COMPTYPE_SELECT
Definition: model_tags.h:55
Bmodel::id
Bstring id
Definition: rwmodel.h:121
COMPLINK_ALPHA
#define COMPLINK_ALPHA
Definition: model_tags.h:67
verbose
int verbose
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
COMPLINK_RADIUS
#define COMPLINK_RADIUS
Definition: model_tags.h:63
detect_and_fix_carriage_return
int detect_and_fix_carriage_return(const char *filename)
Detects carriage returns in text files and converts them to new-lines.
Definition: file_util.cpp:377
COMPONENT_Y
#define COMPONENT_Y
Definition: model_tags.h:32
Bmodel::sel
int sel
Definition: rwmodel.h:129
COMPTYPE_FILENAME
#define COMPTYPE_FILENAME
Definition: model_tags.h:50
VERB_DEBUG_XML
#define VERB_DEBUG_XML
Definition: utilities.h:118
ID
#define ID
Definition: mg_tags.h:22
COMPONENT_X
#define COMPONENT_X
Definition: model_tags.h:31
COMPONENT_VIEW_Z
#define COMPONENT_VIEW_Z
Definition: model_tags.h:36
Bmodel
Definition: rwmodel.h:119
Bcomponent::color
RGBA< float > color
Definition: rwmodel.h:63
COMPONENT_FOM
#define COMPONENT_FOM
Definition: model_tags.h:44
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
COMPONENT
#define COMPONENT
Definition: model_tags.h:28
COMPONENT_VIEW_Y
#define COMPONENT_VIEW_Y
Definition: model_tags.h:35
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bstring::length
long length() const
Definition: Bstring.cpp:263
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
COMPLINK_SELECT
#define COMPLINK_SELECT
Definition: model_tags.h:69
MODEL_MAP_NUMBER
#define MODEL_MAP_NUMBER
Definition: model_tags.h:23
Bstring::next
Bstring * next
Definition: Bstring.h:31
read_model_xml
Bmodel * read_model_xml(Bstring *file_list)
Reads XML model parameters.
Definition: rwmodel_xml.cpp:185
MODEL_HAND
#define MODEL_HAND
Definition: model_tags.h:20
COMPONENT_TYPE_ID
#define COMPONENT_TYPE_ID
Definition: model_tags.h:30
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
COMPTYPE_NUMBER
#define COMPTYPE_NUMBER
Definition: model_tags.h:52
model_add
Bmodel * model_add(Bmodel **model, Bstring &id)
Adds a model to a linked list.
Definition: rwmodel.cpp:181
COMPONENT_RED
#define COMPONENT_RED
Definition: model_tags.h:39
COMPLINK_2
#define COMPLINK_2
Definition: model_tags.h:61
COMPLINK_FOM
#define COMPLINK_FOM
Definition: model_tags.h:68
MODEL_TYPE
#define MODEL_TYPE
Definition: model_tags.h:19
model_util.h
Library routines used for model processing.
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
COMPTYPE_MASS
#define COMPTYPE_MASS
Definition: model_tags.h:53
COMPTYPE_FOM
#define COMPTYPE_FOM
Definition: model_tags.h:54
Bmodel::type
Bcomptype * type
Definition: rwmodel.h:131
Bcomptype::next
Bcomptype * next
Definition: rwmodel.h:26
Bcomponent::radius
float radius
Definition: rwmodel.h:62
COMPLINK_BLUE
#define COMPLINK_BLUE
Definition: model_tags.h:66
MODEL_MAP_FILENAME
#define MODEL_MAP_FILENAME
Definition: model_tags.h:22
xml_set_real
xmlNodePtr xml_set_real(xmlNodePtr parent, const char *tag, double value, const char *format)
Bcomptype::fom
float fom
Definition: rwmodel.h:33
COMPLINK_GREEN
#define COMPLINK_GREEN
Definition: model_tags.h:65
Bcomponent::density
float density
Definition: rwmodel.h:64
COMPONENT_BLUE
#define COMPONENT_BLUE
Definition: model_tags.h:41
COMPONENT_GREEN
#define COMPONENT_GREEN
Definition: model_tags.h:40
xml_get_integer
long xml_get_integer(xmlNodePtr parent, const char *tag)
Bmodel::comment
Bstring comment
Definition: rwmodel.h:126
MODEL_FOM
#define MODEL_FOM
Definition: model_tags.h:24
Bmodel::symmetry
Bstring symmetry
Definition: rwmodel.h:124
model_tags.h
All STAR and XML file format tags for models.
COMPONENT_RADIUS
#define COMPONENT_RADIUS
Definition: model_tags.h:38
Bmodel::type_id
Bstring type_id
Definition: rwmodel.h:122
Bcomponent::sel
int sel
Definition: rwmodel.h:66