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