Xmipp  v3.23.11-Nereus
score_micrograph.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: AUTHOR_NAME (jvargas@cnb.csic.es)
3  *
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/args.h>
27 #include <data/filters.h>
28 #include <core/xmipp_fft.h>
29 #include "data/mask.h"
30 #include "score_micrograph.h"
31 #include <data/fourier_filter.h>
32 #include "fringe_processing.h"
33 
34 /* Read parameters --------------------------------------------------------- */
36 {
37 
38  pieceDim = 256;
39  overlap = 0.5;
40  skipBorders = 2;
41  Nsubpiece = 1;
42  bootstrapN = -1;
43  estimate_ctf = true;
44  defocus_range = 1000;
45  fn_micrograph = getParam("--micrograph");
46  particleSize = getIntParam("--particleSize");
47 
49 
50 }
51 
52 /* Usage ------------------------------------------------------------------- */
54 {
55  addUsageLine("Estimate the score of a given Micrograph attending on different parameters.");
56  addParamsLine(" --micrograph <file> : File with the micrograph");
57  addParamsLine(" [--particleSize <p=100>] : Size of the particle");
59 }
60 
61 /* Apply ------------------------------------------------------------------- */
63 {
64 
65  std::cout << particleSize << std::endl;
66  char bufferDW[200];
67  FileName fn_micrographBadPxl;
68  fn_micrographBadPxl = fn_micrograph.withoutExtension()+"bp__tmp."+fn_micrograph.getExtension();
69  sprintf(bufferDW, "xmipp_transform_filter -i %s -o %s --bad_pixels outliers 2", fn_micrograph.c_str(), fn_micrographBadPxl.c_str());
70  if (!system(bufferDW))
71  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot filter");
72 
73  FileName fn_micrographDwn;
74  fn_micrographDwn = fn_micrograph.withoutExtension()+"_tmp."+fn_micrograph.getExtension();
75  sprintf(bufferDW, "xmipp_transform_downsample -i %s -o %s --step 2.0 --method fourier" , fn_micrographBadPxl.c_str(), fn_micrographDwn.c_str());
76  if (!system(bufferDW))
77  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot downsample");
78 
90  prmEstimateCTFFromMicrograph.fn_root = "/home/jvargas/Linux/Proyectos/LUMC/set_001_challenge/kk";
92 
93  MetaDataVec MD;
94  size_t id=MD.addObject();
95  MD.setValue(MDL_MICROGRAPH,fn_micrographDwn,id);
100  MD.setValue(MDL_IMAGE1,prmEstimateCTFFromMicrograph.fn_root+"_ctfmodel_quadrant.xmp",id);
101  MD.setValue(MDL_IMAGE2,prmEstimateCTFFromMicrograph.fn_root+"_ctfmodel_halfplane.xmp",id);
102  MD.setValue(MDL_CTF_DOWNSAMPLE_PERFORMED,2.000000,id);
103  MD.write("auxiliaryFile.xmd");
104 
105  //prmPSDSort.downsampling =
106  prmPSDSort.fn_in = "auxiliaryFile.xmd";
107  prmPSDSort.fn_out = "kkauxiliaryFile.xmd";
108  // COSS prmPSDSort.downsampling = 2.000000; // this parameter must be written in the metadata
109 
110  char buffer[200];
111  sprintf(buffer, "xmipp_ctf_sort_psds -i %s -o %s --downsampling %f", prmPSDSort.fn_in.c_str(), prmPSDSort.fn_out.c_str(),2.000000);
112  if (!system(bufferDW))
113  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot sort psds");
114 
115  //Spiral Phase Transform
116  MultidimArray<double> im, In, Mod, Part, Ice;
117  MultidimArray<bool> ROI1, ROI2;
118  Image<double> img;
119  img.read(fn_micrographDwn);
120  im = img();
121  im.statisticsAdjust(0.,1.);
122  In.resizeNoCopy(im);
123  Part.resizeNoCopy(im);
124  Ice.resizeNoCopy(im);
125  Mod.resizeNoCopy(im);
126  ROI1.resizeNoCopy(im);
127  ROI1.setXmippOrigin();
128  ROI2.resizeNoCopy(im);
129  ROI2.setXmippOrigin();
130  Ice.setXmippOrigin();
131  Part.setXmippOrigin();
133  A2D_ELEM(ROI1,i,j)= true;
134 
135  //aprox Xdim/particleSize is the particle frequency
136  size_t Xdim, Ydim, Zdim,Ndim;
137  im.getDimensions(Xdim, Ydim,Zdim,Ndim);
138 
139  double avg, stdv;
140  im.computeAvgStdev(avg,stdv);
141  double R = Xdim/particleSize;
142  double S = Xdim/particleSize;
143  FourierTransformer ftrans(FFTW_BACKWARD);
144  normalize(ftrans,im,In,Mod,R,S,ROI1);
145 
146  double partDensity = (Mod.sum()/(Xdim*Ydim))*100;
147 
148  double th=EntropyOtsuSegmentation(Mod,0.005,false);
149  //th *= 2;
151  {
152  if (A2D_ELEM(Mod,i,j) < th)
153  {
154  A2D_ELEM(ROI1,i,j) = false;
155  A2D_ELEM(ROI2,i,j) = true;
156  }
157  }
158 
159  double min_val, max_val, avg1, avg2, stdv1, stdv2;
160  computeStats_within_binary_mask(ROI1, im, min_val, max_val,avg1,stdv1);
161  computeStats_within_binary_mask(ROI2, im, min_val, max_val,avg2,stdv2);
162  double snr = (stdv1/stdv2)*(stdv1/stdv2);
163 
164 
165  double maxFreq;
166  double fitting;
167  double psdCorr90;
168  double psdRadialInt;
169 
170  std::cout << prmPSDSort.fn_in.c_str() << std::endl;
171  std::cout << prmPSDSort.fn_out.c_str() << std::endl;
172 
173  MD.read(prmPSDSort.fn_out);
174  for (size_t objId : MD.ids())
175  {
176  MD.getValue(MDL_CTF_CRIT_MAXFREQ,maxFreq,objId);
177  MD.getValue(MDL_CTF_CRIT_FITTINGSCORE,fitting,objId);
178  MD.getValue(MDL_CTF_CRIT_PSDCORRELATION90,psdCorr90,objId);
179  MD.getValue(MDL_CTF_CRIT_PSDRADIALINTEGRAL,psdRadialInt,objId);
180  std::cout << "partDensity : " << partDensity << std::endl;
181  std::cout << "snr :" << snr << std::endl;
182  std::cout << "maxFreq :" << maxFreq << std::endl;
183  std::cout << "fitting :" << fitting << std::endl;
184  std::cout << "psdCorr90 :" << psdCorr90 << std::endl;
185  std::cout << "psdRadialInt :" << psdRadialInt << std::endl;
186  }
187 
188  img()=Part;
189  img.write("kk1.mrc");
190  img()=Ice;
191  img.write("kk2.mrc");
192 
193 }
194 
195 #undef DEBUG
Just to locate unclassified errors.
Definition: xmipp_error.h:192
double defocus_range
Defocus range.
#define A2D_ELEM(v, i, j)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void resizeNoCopy(const MultidimArray< T1 > &v)
FileName fn_micrograph
Micrograph filename.
HBITMAP buffer
Definition: svm-toy.cpp:37
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 readBasicParams(XmippProgram *program)
Read parameters.
void computeAvgStdev(U &avg, U &stddev) const
double Tm
Sampling rate.
double downsampleFactor
Downsample performed.
PSD correlation at 90 degrees.
Name for the CTF Model (std::string)
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
bool estimate_ctf
Estimate a CTF for each PSD.
int pieceDim
Dimension of micrograph pieces.
bool estimate_ctf
Estimate a CTF for each PSD.
virtual IdIteratorProxy< false > ids()
A Power Spectrum Density file name (std::string)
#define i
void computeStats_within_binary_mask(const MultidimArray< T1 > &mask, const MultidimArray< T > &m, double &min_val, double &max_val, double &avg, double &stddev)
Definition: mask.h:799
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
String getExtension() const
void run()
Process the whole thing.
Score of the fitting.
FileName fn_in
Filenames of input and output Metadata.
const char * getParam(const char *param, int arg=0)
Integral of the radial PSD.
double defocus_range
Defocus range.
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
double EntropyOtsuSegmentation(MultidimArray< double > &V, double percentil, bool binarizeVolume)
Definition: filters.cpp:1145
Image associated to this object (std::string)
quaternion_type< T > normalize(quaternion_type< T > q)
Definition: point.cpp:278
ProgCTFEstimateFromPSD prmEstimateCTFFromPSD
Parameters for adjust_CTF program.
Maximum frequency at which the envelope drops below 0.1 (in Angstroms)
Image associated to this object (std::string)
Name of a micrograph (std::string)
int pieceDim
Dimension of micrograph pieces.
ProgCTFEstimateFromPSD prmEstimateCTFFromPSD
Parameters for adjust_CTF program.
#define j
Downsampling performed to estimate the CTF.
bool getValue(MDObject &mdValueOut, size_t id) const override
A enhanced Power Spectrum Density file name (std::string)
FileName withoutExtension() const
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void addUsageLine(const char *line, bool verbatim=false)
FileName fn_micrograph
Micrograph filename.
int getIntParam(const char *param, int arg=0)
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
double sum() const
void addParamsLine(const String &line)
void statisticsAdjust(U avgF, U stddevF)
ProgCTFEstimateFromMicrograph prmEstimateCTFFromMicrograph
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const