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