Xmipp  v3.23.11-Nereus
micrograph_scissor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include <core/argsparser.h>
27 #include <core/xmipp_program.h>
28 #include <data/micrograph.h>
29 #include <core/args.h>
31 
33 {
34 private:
35 
36 protected:
37  void defineParams()
38  {
39  addUsageLine ("Extract particles from a micrograph");
40  addSeeAlsoLine("micrograph_mark");
41 
42  addParamsLine(" == General Options == ");
43  addParamsLine(" -i <input_untilted_micrograph> : From which the untilted images will be cutted");
44  addParamsLine(" alias --untilted;");
45  addParamsLine(" [--orig <original_micrograph>] : unless this parameter is specified");
46  addParamsLine(" -o <output_stack> : Name for the particle images");
47  addParamsLine(" :+ Two files will be created: output_stack with the particles in a Spider stack an output_stack.xmd with the list of image names, the micrograph they were taken from, and their coordinates");
48  addParamsLine(" alias --untiltfn;");
49  addParamsLine(" [--pos <position_file>] : file with particle coordinates");
50  addParamsLine(" alias --untiltPos;");
51  addParamsLine(" [--extractNoise <n=-1>] : extract noise particles instead of true particles");
52  addParamsLine(" : n is the number of noise particles to extract");
53  addParamsLine(" : If n=-1, then the number of noise particles is the number of coordinates in the pos file");
54  addParamsLine(" : The input posfile WILL be rewritten with the noise coordinates");
55 
56  addParamsLine(" == Processing Options == ");
57  addParamsLine(" --Xdim <window_X_dim> : In pixels");
58  addParamsLine(" [--downsampling <float=1.>] : The positions were determined with this downsampling rate");
59  addParamsLine(" [--Ydim <window_Y_dim>] : If not given Ydim=Xdim");
60  addParamsLine(" [--invert] : Invert contrast");
61  addParamsLine(" [--log] : Take logarithm (compute transmitance)");
62  addParamsLine(" [--appendToStack] : The output stack is deleted.");
63  addParamsLine(" : Use this option to add the new images to the stack");
64  addParamsLine(" [--fillBorders] : If the box is outside the micrograph, fill the missing pixels");
65  addParamsLine(" : instead of setting the whole image to blank");
66 
67  addParamsLine(" == Options for tilt pairs == ");
68  addParamsLine(" [-t <input_tilted_micrograph>] : From which the tilted images will be cutted");
69  addParamsLine(" alias --tilted;");
70  addParamsLine(" [--tiltfn <output_stack>] : Name for tilted images (Stack FileName)");
71  addParamsLine(" requires --untiltfn; ");
72  addParamsLine(" [--tiltAngles <angles_file>] : Name of the estimated tilt angles");
73  addParamsLine(" : Angle from the Y axis to the tilt axis. Both (tilted and untilted) must be cut with its corresponding angle. Cut images are rotated so that the tilt axis is parallel to Y axis.");
74  addParamsLine(" requires --tiltfn; ");
75  addParamsLine(" [--tiltPos <position_file>] : file with particle coordinates");
76  addParamsLine(" requires --tiltfn; ");
77  addParamsLine(" [--ctfparam <ctfparam>] : metadata with ctf parameters for the micrograph.");
78 
79  addExampleLine (" xmipp_micrograph_scissor -i g7107.raw --pos g7107.raw.Common.pos --oroot images --Xdim 64");
80  addExampleLine (" xmipp_micrograph_scissor --untilted Preprocessing/untilt/down1_untilt.raw --tilted Preprocessing/tilt/down1_tilt.raw --untiltfn untilt --tiltfn tilt --Xdim 60 --tiltAngles ParticlePicking/down1_untilt.raw.angles.txt --pos ParticlePicking/down1_untilt.raw.Common.pos --tiltPos ParticlePicking/down1_untilt.raw.tilted.Common.pos");
81  }
85  bool pair_mode;
86  int Ydim, Xdim;
91  bool rmStack;
93  int Nnoise;
95 
96  void readParams()
97  {
98  fn_micrograph = getParam ("-i");
99  pair_mode = checkParam("--tilted");
100  fn_out = getParam ("-o");
101  fn_pos = getParam ("--pos");
102  Xdim = getIntParam("--Xdim");
103  if (checkParam("--Ydim"))
104  Ydim = getIntParam("--Ydim");
105  else
106  Ydim = Xdim;
107 
108  compute_inverse = checkParam("--invert");
109  compute_transmitance = checkParam("--log");
110  rmStack = !checkParam("--appendToStack");
111 
112  if (!pair_mode)
113  {
114  if (checkParam("--orig"))
115  fn_orig = getParam("--orig");
116  }
117  else
118  {
119  fn_out_tilted = getParam("--tiltfn");
120  fn_tilted = getParam("--tilted");
121  fn_angles = getParam("--tiltAngles");
122  fn_tilt_pos = getParam("--tiltPos");
123  }
124  down_transform = getDoubleParam("--downsampling");
125  fillBorders = checkParam("--fillBorders");
126  extractNoise = checkParam("--extractNoise");
127  if (extractNoise)
128  {
130  Nnoise = getIntParam("--extractNoise",0);
131  }
132  }
133 public:
134  void run()
135  {
136  if (!pair_mode)
137  {
138  Micrograph m;
139  m.open_micrograph(fn_micrograph);
140  m.set_window_size(Xdim, Ydim);
141  m.read_coordinates(0, fn_pos);
142  if (down_transform != 1.)
143  m.scale_coordinates(1./down_transform);
144  m.add_label("");
145  m.set_transmitance_flag(compute_transmitance);
146  m.set_inverse_flag(compute_inverse);
147 
148  if (checkParam("--ctfparam"))
149  {
150  MetaDataVec ctfparam(getParam("--ctfparam"));
151  MDRowVec ctfRow;
152  ctfparam.getRow(ctfRow, ctfparam.firstRowId());
153  m.set_ctfparams(ctfRow);
154  }
155  m.produce_all_images(0, -1, fn_out, fn_orig, 0., rmStack, fillBorders, extractNoise, Nnoise);
156  if (extractNoise)
157  {
158  MDRowVec row=firstRow(fn_pos);
159  size_t micId=0;
161  row.getValue(MDL_MICROGRAPH_ID,micId);
162 
163  // Rewrite the input posfile with the true noise coordinates
164  MetaDataVec MD;
165  for (size_t i=0; i<m.coords.size(); i++)
166  {
167  size_t id=MD.addObject();
168  MD.setValue(MDL_XCOOR, m.coords[i].X, id);
169  MD.setValue(MDL_YCOOR, m.coords[i].Y, id);
170  MD.setValue(MDL_MICROGRAPH_ID, micId, id);
171  }
172  MD.write(fn_pos);
173  }
174  }
175  else
176  {
177  MetaDataVec auxMd;
178  // Read angles
179  double alpha_u, alpha_t, tilt_angle;
180  auxMd.read(fn_angles);
181  size_t objId = auxMd.firstRowId();
182  auxMd.getValue(MDL_ANGLE_Y, alpha_u, objId);
183  auxMd.getValue(MDL_ANGLE_Y2, alpha_t, objId);
184  auxMd.getValue(MDL_ANGLE_TILT, tilt_angle, objId);
185 
186  // Generate the images for the untilted image
187  Micrograph m;
188  m.open_micrograph(fn_micrograph);
189  m.set_window_size(Xdim, Ydim);
190  m.read_coordinates(0, fn_pos);
191  m.add_label("");
192  m.set_transmitance_flag(compute_transmitance);
193  m.set_inverse_flag(compute_inverse);
194  m.produce_all_images(0, -1, fn_out, "", alpha_u, rmStack);
195  m.close_micrograph();
196 
197  // Generate the images for the tilted image
198  Micrograph mt;
199  mt.open_micrograph(fn_tilted);
200  mt.set_window_size(Xdim, Ydim);
201  mt.read_coordinates(0, fn_tilt_pos);
202  mt.add_label("");
203  mt.set_transmitance_flag(compute_transmitance);
204  mt.set_inverse_flag(compute_inverse);
205  mt.produce_all_images(0, -1, fn_out_tilted, "", 0., rmStack);
206  mt.close_micrograph();
207  }
208  }
209 };
void set_ctfparams(const MDRowVec &ctf)
Definition: micrograph.h:153
double getDoubleParam(const char *param, int arg=0)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void read_coordinates(int label, const FileName &fn_coords)
Definition: micrograph.cpp:216
Tilting angle of an image (double,degrees)
std::vector< Particle_coords > coords
Definition: micrograph.h:118
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
void produce_all_images(int label, double minCost, const FileName &fn_root, const FileName &fn_image="", double ang=0, bool rmStack=false, bool fillBorders=false, bool extractNoise=false, int Nnoise=-1)
Definition: micrograph.cpp:317
bool containsLabel(MDLabel label) const override
std::unique_ptr< MDRow > getRow(size_t id) override
void set_window_size(int _X_window_size, int _Y_window_size)
Definition: micrograph.h:209
int add_label(const std::string &label)
Definition: micrograph.h:501
Angle between y-axis and tilt-axis (double, degrees) for tilted micrographs.
#define i
void addSeeAlsoLine(const char *seeAlso)
void open_micrograph(const FileName &fn_micrograph)
Definition: micrograph.cpp:61
T & getValue(MDLabel label)
void scale_coordinates(const double &c)
Definition: micrograph.cpp:270
const char * getParam(const char *param, int arg=0)
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
size_t firstRowId() const override
Angle between y-axis and tilt-axis (double, degrees) for untilted micrographs.
X component (int)
void addExampleLine(const char *example, bool verbatim=true)
Micrograph unique id for reference (MDL_ITEM_ID should be used for Micrographs list) ...
int mt
int m
bool getValue(MDObject &mdValueOut, size_t id) const override
void close_micrograph()
Definition: micrograph.cpp:135
void set_transmitance_flag(bool flag_value)
Definition: micrograph.h:220
bool checkParam(const char *param)
Y component (int)
MDRowVec firstRow(const FileName &fnMetadata)
void addUsageLine(const char *line, bool verbatim=false)
unsigned int randomize_random_generator()
void set_inverse_flag(bool flag_value)
Definition: micrograph.h:244
int getIntParam(const char *param, int arg=0)
void addParamsLine(const String &line)