Bsoft  2.1.0
Bernard's software package
rwmgSerialEM.cpp
Go to the documentation of this file.
1 
9 #include "rwmgSerialEM.h"
10 #include "mg_img_proc.h"
11 #include "mg_tomography.h"
12 #include "rwmg.h"
13 #include "rwimg.h"
14 #include "mdoc.h"
15 #include "linked_list.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 
44 int read_project_serialem(Bstring& filename, Bproject* project, int flag)
45 {
46  if ( filename.length() < 1 ) {
47  cerr << "No filenames given to create a project!" << endl;
48  exit(-1);
49  }
50 
51  int err(0);
52 
53  if ( verbose & VERB_DEBUG )
54  cout << "DEBUG read_project_serialem: reading " << filename << endl;
55 
56  MDOCparser parser(filename.str());
57  JSvalue root = parser.parse();
58 
59  if ( verbose ) {
60  cout << "Reading " << filename << endl;
61  cout << "Number of sections: " << root["Sections"].size() << endl;
62  }
63 
64 // cout << root << endl;
65 // cout << root["ImageFile"].value() << endl;
66 
67  Bstring path, base, file, imgfile, text;
68  long img_num;
69  double axis_angle(0);
70 
71  if ( filename.contains("/") ) path = filename.pre_rev('/');
72 
73  if ( verbose & VERB_DEBUG )
74  cout << "DEBUG read_project_serialem: path " << path << endl;
75 
76  imgfile = root["ImageFile"].value();
77  if ( imgfile.contains("/") ) imgfile = imgfile.post_rev('/');
78  if ( imgfile.contains("\\") ) imgfile = imgfile.post_rev('\\');
79  base = imgfile.base();
80 
81  if ( access(imgfile.c_str(), R_OK) )
82  imgfile = path + "/" + imgfile;
83 
84  double volt = root["Voltage"].real();
85  double Cs(2.0), amp(0.07);
86 
87  JSvalue& sections = root["Sections"];
88 
89  Bfield* field = field_add(&project->field, base);
90  Bstring mg_id;
91  Bmicrograph* mg = NULL;
92  Bimage* p = NULL;
93 
94  for ( auto it = sections.begin(); it != sections.end(); ++it ) {
95  JSvalue& obj = *it;
96 // cout << obj << endl;
97  if ( obj.exists("ZValue") ) {
98  img_num = obj["ZValue"].integer();
99  if ( obj.exists("SubFramePath") ) {
100  imgfile = obj["SubFramePath"].value();
101  if ( imgfile.contains("/") ) imgfile = imgfile.post_rev('/');
102  if ( imgfile.contains("\\") ) imgfile = imgfile.post_rev('\\');
103  mg_id = imgfile.base();
104  if ( access(imgfile.c_str(), R_OK) )
105  imgfile = path + "/" + imgfile;
106  if ( access(imgfile.c_str(), R_OK) == 0 )
107  p = read_img(imgfile, 0, -1);
108  } else {
109  mg_id = base + Bstring(img_num+1, "_%03d");
110  if ( access(imgfile.c_str(), R_OK) == 0 )
111  p = read_img(imgfile, 0, img_num);
112  }
113  if ( !p ) {
114  cerr << "Error: Image file " << imgfile << " not read!" << endl;
115  error_show("read_project_serialem", __FILE__, __LINE__);
116  return -1;
117  }
118  if ( p->sizeZ() > 1 ) p->slices_to_images();
119  mg = micrograph_add(&mg, mg_id);
120  if ( !field->mg ) field->mg = mg;
121 // cout << "//" << obj["ZValue"] << "//" << endl;
122  mg->block = img_num;
123  if ( obj.exists("SubFramePath") ) {
124  mg->img_num = 0;
125  mg->fframe = imgfile;
126  } else {
127  mg->img_num = img_num;
128  mg->fmg = imgfile;
129  }
130  mg->pixel_size = p->sampling(0);
131  if ( obj.exists("PixelSpacing") )
132  mg->pixel_size[0] = mg->pixel_size[1] = obj["PixelSpacing"].real();
133  mg->origin = p->size()/2;
134  if ( obj.exists("TiltAngle") ) mg->tilt_angle = obj["TiltAngle"].real()*M_PI/180.0;
135  mg->tilt_axis = axis_angle;
136  mg->mark_radius = 10;
137  mg->intensity = p->image->average();
138  if ( obj.exists("MinMaxMean") ) {
139  JSvalue& mmm = obj["MinMaxMean"];
140  double sc = obj["CountsPerElectron"].real();
141  mg->intensity = mmm[2].real()/sc;
142  }
143  mg->magnification = obj["Magnification"].real();
144  mg->ctf = new CTFparam(volt, Cs, amp);
145  mg->ctf->defocus_average(-obj["TargetDefocus"].real());
146  if ( verbose & VERB_FULL ) {
147  cout << "Creating micrograph " << mg_id << ", with " << p->images() << " frames" << endl;
148  cout << "Pixel size: " << mg->pixel_size << endl;
149  cout << "Origin: " << mg->origin << endl;
150  cout << "Tilt angle: " << mg->tilt_angle*180.0/M_PI << endl;
151  cout << "Intensity: " << mg->intensity << endl;
152  }
153  delete p;
154  } if ( obj.exists("T") ) {
155  text = obj["T"].value();
156  if ( text.contains("SerialEM") ) {
157  project->comment = "# " + text + "\n";
158  } else if ( text.contains("Tilt axis angle") ) {
159  axis_angle = text.post('=').real();
160  axis_angle = (axis_angle - 90)*M_PI/180.0;
161  }
162  }
163  }
164 
167 
168  return err;
169 }
170 
171 
180 {
181  int err(0);
182 
183 // err += write_imod_xf(semfile, project);
184 
185  cerr << "Error: Not implemented yet!" << endl;
186 
187  return err;
188 }
Bsub_image::average
double average()
Definition: Bimage.h:119
project_mg_tilt_to_matrix
int project_mg_tilt_to_matrix(Bproject *project)
Sets the matrix from the tilt and axis angles for each micrograph.
Definition: mg_tomography.cpp:2177
Bmicrograph
Definition: mg_processing.h:201
project_sort_by_tilt
int project_sort_by_tilt(Bproject *project)
Sorts the micrographs in each field by tilt angle.
Definition: mg_select.cpp:1613
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
rwmgSerialEM.h
Converts between SerialEM MDOC files and a micrograph parameter file.
verbose
int verbose
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstring
Definition: Bstring.h:29
field_add
Bfield * field_add(Bfield **field, Bstring &field_id)
Adds a field-of-view parameter structure to a linked list.
Definition: mg_processing.cpp:121
read_project_serialem
int read_project_serialem(Bstring &filename, Bproject *project, int flag)
Creates a project structure using SerialEM MDOC parameters.
Definition: rwmgSerialEM.cpp:44
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bproject::field
Bfield * field
Definition: mg_processing.h:346
base
string base(string &filename)
Definition: string_util.cpp:28
JSvalue::real
double & real()
Definition: json.h:149
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:220
Bmicrograph::img_num
int img_num
Definition: mg_processing.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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
JSvalue::begin
vector< JSvalue >::iterator begin()
Definition: json.h:187
Bstring::real
double real()
Definition: Bstring.cpp:553
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Bfield
Definition: mg_processing.h:260
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:207
MDOCparser
Definition: mdoc.h:18
Bimage::images
long images()
Definition: Bimage.h:504
project
Bproject * project
Definition: bshow.cpp:33
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
rwmg.h
Header file for reading and writing micrograph parameters.
Bstring::str
string str()
Definition: Bstring.h:42
JSvalue::end
vector< JSvalue >::iterator end()
Definition: json.h:188
MDOCparser::parse
JSvalue parse()
Definition: mdoc.h:88
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
JSvalue::exists
bool exists(string tag)
Definition: json.h:221
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bmicrograph::magnification
double magnification
Definition: mg_processing.h:213
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:219
Bimage
General image parameter class.
Definition: Bimage.h:177
Bproject
Definition: mg_processing.h:336
JSvalue
Definition: json.h:53
JSvalue::value
string & value()
Definition: json.h:134
mg_img_proc.h
Header file for image processing from micrograph structures.
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
M_PI
#define M_PI
Definition: utilities.h:63
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
write_project_serialem
int write_project_serialem(Bstring &filename, Bproject *project)
Creates SerialEM MDOC files from a project structure.
Definition: rwmgSerialEM.cpp:179
CTFparam
Definition: ctf.h:28
rwimg.h
Header file for 2D and 3D image I/O.
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:241
Bmicrograph::intensity
double intensity
Definition: mg_processing.h:217
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
Bmicrograph::block
int block
Definition: mg_processing.h:205
micrograph_add
Bmicrograph * micrograph_add(Bmicrograph **mg, Bstring &mg_id)
Adds a micrograph parameter structure to a linked list.
Definition: mg_processing.cpp:154
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bstring::base
Bstring base()
Definition: Bstring.cpp:516
JSvalue::integer
long & integer()
Definition: json.h:142
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bproject::comment
Bstring comment
Definition: mg_processing.h:339
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
JSvalue::size
size_t size()
Definition: json.h:114
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
mdoc.h
mg_tomography.h
Header file for single particle image processing in Bsoft.