]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
changes while testing
[mothur.git] / getseqscommand.cpp
1 /*
2  *  getseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 7/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "getseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13 #include "counttable.h"
14
15 //**********************************************************************************************************************
16 vector<string> GetSeqsCommand::setParameters(){ 
17         try {
18                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none","fasta",false,false,true); parameters.push_back(pfasta);
19         CommandParameter pname("name", "InputTypes", "", "", "NameCount", "FNGLT", "none","name",false,false,true); parameters.push_back(pname);
20         CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "FNGLT", "none","count",false,false,true); parameters.push_back(pcount);
21                 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "FNGLT", "none","group",false,false,true); parameters.push_back(pgroup);
22                 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none","list",false,false,true); parameters.push_back(plist);
23                 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none","taxonomy",false,false,true); parameters.push_back(ptaxonomy);
24                 CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "FNGLT", "none","alignreport",false,false); parameters.push_back(palignreport);
25                 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none","qfile",false,false); parameters.push_back(pqfile);
26                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(paccnos);
27                 CommandParameter pdups("dups", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pdups);
28                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
29                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
30                 CommandParameter paccnos2("accnos2", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos2);
31
32                 vector<string> myArray;
33                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
34                 return myArray;
35         }
36         catch(exception& e) {
37                 m->errorOut(e, "GetSeqsCommand", "setParameters");
38                 exit(1);
39         }
40 }
41 //**********************************************************************************************************************
42 string GetSeqsCommand::getHelpString(){ 
43         try {
44                 string helpString = "";
45                 helpString += "The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, count, list, taxonomy, quality or alignreport file.\n";
46                 helpString += "It outputs a file containing only the sequences in the .accnos file.\n";
47                 helpString += "The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, qfile, alignreport and dups.  You must provide accnos unless you have a valid current accnos file, and at least one of the other parameters.\n";
48                 helpString += "The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=false. \n";
49                 helpString += "The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n";
50                 helpString += "Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n";
51                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
52                 return helpString;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "GetSeqsCommand", "getHelpString");
56                 exit(1);
57         }
58 }
59
60 //**********************************************************************************************************************
61 GetSeqsCommand::GetSeqsCommand(){       
62         try {
63                 abort = true; calledHelp = true;
64                 setParameters();
65                 vector<string> tempOutNames;
66                 outputTypes["fasta"] = tempOutNames;
67                 outputTypes["taxonomy"] = tempOutNames;
68                 outputTypes["name"] = tempOutNames;
69                 outputTypes["group"] = tempOutNames;
70                 outputTypes["alignreport"] = tempOutNames;
71                 outputTypes["list"] = tempOutNames;
72                 outputTypes["qfile"] = tempOutNames;
73         outputTypes["count"] = tempOutNames;
74                 outputTypes["accnosreport"] = tempOutNames;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
78                 exit(1);
79         }
80 }
81 //**********************************************************************************************************************
82 string GetSeqsCommand::getOutputPattern(string type) {
83     try {
84         string pattern = "";
85         
86         if (type == "fasta")            {   pattern = "[filename],pick,[extension]";    }
87         else if (type == "taxonomy")    {   pattern = "[filename],pick,[extension]";    }
88         else if (type == "name")        {   pattern = "[filename],pick,[extension]";    }
89         else if (type == "group")       {   pattern = "[filename],pick,[extension]";    }
90         else if (type == "count")       {   pattern = "[filename],pick,[extension]";    }
91         else if (type == "list")        {   pattern = "[filename],pick,[extension]";    }
92         else if (type == "qfile")       {   pattern = "[filename],pick,[extension]";    }
93         else if (type == "accnosreport")      {   pattern = "[filename],pick.accnos.report";    }
94         else if (type == "alignreport")      {   pattern = "[filename],pick.align.report";    }
95         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
96         
97         return pattern;
98     }
99     catch(exception& e) {
100         m->errorOut(e, "GetSeqsCommand", "getOutputPattern");
101         exit(1);
102     }
103 }
104 //**********************************************************************************************************************
105 GetSeqsCommand::GetSeqsCommand(string option)  {
106         try {
107                 abort = false; calledHelp = false;   
108                                 
109                 //allow user to run help
110                 if(option == "help") { help(); abort = true; calledHelp = true; }
111                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
112                 
113                 else {
114                         vector<string> myArray = setParameters();
115                         
116                         OptionParser parser(option);
117                         map<string,string> parameters = parser.getParameters();
118                         
119                         ValidParameters validParameter;
120                         map<string,string>::iterator it;
121                         
122                         //check to make sure all parameters are valid for command
123                         for (it = parameters.begin(); it != parameters.end(); it++) { 
124                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
125                         }
126                         
127                         //initialize outputTypes
128                         vector<string> tempOutNames;
129                         outputTypes["fasta"] = tempOutNames;
130                         outputTypes["taxonomy"] = tempOutNames;
131                         outputTypes["name"] = tempOutNames;
132                         outputTypes["group"] = tempOutNames;
133                         outputTypes["alignreport"] = tempOutNames;
134                         outputTypes["list"] = tempOutNames;
135                         outputTypes["qfile"] = tempOutNames;
136                         outputTypes["accnosreport"] = tempOutNames;
137             outputTypes["count"] = tempOutNames;
138                         
139                         //if the user changes the output directory command factory will send this info to us in the output parameter 
140                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
141                         
142                         //if the user changes the input directory command factory will send this info to us in the output parameter 
143                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
144                         if (inputDir == "not found"){   inputDir = "";          }
145                         else {
146                                 string path;
147                                 it = parameters.find("alignreport");
148                                 //user has given a template file
149                                 if(it != parameters.end()){ 
150                                         path = m->hasPath(it->second);
151                                         //if the user has not given a path then, add inputdir. else leave path alone.
152                                         if (path == "") {       parameters["alignreport"] = inputDir + it->second;              }
153                                 }
154                                 
155                                 it = parameters.find("fasta");
156                                 //user has given a template file
157                                 if(it != parameters.end()){ 
158                                         path = m->hasPath(it->second);
159                                         //if the user has not given a path then, add inputdir. else leave path alone.
160                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
161                                 }
162                                 
163                                 it = parameters.find("accnos");
164                                 //user has given a template file
165                                 if(it != parameters.end()){ 
166                                         path = m->hasPath(it->second);
167                                         //if the user has not given a path then, add inputdir. else leave path alone.
168                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
169                                 }
170                                 
171                                 it = parameters.find("accnos2");
172                                 //user has given a template file
173                                 if(it != parameters.end()){ 
174                                         path = m->hasPath(it->second);
175                                         //if the user has not given a path then, add inputdir. else leave path alone.
176                                         if (path == "") {       parameters["accnos2"] = inputDir + it->second;          }
177                                 }
178                                 
179                                 it = parameters.find("list");
180                                 //user has given a template file
181                                 if(it != parameters.end()){ 
182                                         path = m->hasPath(it->second);
183                                         //if the user has not given a path then, add inputdir. else leave path alone.
184                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
185                                 }
186                                 
187                                 it = parameters.find("name");
188                                 //user has given a template file
189                                 if(it != parameters.end()){ 
190                                         path = m->hasPath(it->second);
191                                         //if the user has not given a path then, add inputdir. else leave path alone.
192                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
193                                 }
194                                 
195                                 it = parameters.find("group");
196                                 //user has given a template file
197                                 if(it != parameters.end()){ 
198                                         path = m->hasPath(it->second);
199                                         //if the user has not given a path then, add inputdir. else leave path alone.
200                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
201                                 }
202                                 
203                                 it = parameters.find("taxonomy");
204                                 //user has given a template file
205                                 if(it != parameters.end()){ 
206                                         path = m->hasPath(it->second);
207                                         //if the user has not given a path then, add inputdir. else leave path alone.
208                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
209                                 }
210                                 
211                                 it = parameters.find("qfile");
212                                 //user has given a template file
213                                 if(it != parameters.end()){ 
214                                         path = m->hasPath(it->second);
215                                         //if the user has not given a path then, add inputdir. else leave path alone.
216                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
217                                 }
218                 
219                 it = parameters.find("count");
220                                 //user has given a template file
221                                 if(it != parameters.end()){ 
222                                         path = m->hasPath(it->second);
223                                         //if the user has not given a path then, add inputdir. else leave path alone.
224                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
225                                 }
226                         }
227
228                         
229                         //check for required parameters
230                         accnosfile = validParameter.validFile(parameters, "accnos", true);
231                         if (accnosfile == "not open") { abort = true; }
232                         else if (accnosfile == "not found") {  
233                                 accnosfile = m->getAccnosFile(); 
234                                 if (accnosfile != "") {  m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
235                                 else { 
236                                         m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine(); 
237                                         abort = true;
238                                 } 
239                         }else { m->setAccnosFile(accnosfile); } 
240                         
241                         if (accnosfile2 == "not found") { accnosfile2 = ""; }
242                         
243                         fastafile = validParameter.validFile(parameters, "fasta", true);
244                         if (fastafile == "not open") { fastafile = ""; abort = true; }
245                         else if (fastafile == "not found") {  fastafile = "";  }
246                         else { m->setFastaFile(fastafile); }
247                         
248                         namefile = validParameter.validFile(parameters, "name", true);
249                         if (namefile == "not open") { namefile = ""; abort = true; }
250                         else if (namefile == "not found") {  namefile = "";  }  
251                         else { m->setNameFile(namefile); }
252                         
253                         groupfile = validParameter.validFile(parameters, "group", true);
254                         if (groupfile == "not open") { abort = true; }
255                         else if (groupfile == "not found") {  groupfile = "";  }        
256                         else { m->setGroupFile(groupfile); }
257                         
258                         alignfile = validParameter.validFile(parameters, "alignreport", true);
259                         if (alignfile == "not open") { abort = true; }
260                         else if (alignfile == "not found") {  alignfile = "";  }
261                         
262                         listfile = validParameter.validFile(parameters, "list", true);
263                         if (listfile == "not open") { abort = true; }
264                         else if (listfile == "not found") {  listfile = "";  }
265                         else { m->setListFile(listfile); }
266                         
267                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
268                         if (taxfile == "not open") { taxfile = ""; abort = true; }
269                         else if (taxfile == "not found") {  taxfile = "";  }
270                         else { m->setTaxonomyFile(taxfile); }
271                         
272                         qualfile = validParameter.validFile(parameters, "qfile", true);
273                         if (qualfile == "not open") { abort = true; }
274                         else if (qualfile == "not found") {  qualfile = "";  }
275                         else { m->setQualFile(qualfile); }
276                         
277                         accnosfile2 = validParameter.validFile(parameters, "accnos2", true);
278                         if (accnosfile2 == "not open") { abort = true; }
279                         else if (accnosfile2 == "not found") {  accnosfile2 = "";  }
280                         
281             countfile = validParameter.validFile(parameters, "count", true);
282             if (countfile == "not open") { countfile = ""; abort = true; }
283             else if (countfile == "not found") { countfile = "";  }     
284             else { m->setCountTableFile(countfile); }
285             
286             if ((namefile != "") && (countfile != "")) {
287                 m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
288             }
289             
290             if ((groupfile != "") && (countfile != "")) {
291                 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
292             }
293
294                         
295                         string usedDups = "true";
296                         string temp = validParameter.validFile(parameters, "dups", false);      if (temp == "not found") { temp = "true"; usedDups = ""; }
297                         dups = m->isTrue(temp);
298                         
299                         if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "") && (accnosfile2 == "") && (countfile == ""))  { m->mothurOut("You must provide one of the following: fasta, name, group, count, alignreport, taxonomy, quality or listfile."); m->mothurOutEndLine(); abort = true; }
300             
301             if (countfile == "") {
302                 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
303                     vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
304                     parser.getNameFile(files);
305                 }
306             }
307                 }
308
309         }
310         catch(exception& e) {
311                 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
312                 exit(1);
313         }
314 }
315 //**********************************************************************************************************************
316
317 int GetSeqsCommand::execute(){
318         try {
319                 
320                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
321                 
322                 //get names you want to keep
323                 names = m->readAccnos(accnosfile);
324                 
325                 if (m->control_pressed) { return 0; }
326         
327         if (countfile != "") {
328             if ((fastafile != "") || (listfile != "") || (taxfile != "")) { 
329                 m->mothurOut("\n[NOTE]: The count file should contain only unique names, so mothur assumes your fasta, list and taxonomy files also contain only uniques.\n\n");
330             }
331         }
332                 
333                 //read through the correct file and output lines you want to keep
334                 if (namefile != "")                     {               readName();                     }
335                 if (fastafile != "")            {               readFasta();            }
336                 if (groupfile != "")            {               readGroup();            }
337         if (countfile != "")            {               readCount();            }
338                 if (alignfile != "")            {               readAlign();            }
339                 if (listfile != "")                     {               readList();                     }
340                 if (taxfile != "")                      {               readTax();                      }
341                 if (qualfile != "")                     {               readQual();                     }
342                 if (accnosfile2 != "")          {               compareAccnos();        }
343         
344         if (m->debug) { runSanityCheck(); }
345                 
346                 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   m->mothurRemove(outputNames[i]);  } return 0; }
347                 
348                 
349                 if (outputNames.size() != 0) {
350                         m->mothurOutEndLine();
351                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
352                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
353                         m->mothurOutEndLine();
354                         
355                         //set fasta file as new current fastafile
356                         string current = "";
357                         itTypes = outputTypes.find("fasta");
358                         if (itTypes != outputTypes.end()) {
359                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
360                         }
361                         
362                         itTypes = outputTypes.find("name");
363                         if (itTypes != outputTypes.end()) {
364                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
365                         }
366                         
367                         itTypes = outputTypes.find("group");
368                         if (itTypes != outputTypes.end()) {
369                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
370                         }
371                         
372                         itTypes = outputTypes.find("list");
373                         if (itTypes != outputTypes.end()) {
374                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
375                         }
376                         
377                         itTypes = outputTypes.find("taxonomy");
378                         if (itTypes != outputTypes.end()) {
379                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
380                         }
381                         
382                         itTypes = outputTypes.find("qfile");
383                         if (itTypes != outputTypes.end()) {
384                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
385                         }
386                         
387             itTypes = outputTypes.find("count");
388                         if (itTypes != outputTypes.end()) {
389                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
390                         }
391                 }
392                 
393                 return 0;               
394         }
395
396         catch(exception& e) {
397                 m->errorOut(e, "GetSeqsCommand", "execute");
398                 exit(1);
399         }
400 }
401
402 //**********************************************************************************************************************
403 int GetSeqsCommand::readFasta(){
404         try {
405                 string thisOutputDir = outputDir;
406                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
407                 map<string, string> variables; 
408         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
409         variables["[extension]"] = m->getExtension(fastafile);
410                 string outputFileName = getOutputFileName("fasta", variables);
411                 ofstream out;
412                 m->openOutputFile(outputFileName, out);
413                 
414                 
415                 ifstream in;
416                 m->openInputFile(fastafile, in);
417                 string name;
418                 
419                 bool wroteSomething = false;
420                 int selectedCount = 0;
421         
422         if (m->debug) { set<string> temp; sanity["fasta"] = temp; }
423                 
424                 while(!in.eof()){
425                 
426                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
427                         
428                         Sequence currSeq(in);
429                         name = currSeq.getName();
430             
431             if (!dups) {//adjust name if needed
432                 map<string, string>::iterator it = uniqueMap.find(name);
433                 if (it != uniqueMap.end()) { name = it->second; }
434             }
435                         
436                         if (name != "") {
437                                 //if this name is in the accnos file
438                                 if (names.count(name) != 0) {
439                                         wroteSomething = true;
440                                         
441                                         currSeq.printSequence(out);
442                                         selectedCount++;
443                     
444                     if (m->debug) { sanity["fasta"].insert(name); }
445                                 }
446                         }
447                         m->gobble(in);
448                 }
449                 in.close();     
450                 out.close();
451                 
452                 
453                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
454                 outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName); 
455                 
456                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
457                 
458                 return 0;
459
460         }
461         catch(exception& e) {
462                 m->errorOut(e, "GetSeqsCommand", "readFasta");
463                 exit(1);
464         }
465 }
466 //**********************************************************************************************************************
467 int GetSeqsCommand::readQual(){
468         try {
469                 string thisOutputDir = outputDir;
470                 if (outputDir == "") {  thisOutputDir += m->hasPath(qualfile);  }
471                 map<string, string> variables; 
472         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(qualfile));
473         variables["[extension]"] = m->getExtension(qualfile);
474                 string outputFileName = getOutputFileName("qfile", variables);
475                 ofstream out;
476                 m->openOutputFile(outputFileName, out);
477                 
478                 
479                 ifstream in;
480                 m->openInputFile(qualfile, in);
481                 string name;
482                 
483                 bool wroteSomething = false;
484                 int selectedCount = 0;
485                 
486         if (m->debug) { set<string> temp; sanity["qual"] = temp; }
487                 
488                 while(!in.eof()){       
489                         string saveName = "";
490                         string name = "";
491                         string scores = "";
492                         
493                         in >> name;
494             
495             if (!dups) {//adjust name if needed
496                 map<string, string>::iterator it = uniqueMap.find(name);
497                 if (it != uniqueMap.end()) { name = it->second; }
498             }
499                                 
500                         if (name.length() != 0) { 
501                                 saveName = name.substr(1);
502                                 while (!in.eof())       {       
503                                         char c = in.get(); 
504                                         if (c == 10 || c == 13 || c == -1){     break;  }
505                                         else { name += c; }     
506                                 } 
507                                 m->gobble(in);
508                         }
509                         
510                         while(in){
511                                 char letter= in.get();
512                                 if(letter == '>'){      in.putback(letter);     break;  }
513                                 else{ scores += letter; }
514                         }
515                         
516                         m->gobble(in);
517                         
518                         if (names.count(saveName) != 0) {
519                                 wroteSomething = true;
520                                                 
521                                 out << name << endl << scores;
522                                 selectedCount++;
523                 if (m->debug) { sanity["qual"].insert(name); }
524                         }
525                         
526                         m->gobble(in);
527                 }
528                 in.close();
529                 out.close();
530                 
531                 
532                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
533                 outputNames.push_back(outputFileName);  outputTypes["qfile"].push_back(outputFileName); 
534                 
535                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your quality file."); m->mothurOutEndLine();
536
537                 
538                 return 0;
539                 
540         }
541         catch(exception& e) {
542                 m->errorOut(e, "GetSeqsCommand", "readQual");
543                 exit(1);
544         }
545 }
546 //**********************************************************************************************************************
547 int GetSeqsCommand::readCount(){
548         try {
549                 string thisOutputDir = outputDir;
550                 if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
551                 map<string, string> variables; 
552                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
553         variables["[extension]"] = m->getExtension(countfile);
554                 string outputFileName = getOutputFileName("count", variables);
555                 
556                 ofstream out;
557                 m->openOutputFile(outputFileName, out);
558                 
559                 ifstream in;
560                 m->openInputFile(countfile, in);
561                 
562                 bool wroteSomething = false;
563                 int selectedCount = 0;
564                 
565         string headers = m->getline(in); m->gobble(in);
566         out << headers << endl;
567         
568         string name, rest; int thisTotal;
569         while (!in.eof()) {
570             
571             if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
572             
573             in >> name; m->gobble(in); 
574             in >> thisTotal; m->gobble(in);
575             rest = m->getline(in); m->gobble(in);
576             if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + rest + "\n"); }
577             
578             if (names.count(name) != 0) {
579                 out << name << '\t' << thisTotal << '\t' << rest << endl;
580                 wroteSomething = true;
581                 selectedCount+= thisTotal;
582             }
583         }
584         in.close();
585                 out.close();
586         
587         //check for groups that have been eliminated
588         CountTable ct;
589         if (ct.testGroups(outputFileName)) {
590             ct.readTable(outputFileName, true);
591             ct.printTable(outputFileName);
592         }
593                 
594                 if (wroteSomething == false) {  m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
595                 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
596                 
597                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your count file."); m->mothurOutEndLine();
598         
599                 return 0;
600         }
601         catch(exception& e) {
602                 m->errorOut(e, "GetSeqsCommand", "readCount");
603                 exit(1);
604         }
605 }
606
607 //**********************************************************************************************************************
608 int GetSeqsCommand::readList(){
609         try {
610                 string thisOutputDir = outputDir;
611                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
612         map<string, string> variables; 
613                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
614         variables["[extension]"] = m->getExtension(listfile);
615                 string outputFileName = getOutputFileName("list", variables);
616                 ofstream out;
617                 m->openOutputFile(outputFileName, out);
618                 
619                 ifstream in;
620                 m->openInputFile(listfile, in);
621                 
622                 bool wroteSomething = false;
623                 int selectedCount = 0;
624         
625         if (m->debug) { set<string> temp; sanity["list"] = temp; }
626                 
627                 while(!in.eof()){
628                         
629                         selectedCount = 0;
630                         
631                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
632
633                         //read in list vector
634                         ListVector list(in);
635                         
636                         //make a new list vector
637                         ListVector newList;
638                         newList.setLabel(list.getLabel());
639                         
640                         //for each bin
641                         for (int i = 0; i < list.getNumBins(); i++) {
642                         
643                                 //parse out names that are in accnos file
644                                 string binnames = list.get(i);
645                 vector<string> bnames;
646                 m->splitAtComma(binnames, bnames);
647                                 
648                                 string newNames = "";
649                 for (int i = 0; i < bnames.size(); i++) {
650                                         string name = bnames[i];
651                                         //if that name is in the .accnos file, add it
652                                         if (names.count(name) != 0) {  newNames += name + ",";  selectedCount++; if (m->debug) { sanity["list"].insert(name); } }
653                                 }
654                         
655                                 //if there are names in this bin add to new list
656                                 if (newNames != "") { 
657                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
658                                         newList.push_back(newNames);    
659                                 }
660                         }
661                                 
662                         //print new listvector
663                         if (newList.getNumBins() != 0) {
664                                 wroteSomething = true;
665                                 newList.print(out);
666                         }
667                         
668                         m->gobble(in);
669                 }
670                 in.close();     
671                 out.close();
672                 
673                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
674                 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
675                 
676                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
677                 
678                 return 0;
679
680         }
681         catch(exception& e) {
682                 m->errorOut(e, "GetSeqsCommand", "readList");
683                 exit(1);
684         }
685 }
686 //**********************************************************************************************************************
687 int GetSeqsCommand::readName(){
688         try {
689                 string thisOutputDir = outputDir;
690                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
691         map<string, string> variables; 
692                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
693         variables["[extension]"] = m->getExtension(namefile);
694                 string outputFileName = getOutputFileName("name", variables);
695                 ofstream out;
696                 m->openOutputFile(outputFileName, out);
697                 
698
699                 ifstream in;
700                 m->openInputFile(namefile, in);
701                 string name, firstCol, secondCol;
702                 
703                 bool wroteSomething = false;
704                 int selectedCount = 0;
705         
706         if (m->debug) { set<string> temp; sanity["name"] = temp; }
707         if (m->debug) { set<string> temp; sanity["dupname"] = temp; }
708                 
709                 while(!in.eof()){
710                 
711                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
712
713                         in >> firstCol;                         
714                         in >> secondCol;
715                         
716                         string hold = "";
717                         if (dups) { hold = secondCol; }
718                         
719                         vector<string> parsedNames;
720                         m->splitAtComma(secondCol, parsedNames);
721                         
722                         vector<string> validSecond;
723                         for (int i = 0; i < parsedNames.size(); i++) {
724                                 if (names.count(parsedNames[i]) != 0) {
725                                         validSecond.push_back(parsedNames[i]);
726                     if (m->debug) { sanity["dupname"].insert(parsedNames[i]); }
727                                 }
728                         }
729
730                         if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
731                                 for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]); if (m->debug) { sanity["dupname"].insert(parsedNames[i]); } }
732                                 out << firstCol << '\t' << hold << endl;
733                                 wroteSomething = true;
734                                 selectedCount += parsedNames.size();
735                 if (m->debug) { sanity["name"].insert(firstCol); }
736                         }else {
737                                 selectedCount += validSecond.size();
738                                 
739                                 //if the name in the first column is in the set then print it and any other names in second column also in set
740                                 if (names.count(firstCol) != 0) {
741                                 
742                                         wroteSomething = true;
743                                         
744                                         out << firstCol << '\t';
745                                         
746                                         //you know you have at least one valid second since first column is valid
747                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
748                                         out << validSecond[validSecond.size()-1] << endl;
749                     
750                     if (m->debug) { sanity["name"].insert(firstCol); }
751                                         
752                                 
753                                 //make first name in set you come to first column and then add the remaining names to second column
754                                 }else {
755                                         //you want part of this row
756                                         if (validSecond.size() != 0) {
757                                         
758                                                 wroteSomething = true;
759                                                 
760                                                 out << validSecond[0] << '\t';
761                         //we are changing the unique name in the fasta file
762                         uniqueMap[firstCol] = validSecond[0];
763                                         
764                                                 //you know you have at least one valid second since first column is valid
765                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
766                                                 out << validSecond[validSecond.size()-1] << endl;
767                         
768                         if (m->debug) { sanity["name"].insert(validSecond[0]); }
769                                         }
770                                 }
771                         }
772                         m->gobble(in);
773                 }
774                 in.close();
775                 out.close();
776                 
777                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
778                 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
779                 
780                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
781                 
782                 return 0;
783                 
784         }
785         catch(exception& e) {
786                 m->errorOut(e, "GetSeqsCommand", "readName");
787                 exit(1);
788         }
789 }
790
791 //**********************************************************************************************************************
792 int GetSeqsCommand::readGroup(){
793         try {
794                 string thisOutputDir = outputDir;
795                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
796                 map<string, string> variables; 
797                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
798         variables["[extension]"] = m->getExtension(groupfile);
799                 string outputFileName = getOutputFileName("group", variables);
800                 ofstream out;
801                 m->openOutputFile(outputFileName, out);
802                 
803
804                 ifstream in;
805                 m->openInputFile(groupfile, in);
806                 string name, group;
807                 
808                 bool wroteSomething = false;
809                 int selectedCount = 0;
810         
811         if (m->debug) { set<string> temp; sanity["group"] = temp; }
812                 
813                 while(!in.eof()){
814
815                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
816
817
818                         in >> name;                             //read from first column
819                         in >> group;                    //read from second column
820             
821                         
822                         //if this name is in the accnos file
823                         if (names.count(name) != 0) {
824                                 wroteSomething = true;
825                                 
826                                 out << name << '\t' << group << endl;
827                                 selectedCount++;
828                 
829                 if (m->debug) {  sanity["group"].insert(name); }
830                         }
831                                         
832                         m->gobble(in);
833                 }
834                 in.close();
835                 out.close();
836                 
837                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
838                 outputNames.push_back(outputFileName);  outputTypes["group"].push_back(outputFileName);
839                 
840                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
841
842                 
843                 return 0;
844
845         }
846         catch(exception& e) {
847                 m->errorOut(e, "GetSeqsCommand", "readGroup");
848                 exit(1);
849         }
850 }
851 //**********************************************************************************************************************
852 int GetSeqsCommand::readTax(){
853         try {
854                 string thisOutputDir = outputDir;
855                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
856                 map<string, string> variables; 
857                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
858         variables["[extension]"] = m->getExtension(taxfile);
859                 string outputFileName = getOutputFileName("taxonomy", variables);
860                 ofstream out;
861                 m->openOutputFile(outputFileName, out);
862                 
863                 ifstream in;
864                 m->openInputFile(taxfile, in);
865                 string name, tax;
866                 
867                 bool wroteSomething = false;
868                 int selectedCount = 0;
869         
870         if (m->debug) { set<string> temp; sanity["tax"] = temp; }
871                 
872                 while(!in.eof()){
873
874                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
875
876                         in >> name;                             //read from first column
877                         in >> tax;                      //read from second column
878             
879             if (!dups) {//adjust name if needed
880                 map<string, string>::iterator it = uniqueMap.find(name);
881                 if (it != uniqueMap.end()) { name = it->second; }
882             }
883                         
884                         //if this name is in the accnos file
885                         if (names.count(name) != 0) {
886                                 wroteSomething = true;
887                                 
888                                 out << name << '\t' << tax << endl;
889                                 selectedCount++;
890                 
891                 if (m->debug) { sanity["tax"].insert(name); }
892                         }
893                                         
894                         m->gobble(in);
895                 }
896                 in.close();
897                 out.close();
898                 
899                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
900                 outputNames.push_back(outputFileName);  outputTypes["taxonomy"].push_back(outputFileName);
901                 
902                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
903                         
904                 return 0;
905
906         }
907         catch(exception& e) {
908                 m->errorOut(e, "GetSeqsCommand", "readTax");
909                 exit(1);
910         }
911 }
912 //**********************************************************************************************************************
913 //alignreport file has a column header line then all other lines contain 16 columns.  we just want the first column since that contains the name
914 int GetSeqsCommand::readAlign(){
915         try {
916                 string thisOutputDir = outputDir;
917                 if (outputDir == "") {  thisOutputDir += m->hasPath(alignfile);  }
918         map<string, string> variables; 
919                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(alignfile));
920                 string outputFileName = getOutputFileName("alignreport", variables);
921                 ofstream out;
922                 m->openOutputFile(outputFileName, out);
923                 
924
925                 ifstream in;
926                 m->openInputFile(alignfile, in);
927                 string name, junk;
928                 
929                 bool wroteSomething = false;
930                 int selectedCount = 0;
931                 
932                 //read column headers
933                 for (int i = 0; i < 16; i++) {  
934                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
935                         else                    {       break;                  }
936                 }
937                 out << endl;
938                 
939                 while(!in.eof()){
940                 
941                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
942
943
944                         in >> name;                             //read from first column
945             
946             if (!dups) {//adjust name if needed
947                 map<string, string>::iterator it = uniqueMap.find(name);
948                 if (it != uniqueMap.end()) { name = it->second; }
949             }
950                         
951                         //if this name is in the accnos file
952                         if (names.count(name) != 0) {
953                                 wroteSomething = true;
954                                 selectedCount++;
955                                 
956                                 out << name << '\t';
957                                 
958                                 //read rest
959                                 for (int i = 0; i < 15; i++) {  
960                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
961                                         else                    {       break;                  }
962                                 }
963                                 out << endl;
964                                 
965                         }else {//still read just don't do anything with it
966                                 //read rest
967                                 for (int i = 0; i < 15; i++) {  
968                                         if (!in.eof())  {       in >> junk;             }
969                                         else                    {       break;                  }
970                                 }
971                         }
972                         
973                         m->gobble(in);
974                 }
975                 in.close();
976                 out.close();
977                 
978                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
979                 outputNames.push_back(outputFileName);  outputTypes["alignreport"].push_back(outputFileName);
980                 
981                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your alignreport file."); m->mothurOutEndLine();
982                 
983                 return 0;
984                 
985         }
986         catch(exception& e) {
987                 m->errorOut(e, "GetSeqsCommand", "readAlign");
988                 exit(1);
989         }
990 }
991 //**********************************************************************************************************************
992 //just looking at common mistakes. 
993 int GetSeqsCommand::runSanityCheck(){
994         try {
995         string thisOutputDir = outputDir;
996         if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
997         string filename = outputDir + "get.seqs.debug.report";
998         
999         ofstream out;
1000                 m->openOutputFile(filename, out); 
1001
1002        
1003         //compare fasta, name, qual and taxonomy if given to make sure they contain the same seqs
1004         if (fastafile != "") {
1005             if (namefile != "") { //compare with fasta
1006                 if (sanity["fasta"] != sanity["name"]) { //create mismatch file
1007                     createMisMatchFile(out, fastafile, namefile, sanity["fasta"], sanity["name"]);
1008                 }
1009             }
1010             if (qualfile != "") {
1011                 if (sanity["fasta"] != sanity["qual"]) { //create mismatch file
1012                     createMisMatchFile(out, fastafile, qualfile, sanity["fasta"], sanity["qual"]);
1013                 }
1014             }
1015             if (taxfile != "") {
1016                 if (sanity["fasta"] != sanity["tax"]) { //create mismatch file
1017                     createMisMatchFile(out, fastafile, taxfile, sanity["fasta"], sanity["tax"]);
1018                 }
1019             }
1020         }
1021         
1022         //compare dupnames, groups and list if given to make sure they match
1023         if (namefile != "") {
1024             if (groupfile != "") {
1025                 if (sanity["dupname"] != sanity["group"]) { //create mismatch file
1026                     createMisMatchFile(out, namefile, groupfile, sanity["dupname"], sanity["group"]);
1027                 } 
1028             }
1029             if (listfile != "") {
1030                 if (sanity["dupname"] != sanity["list"]) { //create mismatch file
1031                     createMisMatchFile(out, namefile, listfile, sanity["dupname"], sanity["list"]);
1032                 } 
1033             }
1034         }else{
1035
1036             if ((groupfile != "") && (fastafile != "")) {
1037                 if (sanity["fasta"] != sanity["group"]) { //create mismatch file
1038                     createMisMatchFile(out, fastafile, groupfile, sanity["fasta"], sanity["group"]);
1039                 } 
1040             }
1041         }
1042         
1043         out.close();
1044         
1045         if (m->isBlank(filename)) { m->mothurRemove(filename); }
1046         else { m->mothurOut("\n[DEBUG]: " + filename + " contains the file mismatches.\n");outputNames.push_back(filename); outputTypes["debug"].push_back(filename); }
1047         
1048         return 0;
1049     }
1050         catch(exception& e) {
1051                 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
1052                 exit(1);
1053         }
1054 }
1055 //**********************************************************************************************************************
1056 //just looking at common mistakes. 
1057 int GetSeqsCommand::createMisMatchFile(ofstream& out, string filename1, string filename2, set<string> set1, set<string> set2){
1058         try {
1059         out << "****************************************" << endl << endl;
1060         out << "Names unique to " << filename1 << ":\n";
1061         
1062         //remove names in set1 that are also in set2
1063         for (set<string>::iterator it = set1.begin(); it != set1.end();) {
1064             string name = *it;
1065             
1066             if (set2.count(name) == 0)  { out << name << endl;  } //name unique to set1
1067             else                        { set2.erase(name);     } //you are in both so erase 
1068             set1.erase(it++);
1069         }
1070         
1071         out << "\nNames unique to " << filename2 << ":\n";
1072         //output results
1073         for (set<string>::iterator it = set2.begin(); it != set2.end(); it++) {  out << *it << endl;  }
1074         
1075         out << "****************************************" << endl << endl;
1076         
1077         return 0;
1078     }
1079         catch(exception& e) {
1080                 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
1081                 exit(1);
1082         }
1083 }
1084 //**********************************************************************************************************************
1085
1086 int GetSeqsCommand::compareAccnos(){
1087         try {
1088                 
1089                 string thisOutputDir = outputDir;
1090                 if (outputDir == "") {  thisOutputDir += m->hasPath(accnosfile);  }
1091         map<string, string> variables; 
1092                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile));
1093                 string outputFileName = getOutputFileName("accnosreport", variables);
1094                 
1095                 ofstream out;
1096                 m->openOutputFile(outputFileName, out);
1097                 
1098                 ifstream in;
1099                 m->openInputFile(accnosfile2, in);
1100                 string name;
1101                 
1102                 set<string> namesAccnos2;
1103                 set<string> namesDups;
1104                 set<string> namesAccnos = names;
1105                 
1106                 map<string, int> nameCount;
1107                 
1108                 if (namefile != "") {
1109                         ifstream inName;
1110                         m->openInputFile(namefile, inName);
1111                         
1112                         
1113                         while(!inName.eof()){
1114                                 
1115                                 if (m->control_pressed) { inName.close(); return 0; }
1116                                 
1117                                 string thisname, repnames;
1118                                 
1119                                 inName >> thisname;             m->gobble(inName);              //read from first column
1120                                 inName >> repnames;                     //read from second column
1121                                 
1122                                 int num = m->getNumNames(repnames);
1123                                 nameCount[thisname] = num;
1124                                 
1125                                 m->gobble(inName);
1126                         }
1127                         inName.close(); 
1128                 }
1129                 
1130                 while(!in.eof()){
1131                         in >> name;
1132                         
1133                         if (namesAccnos.count(name) == 0){ //name unique to accnos2
1134                                 int pos = name.find_last_of('_');
1135                                 string tempName = name;
1136                                 if (pos != string::npos) {  tempName = tempName.substr(pos+1); cout << tempName << endl; }
1137                                 if (namesAccnos.count(tempName) == 0){
1138                                         namesAccnos2.insert(name);
1139                                 }else { //you are in both so erase
1140                                         namesAccnos.erase(name);
1141                                         namesDups.insert(name);
1142                                 }
1143                         }else { //you are in both so erase
1144                                 namesAccnos.erase(name);
1145                                 namesDups.insert(name);
1146                         }
1147                         
1148                         m->gobble(in);
1149                 }
1150                 in.close();     
1151                 
1152                 out << "Names in both files : " + toString(namesDups.size()) << endl;
1153                 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
1154                 
1155                 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
1156                         out << (*it);
1157                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1158                         out << endl;
1159                 }
1160                 
1161                 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
1162                 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
1163                 
1164                 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
1165                         out << (*it);
1166                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1167                         out << endl;
1168                 }
1169                 
1170                 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
1171                 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
1172                 
1173                 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
1174                         out << (*it);
1175                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1176                         out << endl;
1177                 }
1178
1179                 out.close(); 
1180                 
1181                 outputNames.push_back(outputFileName);  outputTypes["accnosreport"].push_back(outputFileName);
1182                 
1183                 return 0;
1184                 
1185         }
1186         catch(exception& e) {
1187                 m->errorOut(e, "GetSeqsCommand", "compareAccnos");
1188                 exit(1);
1189         }
1190 }
1191
1192
1193 //**********************************************************************************************************************
1194