]> git.donarmstrong.com Git - mothur.git/blob - sffinfocommand.cpp
added pipeline commands which involved change to command factory and command class...
[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::getValidParameters(){    
15         try {
16                 string Array[] =  {"sff","qfile","fasta","flow","trim","accnos","sfftxt","outputdir","inputdir", "outputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "SffInfoCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 SffInfoCommand::SffInfoCommand(){       
27         try {
28                 //initialize outputTypes
29                 vector<string> tempOutNames;
30                 outputTypes["fasta"] = tempOutNames;
31                 outputTypes["flow"] = tempOutNames;
32                 outputTypes["sfftxt"] = tempOutNames;
33                 outputTypes["qual"] = tempOutNames;
34         }
35         catch(exception& e) {
36                 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
37                 exit(1);
38         }
39 }
40 //**********************************************************************************************************************
41 vector<string> SffInfoCommand::getRequiredParameters(){ 
42         try {
43                 string Array[] =  {"sff"};
44                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45                 return myArray;
46         }
47         catch(exception& e) {
48                 m->errorOut(e, "SffInfoCommand", "getRequiredParameters");
49                 exit(1);
50         }
51 }
52 //**********************************************************************************************************************
53 vector<string> SffInfoCommand::getRequiredFiles(){      
54         try {
55                 vector<string> myArray;
56                 return myArray;
57         }
58         catch(exception& e) {
59                 m->errorOut(e, "SffInfoCommand", "getRequiredFiles");
60                 exit(1);
61         }
62 }
63 //**********************************************************************************************************************
64
65 SffInfoCommand::SffInfoCommand(string option)  {
66         try {
67                 abort = false;
68                 hasAccnos = false;
69                 
70                 //allow user to run help
71                 if(option == "help") { help(); abort = true; }
72                 
73                 else {
74                         //valid paramters for this command
75                         string Array[] =  {"sff","qfile","fasta","flow","trim","accnos","sfftxt","outputdir","inputdir", "outputdir"};
76                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
77                         
78                         OptionParser parser(option);
79                         map<string, string> parameters = parser.getParameters();
80                         
81                         ValidParameters validParameter;
82                         //check to make sure all parameters are valid for command
83                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["fasta"] = tempOutNames;
90                         outputTypes["flow"] = tempOutNames;
91                         outputTypes["sfftxt"] = tempOutNames;
92                         outputTypes["qual"] = tempOutNames;
93                         
94                         //if the user changes the output directory command factory will send this info to us in the output parameter 
95                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
96                         
97                         //if the user changes the input directory command factory will send this info to us in the output parameter 
98                         string inputDir = validParameter.validFile(parameters, "inputdir", false);        if (inputDir == "not found"){ inputDir = "";          }
99
100                         sffFilename = validParameter.validFile(parameters, "sff", false);
101                         if (sffFilename == "not found") { m->mothurOut("sff is a required parameter for the sffinfo command."); m->mothurOutEndLine(); abort = true;  }
102                         else { 
103                                 m->splitAtDash(sffFilename, filenames);
104                                 
105                                 //go through files and make sure they are good, if not, then disregard them
106                                 for (int i = 0; i < filenames.size(); i++) {
107                                         if (inputDir != "") {
108                                                 string path = m->hasPath(filenames[i]);
109                                                 //if the user has not given a path then, add inputdir. else leave path alone.
110                                                 if (path == "") {       filenames[i] = inputDir + filenames[i];         }
111                                         }
112         
113                                         ifstream in;
114                                         int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
115                                 
116                                         //if you can't open it, try default location
117                                         if (ableToOpen == 1) {
118                                                 if (m->getDefaultPath() != "") { //default path is set
119                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
120                                                         m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
121                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
122                                                         filenames[i] = tryPath;
123                                                 }
124                                         }
125                                         
126                                         //if you can't open it, try default location
127                                         if (ableToOpen == 1) {
128                                                 if (m->getOutputDir() != "") { //default path is set
129                                                         string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
130                                                         m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
131                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
132                                                         filenames[i] = tryPath;
133                                                 }
134                                         }
135                                         
136                                         in.close();
137                                         
138                                         if (ableToOpen == 1) { 
139                                                 m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
140                                                 //erase from file list
141                                                 filenames.erase(filenames.begin()+i);
142                                                 i--;
143                                         }
144                                 }
145                                 
146                                 //make sure there is at least one valid file left
147                                 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
148                         }
149                         
150                         accnosName = validParameter.validFile(parameters, "accnos", false);
151                         if (accnosName == "not found") { accnosName = "";  }
152                         else { 
153                                 hasAccnos = true;
154                                 m->splitAtDash(accnosName, accnosFileNames);
155                                 
156                                 //go through files and make sure they are good, if not, then disregard them
157                                 for (int i = 0; i < accnosFileNames.size(); i++) {
158                                         if (inputDir != "") {
159                                                 string path = m->hasPath(accnosFileNames[i]);
160                                                 //if the user has not given a path then, add inputdir. else leave path alone.
161                                                 if (path == "") {       accnosFileNames[i] = inputDir + accnosFileNames[i];             }
162                                         }
163         
164                                         ifstream in;
165                                         int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
166                                 
167                                         //if you can't open it, try default location
168                                         if (ableToOpen == 1) {
169                                                 if (m->getDefaultPath() != "") { //default path is set
170                                                         string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
171                                                         m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
172                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
173                                                         accnosFileNames[i] = tryPath;
174                                                 }
175                                         }
176                                         //if you can't open it, try default location
177                                         if (ableToOpen == 1) {
178                                                 if (m->getOutputDir() != "") { //default path is set
179                                                         string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
180                                                         m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
181                                                         ableToOpen = m->openInputFile(tryPath, in, "noerror");
182                                                         accnosFileNames[i] = tryPath;
183                                                 }
184                                         }
185                                         in.close();
186                                         
187                                         if (ableToOpen == 1) { 
188                                                 m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
189                                                 //erase from file list
190                                                 accnosFileNames.erase(accnosFileNames.begin()+i);
191                                                 i--;
192                                         }
193                                 }
194                                 
195                                 //make sure there is at least one valid file left
196                                 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
197                         }
198                         
199                         if (hasAccnos) {
200                                 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(); }
201                         }
202                         
203                         string temp = validParameter.validFile(parameters, "qfile", false);                     if (temp == "not found"){       temp = "T";                             }
204                         qual = m->isTrue(temp); 
205                         
206                         temp = validParameter.validFile(parameters, "fasta", false);                            if (temp == "not found"){       temp = "T";                             }
207                         fasta = m->isTrue(temp); 
208                         
209                         temp = validParameter.validFile(parameters, "flow", false);                                     if (temp == "not found"){       temp = "F";                             }
210                         flow = m->isTrue(temp); 
211                         
212                         temp = validParameter.validFile(parameters, "trim", false);                                     if (temp == "not found"){       temp = "T";                             }
213                         trim = m->isTrue(temp); 
214                         
215                         temp = validParameter.validFile(parameters, "sfftxt", false);                           if (temp == "not found"){       temp = "F";                             }
216                         sfftxt = m->isTrue(temp); 
217                 }
218         }
219         catch(exception& e) {
220                 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
221                 exit(1);
222         }
223 }
224 //**********************************************************************************************************************
225
226 void SffInfoCommand::help(){
227         try {
228                 m->mothurOut("The sffinfo command reads a sff file and extracts the sequence data.\n");
229                 m->mothurOut("The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, and trim. sff is required. \n");
230                 m->mothurOut("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");
231                 m->mothurOut("The fasta parameter allows you to indicate if you would like a fasta formatted file generated.  Default=True. \n");
232                 m->mothurOut("The qfile parameter allows you to indicate if you would like a quality file generated.  Default=True. \n");
233                 m->mothurOut("The flow parameter allows you to indicate if you would like a flowgram file generated.  Default=False. \n");
234                 m->mothurOut("The sfftxt parameter allows you to indicate if you would like a sff.txt file generated.  Default=False. \n");
235                 m->mothurOut("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");
236                 m->mothurOut("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");
237                 m->mothurOut("Example sffinfo(sff=mySffFile.sff, trim=F).\n");
238                 m->mothurOut("Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n\n");
239         }
240         catch(exception& e) {
241                 m->errorOut(e, "SffInfoCommand", "help");
242                 exit(1);
243         }
244 }
245 //**********************************************************************************************************************
246
247 SffInfoCommand::~SffInfoCommand(){}
248
249 //**********************************************************************************************************************
250 int SffInfoCommand::execute(){
251         try {
252                 
253                 if (abort == true) { return 0; }
254                 
255                 for (int s = 0; s < filenames.size(); s++) {
256                         
257                         if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());         } return 0; }
258                         
259                         int start = time(NULL);
260                         
261                         m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
262                         
263                         string accnos = "";
264                         if (hasAccnos) { accnos = accnosFileNames[s]; }
265                         
266                         int numReads = extractSffInfo(filenames[s], accnos);
267
268                         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
269                 }
270                 
271                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());         } return 0; }
272                 
273                 //report output filenames
274                 m->mothurOutEndLine();
275                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
276                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
277                 m->mothurOutEndLine();
278
279                 return 0;
280         }
281         catch(exception& e) {
282                 m->errorOut(e, "SffInfoCommand", "execute");
283                 exit(1);
284         }
285 }
286 //**********************************************************************************************************************
287 int SffInfoCommand::extractSffInfo(string input, string accnos){
288         try {
289                 
290                 if (outputDir == "") {  outputDir += m->hasPath(input); }
291                 
292                 if (accnos != "")       {  readAccnosFile(accnos);  }
293                 else                            {       seqNames.clear();               }
294
295                 ofstream outSfftxt, outFasta, outQual, outFlow;
296                 string outFastaFileName, outQualFileName;
297                 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "sff.txt";
298                 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "flow";
299                 if (trim) {
300                         outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "fasta";
301                         outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "qual";
302                 }else{
303                         outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.fasta";
304                         outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw.qual";
305                 }
306                 
307                 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); }
308                 if (fasta)      { m->openOutputFile(outFastaFileName, outFasta);        outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
309                 if (qual)       { m->openOutputFile(outQualFileName, outQual);          outputNames.push_back(outQualFileName); outputTypes["qual"].push_back(outQualFileName);  }
310                 if (flow)       { m->openOutputFile(outFlowFileName, outFlow);          outputNames.push_back(outFlowFileName);  outputTypes["flow"].push_back(outFlowFileName);  }
311                 
312                 ifstream in;
313                 in.open(input.c_str(), ios::binary);
314                 
315                 CommonHeader header; 
316                 readCommonHeader(in, header);
317                 
318                 int count = 0;
319                 
320                 //check magic number and version
321                 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
322                 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
323         
324                 //print common header
325                 if (sfftxt) { printCommonHeader(outSfftxt, header); }
326         
327                 //read through the sff file
328                 while (!in.eof()) {
329                         
330                         bool print = true;
331                         
332                         //read header
333                         Header readheader;
334                         readHeader(in, readheader);
335                         
336                         //read data
337                         seqRead read; 
338                         readSeqData(in, read, header.numFlowsPerRead, readheader.numBases);
339                                 
340                         //if you have provided an accosfile and this seq is not in it, then dont print
341                         if (seqNames.size() != 0) {   if (seqNames.count(readheader.name) == 0) { print = false; }  }
342                         
343                         //print 
344                         if (print) {
345                                 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
346                                 if (fasta)      {       printFastaSeqData(outFasta, read, readheader);  }
347                                 if (qual)       {       printQualSeqData(outQual, read, readheader);    }
348                                 if (flow)       {       printFlowSeqData(outFlow, read, readheader);    }
349                         }
350                         
351                         count++;
352                 
353                         //report progress
354                         if((count+1) % 10000 == 0){     m->mothurOut(toString(count+1)); m->mothurOutEndLine();         }
355                 
356                         if (m->control_pressed) { count = 0; break;   }
357                         
358                         if (count >= header.numReads) { break; }
359                 }
360                 
361                 //report progress
362                 if (!m->control_pressed) {   if((count) % 10000 != 0){  m->mothurOut(toString(count)); m->mothurOutEndLine();           }  }
363                 
364                 in.close();
365                 
366                 if (sfftxt) {  outSfftxt.close();       }
367                 if (fasta)      {  outFasta.close();    }
368                 if (qual)       {  outQual.close();             }
369                 if (flow)       {  outFlow.close();             }
370                 
371                 return count;
372         }
373         catch(exception& e) {
374                 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
375                 exit(1);
376         }
377 }
378 //**********************************************************************************************************************
379 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
380         try {
381
382                 if (!in.eof()) {
383
384                         //read magic number
385                         char buffer[4];
386                         in.read(buffer, 4);
387                         header.magicNumber = be_int4(*(unsigned int *)(&buffer));
388                 
389                         //read version
390                         char buffer9[4];
391                         in.read(buffer9, 4);
392                         header.version = "";
393                         for (int i = 0; i < 4; i++) {  header.version += toString((int)(buffer9[i])); }
394                                 
395                         //read offset
396                         char buffer2 [8];
397                         in.read(buffer2, 8);
398                         header.indexOffset =  be_int8(*(unsigned long int *)(&buffer2));
399                         
400                         //read index length
401                         char buffer3 [4];
402                         in.read(buffer3, 4);
403                         header.indexLength =  be_int4(*(unsigned int *)(&buffer3));
404                         
405                         //read num reads
406                         char buffer4 [4];
407                         in.read(buffer4, 4);
408                         header.numReads =  be_int4(*(unsigned int *)(&buffer4));
409                                 
410                         //read header length
411                         char buffer5 [2];
412                         in.read(buffer5, 2);
413                         header.headerLength =  be_int2(*(unsigned short *)(&buffer5));
414                                         
415                         //read key length
416                         char buffer6 [2];
417                         in.read(buffer6, 2);
418                         header.keyLength = be_int2(*(unsigned short *)(&buffer6));
419                         
420                         //read number of flow reads
421                         char buffer7 [2];
422                         in.read(buffer7, 2);
423                         header.numFlowsPerRead =  be_int2(*(unsigned short *)(&buffer7));
424                                 
425                         //read format code
426                         char buffer8 [1];
427                         in.read(buffer8, 1);
428                         header.flogramFormatCode = (int)(buffer8[0]);
429                         
430                         //read flow chars
431                         char* tempBuffer = new char[header.numFlowsPerRead];
432                         in.read(&(*tempBuffer), header.numFlowsPerRead); 
433                         header.flowChars = tempBuffer;
434                         if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead);  }
435                         delete[] tempBuffer;
436                         
437                         //read key
438                         char* tempBuffer2 = new char[header.keyLength];
439                         in.read(&(*tempBuffer2), header.keyLength);
440                         header.keySequence = tempBuffer2;
441                         if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength);  }
442                         delete[] tempBuffer2;
443                                 
444                         /* Pad to 8 chars */
445                         unsigned long int spotInFile = in.tellg();
446                         unsigned long int spot = (spotInFile + 7)& ~7;  // ~ inverts
447                         in.seekg(spot);
448                         
449                 }else{
450                         m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
451                 }
452
453                 return 0;
454         }
455         catch(exception& e) {
456                 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
457                 exit(1);
458         }
459 }
460 //**********************************************************************************************************************
461 int SffInfoCommand::readHeader(ifstream& in, Header& header){
462         try {
463         
464                 if (!in.eof()) {
465                         
466                         //read header length
467                         char buffer [2];
468                         in.read(buffer, 2);
469                         header.headerLength = be_int2(*(unsigned short *)(&buffer));
470                                                 
471                         //read name length
472                         char buffer2 [2];
473                         in.read(buffer2, 2);
474                         header.nameLength = be_int2(*(unsigned short *)(&buffer2));
475
476                         //read num bases
477                         char buffer3 [4];
478                         in.read(buffer3, 4);
479                         header.numBases =  be_int4(*(unsigned int *)(&buffer3));
480                         
481                         //read clip qual left
482                         char buffer4 [2];
483                         in.read(buffer4, 2);
484                         header.clipQualLeft =  be_int2(*(unsigned short *)(&buffer4));
485                         header.clipQualLeft = 5; 
486                         
487                         //read clip qual right
488                         char buffer5 [2];
489                         in.read(buffer5, 2);
490                         header.clipQualRight =  be_int2(*(unsigned short *)(&buffer5));
491                         
492                         //read clipAdapterLeft
493                         char buffer6 [2];
494                         in.read(buffer6, 2);
495                         header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
496
497                         //read clipAdapterRight
498                         char buffer7 [2];
499                         in.read(buffer7, 2);
500                         header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
501                 
502                         //read name
503                         char* tempBuffer = new char[header.nameLength];
504                         in.read(&(*tempBuffer), header.nameLength);
505                         header.name = tempBuffer;
506                         if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength);  }
507                         delete[] tempBuffer;
508                         
509                         /* Pad to 8 chars */
510                         unsigned long int spotInFile = in.tellg();
511                         unsigned long int spot = (spotInFile + 7)& ~7;
512                         in.seekg(spot);
513                         
514                 }else{
515                         m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
516                 }
517
518                 return 0;
519         }
520         catch(exception& e) {
521                 m->errorOut(e, "SffInfoCommand", "readHeader");
522                 exit(1);
523         }
524 }
525 //**********************************************************************************************************************
526 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
527         try {
528         
529                 if (!in.eof()) {
530         
531                         //read flowgram
532                         read.flowgram.resize(numFlowReads);
533                         for (int i = 0; i < numFlowReads; i++) {  
534                                 char buffer [2];
535                                 in.read(buffer, 2);
536                                 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
537                         }
538         
539                         //read flowIndex
540                         read.flowIndex.resize(numBases);
541                         for (int i = 0; i < numBases; i++) {  
542                                 char temp[1];
543                                 in.read(temp, 1);
544                                 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
545                         }
546         
547                         //read bases
548                         char* tempBuffer = new char[numBases];
549                         in.read(&(*tempBuffer), numBases);
550                         read.bases = tempBuffer;
551                         if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases);  }
552                         delete[] tempBuffer;
553
554                         //read qual scores
555                         read.qualScores.resize(numBases);
556                         for (int i = 0; i < numBases; i++) {  
557                                 char temp[1];
558                                 in.read(temp, 1);
559                                 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
560                         }
561         
562                         /* Pad to 8 chars */
563                         unsigned long int spotInFile = in.tellg();
564                         unsigned long int spot = (spotInFile + 7)& ~7;
565                         in.seekg(spot);
566                         
567                 }else{
568                         m->mothurOut("Error reading."); m->mothurOutEndLine();
569                 }
570
571                 return 0;
572         }
573         catch(exception& e) {
574                 m->errorOut(e, "SffInfoCommand", "readSeqData");
575                 exit(1);
576         }
577 }
578 //**********************************************************************************************************************
579 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
580         try {
581         
582                 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
583                 out << "Version: " << header.version << endl;
584                 out << "Index Offset: " << header.indexOffset << endl;
585                 out << "Index Length: " << header.indexLength << endl;
586                 out << "Number of Reads: " << header.numReads << endl;
587                 out << "Header Length: " << header.headerLength << endl;
588                 out << "Key Length: " << header.keyLength << endl;
589                 out << "Number of Flows: " << header.numFlowsPerRead << endl;
590                 out << "Format Code: " << header.flogramFormatCode << endl;
591                 out << "Flow Chars: " << header.flowChars << endl;
592                 out << "Key Sequence: " << header.keySequence << endl << endl;
593                         
594                 return 0;
595         }
596         catch(exception& e) {
597                 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
598                 exit(1);
599         }
600 }
601 //**********************************************************************************************************************
602 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
603         try {
604                 
605                 out << ">" << header.name << endl;
606                 out << "Run Prefix: " << endl;
607                 out << "Region #:  " << endl;
608                 out << "XY Location: " << endl << endl;
609                 
610                 out << "Run Name:  " << endl;
611                 out << "Analysis Name:  " << endl;
612                 out << "Full Path: " << endl << endl;
613                 
614                 out << "Read Header Len: " << header.headerLength << endl;
615                 out << "Name Length: " << header.nameLength << endl;
616                 out << "# of Bases: " << header.numBases << endl;
617                 out << "Clip Qual Left: " << header.clipQualLeft << endl;
618                 out << "Clip Qual Right: " << header.clipQualRight << endl;
619                 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
620                 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
621                 
622                 return 0;
623         }
624         catch(exception& e) {
625                 m->errorOut(e, "SffInfoCommand", "printHeader");
626                 exit(1);
627         }
628 }
629
630 //**********************************************************************************************************************
631 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
632         try {
633                 
634                 out << "Flowgram: ";
635                 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t';  }
636                 
637                 out << endl <<  "Flow Indexes: ";
638                 int sum = 0;
639                 for (int i = 0; i < read.flowIndex.size(); i++) {  sum +=  read.flowIndex[i];  out << sum << '\t'; }
640                 
641                 //make the bases you want to clip lowercase and the bases you want to keep upper case
642                 if(header.clipQualRight == 0){  header.clipQualRight = read.bases.length();     }
643                 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
644                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   read.bases[i] = toupper(read.bases[i]);  }
645                 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) {   read.bases[i] = tolower(read.bases[i]);  }
646                 
647                 out << endl <<  "Bases: " << read.bases << endl << "Quality Scores: ";
648                 for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
649         
650                 
651                 out << endl << endl;
652                 
653                 return 0;
654         }
655         catch(exception& e) {
656                 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
657                 exit(1);
658         }
659 }
660 //**********************************************************************************************************************
661 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
662         try {
663                 
664                 string seq = read.bases;
665                 
666                 if (trim) {
667                         if(header.clipQualRight < header.clipQualLeft){
668                                 seq = "NNNN";
669                         }
670                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
671                                 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
672                         }
673                         else {
674                                 seq = seq.substr(header.clipQualLeft-1);
675                         }
676                 }else{
677                         //if you wanted the sfftxt then you already converted the bases to the right case
678                         if (!sfftxt) {
679                                 //make the bases you want to clip lowercase and the bases you want to keep upper case
680                                 if(header.clipQualRight == 0){  header.clipQualRight = seq.length();    }
681                                 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]);  }
682                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++)  {   seq[i] = toupper(seq[i]);  }
683                                 for (int i = (header.clipQualRight-1); i < seq.length(); i++) {   seq[i] = tolower(seq[i]);  }
684                         }
685                 }
686                 
687                 out << ">" << header.name << endl;
688                 out << seq << endl;
689                 
690                 return 0;
691         }
692         catch(exception& e) {
693                 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
694                 exit(1);
695         }
696 }
697
698 //**********************************************************************************************************************
699 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
700         try {
701                 
702                 if (trim) {
703                         if(header.clipQualRight < header.clipQualLeft){
704                                 out << "0\t0\t0\t0";
705                         }
706                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
707                                 out << ">" << header.name << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
708                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   out << read.qualScores[i] << '\t'; }
709                         }
710                         else{
711                                 out << ">" << header.name << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
712                                 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';   }                       
713                         }
714                 }else{
715                         out << ">" << header.name << " length=" << read.qualScores.size() << endl;
716                         for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
717                 }
718                 
719                 out << endl;
720                 
721                 return 0;
722         }
723         catch(exception& e) {
724                 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
725                 exit(1);
726         }
727 }
728
729 //**********************************************************************************************************************
730 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
731         try {
732                 
733                 out << ">" << header.name << endl;
734                 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t';  }
735                 out << endl;
736                 
737                 return 0;
738         }
739         catch(exception& e) {
740                 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
741                 exit(1);
742         }
743 }
744 //**********************************************************************************************************************
745 int SffInfoCommand::readAccnosFile(string filename) {
746         try {
747                 //remove old names
748                 seqNames.clear();
749                 
750                 ifstream in;
751                 m->openInputFile(filename, in);
752                 string name;
753                 
754                 while(!in.eof()){
755                         in >> name; m->gobble(in);
756                                                 
757                         seqNames.insert(name);
758                         
759                         if (m->control_pressed) { seqNames.clear(); break; }
760                 }
761                 in.close();             
762                 
763                 return 0;
764         }
765         catch(exception& e) {
766                 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
767                 exit(1);
768         }
769 }
770 //**********************************************************************************************************************/