Xmipp  v3.23.11-Nereus
pdb_restore_with_dictionary.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
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 
27 #include "core/xmipp_image.h"
28 #include "core/xmipp_funcs.h"
29 
31 {
32  // Usage
33  addUsageLine("This program takes a set of PDB files at low and high resolution and constructs a dictionary for them.");
34 
35  // Parameters
36  addParamsLine(" -i <volume> : Volume to restore");
37  addParamsLine(" [-o <volume=\"\">] : Restored volume");
38  addParamsLine(" [--root <root=dictionary>] : Rootname of the dictionary");
40 }
41 
43 {
44  fnIn=getParam("-i");
45  fnOut=getParam("-o");
46  if (fnOut=="")
47  fnOut=fnIn;
48  fnRoot=getParam("--root");
50 }
51 
52 /* Show -------------------------------------------------------------------- */
54 {
55  if (verbose)
56  {
57  std::cout
58  << "Input volume: " << fnIn << std::endl
59  << "Output volume: " << fnOut << std::endl
60  ;
62  }
63 }
64 
65 //#define DEBUG
67 {
68  show();
71 
72  Image<double> V, Vhigh;
73  MultidimArray<double> weightHigh;
74  V.read(fnIn);
75  const MultidimArray<double> &mV=V();
76  double min, max, mean, std=0;
77  mV.computeStats(mean,std,min,max);
78 
79  MultidimArray<double> patchLow, patchLowNormalized, canonicalPatch, patchHigh;
81  if (mode==0)
83  else
85  patchLow.setXmippOrigin();
86  size_t Npatches=0, NcandidatePatches=0;
87 
88  std::vector< size_t > selectedPatchesIdx;
89  std::vector<double> weight;
90  Matrix1D<double> alpha, canonicalSignature;
92  Vhigh().initZeros(mV);
93  weightHigh.initZeros(mV);
94  MultidimArray<double> &mVhigh=Vhigh();
95  for (int k=patchSize_2; k<(int)ZSIZE(mV)-patchSize_2; ++k)
96  {
97  for (int i=patchSize_2; i<(int)ZSIZE(mV)-patchSize_2; ++i)
98  for (int j=patchSize_2; j<(int)ZSIZE(mV)-patchSize_2; ++j)
99  {
100  ++Npatches;
101  extractPatch(mV,patchLow,k,i,j);
102 
103  double minPatchLow, maxPatchLow, meanPatchLow, stdPatchLow=0;
104  patchLow.computeStats(meanPatchLow,stdPatchLow,minPatchLow,maxPatchLow);
105  double R2=0;
106  patchHigh.initZeros(patchLow);
107 
108  if (stdPatchLow > stdThreshold*std)
109  {
110  ++NcandidatePatches;
111  patchLowNormalized=patchLow;
112  double norm=sqrt(patchLow.sum2());
113  patchLowNormalized*=1.0/norm;
114  size_t idxTransf=0;
115  if (mode==0)
116  idxTransf=canonicalOrientation3D(patchLowNormalized,canonicalPatch,canonicalSignature);
117  else
118  idxTransf=canonicalOrientation2D(patchLowNormalized,canonicalPatch,canonicalSignature);
119  selectDictionaryPatches(canonicalPatch, canonicalSignature, selectedPatchesIdx, weight);
120  if (selectedPatchesIdx.size()>0)
121  {
122  R2=approximatePatch(canonicalPatch,selectedPatchesIdx,weight,alpha);
123  reconstructPatch(idxTransf,selectedPatchesIdx,alpha,patchHigh);
124  patchHigh*=norm;
125  R2*=norm;
126 // Image<double> save;
127 // save()=patchLow;
128 // save.write("PPPlow.vol");
129 // save()=patchHigh;
130 // save.write("PPPhigh.vol");
131 // std::cout << "R2=" << R2 << " alpha=" << alpha << std::endl;
132 // std::cout << "Press any key" << std::endl;
133 // char c; std::cin >> c;
134  }
135  }
136  // Insert patchHigh in Vhigh
137  insertPatch(mVhigh,weightHigh,patchHigh,k,i,j,R2);
138  }
139  progress_bar(k);
140  }
141  progress_bar(ZSIZE(mV));
142 
143  // Correct by the Vhigh weights
144 // Image<double> save;
145 // save()=weightHigh;
146 // save.write("PPPweightHigh.vol");
147 // save()=mVhigh;
148 // save.write("PPPVHigh.vol");
150  if (A3D_ELEM(weightHigh,k,i,j)>0)
151  A3D_ELEM(mVhigh,k,i,j)/=A3D_ELEM(weightHigh,k,i,j);
152 
153  Vhigh.write(fnOut);
154 }
155 
void init_progress_bar(long total)
void extractPatch(const MultidimArray< double > &V, MultidimArray< double > &patch, int k, int i, int j)
void min(Image< double > &op1, const Image< double > &op2)
void resizeNoCopy(const MultidimArray< T1 > &v)
void sqrt(Image< double > &op)
void computeStats(double &avg, double &stddev, T &minval, T &maxval) const
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)
size_t canonicalOrientation3D(const MultidimArray< double > &patch, MultidimArray< double > &canonicalPatch, Matrix1D< double > &patchSignature)
T norm(const std::vector< T > &v)
Definition: vector_ops.h:399
#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)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
const char * getParam(const char *param, int arg=0)
double approximatePatch(const MultidimArray< double > &lowResolutionPatch, std::vector< size_t > &selectedPatchesIdx, std::vector< double > &weight, Matrix1D< double > &alpha)
void progress_bar(long rlen)
void max(Image< double > &op1, const Image< double > &op2)
void insertPatch(MultidimArray< double > &Vhigh, MultidimArray< double > &weightHigh, const MultidimArray< double > &patchHigh, int k, int i, int j, double R2)
#define ZSIZE(v)
int verbose
Verbosity level.
#define j
double sum2() const
void selectDictionaryPatches(const MultidimArray< double > &lowResolutionPatch, Matrix1D< double > &lowResolutionPatchSignature, std::vector< size_t > &selectedPatchesIdx, std::vector< double > &weight)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void reconstructPatch(size_t idxTransf, std::vector< size_t > &selectedPatchesIdx, Matrix1D< double > &alpha, MultidimArray< double > &highResolutionPatch)
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
size_t canonicalOrientation2D(const MultidimArray< double > &patch, MultidimArray< double > &canonicalPatch, Matrix1D< double > &patchSignature)
void addParamsLine(const String &line)