]> git.donarmstrong.com Git - mothur.git/blob - sffinfocommand.cpp
addition of trim.flows
[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);  outFlow.setf(ios::fixed, ios::floatfield); outFlow.setf(ios::showpoint); 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                         //extract info from name
519                         decodeName(header.timestamp, header.region, header.xy, header.name);
520                         
521                         /* Pad to 8 chars */
522                         unsigned long int spotInFile = in.tellg();
523                         unsigned long int spot = (spotInFile + 7)& ~7;
524                         in.seekg(spot);
525                         
526                 }else{
527                         m->mothurOut("Error reading sff header info."); m->mothurOutEndLine();
528                 }
529
530                 return 0;
531         }
532         catch(exception& e) {
533                 m->errorOut(e, "SffInfoCommand", "readHeader");
534                 exit(1);
535         }
536 }
537 //**********************************************************************************************************************
538 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, int numBases){
539         try {
540         
541                 if (!in.eof()) {
542         
543                         //read flowgram
544                         read.flowgram.resize(numFlowReads);
545                         for (int i = 0; i < numFlowReads; i++) {  
546                                 char buffer [2];
547                                 in.read(buffer, 2);
548                                 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
549                         }
550         
551                         //read flowIndex
552                         read.flowIndex.resize(numBases);
553                         for (int i = 0; i < numBases; i++) {  
554                                 char temp[1];
555                                 in.read(temp, 1);
556                                 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
557                         }
558         
559                         //read bases
560                         char* tempBuffer = new char[numBases];
561                         in.read(&(*tempBuffer), numBases);
562                         read.bases = tempBuffer;
563                         if (read.bases.length() > numBases) { read.bases = read.bases.substr(0, numBases);  }
564                         delete[] tempBuffer;
565
566                         //read qual scores
567                         read.qualScores.resize(numBases);
568                         for (int i = 0; i < numBases; i++) {  
569                                 char temp[1];
570                                 in.read(temp, 1);
571                                 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
572                         }
573         
574                         /* Pad to 8 chars */
575                         unsigned long int spotInFile = in.tellg();
576                         unsigned long int spot = (spotInFile + 7)& ~7;
577                         in.seekg(spot);
578                         
579                 }else{
580                         m->mothurOut("Error reading."); m->mothurOutEndLine();
581                 }
582
583                 return 0;
584         }
585         catch(exception& e) {
586                 m->errorOut(e, "SffInfoCommand", "readSeqData");
587                 exit(1);
588         }
589 }
590 //**********************************************************************************************************************
591 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
592         try {
593                 
594                 string time = name.substr(0, 6);
595                 unsigned int timeNum = m->fromBase36(time);
596                         
597                 int q1 = timeNum / 60;
598                 int sec = timeNum - 60 * q1;
599                 int q2 = q1 / 60;
600                 int minute = q1 - 60 * q2;
601                 int q3 = q2 / 24;
602                 int hr = q2 - 24 * q3;
603                 int q4 = q3 / 32;
604                 int day = q3 - 32 * q4;
605                 int q5 = q4 / 13;
606                 int mon = q4 - 13 * q5;
607                 int year = 2000 + q5;
608                 
609                 timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
610                 
611                 region = name.substr(7, 2);
612                 
613                 string xyNum = name.substr(9);
614                 unsigned int myXy = m->fromBase36(xyNum);
615                 int x = myXy >> 12;
616                 int y = myXy & 4095;
617                 
618                 xy = toString(x) + "_" + toString(y);
619                         
620                 return 0;
621         }
622         catch(exception& e) {
623                 m->errorOut(e, "SffInfoCommand", "decodeName");
624                 exit(1);
625         }
626 }
627 //**********************************************************************************************************************
628 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
629         try {
630         
631                 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
632                 out << "Version: " << header.version << endl;
633                 out << "Index Offset: " << header.indexOffset << endl;
634                 out << "Index Length: " << header.indexLength << endl;
635                 out << "Number of Reads: " << header.numReads << endl;
636                 out << "Header Length: " << header.headerLength << endl;
637                 out << "Key Length: " << header.keyLength << endl;
638                 out << "Number of Flows: " << header.numFlowsPerRead << endl;
639                 out << "Format Code: " << header.flogramFormatCode << endl;
640                 out << "Flow Chars: " << header.flowChars << endl;
641                 out << "Key Sequence: " << header.keySequence << endl << endl;
642                         
643                 return 0;
644         }
645         catch(exception& e) {
646                 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
647                 exit(1);
648         }
649 }
650 //**********************************************************************************************************************
651 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
652         try {
653                 
654                 out << ">" << header.name << endl;
655                 out << "Run Prefix: " << header.timestamp << endl;
656                 out << "Region #:  " << header.region << endl;
657                 out << "XY Location: " << header.xy << endl << endl;
658                 
659                 out << "Run Name:  " << endl;
660                 out << "Analysis Name:  " << endl;
661                 out << "Full Path: " << endl << endl;
662                 
663                 out << "Read Header Len: " << header.headerLength << endl;
664                 out << "Name Length: " << header.nameLength << endl;
665                 out << "# of Bases: " << header.numBases << endl;
666                 out << "Clip Qual Left: " << header.clipQualLeft << endl;
667                 out << "Clip Qual Right: " << header.clipQualRight << endl;
668                 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
669                 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
670                 
671                 return 0;
672         }
673         catch(exception& e) {
674                 m->errorOut(e, "SffInfoCommand", "printHeader");
675                 exit(1);
676         }
677 }
678
679 //**********************************************************************************************************************
680 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
681         try {
682                 
683                 out << "Flowgram: ";
684                 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t';  }
685                 
686                 out << endl <<  "Flow Indexes: ";
687                 int sum = 0;
688                 for (int i = 0; i < read.flowIndex.size(); i++) {  sum +=  read.flowIndex[i];  out << sum << '\t'; }
689                 
690                 //make the bases you want to clip lowercase and the bases you want to keep upper case
691                 if(header.clipQualRight == 0){  header.clipQualRight = read.bases.length();     }
692                 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
693                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   read.bases[i] = toupper(read.bases[i]);  }
694                 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) {   read.bases[i] = tolower(read.bases[i]);  }
695                 
696                 out << endl <<  "Bases: " << read.bases << endl << "Quality Scores: ";
697                 for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
698         
699                 
700                 out << endl << endl;
701                 
702                 return 0;
703         }
704         catch(exception& e) {
705                 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
706                 exit(1);
707         }
708 }
709 //**********************************************************************************************************************
710 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
711         try {
712                 
713                 string seq = read.bases;
714                 
715                 if (trim) {
716                         if(header.clipQualRight < header.clipQualLeft){
717                                 seq = "NNNN";
718                         }
719                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
720                                 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
721                         }
722                         else {
723                                 seq = seq.substr(header.clipQualLeft-1);
724                         }
725                 }else{
726                         //if you wanted the sfftxt then you already converted the bases to the right case
727                         if (!sfftxt) {
728                                 //make the bases you want to clip lowercase and the bases you want to keep upper case
729                                 if(header.clipQualRight == 0){  header.clipQualRight = seq.length();    }
730                                 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]);  }
731                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++)  {   seq[i] = toupper(seq[i]);  }
732                                 for (int i = (header.clipQualRight-1); i < seq.length(); i++) {   seq[i] = tolower(seq[i]);  }
733                         }
734                 }
735                 
736                 out << ">" << header.name  << " xy=" << header.xy << endl;
737                 out << seq << endl;
738                 
739                 return 0;
740         }
741         catch(exception& e) {
742                 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
743                 exit(1);
744         }
745 }
746
747 //**********************************************************************************************************************
748 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
749         try {
750                 
751                 if (trim) {
752                         if(header.clipQualRight < header.clipQualLeft){
753                                 out << "0\t0\t0\t0";
754                         }
755                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
756                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
757                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   out << read.qualScores[i] << '\t'; }
758                         }
759                         else{
760                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
761                                 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';   }                       
762                         }
763                 }else{
764                         out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
765                         for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
766                 }
767                 
768                 out << endl;
769                 
770                 return 0;
771         }
772         catch(exception& e) {
773                 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
774                 exit(1);
775         }
776 }
777
778 //**********************************************************************************************************************
779 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
780         try {
781                 if(header.clipQualRight > header.clipQualLeft){
782                         out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << " numflows=" << read.flowgram.size() << endl;
783                         for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << ' ';  }
784                         out << endl;
785                 }
786                 
787                 
788                 return 0;
789         }
790         catch(exception& e) {
791                 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
792                 exit(1);
793         }
794 }
795 //**********************************************************************************************************************
796 int SffInfoCommand::readAccnosFile(string filename) {
797         try {
798                 //remove old names
799                 seqNames.clear();
800                 
801                 ifstream in;
802                 m->openInputFile(filename, in);
803                 string name;
804                 
805                 while(!in.eof()){
806                         in >> name; m->gobble(in);
807                                                 
808                         seqNames.insert(name);
809                         
810                         if (m->control_pressed) { seqNames.clear(); break; }
811                 }
812                 in.close();             
813                 
814                 return 0;
815         }
816         catch(exception& e) {
817                 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
818                 exit(1);
819         }
820 }
821 //**********************************************************************************************************************/