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