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