| Bsoft 2.0.5 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 JSvalue& sections = root["Sections"];
85
86 Bfield* field = field_add(&project->field, base);
87 Bstring mg_id;
88 Bmicrograph* mg = NULL;
89 Bimage* p = NULL;
90
91 for ( auto it = sections.begin(); it != sections.end(); ++it ) {
92 JSvalue& obj = *it;
93 // cout << obj << endl;
94 if ( obj.exists("ZValue") ) {
95 img_num = obj["ZValue"].integer();
96 if ( obj.exists("SubFramePath") ) {
97 imgfile = obj["SubFramePath"].value();
98 if ( imgfile.contains("/") ) imgfile = imgfile.post_rev('/');
99 if ( imgfile.contains("\\") ) imgfile = imgfile.post_rev('\\');
100 mg_id = imgfile.base();
101 if ( access(imgfile.c_str(), R_OK) )
102 imgfile = path + "/" + imgfile;
103 if ( access(imgfile.c_str(), R_OK) == 0 )
104 p = read_img(imgfile, 0, -1);
105 } else {
106 mg_id = base + Bstring(img_num+1, "_%03d");
107 if ( access(imgfile.c_str(), R_OK) == 0 )
108 p = read_img(imgfile, 0, img_num);
109 }
110 if ( !p ) {
111 cerr << "Error: Image file " << imgfile << " not read!" << endl;
112 error_show("read_project_serialem", __FILE__, __LINE__);
113 return -1;
114 }
115 if ( p->sizeZ() > 1 ) p->slices_to_images();
116 mg = micrograph_add(&mg, mg_id);
117 if ( !field->mg ) field->mg = mg;
118 // cout << "//" << obj["ZValue"] << "//" << endl;
119 mg->block = img_num;
120 if ( obj.exists("SubFramePath") ) {
121 mg->img_num = 0;
122 mg->fframe = imgfile;
123 } else {
124 mg->img_num = img_num;
125 mg->fmg = imgfile;
126 }
127 mg->pixel_size = p->sampling()[0];
128 if ( obj.exists("PixelSpacing") ) mg->pixel_size = obj["PixelSpacing"].real();
129 mg->origin = p->size()/2;
130 if ( obj.exists("TiltAngle") ) mg->tilt_angle = obj["TiltAngle"].real()*M_PI/180.0;
131 mg->tilt_axis = axis_angle;
132 mg->mark_radius = 10;
133 mg->dose = p->image->average();
134 if ( obj.exists("MinMaxMean") ) {
135 JSvalue& mmm = obj["MinMaxMean"];
136 double sc = obj["CountsPerElectron"].real();
137 mg->dose = mmm[2].real()/sc;
138 }
139 mg->magnification = obj["Magnification"].real();
140 if ( verbose & VERB_FULL ) {
141 cout << "Creating micrograph " << mg_id << ", with " << p->images() << " frames" << endl;
142 cout << "Pixel size: " << mg->pixel_size << endl;
143 cout << "Origin: " << mg->origin << endl;
144 cout << "Tilt angle: " << mg->tilt_angle*180.0/M_PI << endl;
145 // cout << "Dose: " << mg->dose << endl;
146 }
147 delete p;
148 } if ( obj.exists("T") ) {
149 text = obj["T"].value();
150 if ( text.contains("SerialEM") ) {
151 project->comment = "# " + text + "\n";
152 } else if ( text.contains("Tilt axis angle") ) {
153 axis_angle = text.post('=').real();
154 axis_angle = (axis_angle - 90)*M_PI/180.0;
155 }
156 }
157 }
158
159 project_sort_by_tilt(project);
160 project_mg_tilt_to_matrix(project);
161
162 return err;
163 }
164
165
173 int write_project_serialem(Bstring& filename, Bproject* project)
174 {
175 int err(0);
176
177 // err += write_imod_xf(semfile, project);
178
179 cerr << "Error: Not implemented yet!" << endl;
180
181 return err;
182 }
rwmg.h
Header file for reading and writing micrograph parameters.
Bstring::real
double real()
Definition: Bstring.cpp:545
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:394
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2052
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:211
JSvalue::integer
long & integer()
Definition: json.h:136
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
mg_img_proc.h
Header file for image processing from micrograph structures.
Bstring::length
long length() const
Definition: Bstring.cpp:263
write_project_serialem
int write_project_serialem(Bstring &filename, Bproject *project)
Creates SerialEM MDOC files from a project structure.
Definition: rwmgSerialEM.cpp:173
Bmicrograph::pixel_size
double pixel_size
Definition: mg_processing.h:214
project
Bproject * project
Definition: bshow.cpp:31
project_sort_by_tilt
int project_sort_by_tilt(Bproject *project)
Sorts the micrographs in each field by tilt angle.
Definition: mg_processing.cpp:3490
Bmicrograph::dose
double dose
Definition: mg_processing.h:215
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
rwmgSerialEM.h
Converts between SerialEM MDOC files and a micrograph parameter file.
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:217
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
JSvalue::real
double & real()
Definition: json.h:143
Bimage
General image parameter class.
Definition: Bimage.h:163
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:216
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Bfield
Definition: mg_processing.h:257
M_PI
#define M_PI
Definition: utilities.h:59
linked_list.h
Header file for generalized linked list functions.
base
string base(string &filename)
Definition: string_util.cpp:28
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:206
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:235
Bproject::field
Bfield * field
Definition: mg_processing.h:343
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:1960
Bmicrograph
Definition: mg_processing.h:200
mg_tomography.h
Header file for single particle image processing in Bsoft.
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:220
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
JSvalue
Definition: json.h:53
mdoc.h
JSvalue::end
vector< JSvalue >::iterator end()
Definition: json.h:182
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
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:120
Bstring::base
Bstring base()
Definition: Bstring.cpp:508
JSvalue::begin
vector< JSvalue >::iterator begin()
Definition: json.h:181
Bmicrograph::block
int block
Definition: mg_processing.h:204
Bstring
Definition: Bstring.h:29
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:205
JSvalue::exists
bool exists(string tag)
Definition: json.h:215
Bmicrograph::magnification
double magnification
Definition: mg_processing.h:212
JSvalue::value
string & value()
Definition: json.h:128
verbose
int verbose
rwimg.h
Header file for 2D and 3D image I/O.
Bsub_image::average
double average()
Definition: Bimage.h:115
Bproject::comment
Bstring comment
Definition: mg_processing.h:336
Bimage::images
long images()
Definition: Bimage.h:491
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Bstring::str
string str()
Definition: Bstring.h:42
micrograph_add
Bmicrograph * micrograph_add(Bmicrograph **mg, Bstring &mg_id)
Adds a micrograph parameter structure to a linked list.
Definition: mg_processing.cpp:461
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
Bproject
Definition: mg_processing.h:333
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
MDOCparser
Definition: mdoc.h:18