]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
779ea912ebebbe58ec22e65723899c4ea566b85f
[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 (name != "") {
432                                 //if this name is in the accnos file
433                                 if (names.count(name) != 0) {
434                                         wroteSomething = true;
435                                         
436                                         currSeq.printSequence(out);
437                                         selectedCount++;
438                     
439                     if (m->debug) { sanity["fasta"].insert(name); }
440                                 }
441                         }
442                         m->gobble(in);
443                 }
444                 in.close();     
445                 out.close();
446                 
447                 
448                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
449                 outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName); 
450                 
451                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
452                 
453                 return 0;
454
455         }
456         catch(exception& e) {
457                 m->errorOut(e, "GetSeqsCommand", "readFasta");
458                 exit(1);
459         }
460 }
461 //**********************************************************************************************************************
462 int GetSeqsCommand::readQual(){
463         try {
464                 string thisOutputDir = outputDir;
465                 if (outputDir == "") {  thisOutputDir += m->hasPath(qualfile);  }
466                 map<string, string> variables; 
467         variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(qualfile));
468         variables["[extension]"] = m->getExtension(qualfile);
469                 string outputFileName = getOutputFileName("qfile", variables);
470                 ofstream out;
471                 m->openOutputFile(outputFileName, out);
472                 
473                 
474                 ifstream in;
475                 m->openInputFile(qualfile, in);
476                 string name;
477                 
478                 bool wroteSomething = false;
479                 int selectedCount = 0;
480                 
481         if (m->debug) { set<string> temp; sanity["qual"] = temp; }
482                 
483                 while(!in.eof()){       
484                         string saveName = "";
485                         string name = "";
486                         string scores = "";
487                         
488                         in >> name; 
489                                 
490                         if (name.length() != 0) { 
491                                 saveName = name.substr(1);
492                                 while (!in.eof())       {       
493                                         char c = in.get(); 
494                                         if (c == 10 || c == 13){        break;  }
495                                         else { name += c; }     
496                                 } 
497                                 m->gobble(in);
498                         }
499                         
500                         while(in){
501                                 char letter= in.get();
502                                 if(letter == '>'){      in.putback(letter);     break;  }
503                                 else{ scores += letter; }
504                         }
505                         
506                         m->gobble(in);
507                         
508                         if (names.count(saveName) != 0) {
509                                 wroteSomething = true;
510                                                 
511                                 out << name << endl << scores;
512                                 selectedCount++;
513                 if (m->debug) { sanity["qual"].insert(name); }
514                         }
515                         
516                         m->gobble(in);
517                 }
518                 in.close();
519                 out.close();
520                 
521                 
522                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
523                 outputNames.push_back(outputFileName);  outputTypes["qfile"].push_back(outputFileName); 
524                 
525                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your quality file."); m->mothurOutEndLine();
526
527                 
528                 return 0;
529                 
530         }
531         catch(exception& e) {
532                 m->errorOut(e, "GetSeqsCommand", "readQual");
533                 exit(1);
534         }
535 }
536 //**********************************************************************************************************************
537 int GetSeqsCommand::readCount(){
538         try {
539                 string thisOutputDir = outputDir;
540                 if (outputDir == "") {  thisOutputDir += m->hasPath(countfile);  }
541                 map<string, string> variables; 
542                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
543         variables["[extension]"] = m->getExtension(countfile);
544                 string outputFileName = getOutputFileName("count", variables);
545                 
546                 ofstream out;
547                 m->openOutputFile(outputFileName, out);
548                 
549                 ifstream in;
550                 m->openInputFile(countfile, in);
551                 
552                 bool wroteSomething = false;
553                 int selectedCount = 0;
554                 
555         string headers = m->getline(in); m->gobble(in);
556         out << headers << endl;
557         
558         string name, rest; int thisTotal;
559         while (!in.eof()) {
560             
561             if (m->control_pressed) { in.close();  out.close();  m->mothurRemove(outputFileName);  return 0; }
562             
563             in >> name; m->gobble(in); 
564             in >> thisTotal; m->gobble(in);
565             rest = m->getline(in); m->gobble(in);
566             if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + rest + "\n"); }
567             
568             if (names.count(name) != 0) {
569                 out << name << '\t' << thisTotal << '\t' << rest << endl;
570                 wroteSomething = true;
571                 selectedCount+= thisTotal;
572             }
573         }
574         in.close();
575                 out.close();
576         
577         //check for groups that have been eliminated
578         CountTable ct;
579         if (ct.testGroups(outputFileName)) {
580             ct.readTable(outputFileName);
581             ct.printTable(outputFileName);
582         }
583                 
584                 if (wroteSomething == false) {  m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
585                 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
586                 
587                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your count file."); m->mothurOutEndLine();
588         
589                 return 0;
590         }
591         catch(exception& e) {
592                 m->errorOut(e, "GetSeqsCommand", "readCount");
593                 exit(1);
594         }
595 }
596
597 //**********************************************************************************************************************
598 int GetSeqsCommand::readList(){
599         try {
600                 string thisOutputDir = outputDir;
601                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
602         map<string, string> variables; 
603                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
604         variables["[extension]"] = m->getExtension(listfile);
605                 string outputFileName = getOutputFileName("list", variables);
606                 ofstream out;
607                 m->openOutputFile(outputFileName, out);
608                 
609                 ifstream in;
610                 m->openInputFile(listfile, in);
611                 
612                 bool wroteSomething = false;
613                 int selectedCount = 0;
614         
615         if (m->debug) { set<string> temp; sanity["list"] = temp; }
616                 
617                 while(!in.eof()){
618                         
619                         selectedCount = 0;
620                         
621                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
622
623                         //read in list vector
624                         ListVector list(in);
625                         
626                         //make a new list vector
627                         ListVector newList;
628                         newList.setLabel(list.getLabel());
629                         
630                         //for each bin
631                         for (int i = 0; i < list.getNumBins(); i++) {
632                         
633                                 //parse out names that are in accnos file
634                                 string binnames = list.get(i);
635                 vector<string> bnames;
636                 m->splitAtComma(binnames, bnames);
637                                 
638                                 string newNames = "";
639                 for (int i = 0; i < bnames.size(); i++) {
640                                         string name = bnames[i];
641                                         //if that name is in the .accnos file, add it
642                                         if (names.count(name) != 0) {  newNames += name + ",";  selectedCount++; if (m->debug) { sanity["list"].insert(name); } }
643                                 }
644                         
645                                 //if there are names in this bin add to new list
646                                 if (newNames != "") { 
647                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
648                                         newList.push_back(newNames);    
649                                 }
650                         }
651                                 
652                         //print new listvector
653                         if (newList.getNumBins() != 0) {
654                                 wroteSomething = true;
655                                 newList.print(out);
656                         }
657                         
658                         m->gobble(in);
659                 }
660                 in.close();     
661                 out.close();
662                 
663                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
664                 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
665                 
666                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
667                 
668                 return 0;
669
670         }
671         catch(exception& e) {
672                 m->errorOut(e, "GetSeqsCommand", "readList");
673                 exit(1);
674         }
675 }
676 //**********************************************************************************************************************
677 int GetSeqsCommand::readName(){
678         try {
679                 string thisOutputDir = outputDir;
680                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
681         map<string, string> variables; 
682                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
683         variables["[extension]"] = m->getExtension(namefile);
684                 string outputFileName = getOutputFileName("name", variables);
685                 ofstream out;
686                 m->openOutputFile(outputFileName, out);
687                 
688
689                 ifstream in;
690                 m->openInputFile(namefile, in);
691                 string name, firstCol, secondCol;
692                 
693                 bool wroteSomething = false;
694                 int selectedCount = 0;
695         
696         if (m->debug) { set<string> temp; sanity["name"] = temp; }
697         if (m->debug) { set<string> temp; sanity["dupname"] = temp; }
698                 
699                 while(!in.eof()){
700                 
701                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
702
703                         in >> firstCol;                         
704                         in >> secondCol;
705                         
706                         string hold = "";
707                         if (dups) { hold = secondCol; }
708                         
709                         vector<string> parsedNames;
710                         m->splitAtComma(secondCol, parsedNames);
711                         
712                         vector<string> validSecond;
713                         for (int i = 0; i < parsedNames.size(); i++) {
714                                 if (names.count(parsedNames[i]) != 0) {
715                                         validSecond.push_back(parsedNames[i]);
716                     if (m->debug) { sanity["dupname"].insert(parsedNames[i]); }
717                                 }
718                         }
719
720                         if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
721                                 for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]); if (m->debug) { sanity["dupname"].insert(parsedNames[i]); } }
722                                 out << firstCol << '\t' << hold << endl;
723                                 wroteSomething = true;
724                                 selectedCount += parsedNames.size();
725                 if (m->debug) { sanity["name"].insert(firstCol); }
726                         }else {
727                                 selectedCount += validSecond.size();
728                                 
729                                 //if the name in the first column is in the set then print it and any other names in second column also in set
730                                 if (names.count(firstCol) != 0) {
731                                 
732                                         wroteSomething = true;
733                                         
734                                         out << firstCol << '\t';
735                                         
736                                         //you know you have at least one valid second since first column is valid
737                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
738                                         out << validSecond[validSecond.size()-1] << endl;
739                     
740                     if (m->debug) { sanity["name"].insert(firstCol); }
741                                         
742                                 
743                                 //make first name in set you come to first column and then add the remaining names to second column
744                                 }else {
745                                         //you want part of this row
746                                         if (validSecond.size() != 0) {
747                                         
748                                                 wroteSomething = true;
749                                                 
750                                                 out << validSecond[0] << '\t';
751                                         
752                                                 //you know you have at least one valid second since first column is valid
753                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
754                                                 out << validSecond[validSecond.size()-1] << endl;
755                         
756                         if (m->debug) { sanity["name"].insert(validSecond[0]); }
757                                         }
758                                 }
759                         }
760                         m->gobble(in);
761                 }
762                 in.close();
763                 out.close();
764                 
765                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
766                 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
767                 
768                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
769                 
770                 return 0;
771                 
772         }
773         catch(exception& e) {
774                 m->errorOut(e, "GetSeqsCommand", "readName");
775                 exit(1);
776         }
777 }
778
779 //**********************************************************************************************************************
780 int GetSeqsCommand::readGroup(){
781         try {
782                 string thisOutputDir = outputDir;
783                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
784                 map<string, string> variables; 
785                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
786         variables["[extension]"] = m->getExtension(groupfile);
787                 string outputFileName = getOutputFileName("group", variables);
788                 ofstream out;
789                 m->openOutputFile(outputFileName, out);
790                 
791
792                 ifstream in;
793                 m->openInputFile(groupfile, in);
794                 string name, group;
795                 
796                 bool wroteSomething = false;
797                 int selectedCount = 0;
798         
799         if (m->debug) { set<string> temp; sanity["group"] = temp; }
800                 
801                 while(!in.eof()){
802
803                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
804
805
806                         in >> name;                             //read from first column
807                         in >> group;                    //read from second column
808                         
809                         //if this name is in the accnos file
810                         if (names.count(name) != 0) {
811                                 wroteSomething = true;
812                                 
813                                 out << name << '\t' << group << endl;
814                                 selectedCount++;
815                 
816                 if (m->debug) {  sanity["group"].insert(name); }
817                         }
818                                         
819                         m->gobble(in);
820                 }
821                 in.close();
822                 out.close();
823                 
824                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
825                 outputNames.push_back(outputFileName);  outputTypes["group"].push_back(outputFileName);
826                 
827                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
828
829                 
830                 return 0;
831
832         }
833         catch(exception& e) {
834                 m->errorOut(e, "GetSeqsCommand", "readGroup");
835                 exit(1);
836         }
837 }
838 //**********************************************************************************************************************
839 int GetSeqsCommand::readTax(){
840         try {
841                 string thisOutputDir = outputDir;
842                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
843                 map<string, string> variables; 
844                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
845         variables["[extension]"] = m->getExtension(taxfile);
846                 string outputFileName = getOutputFileName("taxonomy", variables);
847                 ofstream out;
848                 m->openOutputFile(outputFileName, out);
849                 
850                 ifstream in;
851                 m->openInputFile(taxfile, in);
852                 string name, tax;
853                 
854                 bool wroteSomething = false;
855                 int selectedCount = 0;
856         
857         if (m->debug) { set<string> temp; sanity["tax"] = temp; }
858                 
859                 while(!in.eof()){
860
861                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
862
863                         in >> name;                             //read from first column
864                         in >> tax;                      //read from second column
865                         
866                         //if this name is in the accnos file
867                         if (names.count(name) != 0) {
868                                 wroteSomething = true;
869                                 
870                                 out << name << '\t' << tax << endl;
871                                 selectedCount++;
872                 
873                 if (m->debug) { sanity["tax"].insert(name); }
874                         }
875                                         
876                         m->gobble(in);
877                 }
878                 in.close();
879                 out.close();
880                 
881                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
882                 outputNames.push_back(outputFileName);  outputTypes["taxonomy"].push_back(outputFileName);
883                 
884                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
885                         
886                 return 0;
887
888         }
889         catch(exception& e) {
890                 m->errorOut(e, "GetSeqsCommand", "readTax");
891                 exit(1);
892         }
893 }
894 //**********************************************************************************************************************
895 //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
896 int GetSeqsCommand::readAlign(){
897         try {
898                 string thisOutputDir = outputDir;
899                 if (outputDir == "") {  thisOutputDir += m->hasPath(alignfile);  }
900         map<string, string> variables; 
901                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(alignfile));
902                 string outputFileName = getOutputFileName("alignreport", variables);
903                 ofstream out;
904                 m->openOutputFile(outputFileName, out);
905                 
906
907                 ifstream in;
908                 m->openInputFile(alignfile, in);
909                 string name, junk;
910                 
911                 bool wroteSomething = false;
912                 int selectedCount = 0;
913                 
914                 //read column headers
915                 for (int i = 0; i < 16; i++) {  
916                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
917                         else                    {       break;                  }
918                 }
919                 out << endl;
920                 
921                 while(!in.eof()){
922                 
923                         if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName);  return 0; }
924
925
926                         in >> name;                             //read from first column
927                         
928                         //if this name is in the accnos file
929                         if (names.count(name) != 0) {
930                                 wroteSomething = true;
931                                 selectedCount++;
932                                 
933                                 out << name << '\t';
934                                 
935                                 //read rest
936                                 for (int i = 0; i < 15; i++) {  
937                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
938                                         else                    {       break;                  }
939                                 }
940                                 out << endl;
941                                 
942                         }else {//still read just don't do anything with it
943                                 //read rest
944                                 for (int i = 0; i < 15; i++) {  
945                                         if (!in.eof())  {       in >> junk;             }
946                                         else                    {       break;                  }
947                                 }
948                         }
949                         
950                         m->gobble(in);
951                 }
952                 in.close();
953                 out.close();
954                 
955                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
956                 outputNames.push_back(outputFileName);  outputTypes["alignreport"].push_back(outputFileName);
957                 
958                 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your alignreport file."); m->mothurOutEndLine();
959                 
960                 return 0;
961                 
962         }
963         catch(exception& e) {
964                 m->errorOut(e, "GetSeqsCommand", "readAlign");
965                 exit(1);
966         }
967 }
968 //**********************************************************************************************************************
969 //just looking at common mistakes. 
970 int GetSeqsCommand::runSanityCheck(){
971         try {
972         string thisOutputDir = outputDir;
973         if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
974         string filename = outputDir + "get.seqs.debug.report";
975         
976         ofstream out;
977                 m->openOutputFile(filename, out); 
978
979        
980         //compare fasta, name, qual and taxonomy if given to make sure they contain the same seqs
981         if (fastafile != "") {
982             if (namefile != "") { //compare with fasta
983                 if (sanity["fasta"] != sanity["name"]) { //create mismatch file
984                     createMisMatchFile(out, fastafile, namefile, sanity["fasta"], sanity["name"]);
985                 }
986             }
987             if (qualfile != "") {
988                 if (sanity["fasta"] != sanity["qual"]) { //create mismatch file
989                     createMisMatchFile(out, fastafile, qualfile, sanity["fasta"], sanity["qual"]);
990                 }
991             }
992             if (taxfile != "") {
993                 if (sanity["fasta"] != sanity["tax"]) { //create mismatch file
994                     createMisMatchFile(out, fastafile, taxfile, sanity["fasta"], sanity["tax"]);
995                 }
996             }
997         }
998         
999         //compare dupnames, groups and list if given to make sure they match
1000         if (namefile != "") {
1001             if (groupfile != "") {
1002                 if (sanity["dupname"] != sanity["group"]) { //create mismatch file
1003                     createMisMatchFile(out, namefile, groupfile, sanity["dupname"], sanity["group"]);
1004                 } 
1005             }
1006             if (listfile != "") {
1007                 if (sanity["dupname"] != sanity["list"]) { //create mismatch file
1008                     createMisMatchFile(out, namefile, listfile, sanity["dupname"], sanity["list"]);
1009                 } 
1010             }
1011         }else{
1012
1013             if ((groupfile != "") && (fastafile != "")) {
1014                 if (sanity["fasta"] != sanity["group"]) { //create mismatch file
1015                     createMisMatchFile(out, fastafile, groupfile, sanity["fasta"], sanity["group"]);
1016                 } 
1017             }
1018         }
1019         
1020         out.close();
1021         
1022         if (m->isBlank(filename)) { m->mothurRemove(filename); }
1023         else { m->mothurOut("\n[DEBUG]: " + filename + " contains the file mismatches.\n");outputNames.push_back(filename); outputTypes["debug"].push_back(filename); }
1024         
1025         return 0;
1026     }
1027         catch(exception& e) {
1028                 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
1029                 exit(1);
1030         }
1031 }
1032 //**********************************************************************************************************************
1033 //just looking at common mistakes. 
1034 int GetSeqsCommand::createMisMatchFile(ofstream& out, string filename1, string filename2, set<string> set1, set<string> set2){
1035         try {
1036         out << "****************************************" << endl << endl;
1037         out << "Names unique to " << filename1 << ":\n";
1038         
1039         //remove names in set1 that are also in set2
1040         for (set<string>::iterator it = set1.begin(); it != set1.end();) {
1041             string name = *it;
1042             
1043             if (set2.count(name) == 0)  { out << name << endl;  } //name unique to set1
1044             else                        { set2.erase(name);     } //you are in both so erase 
1045             set1.erase(it++);
1046         }
1047         
1048         out << "\nNames unique to " << filename2 << ":\n";
1049         //output results
1050         for (set<string>::iterator it = set2.begin(); it != set2.end(); it++) {  out << *it << endl;  }
1051         
1052         out << "****************************************" << endl << endl;
1053         
1054         return 0;
1055     }
1056         catch(exception& e) {
1057                 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
1058                 exit(1);
1059         }
1060 }
1061 //**********************************************************************************************************************
1062
1063 int GetSeqsCommand::compareAccnos(){
1064         try {
1065                 
1066                 string thisOutputDir = outputDir;
1067                 if (outputDir == "") {  thisOutputDir += m->hasPath(accnosfile);  }
1068         map<string, string> variables; 
1069                 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile));
1070                 string outputFileName = getOutputFileName("accnosreport", variables);
1071                 
1072                 ofstream out;
1073                 m->openOutputFile(outputFileName, out);
1074                 
1075                 ifstream in;
1076                 m->openInputFile(accnosfile2, in);
1077                 string name;
1078                 
1079                 set<string> namesAccnos2;
1080                 set<string> namesDups;
1081                 set<string> namesAccnos = names;
1082                 
1083                 map<string, int> nameCount;
1084                 
1085                 if (namefile != "") {
1086                         ifstream inName;
1087                         m->openInputFile(namefile, inName);
1088                         
1089                         
1090                         while(!inName.eof()){
1091                                 
1092                                 if (m->control_pressed) { inName.close(); return 0; }
1093                                 
1094                                 string thisname, repnames;
1095                                 
1096                                 inName >> thisname;             m->gobble(inName);              //read from first column
1097                                 inName >> repnames;                     //read from second column
1098                                 
1099                                 int num = m->getNumNames(repnames);
1100                                 nameCount[thisname] = num;
1101                                 
1102                                 m->gobble(inName);
1103                         }
1104                         inName.close(); 
1105                 }
1106                 
1107                 while(!in.eof()){
1108                         in >> name;
1109                         
1110                         if (namesAccnos.count(name) == 0){ //name unique to accnos2
1111                                 int pos = name.find_last_of('_');
1112                                 string tempName = name;
1113                                 if (pos != string::npos) {  tempName = tempName.substr(pos+1); cout << tempName << endl; }
1114                                 if (namesAccnos.count(tempName) == 0){
1115                                         namesAccnos2.insert(name);
1116                                 }else { //you are in both so erase
1117                                         namesAccnos.erase(name);
1118                                         namesDups.insert(name);
1119                                 }
1120                         }else { //you are in both so erase
1121                                 namesAccnos.erase(name);
1122                                 namesDups.insert(name);
1123                         }
1124                         
1125                         m->gobble(in);
1126                 }
1127                 in.close();     
1128                 
1129                 out << "Names in both files : " + toString(namesDups.size()) << endl;
1130                 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
1131                 
1132                 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
1133                         out << (*it);
1134                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1135                         out << endl;
1136                 }
1137                 
1138                 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
1139                 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
1140                 
1141                 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
1142                         out << (*it);
1143                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1144                         out << endl;
1145                 }
1146                 
1147                 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
1148                 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
1149                 
1150                 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
1151                         out << (*it);
1152                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1153                         out << endl;
1154                 }
1155
1156                 out.close(); 
1157                 
1158                 outputNames.push_back(outputFileName);  outputTypes["accnosreport"].push_back(outputFileName);
1159                 
1160                 return 0;
1161                 
1162         }
1163         catch(exception& e) {
1164                 m->errorOut(e, "GetSeqsCommand", "compareAccnos");
1165                 exit(1);
1166         }
1167 }
1168
1169
1170 //**********************************************************************************************************************
1171