Bsoft  2.1.0
Bernard's software package
rwFSC_XML.cpp
Go to the documentation of this file.
1 
9 #ifdef HAVE_XML
10 #include "rwxml.h"
11 #endif
12 
13 #include "ps_plot.h"
14 #include "utilities.h"
15 
16 #define FSCX "Resolution (A-1)"
17 #define FSCY "Correlation Coefficient"
18 
19 // Declaration of the global variables
20 extern int verbose; // Output verbosity
21 
22 #ifdef HAVE_XML
23 
32 Bplot* xml_read_fsc(Bstring &filename, Bstring &xsdfile)
33 {
34  if ( verbose & VERB_PROCESS )
35  cout << "# Reading XML file: " << filename << endl;
36 
37  xmlDocPtr doc = xmlParseFile(filename.c_str());
38 
39  if ( xsdfile.length() ) xml_validate(doc, xsdfile);
40 
41  xmlNodePtr fsc_node = xmlDocGetRootElement(doc);
42  xmlNodePtr coor_node;
43 
44  if ( fsc_node == NULL ) {
45  error_show(filename.c_str(), __FILE__, __LINE__);
46  xmlFreeDoc(doc);
47  return NULL;
48  }
49 
50  if ( xmlStrcmp(fsc_node->name, (const xmlChar *) "fsc") ) {
51  cerr << "Error: The document " << filename << " is not an FSC curve!" << endl;
52  xmlFreeDoc(doc);
53  return NULL;
54  }
55 
56  int i, ncol(2), nrow(0);
57 
58  for ( nrow=0, coor_node = fsc_node->xmlChildrenNode; coor_node; coor_node = coor_node->next )
59  if ( !xmlStrcmp(coor_node->name, BAD_CAST "coordinate") ) nrow++;
60 
61  Bstring title = (char *) xmlGetProp(fsc_node, BAD_CAST "title");
62  Bplot* plot = new Bplot(1, nrow, ncol);
63  plot->title(title);
64  plot->page(0).title(title);
65  plot->page(0).columns(ncol);
66  for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
67  plot->page(0).column(0).label((char *) xmlGetProp(fsc_node, BAD_CAST "xaxis"));
68  plot->page(0).column(1).label((char *) xmlGetProp(fsc_node, BAD_CAST "yaxis"));
69 // plot->page(0).axis(1).min(0);
70 // plot->page(0).axis(1).max(1/hi_res);
71 // plot->page(0).axis(1).inc(0.1/hi_res);
72  plot->page(0).axis(1).flags(1);
73  plot->page(0).axis(1).label("Resolution(A)");
74  plot->page(0).axis(3).min(0);
75  plot->page(0).axis(3).max(1);
76  plot->page(0).axis(3).inc(0.1);
77  plot->page(0).column(1).type(2);
78  plot->page(0).column(0).axis(1);
79  plot->page(0).column(1).axis(3);
80 
81  for ( i=0, coor_node = fsc_node->xmlChildrenNode; coor_node; coor_node = coor_node->next ) {
82  if ( !xmlStrcmp(coor_node->name, BAD_CAST "coordinate") ) {
83  (*plot)[i] = xml_get_real(coor_node, "x");
84  (*plot)[nrow+i] = xml_get_real(coor_node, "y");
85  i++;
86  }
87  }
88 
89  xmlFreeDoc(doc);
90 
91  xmlCleanupParser();
92 
93  return plot;
94 }
95 
104 Bplot* xml_read_fsc(Bstring &filename)
105 {
106  Bstring xsdfile;
107  return xml_read_fsc(filename, xsdfile);
108 }
109 
122 void xml_write_fsc(Bstring& filename, Bplot* plot)
123 {
124  long n, i, j, k, cx, cy, nrows = plot->rows();
125  Bstring filename1(filename);
126 
127  xmlDocPtr doc;
128  xmlNodePtr node;
129  xmlNodePtr fsc_node;
130  xmlNodePtr coor_node;
131 
132  for ( n=0; n<plot->pages(); n++ ) {
133  if ( plot->pages() > 1 ) filename1 = filename.pre_rev('.') + Bstring(n+1, "_%03ld.xml");
134  if ( verbose & VERB_PROCESS )
135  cout << "# Writing an XML file: " << filename1 << endl << endl;
136  cx = plot->page(n).column(0).number();
137  cy = plot->page(n).column(1).number();
138  doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
139  node = xmlNewDocPI(doc, BAD_CAST "xml-stylesheet",
140  BAD_CAST "href=\"Bsoft_micrograph.xsl\" type=\"text/xsl\"");
141  xmlDocSetRootElement(doc, node);
142  fsc_node = xmlNewNode(NULL, BAD_CAST "fsc");
143  xmlAddSibling(node, fsc_node);
144  xmlNewProp(fsc_node, BAD_CAST "title", BAD_CAST "FSC Plot");
145  xmlNewProp(fsc_node, BAD_CAST "xaxis", BAD_CAST FSCX);
146  xmlNewProp(fsc_node, BAD_CAST "yaxis", BAD_CAST FSCY);
147  for ( i=cx*nrows, j=cy*nrows, k=0; k<nrows; i++, j++, k++ ) {
148  coor_node = xmlNewChild(fsc_node, NULL, BAD_CAST "coordinate", NULL);
149  node = xml_set_real(coor_node, "x", (*plot)[i], "%g");
150  node = xml_set_real(coor_node, "y", (*plot)[j], "%g");
151  }
152  xmlSaveFormatFile(filename1.c_str(), doc, 1);
153  xmlFreeDoc(doc);
154  }
155 }
156 
157 #else
158 
168 {
169  if ( verbose & VERB_PROCESS )
170  cout << "# Reading XML file: " << filename << endl;
171 
172  ifstream fx;
173  fx.open(filename.c_str());
174  if ( fx.fail() ) return NULL;
175 
176  int i, j, ncol(2), nrow(0);
177  float x, y;
178  char aline[MAXLINELEN];
179 
180  while ( !fx.eof() ) {
181  fx.getline(aline, MAXLINELEN);
182  if ( strstr(aline, "<coordinate>") ) nrow++;
183  }
184 
185  Bstring title = "FSC Plot";
186  Bplot* plot = new Bplot(1, nrow, ncol);
187  plot->title(title);
188  plot->page(0).title(title);
189  plot->page(0).columns(ncol);
190  for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
191  plot->page(0).column(0).label(FSCX);
192  plot->page(0).column(1).label(FSCY);
193 // plot->page(0).axis(1).min(0);
194 // plot->page(0).axis(1).max(1/hi_res);
195 // plot->page(0).axis(1).inc(0.1/hi_res);
196  plot->page(0).axis(1).flags(1);
197  plot->page(0).axis(1).label("Resolution(A)");
198  plot->page(0).axis(3).min(0);
199  plot->page(0).axis(3).max(1);
200  plot->page(0).axis(3).inc(0.1);
201  plot->page(0).column(1).type(2);
202  plot->page(0).column(0).axis(1);
203  plot->page(0).column(1).axis(3);
204 
205  fx.seekg(0, ios::beg);
206 
207  for ( i=0, j=nrow; !fx.eof(); ) {
208  fx.getline(aline, MAXLINELEN);
209  if ( sscanf(aline, "<x>%f</x>", &x) ) (*plot)[i] = x;
210  if ( sscanf(aline, "<y>%f</y>", &y) ) { (*plot)[j] = y; i++; j++; }
211  }
212 
213  fx.close();
214 
215  return plot;
216 }
217 
218 void xml_write_fsc(Bstring& filename, Bplot* plot)
219 {
220  long n, i, j, k, cx, cy, nrows = plot->rows();
221  Bstring filename1(filename);
222  ofstream fx;
223 
224  for ( n=0; n<plot->pages(); n++ ) {
225  if ( plot->pages() > 1 ) filename1 = filename.pre_rev('.') + Bstring(n+1, "_%03ld.xml");
226  cx = plot->page(n).column(0).number();
227  cy = plot->page(n).column(1).number();
228  fx.open(filename1.c_str());
229  fx << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
230  fx << "<fsc title=\"FSC Plot\" xaxis=\"" << FSCX << "\" yaxis=\"" << FSCY << "\">" << endl;
231  for ( i=cx*nrows, j=cy*nrows, k=0; k<nrows; i++, j++, k++ ) {
232  fx << "<coordinate>" << endl;
233  fx << "\t<x>" << (*plot)[i] << "</x>" << endl;
234  fx << "\t<y>" << (*plot)[j] << "</y>" << endl;
235  fx << "</coordinate>" << endl;
236  }
237  fx << "</fsc>" << endl;
238  fx.close();
239  }
240 }
241 
242 #endif
243 
244 
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
verbose
int verbose
rwxml.h
Reads and writes micrograph XML files.
Baxis::flags
long flags()
Definition: Bplot.h:38
Bstring
Definition: Bstring.h:29
Bplot::rows
void rows(long n)
Definition: Bplot.h:212
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bplot
Definition: Bplot.h:146
xml_get_real
double xml_get_real(xmlNodePtr parent, const char *tag)
Bcolumn::type
long type()
Definition: Bplot.h:66
FSCY
#define FSCY
Definition: rwFSC_XML.cpp:17
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bcolumn::axis
long axis()
Definition: Bplot.h:68
ps_plot.h
Header file for postscript output functions.
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bpage::title
Bstring & title()
Definition: Bplot.h:100
xml_validate
int xml_validate(const xmlDocPtr doc, Bstring &xsdfile)
Baxis::inc
double inc()
Definition: Bplot.h:34
FSCX
#define FSCX
Definition: rwFSC_XML.cpp:16
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Bplot::title
Bstring & title()
Definition: Bplot.h:194
Baxis::min
double min()
Definition: Bplot.h:30
Baxis::label
Bstring & label()
Definition: Bplot.h:28
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bplot::pages
void pages(long n)
Definition: Bplot.h:206
xml_write_fsc
void xml_write_fsc(Bstring &filename, Bplot *plot)
Definition: rwFSC_XML.cpp:218
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
xml_read_fsc
Bplot * xml_read_fsc(Bstring &filename)
Reads an XML file with an FSC curve.
Definition: rwFSC_XML.cpp:167
xml_set_real
xmlNodePtr xml_set_real(xmlNodePtr parent, const char *tag, double value, const char *format)
Bcolumn::number
long number()
Definition: Bplot.h:61
Baxis::max
double max()
Definition: Bplot.h:32
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105