]> git.donarmstrong.com Git - mothur.git/blob - getseqscommand.cpp
fixed trim.seqs bug with qtrim parameter and added num=1 special case to database...
[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                         if (accnosfile2 == "not found") { accnosfile2 = ""; }
194                         
195                         fastafile = validParameter.validFile(parameters, "fasta", true);
196                         if (fastafile == "not open") { abort = true; }
197                         else if (fastafile == "not found") {  fastafile = "";  }        
198                         
199                         namefile = validParameter.validFile(parameters, "name", true);
200                         if (namefile == "not open") { abort = true; }
201                         else if (namefile == "not found") {  namefile = "";  }  
202                         
203                         groupfile = validParameter.validFile(parameters, "group", true);
204                         if (groupfile == "not open") { abort = true; }
205                         else if (groupfile == "not found") {  groupfile = "";  }        
206                         
207                         alignfile = validParameter.validFile(parameters, "alignreport", true);
208                         if (alignfile == "not open") { abort = true; }
209                         else if (alignfile == "not found") {  alignfile = "";  }
210                         
211                         listfile = validParameter.validFile(parameters, "list", true);
212                         if (listfile == "not open") { abort = true; }
213                         else if (listfile == "not found") {  listfile = "";  }
214                         
215                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
216                         if (taxfile == "not open") { abort = true; }
217                         else if (taxfile == "not found") {  taxfile = "";  }
218                         
219                         qualfile = validParameter.validFile(parameters, "qfile", true);
220                         if (qualfile == "not open") { abort = true; }
221                         else if (qualfile == "not found") {  qualfile = "";  }
222                         
223                         string usedDups = "true";
224                         string temp = validParameter.validFile(parameters, "dups", false);      if (temp == "not found") { temp = "false"; usedDups = ""; }
225                         dups = m->isTrue(temp);
226                         
227                         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; }
228                 
229                         if ((usedDups != "") && (namefile == "")) {  m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine();  abort = true; }                       
230
231                 }
232
233         }
234         catch(exception& e) {
235                 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
236                 exit(1);
237         }
238 }
239 //**********************************************************************************************************************
240
241 void GetSeqsCommand::help(){
242         try {
243                 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");
244                 m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n");
245                 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");
246                 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");
247                 m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
248                 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
249                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
250         }
251         catch(exception& e) {
252                 m->errorOut(e, "GetSeqsCommand", "help");
253                 exit(1);
254         }
255 }
256
257 //**********************************************************************************************************************
258
259 int GetSeqsCommand::execute(){
260         try {
261                 
262                 if (abort == true) { return 0; }
263                 
264                 //get names you want to keep
265                 readAccnos();
266                 
267                 if (m->control_pressed) { return 0; }
268                 
269                 //read through the correct file and output lines you want to keep
270                 if (namefile != "")                     {               readName();                     }
271                 if (fastafile != "")            {               readFasta();            }
272                 if (groupfile != "")            {               readGroup();            }
273                 if (alignfile != "")            {               readAlign();            }
274                 if (listfile != "")                     {               readList();                     }
275                 if (taxfile != "")                      {               readTax();                      }
276                 if (qualfile != "")                     {               readQual();                     }
277                 if (accnosfile2 != "")          {               compareAccnos();        }
278                 
279                 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {   remove(outputNames[i].c_str());  } return 0; }
280                 
281                 m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
282                 
283                 if (outputNames.size() != 0) {
284                         m->mothurOutEndLine();
285                         m->mothurOut("Output File Names: "); m->mothurOutEndLine();
286                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
287                         m->mothurOutEndLine();
288                 }
289                 
290                 return 0;               
291         }
292
293         catch(exception& e) {
294                 m->errorOut(e, "GetSeqsCommand", "execute");
295                 exit(1);
296         }
297 }
298
299 //**********************************************************************************************************************
300 int GetSeqsCommand::readFasta(){
301         try {
302                 string thisOutputDir = outputDir;
303                 if (outputDir == "") {  thisOutputDir += m->hasPath(fastafile);  }
304                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" +  m->getExtension(fastafile);
305                 ofstream out;
306                 m->openOutputFile(outputFileName, out);
307                 
308                 
309                 ifstream in;
310                 m->openInputFile(fastafile, in);
311                 string name;
312                 
313                 bool wroteSomething = false;
314                 
315                 while(!in.eof()){
316                 
317                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
318                         
319                         Sequence currSeq(in);
320                         name = currSeq.getName();
321                         
322                         if (name != "") {
323                                 //if this name is in the accnos file
324                                 if (names.count(name) != 0) {
325                                         wroteSomething = true;
326                                         
327                                         currSeq.printSequence(out);
328                                 }
329                         }
330                         m->gobble(in);
331                 }
332                 in.close();     
333                 out.close();
334                 
335                 
336                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
337                 outputNames.push_back(outputFileName);  outputTypes["fasta"].push_back(outputFileName); 
338                 
339                 return 0;
340
341         }
342         catch(exception& e) {
343                 m->errorOut(e, "GetSeqsCommand", "readFasta");
344                 exit(1);
345         }
346 }
347 //**********************************************************************************************************************
348 int GetSeqsCommand::readQual(){
349         try {
350                 string thisOutputDir = outputDir;
351                 if (outputDir == "") {  thisOutputDir += m->hasPath(qualfile);  }
352                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" +  m->getExtension(qualfile);
353                 ofstream out;
354                 m->openOutputFile(outputFileName, out);
355                 
356                 
357                 ifstream in;
358                 m->openInputFile(qualfile, in);
359                 string name;
360                 
361                 bool wroteSomething = false;
362                 
363                 
364                 while(!in.eof()){       
365                         string saveName = "";
366                         string name = "";
367                         string scores = "";
368                         
369                         in >> name; 
370                                 
371                         if (name.length() != 0) { 
372                                 saveName = name.substr(1);
373                                 while (!in.eof())       {       
374                                         char c = in.get(); 
375                                         if (c == 10 || c == 13){        break;  }
376                                         else { name += c; }     
377                                 } 
378                                 m->gobble(in);
379                         }
380                         
381                         while(in){
382                                 char letter= in.get();
383                                 if(letter == '>'){      in.putback(letter);     break;  }
384                                 else{ scores += letter; }
385                         }
386                         
387                         m->gobble(in);
388                         
389                         if (names.count(saveName) != 0) {
390                                 wroteSomething = true;
391                                                 
392                                 out << name << endl << scores;
393                         }
394                         
395                         m->gobble(in);
396                 }
397                 in.close();
398                 out.close();
399                 
400                 
401                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
402                 outputNames.push_back(outputFileName);  outputTypes["qfile"].push_back(outputFileName); 
403                 
404                 return 0;
405                 
406         }
407         catch(exception& e) {
408                 m->errorOut(e, "GetSeqsCommand", "readQual");
409                 exit(1);
410         }
411 }
412 //**********************************************************************************************************************
413 int GetSeqsCommand::readList(){
414         try {
415                 string thisOutputDir = outputDir;
416                 if (outputDir == "") {  thisOutputDir += m->hasPath(listfile);  }
417                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" +  m->getExtension(listfile);
418                 ofstream out;
419                 m->openOutputFile(outputFileName, out);
420                 
421                 ifstream in;
422                 m->openInputFile(listfile, in);
423                 
424                 bool wroteSomething = false;
425                 
426                 while(!in.eof()){
427                         
428                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
429
430                         //read in list vector
431                         ListVector list(in);
432                         
433                         //make a new list vector
434                         ListVector newList;
435                         newList.setLabel(list.getLabel());
436                         
437                         //for each bin
438                         for (int i = 0; i < list.getNumBins(); i++) {
439                         
440                                 //parse out names that are in accnos file
441                                 string binnames = list.get(i);
442                                 
443                                 string newNames = "";
444                                 while (binnames.find_first_of(',') != -1) { 
445                                         string name = binnames.substr(0,binnames.find_first_of(','));
446                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
447                                         
448                                         //if that name is in the .accnos file, add it
449                                         if (names.count(name) != 0) {  newNames += name + ",";  }
450                                 }
451                         
452                                 //get last name
453                                 if (names.count(binnames) != 0) {  newNames += binnames + ",";  }
454
455                                 //if there are names in this bin add to new list
456                                 if (newNames != "") { 
457                                         newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
458                                         newList.push_back(newNames);    
459                                 }
460                         }
461                                 
462                         //print new listvector
463                         if (newList.getNumBins() != 0) {
464                                 wroteSomething = true;
465                                 newList.print(out);
466                         }
467                         
468                         m->gobble(in);
469                 }
470                 in.close();     
471                 out.close();
472                 
473                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
474                 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
475                 
476                 return 0;
477
478         }
479         catch(exception& e) {
480                 m->errorOut(e, "GetSeqsCommand", "readList");
481                 exit(1);
482         }
483 }
484 //**********************************************************************************************************************
485 int GetSeqsCommand::readName(){
486         try {
487                 string thisOutputDir = outputDir;
488                 if (outputDir == "") {  thisOutputDir += m->hasPath(namefile);  }
489                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" +  m->getExtension(namefile);
490                 ofstream out;
491                 m->openOutputFile(outputFileName, out);
492                 
493
494                 ifstream in;
495                 m->openInputFile(namefile, in);
496                 string name, firstCol, secondCol;
497                 
498                 bool wroteSomething = false;
499                 
500                 
501                 while(!in.eof()){
502                 
503                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
504
505                         in >> firstCol;                         
506                         in >> secondCol;
507                         
508                         string hold = "";
509                         if (dups) { hold = secondCol; }
510                         
511                         vector<string> parsedNames;
512                         //parse second column saving each name
513                         while (secondCol.find_first_of(',') != -1) { 
514                                 name = secondCol.substr(0,secondCol.find_first_of(','));
515                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
516                                 parsedNames.push_back(name);
517                         }
518                         
519                         //get name after last ,
520                         parsedNames.push_back(secondCol);
521                         
522                         vector<string> validSecond;
523                         for (int i = 0; i < parsedNames.size(); i++) {
524                                 if (names.count(parsedNames[i]) != 0) {
525                                         validSecond.push_back(parsedNames[i]);
526                                 }
527                         }
528
529                         if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
530                                 for (int i = 0; i < parsedNames.size(); i++) {  names.insert(parsedNames[i]);  }
531                                 out << firstCol << '\t' << hold << endl;
532                                 wroteSomething = true;
533                         }else {
534                                 //if the name in the first column is in the set then print it and any other names in second column also in set
535                                 if (names.count(firstCol) != 0) {
536                                 
537                                         wroteSomething = true;
538                                         
539                                         out << firstCol << '\t';
540                                         
541                                         //you know you have at least one valid second since first column is valid
542                                         for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
543                                         out << validSecond[validSecond.size()-1] << endl;
544                                         
545                                 
546                                 //make first name in set you come to first column and then add the remaining names to second column
547                                 }else {
548                                         //you want part of this row
549                                         if (validSecond.size() != 0) {
550                                         
551                                                 wroteSomething = true;
552                                                 
553                                                 out << validSecond[0] << '\t';
554                                         
555                                                 //you know you have at least one valid second since first column is valid
556                                                 for (int i = 0; i < validSecond.size()-1; i++) {  out << validSecond[i] << ',';  }
557                                                 out << validSecond[validSecond.size()-1] << endl;
558                                         }
559                                 }
560                         }
561                         m->gobble(in);
562                 }
563                 in.close();
564                 out.close();
565                 
566                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
567                 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
568                 
569                 return 0;
570                 
571         }
572         catch(exception& e) {
573                 m->errorOut(e, "GetSeqsCommand", "readName");
574                 exit(1);
575         }
576 }
577
578 //**********************************************************************************************************************
579 int GetSeqsCommand::readGroup(){
580         try {
581                 string thisOutputDir = outputDir;
582                 if (outputDir == "") {  thisOutputDir += m->hasPath(groupfile);  }
583                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
584                 ofstream out;
585                 m->openOutputFile(outputFileName, out);
586                 
587
588                 ifstream in;
589                 m->openInputFile(groupfile, in);
590                 string name, group;
591                 
592                 bool wroteSomething = false;
593                 
594                 while(!in.eof()){
595
596                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
597
598
599                         in >> name;                             //read from first column
600                         in >> group;                    //read from second column
601                         
602                         //if this name is in the accnos file
603                         if (names.count(name) != 0) {
604                                 wroteSomething = true;
605                                 
606                                 out << name << '\t' << group << endl;
607                         }
608                                         
609                         m->gobble(in);
610                 }
611                 in.close();
612                 out.close();
613                 
614                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
615                 outputNames.push_back(outputFileName);  outputTypes["group"].push_back(outputFileName);
616                 
617                 return 0;
618
619         }
620         catch(exception& e) {
621                 m->errorOut(e, "GetSeqsCommand", "readGroup");
622                 exit(1);
623         }
624 }
625 //**********************************************************************************************************************
626 int GetSeqsCommand::readTax(){
627         try {
628                 string thisOutputDir = outputDir;
629                 if (outputDir == "") {  thisOutputDir += m->hasPath(taxfile);  }
630                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
631                 ofstream out;
632                 m->openOutputFile(outputFileName, out);
633                 
634                 ifstream in;
635                 m->openInputFile(taxfile, in);
636                 string name, tax;
637                 
638                 bool wroteSomething = false;
639                 
640                 while(!in.eof()){
641
642                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
643
644                         in >> name;                             //read from first column
645                         in >> tax;                      //read from second column
646                         
647                         //if this name is in the accnos file
648                         if (names.count(name) != 0) {
649                                 wroteSomething = true;
650                                 
651                                 out << name << '\t' << tax << endl;
652                         }
653                                         
654                         m->gobble(in);
655                 }
656                 in.close();
657                 out.close();
658                 
659                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
660                 outputNames.push_back(outputFileName);  outputTypes["taxonomy"].push_back(outputFileName);
661                         
662                 return 0;
663
664         }
665         catch(exception& e) {
666                 m->errorOut(e, "GetSeqsCommand", "readTax");
667                 exit(1);
668         }
669 }
670 //**********************************************************************************************************************
671 //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
672 int GetSeqsCommand::readAlign(){
673         try {
674                 string thisOutputDir = outputDir;
675                 if (outputDir == "") {  thisOutputDir += m->hasPath(alignfile);  }
676                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
677                 ofstream out;
678                 m->openOutputFile(outputFileName, out);
679                 
680
681                 ifstream in;
682                 m->openInputFile(alignfile, in);
683                 string name, junk;
684                 
685                 bool wroteSomething = false;
686                 
687                 //read column headers
688                 for (int i = 0; i < 16; i++) {  
689                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
690                         else                    {       break;                  }
691                 }
692                 out << endl;
693                 
694                 while(!in.eof()){
695                 
696                         if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str());  return 0; }
697
698
699                         in >> name;                             //read from first column
700                         
701                         //if this name is in the accnos file
702                         if (names.count(name) != 0) {
703                                 wroteSomething = true;
704                                 
705                                 out << name << '\t';
706                                 
707                                 //read rest
708                                 for (int i = 0; i < 15; i++) {  
709                                         if (!in.eof())  {       in >> junk;      out << junk << '\t';   }
710                                         else                    {       break;                  }
711                                 }
712                                 out << endl;
713                                 
714                         }else {//still read just don't do anything with it
715                                 //read rest
716                                 for (int i = 0; i < 15; i++) {  
717                                         if (!in.eof())  {       in >> junk;             }
718                                         else                    {       break;                  }
719                                 }
720                         }
721                         
722                         m->gobble(in);
723                 }
724                 in.close();
725                 out.close();
726                 
727                 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine();  }
728                 outputNames.push_back(outputFileName);  outputTypes["alignreport"].push_back(outputFileName);
729                 
730                 return 0;
731                 
732         }
733         catch(exception& e) {
734                 m->errorOut(e, "GetSeqsCommand", "readAlign");
735                 exit(1);
736         }
737 }
738 //**********************************************************************************************************************
739
740 int GetSeqsCommand::readAccnos(){
741         try {
742                 
743                 ifstream in;
744                 m->openInputFile(accnosfile, in);
745                 string name;
746                 
747                 while(!in.eof()){
748                         in >> name;
749                                                 
750                         names.insert(name);
751                         
752                         m->gobble(in);
753                 }
754                 in.close();     
755                 
756                 return 0;
757
758         }
759         catch(exception& e) {
760                 m->errorOut(e, "GetSeqsCommand", "readAccnos");
761                 exit(1);
762         }
763 }
764 //**********************************************************************************************************************
765
766 int GetSeqsCommand::compareAccnos(){
767         try {
768                 
769                 string thisOutputDir = outputDir;
770                 if (outputDir == "") {  thisOutputDir += m->hasPath(accnosfile);  }
771                 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report";
772                 ofstream out;
773                 m->openOutputFile(outputFileName, out);
774                 
775                 ifstream in;
776                 m->openInputFile(accnosfile2, in);
777                 string name;
778                 
779                 set<string> namesAccnos2;
780                 set<string> namesDups;
781                 set<string> namesAccnos = names;
782                 
783                 map<string, int> nameCount;
784                 
785                 if (namefile != "") {
786                         ifstream inName;
787                         m->openInputFile(namefile, inName);
788                         
789                         
790                         while(!inName.eof()){
791                                 
792                                 if (m->control_pressed) { inName.close(); return 0; }
793                                 
794                                 string thisname, repnames;
795                                 
796                                 inName >> thisname;             m->gobble(inName);              //read from first column
797                                 inName >> repnames;                     //read from second column
798                                 
799                                 int num = m->getNumNames(repnames);
800                                 nameCount[thisname] = num;
801                                 
802                                 m->gobble(inName);
803                         }
804                         inName.close(); 
805                 }
806                 
807                 while(!in.eof()){
808                         in >> name;
809                         
810                         if (namesAccnos.count(name) == 0){ //name unique to accnos2
811                                 namesAccnos2.insert(name);
812                         }else { //you are in both so erase
813                                 namesAccnos.erase(name);
814                                 namesDups.insert(name);
815                         }
816                         
817                         m->gobble(in);
818                 }
819                 in.close();     
820                 
821                 out << "Names in both files : " + toString(namesDups.size()) << endl;
822                 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
823                 
824                 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
825                         out << (*it);
826                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
827                         out << endl;
828                 }
829                 
830                 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
831                 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
832                 
833                 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
834                         out << (*it);
835                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
836                         out << endl;
837                 }
838                 
839                 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
840                 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
841                 
842                 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
843                         out << (*it);
844                         if (namefile != "") { out << '\t' << nameCount[(*it)]; }
845                         out << endl;
846                 }
847
848                 out.close(); 
849                 
850                 outputNames.push_back(outputFileName);  outputTypes["accnosreport"].push_back(outputFileName);
851                 
852                 return 0;
853                 
854         }
855         catch(exception& e) {
856                 m->errorOut(e, "GetSeqsCommand", "readAccnos");
857                 exit(1);
858         }
859 }
860
861
862 //**********************************************************************************************************************
863