]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[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                 
29                 vector<string> myArray;
30                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
31                 return myArray;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "GetSeqsCommand", "setParameters");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 string GetSeqsCommand::getHelpString(){ 
40         try {
41                 string helpString = "";
42                 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";
43                 helpString += "It outputs a file containing only the sequences in the .accnos file.\n";
44                 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";
45                 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";
46                 helpString += "The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n";
47                 helpString += "Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n";
48                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n";
49                 return helpString;
50         }
51         catch(exception& e) {
52                 m->errorOut(e, "GetSeqsCommand", "getHelpString");
53                 exit(1);
54         }
55 }
56
57 //**********************************************************************************************************************
58 GetSeqsCommand::GetSeqsCommand(){       
59         try {
60                 abort = true; calledHelp = true;
61                 setParameters();
62                 vector<string> tempOutNames;
63                 outputTypes["fasta"] = tempOutNames;
64                 outputTypes["taxonomy"] = tempOutNames;
65                 outputTypes["name"] = tempOutNames;
66                 outputTypes["group"] = tempOutNames;
67                 outputTypes["alignreport"] = tempOutNames;
68                 outputTypes["list"] = tempOutNames;
69                 outputTypes["qfile"] = tempOutNames;
70                 outputTypes["accnosreport"] = tempOutNames;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78 GetSeqsCommand::GetSeqsCommand(string option)  {
79         try {
80                 abort = false; calledHelp = false;   
81                                 
82                 //allow user to run help
83                 if(option == "help") { help(); abort = true; calledHelp = true; }
84                 
85                 else {
86                         vector<string> myArray = setParameters();
87                         
88                         OptionParser parser(option);
89                         map<string,string> parameters = parser.getParameters();
90                         
91                         ValidParameters validParameter;
92                         map<string,string>::iterator it;
93                         
94                         //check to make sure all parameters are valid for command
95                         for (it = parameters.begin(); it != parameters.end(); it++) { 
96                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
97                         }
98                         
99                         //initialize outputTypes
100                         vector<string> tempOutNames;
101                         outputTypes["fasta"] = tempOutNames;
102                         outputTypes["taxonomy"] = tempOutNames;
103                         outputTypes["name"] = tempOutNames;
104                         outputTypes["group"] = tempOutNames;
105                         outputTypes["alignreport"] = tempOutNames;
106                         outputTypes["list"] = tempOutNames;
107                         outputTypes["qfile"] = tempOutNames;
108                         outputTypes["accnosreport"] = tempOutNames;
109                         
110                         //if the user changes the output directory command factory will send this info to us in the output parameter 
111                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
112                         
113                         //if the user changes the input directory command factory will send this info to us in the output parameter 
114                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
115                         if (inputDir == "not found"){   inputDir = "";          }
116                         else {
117                                 string path;
118                                 it = parameters.find("alignreport");
119                                 //user has given a template file
120                                 if(it != parameters.end()){ 
121                                         path = m->hasPath(it->second);
122                                         //if the user has not given a path then, add inputdir. else leave path alone.
123                                         if (path == "") {       parameters["alignreport"] = inputDir + it->second;              }
124                                 }
125                                 
126                                 it = parameters.find("fasta");
127                                 //user has given a template file
128                                 if(it != parameters.end()){ 
129                                         path = m->hasPath(it->second);
130                                         //if the user has not given a path then, add inputdir. else leave path alone.
131                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
132                                 }
133                                 
134                                 it = parameters.find("accnos");
135                                 //user has given a template file
136                                 if(it != parameters.end()){ 
137                                         path = m->hasPath(it->second);
138                                         //if the user has not given a path then, add inputdir. else leave path alone.
139                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
140                                 }
141                                 
142                                 it = parameters.find("accnos2");
143                                 //user has given a template file
144                                 if(it != parameters.end()){ 
145                                         path = m->hasPath(it->second);
146                                         //if the user has not given a path then, add inputdir. else leave path alone.
147                                         if (path == "") {       parameters["accnos2"] = inputDir + it->second;          }
148                                 }
149                                 
150                                 it = parameters.find("list");
151                                 //user has given a template file
152                                 if(it != parameters.end()){ 
153                                         path = m->hasPath(it->second);
154                                         //if the user has not given a path then, add inputdir. else leave path alone.
155                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
156                                 }
157                                 
158                                 it = parameters.find("name");
159                                 //user has given a template file
160                                 if(it != parameters.end()){ 
161                                         path = m->hasPath(it->second);
162                                         //if the user has not given a path then, add inputdir. else leave path alone.
163                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
164                                 }
165                                 
166                                 it = parameters.find("group");
167                                 //user has given a template file
168                                 if(it != parameters.end()){ 
169                                         path = m->hasPath(it->second);
170                                         //if the user has not given a path then, add inputdir. else leave path alone.
171                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
172                                 }
173                                 
174                                 it = parameters.find("taxonomy");
175                                 //user has given a template file
176                                 if(it != parameters.end()){ 
177                                         path = m->hasPath(it->second);
178                                         //if the user has not given a path then, add inputdir. else leave path alone.
179                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
180                                 }
181                                 
182                                 it = parameters.find("qfile");
183                                 //user has given a template file
184                                 if(it != parameters.end()){ 
185                                         path = m->hasPath(it->second);
186                                         //if the user has not given a path then, add inputdir. else leave path alone.
187                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
188                                 }
189                         }
190
191                         
192                         //check for required parameters
193                         accnosfile = validParameter.validFile(parameters, "accnos", true);
194                         if (accnosfile == "not open") { abort = true; }
195                         else if (accnosfile == "not found") {  
196                                 accnosfile = m->getAccnosFile(); 
197                                 if (accnosfile != "") {  m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
198                                 else { 
199                                         m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine(); 
200                                         abort = true;
201                                 } 
202                         }       
203                         
204                         if (accnosfile2 == "not found") { accnosfile2 = ""; }
205                         
206                         fastafile = validParameter.validFile(parameters, "fasta", true);
207                         if (fastafile == "not open") { abort = true; }
208                         else if (fastafile == "not found") {  fastafile = "";  }        
209                         
210                         namefile = validParameter.validFile(parameters, "name", true);
211                         if (namefile == "not open") { abort = true; }
212                         else if (namefile == "not found") {  namefile = "";  }  
213                         
214                         groupfile = validParameter.validFile(parameters, "group", true);
215                         if (groupfile == "not open") { abort = true; }
216                         else if (groupfile == "not found") {  groupfile = "";  }        
217                         
218                         alignfile = validParameter.validFile(parameters, "alignreport", true);
219                         if (alignfile == "not open") { abort = true; }
220                         else if (alignfile == "not found") {  alignfile = "";  }
221                         
222                         listfile = validParameter.validFile(parameters, "list", true);
223                         if (listfile == "not open") { abort = true; }
224                         else if (listfile == "not found") {  listfile = "";  }
225                         
226                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
227                         if (taxfile == "not open") { abort = true; }
228                         else if (taxfile == "not found") {  taxfile = "";  }
229                         
230                         qualfile = validParameter.validFile(parameters, "qfile", true);
231                         if (qualfile == "not open") { abort = true; }
232                         else if (qualfile == "not found") {  qualfile = "";  }
233                         
234                         string usedDups = "true";
235                         string temp = validParameter.validFile(parameters, "dups", false);      if (temp == "not found") { temp = "true"; usedDups = ""; }
236                         dups = m->isTrue(temp);
237                         
238                         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; }
239                 }
240
241         }
242         catch(exception& e) {
243                 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
244                 exit(1);
245         }
246 }
247 //**********************************************************************************************************************
248
249 int GetSeqsCommand::execute(){
250         try {
251                 
252                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
253                 
254                 //get names you want to keep
255                 readAccnos();
256                 
257                 if (m->control_pressed) { return 0; }
258                 
259                 //read through the correct file and output lines you want to keep
260                 if (namefile != "")                     {               readName();                     }
261                 if (fastafile != "")            {               readFasta();            }
262                 if (groupfile != "")            {               readGroup();            }
263                 if (alignfile != "")            {               readAlign();            }
264                 if (listfile != "")                     {               readList();                     }
265                 if (taxfile != "")                      {               readTax();                      }
266                 if (qualfile != "")                     {               readQual();                     }
267                 if (accnosfile2 != "")          {               compareAccnos();        }
268                 
269                 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str());  } return 0; }
270                 
271                 m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
272                 
273                 if (outputNames.size() != 0) {
274                         m->mothurOutEndLine();
275                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
276                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
277                         m->mothurOutEndLine();
278                         
279                         //set fasta file as new current fastafile
280                         string current = "";
281                         itTypes = outputTypes.find("fasta");
282                         if (itTypes != outputTypes.end()) {
283                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
284                         }
285                         
286                         itTypes = outputTypes.find("name");
287                         if (itTypes != outputTypes.end()) {
288                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
289                         }
290                         
291                         itTypes = outputTypes.find("group");
292                         if (itTypes != outputTypes.end()) {
293                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
294                         }
295                         
296                         itTypes = outputTypes.find("list");
297                         if (itTypes != outputTypes.end()) {
298                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
299                         }
300                         
301                         itTypes = outputTypes.find("taxonomy");
302                         if (itTypes != outputTypes.end()) {
303                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
304                         }
305                         
306                         itTypes = outputTypes.find("qfile");
307                         if (itTypes != outputTypes.end()) {
308                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
309                         }
310                         
311                 }
312                 
313                 return 0;               
314         }
315
316         catch(exception& e) {
317                 m->errorOut(e, "GetSeqsCommand", "execute");
318                 exit(1);
319         }
320 }
321
322 //**********************************************************************************************************************
323 int GetSeqsCommand::readFasta(){
324         try {
325                 string thisOutputDir = outputDir;
326                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
327                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" +  m->getExtension(fastafile);
328                 ofstream out;
329                 m->openOutputFile(outputFileName, out);
330                 
331                 
332                 ifstream in;
333                 m->openInputFile(fastafile, in);
334                 string name;
335                 
336                 bool wroteSomething = false;
337                 
338                 while(!in.eof()){
339                 
340                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
341                         
342                         Sequence currSeq(in);
343                         name = currSeq.getName();
344                         
345                         if (name != "") {
346                                 //if this name is in the accnos file
347                                 if (names.count(name) != 0) {
348                                         wroteSomething = true;
349                                         
350                                         currSeq.printSequence(out);
351                                 }
352                         }
353                         m->gobble(in);
354                 }
355                 in.close();     
356                 out.close();
357                 
358                 
359                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
360                 outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName); 
361                 
362                 return 0;
363
364         }
365         catch(exception& e) {
366                 m->errorOut(e, "GetSeqsCommand", "readFasta");
367                 exit(1);
368         }
369 }
370 //**********************************************************************************************************************
371 int GetSeqsCommand::readQual(){
372         try {
373                 string thisOutputDir = outputDir;
374                 if (outputDir == "") {  thisOutputDir += m->hasPath(qualfile);  }
375                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" +  m->getExtension(qualfile);
376                 ofstream out;
377                 m->openOutputFile(outputFileName, out);
378                 
379                 
380                 ifstream in;
381                 m->openInputFile(qualfile, in);
382                 string name;
383                 
384                 bool wroteSomething = false;
385                 
386                 
387                 while(!in.eof()){       
388                         string saveName = "";
389                         string name = "";
390                         string scores = "";
391                         
392                         in >> name; 
393                                 
394                         if (name.length() != 0) { 
395                                 saveName = name.substr(1);
396                                 while (!in.eof())       {       
397                                         char c = in.get(); 
398                                         if (c == 10 || c == 13){        break;  }
399                                         else { name += c; }     
400                                 } 
401                                 m->gobble(in);
402                         }
403                         
404                         while(in){
405                                 char letter= in.get();
406                                 if(letter == '>'){      in.putback(letter);     break;  }
407                                 else{ scores += letter; }
408                         }
409                         
410                         m->gobble(in);
411                         
412                         if (names.count(saveName) != 0) {
413                                 wroteSomething = true;
414                                                 
415                                 out << name << endl << scores;
416                         }
417                         
418                         m->gobble(in);
419                 }
420                 in.close();
421                 out.close();
422                 
423                 
424                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
425                 outputNames.push_back(outputFileName);  outputTypes["qfile"].push_back(outputFileName); 
426                 
427                 return 0;
428                 
429         }
430         catch(exception& e) {
431                 m->errorOut(e, "GetSeqsCommand", "readQual");
432                 exit(1);
433         }
434 }
435 //**********************************************************************************************************************
436 int GetSeqsCommand::readList(){
437         try {
438                 string thisOutputDir = outputDir;
439                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
440                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
441                 ofstream out;
442                 m->openOutputFile(outputFileName, out);
443                 
444                 ifstream in;
445                 m->openInputFile(listfile, in);
446                 
447                 bool wroteSomething = false;
448                 
449                 while(!in.eof()){
450                         
451                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
452
453                         //read in list vector
454                         ListVector list(in);
455                         
456                         //make a new list vector
457                         ListVector newList;
458                         newList.setLabel(list.getLabel());
459                         
460                         //for each bin
461                         for (int i = 0; i < list.getNumBins(); i++) {
462                         
463                                 //parse out names that are in accnos file
464                                 string binnames = list.get(i);
465                                 
466                                 string newNames = "";
467                                 while (binnames.find_first_of(',') != -1) { 
468                                         string name = binnames.substr(0,binnames.find_first_of(','));
469                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
470                                         
471                                         //if that name is in the .accnos file, add it
472                                         if (names.count(name) != 0) {  newNames += name + ",";  }
473                                 }
474                         
475                                 //get last name
476                                 if (names.count(binnames) != 0) {  newNames += binnames + ",";  }
477
478                                 //if there are names in this bin add to new list
479                                 if (newNames != "") { 
480                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
481                                         newList.push_back(newNames);    
482                                 }
483                         }
484                                 
485                         //print new listvector
486                         if (newList.getNumBins() != 0) {
487                                 wroteSomething = true;
488                                 newList.print(out);
489                         }
490                         
491                         m->gobble(in);
492                 }
493                 in.close();     
494                 out.close();
495                 
496                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
497                 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
498                 
499                 return 0;
500
501         }
502         catch(exception& e) {
503                 m->errorOut(e, "GetSeqsCommand", "readList");
504                 exit(1);
505         }
506 }
507 //**********************************************************************************************************************
508 int GetSeqsCommand::readName(){
509         try {
510                 string thisOutputDir = outputDir;
511                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
512                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" +  m->getExtension(namefile);
513                 ofstream out;
514                 m->openOutputFile(outputFileName, out);
515                 
516
517                 ifstream in;
518                 m->openInputFile(namefile, in);
519                 string name, firstCol, secondCol;
520                 
521                 bool wroteSomething = false;
522                 
523                 
524                 while(!in.eof()){
525                 
526                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
527
528                         in >> firstCol;                         
529                         in >> secondCol;
530                         
531                         string hold = "";
532                         if (dups) { hold = secondCol; }
533                         
534                         vector<string> parsedNames;
535                         m->splitAtComma(secondCol, parsedNames);
536                         
537                         vector<string> validSecond;
538                         for (int i = 0; i < parsedNames.size(); i++) {
539                                 if (names.count(parsedNames[i]) != 0) {
540                                         validSecond.push_back(parsedNames[i]);
541                                 }
542                         }
543
544                         if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
545                                 for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]);  }
546                                 out << firstCol << '\t' << hold << endl;
547                                 wroteSomething = true;
548                         }else {
549                                 //if the name in the first column is in the set then print it and any other names in second column also in set
550                                 if (names.count(firstCol) != 0) {
551                                 
552                                         wroteSomething = true;
553                                         
554                                         out << firstCol << '\t';
555                                         
556                                         //you know you have at least one valid second since first column is valid
557                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
558                                         out << validSecond[validSecond.size()-1] << endl;
559                                         
560                                 
561                                 //make first name in set you come to first column and then add the remaining names to second column
562                                 }else {
563                                         //you want part of this row
564                                         if (validSecond.size() != 0) {
565                                         
566                                                 wroteSomething = true;
567                                                 
568                                                 out << validSecond[0] << '\t';
569                                         
570                                                 //you know you have at least one valid second since first column is valid
571                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
572                                                 out << validSecond[validSecond.size()-1] << endl;
573                                         }
574                                 }
575                         }
576                         m->gobble(in);
577                 }
578                 in.close();
579                 out.close();
580                 
581                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
582                 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
583                 
584                 return 0;
585                 
586         }
587         catch(exception& e) {
588                 m->errorOut(e, "GetSeqsCommand", "readName");
589                 exit(1);
590         }
591 }
592
593 //**********************************************************************************************************************
594 int GetSeqsCommand::readGroup(){
595         try {
596                 string thisOutputDir = outputDir;
597                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
598                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
599                 ofstream out;
600                 m->openOutputFile(outputFileName, out);
601                 
602
603                 ifstream in;
604                 m->openInputFile(groupfile, in);
605                 string name, group;
606                 
607                 bool wroteSomething = false;
608                 
609                 while(!in.eof()){
610
611                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
612
613
614                         in >> name;                             //read from first column
615                         in >> group;                    //read from second column
616                         
617                         //if this name is in the accnos file
618                         if (names.count(name) != 0) {
619                                 wroteSomething = true;
620                                 
621                                 out << name << '\t' << group << endl;
622                         }
623                                         
624                         m->gobble(in);
625                 }
626                 in.close();
627                 out.close();
628                 
629                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
630                 outputNames.push_back(outputFileName);  outputTypes["group"].push_back(outputFileName);
631                 
632                 return 0;
633
634         }
635         catch(exception& e) {
636                 m->errorOut(e, "GetSeqsCommand", "readGroup");
637                 exit(1);
638         }
639 }
640 //**********************************************************************************************************************
641 int GetSeqsCommand::readTax(){
642         try {
643                 string thisOutputDir = outputDir;
644                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
645                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
646                 ofstream out;
647                 m->openOutputFile(outputFileName, out);
648                 
649                 ifstream in;
650                 m->openInputFile(taxfile, in);
651                 string name, tax;
652                 
653                 bool wroteSomething = false;
654                 
655                 while(!in.eof()){
656
657                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
658
659                         in >> name;                             //read from first column
660                         in >> tax;                      //read from second column
661                         
662                         //if this name is in the accnos file
663                         if (names.count(name) != 0) {
664                                 wroteSomething = true;
665                                 
666                                 out << name << '\t' << tax << endl;
667                         }
668                                         
669                         m->gobble(in);
670                 }
671                 in.close();
672                 out.close();
673                 
674                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
675                 outputNames.push_back(outputFileName);  outputTypes["taxonomy"].push_back(outputFileName);
676                         
677                 return 0;
678
679         }
680         catch(exception& e) {
681                 m->errorOut(e, "GetSeqsCommand", "readTax");
682                 exit(1);
683         }
684 }
685 //**********************************************************************************************************************
686 //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
687 int GetSeqsCommand::readAlign(){
688         try {
689                 string thisOutputDir = outputDir;
690                 if (outputDir == "") {  thisOutputDir += m->hasPath(alignfile);  }
691                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
692                 ofstream out;
693                 m->openOutputFile(outputFileName, out);
694                 
695
696                 ifstream in;
697                 m->openInputFile(alignfile, in);
698                 string name, junk;
699                 
700                 bool wroteSomething = false;
701                 
702                 //read column headers
703                 for (int i = 0; i < 16; i++) {  
704                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
705                         else                    {       break;                  }
706                 }
707                 out << endl;
708                 
709                 while(!in.eof()){
710                 
711                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
712
713
714                         in >> name;                             //read from first column
715                         
716                         //if this name is in the accnos file
717                         if (names.count(name) != 0) {
718                                 wroteSomething = true;
719                                 
720                                 out << name << '\t';
721                                 
722                                 //read rest
723                                 for (int i = 0; i < 15; i++) {  
724                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
725                                         else                    {       break;                  }
726                                 }
727                                 out << endl;
728                                 
729                         }else {//still read just don't do anything with it
730                                 //read rest
731                                 for (int i = 0; i < 15; i++) {  
732                                         if (!in.eof())  {       in >> junk;             }
733                                         else                    {       break;                  }
734                                 }
735                         }
736                         
737                         m->gobble(in);
738                 }
739                 in.close();
740                 out.close();
741                 
742                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
743                 outputNames.push_back(outputFileName);  outputTypes["alignreport"].push_back(outputFileName);
744                 
745                 return 0;
746                 
747         }
748         catch(exception& e) {
749                 m->errorOut(e, "GetSeqsCommand", "readAlign");
750                 exit(1);
751         }
752 }
753 //**********************************************************************************************************************
754
755 int GetSeqsCommand::readAccnos(){
756         try {
757                 
758                 ifstream in;
759                 m->openInputFile(accnosfile, in);
760                 string name;
761                 
762                 while(!in.eof()){
763                         in >> name;
764                                                 
765                         names.insert(name);
766                         
767                         m->gobble(in);
768                 }
769                 in.close();     
770                 
771                 return 0;
772
773         }
774         catch(exception& e) {
775                 m->errorOut(e, "GetSeqsCommand", "readAccnos");
776                 exit(1);
777         }
778 }
779 //**********************************************************************************************************************
780
781 int GetSeqsCommand::compareAccnos(){
782         try {
783                 
784                 string thisOutputDir = outputDir;
785                 if (outputDir == "") {  thisOutputDir += m->hasPath(accnosfile);  }
786                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report";
787                 ofstream out;
788                 m->openOutputFile(outputFileName, out);
789                 
790                 ifstream in;
791                 m->openInputFile(accnosfile2, in);
792                 string name;
793                 
794                 set<string> namesAccnos2;
795                 set<string> namesDups;
796                 set<string> namesAccnos = names;
797                 
798                 map<string, int> nameCount;
799                 
800                 if (namefile != "") {
801                         ifstream inName;
802                         m->openInputFile(namefile, inName);
803                         
804                         
805                         while(!inName.eof()){
806                                 
807                                 if (m->control_pressed) { inName.close(); return 0; }
808                                 
809                                 string thisname, repnames;
810                                 
811                                 inName >> thisname;             m->gobble(inName);              //read from first column
812                                 inName >> repnames;                     //read from second column
813                                 
814                                 int num = m->getNumNames(repnames);
815                                 nameCount[thisname] = num;
816                                 
817                                 m->gobble(inName);
818                         }
819                         inName.close(); 
820                 }
821                 
822                 while(!in.eof()){
823                         in >> name;
824                         
825                         if (namesAccnos.count(name) == 0){ //name unique to accnos2
826                                 namesAccnos2.insert(name);
827                         }else { //you are in both so erase
828                                 namesAccnos.erase(name);
829                                 namesDups.insert(name);
830                         }
831                         
832                         m->gobble(in);
833                 }
834                 in.close();     
835                 
836                 out << "Names in both files : " + toString(namesDups.size()) << endl;
837                 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
838                 
839                 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
840                         out << (*it);
841                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
842                         out << endl;
843                 }
844                 
845                 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
846                 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
847                 
848                 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
849                         out << (*it);
850                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
851                         out << endl;
852                 }
853                 
854                 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
855                 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
856                 
857                 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
858                         out << (*it);
859                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
860                         out << endl;
861                 }
862
863                 out.close(); 
864                 
865                 outputNames.push_back(outputFileName);  outputTypes["accnosreport"].push_back(outputFileName);
866                 
867                 return 0;
868                 
869         }
870         catch(exception& e) {
871                 m->errorOut(e, "GetSeqsCommand", "readAccnos");
872                 exit(1);
873         }
874 }
875
876
877 //**********************************************************************************************************************
878