]> git.donarmstrong.com Git - mothur.git/blob - sffinfocommand.cpp
added load.logfile command. changed summary.single output for subsample=t.
[mothur.git] / sffinfocommand.cpp
1 /*
2  *  sffinfocommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 7/7/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "sffinfocommand.h"
11 #include "endiannessmacros.h"
12
13 //**********************************************************************************************************************
14 vector<string> SffInfoCommand::setParameters(){ 
15         try {           
16                 CommandParameter psff("sff", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psff);
17                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
18                 CommandParameter psfftxt("sfftxt", "String", "", "", "", "", "",false,false); parameters.push_back(psfftxt);
19                 CommandParameter pflow("flow", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pflow);
20                 CommandParameter ptrim("trim", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(ptrim);
21                 CommandParameter pfasta("fasta", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pfasta);
22                 CommandParameter pqfile("name", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pqfile);
23                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
25                 
26                 vector<string> myArray;
27                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
28                 return myArray;
29         }
30         catch(exception& e) {
31                 m->errorOut(e, "SffInfoCommand", "setParameters");
32                 exit(1);
33         }
34 }
35 //**********************************************************************************************************************
36 string SffInfoCommand::getHelpString(){ 
37         try {
38                 string helpString = "";
39                 helpString += "The sffinfo command reads a sff file and extracts the sequence data, or you can use it to parse a sfftxt file.\n";
40                 helpString += "The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, and trim. sff is required. \n";
41                 helpString += "The sff parameter allows you to enter the sff file you would like to extract data from.  You may enter multiple files by separating them by -'s.\n";
42                 helpString += "The fasta parameter allows you to indicate if you would like a fasta formatted file generated.  Default=True. \n";
43                 helpString += "The qfile parameter allows you to indicate if you would like a quality file generated.  Default=True. \n";
44                 helpString += "The flow parameter allows you to indicate if you would like a flowgram file generated.  Default=True. \n";
45                 helpString += "The sfftxt parameter allows you to indicate if you would like a sff.txt file generated.  Default=False. \n";
46                 helpString += "If you want to parse an existing sfftxt file into flow, fasta and quality file, enter the file name using the sfftxt parameter. \n";
47                 helpString += "The trim parameter allows you to indicate if you would like a sequences and quality scores trimmed to the clipQualLeft and clipQualRight values.  Default=True. \n";
48                 helpString += "The accnos parameter allows you to provide a accnos file containing the names of the sequences you would like extracted. You may enter multiple files by separating them by -'s. \n";
49                 helpString += "Example sffinfo(sff=mySffFile.sff, trim=F).\n";
50                 helpString += "Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n";
51                 return helpString;
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "SffInfoCommand", "getHelpString");
55                 exit(1);
56         }
57 }
58 //**********************************************************************************************************************
59 string SffInfoCommand::getOutputFileNameTag(string type, string inputName=""){  
60         try {
61         string outputFileName = "";
62                 map<string, vector<string> >::iterator it;
63         
64         //is this a type this command creates
65         it = outputTypes.find(type);
66         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
67         else {
68             if (type == "fasta")            {   outputFileName =  "fasta";   }
69             else if (type == "flow")    {   outputFileName =  "flow";   }
70             else if (type == "sfftxt")        {   outputFileName =  "sff.txt";   }
71             else if (type == "qfile")       {   outputFileName =  "qual";   }
72              else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
73         }
74         return outputFileName;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "SffInfoCommand", "getOutputFileNameTag");
78                 exit(1);
79         }
80 }
81
82
83 //**********************************************************************************************************************
84 SffInfoCommand::SffInfoCommand(){       
85         try {
86                 abort = true; calledHelp = true; 
87                 setParameters();
88                 vector<string> tempOutNames;
89                 outputTypes["fasta"] = tempOutNames;
90                 outputTypes["flow"] = tempOutNames;
91                 outputTypes["sfftxt"] = tempOutNames;
92                 outputTypes["qfile"] = tempOutNames;
93         }
94         catch(exception& e) {
95                 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
96                 exit(1);
97         }
98 }
99 //**********************************************************************************************************************
100
101 SffInfoCommand::SffInfoCommand(string option)  {
102         try {
103                 abort = false; calledHelp = false;   
104                 hasAccnos = false;
105                 
106                 //allow user to run help
107                 if(option == "help") { help(); abort = true; calledHelp = true; }
108                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
109                 
110                 else {
111                         //valid paramters for this command
112                         vector<string> myArray = setParameters();
113                         
114                         OptionParser parser(option);
115                         map<string, string> parameters = parser.getParameters();
116                         
117                         ValidParameters validParameter;
118                         //check to make sure all parameters are valid for command
119                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
120                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
121                         }
122                         
123                         //initialize outputTypes
124                         vector<string> tempOutNames;
125                         outputTypes["fasta"] = tempOutNames;
126                         outputTypes["flow"] = tempOutNames;
127                         outputTypes["sfftxt"] = tempOutNames;
128                         outputTypes["qfile"] = tempOutNames;
129                         
130                         //if the user changes the output directory command factory will send this info to us in the output parameter 
131                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
132                         
133                         //if the user changes the input directory command factory will send this info to us in the output parameter 
134                         string inputDir = validParameter.validFile(parameters, "inputdir", false);        if (inputDir == "not found"){ inputDir = "";          }
135
136                         sffFilename = validParameter.validFile(parameters, "sff", false);
137                         if (sffFilename == "not found") { sffFilename = "";  }
138                         else { 
139                                 m->splitAtDash(sffFilename, filenames);
140                                 
141                                 //go through files and make sure they are good, if not, then disregard them
142                                 for (int i = 0; i < filenames.size(); i++) {
143                                         bool ignore = false;
144                                         if (filenames[i] == "current") { 
145                                                 filenames[i] = m->getSFFFile(); 
146                                                 if (filenames[i] != "") {  m->mothurOut("Using " + filenames[i] + " as input file for the sff parameter where you had given current."); m->mothurOutEndLine(); }
147                                                 else {  
148                                                         m->mothurOut("You have no current sfffile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
149                                                         //erase from file list
150                                                         filenames.erase(filenames.begin()+i);
151                                                         i--;
152                                                 }
153                                         }
154                                         
155                                         if (!ignore) {
156                                                 if (inputDir != "") {
157                                                         string path = m->hasPath(filenames[i]);
158                                                         //if the user has not given a path then, add inputdir. else leave path alone.
159                                                         if (path == "") {       filenames[i] = inputDir + filenames[i];         }
160                                                 }
161                 
162                                                 ifstream in;
163                                                 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
164                                         
165                                                 //if you can't open it, try default location
166                                                 if (ableToOpen == 1) {
167                                                         if (m->getDefaultPath() != "") { //default path is set
168                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
169                                                                 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
170                                                                 ifstream in2;
171                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
172                                                                 in2.close();
173                                                                 filenames[i] = tryPath;
174                                                         }
175                                                 }
176                                                 
177                                                 //if you can't open it, try default location
178                                                 if (ableToOpen == 1) {
179                                                         if (m->getOutputDir() != "") { //default path is set
180                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
181                                                                 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
182                                                                 ifstream in2;
183                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
184                                                                 in2.close();
185                                                                 filenames[i] = tryPath;
186                                                         }
187                                                 }
188                                                 
189                                                 in.close();
190                                                 
191                                                 if (ableToOpen == 1) { 
192                                                         m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
193                                                         //erase from file list
194                                                         filenames.erase(filenames.begin()+i);
195                                                         i--;
196                                                 }else { m->setSFFFile(filenames[i]); }
197                                         }
198                                 }
199                                 
200                                 //make sure there is at least one valid file left
201                                 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
202                         }
203                         
204                         accnosName = validParameter.validFile(parameters, "accnos", false);
205                         if (accnosName == "not found") { accnosName = "";  }
206                         else { 
207                                 hasAccnos = true;
208                                 m->splitAtDash(accnosName, accnosFileNames);
209                                 
210                                 //go through files and make sure they are good, if not, then disregard them
211                                 for (int i = 0; i < accnosFileNames.size(); i++) {
212                                         bool ignore = false;
213                                         if (accnosFileNames[i] == "current") { 
214                                                 accnosFileNames[i] = m->getAccnosFile(); 
215                                                 if (accnosFileNames[i] != "") {  m->mothurOut("Using " + accnosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
216                                                 else {  
217                                                         m->mothurOut("You have no current accnosfile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
218                                                         //erase from file list
219                                                         accnosFileNames.erase(accnosFileNames.begin()+i);
220                                                         i--;
221                                                 }
222                                         }
223                                         
224                                         if (!ignore) {
225                                         
226                                                 if (inputDir != "") {
227                                                         string path = m->hasPath(accnosFileNames[i]);
228                                                         //if the user has not given a path then, add inputdir. else leave path alone.
229                                                         if (path == "") {       accnosFileNames[i] = inputDir + accnosFileNames[i];             }
230                                                 }
231                 
232                                                 ifstream in;
233                                                 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
234                                         
235                                                 //if you can't open it, try default location
236                                                 if (ableToOpen == 1) {
237                                                         if (m->getDefaultPath() != "") { //default path is set
238                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
239                                                                 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
240                                                                 ifstream in2;
241                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
242                                                                 in2.close();
243                                                                 accnosFileNames[i] = tryPath;
244                                                         }
245                                                 }
246                                                 //if you can't open it, try default location
247                                                 if (ableToOpen == 1) {
248                                                         if (m->getOutputDir() != "") { //default path is set
249                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
250                                                                 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
251                                                                 ifstream in2;
252                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
253                                                                 in2.close();
254                                                                 accnosFileNames[i] = tryPath;
255                                                         }
256                                                 }
257                                                 in.close();
258                                                 
259                                                 if (ableToOpen == 1) { 
260                                                         m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
261                                                         //erase from file list
262                                                         accnosFileNames.erase(accnosFileNames.begin()+i);
263                                                         i--;
264                                                 }
265                                         }
266                                 }
267                                 
268                                 //make sure there is at least one valid file left
269                                 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
270                         }
271                         
272                         if (hasAccnos) {
273                                 if (accnosFileNames.size() != filenames.size()) { abort = true; m->mothurOut("If you provide a accnos file, you must have one for each sff file."); m->mothurOutEndLine(); }
274                         }
275                         
276                         string temp = validParameter.validFile(parameters, "qfile", false);                     if (temp == "not found"){       temp = "T";                             }
277                         qual = m->isTrue(temp); 
278                         
279                         temp = validParameter.validFile(parameters, "fasta", false);                            if (temp == "not found"){       temp = "T";                             }
280                         fasta = m->isTrue(temp); 
281                         
282                         temp = validParameter.validFile(parameters, "flow", false);                                     if (temp == "not found"){       temp = "T";                             }
283                         flow = m->isTrue(temp); 
284                         
285                         temp = validParameter.validFile(parameters, "trim", false);                                     if (temp == "not found"){       temp = "T";                             }
286                         trim = m->isTrue(temp); 
287                         
288                         temp = validParameter.validFile(parameters, "sfftxt", false);                           
289                         if (temp == "not found")        {       temp = "F";      sfftxt = false; sfftxtFilename = "";           }
290                         else if (m->isTrue(temp))       {       sfftxt = true;          sfftxtFilename = "";                            }
291                         else {
292                                 //you are a filename
293                                 if (inputDir != "") {
294                                         map<string,string>::iterator it = parameters.find("sfftxt");
295                                         //user has given a template file
296                                         if(it != parameters.end()){ 
297                                                 string path = m->hasPath(it->second);
298                                                 //if the user has not given a path then, add inputdir. else leave path alone.
299                                                 if (path == "") {       parameters["sfftxt"] = inputDir + it->second;           }
300                                         }
301                                 }
302                                 
303                                 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
304                                 if (sfftxtFilename == "not found") { sfftxtFilename = "";  }
305                                 else if (sfftxtFilename == "not open") { sfftxtFilename = "";  }
306                         }
307                         
308                         if ((sfftxtFilename == "") && (filenames.size() == 0)) {  
309                                 //if there is a current sff file, use it
310                                 string filename = m->getSFFFile(); 
311                                 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
312                                 else {  m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true;  }
313                         }
314                 }
315         }
316         catch(exception& e) {
317                 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
318                 exit(1);
319         }
320 }
321 //**********************************************************************************************************************
322 int SffInfoCommand::execute(){
323         try {
324                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
325                 
326                 for (int s = 0; s < filenames.size(); s++) {
327                         
328                         if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);        } return 0; }
329                         
330                         int start = time(NULL);
331                         
332             filenames[s] = m->getFullPathName(filenames[s]);
333                         m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
334                         
335                         string accnos = "";
336                         if (hasAccnos) { accnos = accnosFileNames[s]; }
337                         
338                         int numReads = extractSffInfo(filenames[s], accnos);
339
340                         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
341                 }
342                 
343                 if (sfftxtFilename != "") {  parseSffTxt(); }
344                 
345                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);        } return 0; }
346                 
347                 //set fasta file as new current fastafile
348                 string current = "";
349                 itTypes = outputTypes.find("fasta");
350                 if (itTypes != outputTypes.end()) {
351                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
352                 }
353                 
354                 itTypes = outputTypes.find("qfile");
355                 if (itTypes != outputTypes.end()) {
356                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
357                 }
358                 
359                 itTypes = outputTypes.find("flow");
360                 if (itTypes != outputTypes.end()) {
361                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
362                 }
363                 
364                 //report output filenames
365                 m->mothurOutEndLine();
366                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
367                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
368                 m->mothurOutEndLine();
369
370                 return 0;
371         }
372         catch(exception& e) {
373                 m->errorOut(e, "SffInfoCommand", "execute");
374                 exit(1);
375         }
376 }
377 //**********************************************************************************************************************
378 int SffInfoCommand::extractSffInfo(string input, string accnos){
379         try {
380                 
381                 if (outputDir == "") {  outputDir += m->hasPath(input); }
382                 
383                 if (accnos != "")       {  readAccnosFile(accnos);  }
384                 else                            {       seqNames.clear();               }
385
386                 ofstream outSfftxt, outFasta, outQual, outFlow;
387                 string outFastaFileName, outQualFileName;
388         string rootName = outputDir + m->getRootName(m->getSimpleName(input));
389         if(rootName.find_last_of(".") == rootName.npos){ rootName += "."; }
390         
391                 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("sfftxt");
392                 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("flow");
393                 if (trim) {
394                         outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("fasta");
395                         outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("qfile");
396                 }else{
397                         outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("fasta");
398                         outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("qfile");
399                 }
400                 
401                 if (sfftxt) { m->openOutputFile(sfftxtFileName, outSfftxt); outSfftxt.setf(ios::fixed, ios::floatfield); outSfftxt.setf(ios::showpoint);  outputNames.push_back(sfftxtFileName);  outputTypes["sfftxt"].push_back(sfftxtFileName); }
402                 if (fasta)      { m->openOutputFile(outFastaFileName, outFasta);        outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
403                 if (qual)       { m->openOutputFile(outQualFileName, outQual);          outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName);  }
404                 if (flow)       { m->openOutputFile(outFlowFileName, outFlow);          outputNames.push_back(outFlowFileName);  outFlow.setf(ios::fixed, ios::floatfield); outFlow.setf(ios::showpoint); outputTypes["flow"].push_back(outFlowFileName);  }
405                 
406                 ifstream in;
407                 in.open(input.c_str(), ios::binary);
408                 
409                 CommonHeader header; 
410                 readCommonHeader(in, header);
411         
412                 int count = 0;
413                 mycount = 0;
414                 
415                 //check magic number and version
416                 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
417                 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
418         
419                 //print common header
420                 if (sfftxt) {   printCommonHeader(outSfftxt, header);           }
421                 if (flow)       {       outFlow << header.numFlowsPerRead << endl;      }
422                         
423                 //read through the sff file
424                 while (!in.eof()) {
425                         
426                         bool print = true;
427                         
428                         //read header
429                         Header readheader;
430                         readHeader(in, readheader);
431                         
432                         //read data
433                         seqRead read; 
434                         readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
435             bool okay = sanityCheck(readheader, read);
436             if (!okay) { break; }
437             
438                         //if you have provided an accosfile and this seq is not in it, then dont print
439                         if (seqNames.size() != 0) {   if (seqNames.count(readheader.name) == 0) { print = false; }  }
440                         
441                         //print 
442                         if (print) {
443                                 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
444                                 if (fasta)      {       printFastaSeqData(outFasta, read, readheader);  }
445                                 if (qual)       {       printQualSeqData(outQual, read, readheader);    }
446                                 if (flow)       {       printFlowSeqData(outFlow, read, readheader);    }
447                         }
448                         
449                         count++;
450                         mycount++;
451                 
452                         //report progress
453                         if((count+1) % 10000 == 0){     m->mothurOut(toString(count+1)); m->mothurOutEndLine();         }
454                 
455                         if (m->control_pressed) { count = 0; break;   }
456                         
457                         if (count >= header.numReads) { break; }
458                 }
459                 
460                 //report progress
461                 if (!m->control_pressed) {   if((count) % 10000 != 0){  m->mothurOut(toString(count)); m->mothurOutEndLine();           }  }
462                 
463                 in.close();
464                 
465                 if (sfftxt) {  outSfftxt.close();       }
466                 if (fasta)      {  outFasta.close();    }
467                 if (qual)       {  outQual.close();             }
468                 if (flow)       {  outFlow.close();             }
469                 
470                 return count;
471         }
472         catch(exception& e) {
473                 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
474                 exit(1);
475         }
476 }
477 //**********************************************************************************************************************
478 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
479         try {
480
481                 if (!in.eof()) {
482
483                         //read magic number
484                         char buffer[4];
485                         in.read(buffer, 4);
486                         header.magicNumber = be_int4(*(unsigned int *)(&buffer));
487                 
488                         //read version
489                         char buffer9[4];
490                         in.read(buffer9, 4);
491                         header.version = "";
492                         for (int i = 0; i < 4; i++) {  header.version += toString((int)(buffer9[i])); }
493                                 
494                         //read offset
495                         char buffer2 [8];
496                         in.read(buffer2, 8);
497                         header.indexOffset =  be_int8(*(unsigned long long *)(&buffer2));
498                         
499                         //read index length
500                         char buffer3 [4];
501                         in.read(buffer3, 4);
502                         header.indexLength =  be_int4(*(unsigned int *)(&buffer3));
503                         
504                         //read num reads
505                         char buffer4 [4];
506                         in.read(buffer4, 4);
507                         header.numReads =  be_int4(*(unsigned int *)(&buffer4));
508                                 
509                         //read header length
510                         char buffer5 [2];
511                         in.read(buffer5, 2);
512                         header.headerLength =  be_int2(*(unsigned short *)(&buffer5));
513                                         
514                         //read key length
515                         char buffer6 [2];
516                         in.read(buffer6, 2);
517                         header.keyLength = be_int2(*(unsigned short *)(&buffer6));
518                         
519                         //read number of flow reads
520                         char buffer7 [2];
521                         in.read(buffer7, 2);
522                         header.numFlowsPerRead =  be_int2(*(unsigned short *)(&buffer7));
523                                 
524                         //read format code
525                         char buffer8 [1];
526                         in.read(buffer8, 1);
527                         header.flogramFormatCode = (int)(buffer8[0]);
528                         
529                         //read flow chars
530                         char* tempBuffer = new char[header.numFlowsPerRead];
531                         in.read(&(*tempBuffer), header.numFlowsPerRead); 
532                         header.flowChars = tempBuffer;
533                         if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead);  }
534                         delete[] tempBuffer;
535                         
536                         //read key
537                         char* tempBuffer2 = new char[header.keyLength];
538                         in.read(&(*tempBuffer2), header.keyLength);
539                         header.keySequence = tempBuffer2;
540                         if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength);  }
541                         delete[] tempBuffer2;
542                                 
543                         /* Pad to 8 chars */
544                         unsigned long long spotInFile = in.tellg();
545                         unsigned long long spot = (spotInFile + 7)& ~7;  // ~ inverts
546                         in.seekg(spot);
547                         
548                 }else{
549                         m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
550                 }
551
552                 return 0;
553         }
554         catch(exception& e) {
555                 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
556                 exit(1);
557         }
558 }
559 //**********************************************************************************************************************
560 int SffInfoCommand::readHeader(ifstream& in, Header& header){
561         try {
562         
563                 if (!in.eof()) {
564                         
565                         //read header length
566                         char buffer [2];
567                         in.read(buffer, 2);
568                         header.headerLength = be_int2(*(unsigned short *)(&buffer));
569                                                 
570                         //read name length
571                         char buffer2 [2];
572                         in.read(buffer2, 2);
573                         header.nameLength = be_int2(*(unsigned short *)(&buffer2));
574
575                         //read num bases
576                         char buffer3 [4];
577                         in.read(buffer3, 4);
578                         header.numBases =  be_int4(*(unsigned int *)(&buffer3));
579                         
580                         //read clip qual left
581                         char buffer4 [2];
582                         in.read(buffer4, 2);
583                         header.clipQualLeft =  be_int2(*(unsigned short *)(&buffer4));
584                         header.clipQualLeft = 5; 
585                         
586                         //read clip qual right
587                         char buffer5 [2];
588                         in.read(buffer5, 2);
589                         header.clipQualRight =  be_int2(*(unsigned short *)(&buffer5));
590                         
591                         //read clipAdapterLeft
592                         char buffer6 [2];
593                         in.read(buffer6, 2);
594                         header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
595
596                         //read clipAdapterRight
597                         char buffer7 [2];
598                         in.read(buffer7, 2);
599                         header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
600                 
601                         //read name
602                         char* tempBuffer = new char[header.nameLength];
603                         in.read(&(*tempBuffer), header.nameLength);
604                         header.name = tempBuffer;
605                         if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength);  }
606                         delete[] tempBuffer;
607                         
608                         //extract info from name
609                         decodeName(header.timestamp, header.region, header.xy, header.name);
610                         
611                         /* Pad to 8 chars */
612                         unsigned long long spotInFile = in.tellg();
613                         unsigned long long spot = (spotInFile + 7)& ~7;
614                         in.seekg(spot);
615                         
616                 }else{
617                         m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
618                 }
619
620                 return 0;
621         }
622         catch(exception& e) {
623                 m->errorOut(e, "SffInfoCommand", "readHeader");
624                 exit(1);
625         }
626 }
627 //**********************************************************************************************************************
628 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
629         try {
630         
631                 if (!in.eof()) {
632         
633                         //read flowgram
634                         read.flowgram.resize(numFlowReads);
635                         for (int i = 0; i < numFlowReads; i++) {  
636                                 char buffer [2];
637                                 in.read(buffer, 2);
638                                 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
639                         }
640             
641                         //read flowIndex
642                         read.flowIndex.resize(numBases);
643                         for (int i = 0; i < numBases; i++) {  
644                                 char temp[1];
645                                 in.read(temp, 1);
646                                 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
647                         }
648         
649                         //read bases
650                         char* tempBuffer = new char[numBases];
651                         in.read(&(*tempBuffer), numBases);
652                         read.bases = tempBuffer;
653                         if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases);  }
654                         delete[] tempBuffer;
655
656                         //read qual scores
657                         read.qualScores.resize(numBases);
658                         for (int i = 0; i < numBases; i++) {  
659                                 char temp[1];
660                                 in.read(temp, 1);
661                                 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
662                         }
663         
664                         /* Pad to 8 chars */
665                         unsigned long long spotInFile = in.tellg();
666                         unsigned long long spot = (spotInFile + 7)& ~7;
667                         in.seekg(spot);
668                         
669                 }else{
670                         m->mothurOut("Error reading."); m->mothurOutEndLine();
671                 }
672
673                 return 0;
674         }
675         catch(exception& e) {
676                 m->errorOut(e, "SffInfoCommand", "readSeqData");
677                 exit(1);
678         }
679 }
680 //**********************************************************************************************************************
681 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
682         try {
683                 
684                 if (name.length() >= 6) {
685                         string time = name.substr(0, 6);
686                         unsigned int timeNum = m->fromBase36(time);
687                         
688                         int q1 = timeNum / 60;
689                         int sec = timeNum - 60 * q1;
690                         int q2 = q1 / 60;
691                         int minute = q1 - 60 * q2;
692                         int q3 = q2 / 24;
693                         int hr = q2 - 24 * q3;
694                         int q4 = q3 / 32;
695                         int day = q3 - 32 * q4;
696                         int q5 = q4 / 13;
697                         int mon = q4 - 13 * q5;
698                         int year = 2000 + q5;
699                 
700                         timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
701                 }
702                 
703                 if (name.length() >= 9) {
704                         region = name.substr(7, 2);
705                 
706                         string xyNum = name.substr(9);
707                         unsigned int myXy = m->fromBase36(xyNum);
708                         int x = myXy >> 12;
709                         int y = myXy & 4095;
710                 
711                         xy = toString(x) + "_" + toString(y);
712                 }
713                 
714                 return 0;
715         }
716         catch(exception& e) {
717                 m->errorOut(e, "SffInfoCommand", "decodeName");
718                 exit(1);
719         }
720 }
721 //**********************************************************************************************************************
722 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
723         try {
724         
725                 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
726                 out << "Version: " << header.version << endl;
727                 out << "Index Offset: " << header.indexOffset << endl;
728                 out << "Index Length: " << header.indexLength << endl;
729                 out << "Number of Reads: " << header.numReads << endl;
730                 out << "Header Length: " << header.headerLength << endl;
731                 out << "Key Length: " << header.keyLength << endl;
732                 out << "Number of Flows: " << header.numFlowsPerRead << endl;
733                 out << "Format Code: " << header.flogramFormatCode << endl;
734                 out << "Flow Chars: " << header.flowChars << endl;
735                 out << "Key Sequence: " << header.keySequence << endl << endl;
736                         
737                 return 0;
738         }
739         catch(exception& e) {
740                 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
741                 exit(1);
742         }
743 }
744 //**********************************************************************************************************************
745 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
746         try {
747                 
748                 out << ">" << header.name << endl;
749                 out << "Run Prefix: " << header.timestamp << endl;
750                 out << "Region #:  " << header.region << endl;
751                 out << "XY Location: " << header.xy << endl << endl;
752                 
753                 out << "Run Name:  " << endl;
754                 out << "Analysis Name:  " << endl;
755                 out << "Full Path: " << endl << endl;
756                 
757                 out << "Read Header Len: " << header.headerLength << endl;
758                 out << "Name Length: " << header.nameLength << endl;
759                 out << "# of Bases: " << header.numBases << endl;
760                 out << "Clip Qual Left: " << header.clipQualLeft << endl;
761                 out << "Clip Qual Right: " << header.clipQualRight << endl;
762                 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
763                 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
764                 
765                 return 0;
766         }
767         catch(exception& e) {
768                 m->errorOut(e, "SffInfoCommand", "printHeader");
769                 exit(1);
770         }
771 }
772 //**********************************************************************************************************************
773 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
774         try {
775         bool okay = true;
776         string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
777         
778         if (header.clipQualLeft > read.bases.length()) {
779             okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
780         }
781         if (header.clipQualRight > read.bases.length()) {
782             okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
783         }
784         if (header.clipQualLeft > read.qualScores.size()) {
785             okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
786         }
787         if (header.clipQualRight > read.qualScores.size()) {
788             okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
789         }
790         
791         if (okay == false) {
792             m->mothurOut(message); m->mothurOutEndLine();
793         }
794         
795                 return okay;
796         }
797         catch(exception& e) {
798                 m->errorOut(e, "SffInfoCommand", "sanityCheck");
799                 exit(1);
800         }
801 }
802 //**********************************************************************************************************************
803 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
804         try {
805                 out << "Flowgram: ";
806                 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t';  }
807                 
808                 out << endl <<  "Flow Indexes: ";
809                 int sum = 0;
810                 for (int i = 0; i < read.flowIndex.size(); i++) {  sum +=  read.flowIndex[i];  out << sum << '\t'; }
811                 
812                 //make the bases you want to clip lowercase and the bases you want to keep upper case
813                 if(header.clipQualRight == 0){  header.clipQualRight = read.bases.length();     }
814                 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
815                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   read.bases[i] = toupper(read.bases[i]);  }
816                 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) {   read.bases[i] = tolower(read.bases[i]);  }
817                 
818                 out << endl <<  "Bases: " << read.bases << endl << "Quality Scores: ";
819                 for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
820         
821                 
822                 out << endl << endl;
823                 
824                 return 0;
825         }
826         catch(exception& e) {
827                 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
828                 exit(1);
829         }
830 }
831 //**********************************************************************************************************************
832 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
833         try {
834                 string seq = read.bases;
835                 
836         if (trim) {
837                         if(header.clipQualRight < header.clipQualLeft){
838                                 seq = "NNNN";
839                         }
840                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
841                                 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
842                         }
843                         else {
844                                 seq = seq.substr(header.clipQualLeft-1);
845                         }
846                 }else{
847                         //if you wanted the sfftxt then you already converted the bases to the right case
848                         if (!sfftxt) {
849                                 //make the bases you want to clip lowercase and the bases you want to keep upper case
850                                 if(header.clipQualRight == 0){  header.clipQualRight = seq.length();    }
851                                 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]);  }
852                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++)  {   seq[i] = toupper(seq[i]);  }
853                                 for (int i = (header.clipQualRight-1); i < seq.length(); i++) {   seq[i] = tolower(seq[i]);  }
854                         }
855                 }
856                 
857                 out << ">" << header.name  << " xy=" << header.xy << endl;
858                 out << seq << endl;
859                 
860                 return 0;
861         }
862         catch(exception& e) {
863                 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
864                 exit(1);
865         }
866 }
867
868 //**********************************************************************************************************************
869 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
870         try {
871                 
872                 if (trim) {
873                         if(header.clipQualRight < header.clipQualLeft){
874                                 out << ">" << header.name << " xy=" << header.xy << endl;
875                                 out << "0\t0\t0\t0";
876                         }
877                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
878                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
879                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   out << read.qualScores[i] << '\t'; }
880                         }
881                         else{
882                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
883                                 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';   }                       
884                         }
885                 }else{
886                         out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
887                         for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
888                 }
889                 
890                 out << endl;
891                 
892                 return 0;
893         }
894         catch(exception& e) {
895                 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
896                 exit(1);
897         }
898 }
899
900 //**********************************************************************************************************************
901 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
902         try {
903                 if(header.clipQualRight > header.clipQualLeft){
904                         
905                         int rightIndex = 0;
906                         for (int i = 0; i < header.clipQualRight; i++) {  rightIndex +=  read.flowIndex[i];     }
907
908                         out << header.name << ' ' << rightIndex;
909                         for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100);  }
910                         out << endl;
911                 }
912                 
913                 
914                 return 0;
915         }
916         catch(exception& e) {
917                 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
918                 exit(1);
919         }
920 }
921 //**********************************************************************************************************************
922 int SffInfoCommand::readAccnosFile(string filename) {
923         try {
924                 //remove old names
925                 seqNames.clear();
926                 
927                 ifstream in;
928                 m->openInputFile(filename, in);
929                 string name;
930                 
931                 while(!in.eof()){
932                         in >> name; m->gobble(in);
933                                                 
934                         seqNames.insert(name);
935                         
936                         if (m->control_pressed) { seqNames.clear(); break; }
937                 }
938                 in.close();             
939                 
940                 return 0;
941         }
942         catch(exception& e) {
943                 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
944                 exit(1);
945         }
946 }
947 //**********************************************************************************************************************
948 int SffInfoCommand::parseSffTxt() {
949         try {
950                 
951                 ifstream inSFF;
952                 m->openInputFile(sfftxtFilename, inSFF);
953                 
954                 if (outputDir == "") {  outputDir += m->hasPath(sfftxtFilename); }
955                 
956                 //output file names
957                 ofstream outFasta, outQual, outFlow;
958                 string outFastaFileName, outQualFileName;
959                 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
960                 if (fileRoot.length() > 0) {
961                         //rip off last .
962                         fileRoot = fileRoot.substr(0, fileRoot.length()-1);
963                         fileRoot = m->getRootName(fileRoot);
964                 }
965                 
966                 string outFlowFileName = outputDir + fileRoot + getOutputFileNameTag("flow");
967                 if (trim) {
968                         outFastaFileName = outputDir + fileRoot + getOutputFileNameTag("fasta");
969                         outQualFileName = outputDir + fileRoot + getOutputFileNameTag("qfile");
970                 }else{
971                         outFastaFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("fasta");
972                         outQualFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("qfile");
973                 }
974                 
975                 if (fasta)      { m->openOutputFile(outFastaFileName, outFasta);        outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
976                 if (qual)       { m->openOutputFile(outQualFileName, outQual);          outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName);  }
977                 if (flow)       { m->openOutputFile(outFlowFileName, outFlow);          outputNames.push_back(outFlowFileName);  outFlow.setf(ios::fixed, ios::floatfield); outFlow.setf(ios::showpoint); outputTypes["flow"].push_back(outFlowFileName);  }
978                 
979                 //read common header
980                 string commonHeader = m->getline(inSFF);
981                 string magicNumber = m->getline(inSFF); 
982                 string version = m->getline(inSFF);
983                 string indexOffset = m->getline(inSFF);
984                 string indexLength = m->getline(inSFF);
985                 int numReads = parseHeaderLineToInt(inSFF);
986                 string headerLength = m->getline(inSFF);
987                 string keyLength = m->getline(inSFF);
988                 int numFlows = parseHeaderLineToInt(inSFF);
989                 string flowgramCode = m->getline(inSFF);
990                 string flowChars = m->getline(inSFF);
991                 string keySequence = m->getline(inSFF);
992                 m->gobble(inSFF);
993                 
994                 string seqName;
995                 
996                 if (flow)       {       outFlow << numFlows << endl;    }
997                 
998                 for(int i=0;i<numReads;i++){
999                         
1000                         //sanity check
1001                         if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
1002                         
1003                         Header header;
1004                         
1005                         //parse read header
1006                         inSFF >> seqName;
1007                         seqName = seqName.substr(1);
1008                         m->gobble(inSFF);
1009                         header.name = seqName;
1010                         
1011                         string runPrefix = parseHeaderLineToString(inSFF);              header.timestamp = runPrefix;
1012                         string regionNumber = parseHeaderLineToString(inSFF);   header.region = regionNumber;
1013                         string xyLocation = parseHeaderLineToString(inSFF);             header.xy = xyLocation;
1014                         m->gobble(inSFF);
1015                                 
1016                         string runName = parseHeaderLineToString(inSFF);
1017                         string analysisName = parseHeaderLineToString(inSFF);
1018                         string fullPath = parseHeaderLineToString(inSFF);
1019                         m->gobble(inSFF);
1020                         
1021                         string readHeaderLen = parseHeaderLineToString(inSFF);  convert(readHeaderLen, header.headerLength);
1022                         string nameLength = parseHeaderLineToString(inSFF);             convert(nameLength, header.nameLength);
1023                         int numBases = parseHeaderLineToInt(inSFF);                             header.numBases = numBases;
1024                         string clipQualLeft = parseHeaderLineToString(inSFF);   convert(clipQualLeft, header.clipQualLeft);
1025                         int clipQualRight = parseHeaderLineToInt(inSFF);                header.clipQualRight = clipQualRight;
1026                         string clipAdapLeft = parseHeaderLineToString(inSFF);   convert(clipAdapLeft, header.clipAdapterLeft);
1027                         string clipAdapRight = parseHeaderLineToString(inSFF);  convert(clipAdapRight, header.clipAdapterRight);
1028                         m->gobble(inSFF);
1029                                 
1030                         seqRead read;
1031                         
1032                         //parse read
1033                         vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows);      read.flowgram = flowVector;
1034                         vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases); 
1035                         
1036                         //adjust for print
1037                         vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1038                         for (int j = 1; j < flowIndices.size(); j++) {   flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]);   }
1039                         read.flowIndex = flowIndicesAdjusted;
1040                         
1041                         string bases = parseHeaderLineToString(inSFF);                                                                          read.bases = bases;
1042                         vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases);       read.qualScores = qualityScores;
1043                         m->gobble(inSFF);
1044                                         
1045                         //if you have provided an accosfile and this seq is not in it, then dont print
1046                         bool print = true;
1047                         if (seqNames.size() != 0) {   if (seqNames.count(header.name) == 0) { print = false; }  }
1048                         
1049                         //print 
1050                         if (print) {
1051                                 if (fasta)      {       printFastaSeqData(outFasta, read, header);      }
1052                                 if (qual)       {       printQualSeqData(outQual, read, header);        }
1053                                 if (flow)       {       printFlowSeqData(outFlow, read, header);        }
1054                         }
1055                         
1056                         //report progress
1057                         if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine();             }
1058                         
1059                         if (m->control_pressed) {  break;  }
1060                 }
1061                 
1062                 //report progress
1063                 if (!m->control_pressed) {   if((numReads) % 10000 != 0){       m->mothurOut(toString(numReads)); m->mothurOutEndLine();                }  }
1064                 
1065                 inSFF.close();
1066                 
1067                 if (fasta)      {  outFasta.close();    }
1068                 if (qual)       {  outQual.close();             }
1069                 if (flow)       {  outFlow.close();             }
1070                 
1071                 return 0;
1072         }
1073         catch(exception& e) {
1074                 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1075                 exit(1);
1076         }
1077 }
1078 //**********************************************************************************************************************
1079
1080 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1081         try {
1082                 int number;
1083                 
1084                 while (!file.eof())     {
1085                         
1086                         char c = file.get(); 
1087                         if (c == ':'){
1088                                 file >> number;
1089                                 break;
1090                         }
1091                         
1092                 }
1093                 m->gobble(file);
1094                 return number;
1095         }
1096         catch(exception& e) {
1097                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1098                 exit(1);
1099         }
1100         
1101 }
1102
1103 //**********************************************************************************************************************
1104
1105 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1106         try {
1107                 string text;
1108                 
1109                 while (!file.eof())     {
1110                         char c = file.get(); 
1111                         
1112                         if (c == ':'){
1113                                 //m->gobble(file);
1114                                 //text = m->getline(file);      
1115                                 file >> text;
1116                                 break;
1117                         }
1118                 }
1119                 m->gobble(file);
1120                 
1121                 return text;
1122         }
1123         catch(exception& e) {
1124                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1125                 exit(1);
1126         }
1127 }
1128
1129 //**********************************************************************************************************************
1130
1131 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1132         try {
1133                 vector<unsigned short> floatVector(length);
1134                 
1135                 while (!file.eof())     {
1136                         char c = file.get(); 
1137                         if (c == ':'){
1138                                 float temp;
1139                                 for(int i=0;i<length;i++){
1140                                         file >> temp;
1141                                         floatVector[i] = temp * 100;
1142                                 }
1143                                 break;
1144                         }
1145                 }
1146                 m->gobble(file);        
1147                 return floatVector;
1148         }
1149         catch(exception& e) {
1150                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1151                 exit(1);
1152         }
1153 }
1154
1155 //**********************************************************************************************************************
1156
1157 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1158         try {
1159                 vector<unsigned int> intVector(length);
1160                 
1161                 while (!file.eof())     {
1162                         char c = file.get(); 
1163                         if (c == ':'){
1164                                 for(int i=0;i<length;i++){
1165                                         file >> intVector[i];
1166                                 }
1167                                 break;
1168                         }
1169                 }
1170                 m->gobble(file);        
1171                 return intVector;
1172         }
1173         catch(exception& e) {
1174                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1175                 exit(1);
1176         }
1177 }
1178
1179 //**********************************************************************************************************************
1180
1181
1182                                 
1183