File: FileHeader.cpp

package info (click to toggle)
bitseq 0.7.5+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 676 kB
  • sloc: cpp: 7,043; python: 562; makefile: 150; sh: 52
file content (117 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<cstdlib>

#include "FileHeader.h"
#include "misc.h"

#include "common.h"

using namespace ns_fileHeader;

void FileHeader::skipEmptyLines() {//{{{
   if(!file) return;
   while(file->good() &&
         ((file->peek() == ' ') ||
          (file->peek() == '\n')))
      file->get();
}//}}}

bool FileHeader::readValues(ofstream *outF){//{{{
   if((file==NULL)||(!file->is_open())){
      error("FileHeader: Input file not opened for reading.\n");
      return false;
   }
   string line;
   vector<string> words;
   long value;
   char *chP;
   skipEmptyLines();
   while(file->good() && (file->peek() == '#')){
      // Read line.
      getline(*file, line);
      // If outF is defined, copy the header there.
      if(outF!=NULL)(*outF)<<line<<endl;
      skipEmptyLines();
      // Tokenize line into words.
      words = ns_misc::tokenize(line);
      // Store words as flags. Start with 1st word as the 0th one are hashes.
      // If word is followed by a numeric value, use it as a value for the flag.
      for(long i=1;i<(long)words.size();i++){
         // Only add new entry if it wasn't there already.
         if(values.count(words[i])==0)
            values[words[i]] = no_value;
         // See if next word is numeric and if so, then use it as a value.
         if(i+1<(long)words.size()){
            value = strtol(words[i+1].c_str(), &chP, 10);
            // Conversion was succesful the value is non-zero OR the pointer should point to end of string (null character).
            if((value!=0)||(*chP=='\0')) {
               // Save value and skip the number.
               values[words[i]] = value;
               i++;
            }
         }
      }
   }
   return true;
}//}}}

bool FileHeader::samplesHeader(long *n, long *m, bool *transposed, bool *logged){//{{{
   if(!readValues()){
      *n=0;
      *m=0;
      return false;
   }
   if(logged!=NULL)if(values.count("L"))*logged = true;
   if(values.count("T"))*transposed = true;
   if(values.count("M") && (values["M"]!=no_value))*m = values["M"];
   if(values.count("N") && (values["N"]!=no_value))*n = values["N"];
   return true;
}//}}}

bool FileHeader::transcriptsHeader(long *m, long *colN){//{{{
   if(!readValues()){
      *m=0;
      return false;
   }
   if(values.count("M") && (values["M"]!=no_value))*m = values["M"];
   if(colN!=NULL)
      if(values.count("colN") && (values["colN"]!=no_value))*colN = values["colN"];
   return true;
}//}}}

bool FileHeader::probHeader(long *Nmap, long *Ntotal, long *M, AlignmentFileType *format){//{{{
   if(!readValues()){
      *M=0;
      *Nmap=0;
      return false;
   }
   if(values.count("LOGFORMAT")){*format = LOG_FORMAT;}
   else if(values.count("NEWFORMAT")){*format = NEW_FORMAT;}
   else *format = OLD_FORMAT;
   if(values.count("Ntotal") && (values["Ntotal"]!=no_value))*Ntotal = values["Ntotal"];
   if(values.count("Nmap") && (values["Nmap"]!=no_value))*Nmap = values["Nmap"];
   if(values.count("M") && (values["M"]!=no_value))*M = values["M"];
   return true;
}//}}}

bool FileHeader::varianceHeader(long *m,bool *logged){//{{{
   if(!readValues()){
      *m=0;
      return false;
   }
   if(logged!=NULL)if(values.count("L"))*logged = true;
   if(values.count("M") && (values["M"]!=no_value))*m = values["M"];
   return true;
}//}}}

bool FileHeader::paramsHeader(long *parN, ofstream *outF){//{{{
   if(!readValues(outF)){
      *parN=0;
      return false;
   }
   *parN = 0;
   if(values.count("PN") && (values["PN"]!=no_value))*parN = values["PN"];
   return true;
}//}}}