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