Xmipp  v3.23.11-Nereus
metadata_xml.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Vahid Abrishami (vabrishami@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 
26 #include <fstream>
27 #include "core/xmipp_program.h"
28 #include "core/metadata_vec.h"
29 
30 class ProgTest: public XmippProgram
31 {
32  FileName fnIn,fnOut;
33  bool extracPart;
34 
35  void defineParams()
36  {
37  addUsageLine("This program convert Metadata to XML");
38  addParamsLine("== Basic ==");
39  addParamsLine(" -i <metadata> : metadata input for testing");
40  addParamsLine(" -o <file> : output XML file");
41  addParamsLine(" [--extractParticlesMD] : If we want to process a MetaData that comes from ExtractParticles protocol and some of the particles have been rejected");
42  addExampleLine("Produce XML file from a metadata for benchmark",false);
43  addExampleLine("xmipp_metadata_XML -i DefaultFamily_extract_list.xmd -o XMLFile.xml");
44 
45  }
46  void readParams()
47  {
48  fnIn = getParam("-i");
49  fnOut = getParam("-o");
50  extracPart = checkParam("--extractParticlesMD");
51 
52  }
53 
54  void run()
55  {
56 
57  MetaDataVec MD, sortedMD;
58  std::ofstream fhOut;
59  StringVector blockList;
60  fhOut.open(fnOut.c_str());
61  fhOut<<"<particlepicking>"<<std::endl;
62  int x,y;
63  FileName micName,sTemp;
64 
65  if (!extracPart)
66  {
67  getBlocksInMetaDataFile(fnIn,blockList);
68  for (size_t i=0; i<blockList.size(); i++)
69  {
70  MD.read(blockList[i]+"@"+fnIn);
71  sTemp=blockList[i];
72  micName=sTemp.removeUntilPrefix("_");
73  fhOut<<"<micrograph id=\""<<micName<<"\">"<<std::endl;
74  for (size_t objId : MD.ids())
75  {
76  MD.getValue(MDL_XCOOR, x, objId);
77  MD.getValue(MDL_YCOOR, y, objId);
78  fhOut<<"<coordinate x=\""<<x<<"\" y=\""<<y<<"\"/>"<<std::endl;
79  }
80  fhOut<<"</micrograph>"<<std::endl;
81  }
82  }
83  else
84  {
85  FileName name,newName,nodirName;
86  MD.read(fnIn);
87  MD.removeDisabled();
88 
89  std::cout << fnIn << std::endl;
90  sortedMD.sort(MD,MDL_MICROGRAPH);
91 
92  sortedMD.getValue(MDL_MICROGRAPH, name, MD.firstRowId());
93  nodirName=name.removeDirectories();
94  nodirName=nodirName.removeAllExtensions();
95  fhOut<<"<micrograph id=\""<<nodirName<<"\">"<<std::endl;
96 
97  sortedMD.getValue(MDL_XCOOR, x, sortedMD.firstRowId());
98  sortedMD.getValue(MDL_YCOOR, y, sortedMD.firstRowId());
99  fhOut<<"<coordinate x=\""<<x<<"\" y=\""<<y<<"\"/>"<<std::endl;
100 
101  for (auto idIt = sortedMD.ids().begin(); idIt != sortedMD.ids().end(); ++idIt)
102  {
103  if (*idIt == MD.firstRowId())
104  ++idIt;
105 
106  size_t objId = *idIt;
107 
108  sortedMD.getValue(MDL_MICROGRAPH, newName, objId);
109 
110  if (name == newName)
111  {
112  sortedMD.getValue(MDL_XCOOR, x, objId);
113  sortedMD.getValue(MDL_YCOOR, y, objId);
114  fhOut<<"<coordinate x=\""<<x<<"\" y=\""<<y<<"\"/>"<<std::endl;
115  }
116  else
117  {
118  fhOut<<"</micrograph>"<<std::endl;
119  name = newName;
120  nodirName=name.removeDirectories();
121  nodirName=nodirName.removeAllExtensions();
122  fhOut<<"<micrograph id=\""<<nodirName<<"\">"<<std::endl;
123  sortedMD.getValue(MDL_XCOOR, x, objId);
124  sortedMD.getValue(MDL_YCOOR, y, objId);
125  fhOut<<"<coordinate x=\""<<x<<"\" y=\""<<y<<"\"/>"<<std::endl;
126  }
127  }
128 
129  }
130  fhOut<<"</micrograph>"<<std::endl;
131  fhOut<<"</particlepicking>"<<std::endl;
132  fhOut.close();
133  }
134 };
void sort(const MetaDataVec &MDin, const MDLabel sortLabel, bool asc=true, int limit=-1, int offset=0)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
static double * y
FileName removeDirectories(int keep=0) const
FileName removeAllExtensions() const
virtual IdIteratorProxy< false > ids()
void getBlocksInMetaDataFile(const FileName &inFile, StringVector &blockList)
std::vector< String > StringVector
Definition: xmipp_strings.h:35
doublereal * x
#define i
const char * getParam(const char *param, int arg=0)
size_t firstRowId() const override
X component (int)
void addExampleLine(const char *example, bool verbatim=true)
Name of a micrograph (std::string)
FileName removeUntilPrefix(const String &prefix) const
const char * name() const
bool getValue(MDObject &mdValueOut, size_t id) const override
virtual void removeDisabled()
bool checkParam(const char *param)
Y component (int)
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)