Xmipp  v3.23.11-Nereus
docfile.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 
26 #include <fstream>
27 #include <sstream>
28 #include <cstdio>
29 #include "core/transformations.h"
30 #include "docfile.h"
31 #include "core/args.h"
32 #include "core/xmipp_funcs.h"
33 
34 double& DocLine::operator[](size_t i)
35 {
36  if (i+1 > data.size())
37  {
38  std::cout << "Docline=" << *this << std::endl;
39  REPORT_ERROR(ERR_DOCFILE, "Trying to access to non-existing element " +
40  integerToString(i) + " of a document line");
41  }
42 
43  return data[i];
44 }
45 
46 double DocLine::operator[](size_t i) const
47 {
48  if (i+1 > data.size())
49  {
50  std::cout << "Docline=" << *this << std::endl;
51  REPORT_ERROR(ERR_DOCFILE, "Trying to access to non-existing element " +
52  integerToString(i) + " of a document line");
53  }
54 
55  return data[i];
56 }
57 
58 void DocLine::set(size_t i, double val)
59 {
60  // Make sure there is enough memory
61  if (i + 1 > data.size())
62  data.reserve(i + 1);
63 
64  // Pad with zeros for the non-existing indexes in between
65  int space_needed = i + 1 - data.size();
66  for (int k = 0; k < space_needed; k++)
67  data.push_back(0);
68 
69  // Set required data
70  data[i] = val;
71 }
72 
74 {
75  data.clear();
76  if (line_type != DATALINE)
77  {
78  line_type = DATALINE;
79  key = 0;
80  }
81 
82  data.reserve(VEC_XSIZE(v));
84  data.push_back(VEC_ELEM(v, i));
85 }
86 
88 {
89  line_type = NOT_ASSIGNED;
90  text = "";
91  key = 0;
92  data.clear();
93 }
94 
95 std::ostream& operator<<(std::ostream& o, const DocLine& line)
96 {
97  char aux[30];
98  switch (line.line_type)
99  {
100  case DocLine::DATALINE:
101  // Print a data line
102  sprintf(aux, "%5d ", line.key);
103  o << aux;
104  sprintf(aux, "%-2lu", (unsigned long int)(line.data.size()));
105  o << aux;
106 
107  int imax;
108  imax = line.data.size();
109  for (int i = 0; i < imax; i++)
110 {
111  sprintf(aux, " % 10.5f", line.data[i]);
112  o << aux;
113  }
114 
115  o << std::endl;
116  break;
117 
118  case DocLine::COMMENT:
119  // Print a comment
120  o << line.text << std::endl;
121  break;
122  default:
123  break;
124  }
125 
126  return o;
127 }
128 
130 {
131  std::string line;
132  int param_no;
133 
134  // Get line
135  getline(in, line);
136 
137  // Initialize target
138  line_type = DocLine::NOT_ASSIGNED;
139  text = "";
140  key = 0;
141  data.clear();
142 
143  // Check if comment or empty line
144  int charpos1 = line.find_first_not_of(" \t");
145  if (line[0] == '\0' || line[charpos1] == '#' || line[charpos1] == ';')
146  {
147  line_type = DocLine::COMMENT;
148  text = line;
149  data.clear();
150  key = 0;
151  }
152  // Read a true document file line
153  else
154  {
155  line_type = DocLine::DATALINE;
156  text = "";
157  size_t i = 0;
158 
159  key = textToInteger(nextToken(line, i));
160  param_no = textToInteger(nextToken(line, i));
161  std::string auxline = line;
162 
163  try
164  {
165  // Try unfixed mode first
166  readFloatList(line, i, param_no, data);
167  }
168  catch (XmippError &e)
169  {
170  // Try fixed mode then
171  data.clear();
172  data.reserve(param_no);
173  for (int i = 0; i < param_no; i++)
174  {
175  data.push_back(textToFloat(line.substr(8 + i*12, 12)));
176  }
177  }
178  }
179 }
180 
182 {
183  fn_doc = "";
184  m.clear();
185  no_lines = 0;
186  first_key = 1;
187  current_line = m.begin();
188 }
189 
191 {
192  clear();
193  DocLine temp;
194 
195  for (size_t i = 0; i <MAT_YSIZE(A); i++)
196  {
197  temp.clear();
198  temp.line_type = DocLine::DATALINE;
199  temp.data.resize(MAT_XSIZE(A));
200 
201  for (size_t j = 0; j < MAT_XSIZE(A); j++)
202  temp.data[j] = MAT_ELEM(A, i, j);
203 
204  m.push_back(temp);
205  }
206 
207  fn_doc = "";
208  no_lines = MAT_YSIZE(A);
209  renum();
210  go_beginning();
211 
212  return *this;
213 }
214 
215 //NOTE: MPI PROGRAMS USE << TP PASS DOCFILES
216 //FROM MASTER TO SLAVE
217 //PLEASE DO NOT ALTER THE OUTPUR FORMAT
218 std::ostream& operator<<(std::ostream& o, const DocFile& doc)
219 {
220  std::vector< DocLine >::const_iterator current = doc.m.begin();
221  std::vector< DocLine >::const_iterator last = doc.m.end();
222 
223  while (current != last)
224  {
225  o << *current;
226  current++;
227  }
228 
229  return o;
230 }
231 
232 void DocFile::show_line(std::ostream& o, int key)
233 {
234  if (key == -1)
235  {
236  if (current_line == m.end())
237  o << "Current line is at the end of file\n";
238  else
239  o << *current_line;
240  }
241  else
242  {
243  std::vector< DocLine >::iterator line = find(key);
244  if (line == m.end())
245  o << "Key " << key << " not found\n";
246  else
247  o << *line;
248  }
249 }
250 
252 {
253  std::vector< DocLine >::iterator current = m.begin();
254  std::vector< DocLine >::iterator last = m.end();
255 
256  while (current != last)
257  {
258  if ((*current).line_type == DocLine::DATALINE ||
259  (*current).line_type == DocLine::COMMENT)
260  std::cout << *current;
261  else
262  {
263  char aux[30];
264  std::string str = "";
265 
266  std::cout << "Special line\n";
267  std::cout << " Type: " << (*current).line_type << std::endl;
268  std::cout << " Key: " << (*current).key << std::endl;
269  std::cout << " Text: " << (*current).text << std::endl;
270  std::cout << " Data: ";
271  for (size_t i = 0; i < (*current).data.size(); i++)
272  {
273  sprintf(aux, " % 11.5f", (*current).data[i]);
274  str += aux;
275  }
276  std::cout << str << std::endl;
277  }
278 
279  current++;
280  }
281 }
282 
283 std::vector< DocLine >::iterator DocFile::find(int k)
284 {
285  std::vector< DocLine >::iterator current = m.begin();
286  std::vector< DocLine >::iterator last = m.end();
287 
288  while (current != last)
289  {
290  if ((*current).line_type == DocLine::DATALINE && (*current).key == k)
291  return current;
292 
293  current++;
294  }
295 
296  return current;
297 }
298 
300 {
301  if (current_line == m.end())
302  return;
303 
304  while ((*current_line).line_type != DocLine::DATALINE)
305  {
306  current_line++;
307 
308  if (current_line == m.end())
309  return;
310  }
311 }
312 
314 {
315  std::vector< DocLine >::iterator current = m.begin();
316  std::vector< DocLine >::iterator last = m.end();
317  int act_key = first_key;
318 
319  while (current != last)
320  {
321  if ((*current).line_type == DocLine::DATALINE)
322  (*current).key = act_key++;
323 
324  current++;
325  }
326 }
327 
328 void DocFile::read(const FileName& name, int overriding)
329 {
330  DocLine temp;
331  std::ifstream in;
332  int line_no = 1;
333 
334  // Empties current DocFile
335  if (overriding)
336  clear();
337 
338  // Open file
339  in.open(name.c_str(), std::ios::in);
340  if (!in)
341  REPORT_ERROR(ERR_DOCFILE, "DocFile::read: File " + name + " not found");
342 
343  // Read each line and keep it in the list of the DocFile object
344  in.peek();
345  while (!in.eof())
346  {
347  try
348  {
349  temp.read(in);
350  }
351  catch (XmippError &e)
352  {
353  std::cout << "Doc File: Line " << line_no <<
354  " is skipped due to an error\n";
355  }
356 
357  switch (temp.line_type)
358  {
360  break; // Line with an error
361 
362  case DocLine::DATALINE:
363  no_lines++;
364  m.push_back(temp);
365  break;
366 
367  case DocLine::COMMENT:
368  m.push_back(temp);
369  break;
370  default:
371  break;
372  }
373 
374  line_no++;
375  in.peek();
376  }
377 
378  // Close file
379  in.close();
380 
381  // Set "pointer" to the beginning of the file
382  if (overriding)
383  fn_doc = name;
384 
385  go_first_data_line();
386  renum();
387 }
388 
389 void DocFile::write(const FileName& name)
390 {
391  std::ofstream out;
392  std::vector< DocLine >::iterator current = m.begin();
393  std::vector< DocLine >::iterator last = m.end();
394 
395  if (name != "")
396  fn_doc = name;
397 
398  renum();
399 
400  // Open file
401  out.open(fn_doc.c_str(), std::ios::out);
402  if (!out)
403  REPORT_ERROR(ERR_IO_NOWRITE, "DocFile::write: File " + fn_doc +
404  " cannot be written");
405 
406  // Read each line and keep it in the list of the SelFile object
407  while (current != last)
408  out << *(current++);
409 
410  // Close file
411  out.close();
412 }
413 
414 void DocFile::jump(int count)
415 {
416  adjust_to_data_line();
417 
418  for (int i = 0; i < count; i++)
419  if (current_line != m.end())
420  {
421  current_line++;
422  adjust_to_data_line();
423  }
424 }
425 
426 int DocFile::search_comment(std::string comment)
427 {
428  go_beginning();
429  comment = " ; " + comment;
430 
431  while (!eof())
432  {
433  if ((*current_line).Is_comment())
434  {
435  if (strcmp(comment.c_str(), ((*current_line).get_text()).c_str())
436  == 0)
437  {
438  adjust_to_data_line();
439  return 1;
440  }
441  }
442 
443  next();
444  }
445 
446  return 0;
447 }
448 
449 int DocFile::remove_multiple_strings(std::string pattern)
450 {
451  bool found_once = false;
452  go_beginning();
453  while (!eof())
454  {
455  if ((*current_line).Is_comment())
456  {
457  if (((*current_line).get_text()).find(pattern) <
458  ((*current_line).get_text()).size() )
459  {
460  if (found_once)
461  {
462  remove_current();
463  }
464  found_once = true;
465  }
466  }
467  next();
468  }
469 
470  return 0;
471 
472 }
473 
474 
476 {
477  go_beginning();
478 
479  if ((*current_line).Is_comment())
480  if (strstr(((*current_line).get_text()).c_str(), "Headerinfo") == nullptr)
482  "DocFile::get_selfile: Docfile is of non-NewXmipp type!");
483 
484  sel.clear();
485  next();
486  FileName img;
487  while (!eof())
488  {
489  if (strstr(((*current_line).get_text()).c_str(), " ; ") != nullptr)
490  {
491  img = (*current_line).get_text();
492  sel.setValue(MDL_IMAGE,img.removeSubstring(" ; "), sel.addObject());
493  }
494 
495  next();
496  }
497 }
498 
500 {
501  std::vector< DocLine >::iterator last = m.end();
502 
503  current_line = m.begin();
504  while (current_line != last)
505  {
506  if ((*current_line).line_type == DocLine::DATALINE &&
507  (*current_line).key >= k)
508  return;
509 
510  current_line++;
511  }
512 }
513 
514 int DocFile::getColNumberFromHeader(const char * pattern)
515 {
516  std::string header;
517  go_beginning();
518  if ((*current_line).Is_comment())
519  {
520  header = (*current_line).get_text();
521  if (strstr(header.c_str(), "Headerinfo") == nullptr)
522  REPORT_ERROR(ERR_DOCFILE,"DocFile:: docfile is of non-NewXmipp type!");
523  else
524  {
525  std::vector<std::string> tokens;
526  tokenize(header,tokens," \t()");
527  for (size_t i = 0; i < tokens.size(); i++)
528  {
529  if (strstr(tokens[i].c_str(), pattern) != nullptr)
530  {
531  return textToInteger(tokens[i+1]);
532  }
533  }
534  }
535  }
536  return -1;
537 
538 }
539 
540 
542 {
543  std::vector< DocLine >::iterator aux = current_line;
544  go_first_data_line();
545 
546  int ret = current_line->data.size();
547  current_line = aux;
548 
549  return ret;
550 }
551 
553 {
554  std::vector< DocLine >::iterator last = m.end();
555  std::vector< DocLine >::iterator first = m.begin();
556 
557  do
558  {
559  if (last == first)
560  return first_key - 1;
561 
562  last--;
563  if (last->line_type == DocLine::DATALINE)
564  return last->key;
565 
566  }
567  while (true);
568 }
569 
570 double DocFile::operator()(int k, int i)
571 {
572  std::vector< DocLine >::iterator aux = find(k);
573 
574  if (aux == m.end())
575  REPORT_ERROR(ERR_DOCFILE, "DocFile::operator(): The given key (" + integerToString(k)
576  + ") is not in the file");
577 
578  return (*aux)[i];
579 }
580 
581 void DocFile::get_angles(int k, double& rot, double& tilt, double& psi,
582  const std::string& ang1, const std::string& ang2, const std::string& ang3)
583 {
584  std::vector< DocLine >::iterator it = find(k);
585 
586  if (it == m.end())
588  "DocFile::get_angles(): The given key (" + integerToString(k)
589  + ") is not in the file");
590 
591  switch (ang1[0])
592  {
593  case 'r':
594  rot = (*it)[0];
595  break;
596 
597  case 't':
598  tilt = (*it)[0];
599  break;
600 
601  case 'p':
602  psi = (*it)[0];
603  break;
604  }
605 
606  switch (ang2[0])
607  {
608  case 'r':
609  rot = (*it)[1];
610  break;
611 
612  case 't':
613  tilt = (*it)[1];
614  break;
615 
616  case 'p':
617  psi = (*it)[1];
618  break;
619  }
620 
621  switch (ang3[0])
622  {
623  case 'r':
624  rot = (*it)[2];
625  break;
626 
627  case 't':
628  tilt = (*it)[2];
629  break;
630 
631  case 'p':
632  psi = (*it)[2];
633  break;
634  }
635 }
636 
637 void DocFile::get_angles1(int k, double& rot, double& tilt, double& psi,
638  const std::string& ang1, const std::string& ang2, const std::string& ang3)
639 {
640  std::vector< DocLine >::iterator it = find(k);
641 
642  if (it == m.end())
643  REPORT_ERROR(ERR_DOCFILE, "DocFile::get_angles1(): The given key (" +
644  integerToString(k) + ") is not in the file");
645 
646  switch (ang1[0])
647  {
648  case 'r':
649  rot = (*it)[3];
650  break;
651 
652  case 't':
653  tilt = (*it)[3];
654  break;
655 
656  case 'p':
657  psi = (*it)[3];
658  break;
659  }
660 
661  switch (ang2[0])
662  {
663  case 'r':
664  rot = (*it)[4];
665  break;
666 
667  case 't':
668  tilt = (*it)[4];
669  break;
670 
671  case 'p':
672  psi = (*it)[4];
673  break;
674  }
675 
676  switch (ang3[0])
677  {
678  case 'r':
679  rot = (*it)[5];
680  break;
681 
682  case 't':
683  tilt = (*it)[5];
684  break;
685 
686  case 'p':
687  psi = (*it)[5];
688  break;
689  }
690 }
691 
692 void DocFile::get_angles2(int k, double& rot, double& tilt, double &psi,
693  const std::string& ang1, const std::string& ang2, const std::string& ang3)
694 {
695  std::vector< DocLine >::iterator it = find(k);
696  if (it == m.end())
697  REPORT_ERROR(ERR_DOCFILE, "DocFile::get_angles2(): The given key (" + integerToString(k)
698  + ") is not in the file");
699 
700  switch (ang1[0])
701  {
702  case 'r':
703  rot = (*it)[6];
704  break;
705 
706  case 't':
707  tilt = (*it)[6];
708  break;
709 
710  case 'p':
711  psi = (*it)[6];
712  break;
713  }
714 
715  switch (ang2[0])
716  {
717  case 'r':
718  rot = (*it)[7];
719  break;
720 
721  case 't':
722  tilt = (*it)[7];
723  break;
724 
725  case 'p':
726  psi = (*it)[7];
727  break;
728  }
729 
730  switch (ang3[0])
731  {
732  case 'r':
733  rot = (*it)[8];
734  break;
735 
736  case 't':
737  tilt = (*it)[8];
738  break;
739 
740  case 'p':
741  psi = (*it)[8];
742  break;
743  }
744 }
745 
746 void DocFile::set_angles(int k, double rot, double tilt, double psi,
747  const std::string& ang1, const std::string& ang2, const std::string& ang3)
748 {
749  std::vector< DocLine >::iterator it = find(k);
750  if (it == m.end())
751  REPORT_ERROR(ERR_DOCFILE, "DocFile::set_angles(): The given key (" +
752  integerToString(k) + ") is not in the file");
753 
754  switch (ang1[0])
755  {
756  case 'r':
757  (*it)[0] = rot;
758  break;
759 
760  case 't':
761  (*it)[0] = tilt;
762  break;
763 
764  case 'p':
765  (*it)[0] = psi;
766  break;
767  }
768 
769  switch (ang2[0])
770  {
771  case 'r':
772  (*it)[1] = rot;
773  break;
774 
775  case 't':
776  (*it)[1] = tilt;
777  break;
778 
779  case 'p':
780  (*it)[1] = psi;
781  break;
782  }
783 
784  switch (ang3[0])
785  {
786  case 'r':
787  (*it)[2] = rot;
788  break;
789 
790  case 't':
791  (*it)[2] = tilt;
792  break;
793 
794  case 'p':
795  (*it)[2] = psi;
796  break;
797  }
798 }
799 
800 void DocFile::get_image(int key, Image<double> &I, bool apply_geo)
801 {
802  current_line = m.begin();
803  current_line += 2*key;
804  DocLine DL = get_current_line();
805  previous();
806  FileName fn_img;
807  if (get_current_line().Is_comment())
808  fn_img = ((get_current_line()).get_text()).erase(0, 3);
809  else
810  REPORT_ERROR(ERR_DOCFILE,"The docfile provided is not of type Alignment");
811 
812  // Read actual image
813  I.read(fn_img);
814  I().setXmippOrigin();
815 
816  // Store translation in header and apply it to the actual image
817  I.setRot(DL[0]);
818  I.setTilt(DL[1]);
819  I.setPsi(DL[2]);
820  I.setXoff(DL[3]);
821  I.setYoff(DL[4]);
822  I.setFlip(0.);
823 
824  if (apply_geo)
825  {
827  I.getTransformationMatrix(A, true);
828  if (!A.isIdentity())
829  selfApplyGeometry(xmipp_transformation::BSPLINE3, I(), A, xmipp_transformation::IS_INV, xmipp_transformation::WRAP);
830  }
831 }
832 
834 {
835  current_line = m.begin();
836  current_line += 2*key;
837  DocLine DL = get_current_line();
838  previous();
839  FileName fn_img;
840  if (get_current_line().Is_comment())
841  fn_img = ((get_current_line()).get_text()).erase(0, 3);
842  else
843  REPORT_ERROR(ERR_DOCFILE,"The docfile provided is not of type Alignment");
844  return fn_img;
845 }
846 
847 void DocFile::set(int i, double val)
848 {
849  if (current_line != m.end())
850  (*current_line).set(i, val);
851  else
852  {
853  // Add a new line
854  DocLine tmp;
855  tmp.line_type = DocLine::DATALINE;
856  tmp.set(i, val);
857  m.push_back(tmp);
858  current_line = m.end();
859  }
860 }
861 
862 void DocFile::set(int k, int i, double val)
863 {
864  std::vector< DocLine >::iterator it = find(k);
865  if (it == m.end())
866  REPORT_ERROR(ERR_DOCFILE, "DocFile::set(): The given key (" + integerToString(k)
867  + ") is not in the file");
868 
869  it->set(i, val);
870 }
871 
872 void DocFile::remove(int key0, int keyF)
873 {
874  std::vector< DocLine >::iterator last = m.end();
875 
876  if (keyF == -1)
877  keyF = key0;
878 
879  int old = (*current_line).key;
880 
881  current_line = m.begin();
882  while (current_line != last)
883  {
884  if (current_line->line_type != DocLine::DATALINE)
885  continue;
886 
887  if (current_line->key >= key0 && current_line->key <= keyF)
888  remove_current();
889  else
890  current_line++;
891  }
892 
893  locate(old);
894 }
895 
897 {
898  if (current_line != m.end())
899  {
900  std::vector< DocLine >::iterator it = current_line;
901  it++;
902 
903  if (current_line->line_type == DocLine::DATALINE)
904  no_lines--;
905 
906  m.erase(current_line);
907  current_line = it;
908  }
909 }
910 
912 {
913  DocLine tmp;
914  tmp.line_type = DocLine::DATALINE;
915 
916  std::vector< DocLine >::iterator it;
917  for (int i = 0; i < count; i++)
918  {
919  current_line = m.insert(current_line, tmp);
920 
921  if (i == 0)
922  it = current_line;
923 
924  current_line++;
925  }
926 
927  renum();
928  no_lines += count;
929 
930  return it->key;
931 }
932 
934 {
935  DocLine tmp;
936  tmp.set(v);
937 
938  current_line = m.insert(current_line, tmp);
939  renum();
940 
941  int ret = current_line->key;
942  current_line++;
943  no_lines++;
944 
945  return ret;
946 }
947 
948 void DocFile::insert_comment(std::string comment)
949 {
950  DocLine tmp;
951  tmp.line_type = DocLine::COMMENT;
952  tmp.text = " ; " + comment;
953 
954  // Insert and update current_line
955  current_line = m.insert(current_line, tmp);
956  current_line++;
957 }
958 
960 {
961  int ret = 0;
962  current_line = m.insert(current_line, line);
963 
964  if (line.line_type == DocLine::DATALINE)
965  {
966  renum();
967  ret = current_line->key;
968  no_lines++;
969  }
970 
971  current_line++;
972  return ret;
973 }
974 
976 {
977  DocLine tmp;
978  tmp.line_type = DocLine::DATALINE;
979 
980  int ret = get_last_key() + 1;
981  int act_key = ret;
982 
983  for (int i = 0; i < count; i++, act_key++)
984  {
985  tmp.key = act_key;
986  m.push_back(tmp);
987  }
988 
989  no_lines += count;
990 
991  return ret;
992 }
993 
995 {
996  DocLine tmp;
997  tmp.set(v);
998  tmp.key = get_last_key() + 1;
999  m.push_back(tmp);
1000  no_lines++;
1001 
1002  return tmp.key;
1003 }
1004 
1005 void DocFile::append_comment(const std::string& comment)
1006 {
1007  DocLine tmp;
1008  tmp.line_type = DocLine::COMMENT;
1009 
1010  tmp.text = " ; " + comment;
1011  m.push_back(tmp);
1012 }
1013 
1014 int DocFile::append_angles(double rot, double tilt, double psi,
1015  const std::string& ang1, const std::string& ang2, const std::string& ang3)
1016 {
1017  Matrix1D< double > aux(3);
1018 
1019  if (ang1[0] == 'r')
1020  VEC_ELEM(aux, 0) = rot;
1021  else if (ang1[0] == 't')
1022  VEC_ELEM(aux, 0) = tilt;
1023  else if (ang1[0] == 'p')
1024  VEC_ELEM(aux, 0) = psi;
1025 
1026  if (ang2[0] == 'r')
1027  VEC_ELEM(aux, 1) = rot;
1028  else if (ang2[0] == 't')
1029  VEC_ELEM(aux, 1) = tilt;
1030  else if (ang2[0] == 'p')
1031  VEC_ELEM(aux, 1) = psi;
1032 
1033  if (ang3[0] == 'r')
1034  VEC_ELEM(aux, 2) = rot;
1035  else if (ang3[0] == 't')
1036  VEC_ELEM(aux, 2) = tilt;
1037  else if (ang3[0] == 'p')
1038  VEC_ELEM(aux, 2) = psi;
1039 
1040  return append_data_line(aux);
1041 }
1042 
1043 int DocFile::append_angles(double rot, double tilt, double psi,
1044  double rot1, double tilt1, double psi1,
1045  const std::string& ang1, const std::string& ang2, const std::string& ang3)
1046 {
1047  Matrix1D< double > aux(6);
1048 
1049  if (ang1[0] == 'r')
1050  VEC_ELEM(aux, 0) = rot;
1051  else if (ang1[0] == 't')
1052  VEC_ELEM(aux, 0) = tilt;
1053  else if (ang1[0] == 'p')
1054  VEC_ELEM(aux, 0) = psi;
1055 
1056  if (ang2[0] == 'r')
1057  VEC_ELEM(aux, 1) = rot;
1058  else if (ang2[0] == 't')
1059  VEC_ELEM(aux, 1) = tilt;
1060  else if (ang2[0] == 'p')
1061  VEC_ELEM(aux, 1) = psi;
1062 
1063  if (ang3[0] == 'r')
1064  VEC_ELEM(aux, 2) = rot;
1065  else if (ang3[0] == 't')
1066  VEC_ELEM(aux, 2) = tilt;
1067  else if (ang3[0] == 'p')
1068  VEC_ELEM(aux, 2) = psi;
1069 
1070  if (ang1[0] == 'r')
1071  VEC_ELEM(aux, 3) = rot1;
1072  else if (ang1[0] == 't')
1073  VEC_ELEM(aux, 3) = tilt1;
1074  else if (ang1[0] == 'p')
1075  VEC_ELEM(aux, 3) = psi1;
1076 
1077  if (ang2[0] == 'r')
1078  VEC_ELEM(aux, 4) = rot1;
1079  else if (ang2[0] == 't')
1080  VEC_ELEM(aux, 4) = tilt1;
1081  else if (ang2[0] == 'p')
1082  VEC_ELEM(aux, 4) = psi1;
1083 
1084  if (ang3[0] == 'r')
1085  VEC_ELEM(aux, 5) = rot1;
1086  else if (ang3[0] == 't')
1087  VEC_ELEM(aux, 5) = tilt1;
1088  else if (ang3[0] == 'p')
1089  VEC_ELEM(aux, 5) = psi1;
1090 
1091  return append_data_line(aux);
1092 }
1093 
1094 int DocFile::append_angles(double rot, double tilt, double psi,
1095  double rot1, double tilt1, double psi1,
1096  double rot2, double tilt2, double psi2,
1097  const std::string& ang1, const std::string& ang2, const std::string& ang3)
1098 {
1099  Matrix1D< double > aux(9);
1100 
1101  if (ang1[0] == 'r')
1102  VEC_ELEM(aux, 0) = rot;
1103  else if (ang1[0] == 't')
1104  VEC_ELEM(aux, 0) = tilt;
1105  else if (ang1[0] == 'p')
1106  VEC_ELEM(aux, 0) = psi;
1107 
1108  if (ang2[0] == 'r')
1109  VEC_ELEM(aux, 1) = rot;
1110  else if (ang2[0] == 't')
1111  VEC_ELEM(aux, 1) = tilt;
1112  else if (ang2[0] == 'p')
1113  VEC_ELEM(aux, 1) = psi;
1114 
1115  if (ang3[0] == 'r')
1116  VEC_ELEM(aux, 2) = rot;
1117  else if (ang3[0] == 't')
1118  VEC_ELEM(aux, 2) = tilt;
1119  else if (ang3[0] == 'p')
1120  VEC_ELEM(aux, 2) = psi;
1121 
1122  if (ang1[0] == 'r')
1123  VEC_ELEM(aux, 3) = rot1;
1124  else if (ang1[0] == 't')
1125  VEC_ELEM(aux, 3) = tilt1;
1126  else if (ang1[0] == 'p')
1127  VEC_ELEM(aux, 3) = psi1;
1128 
1129  if (ang2[0] == 'r')
1130  VEC_ELEM(aux, 4) = rot1;
1131  else if (ang2[0] == 't')
1132  VEC_ELEM(aux, 4) = tilt1;
1133  else if (ang2[0] == 'p')
1134  VEC_ELEM(aux, 4) = psi1;
1135 
1136  if (ang3[0] == 'r')
1137  VEC_ELEM(aux, 5) = rot1;
1138  else if (ang3[0] == 't')
1139  VEC_ELEM(aux, 5) = tilt1;
1140  else if (ang3[0] == 'p')
1141  VEC_ELEM(aux, 5) = psi1;
1142 
1143  if (ang1[0] == 'r')
1144  VEC_ELEM(aux, 6) = rot2;
1145  else if (ang1[0] == 't')
1146  VEC_ELEM(aux, 6) = tilt2;
1147  else if (ang1[0] == 'p')
1148  VEC_ELEM(aux, 6) = psi2;
1149 
1150  if (ang2[0] == 'r')
1151  VEC_ELEM(aux, 7) = rot2;
1152  else if (ang2[0] == 't')
1153  VEC_ELEM(aux, 7) = tilt2;
1154  else if (ang2[0] == 'p')
1155  VEC_ELEM(aux, 7) = psi2;
1156 
1157  if (ang3[0] == 'r')
1158  VEC_ELEM(aux, 8) = rot2;
1159  else if (ang3[0] == 't')
1160  VEC_ELEM(aux, 8) = tilt2;
1161  else if (ang3[0] == 'p')
1162  VEC_ELEM(aux, 8) = psi2;
1163 
1164  return append_data_line(aux);
1165 }
1166 
1168 {
1169  int ret = 0;
1170  if (line.line_type == DocLine::DATALINE)
1171  {
1172  no_lines++;
1173  ret = line.key = get_last_key() + 1;
1174  }
1175 
1176  m.push_back(line);
1177 
1178  return ret;
1179 }
1180 
1182 {
1183  std::vector< DocLine >::iterator last = m.end();
1184  current_line = m.begin();
1185 
1186  while (current_line != last)
1187  {
1188  if (current_line->line_type == DocLine::COMMENT)
1189  remove_current();
1190  else
1191  current_line++;
1192  }
1193 
1194  current_line = m.begin();
1195 }
1196 
1198 {
1199  DocFile result, aux;
1200  int i;
1201  int rnd_indx;
1202 
1204  if (no_lines == 0)
1205  return aux;
1206 
1207  aux = *this;
1208 
1209  for (i = no_lines; i > 0; i--)
1210  {
1211  // Jump a random number from the beginning
1212  rnd_indx = (int) rnd_unif(0, i);
1213  aux.go_first_data_line();
1214  aux.jump(rnd_indx);
1215 
1216  result.m.push_back(*aux.current_line);
1217  aux.current_line->line_type = DocLine::NOT_CONSIDERED;
1218  }
1219 
1220  // Adjust remaining fields
1221  result.no_lines = no_lines;
1222  result.current_line = result.m.begin();
1223  result.renum();
1224 
1225  return result;
1226 }
1227 
1228 void DocFile::perturb_column(int col, double sigma)
1229 {
1230 
1232 
1233  std::vector< DocLine >::iterator current = m.begin();
1234  std::vector< DocLine >::iterator last = m.end();
1235 
1236  while (current != last)
1237  {
1238  if (current->line_type == DocLine::DATALINE)
1239  (*current)[col] += rnd_gaus(0., sigma);
1240  current++;
1241  }
1242 
1243 }
1244 
1245 //merge
1246 void DocFile::merge(const FileName& name, int mode, int sumcol)
1247 {
1248  DocFile DFaux(name);
1249  merge(DFaux, mode, sumcol);
1250 }
1251 
1252 //merge
1253 void DocFile::merge(DocFile& DF, int mode, int sumcol)
1254 {
1255 
1256  DocLine DL, DLold;
1257  MetaDataVec SF;
1258  FileName fn_img;
1259  double w;
1260 
1261  DF.get_selfile(SF);
1262  for (size_t objId : SF.ids())
1263  {
1264  SF.getValue(MDL_IMAGE,fn_img, objId);
1265  DF.search_comment(fn_img);
1266  DL=DF.get_current_line();
1267  if (search_comment(fn_img))
1268  {
1269  switch (mode)
1270  {
1271  case DOCMERGE_KEEP_OLD:
1272  {
1273  // just keep what's there and do nothing
1274  break;
1275  }
1276  case DOCMERGE_KEEP_NEW:
1277  {
1278  //Replace current data line with the new one
1279  (*current_line) = DL;
1280  break;
1281  }
1282  case DOCMERGE_SUM_COLUMN:
1283  {
1284  // Just sum column
1285  w = (*current_line).data[sumcol] + DF(sumcol);
1286  (*current_line).set(sumcol, w);
1287  break;
1288  }
1289  case DOCMERGE_ERROR:
1290  std::cerr<<"image name = "<<fn_img;
1291  REPORT_ERROR(ERR_DOCFILE,"Image occurred in two docfiles to be merged");
1292  }
1293  }
1294  else
1295 {
1296  // Just add new line at the end of the file
1297  current_line = m.end();
1298  append_comment(fn_img);
1299  append_line(DL);
1300  }
1301  }
1302 }
1303 
1305 {
1306  DocFile result;
1307  int i, rnd_indx;
1308 
1309  result = *this;
1311  n = std::min(n, no_lines);
1312 
1313  for (i = 0; i < n; i++)
1314  {
1315  // Jump a random number from the beginning
1316  rnd_indx = (int) rnd_unif(0, result.no_lines);
1317  result.go_first_data_line();
1318  result.jump(rnd_indx);
1319  result.remove_current();
1320  }
1321  result.go_beginning();
1322 
1323  return result;
1324 }
1325 
1327 {
1328  Matrix1D< double > result(no_lines);
1329 
1330  std::vector< DocLine >::iterator current = m.begin();
1331  std::vector< DocLine >::iterator last = m.end();
1332  int i = 0;
1333 
1334  while (current != last)
1335  {
1336  if (current->line_type == DocLine::DATALINE)
1337  VEC_ELEM(result, i++) = (*current)[c];
1338 
1339  current++;
1340  }
1341 
1342  return result;
1343 }
1344 
1346 {
1347  Matrix1D< double > result;
1348  std::vector< DocLine >::iterator it = find(k);
1349  if (it == m.end())
1350  return result;
1351 
1352  result.resize(it->data.size());
1353  result.setRow();
1354 
1355  for (size_t i = 0; i < VEC_XSIZE(result); i++)
1356  VEC_ELEM(result, i) = it->data[i];
1357 
1358  return result;
1359 }
1360 
1362 {
1363  go_first_data_line();
1364 
1365  for (size_t i = 0; i <VEC_XSIZE(v); i++)
1366  {
1367  set(c, VEC_ELEM(v, i));
1368  next_data_line();
1369  }
1370  renum();
1371 
1372  if (VEC_XSIZE(v) < m.size())
1373  REPORT_ERROR(ERR_DOCFILE, "DocFile::setCol(): Column assignment not complete");
1374 }
1375 
1376 int read_Euler_document_file(FileName name, std::string ang1, std::string ang2,
1377  std::string ang3, DocFile& doc)
1378 {
1379  DocFile aux(name);
1380  DocLine line1, line2;
1381 
1382  // Set line2 as a data line and go to the beginning of file
1383  line2.set_type(DocLine::DATALINE);
1384  aux.go_beginning();
1385 
1386  // Macro to assign the angle from line1 in the correct place of line2
1387  // The angle order in line2 is (rot, tilt, psi)
1388 #define assign_in_correct_place_of_line2(angle_descr,angle_index) \
1389  switch (angle_descr[0]) \
1390  { \
1391  case ('r'): line2.set(0, line1[angle_index]); break; \
1392  case ('t'): line2.set(1, line1[angle_index]); break; \
1393  case ('p'): line2.set(2, line1[angle_index]); break; \
1394  }
1395 
1396  // Read the whole file
1397  while (!aux.eof())
1398  {
1399  line1 = aux.get_current_line();
1400 
1401  // If line1 type is neither DATALINE nor COMMENT the line is skipped!!
1402  if (line1.Is_data())
1403  {
1404  // Reorder the angles and insert
1408 
1409  doc.append_line(line2);
1410 
1411  }
1412  else if (line1.Is_comment())
1413  // Insert the comment
1414  doc.append_line(line1);
1415 
1416  // Next line
1417  aux.next();
1418  }
1419 
1420  return doc.dataLineNo();
1421 }
1422 
1423 void select_images(DocFile& doc, MetaData& sel, int col, bool en_limit0,
1424  double limit0, bool en_limitF, double limitF)
1425 {
1426  doc.go_first_data_line();
1427  int enabled;
1428  for (size_t objId : sel.ids())
1429  {
1430  sel.getValue(MDL_ENABLED,enabled, objId);
1431  if (enabled == 1)
1432  {
1433  if ((en_limit0 && doc(col) < limit0) || (en_limitF && doc(col) > limitF))
1434  sel.setValue(MDL_ENABLED, -1, objId);
1435  }
1436 
1437  doc.next_data_line();
1438  }
1439 }
1440 
1441 void get_subset_docfile(DocFile& DFin, MetaData& SF, DocFile& DFout)
1442 {
1443  DocLine DL;
1444  FileName fn_tmp;
1445 
1446  DFout.clear();
1447  DFin.go_beginning();
1448  DL = DFin.get_current_line();
1449  if (DL.Is_comment())
1450  fn_tmp = DL.get_text();
1451  if (strstr(fn_tmp.c_str(), "Headerinfo") == nullptr)
1452  REPORT_ERROR(ERR_DOCFILE,"Input docfile is not of NewXmipp-style");
1453  else
1454  // append the same header to DFout
1455  DFout.append_comment(fn_tmp.removeSubstring(" ; "));
1456 
1457  for (size_t objId : SF.ids())
1458  {
1459  SF.getValue(MDL_IMAGE,fn_tmp, objId);
1460  if (DFin.search_comment(fn_tmp))
1461  {
1462  DFout.append_comment(fn_tmp);
1463  DL=DFin.get_current_line();
1464  DFout.append_line(DL);
1465  }
1466  else
1467  REPORT_ERROR(ERR_DOCFILE, (std::string)"Docfile: Cannot find " + fn_tmp + " in docfile ");
1468  }
1469 }
1470 
void selfApplyGeometry(int Splinedegree, MultidimArray< std::complex< double > > &V1, const Matrix2D< double > &A, bool inv, bool wrap, std::complex< double > outside)
void locate(int _key)
Definition: docfile.cpp:499
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
bool isIdentity() const
Definition: matrix2d.cpp:1323
void setRot(double rot, const size_t n=0)
void min(Image< double > &op1, const Image< double > &op2)
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void go_beginning()
Definition: docfile.h:378
void perturb_column(int col, double sigma)
Definition: docfile.cpp:1228
virtual size_t addObject()=0
void get_image(int key, Image< double > &I, bool apply_geo=false)
Definition: docfile.cpp:800
DocLine get_current_line()
Definition: docfile.h:796
std::string get_text()
Definition: docfile.h:135
virtual void clear()
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
int read_Euler_document_file(FileName name, std::string ang1, std::string ang2, std::string ang3, DocFile &doc)
Definition: docfile.cpp:1376
Matrix1D< double > row(int _key)
Definition: docfile.cpp:1345
void read(const FileName &_name, int overrinding=1)
Definition: docfile.cpp:328
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
doublereal * c
std::vector< SelLine >::iterator find(std::vector< SelLine > &text, const std::string &img_name)
Definition: selfile.cpp:553
Couldn&#39;t write to file.
Definition: xmipp_error.h:140
int append_angles(double rot, double tilt, double psi, const std::string &ang1, const std::string &ang2, const std::string &ang3)
Definition: docfile.cpp:1014
void readFloatList(const char *str, int N, std::vector< T > &v)
Definition: args.h:104
int insert_data_line(int no_lines_to_insert=1)
Definition: docfile.cpp:911
DocFile random_discard(int N)
Definition: docfile.cpp:1304
void show_line(std::ostream &o, int key=-1)
Definition: docfile.cpp:232
void get_selfile(MetaData &SF)
Definition: docfile.cpp:475
doublereal * w
String integerToString(int I, int _width, char fill_with)
#define DOCMERGE_SUM_COLUMN
Definition: docfile.h:1051
virtual IdIteratorProxy< false > ids()
int get_last_key()
Definition: docfile.cpp:552
void setPsi(double psi, const size_t n=0)
void get_angles(int _key, double &rot, double &tilt, double &psi, const std::string &ang1, const std::string &ang2, const std::string &ang3)
Definition: docfile.cpp:581
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0
#define i
Is this image enabled? (int [-1 or 1])
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
void next_data_line()
Definition: docfile.h:442
void setCol(int _col, Matrix1D< double > &v)
Definition: docfile.cpp:1361
int dataLineNo() const
Definition: docfile.h:581
double operator()(int i) const
Definition: docfile.h:674
#define DOCMERGE_ERROR
Definition: docfile.h:1052
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
double rnd_unif()
int insert_line(const DocLine &DL)
Definition: docfile.cpp:959
glob_log first
void write(const FileName &_name="")
Definition: docfile.cpp:389
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
void get_angles2(int _key, double &rot, double &tilt, double &psi, const std::string &ang1, const std::string &ang2, const std::string &ang3)
Definition: docfile.cpp:692
void set_angles(int _key, double rot, double tilt, double psi, const std::string &ang1, const std::string &ang2, const std::string &ang3)
Definition: docfile.cpp:746
void setYoff(double yoff, const size_t n=0)
DocFile & operator=(const Matrix2D< double > &A)
Definition: docfile.cpp:190
void set(int i, double val)
Definition: docfile.cpp:847
Matrix1D< double > col(int _col)
Definition: docfile.cpp:1326
int in
float textToFloat(const char *str)
void setFlip(bool flip, const size_t n=0)
int append_data_line(int no_lines_to_append=1)
Definition: docfile.cpp:975
FileName get_imagename(int key)
Definition: docfile.cpp:833
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
void read(std::istream &i)
Definition: docfile.cpp:129
void clear()
Definition: docfile.cpp:181
void tokenize(const String &str, StringVector &tokens, const String &delimiters)
void clean_comments()
Definition: docfile.cpp:1181
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
#define DOCMERGE_KEEP_NEW
Definition: docfile.h:1050
int FirstLine_colNumber()
Definition: docfile.cpp:541
void remove_current()
Definition: docfile.cpp:896
int getColNumberFromHeader(const char *pattern)
Definition: docfile.cpp:514
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
#define j
double & operator[](size_t i)
Definition: docfile.cpp:34
friend std::ostream & operator<<(std::ostream &o, const DocLine &DL)
Definition: docfile.cpp:95
int append_line(DocLine &DL)
Definition: docfile.cpp:1167
int Is_comment()
Definition: docfile.h:165
int m
int search_comment(std::string comment)
Definition: docfile.cpp:426
bool getValue(MDObject &mdValueOut, size_t id) const override
void insert_comment(std::string comment)
Definition: docfile.cpp:948
bool setValue(const MDLabel label, const T &valueIn, size_t id)
void renum()
Definition: docfile.cpp:313
void setXoff(double xoff, const size_t n=0)
void setTilt(double tilt, const size_t n=0)
int Is_data()
Definition: docfile.h:172
void next()
Definition: docfile.h:416
void merge(const FileName &name, int mode=DOCMERGE_KEEP_OLD, int sumcol=5)
Definition: docfile.cpp:1246
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
int eof()
Definition: docfile.h:530
Error in docfile format.
Definition: xmipp_error.h:122
int remove_multiple_strings(std::string pattern)
Definition: docfile.cpp:449
double psi(const double x)
void append_comment(const std::string &comment)
Definition: docfile.cpp:1005
void adjust_to_data_line()
Definition: docfile.cpp:299
double rnd_gaus()
int textToInteger(const char *str)
void set(size_t i, double val)
Definition: docfile.cpp:58
void select_images(DocFile &doc, MetaData &sel, int col, bool en_limit0, double limit0, bool en_limitF, double limitF)
Definition: docfile.cpp:1423
void getTransformationMatrix(Matrix2D< double > &A, bool only_apply_shifts=false, const size_t n=0)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
FileName removeSubstring(const String &sub) const
unsigned int randomize_random_generator()
void jump(int how_many)
Definition: docfile.cpp:414
void remove(int _key0, int _keyF=-1)
Definition: docfile.cpp:872
String nextToken(const String &str, size_t &i)
void clear()
Definition: docfile.cpp:87
Incorrect value received.
Definition: xmipp_error.h:195
void get_angles1(int _key, double &rot, double &tilt, double &psi, const std::string &ang1, const std::string &ang2, const std::string &ang3)
Definition: docfile.cpp:637
int * n
Name of an image (std::string)
DocFile randomize()
Definition: docfile.cpp:1197
#define assign_in_correct_place_of_line2(angle_descr, angle_index)
void setRow()
Definition: matrix1d.h:543
void debug()
Definition: docfile.cpp:251