Xmipp  v3.23.11-Nereus
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
ProgTomoExtractSubtomograms Class Reference

#include <tomo_extract_subtomograms.h>

Inheritance diagram for ProgTomoExtractSubtomograms:
Inheritance graph
[legend]
Collaboration diagram for ProgTomoExtractSubtomograms:
Collaboration graph
[legend]

Public Member Functions

void defineParams ()
 
void readParams ()
 
void createSphere (int halfboxsize)
 
void normalizeSubtomo (MultidimArray< double > &subtomo, int halfboxsize)
 
void extractSubtomoFixedSize (MultidimArray< double > &subtomoExtraction)
 
void writeSubtomo (int idx, int xcoor, int ycoor, int zcoor, size_t particleid)
 
void run ()
 
- Public Member Functions inherited from XmippProgram
const char * getParam (const char *param, int arg=0)
 
const char * getParam (const char *param, const char *subparam, int arg=0)
 
int getIntParam (const char *param, int arg=0)
 
int getIntParam (const char *param, const char *subparam, int arg=0)
 
double getDoubleParam (const char *param, int arg=0)
 
double getDoubleParam (const char *param, const char *subparam, int arg=0)
 
float getFloatParam (const char *param, int arg=0)
 
float getFloatParam (const char *param, const char *subparam, int arg=0)
 
void getListParam (const char *param, StringVector &list)
 
int getCountParam (const char *param)
 
bool checkParam (const char *param)
 
bool existsParam (const char *param)
 
void addParamsLine (const String &line)
 
void addParamsLine (const char *line)
 
ParamDefgetParamDef (const char *param) const
 
virtual void quit (int exit_code=0) const
 
virtual int tryRun ()
 
void initProgress (size_t total, size_t stepBin=60)
 
void setProgress (size_t value=0)
 
void endProgress ()
 
void processDefaultComment (const char *param, const char *left)
 
void setDefaultComment (const char *param, const char *comment)
 
virtual void initComments ()
 
void setProgramName (const char *name)
 
void addUsageLine (const char *line, bool verbatim=false)
 
void clearUsage ()
 
void addExampleLine (const char *example, bool verbatim=true)
 
void addSeeAlsoLine (const char *seeAlso)
 
void addKeywords (const char *keywords)
 
const char * name () const
 
virtual void usage (int verb=0) const
 
virtual void usage (const String &param, int verb=2)
 
int version () const
 
virtual void show () const
 
virtual void read (int argc, const char **argv, bool reportErrors=true)
 
virtual void read (int argc, char **argv, bool reportErrors=true)
 
void read (const String &argumentsLine)
 
 XmippProgram ()
 
 XmippProgram (int argc, const char **argv)
 
virtual ~XmippProgram ()
 

Static Public Member Functions

static void upsample (const MultidimArray< std::complex< double >> &from, MultidimArray< std::complex< double >> &to)
 
static void downsample (const MultidimArray< std::complex< double >> &from, MultidimArray< std::complex< double >> &to)
 

Public Attributes

FileName fnOut
 
FileName fnTom
 
FileName fnCoor
 
size_t Xdim
 
size_t Ydim
 
size_t Zdim
 
MetaDataVec md
 
MetaDataVec mdout
 
MDRowVec rowout
 
bool invertContrast
 
bool normalize
 
bool fixedBoxSize
 
double scaleFactor
 
double downsampleFactor
 
std::vector< size_t > maskIdx
 
int boxsize
 
int nthrs
 
Image< double > subtomoImg
 
- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Additional Inherited Members

- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 
- Protected Attributes inherited from XmippProgram
int errorCode
 
ProgramDefprogDef
 Program definition and arguments parser. More...
 
std::map< String, CommentListdefaultComments
 
int argc
 Original command line arguments. More...
 
const char ** argv
 

Detailed Description

Definition at line 44 of file tomo_extract_subtomograms.h.

Member Function Documentation

◆ createSphere()

void ProgTomoExtractSubtomograms::createSphere ( int  halfboxsize)

Definition at line 62 of file tomo_extract_subtomograms.cpp.

63 {
64  long n=0;
65 
66  for (int k=0; k<boxsize; k++)
67  {
68  int k2 = (k-halfboxsize);
69  k2 = k2*k2;
70  for (int i=0; i<boxsize; i++)
71  {
72  int i2 = i-halfboxsize;
73  int i2k2 = i2*i2 +k2 ;
74  for (int j=0; j<boxsize; j++)
75  {
76  int j2 = (j- halfboxsize);
77  if (sqrt(i2k2 + j2*j2)>halfboxsize)
78  {
79  maskIdx.push_back(n);
80  }
81  n++;
82  }
83  }
84  }
85 }
void sqrt(Image< double > &op)
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define j
int * n

◆ defineParams()

void ProgTomoExtractSubtomograms::defineParams ( )
virtual

Function in which the param of each Program are defined.

Reimplemented from XmippProgram.

Definition at line 47 of file tomo_extract_subtomograms.cpp.

48 {
49  addUsageLine("This function takes a tomogram an extract a set of subtomogram from it. The coordinates of the subtomograms are speciffied in the metadata given by coordinates.");
50  addParamsLine(" --tomogram <vol_file=\"\"> : Filename of the tomogram containing the subtomograms to be extracted");
51  addParamsLine(" --coordinates <vol_file=\"\"> : Metadata (.xmd file) with the coordidanates to be extracted from the tomogram");
52  addParamsLine(" --boxsize <boxsize=100> : Particle box size in voxels, of the particle without downsampling.");
53  addParamsLine(" [--invertContrast] : Set this flag to invert the contrast of the extracted subtomograms");
54  addParamsLine(" [--normalize] : This flag will set the subtomograms to have zero mean and unit standard deviation.");
55  addParamsLine(" [--downsample <downsampleFactor=1.0>] : Scale factor of the extracted subtomograms. It must be greater than 1. A downsampling 2 reduces in a factor 2 the size of the subtomos.");
56  addParamsLine(" [--fixedBoxSize] : If selected, programs calculates the extraction box to obtain a box size of the selected size when downsampling.");
57  addParamsLine(" -o <vol_file=\"\"> : Path of the output directory. ");
58  addParamsLine(" [--threads <s=4>] : Number of threads");
59 }
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)

◆ downsample()

void ProgTomoExtractSubtomograms::downsample ( const MultidimArray< std::complex< double >> &  from,
MultidimArray< std::complex< double >> &  to 
)
static

Definition at line 334 of file tomo_extract_subtomograms.cpp.

335 {
336  for (size_t k = 0; k < ZSIZE(to)/2; k++)
337  {
338  for (size_t j = 0; j < YSIZE(to)/2; j++)
339  {
340  for (size_t i = 0; i < XSIZE(to); i++)
341  {
342  // Origin cube
343  DIRECT_A3D_ELEM(to, k, j, i) = DIRECT_A3D_ELEM(from, k, j, i);
344 
345  // Oposite cube
346  DIRECT_A3D_ELEM(to, ZSIZE(to)/2 + k, YSIZE(to)/2 + j, i) =
347  DIRECT_A3D_ELEM(from, ZSIZE(from) - ZSIZE(to)/2 + k, YSIZE(from) - YSIZE(to)/2 + j, i);
348 
349  // Offset-Z cube
350  DIRECT_A3D_ELEM(to, ZSIZE(to)/2 + k, j, i) =
351  DIRECT_A3D_ELEM(from, ZSIZE(from) - ZSIZE(to)/2 + k, j, i);
352 
353  // Offset-Y cube
354  DIRECT_A3D_ELEM(to, k, YSIZE(to)/2 + j, i) =
355  DIRECT_A3D_ELEM(from, k, YSIZE(from) - YSIZE(to)/2 + j, i);
356  }
357  }
358  }
359 }
#define YSIZE(v)
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define XSIZE(v)
#define ZSIZE(v)
#define DIRECT_A3D_ELEM(v, k, i, j)
#define j

◆ extractSubtomoFixedSize()

void ProgTomoExtractSubtomograms::extractSubtomoFixedSize ( MultidimArray< double > &  subtomoExtraction)

Definition at line 144 of file tomo_extract_subtomograms.cpp.

145 {
146  // Downsampling
147  FourierTransformer transformer1;
148  FourierTransformer transformer2;
149 
150  MultidimArray< std::complex<double> > fftSubtomoExtraction;
152 
153  fftSubtomo.initZeros(boxsize, boxsize, (boxsize/2)+1);
154  transformer1.FourierTransform(subtomoExtraction, fftSubtomoExtraction, true);
155 
156  #ifdef DEBUG
157  std::cout << "XSIZE(fftSubtomo) " << XSIZE(fftSubtomo) << std::endl;
158  std::cout << "YSIZE(fftSubtomo) " << YSIZE(fftSubtomo) << std::endl;
159  std::cout << "ZSIZE(fftSubtomo) " << ZSIZE(fftSubtomo) << std::endl;
160 
161  std::cout << "XSIZE(fftSubtomoExtraction) " << XSIZE(fftSubtomoExtraction) << std::endl;
162  std::cout << "YSIZE(fftSubtomoExtraction) " << YSIZE(fftSubtomoExtraction) << std::endl;
163  std::cout << "ZSIZE(fftSubtomoExtraction) " << ZSIZE(fftSubtomoExtraction) << std::endl;
164  #endif
165 
166  if (downsampleFactor > 1)
167  {
168  downsample(fftSubtomoExtraction, fftSubtomo);
169  }
170  else // downsampleFactor < 1
171  {
172  upsample(fftSubtomoExtraction, fftSubtomo);
173  }
174 
175  subtomoExtraction.initZeros(1, boxsize, boxsize, boxsize);
176 
177  transformer2.inverseFourierTransform(fftSubtomo, subtomoExtraction);
178 }
#define YSIZE(v)
static void upsample(const MultidimArray< std::complex< double >> &from, MultidimArray< std::complex< double >> &to)
void inverseFourierTransform()
Definition: xmipp_fftw.cpp:329
static void downsample(const MultidimArray< std::complex< double >> &from, MultidimArray< std::complex< double >> &to)
#define XSIZE(v)
#define ZSIZE(v)
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
void initZeros(const MultidimArray< T1 > &op)

◆ normalizeSubtomo()

void ProgTomoExtractSubtomograms::normalizeSubtomo ( MultidimArray< double > &  subtomo,
int  halfboxsize 
)

Definition at line 88 of file tomo_extract_subtomograms.cpp.

89 {
90  MultidimArray<double> maskNormalize;
91 
92  double sumVal = 0;
93  double sumVal2 = 0;
94 
95  auto counter = maskIdx.size();
96  for (size_t i=0; i<maskIdx.size(); i++)
97  {
98  double val = DIRECT_MULTIDIM_ELEM(subtomo, maskIdx[i]);
99  sumVal += val;
100  sumVal2 += val*val;
101 
102  }
103 
104  double mean;
105  double sigma2;
106  mean = sumVal/counter;
107  sigma2 = sqrt(sumVal2/counter - mean*mean);
108 
110  {
111  DIRECT_MULTIDIM_ELEM(subtomo, n) -= mean;
112  DIRECT_MULTIDIM_ELEM(subtomo, n) /= sigma2;
113  }
114 }
void sqrt(Image< double > &op)
#define i
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
#define DIRECT_MULTIDIM_ELEM(v, n)
int * n

◆ readParams()

void ProgTomoExtractSubtomograms::readParams ( )
virtual

Function in which each program will read parameters that it need. If some error occurs the usage will be printed out.

Reimplemented from XmippProgram.

Definition at line 33 of file tomo_extract_subtomograms.cpp.

34 {
35  fnTom = getParam("--tomogram");
36  fnCoor = getParam("--coordinates");
37  boxsize = getIntParam("--boxsize");
38  invertContrast = checkParam("--invertContrast");
39  normalize = checkParam("--normalize");
40  fnOut = getParam("-o");
41  downsampleFactor = getDoubleParam("--downsample");
42  nthrs = getIntParam("--threads");
43  fixedBoxSize = checkParam("--fixedBoxSize");
44 }
double getDoubleParam(const char *param, int arg=0)
const char * getParam(const char *param, int arg=0)
bool checkParam(const char *param)
int getIntParam(const char *param, int arg=0)

◆ run()

void ProgTomoExtractSubtomograms::run ( )
virtual

This function will be start running the program. it also should be implemented by derived classes.

Reimplemented from XmippProgram.

Definition at line 180 of file tomo_extract_subtomograms.cpp.

181 {
182  std::cout << "Starting ... "<< std::endl;
183 
184  md.read(fnCoor);
185 
186  Image<double> tomImg;
187  auto &tom = tomImg();
188  tomImg.read(fnTom);
189 
190  size_t Xtom;
191  size_t Ytom;
192  size_t Ztom;
193 
194  Xtom = XSIZE(tom);
195  Ytom = YSIZE(tom);
196  Ztom = ZSIZE(tom);
197 
198  auto &subtomo = subtomoImg();
199 
200  MultidimArray<double> subtomoExtraction;
201 
202  int xcoor;
203  int ycoor;
204  int zcoor;
205  int xinit;
206  int yinit;
207  int zinit;
208 
209  size_t idx=1;
210 
211  int halfboxsize = floor(0.5*boxsize);
212 
213  double invertSign = 1.0;
214 
215  if (invertContrast)
216  {
217  invertSign = -1;
218  }
219 
220  if (normalize)
221  {
222  createSphere(halfboxsize);
223  }
224 
225  double dsFactorTolerance = 0.01;
226  double dsFactorDiff = abs(downsampleFactor - 1);
227 
228  size_t boxSizeExtraction;
229  if (fixedBoxSize && dsFactorDiff > dsFactorTolerance)
230  {
231  #ifdef DEBUG
232  std::cout << "Entering fixed box size mode" << std::endl;
233  #endif
234 
235 
236  boxSizeExtraction = boxsize * downsampleFactor;
237  halfboxsize = floor(0.5*boxSizeExtraction);
238  }
239 
240  for (const auto& row : md)
241  {
242  row.getValue(MDL_XCOOR, xcoor);
243  row.getValue(MDL_YCOOR, ycoor);
244  row.getValue(MDL_ZCOOR, zcoor);
245 
246  int xlim = xcoor + halfboxsize;
247  int ylim = ycoor + halfboxsize;
248  int zlim = zcoor + halfboxsize;
249 
250  xinit = xcoor - halfboxsize;
251  yinit = ycoor - halfboxsize;
252  zinit = zcoor - halfboxsize;
253 
254  if ((xlim>Xtom) || (ylim>Ytom) || (zlim>Ztom) || (xinit<0) || (yinit<0) || (zinit<0))
255  continue;
256 
257  if (fixedBoxSize && dsFactorDiff > dsFactorTolerance)
258  {
259  subtomo.initZeros(1, boxSizeExtraction, boxSizeExtraction, boxSizeExtraction);
260  }
261  else
262  {
263  subtomo.initZeros(1, boxsize, boxsize, boxsize);
264  }
265 
266  // Contrast inversion
267  for (int k=zinit; k<zlim; k++)
268  {
269  int kk = k - zcoor;
270  for (int i=yinit; i<ylim; i++)
271  {
272  int ii = i-ycoor;
273  for (int j=xinit; j<xlim; j++)
274  {
275  A3D_ELEM(subtomo, kk+halfboxsize, ii+halfboxsize, j+halfboxsize-xcoor) = invertSign * A3D_ELEM(tom, k, i, j);
276  }
277  }
278  }
279 
280  if (fixedBoxSize && dsFactorDiff > dsFactorTolerance)
281  {
282  extractSubtomoFixedSize(subtomo);
283  }
284 
285  // Normalization
286  if (normalize)
287  {
288  normalizeSubtomo(subtomo, halfboxsize);
289  }
290 
291  size_t particleid = -1;
292  if (row.containsLabel(MDL_PARTICLE_ID))
293  {
294  row.getValue(MDL_PARTICLE_ID, particleid);
295  }
296 
297  writeSubtomo(idx, xcoor, ycoor, zcoor, particleid);
298 
299  ++idx;
300  }
301 
302 
303  std::cout << "Subtomo substraction finished succesfully!!" << std::endl;
304 }
void extractSubtomoFixedSize(MultidimArray< double > &subtomoExtraction)
#define YSIZE(v)
__host__ __device__ float2 floor(const float2 v)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void abs(Image< double > &op)
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define A3D_ELEM(V, k, i, j)
void normalizeSubtomo(MultidimArray< double > &subtomo, int halfboxsize)
X component (int)
#define XSIZE(v)
#define ZSIZE(v)
void writeSubtomo(int idx, int xcoor, int ycoor, int zcoor, size_t particleid)
#define j
Z component (int)
Particle unique identifier for reference. (The MDL_ITEM_ID should be used when particle list) ...
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
Y component (int)

◆ upsample()

void ProgTomoExtractSubtomograms::upsample ( const MultidimArray< std::complex< double >> &  from,
MultidimArray< std::complex< double >> &  to 
)
static

Definition at line 307 of file tomo_extract_subtomograms.cpp.

308 {
309  for (size_t k = 0; k < ZSIZE(from)/2; k++)
310  {
311  for (size_t j = 0; j < YSIZE(from)/2; j++)
312  {
313  for (size_t i = 0; i < XSIZE(from); i++)
314  {
315  // Origin cube
316  DIRECT_A3D_ELEM(to, k, j, i) = DIRECT_A3D_ELEM(from, k, j, i);
317 
318  // Oposite cube
319  DIRECT_A3D_ELEM(to, ZSIZE(to) - ZSIZE(from)/2 + k, YSIZE(to) - YSIZE(from)/2 + j, i) =
320  DIRECT_A3D_ELEM(from, ZSIZE(from)/2 + k, YSIZE(from)/2 + j, i);
321 
322  // Offset-Z cube
323  DIRECT_A3D_ELEM(to, ZSIZE(to) - ZSIZE(from)/2 + k, j, i) =
324  DIRECT_A3D_ELEM(from, ZSIZE(from)/2 + k, j, i);
325 
326  // Offset-Y cube
327  DIRECT_A3D_ELEM(to, k, YSIZE(to) - YSIZE(from)/2 + j, i) =
328  DIRECT_A3D_ELEM(from, k, YSIZE(from)/2 + j, i);
329  }
330  }
331  }
332 }
#define YSIZE(v)
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define XSIZE(v)
#define ZSIZE(v)
#define DIRECT_A3D_ELEM(v, k, i, j)
#define j

◆ writeSubtomo()

void ProgTomoExtractSubtomograms::writeSubtomo ( int  idx,
int  xcoor,
int  ycoor,
int  zcoor,
size_t  particleid 
)

Definition at line 116 of file tomo_extract_subtomograms.cpp.

117 {
118  FileName fn;
119  fn = fnCoor.getBaseName() + formatString("-%i.mrc", idx);
120 
121  #ifdef DEBUG
122  std::cout << fn << std::endl;
123  std::cout << fnOut << std::endl;
124  std::cout << fnOut+fn << std::endl;
125  #endif
126 
127  subtomoImg.write(fnOut+"/"+fn);
128 
129  if (particleid != -1)
130  {
131  rowout.setValue(MDL_PARTICLE_ID, particleid);
132  }
133 
134  rowout.setValue(MDL_XCOOR, xcoor);
135  rowout.setValue(MDL_YCOOR, ycoor);
136  rowout.setValue(MDL_ZCOOR, zcoor);
139 
140  fn = fnCoor.getBaseName() + "_extracted.xmd";
141  mdout.write(fnOut+"/"+fn);
142 }
void setValue(const MDObject &object) override
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
size_t addRow(const MDRow &row) override
X component (int)
Z component (int)
Particle unique identifier for reference. (The MDL_ITEM_ID should be used when particle list) ...
String formatString(const char *format,...)
Y component (int)
FileName getBaseName() const
Name of an image (std::string)

Member Data Documentation

◆ boxsize

int ProgTomoExtractSubtomograms::boxsize

Is the volume previously masked?

Definition at line 69 of file tomo_extract_subtomograms.h.

◆ downsampleFactor

double ProgTomoExtractSubtomograms::downsampleFactor

Definition at line 65 of file tomo_extract_subtomograms.h.

◆ fixedBoxSize

bool ProgTomoExtractSubtomograms::fixedBoxSize

Definition at line 62 of file tomo_extract_subtomograms.h.

◆ fnCoor

FileName ProgTomoExtractSubtomograms::fnCoor

Definition at line 50 of file tomo_extract_subtomograms.h.

◆ fnOut

FileName ProgTomoExtractSubtomograms::fnOut

Filenames

Definition at line 48 of file tomo_extract_subtomograms.h.

◆ fnTom

FileName ProgTomoExtractSubtomograms::fnTom

Definition at line 49 of file tomo_extract_subtomograms.h.

◆ invertContrast

bool ProgTomoExtractSubtomograms::invertContrast

Definition at line 60 of file tomo_extract_subtomograms.h.

◆ maskIdx

std::vector<size_t> ProgTomoExtractSubtomograms::maskIdx

Definition at line 66 of file tomo_extract_subtomograms.h.

◆ md

MetaDataVec ProgTomoExtractSubtomograms::md

Definition at line 56 of file tomo_extract_subtomograms.h.

◆ mdout

MetaDataVec ProgTomoExtractSubtomograms::mdout

Definition at line 57 of file tomo_extract_subtomograms.h.

◆ normalize

bool ProgTomoExtractSubtomograms::normalize

Definition at line 61 of file tomo_extract_subtomograms.h.

◆ nthrs

int ProgTomoExtractSubtomograms::nthrs

Definition at line 70 of file tomo_extract_subtomograms.h.

◆ rowout

MDRowVec ProgTomoExtractSubtomograms::rowout

Definition at line 58 of file tomo_extract_subtomograms.h.

◆ scaleFactor

double ProgTomoExtractSubtomograms::scaleFactor

Definition at line 64 of file tomo_extract_subtomograms.h.

◆ subtomoImg

Image<double> ProgTomoExtractSubtomograms::subtomoImg

Definition at line 72 of file tomo_extract_subtomograms.h.

◆ Xdim

size_t ProgTomoExtractSubtomograms::Xdim

Definition at line 52 of file tomo_extract_subtomograms.h.

◆ Ydim

size_t ProgTomoExtractSubtomograms::Ydim

Definition at line 53 of file tomo_extract_subtomograms.h.

◆ Zdim

size_t ProgTomoExtractSubtomograms::Zdim

Definition at line 54 of file tomo_extract_subtomograms.h.


The documentation for this class was generated from the following files: