]> git.donarmstrong.com Git - mothur.git/blob - sffinfocommand.cpp
added sff.multiple command. fixed issue with windows paralellization in chimera...
[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; hasOligos = 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>::iterator it;
587                         set<string> namesToRemove;
588                         for(int i=0;i<filehandles.size();i++){
589                                 for(int j=0;j<filehandles[0].size();j++){
590                                         if (filehandles[i][j] != "") {
591                                                 if (namesToRemove.count(filehandles[i][j]) == 0) {
592                                                         if(m->isBlank(filehandles[i][j])){
593                                                                 m->mothurRemove(filehandles[i][j]);
594                                 m->mothurRemove(filehandlesHeaders[i][j]);
595                                                                 namesToRemove.insert(filehandles[i][j]);
596                             }
597                                                 }
598                                         }
599                                 }
600                         }
601             
602             //append new header to reads
603             for (int i = 0; i < filehandles.size(); i++) {
604                 for (int j = 0; j < filehandles[i].size(); j++) {
605                     m->appendFiles(filehandles[i][j], filehandlesHeaders[i][j]);
606                     m->renameFile(filehandlesHeaders[i][j], filehandles[i][j]);
607                     m->mothurRemove(filehandlesHeaders[i][j]);
608                     if (numSplitReads[i][j] == 0) { m->mothurRemove(filehandles[i][j]); }
609                 }
610             }
611                         
612                         //remove names for outputFileNames, just cleans up the output
613                         for(int i = 0; i < outputNames.size(); i++) { 
614                 if (namesToRemove.count(outputNames[i]) != 0) { 
615                     outputNames.erase(outputNames.begin()+i);
616                     i--;
617                 } 
618             }
619             
620             if(m->isBlank(noMatchFile)){  m->mothurRemove(noMatchFile); }
621             else { outputNames.push_back(noMatchFile); outputTypes["sff"].push_back(noMatchFile); }
622         }
623         
624                 return count;
625         }
626         catch(exception& e) {
627                 m->errorOut(e, "SffInfoCommand", "extractSffInfo");
628                 exit(1);
629         }
630 }
631 //**********************************************************************************************************************
632 int SffInfoCommand::readCommonHeader(ifstream& in, CommonHeader& header){
633         try {
634         
635                 if (!in.eof()) {
636
637                         //read magic number
638                         char buffer[4];
639                         in.read(buffer, 4);
640                         header.magicNumber = be_int4(*(unsigned int *)(&buffer));
641             
642                         //read version
643                         char buffer9[4];
644                         in.read(buffer9, 4);
645                         header.version = "";
646                         for (int i = 0; i < 4; i++) {  header.version += toString((int)(buffer9[i]));  }
647     
648                         //read offset
649                         char buffer2 [8];
650                         in.read(buffer2, 8);
651                         header.indexOffset =  be_int8(*(unsigned long long *)(&buffer2));
652                         
653                         //read index length
654                         char buffer3 [4];
655                         in.read(buffer3, 4);
656                         header.indexLength =  be_int4(*(unsigned int *)(&buffer3));
657                         
658                         //read num reads
659                         char buffer4 [4];
660                         in.read(buffer4, 4);
661                         header.numReads =  be_int4(*(unsigned int *)(&buffer4));
662                                 
663                         //read header length
664                         char buffer5 [2];
665                         in.read(buffer5, 2);
666                         header.headerLength =  be_int2(*(unsigned short *)(&buffer5));
667                                         
668                         //read key length
669                         char buffer6 [2];
670                         in.read(buffer6, 2);
671                         header.keyLength = be_int2(*(unsigned short *)(&buffer6));
672                         
673                         //read number of flow reads
674                         char buffer7 [2];
675                         in.read(buffer7, 2);
676                         header.numFlowsPerRead =  be_int2(*(unsigned short *)(&buffer7));
677                                 
678                         //read format code
679                         char buffer8 [1];
680                         in.read(buffer8, 1);
681                         header.flogramFormatCode = (int)(buffer8[0]);
682                         
683                         //read flow chars
684                         char* tempBuffer = new char[header.numFlowsPerRead];
685                         in.read(&(*tempBuffer), header.numFlowsPerRead); 
686                         header.flowChars = tempBuffer;
687                         if (header.flowChars.length() > header.numFlowsPerRead) { header.flowChars = header.flowChars.substr(0, header.numFlowsPerRead);  }
688                         delete[] tempBuffer;
689                         
690                         //read key
691                         char* tempBuffer2 = new char[header.keyLength];
692                         in.read(&(*tempBuffer2), header.keyLength);
693                         header.keySequence = tempBuffer2;
694                         if (header.keySequence.length() > header.keyLength) { header.keySequence = header.keySequence.substr(0, header.keyLength);  }
695                         delete[] tempBuffer2;
696                         
697                         /* Pad to 8 chars */
698                         unsigned long long spotInFile = in.tellg();
699                         unsigned long long spot = (spotInFile + 7)& ~7;  // ~ inverts
700                         in.seekg(spot);
701             
702         }else{
703                         m->mothurOut("Error reading sff common header."); m->mothurOutEndLine();
704                 }
705         
706                 return 0;
707         
708         }
709         catch(exception& e) {
710                 m->errorOut(e, "SffInfoCommand", "readCommonHeader");
711                 exit(1);
712         }
713 }
714 //**********************************************************************************************************************
715 int SffInfoCommand::adjustCommonHeader(CommonHeader header){
716         try {
717
718         char* mybuffer = new char[4];
719         ifstream in;
720         in.open(currentFileName.c_str(), ios::binary);
721         
722         //magic number
723         in.read(mybuffer,4);
724         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
725             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
726                 ofstream out;
727                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
728                 out.write(mybuffer, in.gcount()); 
729                 out.close();
730             }
731         }
732         delete[] mybuffer;
733         
734         //version
735         mybuffer = new char[4];
736         in.read(mybuffer,4);
737         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
738             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
739                 ofstream out;
740                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
741                 out.write(mybuffer, in.gcount()); 
742                 out.close();
743             }
744         }
745         delete[] mybuffer;
746         
747         //offset
748         mybuffer = new char[8];
749         in.read(mybuffer,8);
750         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
751             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
752                 ofstream out;
753                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
754                 out.write(mybuffer, in.gcount()); 
755                 out.close();
756             }
757         }
758         delete[] mybuffer;
759             
760                         
761         //read index length
762                 mybuffer = new char[4];
763         in.read(mybuffer,4);
764         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
765             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
766                 ofstream out;
767                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
768                 out.write(mybuffer, in.gcount()); 
769                 out.close();
770             }
771         }
772         delete[] mybuffer;
773                 
774         //change num reads
775         mybuffer = new char[4];
776         in.read(mybuffer,4);
777         delete[] mybuffer;
778         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
779             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
780                 ofstream out;
781                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
782                 //convert number of reads to 4 byte char*
783                 char* thisbuffer = new char[4];
784                 thisbuffer[0] = (numSplitReads[i][j] >> 24) & 0xFF;
785                 thisbuffer[1] = (numSplitReads[i][j] >> 16) & 0xFF;
786                 thisbuffer[2] = (numSplitReads[i][j] >> 8) & 0xFF;
787                 thisbuffer[3] = numSplitReads[i][j] & 0xFF;
788                 out.write(thisbuffer, 4);
789                 out.close();
790                 delete[] thisbuffer;
791             }
792         }
793             
794         //read header length
795         mybuffer = new char[2];
796         in.read(mybuffer,2);
797         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
798             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
799                 ofstream out;
800                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
801                 out.write(mybuffer, in.gcount()); 
802                 out.close();
803             }
804         }
805         delete[] mybuffer;
806             
807         //read key length
808         mybuffer = new char[2];
809         in.read(mybuffer,2);
810         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
811             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
812                 ofstream out;
813                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
814                 out.write(mybuffer, in.gcount()); 
815                 out.close();
816             }
817         }
818         delete[] mybuffer;
819                         
820         //read number of flow reads
821         mybuffer = new char[2];
822         in.read(mybuffer,2);
823         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
824             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
825                 ofstream out;
826                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
827                 out.write(mybuffer, in.gcount()); 
828                 out.close();
829             }
830         }
831         delete[] mybuffer;
832             
833         //read format code
834         mybuffer = new char[1];
835         in.read(mybuffer,1);
836         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
837             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
838                 ofstream out;
839                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
840                 out.write(mybuffer, in.gcount()); 
841                 out.close();
842             }
843         }
844         delete[] mybuffer;
845                         
846         //read flow chars
847         mybuffer = new char[header.numFlowsPerRead];
848         in.read(mybuffer,header.numFlowsPerRead);
849         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
850             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
851                 ofstream out;
852                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
853                 out.write(mybuffer, in.gcount()); 
854                 out.close();
855             }
856         }
857         delete[] mybuffer;
858                         
859         //read key
860         mybuffer = new char[header.keyLength];
861         in.read(mybuffer,header.keyLength);
862         for (int i = 0; i < filehandlesHeaders.size(); i++) {  
863             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
864                 ofstream out;
865                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
866                 out.write(mybuffer, in.gcount()); 
867                 out.close();
868             }
869         }
870         delete[] mybuffer;
871         
872                         
873         /* Pad to 8 chars */
874         unsigned long long spotInFile = in.tellg();
875         unsigned long long spot = (spotInFile + 7)& ~7;  // ~ inverts
876         in.seekg(spot);
877         
878         mybuffer = new char[spot-spotInFile];
879         for (int i = 0; i < filehandlesHeaders.size(); i++) { 
880             for (int j = 0; j < filehandlesHeaders[i].size(); j++) {
881                 ofstream out;
882                 m->openOutputFileAppend(filehandlesHeaders[i][j], out);
883                 out.write(mybuffer, spot-spotInFile); 
884                 out.close();
885             }
886         }
887         delete[] mybuffer;
888         in.close();
889                 return 0;
890         
891         }
892         catch(exception& e) {
893                 m->errorOut(e, "SffInfoCommand", "adjustCommonHeader");
894                 exit(1);
895         }
896 }
897 //**********************************************************************************************************************
898 int SffInfoCommand::readSeqData(ifstream& in, seqRead& read, int numFlowReads, Header& header){
899         try {
900         unsigned long long startSpotInFile = in.tellg();
901                 if (!in.eof()) {
902             
903             /*****************************************/
904             //read header
905             
906             //read header length
907                         char buffer [2];
908                         in.read(buffer, 2);
909                         header.headerLength = be_int2(*(unsigned short *)(&buffer));
910             
911                         //read name length
912                         char buffer2 [2];
913                         in.read(buffer2, 2);
914                         header.nameLength = be_int2(*(unsigned short *)(&buffer2));
915             
916                         //read num bases
917                         char buffer3 [4];
918                         in.read(buffer3, 4);
919                         header.numBases =  be_int4(*(unsigned int *)(&buffer3));
920                         
921                         //read clip qual left
922                         char buffer4 [2];
923                         in.read(buffer4, 2);
924                         header.clipQualLeft =  be_int2(*(unsigned short *)(&buffer4));
925                         header.clipQualLeft = 5; 
926                         
927                         //read clip qual right
928                         char buffer5 [2];
929                         in.read(buffer5, 2);
930                         header.clipQualRight =  be_int2(*(unsigned short *)(&buffer5));
931                         
932                         //read clipAdapterLeft
933                         char buffer6 [2];
934                         in.read(buffer6, 2);
935                         header.clipAdapterLeft = be_int2(*(unsigned short *)(&buffer6));
936             
937                         //read clipAdapterRight
938                         char buffer7 [2];
939                         in.read(buffer7, 2);
940                         header.clipAdapterRight = be_int2(*(unsigned short *)(&buffer7));
941             
942                         //read name
943                         char* tempBuffer = new char[header.nameLength];
944                         in.read(&(*tempBuffer), header.nameLength);
945                         header.name = tempBuffer;
946                         if (header.name.length() > header.nameLength) { header.name = header.name.substr(0, header.nameLength);  }
947                         delete[] tempBuffer;
948                         
949                         //extract info from name
950                         decodeName(header.timestamp, header.region, header.xy, header.name);
951                         
952                         /* Pad to 8 chars */
953                         unsigned long long spotInFile = in.tellg();
954                         unsigned long long spot = (spotInFile + 7)& ~7;
955                         in.seekg(spot);
956
957             /*****************************************/
958             //sequence read 
959             
960                         //read flowgram
961                         read.flowgram.resize(numFlowReads);
962                         for (int i = 0; i < numFlowReads; i++) {  
963                                 char buffer [2];
964                                 in.read(buffer, 2);
965                                 read.flowgram[i] = be_int2(*(unsigned short *)(&buffer));
966                         }
967             
968                         //read flowIndex
969                         read.flowIndex.resize(header.numBases);
970                         for (int i = 0; i < header.numBases; i++) {  
971                                 char temp[1];
972                                 in.read(temp, 1);
973                                 read.flowIndex[i] = be_int1(*(unsigned char *)(&temp));
974                         }
975         
976                         //read bases
977                         char* tempBuffer6 = new char[header.numBases];
978                         in.read(&(*tempBuffer6), header.numBases);
979                         read.bases = tempBuffer6;
980                         if (read.bases.length() > header.numBases) { read.bases = read.bases.substr(0, header.numBases);  }
981                         delete[] tempBuffer6;
982
983                         //read qual scores
984                         read.qualScores.resize(header.numBases);
985                         for (int i = 0; i < header.numBases; i++) {  
986                                 char temp[1];
987                                 in.read(temp, 1);
988                                 read.qualScores[i] = be_int1(*(unsigned char *)(&temp));
989                         }
990         
991                         /* Pad to 8 chars */
992                         spotInFile = in.tellg();
993                         spot = (spotInFile + 7)& ~7;
994                         in.seekg(spot);
995             
996             if (split > 1) {
997                 char * mybuffer;
998                 mybuffer = new char [spot-startSpotInFile];
999                 ifstream in2;
1000                 m->openInputFile(currentFileName, in2);
1001                 in2.seekg(startSpotInFile);
1002                 in2.read(mybuffer,spot-startSpotInFile);
1003                 in2.close();
1004                 
1005                 int barcodeIndex, primerIndex;
1006                 int trashCodeLength = findGroup(header, read, barcodeIndex, primerIndex);
1007                                 
1008                 if(trashCodeLength == 0){
1009                     ofstream out;
1010                     m->openOutputFileAppend(filehandles[barcodeIndex][primerIndex], out);
1011                     out.write(mybuffer, in2.gcount()); 
1012                     out.close();
1013                     delete[] mybuffer;
1014                     numSplitReads[barcodeIndex][primerIndex]++;
1015                                 }
1016                                 else{
1017                                         ofstream out;
1018                     m->openOutputFileAppend(noMatchFile, out);
1019                     out.write(mybuffer, in2.gcount()); 
1020                     out.close();
1021                     delete[] mybuffer;
1022                                 }
1023                                 
1024                         }
1025                 }else{
1026                         m->mothurOut("Error reading."); m->mothurOutEndLine();
1027                 }
1028
1029                 return 0;
1030         }
1031         catch(exception& e) {
1032                 m->errorOut(e, "SffInfoCommand", "readSeqData");
1033                 exit(1);
1034         }
1035 }
1036 //**********************************************************************************************************************
1037 int SffInfoCommand::findGroup(Header header, seqRead read, int& barcode, int& primer) {
1038         try {
1039         //find group read belongs to
1040         TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, rbarcodes, revPrimer, linker, spacer);
1041         
1042         int success = 1;
1043         string trashCode = "";
1044         int currentSeqsDiffs = 0;
1045         
1046         string seq = read.bases;
1047         
1048         if (trim) {
1049             if(header.clipQualRight < header.clipQualLeft){
1050                 seq = "NNNN";
1051             }
1052             else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1053                 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1054             }
1055             else {
1056                 seq = seq.substr(header.clipQualLeft-1);
1057             }
1058         }else{
1059             //if you wanted the sfftxt then you already converted the bases to the right case
1060             if (!sfftxt) {
1061                 //make the bases you want to clip lowercase and the bases you want to keep upper case
1062                 if(header.clipQualRight == 0){  header.clipQualRight = seq.length();    }
1063                 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]);  }
1064                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++)  {   seq[i] = toupper(seq[i]);  }
1065                 for (int i = (header.clipQualRight-1); i < seq.length(); i++) {   seq[i] = tolower(seq[i]);  }
1066             }
1067         }
1068         
1069         Sequence currSeq(header.name, seq);
1070         QualityScores currQual;
1071         
1072         if(numLinkers != 0){
1073             success = trimOligos.stripLinker(currSeq, currQual);
1074             if(success > ldiffs)                {       trashCode += 'k';       }
1075             else{ currentSeqsDiffs += success;  }
1076             
1077         }
1078         
1079         if(barcodes.size() != 0){
1080             success = trimOligos.stripBarcode(currSeq, currQual, barcode);
1081             if(success > bdiffs)                {       trashCode += 'b';       }
1082             else{ currentSeqsDiffs += success;  }
1083         }
1084         
1085         if(rbarcodes.size() != 0){
1086             success = trimOligos.stripRBarcode(currSeq, currQual, barcode);
1087             if(success > bdiffs)                {       trashCode += 'b';       }
1088             else{ currentSeqsDiffs += success;  }
1089         }
1090         
1091         if(numSpacers != 0){
1092             success = trimOligos.stripSpacer(currSeq, currQual);
1093             if(success > sdiffs)                {       trashCode += 's';       }
1094             else{ currentSeqsDiffs += success;  }
1095             
1096         }
1097         
1098         if(numFPrimers != 0){
1099             success = trimOligos.stripForward(currSeq, currQual, primer, true);
1100             if(success > pdiffs)                {       trashCode += 'f';       }
1101             else{ currentSeqsDiffs += success;  }
1102         }
1103         
1104         if (currentSeqsDiffs > tdiffs)  {       trashCode += 't';   }
1105         
1106         if(revPrimer.size() != 0){
1107             success = trimOligos.stripReverse(currSeq, currQual);
1108             if(!success)                                {       trashCode += 'r';       }
1109         }
1110
1111         
1112         return trashCode.length();
1113     }
1114         catch(exception& e) {
1115                 m->errorOut(e, "SffInfoCommand", "findGroup");
1116                 exit(1);
1117         }
1118 }     
1119 //**********************************************************************************************************************
1120 int SffInfoCommand::decodeName(string& timestamp, string& region, string& xy, string name) {
1121         try {
1122                 
1123                 if (name.length() >= 6) {
1124                         string time = name.substr(0, 6);
1125                         unsigned int timeNum = m->fromBase36(time);
1126                         
1127                         int q1 = timeNum / 60;
1128                         int sec = timeNum - 60 * q1;
1129                         int q2 = q1 / 60;
1130                         int minute = q1 - 60 * q2;
1131                         int q3 = q2 / 24;
1132                         int hr = q2 - 24 * q3;
1133                         int q4 = q3 / 32;
1134                         int day = q3 - 32 * q4;
1135                         int q5 = q4 / 13;
1136                         int mon = q4 - 13 * q5;
1137                         int year = 2000 + q5;
1138                 
1139                         timestamp = toString(year) + "_" + toString(mon) + "_" + toString(day) + "_" + toString(hr) + "_" + toString(minute) + "_" + toString(sec);
1140                 }
1141                 
1142                 if (name.length() >= 9) {
1143                         region = name.substr(7, 2);
1144                 
1145                         string xyNum = name.substr(9);
1146                         unsigned int myXy = m->fromBase36(xyNum);
1147                         int x = myXy >> 12;
1148                         int y = myXy & 4095;
1149                 
1150                         xy = toString(x) + "_" + toString(y);
1151                 }
1152                 
1153                 return 0;
1154         }
1155         catch(exception& e) {
1156                 m->errorOut(e, "SffInfoCommand", "decodeName");
1157                 exit(1);
1158         }
1159 }
1160 //**********************************************************************************************************************
1161 int SffInfoCommand::printCommonHeader(ofstream& out, CommonHeader& header) {
1162         try {
1163         
1164                 out << "Common Header:\nMagic Number: " << header.magicNumber << endl;
1165                 out << "Version: " << header.version << endl;
1166                 out << "Index Offset: " << header.indexOffset << endl;
1167                 out << "Index Length: " << header.indexLength << endl;
1168                 out << "Number of Reads: " << header.numReads << endl;
1169                 out << "Header Length: " << header.headerLength << endl;
1170                 out << "Key Length: " << header.keyLength << endl;
1171                 out << "Number of Flows: " << header.numFlowsPerRead << endl;
1172                 out << "Format Code: " << header.flogramFormatCode << endl;
1173                 out << "Flow Chars: " << header.flowChars << endl;
1174                 out << "Key Sequence: " << header.keySequence << endl << endl;
1175                         
1176                 return 0;
1177         }
1178         catch(exception& e) {
1179                 m->errorOut(e, "SffInfoCommand", "printCommonHeader");
1180                 exit(1);
1181         }
1182 }
1183 //**********************************************************************************************************************
1184 int SffInfoCommand::printHeader(ofstream& out, Header& header) {
1185         try {
1186                 
1187                 out << ">" << header.name << endl;
1188                 out << "Run Prefix: " << header.timestamp << endl;
1189                 out << "Region #:  " << header.region << endl;
1190                 out << "XY Location: " << header.xy << endl << endl;
1191                 
1192                 out << "Run Name:  " << endl;
1193                 out << "Analysis Name:  " << endl;
1194                 out << "Full Path: " << endl << endl;
1195                 
1196                 out << "Read Header Len: " << header.headerLength << endl;
1197                 out << "Name Length: " << header.nameLength << endl;
1198                 out << "# of Bases: " << header.numBases << endl;
1199                 out << "Clip Qual Left: " << header.clipQualLeft << endl;
1200                 out << "Clip Qual Right: " << header.clipQualRight << endl;
1201                 out << "Clip Adap Left: " << header.clipAdapterLeft << endl;
1202                 out << "Clip Adap Right: " << header.clipAdapterRight << endl << endl;
1203                 
1204                 return 0;
1205         }
1206         catch(exception& e) {
1207                 m->errorOut(e, "SffInfoCommand", "printHeader");
1208                 exit(1);
1209         }
1210 }
1211 //**********************************************************************************************************************
1212 bool SffInfoCommand::sanityCheck(Header& header, seqRead& read) {
1213         try {
1214         bool okay = true;
1215         string message = "[WARNING]: Your sff file may be corrupted! Sequence: " + header.name + "\n";
1216         
1217         if (header.clipQualLeft > read.bases.length()) {
1218             okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1219         }
1220         if (header.clipQualRight > read.bases.length()) {
1221             okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.bases.length()) + " bases.\n";
1222         }
1223         if (header.clipQualLeft > read.qualScores.size()) {
1224             okay = false; message += "Clip Qual Left = " + toString(header.clipQualLeft) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1225         }
1226         if (header.clipQualRight > read.qualScores.size()) {
1227             okay = false; message += "Clip Qual Right = " + toString(header.clipQualRight) + ", but we only read " + toString(read.qualScores.size()) + " quality scores.\n";
1228         }
1229         
1230         if (okay == false) {
1231             m->mothurOut(message); m->mothurOutEndLine();
1232         }
1233         
1234                 return okay;
1235         }
1236         catch(exception& e) {
1237                 m->errorOut(e, "SffInfoCommand", "sanityCheck");
1238                 exit(1);
1239         }
1240 }
1241 //**********************************************************************************************************************
1242 int SffInfoCommand::printSffTxtSeqData(ofstream& out, seqRead& read, Header& header) {
1243         try {
1244                 out << "Flowgram: ";
1245                 for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << (read.flowgram[i]/(float)100) << '\t';  }
1246                 
1247                 out << endl <<  "Flow Indexes: ";
1248                 int sum = 0;
1249                 for (int i = 0; i < read.flowIndex.size(); i++) {  sum +=  read.flowIndex[i];  out << sum << '\t'; }
1250                 
1251                 //make the bases you want to clip lowercase and the bases you want to keep upper case
1252                 if(header.clipQualRight == 0){  header.clipQualRight = read.bases.length();     }
1253                 for (int i = 0; i < (header.clipQualLeft-1); i++) { read.bases[i] = tolower(read.bases[i]); }
1254                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   read.bases[i] = toupper(read.bases[i]);  }
1255                 for (int i = (header.clipQualRight-1); i < read.bases.length(); i++) {   read.bases[i] = tolower(read.bases[i]);  }
1256                 
1257                 out << endl <<  "Bases: " << read.bases << endl << "Quality Scores: ";
1258                 for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
1259         
1260                 
1261                 out << endl << endl;
1262                 
1263                 return 0;
1264         }
1265         catch(exception& e) {
1266                 m->errorOut(e, "SffInfoCommand", "printSffTxtSeqData");
1267                 exit(1);
1268         }
1269 }
1270 //**********************************************************************************************************************
1271 int SffInfoCommand::printFastaSeqData(ofstream& out, seqRead& read, Header& header) {
1272         try {
1273                 string seq = read.bases;
1274                 
1275         if (trim) {
1276                         if(header.clipQualRight < header.clipQualLeft){
1277                                 seq = "NNNN";
1278                         }
1279                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1280                                 seq = seq.substr((header.clipQualLeft-1), (header.clipQualRight-header.clipQualLeft));
1281                         }
1282                         else {
1283                                 seq = seq.substr(header.clipQualLeft-1);
1284                         }
1285                 }else{
1286                         //if you wanted the sfftxt then you already converted the bases to the right case
1287                         if (!sfftxt) {
1288                                 //make the bases you want to clip lowercase and the bases you want to keep upper case
1289                                 if(header.clipQualRight == 0){  header.clipQualRight = seq.length();    }
1290                                 for (int i = 0; i < (header.clipQualLeft-1); i++) { seq[i] = tolower(seq[i]);  }
1291                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++)  {   seq[i] = toupper(seq[i]);  }
1292                                 for (int i = (header.clipQualRight-1); i < seq.length(); i++) {   seq[i] = tolower(seq[i]);  }
1293                         }
1294                 }
1295                 
1296                 out << ">" << header.name  << " xy=" << header.xy << endl;
1297                 out << seq << endl;
1298                 
1299                 return 0;
1300         }
1301         catch(exception& e) {
1302                 m->errorOut(e, "SffInfoCommand", "printFastaSeqData");
1303                 exit(1);
1304         }
1305 }
1306
1307 //**********************************************************************************************************************
1308 int SffInfoCommand::printQualSeqData(ofstream& out, seqRead& read, Header& header) {
1309         try {
1310                 
1311                 if (trim) {
1312                         if(header.clipQualRight < header.clipQualLeft){
1313                                 out << ">" << header.name << " xy=" << header.xy << endl;
1314                                 out << "0\t0\t0\t0";
1315                         }
1316                         else if((header.clipQualRight != 0) && ((header.clipQualRight-header.clipQualLeft) >= 0)){
1317                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1318                                 for (int i = (header.clipQualLeft-1); i < (header.clipQualRight-1); i++) {   out << read.qualScores[i] << '\t'; }
1319                         }
1320                         else{
1321                                 out << ">" << header.name << " xy=" << header.xy << " length=" << (header.clipQualRight-header.clipQualLeft) << endl;
1322                                 for (int i = (header.clipQualLeft-1); i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';   }                       
1323                         }
1324                 }else{
1325                         out << ">" << header.name << " xy=" << header.xy << " length=" << read.qualScores.size() << endl;
1326                         for (int i = 0; i < read.qualScores.size(); i++) {   out << read.qualScores[i] << '\t';  }
1327                 }
1328                 
1329                 out << endl;
1330                 
1331                 return 0;
1332         }
1333         catch(exception& e) {
1334                 m->errorOut(e, "SffInfoCommand", "printQualSeqData");
1335                 exit(1);
1336         }
1337 }
1338
1339 //**********************************************************************************************************************
1340 int SffInfoCommand::printFlowSeqData(ofstream& out, seqRead& read, Header& header) {
1341         try {
1342                 if(header.clipQualRight > header.clipQualLeft){
1343                         
1344                         int rightIndex = 0;
1345                         for (int i = 0; i < header.clipQualRight; i++) {  rightIndex +=  read.flowIndex[i];     }
1346
1347                         out << header.name << ' ' << rightIndex;
1348                         for (int i = 0; i < read.flowgram.size(); i++) { out << setprecision(2) << ' ' << (read.flowgram[i]/(float)100);  }
1349                         out << endl;
1350                 }
1351                 
1352                 
1353                 return 0;
1354         }
1355         catch(exception& e) {
1356                 m->errorOut(e, "SffInfoCommand", "printFlowSeqData");
1357                 exit(1);
1358         }
1359 }
1360 //**********************************************************************************************************************
1361 int SffInfoCommand::readAccnosFile(string filename) {
1362         try {
1363                 //remove old names
1364                 seqNames.clear();
1365                 
1366                 ifstream in;
1367                 m->openInputFile(filename, in);
1368                 string name;
1369                 
1370                 while(!in.eof()){
1371                         in >> name; m->gobble(in);
1372                                                 
1373                         seqNames.insert(name);
1374                         
1375                         if (m->control_pressed) { seqNames.clear(); break; }
1376                 }
1377                 in.close();             
1378                 
1379                 return 0;
1380         }
1381         catch(exception& e) {
1382                 m->errorOut(e, "SffInfoCommand", "readAccnosFile");
1383                 exit(1);
1384         }
1385 }
1386 //**********************************************************************************************************************
1387 int SffInfoCommand::parseSffTxt() {
1388         try {
1389                 
1390                 ifstream inSFF;
1391                 m->openInputFile(sfftxtFilename, inSFF);
1392                 
1393                 if (outputDir == "") {  outputDir += m->hasPath(sfftxtFilename); }
1394                 
1395                 //output file names
1396                 ofstream outFasta, outQual, outFlow;
1397                 string outFastaFileName, outQualFileName;
1398                 string fileRoot = m->getRootName(m->getSimpleName(sfftxtFilename));
1399                 if (fileRoot.length() > 0) {
1400                         //rip off last .
1401                         fileRoot = fileRoot.substr(0, fileRoot.length()-1);
1402                         fileRoot = m->getRootName(fileRoot);
1403                 }
1404                 
1405                 string outFlowFileName = outputDir + fileRoot + getOutputFileNameTag("flow");
1406                 if (trim) {
1407                         outFastaFileName = outputDir + fileRoot + getOutputFileNameTag("fasta");
1408                         outQualFileName = outputDir + fileRoot + getOutputFileNameTag("qfile");
1409                 }else{
1410                         outFastaFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("fasta");
1411                         outQualFileName = outputDir + fileRoot + "raw." + getOutputFileNameTag("qfile");
1412                 }
1413                 
1414                 if (fasta)      { m->openOutputFile(outFastaFileName, outFasta);        outputNames.push_back(outFastaFileName); outputTypes["fasta"].push_back(outFastaFileName); }
1415                 if (qual)       { m->openOutputFile(outQualFileName, outQual);          outputNames.push_back(outQualFileName); outputTypes["qfile"].push_back(outQualFileName);  }
1416                 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);  }
1417                 
1418                 //read common header
1419                 string commonHeader = m->getline(inSFF);
1420                 string magicNumber = m->getline(inSFF); 
1421                 string version = m->getline(inSFF);
1422                 string indexOffset = m->getline(inSFF);
1423                 string indexLength = m->getline(inSFF);
1424                 int numReads = parseHeaderLineToInt(inSFF);
1425                 string headerLength = m->getline(inSFF);
1426                 string keyLength = m->getline(inSFF);
1427                 int numFlows = parseHeaderLineToInt(inSFF);
1428                 string flowgramCode = m->getline(inSFF);
1429                 string flowChars = m->getline(inSFF);
1430                 string keySequence = m->getline(inSFF);
1431                 m->gobble(inSFF);
1432                 
1433                 string seqName;
1434                 
1435                 if (flow)       {       outFlow << numFlows << endl;    }
1436                 
1437                 for(int i=0;i<numReads;i++){
1438                         
1439                         //sanity check
1440                         if (inSFF.eof()) { m->mothurOut("[ERROR]: Expected " + toString(numReads) + " but reached end of file at " + toString(i+1) + "."); m->mothurOutEndLine(); break; }
1441                         
1442                         Header header;
1443                         
1444                         //parse read header
1445                         inSFF >> seqName;
1446                         seqName = seqName.substr(1);
1447                         m->gobble(inSFF);
1448                         header.name = seqName;
1449                         
1450                         string runPrefix = parseHeaderLineToString(inSFF);              header.timestamp = runPrefix;
1451                         string regionNumber = parseHeaderLineToString(inSFF);   header.region = regionNumber;
1452                         string xyLocation = parseHeaderLineToString(inSFF);             header.xy = xyLocation;
1453                         m->gobble(inSFF);
1454                                 
1455                         string runName = parseHeaderLineToString(inSFF);
1456                         string analysisName = parseHeaderLineToString(inSFF);
1457                         string fullPath = parseHeaderLineToString(inSFF);
1458                         m->gobble(inSFF);
1459                         
1460                         string readHeaderLen = parseHeaderLineToString(inSFF);  convert(readHeaderLen, header.headerLength);
1461                         string nameLength = parseHeaderLineToString(inSFF);             convert(nameLength, header.nameLength);
1462                         int numBases = parseHeaderLineToInt(inSFF);                             header.numBases = numBases;
1463                         string clipQualLeft = parseHeaderLineToString(inSFF);   convert(clipQualLeft, header.clipQualLeft);
1464                         int clipQualRight = parseHeaderLineToInt(inSFF);                header.clipQualRight = clipQualRight;
1465                         string clipAdapLeft = parseHeaderLineToString(inSFF);   convert(clipAdapLeft, header.clipAdapterLeft);
1466                         string clipAdapRight = parseHeaderLineToString(inSFF);  convert(clipAdapRight, header.clipAdapterRight);
1467                         m->gobble(inSFF);
1468                                 
1469                         seqRead read;
1470                         
1471                         //parse read
1472                         vector<unsigned short> flowVector = parseHeaderLineToFloatVector(inSFF, numFlows);      read.flowgram = flowVector;
1473                         vector<unsigned int> flowIndices = parseHeaderLineToIntVector(inSFF, numBases); 
1474                         
1475                         //adjust for print
1476                         vector<unsigned int> flowIndicesAdjusted; flowIndicesAdjusted.push_back(flowIndices[0]);
1477                         for (int j = 1; j < flowIndices.size(); j++) {   flowIndicesAdjusted.push_back(flowIndices[j] - flowIndices[j-1]);   }
1478                         read.flowIndex = flowIndicesAdjusted;
1479                         
1480                         string bases = parseHeaderLineToString(inSFF);                                                                          read.bases = bases;
1481                         vector<unsigned int> qualityScores = parseHeaderLineToIntVector(inSFF, numBases);       read.qualScores = qualityScores;
1482                         m->gobble(inSFF);
1483                                         
1484                         //if you have provided an accosfile and this seq is not in it, then dont print
1485                         bool print = true;
1486                         if (seqNames.size() != 0) {   if (seqNames.count(header.name) == 0) { print = false; }  }
1487                         
1488                         //print 
1489                         if (print) {
1490                                 if (fasta)      {       printFastaSeqData(outFasta, read, header);      }
1491                                 if (qual)       {       printQualSeqData(outQual, read, header);        }
1492                                 if (flow)       {       printFlowSeqData(outFlow, read, header);        }
1493                         }
1494                         
1495                         //report progress
1496                         if((i+1) % 10000 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine();             }
1497                         
1498                         if (m->control_pressed) {  break;  }
1499                 }
1500                 
1501                 //report progress
1502                 if (!m->control_pressed) {   if((numReads) % 10000 != 0){       m->mothurOut(toString(numReads)); m->mothurOutEndLine();                }  }
1503                 
1504                 inSFF.close();
1505                 
1506                 if (fasta)      {  outFasta.close();    }
1507                 if (qual)       {  outQual.close();             }
1508                 if (flow)       {  outFlow.close();             }
1509                 
1510                 return 0;
1511         }
1512         catch(exception& e) {
1513                 m->errorOut(e, "SffInfoCommand", "parseSffTxt");
1514                 exit(1);
1515         }
1516 }
1517 //**********************************************************************************************************************
1518
1519 int SffInfoCommand::parseHeaderLineToInt(ifstream& file){
1520         try {
1521                 int number;
1522                 
1523                 while (!file.eof())     {
1524                         
1525                         char c = file.get(); 
1526                         if (c == ':'){
1527                                 file >> number;
1528                                 break;
1529                         }
1530                         
1531                 }
1532                 m->gobble(file);
1533                 return number;
1534         }
1535         catch(exception& e) {
1536                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToInt");
1537                 exit(1);
1538         }
1539         
1540 }
1541
1542 //**********************************************************************************************************************
1543
1544 string SffInfoCommand::parseHeaderLineToString(ifstream& file){
1545         try {
1546                 string text;
1547                 
1548                 while (!file.eof())     {
1549                         char c = file.get(); 
1550                         
1551                         if (c == ':'){
1552                                 //m->gobble(file);
1553                                 //text = m->getline(file);      
1554                                 file >> text;
1555                                 break;
1556                         }
1557                 }
1558                 m->gobble(file);
1559                 
1560                 return text;
1561         }
1562         catch(exception& e) {
1563                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToString");
1564                 exit(1);
1565         }
1566 }
1567
1568 //**********************************************************************************************************************
1569
1570 vector<unsigned short> SffInfoCommand::parseHeaderLineToFloatVector(ifstream& file, int length){
1571         try {
1572                 vector<unsigned short> floatVector(length);
1573                 
1574                 while (!file.eof())     {
1575                         char c = file.get(); 
1576                         if (c == ':'){
1577                                 float temp;
1578                                 for(int i=0;i<length;i++){
1579                                         file >> temp;
1580                                         floatVector[i] = temp * 100;
1581                                 }
1582                                 break;
1583                         }
1584                 }
1585                 m->gobble(file);        
1586                 return floatVector;
1587         }
1588         catch(exception& e) {
1589                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToFloatVector");
1590                 exit(1);
1591         }
1592 }
1593
1594 //**********************************************************************************************************************
1595
1596 vector<unsigned int> SffInfoCommand::parseHeaderLineToIntVector(ifstream& file, int length){
1597         try {
1598                 vector<unsigned int> intVector(length);
1599                 
1600                 while (!file.eof())     {
1601                         char c = file.get(); 
1602                         if (c == ':'){
1603                                 for(int i=0;i<length;i++){
1604                                         file >> intVector[i];
1605                                 }
1606                                 break;
1607                         }
1608                 }
1609                 m->gobble(file);        
1610                 return intVector;
1611         }
1612         catch(exception& e) {
1613                 m->errorOut(e, "SffInfoCommand", "parseHeaderLineToIntVector");
1614                 exit(1);
1615         }
1616 }
1617 //***************************************************************************************************************
1618
1619 bool SffInfoCommand::readOligos(string oligoFile){
1620         try {
1621         filehandles.clear();
1622         numSplitReads.clear();
1623         filehandlesHeaders.clear();
1624         
1625                 ifstream inOligos;
1626                 m->openInputFile(oligoFile, inOligos);
1627                 
1628                 string type, oligo, group;
1629         
1630                 int indexPrimer = 0;
1631                 int indexBarcode = 0;
1632                 
1633                 while(!inOligos.eof()){
1634             
1635                         inOligos >> type; 
1636             
1637                         if(type[0] == '#'){
1638                                 while (!inOligos.eof()) {       char c = inOligos.get();  if (c == 10 || c == 13){      break;  }       } // get rest of line if there's any crap there
1639                                 m->gobble(inOligos);
1640                         }
1641                         else{
1642                                 m->gobble(inOligos);
1643                                 //make type case insensitive
1644                                 for(int i=0;i<type.length();i++){       type[i] = toupper(type[i]);  }
1645                                 
1646                                 inOligos >> oligo;
1647                                 
1648                                 for(int i=0;i<oligo.length();i++){
1649                                         oligo[i] = toupper(oligo[i]);
1650                                         if(oligo[i] == 'U')     {       oligo[i] = 'T'; }
1651                                 }
1652                                 
1653                                 if(type == "FORWARD"){
1654                                         group = "";
1655                                         
1656                                         // get rest of line in case there is a primer name
1657                                         while (!inOligos.eof()) {       
1658                                                 char c = inOligos.get(); 
1659                                                 if (c == 10 || c == 13){        break;  }
1660                                                 else if (c == 32 || c == 9){;} //space or tab
1661                                                 else {  group += c;  }
1662                                         } 
1663                                         
1664                                         //check for repeat barcodes
1665                                         map<string, int>::iterator itPrime = primers.find(oligo);
1666                                         if (itPrime != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
1667                                         
1668                                         primers[oligo]=indexPrimer; indexPrimer++;              
1669                                         primerNameVector.push_back(group);
1670                                 }else if(type == "REVERSE"){
1671                                         //Sequence oligoRC("reverse", oligo);
1672                                         //oligoRC.reverseComplement();
1673                     string oligoRC = reverseOligo(oligo);
1674                                         revPrimer.push_back(oligoRC);
1675                                 }
1676                                 else if(type == "BARCODE"){
1677                                         inOligos >> group;
1678                     
1679                     //barcode lines can look like   BARCODE   atgcatgc   groupName  - for 454 seqs
1680                     //or                            BARCODE   atgcatgc   atgcatgc    groupName  - for illumina data that has forward and reverse info
1681                                         string temp = "";
1682                     while (!inOligos.eof())     {       
1683                                                 char c = inOligos.get(); 
1684                                                 if (c == 10 || c == 13){        break;  }
1685                                                 else if (c == 32 || c == 9){;} //space or tab
1686                                                 else {  temp += c;  }
1687                                         } 
1688                                         
1689                     //then this is illumina data with 4 columns
1690                     if (temp != "") {  
1691                         string reverseBarcode = reverseOligo(group); //reverse barcode
1692                         group = temp;
1693                         
1694                         //check for repeat barcodes
1695                         map<string, int>::iterator itBar = rbarcodes.find(reverseBarcode);
1696                         if (itBar != rbarcodes.end()) { m->mothurOut("barcode " + reverseBarcode + " is in your oligos file already."); m->mothurOutEndLine();  }
1697                                                 
1698                         rbarcodes[reverseBarcode]=indexBarcode; 
1699                     }
1700                     
1701                                         //check for repeat barcodes
1702                                         map<string, int>::iterator itBar = barcodes.find(oligo);
1703                                         if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine();  }
1704                     
1705                                         barcodes[oligo]=indexBarcode; indexBarcode++;
1706                                         barcodeNameVector.push_back(group);
1707
1708                                 }else if(type == "LINKER"){
1709                                         linker.push_back(oligo);
1710                                 }else if(type == "SPACER"){
1711                                         spacer.push_back(oligo);
1712                                 }
1713                                 else{   m->mothurOut("[WARNING]: " + type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine(); }
1714                         }
1715                         m->gobble(inOligos);
1716                 }       
1717                 inOligos.close();
1718                 
1719                 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ split = 1;      }
1720                 
1721                 //add in potential combos
1722                 if(barcodeNameVector.size() == 0){
1723                         barcodes[""] = 0;
1724                         barcodeNameVector.push_back("");                        
1725                 }
1726                 
1727                 if(primerNameVector.size() == 0){
1728                         primers[""] = 0;
1729                         primerNameVector.push_back("");                 
1730                 }
1731                 
1732                 filehandles.resize(barcodeNameVector.size());
1733                 for(int i=0;i<filehandles.size();i++){
1734                         filehandles[i].assign(primerNameVector.size(), "");
1735                 }
1736                         
1737                 if(split > 1){
1738                         set<string> uniqueNames; //used to cleanup outputFileNames
1739                         for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
1740                                 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
1741                                         
1742                                         string primerName = primerNameVector[itPrimer->second];
1743                                         string barcodeName = barcodeNameVector[itBar->second];
1744                                         
1745                                         string comboGroupName = "";
1746                                         string fastaFileName = "";
1747                                         string qualFileName = "";
1748                                         string nameFileName = "";
1749                                         
1750                                         if(primerName == ""){
1751                                                 comboGroupName = barcodeNameVector[itBar->second];
1752                                         }
1753                                         else{
1754                                                 if(barcodeName == ""){
1755                                                         comboGroupName = primerNameVector[itPrimer->second];
1756                                                 }
1757                                                 else{
1758                                                         comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
1759                                                 }
1760                                         }
1761                                         
1762                                         ofstream temp;
1763                                         string thisFilename = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + comboGroupName + "." + getOutputFileNameTag("sff");
1764                                         if (uniqueNames.count(thisFilename) == 0) {
1765                                                 outputNames.push_back(thisFilename);
1766                                                 outputTypes["sff"].push_back(thisFilename);
1767                                                 uniqueNames.insert(thisFilename);
1768                                         }
1769                                         
1770                                         filehandles[itBar->second][itPrimer->second] = thisFilename;
1771                                         m->openOutputFile(thisFilename, temp);          temp.close();
1772                                 }
1773                         }
1774                 }
1775                 numFPrimers = primers.size();
1776         numLinkers = linker.size();
1777         numSpacers = spacer.size();
1778                 noMatchFile = outputDir + m->getRootName(m->getSimpleName(currentFileName)) + "scrap." + getOutputFileNameTag("sff");
1779         m->mothurRemove(noMatchFile);
1780         
1781                 bool allBlank = true;
1782                 for (int i = 0; i < barcodeNameVector.size(); i++) {
1783                         if (barcodeNameVector[i] != "") {
1784                                 allBlank = false;
1785                                 break;
1786                         }
1787                 }
1788                 for (int i = 0; i < primerNameVector.size(); i++) {
1789                         if (primerNameVector[i] != "") {
1790                                 allBlank = false;
1791                                 break;
1792                         }
1793                 }
1794                 
1795         filehandlesHeaders.resize(filehandles.size());
1796         numSplitReads.resize(filehandles.size());
1797         for (int i = 0; i < filehandles.size(); i++) { 
1798             numSplitReads[i].resize(filehandles[i].size(), 0); 
1799             for (int j = 0; j < filehandles[i].size(); j++) {
1800                 filehandlesHeaders[i].push_back(filehandles[i][j]+"headers");
1801             }
1802         }
1803                              
1804                 if (allBlank) {
1805                         m->mothurOut("[WARNING]: your oligos file does not contain any group names.  mothur will not create a split the sff file."); m->mothurOutEndLine();
1806                         split = 1;
1807                         return false;
1808                 }
1809                 
1810                 return true;
1811                 
1812         }
1813         catch(exception& e) {
1814                 m->errorOut(e, "SffInfoCommand", "readOligos");
1815                 exit(1);
1816         }
1817 }
1818 //********************************************************************/
1819 string SffInfoCommand::reverseOligo(string oligo){
1820         try {
1821         string reverse = "";
1822         
1823         for(int i=oligo.length()-1;i>=0;i--){
1824             
1825             if(oligo[i] == 'A')         {       reverse += 'T'; }
1826             else if(oligo[i] == 'T'){   reverse += 'A'; }
1827             else if(oligo[i] == 'U'){   reverse += 'A'; }
1828             
1829             else if(oligo[i] == 'G'){   reverse += 'C'; }
1830             else if(oligo[i] == 'C'){   reverse += 'G'; }
1831             
1832             else if(oligo[i] == 'R'){   reverse += 'Y'; }
1833             else if(oligo[i] == 'Y'){   reverse += 'R'; }
1834             
1835             else if(oligo[i] == 'M'){   reverse += 'K'; }
1836             else if(oligo[i] == 'K'){   reverse += 'M'; }
1837             
1838             else if(oligo[i] == 'W'){   reverse += 'W'; }
1839             else if(oligo[i] == 'S'){   reverse += 'S'; }
1840             
1841             else if(oligo[i] == 'B'){   reverse += 'V'; }
1842             else if(oligo[i] == 'V'){   reverse += 'B'; }
1843             
1844             else if(oligo[i] == 'D'){   reverse += 'H'; }
1845             else if(oligo[i] == 'H'){   reverse += 'D'; }
1846             
1847             else                                                {       reverse += 'N'; }
1848         }
1849         
1850         
1851         return reverse;
1852     }
1853         catch(exception& e) {
1854                 m->errorOut(e, "SffInfoCommand", "reverseOligo");
1855                 exit(1);
1856         }
1857 }
1858
1859 //**********************************************************************************************************************
1860
1861
1862                                 
1863