Xmipp  v3.23.11-Nereus
tomo_map_back.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Estrella Fernandez Gimenez me.fernandez@cnb.csic.es (2019)
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 "tomo_map_back.h"
27 #include "core/transformations.h"
28 
29 // Read arguments ==========================================================
31 {
32 
33  fn_tomo = getParam("-i");
34  fn_geom = getParam("--geom");
35  fn_ref = getParam("--ref");
36  fn_out = getParam("-o");
37  if (fn_out=="")
39  modeStr = getParam("--method");
40  if (modeStr=="highlight")
41  K=getDoubleParam("--method",1);
42  if (modeStr=="avg" || modeStr=="copy_binary")
43  threshold=getDoubleParam("--method",1);
44 }
45 
46 // Show ====================================================================
48 {
49  if (verbose>0)
50  {
51  std::cout
52  << "Input tomogram : " << fn_tomo << std::endl
53  << "Input geometry : " << fn_geom << std::endl
54  << "Input reference : " << fn_ref << std::endl
55  << "Painting mode : " << modeStr << std::endl
56  ;
57  }
58 }
59 
60 // usage ===================================================================
62 {
63  addUsageLine("This program takes a tomogram, a reference subtomogram and a metadata with geometrical parameters");
64  addUsageLine("(x,y,z) and places the reference subtomogram on the tomogram at the designated locations (map back).");
65  addUsageLine("The program has several 'painting' options:");
66  addUsageLine(" 1. Copying the reference onto the tomogram");
67  addUsageLine(" 2. Setting the region occupied by the reference in the tomogram to the average value of that region");
68  addUsageLine(" 3. Add the reference multiplied by a constant to the location specified");
69  addUsageLine(" 4. Copy a binarized version of the reference onto the tomogram");
70  addParamsLine(" -i <tomogram> : Original tomogram");
71  addParamsLine(" [-o <tomogram=\"\">] : Output tomogram mapped back");
72  addParamsLine(" --geom <geometry> : Subtomograms coordinates and rotation angles (it must be a metadata)");
73  addParamsLine(" --ref <reference> : Subtomogram reference");
74  addParamsLine(" [--method <mode=copy>] : Painting mode");
75  addParamsLine(" where <mode>");
76  addParamsLine(" copy");
77  addParamsLine(" avg <threshold=0.5>");
78  addParamsLine(" highlight <K=1>");
79  addParamsLine(" copy_binary <threshold=0.5>");
80 }
81 
82 // Produce side information ================================================
84 {
85  tomo.read(fn_tomo);
88  reference().setXmippOrigin();
89  const MultidimArray<double> &mReference=reference();
90 
91  if (modeStr=="copy")
92  mode=1;
93  else if (modeStr=="avg")
94  mode=2;
95  else if (modeStr=="highlight")
96  mode=3;
97  else if (modeStr=="copy_binary")
98  mode=4;
99 
100  if (mode==4 || mode==2)
101  {
102  FOR_ALL_ELEMENTS_IN_ARRAY3D(mReference)
103  A3D_ELEM(mReference,k,i,j)=(A3D_ELEM(mReference,k,i,j)>threshold) ? 1:0;
104  }
105 }
106 
107 #define GET_TOMO_COORD \
108 int xp=x+j;\
109 if (xp<0 || xp>=XSIZE(mTomo))\
110  continue;\
111 int yp=y+i;\
112 if (yp<0 || yp>=YSIZE(mTomo))\
113  continue;\
114 int zp=z+k;\
115 if (zp<0 || zp>=ZSIZE(mTomo))\
116  continue;\
117 
119 {
120  show();
122  int x,y,z;
123  const MultidimArray<double> &mReference=reference();
124  MultidimArray<double> &mTomo=tomo();
125  MultidimArray<double> referenceRotated;
127  A.initIdentity(4);
128 
129  for (const auto& row : mdGeom)
130  {
131  row.getValue(MDL_XCOOR,x);
132  row.getValue(MDL_YCOOR,y);
133  row.getValue(MDL_ZCOOR,z);
135  applyGeometry(xmipp_transformation::LINEAR, referenceRotated, mReference, A, xmipp_transformation::IS_NOT_INV, xmipp_transformation::DONT_WRAP);
136 
137  double avg=0, avgN=0;
138  if (mode==2)
139  {
140  FOR_ALL_ELEMENTS_IN_ARRAY3D(referenceRotated)
141  {
143  avg+=DIRECT_A3D_ELEM(mTomo,zp,yp,xp);
144  avgN+=1;
145  }
146  if (avgN>0)
147  avg/=avgN;
148  }
149 
150  FOR_ALL_ELEMENTS_IN_ARRAY3D(referenceRotated)
151  {
153  double val=A3D_ELEM(referenceRotated,k,i,j);
154  switch (mode)
155  {
156  case 1:
157  case 4:
158  DIRECT_A3D_ELEM(mTomo,zp,yp,xp)=val;
159  break;
160  case 2:
161  if (val>0)
162  DIRECT_A3D_ELEM(mTomo,zp,yp,xp)=avg;
163  break;
164  case 3:
165  DIRECT_A3D_ELEM(mTomo,zp,yp,xp)+=K*val;
166  break;
167  }
168  }
169  }
170 
171  tomo.write(fn_out);
172 }
double getDoubleParam(const char *param, int arg=0)
void defineParams()
Define parameters.
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
Image< double > tomo
Definition: tomo_map_back.h:45
void geo2TransformationMatrix(const MDRow &imageGeo, Matrix2D< double > &A, bool only_apply_shifts)
static double * y
void applyGeometry(int SplineDegree, MultidimArray< std::complex< double > > &V2, const MultidimArray< std::complex< double > > &V1, const Matrix2D< double > &A, bool inv, bool wrap, std::complex< double > outside, MultidimArray< double > *BcoeffsPtr)
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 readParams()
Read arguments.
doublereal * x
#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
Image< double > reference
Definition: tomo_map_back.h:45
#define A3D_ELEM(V, k, i, j)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
const char * getParam(const char *param, int arg=0)
void show() const
Show.
X component (int)
FileName fn_geom
Definition: tomo_map_back.h:40
#define GET_TOMO_COORD
double z
int verbose
Verbosity level.
#define DIRECT_A3D_ELEM(v, k, i, j)
#define j
Z component (int)
MetaDataVec mdGeom
Definition: tomo_map_back.h:46
void produce_side_info()
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
FileName fn_tomo
Input.
Definition: tomo_map_back.h:40
Y component (int)
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)
void initIdentity()
Definition: matrix2d.h:673