]> git.donarmstrong.com Git - mothur.git/blob - sffinfocommand.cpp
added oligos, pdiffs, bdiffs, ldiffs, sdiffs, tiffs parameters to sffinfo to allow...
[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 #include "trimoligos.h"
13 #include "sequence.hpp"
14 #include "qualityscores.h"
15
16 //**********************************************************************************************************************
17 vector<string> SffInfoCommand::setParameters(){ 
18         try {           
19                 CommandParameter psff("sff", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psff);
20         CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(poligos);
21                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
22                 CommandParameter psfftxt("sfftxt", "String", "", "", "", "", "",false,false); parameters.push_back(psfftxt);
23                 CommandParameter pflow("flow", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pflow);
24                 CommandParameter ptrim("trim", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(ptrim);
25                 CommandParameter pfasta("fasta", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pfasta);
26                 CommandParameter pqfile("name", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pqfile);
27         CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ppdiffs);
28                 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pbdiffs);
29         CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pldiffs);
30                 CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(psdiffs);
31         CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ptdiffs);
32                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
33                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
34                 
35                 vector<string> myArray;
36                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
37                 return myArray;
38         }
39         catch(exception& e) {
40                 m->errorOut(e, "SffInfoCommand", "setParameters");
41                 exit(1);
42         }
43 }
44 //**********************************************************************************************************************
45 string SffInfoCommand::getHelpString(){ 
46         try {
47                 string helpString = "";
48                 helpString += "The sffinfo command reads a sff file and extracts the sequence data, or you can use it to parse a sfftxt file.\n";
49                 helpString += "The sffinfo command parameters are sff, fasta, qfile, accnos, flow, sfftxt, oligos, bdiffs, tdiffs, ldiffs, sdiffs, pdiffs and trim. sff is required. \n";
50                 helpString += "The sff parameter allows you to enter the sff file you would like to extract data from.  You may enter multiple files by separating them by -'s.\n";
51                 helpString += "The fasta parameter allows you to indicate if you would like a fasta formatted file generated.  Default=True. \n";
52                 helpString += "The qfile parameter allows you to indicate if you would like a quality file generated.  Default=True. \n";
53         helpString += "The oligos parameter allows you to provide an oligos file to split your sff file into separate sff files by barcode. \n";
54         helpString += "The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs + sdiffs + ldiffs.\n";
55                 helpString += "The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n";
56                 helpString += "The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n";
57         helpString += "The ldiffs parameter is used to specify the number of differences allowed in the linker. The default is 0.\n";
58                 helpString += "The sdiffs parameter is used to specify the number of differences allowed in the spacer. The default is 0.\n";
59                 helpString += "The flow parameter allows you to indicate if you would like a flowgram file generated.  Default=True. \n";
60                 helpString += "The sfftxt parameter allows you to indicate if you would like a sff.txt file generated.  Default=False. \n";
61                 helpString += "If you want to parse an existing sfftxt file into flow, fasta and quality file, enter the file name using the sfftxt parameter. \n";
62                 helpString += "The trim parameter allows you to indicate if you would like a sequences and quality scores trimmed to the clipQualLeft and clipQualRight values.  Default=True. \n";
63                 helpString += "The accnos parameter allows you to provide a accnos file containing the names of the sequences you would like extracted. You may enter multiple files by separating them by -'s. \n";
64                 helpString += "Example sffinfo(sff=mySffFile.sff, trim=F).\n";
65                 helpString += "Note: No spaces between parameter labels (i.e. sff), '=' and parameters (i.e.yourSffFileName).\n";
66                 return helpString;
67         }
68         catch(exception& e) {
69                 m->errorOut(e, "SffInfoCommand", "getHelpString");
70                 exit(1);
71         }
72 }
73 //**********************************************************************************************************************
74 string SffInfoCommand::getOutputFileNameTag(string type, string inputName=""){  
75         try {
76         string outputFileName = "";
77                 map<string, vector<string> >::iterator it;
78         
79         //is this a type this command creates
80         it = outputTypes.find(type);
81         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
82         else {
83             if (type == "fasta")            {   outputFileName =  "fasta";   }
84             else if (type == "flow")    {   outputFileName =  "flow";   }
85             else if (type == "sfftxt")        {   outputFileName =  "sff.txt";   }
86             else if (type == "sff")        {   outputFileName =  "sff";   }
87             else if (type == "qfile")       {   outputFileName =  "qual";   }
88              else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
89         }
90         return outputFileName;
91         }
92         catch(exception& e) {
93                 m->errorOut(e, "SffInfoCommand", "getOutputFileNameTag");
94                 exit(1);
95         }
96 }
97
98
99 //**********************************************************************************************************************
100 SffInfoCommand::SffInfoCommand(){       
101         try {
102                 abort = true; calledHelp = true; 
103                 setParameters();
104                 vector<string> tempOutNames;
105                 outputTypes["fasta"] = tempOutNames;
106                 outputTypes["flow"] = tempOutNames;
107                 outputTypes["sfftxt"] = tempOutNames;
108                 outputTypes["qfile"] = tempOutNames;
109         outputTypes["sff"] = tempOutNames;
110         }
111         catch(exception& e) {
112                 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
113                 exit(1);
114         }
115 }
116 //**********************************************************************************************************************
117
118 SffInfoCommand::SffInfoCommand(string option)  {
119         try {
120                 abort = false; calledHelp = false;   
121                 hasAccnos = false;
122         split = 1;
123                 
124                 //allow user to run help
125                 if(option == "help") { help(); abort = true; calledHelp = true; }
126                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
127                 
128                 else {
129                         //valid paramters for this command
130                         vector<string> myArray = setParameters();
131                         
132                         OptionParser parser(option);
133                         map<string, string> parameters = parser.getParameters();
134                         
135                         ValidParameters validParameter;
136                         //check to make sure all parameters are valid for command
137                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
138                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
139                         }
140                         
141                         //initialize outputTypes
142                         vector<string> tempOutNames;
143                         outputTypes["fasta"] = tempOutNames;
144                         outputTypes["flow"] = tempOutNames;
145                         outputTypes["sfftxt"] = tempOutNames;
146                         outputTypes["qfile"] = tempOutNames;
147             outputTypes["sff"] = tempOutNames;
148                         
149                         //if the user changes the output directory command factory will send this info to us in the output parameter 
150                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
151                         
152                         //if the user changes the input directory command factory will send this info to us in the output parameter 
153                         string inputDir = validParameter.validFile(parameters, "inputdir", false);        if (inputDir == "not found"){ inputDir = "";          }
154
155                         sffFilename = validParameter.validFile(parameters, "sff", false);
156                         if (sffFilename == "not found") { sffFilename = "";  }
157                         else { 
158                                 m->splitAtDash(sffFilename, filenames);
159                                 
160                                 //go through files and make sure they are good, if not, then disregard them
161                                 for (int i = 0; i < filenames.size(); i++) {
162                                         bool ignore = false;
163                                         if (filenames[i] == "current") { 
164                                                 filenames[i] = m->getSFFFile(); 
165                                                 if (filenames[i] != "") {  m->mothurOut("Using " + filenames[i] + " as input file for the sff parameter where you had given current."); m->mothurOutEndLine(); }
166                                                 else {  
167                                                         m->mothurOut("You have no current sfffile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
168                                                         //erase from file list
169                                                         filenames.erase(filenames.begin()+i);
170                                                         i--;
171                                                 }
172                                         }
173                                         
174                                         if (!ignore) {
175                                                 if (inputDir != "") {
176                                                         string path = m->hasPath(filenames[i]);
177                                                         //if the user has not given a path then, add inputdir. else leave path alone.
178                                                         if (path == "") {       filenames[i] = inputDir + filenames[i];         }
179                                                 }
180                 
181                                                 ifstream in;
182                                                 int ableToOpen = m->openInputFile(filenames[i], in, "noerror");
183                                         
184                                                 //if you can't open it, try default location
185                                                 if (ableToOpen == 1) {
186                                                         if (m->getDefaultPath() != "") { //default path is set
187                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(filenames[i]);
188                                                                 m->mothurOut("Unable to open " + filenames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
189                                                                 ifstream in2;
190                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
191                                                                 in2.close();
192                                                                 filenames[i] = tryPath;
193                                                         }
194                                                 }
195                                                 
196                                                 //if you can't open it, try default location
197                                                 if (ableToOpen == 1) {
198                                                         if (m->getOutputDir() != "") { //default path is set
199                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(filenames[i]);
200                                                                 m->mothurOut("Unable to open " + filenames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
201                                                                 ifstream in2;
202                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
203                                                                 in2.close();
204                                                                 filenames[i] = tryPath;
205                                                         }
206                                                 }
207                                                 
208                                                 in.close();
209                                                 
210                                                 if (ableToOpen == 1) { 
211                                                         m->mothurOut("Unable to open " + filenames[i] + ". It will be disregarded."); m->mothurOutEndLine();
212                                                         //erase from file list
213                                                         filenames.erase(filenames.begin()+i);
214                                                         i--;
215                                                 }else { m->setSFFFile(filenames[i]); }
216                                         }
217                                 }
218                                 
219                                 //make sure there is at least one valid file left
220                                 if (filenames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
221                         }
222                         
223                         accnosName = validParameter.validFile(parameters, "accnos", false);
224                         if (accnosName == "not found") { accnosName = "";  }
225                         else { 
226                                 hasAccnos = true;
227                                 m->splitAtDash(accnosName, accnosFileNames);
228                                 
229                                 //go through files and make sure they are good, if not, then disregard them
230                                 for (int i = 0; i < accnosFileNames.size(); i++) {
231                                         bool ignore = false;
232                                         if (accnosFileNames[i] == "current") { 
233                                                 accnosFileNames[i] = m->getAccnosFile(); 
234                                                 if (accnosFileNames[i] != "") {  m->mothurOut("Using " + accnosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
235                                                 else {  
236                                                         m->mothurOut("You have no current accnosfile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
237                                                         //erase from file list
238                                                         accnosFileNames.erase(accnosFileNames.begin()+i);
239                                                         i--;
240                                                 }
241                                         }
242                                         
243                                         if (!ignore) {
244                                         
245                                                 if (inputDir != "") {
246                                                         string path = m->hasPath(accnosFileNames[i]);
247                                                         //if the user has not given a path then, add inputdir. else leave path alone.
248                                                         if (path == "") {       accnosFileNames[i] = inputDir + accnosFileNames[i];             }
249                                                 }
250                 
251                                                 ifstream in;
252                                                 int ableToOpen = m->openInputFile(accnosFileNames[i], in, "noerror");
253                                         
254                                                 //if you can't open it, try default location
255                                                 if (ableToOpen == 1) {
256                                                         if (m->getDefaultPath() != "") { //default path is set
257                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(accnosFileNames[i]);
258                                                                 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
259                                                                 ifstream in2;
260                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
261                                                                 in2.close();
262                                                                 accnosFileNames[i] = tryPath;
263                                                         }
264                                                 }
265                                                 //if you can't open it, try default location
266                                                 if (ableToOpen == 1) {
267                                                         if (m->getOutputDir() != "") { //default path is set
268                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(accnosFileNames[i]);
269                                                                 m->mothurOut("Unable to open " + accnosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
270                                                                 ifstream in2;
271                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
272                                                                 in2.close();
273                                                                 accnosFileNames[i] = tryPath;
274                                                         }
275                                                 }
276                                                 in.close();
277                                                 
278                                                 if (ableToOpen == 1) { 
279                                                         m->mothurOut("Unable to open " + accnosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
280                                                         //erase from file list
281                                                         accnosFileNames.erase(accnosFileNames.begin()+i);
282                                                         i--;
283                                                 }
284                                         }
285                                 }
286                                 
287                                 //make sure there is at least one valid file left
288                                 if (accnosFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
289                         }
290             
291             oligosfile = validParameter.validFile(parameters, "oligos", false);
292                         if (oligosfile == "not found") { oligosfile = "";  }
293                         else { 
294                                 hasOligos = true;
295                                 m->splitAtDash(oligosfile, oligosFileNames);
296                                 
297                                 //go through files and make sure they are good, if not, then disregard them
298                                 for (int i = 0; i < oligosFileNames.size(); i++) {
299                                         bool ignore = false;
300                                         if (oligosFileNames[i] == "current") { 
301                                                 oligosFileNames[i] = m->getOligosFile(); 
302                                                 if (oligosFileNames[i] != "") {  m->mothurOut("Using " + oligosFileNames[i] + " as input file for the accnos parameter where you had given current."); m->mothurOutEndLine(); }
303                                                 else {  
304                                                         m->mothurOut("You have no current oligosfile, ignoring current."); m->mothurOutEndLine(); ignore=true; 
305                                                         //erase from file list
306                                                         oligosFileNames.erase(oligosFileNames.begin()+i);
307                                                         i--;
308                                                 }
309                                         }
310                                         
311                                         if (!ignore) {
312                         
313                                                 if (inputDir != "") {
314                                                         string path = m->hasPath(oligosFileNames[i]);
315                                                         //if the user has not given a path then, add inputdir. else leave path alone.
316                                                         if (path == "") {       oligosFileNames[i] = inputDir + oligosFileNames[i];             }
317                                                 }
318                         
319                                                 ifstream in;
320                                                 int ableToOpen = m->openInputFile(oligosFileNames[i], in, "noerror");
321                         
322                                                 //if you can't open it, try default location
323                                                 if (ableToOpen == 1) {
324                                                         if (m->getDefaultPath() != "") { //default path is set
325                                                                 string tryPath = m->getDefaultPath() + m->getSimpleName(oligosFileNames[i]);
326                                                                 m->mothurOut("Unable to open " + oligosFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
327                                                                 ifstream in2;
328                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
329                                                                 in2.close();
330                                                                 oligosFileNames[i] = tryPath;
331                                                         }
332                                                 }
333                                                 //if you can't open it, try default location
334                                                 if (ableToOpen == 1) {
335                                                         if (m->getOutputDir() != "") { //default path is set
336                                                                 string tryPath = m->getOutputDir() + m->getSimpleName(oligosFileNames[i]);
337                                                                 m->mothurOut("Unable to open " + oligosFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
338                                                                 ifstream in2;
339                                                                 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
340                                                                 in2.close();
341                                                                 oligosFileNames[i] = tryPath;
342                                                         }
343                                                 }
344                                                 in.close();
345                                                 
346                                                 if (ableToOpen == 1) { 
347                                                         m->mothurOut("Unable to open " + oligosFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
348                                                         //erase from file list
349                                                         oligosFileNames.erase(oligosFileNames.begin()+i);
350                                                         i--;
351                                                 }
352                                         }
353                                 }
354                                 
355                                 //make sure there is at least one valid file left
356                                 if (oligosFileNames.size() == 0) { m->mothurOut("no valid oligos files."); m->mothurOutEndLine(); abort = true; }
357                         }
358
359                         if (hasOligos) {
360                 split = 2;
361                                 if (oligosFileNames.size() != filenames.size()) { abort = true; m->mothurOut("If you provide a oligos file, you must have one for each sff file."); m->mothurOutEndLine(); }
362                         }
363             
364                         if (hasAccnos) {
365                                 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(); }
366                         }
367                         
368                         string temp = validParameter.validFile(parameters, "qfile", false);                     if (temp == "not found"){       temp = "T";                             }
369                         qual = m->isTrue(temp); 
370                         
371                         temp = validParameter.validFile(parameters, "fasta", false);                            if (temp == "not found"){       temp = "T";                             }
372                         fasta = m->isTrue(temp); 
373                         
374                         temp = validParameter.validFile(parameters, "flow", false);                                     if (temp == "not found"){       temp = "T";                             }
375                         flow = m->isTrue(temp); 
376                         
377                         temp = validParameter.validFile(parameters, "trim", false);                                     if (temp == "not found"){       temp = "T";                             }
378                         trim = m->isTrue(temp); 
379             
380             temp = validParameter.validFile(parameters, "bdiffs", false);               if (temp == "not found") { temp = "0"; }
381                         m->mothurConvert(temp, bdiffs);
382                         
383                         temp = validParameter.validFile(parameters, "pdiffs", false);           if (temp == "not found") { temp = "0"; }
384                         m->mothurConvert(temp, pdiffs);
385             
386             temp = validParameter.validFile(parameters, "ldiffs", false);               if (temp == "not found") { temp = "0"; }
387                         m->mothurConvert(temp, ldiffs);
388             
389             temp = validParameter.validFile(parameters, "sdiffs", false);               if (temp == "not found") { temp = "0"; }
390                         m->mothurConvert(temp, sdiffs);
391                         
392                         temp = validParameter.validFile(parameters, "tdiffs", false);           if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs;  temp = toString(tempTotal); }
393                         m->mothurConvert(temp, tdiffs);
394                         
395                         if(tdiffs == 0){        tdiffs = bdiffs + pdiffs + ldiffs + sdiffs;     }
396             
397                         temp = validParameter.validFile(parameters, "sfftxt", false);                           
398                         if (temp == "not found")        {       temp = "F";      sfftxt = false; sfftxtFilename = "";           }
399                         else if (m->isTrue(temp))       {       sfftxt = true;          sfftxtFilename = "";                            }
400                         else {
401                                 //you are a filename
402                                 if (inputDir != "") {
403                                         map<string,string>::iterator it = parameters.find("sfftxt");
404                                         //user has given a template file
405                                         if(it != parameters.end()){ 
406                                                 string path = m->hasPath(it->second);
407                                                 //if the user has not given a path then, add inputdir. else leave path alone.
408                                                 if (path == "") {       parameters["sfftxt"] = inputDir + it->second;           }
409                                         }
410                                 }
411                                 
412                                 sfftxtFilename = validParameter.validFile(parameters, "sfftxt", true);
413                                 if (sfftxtFilename == "not found") { sfftxtFilename = "";  }
414                                 else if (sfftxtFilename == "not open") { sfftxtFilename = "";  }
415                         }
416                         
417                         if ((sfftxtFilename == "") && (filenames.size() == 0)) {  
418                                 //if there is a current sff file, use it
419                                 string filename = m->getSFFFile(); 
420                                 if (filename != "") { filenames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the sff parameter."); m->mothurOutEndLine(); }
421                                 else {  m->mothurOut("[ERROR]: you must provide a valid sff or sfftxt file."); m->mothurOutEndLine(); abort=true;  }
422                         }
423             
424             
425                 }
426         }
427         catch(exception& e) {
428                 m->errorOut(e, "SffInfoCommand", "SffInfoCommand");
429                 exit(1);
430         }
431 }
432 //**********************************************************************************************************************
433 int SffInfoCommand::execute(){
434         try {
435                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
436                 
437                 for (int s = 0; s < filenames.size(); s++) {
438                         
439                         if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);        } return 0; }
440                         
441                         int start = time(NULL);
442                         
443             filenames[s] = m->getFullPathName(filenames[s]);
444                         m->mothurOut("Extracting info from " + filenames[s] + " ..." ); m->mothurOutEndLine();
445                         
446                         string accnos = "";
447                         if (hasAccnos) { accnos = accnosFileNames[s]; }
448             
449             string oligos = "";
450             if (hasOligos) { oligos = oligosFileNames[s]; }
451                         
452                         int numReads = extractSffInfo(filenames[s], accnos, oligos);
453
454                         m->mothurOut("It took " + toString(time(NULL) - start) + " secs to extract " + toString(numReads) + ".");
455                 }
456                 
457                 if (sfftxtFilename != "") {  parseSffTxt(); }
458                 
459                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]);        } return 0; }
460                 
461                 //set fasta file as new current fastafile
462                 string current = "";
463                 itTypes = outputTypes.find("fasta");
464                 if (itTypes != outputTypes.end()) {
465                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
466                 }
467                 
468                 itTypes = outputTypes.find("qfile");
469                 if (itTypes != outputTypes.end()) {
470                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
471                 }
472                 
473                 itTypes = outputTypes.find("flow");
474                 if (itTypes != outputTypes.end()) {
475                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFlowFile(current); }
476                 }
477                 
478                 //report output filenames
479                 m->mothurOutEndLine();
480                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
481                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
482                 m->mothurOutEndLine();
483
484                 return 0;
485         }
486         catch(exception& e) {
487                 m->errorOut(e, "SffInfoCommand", "execute");
488                 exit(1);
489         }
490 }
491 //**********************************************************************************************************************
492 int SffInfoCommand::extractSffInfo(string input, string accnos, string oligos){
493         try {
494                 currentFileName = input;
495                 if (outputDir == "") {  outputDir += m->hasPath(input); }
496                 
497                 if (accnos != "")       {  readAccnosFile(accnos);  }
498                 else                            {       seqNames.clear();               }
499          
500         if (oligos != "")   {   readOligos(oligos);  split = 2;   }
501
502                 ofstream outSfftxt, outFasta, outQual, outFlow;
503                 string outFastaFileName, outQualFileName;
504         string rootName = outputDir + m->getRootName(m->getSimpleName(input));
505         if(rootName.find_last_of(".") == rootName.npos){ rootName += "."; }
506         
507                 string sfftxtFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("sfftxt");
508                 string outFlowFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("flow");
509                 if (trim) {
510                         outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("fasta");
511                         outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + getOutputFileNameTag("qfile");
512                 }else{
513                         outFastaFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("fasta");
514                         outQualFileName = outputDir + m->getRootName(m->getSimpleName(input)) + "raw." + getOutputFileNameTag("qfile");
515                 }
516                 
517                 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); }
518                 if (fasta)      { m->openOutputFile(outFastaFileName, outFasta);        outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
519                 if (qual)       { m->openOutputFile(outQualFileName, outQual);          outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName);  }
520                 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);  }
521                 
522                 ifstream in;
523                 in.open(input.c_str(), ios::binary);
524                 
525                 CommonHeader header; 
526                 readCommonHeader(in, header);
527         
528                 int count = 0;
529                 mycount = 0;
530                 
531                 //check magic number and version
532                 if (header.magicNumber != 779314790) { m->mothurOut("Magic Number is not correct, not a valid .sff file"); m->mothurOutEndLine(); return count; }
533                 if (header.version != "0001") { m->mothurOut("Version is not supported, only support version 0001."); m->mothurOutEndLine(); return count; }
534         
535                 //print common header
536                 if (sfftxt) {   printCommonHeader(outSfftxt, header);           }
537                 if (flow)       {       outFlow << header.numFlowsPerRead << endl;      }
538                         
539                 //read through the sff file
540                 while (!in.eof()) {
541                         
542                         bool print = true;
543                                                 
544                         //read data
545                         seqRead read;  Header readheader;
546                         readSeqData(in, read, header.numFlowsPerRead, readheader);
547             bool okay = sanityCheck(readheader, read);
548             if (!okay) { break; }
549             
550                         //if you have provided an accosfile and this seq is not in it, then dont print
551                         if (seqNames.size() != 0) {   if (seqNames.count(readheader.name) == 0) { print = false; }  }
552                         
553                         //print 
554                         if (print) {
555                                 if (sfftxt) { printHeader(outSfftxt, readheader); printSffTxtSeqData(outSfftxt, read, readheader); }
556                                 if (fasta)      {       printFastaSeqData(outFasta, read, readheader);  }
557                                 if (qual)       {       printQualSeqData(outQual, read, readheader);    }
558                                 if (flow)       {       printFlowSeqData(outFlow, read, readheader);    }
559                         }
560                         
561                         count++;
562                         mycount++;
563         
564                         //report progress
565                         if((count+1) % 10000 == 0){     m->mothurOut(toString(count+1)); m->mothurOutEndLine();         }
566                 
567                         if (m->control_pressed) { count = 0; break;   }
568                         
569                         if (count >= header.numReads) { break; }
570                 }
571                 
572                 //report progress
573                 if (!m->control_pressed) {   if((count) % 10000 != 0){  m->mothurOut(toString(count)); m->mothurOutEndLine();           }  }
574                 
575                 in.close();
576                 
577                 if (sfftxt) {  outSfftxt.close();       }
578                 if (fasta)      {  outFasta.close();    }
579                 if (qual)       {  outQual.close();             }
580                 if (flow)       {  outFlow.close();             }
581                 
582         if (split > 1) {
583             //create new common headers for each file with the correct number of reads
584             adjustCommonHeader(header);
585             
586             map<string, string> uniqueSffNames;// so we don't add the same sff multiple times
587                         map<string, string>::iterator it;
588                         set<string> namesToRemove;
589                         for(int i=0;i<filehandles.size();i++){
590                                 for(int j=0;j<filehandles[0].size();j++){
591                                         if (filehandles[i][j] != "") {
592                                                 if (namesToRemove.count(filehandles[i][j]) == 0) {
593                                                         if(m->isBlank(filehandles[i][j])){
594                                                                 m->mothurRemove(filehandles[i][j]);
595                                 m->mothurRemove(filehandlesHeaders[i][j]);
596                                                                 namesToRemove.insert(filehandles[i][j]);
597                             }else{      
598                                                                 it = uniqueSffNames.find(filehandles[i][j]);
599                                                                 if (it == uniqueSffNames.end()) {       
600                                                                         uniqueSffNames[filehandles[i][j]] = barcodeNameVector[i];       
601                                                                 }       
602                                                         }
603                                                 }
604                                         }
605                                 }
606                         }
607             
608             //append new header to reads
609             for (int i = 0; i < filehandles.size(); i++) {
610                 for (int j = 0; j < filehandles[i].size(); j++) {
611                     m->appendFiles(filehandles[i][j], filehandlesHeaders[i][j]);
612                     m->renameFile(filehandlesHeaders[i][j], filehandles[i][j]);
613                     m->mothurRemove(filehandlesHeaders[i][j]);
614                     if (numSplitReads[i][j] == 0) { m->mothurRemove(filehandles[i][j]); }
615                 }
616             }
617                         
618                         //remove names for outputFileNames, just cleans up the output
619                         for(int i = 0; i < outputNames.size(); i++) { 
620                 if (namesToRemove.count(outputNames[i]) != 0) { 
621                     outputNames.erase(outputNames.begin()+i);
622                     i--;
623                 } 
624             }
625             
626             if(m->isBlank(noMatchFile)){  m->mothurRemove(noMatchFile); }
627             else { outputNames.push_back(noMatchFile); outputTypes["sff"].push_back(noMatchFile); }
628         }
629         
630                 return count;
631         }
632         catch(exception& e) {
633                 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
634                 exit(1);
635         }
636 }
637 //**********************************************************************************************************************
638 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
639         try {
640         
641                 if (!in.eof()) {
642
643                         //read magic number
644                         char buffer[4];
645                         in.read(buffer, 4);
646                         header.magicNumber = be_int4(*(unsigned int *)(&buffer));
647             
648                         //read version
649                         char buffer9[4];
650                         in.read(buffer9, 4);
651                         header.version = "";
652                         for (int i = 0; i < 4; i++) {  header.version += toString((int)(buffer9[i]));  }
653     
654                         //read offset
655                         char buffer2 [8];
656                         in.read(buffer2, 8);
657                         header.indexOffset =  be_int8(*(unsigned long long *)(&buffer2));
658                         
659                         //read index length
660                         char buffer3 [4];
661                         in.read(buffer3, 4);
662                         header.indexLength =  be_int4(*(unsigned int *)(&buffer3));
663                         
664                         //read num reads
665                         char buffer4 [4];
666                         in.read(buffer4, 4);
667                         header.numReads =  be_int4(*(unsigned int *)(&buffer4));
668                                 
669                         //read header length
670                         char buffer5 [2];
671                         in.read(buffer5, 2);
672                         header.headerLength =  be_int2(*(unsigned short *)(&buffer5));
673                                         
674                         //read key length
675                         char buffer6 [2];
676                         in.read(buffer6, 2);
677                         header.keyLength = be_int2(*(unsigned short *)(&buffer6));
678                         
679                         //read number of flow reads
680                         char buffer7 [2];
681                         in.read(buffer7, 2);
682                         header.numFlowsPerRead =  be_int2(*(unsigned short *)(&buffer7));
683                                 
684                         //read format code
685                         char buffer8 [1];
686                         in.read(buffer8, 1);
687                         header.flogramFormatCode = (int)(buffer8[0]);
688                         
689                         //read flow chars
690                         char* tempBuffer = new char[header.numFlowsPerRead];
691                         in.read(&(*tempBuffer), header.numFlowsPerRead); 
692                         header.flowChars = tempBuffer;
693                         if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead);  }
694                         delete[] tempBuffer;
695                         
696                         //read key
697                         char* tempBuffer2 = new char[header.keyLength];
698                         in.read(&(*tempBuffer2), header.keyLength);
699                         header.keySequence = tempBuffer2;
700                         if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength);  }
701                         delete[] tempBuffer2;
702                         
703                         /* Pad to 8 chars */
704                         unsigned long long spotInFile = in.tellg();
705                         unsigned long long spot = (spotInFile + 7)& ~7;  // ~ inverts
706                         in.seekg(spot);
707             
708         }else{
709                         m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
710                 }
711         
712                 return 0;
713         
714         }
715         catch(exception& e) {
716                 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
717                 exit(1);
718         }
719 }
720 //**********************************************************************************************************************
721 int SffInfoCommand::adjustCommonHeader(CommonHeader header){
722         try {
723
724         char* mybuffer = new char[4];
725         ifstream in;
726         in.open(currentFileName.c_str(), ios::binary);
727         
728         //magic number
729         in.read(mybuffer,4);
730         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
731             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
732                 ofstream out;
733                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
734                 out.write(mybuffer, in.gcount()); 
735                 out.close();
736             }
737         }
738         delete[] mybuffer;
739         
740         //version
741         mybuffer = new char[4];
742         in.read(mybuffer,4);
743         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
744             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
745                 ofstream out;
746                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
747                 out.write(mybuffer, in.gcount()); 
748                 out.close();
749             }
750         }
751         delete[] mybuffer;
752         
753         //offset
754         mybuffer = new char[8];
755         in.read(mybuffer,8);
756         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
757             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
758                 ofstream out;
759                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
760                 out.write(mybuffer, in.gcount()); 
761                 out.close();
762             }
763         }
764         delete[] mybuffer;
765             
766                         
767         //read index length
768                 mybuffer = new char[4];
769         in.read(mybuffer,4);
770         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
771             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
772                 ofstream out;
773                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
774                 out.write(mybuffer, in.gcount()); 
775                 out.close();
776             }
777         }
778         delete[] mybuffer;
779                 
780         //change num reads
781         mybuffer = new char[4];
782         in.read(mybuffer,4);
783         delete[] mybuffer;
784         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
785             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
786                 ofstream out;
787                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
788                 //convert number of reads to 4 byte char*
789                 char* thisbuffer = new char[4];
790                 thisbuffer[0] = (numSplitReads[i][j] >> 24) & 0xFF;
791                 thisbuffer[1] = (numSplitReads[i][j] >> 16) & 0xFF;
792                 thisbuffer[2] = (numSplitReads[i][j] >> 8) & 0xFF;
793                 thisbuffer[3] = numSplitReads[i][j] & 0xFF;
794                 out.write(thisbuffer, 4);
795                 out.close();
796                 delete[] thisbuffer;
797             }
798         }
799             
800         //read header length
801         mybuffer = new char[2];
802         in.read(mybuffer,2);
803         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
804             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
805                 ofstream out;
806                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
807                 out.write(mybuffer, in.gcount()); 
808                 out.close();
809             }
810         }
811         delete[] mybuffer;
812             
813         //read key length
814         mybuffer = new char[2];
815         in.read(mybuffer,2);
816         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
817             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
818                 ofstream out;
819                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
820                 out.write(mybuffer, in.gcount()); 
821                 out.close();
822             }
823         }
824         delete[] mybuffer;
825                         
826         //read number of flow reads
827         mybuffer = new char[2];
828         in.read(mybuffer,2);
829         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
830             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
831                 ofstream out;
832                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
833                 out.write(mybuffer, in.gcount()); 
834                 out.close();
835             }
836         }
837         delete[] mybuffer;
838             
839         //read format code
840         mybuffer = new char[1];
841         in.read(mybuffer,1);
842         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
843             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
844                 ofstream out;
845                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
846                 out.write(mybuffer, in.gcount()); 
847                 out.close();
848             }
849         }
850         delete[] mybuffer;
851                         
852         //read flow chars
853         mybuffer = new char[header.numFlowsPerRead];
854         in.read(mybuffer,header.numFlowsPerRead);
855         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
856             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
857                 ofstream out;
858                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
859                 out.write(mybuffer, in.gcount()); 
860                 out.close();
861             }
862         }
863         delete[] mybuffer;
864                         
865         //read key
866         mybuffer = new char[header.keyLength];
867         in.read(mybuffer,header.keyLength);
868         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
869             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
870                 ofstream out;
871                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
872                 out.write(mybuffer, in.gcount()); 
873                 out.close();
874             }
875         }
876         delete[] mybuffer;
877         
878                         
879         /* Pad to 8 chars */
880         unsigned long long spotInFile = in.tellg();
881         unsigned long long spot = (spotInFile + 7)& ~7;  // ~ inverts
882         in.seekg(spot);
883         
884         mybuffer = new char[spot-spotInFile];
885         for (int i = 0; i < filehandlesHeaders.size(); i++) { 
886             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
887                 ofstream out;
888                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
889                 out.write(mybuffer, spot-spotInFile); 
890                 out.close();
891             }
892         }
893         delete[] mybuffer;
894         in.close();
895                 return 0;
896         
897         }
898         catch(exception& e) {
899                 m->errorOut(e, "SffInfoCommand", "adjustCommonHeader");
900                 exit(1);
901         }
902 }
903 //**********************************************************************************************************************
904 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, Header& header){
905         try {
906         unsigned long long startSpotInFile = in.tellg();
907                 if (!in.eof()) {
908             
909             /*****************************************/
910             //read header
911             
912             //read header length
913                         char buffer [2];
914                         in.read(buffer, 2);
915                         header.headerLength = be_int2(*(unsigned short *)(&buffer));
916             
917                         //read name length
918                         char buffer2 [2];
919                         in.read(buffer2, 2);
920                         header.nameLength = be_int2(*(unsigned short *)(&buffer2));
921             
922                         //read num bases
923                         char buffer3 [4];
924                         in.read(buffer3, 4);
925                         header.numBases =  be_int4(*(unsigned int *)(&buffer3));
926                         
927                         //read clip qual left
928                         char buffer4 [2];
929                         in.read(buffer4, 2);
930                         header.clipQualLeft =  be_int2(*(unsigned short *)(&buffer4));
931                         header.clipQualLeft = 5; 
932                         
933                         //read clip qual right
934                         char buffer5 [2];
935                         in.read(buffer5, 2);
936                         header.clipQualRight =  be_int2(*(unsigned short *)(&buffer5));
937                         
938                         //read clipAdapterLeft
939                         char buffer6 [2];
940                         in.read(buffer6, 2);
941                         header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
942             
943                         //read clipAdapterRight
944                         char buffer7 [2];
945                         in.read(buffer7, 2);
946                         header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
947             
948                         //read name
949                         char* tempBuffer = new char[header.nameLength];
950                         in.read(&(*tempBuffer), header.nameLength);
951                         header.name = tempBuffer;
952                         if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength);  }
953                         delete[] tempBuffer;
954                         
955                         //extract info from name
956                         decodeName(header.timestamp, header.region, header.xy, header.name);
957                         
958                         /* Pad to 8 chars */
959                         unsigned long long spotInFile = in.tellg();
960                         unsigned long long spot = (spotInFile + 7)& ~7;
961                         in.seekg(spot);
962
963             /*****************************************/
964             //sequence read 
965             
966                         //read flowgram
967                         read.flowgram.resize(numFlowReads);
968                         for (int i = 0; i < numFlowReads; i++) {  
969                                 char buffer [2];
970                                 in.read(buffer, 2);
971                                 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
972                         }
973             
974                         //read flowIndex
975                         read.flowIndex.resize(header.numBases);
976                         for (int i = 0; i < header.numBases; i++) {  
977                                 char temp[1];
978                                 in.read(temp, 1);
979                                 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
980                         }
981         
982                         //read bases
983                         char* tempBuffer6 = new char[header.numBases];
984                         in.read(&(*tempBuffer6), header.numBases);
985                         read.bases = tempBuffer6;
986                         if (read.bases.length() > header.numBases) { read.bases = read.bases.substr(0, header.numBases);  }
987                         delete[] tempBuffer6;
988
989                         //read qual scores
990                         read.qualScores.resize(header.numBases);
991                         for (int i = 0; i < header.numBases; i++) {  
992                                 char temp[1];
993                                 in.read(temp, 1);
994                                 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
995                         }
996         
997                         /* Pad to 8 chars */
998                         spotInFile = in.tellg();
999                         spot = (spotInFile + 7)& ~7;
1000                         in.seekg(spot);
1001             
1002             if (split > 1) {
1003                 char * mybuffer;
1004                 mybuffer = new char [spot-startSpotInFile];
1005                 ifstream in2;
1006                 m->openInputFile(currentFileName, in2);
1007                 in2.seekg(startSpotInFile);
1008                 in2.read(mybuffer,spot-startSpotInFile);
1009                 in2.close();
1010                 
1011                 int barcodeIndex, primerIndex;
1012                 int trashCodeLength = findGroup(header, read, barcodeIndex, primerIndex);
1013                                 
1014                 if(trashCodeLength == 0){
1015                     ofstream out;
1016                     m->openOutputFileAppend(filehandles[barcodeIndex][primerIndex], out);
1017                     out.write(mybuffer, in2.gcount()); 
1018                     out.close();
1019                     delete[] mybuffer;
1020                     numSplitReads[barcodeIndex][primerIndex]++;
1021                                 }
1022                                 else{
1023                                         ofstream out;
1024                     m->openOutputFileAppend(noMatchFile, out);
1025                     out.write(mybuffer, in2.gcount()); 
1026                     out.close();
1027                     delete[] mybuffer;
1028                                 }
1029                                 
1030                         }
1031                 }else{
1032                         m->mothurOut("Error reading."); m->mothurOutEndLine();
1033                 }
1034
1035                 return 0;
1036         }
1037         catch(exception& e) {
1038                 m->errorOut(e, "SffInfoCommand", "readSeqData");
1039                 exit(1);
1040         }
1041 }
1042 //**********************************************************************************************************************
1043 int SffInfoCommand::findGroup(Header header, seqRead read, int& barcode, int& primer) {
1044         try {
1045         //find group read belongs to
1046         TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, rbarcodes, revPrimer, linker, spacer);
1047         
1048         int success = 1;
1049         string trashCode = "";
1050         int currentSeqsDiffs = 0;
1051         
1052         string seq = read.bases;
1053         
1054         if (trim) {
1055             if(header.clipQualRight < header.clipQualLeft){
1056                 seq = "NNNN";
1057             }
1058             else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1059                 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1060             }
1061             else {
1062                 seq = seq.substr(header.clipQualLeft-1);
1063             }
1064         }else{
1065             //if you wanted the sfftxt then you already converted the bases to the right case
1066             if (!sfftxt) {
1067                 //make the bases you want to clip lowercase and the bases you want to keep upper case
1068                 if(header.clipQualRight == 0){  header.clipQualRight = seq.length();    }
1069                 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]);  }
1070                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++)  {   seq[i] = toupper(seq[i]);  }
1071                 for (int i = (header.clipQualRight-1); i < seq.length(); i++) {   seq[i] = tolower(seq[i]);  }
1072             }
1073         }
1074         
1075         Sequence currSeq(header.name, seq);
1076         QualityScores currQual;
1077         
1078         if(numLinkers != 0){
1079             success = trimOligos.stripLinker(currSeq, currQual);
1080             if(success > ldiffs)                {       trashCode += 'k';       }
1081             else{ currentSeqsDiffs += success;  }
1082             
1083         }
1084         
1085         if(barcodes.size() != 0){
1086             success = trimOligos.stripBarcode(currSeq, currQual, barcode);
1087             if(success > bdiffs)                {       trashCode += 'b';       }
1088             else{ currentSeqsDiffs += success;  }
1089         }
1090         
1091         if(rbarcodes.size() != 0){
1092             success = trimOligos.stripRBarcode(currSeq, currQual, barcode);
1093             if(success > bdiffs)                {       trashCode += 'b';       }
1094             else{ currentSeqsDiffs += success;  }
1095         }
1096         
1097         if(numSpacers != 0){
1098             success = trimOligos.stripSpacer(currSeq, currQual);
1099             if(success > sdiffs)                {       trashCode += 's';       }
1100             else{ currentSeqsDiffs += success;  }
1101             
1102         }
1103         
1104         if(numFPrimers != 0){
1105             success = trimOligos.stripForward(currSeq, currQual, primer, true);
1106             if(success > pdiffs)                {       trashCode += 'f';       }
1107             else{ currentSeqsDiffs += success;  }
1108         }
1109         
1110         if (currentSeqsDiffs > tdiffs)  {       trashCode += 't';   }
1111         
1112         if(revPrimer.size() != 0){
1113             success = trimOligos.stripReverse(currSeq, currQual);
1114             if(!success)                                {       trashCode += 'r';       }
1115         }
1116
1117         
1118         return trashCode.length();
1119     }
1120         catch(exception& e) {
1121                 m->errorOut(e, "SffInfoCommand", "findGroup");
1122                 exit(1);
1123         }
1124 }     
1125 //**********************************************************************************************************************
1126 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
1127         try {
1128                 
1129                 if (name.length() >= 6) {
1130                         string time = name.substr(0, 6);
1131                         unsigned int timeNum = m->fromBase36(time);
1132                         
1133                         int q1 = timeNum / 60;
1134                         int sec = timeNum - 60 * q1;
1135                         int q2 = q1 / 60;
1136                         int minute = q1 - 60 * q2;
1137                         int q3 = q2 / 24;
1138                         int hr = q2 - 24 * q3;
1139                         int q4 = q3 / 32;
1140                         int day = q3 - 32 * q4;
1141                         int q5 = q4 / 13;
1142                         int mon = q4 - 13 * q5;
1143                         int year = 2000 + q5;
1144                 
1145                         timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
1146                 }
1147                 
1148                 if (name.length() >= 9) {
1149                         region = name.substr(7, 2);
1150                 
1151                         string xyNum = name.substr(9);
1152                         unsigned int myXy = m->fromBase36(xyNum);
1153                         int x = myXy >> 12;
1154                         int y = myXy & 4095;
1155                 
1156                         xy = toString(x) + "_" + toString(y);
1157                 }
1158                 
1159                 return 0;
1160         }
1161         catch(exception& e) {
1162                 m->errorOut(e, "SffInfoCommand", "decodeName");
1163                 exit(1);
1164         }
1165 }
1166 //**********************************************************************************************************************
1167 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
1168         try {
1169         
1170                 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
1171                 out << "Version: " << header.version << endl;
1172                 out << "Index Offset: " << header.indexOffset << endl;
1173                 out << "Index Length: " << header.indexLength << endl;
1174                 out << "Number of Reads: " << header.numReads << endl;
1175                 out << "Header Length: " << header.headerLength << endl;
1176                 out << "Key Length: " << header.keyLength << endl;
1177                 out << "Number of Flows: " << header.numFlowsPerRead << endl;
1178                 out << "Format Code: " << header.flogramFormatCode << endl;
1179                 out << "Flow Chars: " << header.flowChars << endl;
1180                 out << "Key Sequence: " << header.keySequence << endl << endl;
1181                         
1182                 return 0;
1183         }
1184         catch(exception& e) {
1185                 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
1186                 exit(1);
1187         }
1188 }
1189 //**********************************************************************************************************************
1190 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
1191         try {
1192                 
1193                 out << ">" << header.name << endl;
1194                 out << "Run Prefix: " << header.timestamp << endl;
1195                 out << "Region #:  " << header.region << endl;
1196                 out << "XY Location: " << header.xy << endl << endl;
1197                 
1198                 out << "Run Name:  " << endl;
1199                 out << "Analysis Name:  " << endl;
1200                 out << "Full Path: " << endl << endl;
1201                 
1202                 out << "Read Header Len: " << header.headerLength << endl;
1203                 out << "Name Length: " << header.nameLength << endl;
1204                 out << "# of Bases: " << header.numBases << endl;
1205                 out << "Clip Qual Left: " << header.clipQualLeft << endl;
1206                 out << "Clip Qual Right: " << header.clipQualRight << endl;
1207                 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
1208                 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
1209                 
1210                 return 0;
1211         }
1212         catch(exception& e) {
1213                 m->errorOut(e, "SffInfoCommand", "printHeader");
1214                 exit(1);
1215         }
1216 }
1217 //**********************************************************************************************************************
1218 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
1219         try {
1220         bool okay = true;
1221         string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
1222         
1223         if (header.clipQualLeft > read.bases.length()) {
1224             okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1225         }
1226         if (header.clipQualRight > read.bases.length()) {
1227             okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1228         }
1229         if (header.clipQualLeft > read.qualScores.size()) {
1230             okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1231         }
1232         if (header.clipQualRight > read.qualScores.size()) {
1233             okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1234         }
1235         
1236         if (okay == false) {
1237             m->mothurOut(message); m->mothurOutEndLine();
1238         }
1239         
1240                 return okay;
1241         }
1242         catch(exception& e) {
1243                 m->errorOut(e, "SffInfoCommand", "sanityCheck");
1244                 exit(1);
1245         }
1246 }
1247 //**********************************************************************************************************************
1248 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
1249         try {
1250                 out << "Flowgram: ";
1251                 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t';  }
1252                 
1253                 out << endl <<  "Flow Indexes: ";
1254                 int sum = 0;
1255                 for (int i = 0; i < read.flowIndex.size(); i++) {  sum +=  read.flowIndex[i];  out << sum << '\t'; }
1256                 
1257                 //make the bases you want to clip lowercase and the bases you want to keep upper case
1258                 if(header.clipQualRight == 0){  header.clipQualRight = read.bases.length();     }
1259                 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
1260                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   read.bases[i] = toupper(read.bases[i]);  }
1261                 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) {   read.bases[i] = tolower(read.bases[i]);  }
1262                 
1263                 out << endl <<  "Bases: " << read.bases << endl << "Quality Scores: ";
1264                 for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
1265         
1266                 
1267                 out << endl << endl;
1268                 
1269                 return 0;
1270         }
1271         catch(exception& e) {
1272                 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
1273                 exit(1);
1274         }
1275 }
1276 //**********************************************************************************************************************
1277 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
1278         try {
1279                 string seq = read.bases;
1280                 
1281         if (trim) {
1282                         if(header.clipQualRight < header.clipQualLeft){
1283                                 seq = "NNNN";
1284                         }
1285                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1286                                 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1287                         }
1288                         else {
1289                                 seq = seq.substr(header.clipQualLeft-1);
1290                         }
1291                 }else{
1292                         //if you wanted the sfftxt then you already converted the bases to the right case
1293                         if (!sfftxt) {
1294                                 //make the bases you want to clip lowercase and the bases you want to keep upper case
1295                                 if(header.clipQualRight == 0){  header.clipQualRight = seq.length();    }
1296                                 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]);  }
1297                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++)  {   seq[i] = toupper(seq[i]);  }
1298                                 for (int i = (header.clipQualRight-1); i < seq.length(); i++) {   seq[i] = tolower(seq[i]);  }
1299                         }
1300                 }
1301                 
1302                 out << ">" << header.name  << " xy=" << header.xy << endl;
1303                 out << seq << endl;
1304                 
1305                 return 0;
1306         }
1307         catch(exception& e) {
1308                 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
1309                 exit(1);
1310         }
1311 }
1312
1313 //**********************************************************************************************************************
1314 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
1315         try {
1316                 
1317                 if (trim) {
1318                         if(header.clipQualRight < header.clipQualLeft){
1319                                 out << ">" << header.name << " xy=" << header.xy << endl;
1320                                 out << "0\t0\t0\t0";
1321                         }
1322                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1323                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1324                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   out << read.qualScores[i] << '\t'; }
1325                         }
1326                         else{
1327                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1328                                 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';   }                       
1329                         }
1330                 }else{
1331                         out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
1332                         for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
1333                 }
1334                 
1335                 out << endl;
1336                 
1337                 return 0;
1338         }
1339         catch(exception& e) {
1340                 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
1341                 exit(1);
1342         }
1343 }
1344
1345 //**********************************************************************************************************************
1346 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
1347         try {
1348                 if(header.clipQualRight > header.clipQualLeft){
1349                         
1350                         int rightIndex = 0;
1351                         for (int i = 0; i < header.clipQualRight; i++) {  rightIndex +=  read.flowIndex[i];     }
1352
1353                         out << header.name << ' ' << rightIndex;
1354                         for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100);  }
1355                         out << endl;
1356                 }
1357                 
1358                 
1359                 return 0;
1360         }
1361         catch(exception& e) {
1362                 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
1363                 exit(1);
1364         }
1365 }
1366 //**********************************************************************************************************************
1367 int SffInfoCommand::readAccnosFile(string filename) {
1368         try {
1369                 //remove old names
1370                 seqNames.clear();
1371                 
1372                 ifstream in;
1373                 m->openInputFile(filename, in);
1374                 string name;
1375                 
1376                 while(!in.eof()){
1377                         in >> name; m->gobble(in);
1378                                                 
1379                         seqNames.insert(name);
1380                         
1381                         if (m->control_pressed) { seqNames.clear(); break; }
1382                 }
1383                 in.close();             
1384                 
1385                 return 0;
1386         }
1387         catch(exception& e) {
1388                 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
1389                 exit(1);
1390         }
1391 }
1392 //**********************************************************************************************************************
1393 int SffInfoCommand::parseSffTxt() {
1394         try {
1395                 
1396                 ifstream inSFF;
1397                 m->openInputFile(sfftxtFilename, inSFF);
1398                 
1399                 if (outputDir == "") {  outputDir += m->hasPath(sfftxtFilename); }
1400                 
1401                 //output file names
1402                 ofstream outFasta, outQual, outFlow;
1403                 string outFastaFileName, outQualFileName;
1404                 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
1405                 if (fileRoot.length() > 0) {
1406                         //rip off last .
1407                         fileRoot = fileRoot.substr(0, fileRoot.length()-1);
1408                         fileRoot = m->getRootName(fileRoot);
1409                 }
1410                 
1411                 string outFlowFileName = outputDir + fileRoot + getOutputFileNameTag("flow");
1412                 if (trim) {
1413                         outFastaFileName = outputDir + fileRoot + getOutputFileNameTag("fasta");
1414                         outQualFileName = outputDir + fileRoot + getOutputFileNameTag("qfile");
1415                 }else{
1416                         outFastaFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("fasta");
1417                         outQualFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("qfile");
1418                 }
1419                 
1420                 if (fasta)      { m->openOutputFile(outFastaFileName, outFasta);        outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
1421                 if (qual)       { m->openOutputFile(outQualFileName, outQual);          outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName);  }
1422                 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);  }
1423                 
1424                 //read common header
1425                 string commonHeader = m->getline(inSFF);
1426                 string magicNumber = m->getline(inSFF); 
1427                 string version = m->getline(inSFF);
1428                 string indexOffset = m->getline(inSFF);
1429                 string indexLength = m->getline(inSFF);
1430                 int numReads = parseHeaderLineToInt(inSFF);
1431                 string headerLength = m->getline(inSFF);
1432                 string keyLength = m->getline(inSFF);
1433                 int numFlows = parseHeaderLineToInt(inSFF);
1434                 string flowgramCode = m->getline(inSFF);
1435                 string flowChars = m->getline(inSFF);
1436                 string keySequence = m->getline(inSFF);
1437                 m->gobble(inSFF);
1438                 
1439                 string seqName;
1440                 
1441                 if (flow)       {       outFlow << numFlows << endl;    }
1442                 
1443                 for(int i=0;i<numReads;i++){
1444                         
1445                         //sanity check
1446                         if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
1447                         
1448                         Header header;
1449                         
1450                         //parse read header
1451                         inSFF >> seqName;
1452                         seqName = seqName.substr(1);
1453                         m->gobble(inSFF);
1454                         header.name = seqName;
1455                         
1456                         string runPrefix = parseHeaderLineToString(inSFF);              header.timestamp = runPrefix;
1457                         string regionNumber = parseHeaderLineToString(inSFF);   header.region = regionNumber;
1458                         string xyLocation = parseHeaderLineToString(inSFF);             header.xy = xyLocation;
1459                         m->gobble(inSFF);
1460                                 
1461                         string runName = parseHeaderLineToString(inSFF);
1462                         string analysisName = parseHeaderLineToString(inSFF);
1463                         string fullPath = parseHeaderLineToString(inSFF);
1464                         m->gobble(inSFF);
1465                         
1466                         string readHeaderLen = parseHeaderLineToString(inSFF);  convert(readHeaderLen, header.headerLength);
1467                         string nameLength = parseHeaderLineToString(inSFF);             convert(nameLength, header.nameLength);
1468                         int numBases = parseHeaderLineToInt(inSFF);                             header.numBases = numBases;
1469                         string clipQualLeft = parseHeaderLineToString(inSFF);   convert(clipQualLeft, header.clipQualLeft);
1470                         int clipQualRight = parseHeaderLineToInt(inSFF);                header.clipQualRight = clipQualRight;
1471                         string clipAdapLeft = parseHeaderLineToString(inSFF);   convert(clipAdapLeft, header.clipAdapterLeft);
1472                         string clipAdapRight = parseHeaderLineToString(inSFF);  convert(clipAdapRight, header.clipAdapterRight);
1473                         m->gobble(inSFF);
1474                                 
1475                         seqRead read;
1476                         
1477                         //parse read
1478                         vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows);      read.flowgram = flowVector;
1479                         vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases); 
1480                         
1481                         //adjust for print
1482                         vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1483                         for (int j = 1; j < flowIndices.size(); j++) {   flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]);   }
1484                         read.flowIndex = flowIndicesAdjusted;
1485                         
1486                         string bases = parseHeaderLineToString(inSFF);                                                                          read.bases = bases;
1487                         vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases);       read.qualScores = qualityScores;
1488                         m->gobble(inSFF);
1489                                         
1490                         //if you have provided an accosfile and this seq is not in it, then dont print
1491                         bool print = true;
1492                         if (seqNames.size() != 0) {   if (seqNames.count(header.name) == 0) { print = false; }  }
1493                         
1494                         //print 
1495                         if (print) {
1496                                 if (fasta)      {       printFastaSeqData(outFasta, read, header);      }
1497                                 if (qual)       {       printQualSeqData(outQual, read, header);        }
1498                                 if (flow)       {       printFlowSeqData(outFlow, read, header);        }
1499                         }
1500                         
1501                         //report progress
1502                         if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine();             }
1503                         
1504                         if (m->control_pressed) {  break;  }
1505                 }
1506                 
1507                 //report progress
1508                 if (!m->control_pressed) {   if((numReads) % 10000 != 0){       m->mothurOut(toString(numReads)); m->mothurOutEndLine();                }  }
1509                 
1510                 inSFF.close();
1511                 
1512                 if (fasta)      {  outFasta.close();    }
1513                 if (qual)       {  outQual.close();             }
1514                 if (flow)       {  outFlow.close();             }
1515                 
1516                 return 0;
1517         }
1518         catch(exception& e) {
1519                 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1520                 exit(1);
1521         }
1522 }
1523 //**********************************************************************************************************************
1524
1525 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1526         try {
1527                 int number;
1528                 
1529                 while (!file.eof())     {
1530                         
1531                         char c = file.get(); 
1532                         if (c == ':'){
1533                                 file >> number;
1534                                 break;
1535                         }
1536                         
1537                 }
1538                 m->gobble(file);
1539                 return number;
1540         }
1541         catch(exception& e) {
1542                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1543                 exit(1);
1544         }
1545         
1546 }
1547
1548 //**********************************************************************************************************************
1549
1550 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1551         try {
1552                 string text;
1553                 
1554                 while (!file.eof())     {
1555                         char c = file.get(); 
1556                         
1557                         if (c == ':'){
1558                                 //m->gobble(file);
1559                                 //text = m->getline(file);      
1560                                 file >> text;
1561                                 break;
1562                         }
1563                 }
1564                 m->gobble(file);
1565                 
1566                 return text;
1567         }
1568         catch(exception& e) {
1569                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1570                 exit(1);
1571         }
1572 }
1573
1574 //**********************************************************************************************************************
1575
1576 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1577         try {
1578                 vector<unsigned short> floatVector(length);
1579                 
1580                 while (!file.eof())     {
1581                         char c = file.get(); 
1582                         if (c == ':'){
1583                                 float temp;
1584                                 for(int i=0;i<length;i++){
1585                                         file >> temp;
1586                                         floatVector[i] = temp * 100;
1587                                 }
1588                                 break;
1589                         }
1590                 }
1591                 m->gobble(file);        
1592                 return floatVector;
1593         }
1594         catch(exception& e) {
1595                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1596                 exit(1);
1597         }
1598 }
1599
1600 //**********************************************************************************************************************
1601
1602 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1603         try {
1604                 vector<unsigned int> intVector(length);
1605                 
1606                 while (!file.eof())     {
1607                         char c = file.get(); 
1608                         if (c == ':'){
1609                                 for(int i=0;i<length;i++){
1610                                         file >> intVector[i];
1611                                 }
1612                                 break;
1613                         }
1614                 }
1615                 m->gobble(file);        
1616                 return intVector;
1617         }
1618         catch(exception& e) {
1619                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1620                 exit(1);
1621         }
1622 }
1623 //***************************************************************************************************************
1624
1625 bool SffInfoCommand::readOligos(string oligoFile){
1626         try {
1627         filehandles.clear();
1628         numSplitReads.clear();
1629         filehandlesHeaders.clear();
1630         
1631                 ifstream inOligos;
1632                 m->openInputFile(oligoFile, inOligos);
1633                 
1634                 string type, oligo, group;
1635         
1636                 int indexPrimer = 0;
1637                 int indexBarcode = 0;
1638                 
1639                 while(!inOligos.eof()){
1640             
1641                         inOligos >> type; 
1642             
1643                         if(type[0] == '#'){
1644                                 while (!inOligos.eof()) {       char c = inOligos.get();  if (c == 10 || c == 13){      break;  }       } // get rest of line if there's any crap there
1645                                 m->gobble(inOligos);
1646                         }
1647                         else{
1648                                 m->gobble(inOligos);
1649                                 //make type case insensitive
1650                                 for(int i=0;i<type.length();i++){       type[i] = toupper(type[i]);  }
1651                                 
1652                                 inOligos >> oligo;
1653                                 
1654                                 for(int i=0;i<oligo.length();i++){
1655                                         oligo[i] = toupper(oligo[i]);
1656                                         if(oligo[i] == 'U')     {       oligo[i] = 'T'; }
1657                                 }
1658                                 
1659                                 if(type == "FORWARD"){
1660                                         group = "";
1661                                         
1662                                         // get rest of line in case there is a primer name
1663                                         while (!inOligos.eof()) {       
1664                                                 char c = inOligos.get(); 
1665                                                 if (c == 10 || c == 13){        break;  }
1666                                                 else if (c == 32 || c == 9){;} //space or tab
1667                                                 else {  group += c;  }
1668                                         } 
1669                                         
1670                                         //check for repeat barcodes
1671                                         map<string, int>::iterator itPrime = primers.find(oligo);
1672                                         if (itPrime != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
1673                                         
1674                                         primers[oligo]=indexPrimer; indexPrimer++;              
1675                                         primerNameVector.push_back(group);
1676                                 }else if(type == "REVERSE"){
1677                                         //Sequence oligoRC("reverse", oligo);
1678                                         //oligoRC.reverseComplement();
1679                     string oligoRC = reverseOligo(oligo);
1680                                         revPrimer.push_back(oligoRC);
1681                                 }
1682                                 else if(type == "BARCODE"){
1683                                         inOligos >> group;
1684                     
1685                     //barcode lines can look like   BARCODE   atgcatgc   groupName  - for 454 seqs
1686                     //or                            BARCODE   atgcatgc   atgcatgc    groupName  - for illumina data that has forward and reverse info
1687                                         string temp = "";
1688                     while (!inOligos.eof())     {       
1689                                                 char c = inOligos.get(); 
1690                                                 if (c == 10 || c == 13){        break;  }
1691                                                 else if (c == 32 || c == 9){;} //space or tab
1692                                                 else {  temp += c;  }
1693                                         } 
1694                                         
1695                     //then this is illumina data with 4 columns
1696                     if (temp != "") {  
1697                         string reverseBarcode = reverseOligo(group); //reverse barcode
1698                         group = temp;
1699                         
1700                         //check for repeat barcodes
1701                         map<string, int>::iterator itBar = rbarcodes.find(reverseBarcode);
1702                         if (itBar != rbarcodes.end()) { m->mothurOut("barcode " + reverseBarcode + " is in your oligos file already."); m->mothurOutEndLine();  }
1703                                                 
1704                         rbarcodes[reverseBarcode]=indexBarcode; 
1705                     }
1706                     
1707                                         //check for repeat barcodes
1708                                         map<string, int>::iterator itBar = barcodes.find(oligo);
1709                                         if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
1710                     
1711                                         barcodes[oligo]=indexBarcode; indexBarcode++;
1712                                         barcodeNameVector.push_back(group);
1713
1714                                 }else if(type == "LINKER"){
1715                                         linker.push_back(oligo);
1716                                 }else if(type == "SPACER"){
1717                                         spacer.push_back(oligo);
1718                                 }
1719                                 else{   m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine(); }
1720                         }
1721                         m->gobble(inOligos);
1722                 }       
1723                 inOligos.close();
1724                 
1725                 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ split = 1;      }
1726                 
1727                 //add in potential combos
1728                 if(barcodeNameVector.size() == 0){
1729                         barcodes[""] = 0;
1730                         barcodeNameVector.push_back("");                        
1731                 }
1732                 
1733                 if(primerNameVector.size() == 0){
1734                         primers[""] = 0;
1735                         primerNameVector.push_back("");                 
1736                 }
1737                 
1738                 filehandles.resize(barcodeNameVector.size());
1739                 for(int i=0;i<filehandles.size();i++){
1740                         filehandles[i].assign(primerNameVector.size(), "");
1741                 }
1742                         
1743                 if(split > 1){
1744                         set<string> uniqueNames; //used to cleanup outputFileNames
1745                         for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1746                                 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1747                                         
1748                                         string primerName = primerNameVector[itPrimer->second];
1749                                         string barcodeName = barcodeNameVector[itBar->second];
1750                                         
1751                                         string comboGroupName = "";
1752                                         string fastaFileName = "";
1753                                         string qualFileName = "";
1754                                         string nameFileName = "";
1755                                         
1756                                         if(primerName == ""){
1757                                                 comboGroupName = barcodeNameVector[itBar->second];
1758                                         }
1759                                         else{
1760                                                 if(barcodeName == ""){
1761                                                         comboGroupName = primerNameVector[itPrimer->second];
1762                                                 }
1763                                                 else{
1764                                                         comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
1765                                                 }
1766                                         }
1767                                         
1768                                         ofstream temp;
1769                                         string thisFilename = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + comboGroupName + "." + getOutputFileNameTag("sff");
1770                                         if (uniqueNames.count(thisFilename) == 0) {
1771                                                 outputNames.push_back(thisFilename);
1772                                                 outputTypes["sff"].push_back(thisFilename);
1773                                                 uniqueNames.insert(thisFilename);
1774                                         }
1775                                         
1776                                         filehandles[itBar->second][itPrimer->second] = thisFilename;
1777                                         m->openOutputFile(thisFilename, temp);          temp.close();
1778                                 }
1779                         }
1780                 }
1781                 numFPrimers = primers.size();
1782         numLinkers = linker.size();
1783         numSpacers = spacer.size();
1784                 noMatchFile = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + "scrap." + getOutputFileNameTag("sff");
1785         m->mothurRemove(noMatchFile);
1786         
1787                 bool allBlank = true;
1788                 for (int i = 0; i < barcodeNameVector.size(); i++) {
1789                         if (barcodeNameVector[i] != "") {
1790                                 allBlank = false;
1791                                 break;
1792                         }
1793                 }
1794                 for (int i = 0; i < primerNameVector.size(); i++) {
1795                         if (primerNameVector[i] != "") {
1796                                 allBlank = false;
1797                                 break;
1798                         }
1799                 }
1800                 
1801         filehandlesHeaders.resize(filehandles.size());
1802         numSplitReads.resize(filehandles.size());
1803         for (int i = 0; i < filehandles.size(); i++) { 
1804             numSplitReads[i].resize(filehandles[i].size(), 0); 
1805             for (int j = 0; j < filehandles[i].size(); j++) {
1806                 filehandlesHeaders[i].push_back(filehandles[i][j]+"headers");
1807             }
1808         }
1809                              
1810                 if (allBlank) {
1811                         m->mothurOut("[WARNING]: your oligos file does not contain any group names.  mothur will not create a split the sff file."); m->mothurOutEndLine();
1812                         split = 1;
1813                         return false;
1814                 }
1815                 
1816                 return true;
1817                 
1818         }
1819         catch(exception& e) {
1820                 m->errorOut(e, "SffInfoCommand", "readOligos");
1821                 exit(1);
1822         }
1823 }
1824 //********************************************************************/
1825 string SffInfoCommand::reverseOligo(string oligo){
1826         try {
1827         string reverse = "";
1828         
1829         for(int i=oligo.length()-1;i>=0;i--){
1830             
1831             if(oligo[i] == 'A')         {       reverse += 'T'; }
1832             else if(oligo[i] == 'T'){   reverse += 'A'; }
1833             else if(oligo[i] == 'U'){   reverse += 'A'; }
1834             
1835             else if(oligo[i] == 'G'){   reverse += 'C'; }
1836             else if(oligo[i] == 'C'){   reverse += 'G'; }
1837             
1838             else if(oligo[i] == 'R'){   reverse += 'Y'; }
1839             else if(oligo[i] == 'Y'){   reverse += 'R'; }
1840             
1841             else if(oligo[i] == 'M'){   reverse += 'K'; }
1842             else if(oligo[i] == 'K'){   reverse += 'M'; }
1843             
1844             else if(oligo[i] == 'W'){   reverse += 'W'; }
1845             else if(oligo[i] == 'S'){   reverse += 'S'; }
1846             
1847             else if(oligo[i] == 'B'){   reverse += 'V'; }
1848             else if(oligo[i] == 'V'){   reverse += 'B'; }
1849             
1850             else if(oligo[i] == 'D'){   reverse += 'H'; }
1851             else if(oligo[i] == 'H'){   reverse += 'D'; }
1852             
1853             else                                                {       reverse += 'N'; }
1854         }
1855         
1856         
1857         return reverse;
1858     }
1859         catch(exception& e) {
1860                 m->errorOut(e, "SffInfoCommand", "reverseOligo");
1861                 exit(1);
1862         }
1863 }
1864
1865 //**********************************************************************************************************************
1866
1867
1868                                 
1869