Xmipp  v3.23.11-Nereus
docfile.h
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 
26 #ifndef DOCFILE_H
27 #define DOCFILE_H
28 
29 #include <vector>
30 #include <string>
31 
32 #include <core/args.h>
33 #include <core/matrix1d.h>
34 #include <core/matrix2d.h>
35 #include <core/xmipp_image.h>
36 #include <core/metadata_vec.h>
37 
38 // Forward declaration
39 class DocFile;
40 
43 
44 
54 class DocLine
55 {
56 public:
57  typedef enum
58  {
61  DATALINE = 1,
62  COMMENT = 2
63  } Line_Type;
64 
65 private:
66  Line_Type line_type;
67  std::string text;
68  int key; //< key of this line
69  std::vector< double > data;
70 
71  friend class DocFile;
72 
73 public:
75 
76 
82  DocLine() : line_type(NOT_ASSIGNED)
83  {}
85 
87 
88 
102  double& operator[](size_t i);
103 
110  double operator[](size_t i) const;
111 
117  void set(size_t i, double val);
118 
125  void set(const Matrix1D< double >& v);
127 
129 
130 
135  std::string get_text()
136  {
137  return text;
138  }
139 
142  int get_key() const
143  {
144  return key;
145  }
146 
151  int get_no_components() const
152  {
153  if (line_type == DATALINE)
154  return data.size();
155  else
156  return -1;
157  }
158 
161  void clear();
162 
166  {
167  return line_type == COMMENT;
168  }
169 
172  int Is_data()
173  {
174  return line_type == DATALINE;
175  }
176 
183  void set_type(Line_Type _line_type)
184  {
185  line_type = _line_type;
186  }
188 
191  friend std::ostream& operator<<(std::ostream& o, const DocLine& DL);
192 
198  void read(std::istream& i);
199 };
200 
216 class DocFile
217 {
218  FileName fn_doc;
219  std::vector< DocLine > m;
220  int no_lines;
221  int first_key;
222  std::vector< DocLine >::iterator current_line;
223 
224  // Function to locate a given key within the docfile
225  std::vector< DocLine >::iterator find(int _key);
226 
227 public:
229 
230 
233  DocFile(): fn_doc(""), no_lines(0), first_key(1)
234  {
235  current_line = m.begin();
236  }
237 
247  DocFile(const FileName& doc_name)
248  {
249  first_key = 1;
250  read(doc_name);
251  }
252 
259  void clear();
260 
268  void reserve(int N)
269  {
270  m.reserve(N);
271  current_line = m.begin();
272  }
274 
276 
277 
288  DocFile& operator=(const Matrix2D< double >& A);
289 
298  friend std::ostream& operator<<(std::ostream& o, const DocFile& DF);
299 
311  void show_line(std::ostream& o, int key = -1);
312 
315  void debug();
317 
319 
320 
335  void read(const FileName& _name, int overrinding = 1);
336 
347  void append(const FileName &_name)
348  {
349  read(_name, 0);
350  }
351 
363  void write(const FileName& _name = "");
365 
367 
368 
379  {
380  current_line = m.begin();
381  }
382 
392  {
393  go_beginning();
394  adjust_to_data_line();
395  }
396 
406  void adjust_to_data_line();
407 
416  void next()
417  {
418  if (current_line != m.end())
419  current_line++;
420  }
421 
430  void previous()
431  {
432  if (current_line != m.begin())
433  current_line--;
434  }
435 
443  {
444  jump(1);
445  }
446 
459  void jump(int how_many);
460 
472  void search(int _key)
473  {
474  current_line = find(_key);
475  }
476 
487  int search_comment(std::string comment);
488 
496  int remove_multiple_strings(std::string pattern);
497 
506  void get_selfile(MetaData& SF);
507 
521  void locate(int _key);
522 
530  int eof()
531  {
532  return current_line == m.end();
533  }
535 
537 
538 
541  std::string name() const
542  {
543  return fn_doc;
544  }
545 
555  int exists(int _key)
556  {
557  return find(_key) != m.end();
558  }
559 
565  int getColNumberFromHeader(const char * pattern);
566 
567 
572  int FirstLine_colNumber();
573 
581  int dataLineNo() const
582  {
583  return no_lines;
584  }
585 
596  int LineNo() const
597  {
598  return m.size();
599  }
600 
609  int get_last_key();
610 
621  {
622  return (*current_line).key;
623  }
624 
634  int FirstKey() const
635  {
636  return first_key;
637  }
638 
645  void set_FirstKey(int _first_key)
646  {
647  first_key = _first_key;
648  }
649 
659  int get_current_valNo() const
660  {
661  return (*current_line).data.size();
662  }
663 
674  double operator()(int i) const
675  {
676  return (*current_line)[i];
677  }
678 
692  double operator()(int _key, int i);
693 
699  void get_angles(int _key,
700  double& rot,
701  double& tilt,
702  double& psi,
703  const std::string& ang1,
704  const std::string& ang2,
705  const std::string& ang3);
706 
712  void get_angles1(int _key,
713  double& rot,
714  double& tilt,
715  double& psi,
716  const std::string& ang1,
717  const std::string& ang2,
718  const std::string& ang3);
719 
725  void get_angles2(int _key,
726  double& rot,
727  double& tilt,
728  double& psi,
729  const std::string& ang1,
730  const std::string& ang2,
731  const std::string& ang3);
732 
738  void set_angles(int _key,
739  double rot,
740  double tilt,
741  double psi,
742  const std::string& ang1,
743  const std::string& ang2,
744  const std::string& ang3);
745 
751  void get_image(int key, Image<double> &I, bool apply_geo=false);
752 
758  FileName get_imagename(int key);
759 
776  void set(int i, double val);
777 
792  void set(int _key, int i, double val);
793 
797  {
798  return *current_line;
799  }
801 
803 
804 
814  void renum();
815 
830  void remove(int _key0, int _keyF = -1);
831 
843  void remove_current();
844 
856  int insert_data_line(int no_lines_to_insert = 1);
857 
882  int insert_data_line(const Matrix1D< double >& v);
883 
895  void insert_comment(std::string comment);
896 
912  int insert_line(const DocLine& DL);
913 
925  int append_data_line(int no_lines_to_append = 1);
926 
940  int append_data_line(const Matrix1D< double >& v);
941 
946  int append_angles(double rot,
947  double tilt,
948  double psi,
949  const std::string& ang1,
950  const std::string& ang2,
951  const std::string& ang3);
952 
957  int append_angles(double rot,
958  double tilt,
959  double psi,
960  double rot1,
961  double tilt1,
962  double psi1,
963  const std::string& ang1,
964  const std::string& ang2,
965  const std::string& ang3);
966 
971  int append_angles(double rot,
972  double tilt,
973  double psi,
974  double rot1,
975  double tilt1,
976  double psi1,
977  double rot2,
978  double tilt2,
979  double psi2,
980  const std::string& ang1,
981  const std::string& ang2,
982  const std::string& ang3);
983 
994  void append_comment(const std::string& comment);
995 
1007  int append_line(DocLine& DL);
1008 
1017  void clean_comments();
1019 
1021 
1022 
1035  DocFile randomize();
1036 
1047  void perturb_column(int col, double sigma);
1048 
1049 #define DOCMERGE_KEEP_OLD 1
1050 #define DOCMERGE_KEEP_NEW 2
1051 #define DOCMERGE_SUM_COLUMN 3
1052 #define DOCMERGE_ERROR 4
1053 
1073  void merge(const FileName& name, int mode=DOCMERGE_KEEP_OLD, int sumcol=5);
1074 
1077  void merge(DocFile& DF, int mode=DOCMERGE_KEEP_OLD, int sumcol=5);
1078 
1091  DocFile random_discard(int N);
1092 
1106  Matrix1D< double > col(int _col);
1107 
1118  Matrix1D< double > row(int _key);
1119 
1135  void setCol(int _col, Matrix1D< double >& v);
1137 };
1138 
1140 
1141 
1160  std::string ang1,
1161  std::string ang2,
1162  std::string ang3,
1163  DocFile& DF);
1164 
1176 void select_images(DocFile& DF,
1177  MetaData& SF,
1178  int col,
1179  bool en_limit0,
1180  double limit0,
1181  bool en_limitF,
1182  double limitF);
1183 
1192 void get_subset_docfile(DocFile& DFin,
1193  MetaData& SF,
1194  DocFile& DFout);
1196 
1197 #endif
void go_beginning()
Definition: docfile.h:378
DocLine get_current_line()
Definition: docfile.h:796
std::string get_text()
Definition: docfile.h:135
void write(std::ostream &os, const datablock &db)
Definition: cif2pdb.cpp:3747
int exists(int _key)
Definition: docfile.h:555
int read_Euler_document_file(FileName fn, std::string ang1, std::string ang2, std::string ang3, DocFile &DF)
Definition: docfile.cpp:1376
std::vector< SelLine >::iterator find(std::vector< SelLine > &text, const std::string &img_name)
Definition: selfile.cpp:553
void previous()
Definition: docfile.h:430
int get_current_key()
Definition: docfile.h:620
int get_current_valNo() const
Definition: docfile.h:659
#define i
void next_data_line()
Definition: docfile.h:442
int dataLineNo() const
Definition: docfile.h:581
double operator()(int i) const
Definition: docfile.h:674
void reserve(int N)
Definition: docfile.h:268
void read(std::istream &i)
Definition: docfile.cpp:129
std::string name() const
Definition: docfile.h:541
DocFile()
Definition: docfile.h:233
int debug
void go_first_data_line()
Definition: docfile.h:391
#define DOCMERGE_KEEP_OLD
Definition: docfile.h:1049
void set_type(Line_Type _line_type)
Definition: docfile.h:183
void mode
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
void get_subset_docfile(DocFile &DFin, MetaData &SF, DocFile &DFout)
Definition: docfile.cpp:1441
Line_Type
Definition: docfile.h:57
void append(const FileName &_name)
Definition: docfile.h:347
double & operator[](size_t i)
Definition: docfile.cpp:34
friend std::ostream & operator<<(std::ostream &o, const DocLine &DL)
Definition: docfile.cpp:95
int Is_comment()
Definition: docfile.h:165
int m
DocFile(const FileName &doc_name)
Definition: docfile.h:247
int get_no_components() const
Definition: docfile.h:151
int get_key() const
Definition: docfile.h:142
void set_FirstKey(int _first_key)
Definition: docfile.h:645
int Is_data()
Definition: docfile.h:172
int FirstKey() const
Definition: docfile.h:634
DocLine()
Definition: docfile.h:82
void next()
Definition: docfile.h:416
int eof()
Definition: docfile.h:530
double psi(const double x)
void select_images(DocFile &DF, MetaData &SF, int col, bool en_limit0, double limit0, bool en_limitF, double limitF)
Definition: docfile.cpp:1423
void clear()
Definition: docfile.cpp:87
void search(int _key)
Definition: docfile.h:472
int LineNo() const
Definition: docfile.h:596