Xmipp  v3.23.11-Nereus
volumeset_align.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Mohamad Harastani mohamad.harastani@upmc.fr
4  * Slavica Jonic slavica.jonic@upmc.fr
5  * Carlos Oscar Sanchez Sorzano coss.eps@ceu.es
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.uam.es'
24  ***************************************************************************/
25 
26 #include "program_extension.h"
27 #include "volumeset_align.h"
28 #include "core/metadata_db.h"
29 
30 // Empty constructor =======================================================
33  produces_an_output = true;
34 }
35 
36 // Params definition ============================================================
38  addUsageLine("Align a set of volumes with a reference volume");
39  defaultComments["-i"].clear();
40  defaultComments["-i"].addComment("Metadata with volume filenames");
41  defaultComments["-o"].clear();
42  defaultComments["-o"].addComment("Metadata with output Euler angles and shifts");
44  addParamsLine(" --ref <VOL_filename> : Reference volume");
45  addParamsLine(" [--odir <outputDir=\".\">] : Output directory");
46  addParamsLine(" [--resume] : Resume processing");
47  addParamsLine(" [--frm_parameters <frm_freq=0.25> <frm_shift=10>] : This is using frm method for volume alignment with frm_freq and frm_shift as parameters");
48  addParamsLine(" [--tilt_values <tilt0=-90> <tiltF=90>] : Optional compensation for the missing wedge. Tested extensively with tilt between [-60 60]");
49  addParamsLine(" [--mask <mask_filename=\"\">] : Optional mask during the alignment");
50  addExampleLine("xmipp_volumeset_align -i volumes.xmd --ref reference.vol -o output.xmd --resume");
51 }
52 
53 // Read arguments ==========================================================
56  fnREF = getParam("--ref");
57  fnOutDir = getParam("--odir");
58  Rerunable::setFileName(fnOutDir+"/AlignedSoFar.xmd");
59  resume = checkParam("--resume");
60  frm_freq = getDoubleParam("--frm_parameters",0);
61  frm_shift= getIntParam("--frm_parameters",1);
62  tilt0=getIntParam("--tilt_values",0);
63  tiltF=getIntParam("--tilt_values",1);
64  fnMask = getParam("--mask");
65 }
66 
67 // Produce side information ================================================
68 
70  // Set the pointer of the program to this object
72 }
73 
76  const char* aligneddata = "/AlignedSoFar.xmd";
77  rename((fnOutDir+aligneddata).c_str(), fn_out.c_str());
78 }
79 
80 // Compute fitness =========================================================
81 void ProgVolumeSetAlign::computeFitness(){
82  FileName fnRandom;
84  const char * randStr = fnRandom.c_str();
85 
86  FileName fnShiftsAngles = fnRandom + "_angles_shifts.txt";
87  const char * shifts_angles = fnShiftsAngles.c_str();
88 
89  String fnVolume1 = this->currentVolName;
90  String fnVolume2 = this->fnREF;
91 
92  if (this->tilt0!=-90 || this->tiltF!=90 ){
93  this->flipped = true;
94  runSystem("xmipp_transform_geometry",formatString("-i %s -o %s_currentvolume.vol --rotate_volume euler 0 90 0 -v 0",fnVolume1.c_str(),randStr));
95  fnVolume1 = fnRandom+"_currentvolume.vol";
96  }
97 
98  const char * Volume1 = fnVolume1.c_str();
99  const char * Volume2 = fnVolume2.c_str();
100 
101  int err;
102 
103  String args = formatString("--i1 %s --i2 %s --frm %f %d %d %d --store %s -v 0",
104  Volume1,Volume2,this->frm_freq, this->frm_shift, this->tilt0, this->tiltF, shifts_angles);
105 
106  if (!fnMask.isEmpty()){
107  const char * mask = this->fnMask.c_str();
108  args += formatString(" --mask binary_file %s", mask);
109  }
110 
111 
112  runSystem("xmipp_volume_align",args);
113 
114 
115  //The first 6 parameters are angles and shifts, and the 7th is the fitness value
116  fnAnglesAndShifts = fopen(shifts_angles, "r");
117  for (int i = 0; i < 6; i++){
118  err = fscanf(fnAnglesAndShifts, "%f,", &this->Matrix_Angles_Shifts[i]);
119  if (1!=err)
120  REPORT_ERROR(ERR_IO, "reading the angles and shifts was not successful");
121  }
122  err = fscanf(fnAnglesAndShifts, "%f,", &this->fitness);
123  if(1!=err)
124  REPORT_ERROR(ERR_IO, "reading the fitness value was not successful");
125 
126  fclose(fnAnglesAndShifts);
127 
128  runSystem("rm", formatString("-rf %s* &", randStr));
129 }
130 
131 
133  const FileName &, const MDRow &, MDRow &) {
134  static size_t imageCounter = 0;
135  ++imageCounter;
136  currentVolName = fnImg;
137  snprintf(nameTemplate, 255 ,"_node%d_img%lu_XXXXXX", rangen, (long unsigned int)imageCounter);
138  computeFitness();
139  writeVolumeParameters(fnImg);
140 }
141 
143  MetaDataVec md;
144  size_t objId = md.addObject();
145  md.setValue(MDL_IMAGE, fnImg, objId);
146  md.setValue(MDL_ENABLED, 1, objId);
147  md.setValue(MDL_ANGLE_ROT, (double)this->Matrix_Angles_Shifts[0], objId);
148  md.setValue(MDL_ANGLE_TILT, (double)this->Matrix_Angles_Shifts[1], objId);
149  md.setValue(MDL_ANGLE_PSI, (double)this->Matrix_Angles_Shifts[2], objId);
150  md.setValue(MDL_SHIFT_X, (double)this->Matrix_Angles_Shifts[3], objId);
151  md.setValue(MDL_SHIFT_Y, (double)this->Matrix_Angles_Shifts[4], objId);
152  md.setValue(MDL_SHIFT_Z, (double)this->Matrix_Angles_Shifts[5], objId);
153  md.setValue(MDL_MAXCC, -(double)this->fitness, objId);
154  if (this->flipped){
155  md.setValue(MDL_ANGLE_Y, 90.0 , objId);
156  }
157  else{
158  md.setValue(MDL_ANGLE_Y, 0.0 , objId);
159  }
161 }
162 
Rotation angle of an image (double,degrees)
double getDoubleParam(const char *param, int arg=0)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual void writeVolumeParameters(const FileName &fnImg)
Tilting angle of an image (double,degrees)
float Matrix_Angles_Shifts[6]
Shift for the image in the X axis (double)
ProgVolumeSetAlign()
Empty constructor.
Special label to be used when gathering MDs in MpiMetadataPrograms.
Input/Output general error.
Definition: xmipp_error.h:134
void setFileName(const FileName &fn)
void initUniqueName(const char *templateStr="xmippTemp_XXXXXX", const String &fnDir="")
void runSystem(const String &program, const String &arguments, bool useSystem)
#define i
Is this image enabled? (int [-1 or 1])
void readParams()
Read arguments from command line.
const FileName & getFileName() const
const char * getParam(const char *param, int arg=0)
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
Angle between y-axis and tilt-axis (double, degrees) for untilted micrographs.
void processImage(const FileName &fnImg, const FileName &, const MDRow &, MDRow &)
bool produces_an_output
Indicate that a unique final output is produced.
void addExampleLine(const char *example, bool verbatim=true)
Maximum cross-correlation for the image (double)
virtual void preProcess()
virtual void finishProcessing()
FileName fnREF
Reference volume structure.
virtual void createWorkFiles()
bool isEmpty() const
void append(const FileName &outFile) const
std::string String
Definition: xmipp_strings.h:34
String formatString(const char *format,...)
Shift for the image in the Z axis (double)
FileName fnOutDir
Output directory.
bool checkParam(const char *param)
bool each_image_produces_an_output
Indicate that an output is produced for each image in the input.
Shift for the image in the Y axis (double)
void addUsageLine(const char *line, bool verbatim=false)
int getIntParam(const char *param, int arg=0)
Name of an image (std::string)
void addParamsLine(const String &line)
void defineParams()
Define params.
std::map< String, CommentList > defaultComments
Definition: xmipp_program.h:83