Xmipp  v3.23.11-Nereus
pdb_label_from_volume.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors:
4  *
5  * Erney Ramirez-Aportela (eramirea@cnb.csic.es)
6  * Carlos Oscar S. Sorzano (coss@cnb.csic.es)
7  *
8  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  * 02111-1307 USA
24  *
25  * All comments concerning this program package may be sent to the
26  * e-mail address 'xmipp@cnb.csic.es'
27  ***************************************************************************/
28 
29 #include <fstream>
30 #include <iomanip>
31 #include "pdb_label_from_volume.h"
32 #include "core/xmipp_image.h"
33 #include "core/metadata_vec.h"
34 #include "data/pdb.h"
35 
36 /* Usage ------------------------------------------------------------------- */
37 
39 {
40  addUsageLine("Put a volume value to PDB file.");
41  addExampleLine(" xmipp_pdb_label_from_volume --pdb 1o7d.pdb --vol volume.vol -o pdb_label.pdb --sampling 1.6");
42 
43  addParamsLine(" --pdb <pdb_file> : File to process");
44  addParamsLine(" --vol <vol_file=\"\"> : Input volume");
45  addParamsLine(" [--mask <vol_file=\"\">] : Input mask (to calculate inside mask)");
46  addParamsLine(" -o <file> : Modified output PDB");
47  addParamsLine(" --sampling <Ts=1> : Sampling rate (Angstroms/pixel)");
48  addParamsLine(" [--origin <...>] : Origin of the volume --origin x y z");
49  addParamsLine(" [--radius <radius=0.8>] : Considered as radius of the atom (Angstroms)");
50  addParamsLine(" [--md <output=\"params.xmd\">] : Save mean and absolute mean output");
51 }
52 /* Read parameters --------------------------------------------------------- */
54 {
55  fn_pdb = getParam("--pdb");
56  fnVol = getParam("--vol");
57  fnMask = getParam("--mask");
58  fn_out=getParam("-o");
59  fnMD = getParam("--md");
60  Ts = getDoubleParam("--sampling");
61  radius = getDoubleParam("--radius");
62  withMask = checkParam("--mask");
63  defOrig = checkParam("--origin");
64  getListParam("--origin", origin);
65 
66 }
67 
68 /* Show -------------------------------------------------------------------- */
70 {
71  if (verbose==0)
72  return;
73  std::cout << "PDB file: " << fn_pdb << std::endl
74  << "Output: " << fn_out << std::endl
75  << "Sampling rate: " << Ts << std::endl
76  << "Origin: ";
77  for (size_t i=0; i<origin.size(); ++i)
78  std::cout << origin[i] << " ";
79  std::cout << std::endl
80  << "Radius: " << radius << std::endl;
81 }
82 
83 /* Produce Side Info ------------------------------------------------------- */
85 {
86  Image<double> V, M;
87  V.read(fnVol);
88  V().setXmippOrigin();
89  inputVol = V();
91 
92  if (withMask)
93  {
94  M.read(fnMask);
95  inputMask = M();
97  }
98 
99  //Origin of the volume
100  if (defOrig)
101  {
105 
109  }
110 
111  else
112  {
113  STARTINGZ(inputVol) = 0;
114  STARTINGY(inputVol) = 0;
115  STARTINGX(inputVol) = 0;
116 
117  STARTINGZ(inputMask) = 0;
118  STARTINGY(inputMask) = 0;
119  STARTINGX(inputMask) = 0;
120  }
121 
122 }
123 
124 /* Compute protein geometry ------------------------------------------------ */
126 {
127  PDBRichPhantom pdbIn;
128  PDBRichPhantom pdbOut;
129  pdbIn.read(fn_pdb);
130 
131  MetaDataVec mdmean;
132  size_t objId;
133  objId = mdmean.addObject();
134 
135  double suma=0, sumaP=0;
136  int numA=0;
137  for (const auto& atomIn : pdbIn.atomList)
138  {
139  const auto& x = atomIn.x;
140  const auto& y = atomIn.y;
141  const auto& z = atomIn.z;
142 
143  // Correct position
144  Matrix1D<double> r(3);
145  VECTOR_R3(r, x, y, z);
146  r *= 1/Ts;
147 
148  // Characterize atom
149  double radius2=radius*radius;
150 
151  // Find the part of the volume that must be updated
152  int k0 = XMIPP_MAX(FLOOR(ZZ(r) - radius), STARTINGZ(inputVol));
153  int kF = XMIPP_MIN(CEIL(ZZ(r) + radius), FINISHINGZ(inputVol));
154  int i0 = XMIPP_MAX(FLOOR(YY(r) - radius), STARTINGY(inputVol));
155  int iF = XMIPP_MIN(CEIL(YY(r) + radius), FINISHINGY(inputVol));
156  int j0 = XMIPP_MAX(FLOOR(XX(r) - radius), STARTINGX(inputVol));
157  int jF = XMIPP_MIN(CEIL(XX(r) + radius), FINISHINGX(inputVol));
158 
159  int ka = XMIPP_MAX(FLOOR(ZZ(r)), STARTINGZ(inputVol));
160  int ia = XMIPP_MAX(FLOOR(YY(r)), STARTINGZ(inputVol));
161  int ja = XMIPP_MAX(FLOOR(XX(r)), STARTINGZ(inputVol));
162 
163 
164  float atomS=0.0;
165  float atomP=0.0;
166  float value=0.0;
167  int cont=0;
168  for (int k = k0; k <= kF; k++)
169  {
170  double zdiff=ZZ(r) - k;
171  double zdiff2=zdiff*zdiff;
172  for (int i = i0; i <= iF; i++)
173  {
174  double ydiff=YY(r) - i;
175  double zydiff2=zdiff2+ydiff*ydiff;
176  for (int j = j0; j <= jF; j++)
177  {
178  double xdiff=XX(r) - j;
179  double rdiffModule2=zydiff2+xdiff*xdiff;
180  if (withMask)
181  {
182  if ( (rdiffModule2<radius2 || (k==ka && i==ia && j==ja)) && (inputMask(k, i , j)>0.00001) )
183  {
184  atomS += A3D_ELEM(inputVol,k, i, j);
185  ++cont;
186 
187  //Absolute
188  if (A3D_ELEM(inputVol,k, i, j) < 0)
189  {
190  value = -A3D_ELEM(inputVol,k, i, j);
191  atomP += value;
192  }
193  else
194  atomP += A3D_ELEM(inputVol,k, i, j);
195 
196  }
197  }
198  else
199  {
200  if ( (rdiffModule2<radius2) || (k==ka && i==ia && j==ja))
201  {
202  atomS+=A3D_ELEM(inputVol,k, i, j);
203  ++cont;
204 
205  //Absolute
206  if (A3D_ELEM(inputVol,k, i, j) < 0)
207  {
208  value = -A3D_ELEM(inputVol,k, i, j);
209  atomP += value;
210  }
211  else
212  atomP += A3D_ELEM(inputVol,k, i, j);
213 
214  }
215  }
216 
217  }
218  }
219  }
220 
221  if (atomS>=0)
222  atomS = atomP;
223  else
224  atomS = -atomP;
225 
226  if (atomS != 0)
227  atomS=atomS/cont;
228  else
229  atomS=0.00;
230 
231  if (atomP != 0)
232  atomP=atomP/cont;
233  else
234  atomP=0.00;
235 
236 
237  ++numA;
238  suma+=atomS;
239  sumaP+=atomP;
240 
241  auto atomOut = atomIn;
242  atomOut.occupancy = atomS;
243  pdbOut.addAtom(atomOut);
244  }
245 
246  double mean = suma/numA;
247  double meanA = sumaP/numA;
248  std::cout << "mean value: = " << mean << std::endl;
249  std::cout << "absolute mean value: = " << meanA << std::endl;
250 
251  mdmean.setValue(MDL_VOLUME_SCORE1, mean, objId);
252  mdmean.setValue(MDL_VOLUME_SCORE2, meanA, objId);
253  mdmean.write(fnMD);
254 
255  pdbOut.write(fn_out);
256 }
257 
258 
259 /* Run --------------------------------------------------------------------- */
261 {
262  produceSideInfo();
263  show();
265 }
void read(const FileName &fnPDB, const bool pseudoatoms=false, const double threshold=0.0)
Read rich phantom from either a PDB of CIF file.
Definition: pdb.cpp:704
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
double getDoubleParam(const char *param, int arg=0)
#define FINISHINGX(v)
void write(const FileName &fnPDB, const bool renumber=false)
Write rich phantom to PDB or CIF file.
Definition: pdb.cpp:872
static double * y
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
#define FINISHINGZ(v)
void getListParam(const char *param, StringVector &list)
#define STARTINGX(v)
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
#define STARTINGY(v)
#define A3D_ELEM(V, k, i, j)
#define FLOOR(x)
Definition: xmipp_macros.h:240
const char * getParam(const char *param, int arg=0)
#define XX(v)
Definition: matrix1d.h:85
bool setValue(const MDObject &mdValueIn, size_t id)
#define CEIL(x)
Definition: xmipp_macros.h:225
size_t addObject() override
MultidimArray< double > inputVol
double z
void addExampleLine(const char *example, bool verbatim=true)
int verbose
Verbosity level.
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define j
MultidimArray< double > inputMask
#define YY(v)
Definition: matrix1d.h:93
std::vector< RichAtom > atomList
List of atoms.
Definition: pdb.h:257
#define FINISHINGY(v)
#define VECTOR_R3(v, x, y, z)
Definition: matrix1d.h:124
int textToInteger(const char *str)
bool checkParam(const char *param)
< Score 1 for volumes
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)
void addAtom(const RichAtom &atom)
Add Atom.
Definition: pdb.h:264
#define STARTINGZ(v)
#define ZZ(v)
Definition: matrix1d.h:101
void addParamsLine(const String &line)