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