]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
changes while testing
[mothur.git] / listseqscommand.cpp
1 /*
2  *  listseqscommand.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 "listseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
13 #include "counttable.h"
14
15
16 //**********************************************************************************************************************
17 vector<string> ListSeqsCommand::setParameters(){        
18         try {
19                 CommandParameter pfasta("fasta", "InputTypes", "", "", "FNGLT", "FNGLT", "none","accnos",false,false,true); parameters.push_back(pfasta);
20                 CommandParameter pname("name", "InputTypes", "", "", "FNGLT", "FNGLT", "none","accnos",false,false,true); parameters.push_back(pname);
21         CommandParameter pcount("count", "InputTypes", "", "", "FNGLT", "FNGLT", "none","accnos",false,false,true); parameters.push_back(pcount);
22                 CommandParameter pgroup("group", "InputTypes", "", "", "FNGLT", "FNGLT", "none","accnos",false,false,true); parameters.push_back(pgroup);
23                 CommandParameter plist("list", "InputTypes", "", "", "FNGLT", "FNGLT", "none","accnos",false,false,true); parameters.push_back(plist);
24                 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "FNGLT", "FNGLT", "none","accnos",false,false,true); parameters.push_back(ptaxonomy);
25                 CommandParameter palignreport("alignreport", "InputTypes", "", "", "FNGLT", "FNGLT", "none","accnos",false,false); parameters.push_back(palignreport);
26                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
27                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
28                 
29                 vector<string> myArray;
30                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
31                 return myArray;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "ListSeqsCommand", "setParameters");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 string ListSeqsCommand::getHelpString(){        
40         try {
41                 string helpString = "";
42                 helpString += "The list.seqs command reads a fasta, name, group, count, list, taxonomy or alignreport file and outputs a .accnos file containing sequence names.\n";
43                 helpString += "The list.seqs command parameters are fasta, name, group, count, list, taxonomy and alignreport.  You must provide one of these parameters.\n";
44                 helpString += "The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n";
45                 helpString += "Example list.seqs(fasta=amazon.fasta).\n";
46                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
47                 return helpString;
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "ListSeqsCommand", "getHelpString");
51                 exit(1);
52         }
53 }
54 //**********************************************************************************************************************
55 string ListSeqsCommand::getOutputPattern(string type) {
56     try {
57         string pattern = "";
58         
59         if (type == "accnos") {  pattern = "[filename],accnos"; } 
60         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
61         
62         return pattern;
63     }
64     catch(exception& e) {
65         m->errorOut(e, "ListSeqsCommand", "getOutputPattern");
66         exit(1);
67     }
68 }
69 //**********************************************************************************************************************
70 ListSeqsCommand::ListSeqsCommand(){     
71         try {
72                 abort = true; calledHelp = true; 
73                 setParameters();
74                 vector<string> tempOutNames;
75                 outputTypes["accnos"] = tempOutNames;
76         }
77         catch(exception& e) {
78                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
79                 exit(1);
80         }
81 }
82 //**********************************************************************************************************************
83
84 ListSeqsCommand::ListSeqsCommand(string option)  {
85         try {
86                 abort = false; calledHelp = false;   
87                 
88                 //allow user to run help
89                 if(option == "help") { help(); abort = true; calledHelp = true; }
90                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
91                 else {
92                         vector<string> myArray = setParameters();
93                         
94                         OptionParser parser(option);
95                         map<string,string> parameters = parser.getParameters();
96                         
97                         ValidParameters validParameter;
98                         map<string,string>::iterator it;
99                         
100                         //check to make sure all parameters are valid for command
101                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
102                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
103                         }
104                         
105                         //initialize outputTypes
106                         vector<string> tempOutNames;
107                         outputTypes["accnos"] = tempOutNames;
108                         
109                         //if the user changes the output directory command factory will send this info to us in the output parameter 
110                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
111                         
112                         //if the user changes the input directory command factory will send this info to us in the output parameter 
113                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
114                         if (inputDir == "not found"){   inputDir = "";          }
115                         else {
116                                 string path;
117                                 it = parameters.find("alignreport");
118                                 //user has given a template file
119                                 if(it != parameters.end()){ 
120                                         path = m->hasPath(it->second);
121                                         //if the user has not given a path then, add inputdir. else leave path alone.
122                                         if (path == "") {       parameters["alignreport"] = inputDir + it->second;              }
123                                 }
124                                 
125                                 it = parameters.find("fasta");
126                                 //user has given a template file
127                                 if(it != parameters.end()){ 
128                                         path = m->hasPath(it->second);
129                                         //if the user has not given a path then, add inputdir. else leave path alone.
130                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
131                                 }
132                                 
133                                 it = parameters.find("list");
134                                 //user has given a template file
135                                 if(it != parameters.end()){ 
136                                         path = m->hasPath(it->second);
137                                         //if the user has not given a path then, add inputdir. else leave path alone.
138                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
139                                 }
140                                 
141                                 it = parameters.find("name");
142                                 //user has given a template file
143                                 if(it != parameters.end()){ 
144                                         path = m->hasPath(it->second);
145                                         //if the user has not given a path then, add inputdir. else leave path alone.
146                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
147                                 }
148                                 
149                                 it = parameters.find("group");
150                                 //user has given a template file
151                                 if(it != parameters.end()){ 
152                                         path = m->hasPath(it->second);
153                                         //if the user has not given a path then, add inputdir. else leave path alone.
154                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
155                                 }
156                                 
157                                 it = parameters.find("taxonomy");
158                                 //user has given a template file
159                                 if(it != parameters.end()){ 
160                                         path = m->hasPath(it->second);
161                                         //if the user has not given a path then, add inputdir. else leave path alone.
162                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
163                                 }
164                 
165                 it = parameters.find("count");
166                                 //user has given a template file
167                                 if(it != parameters.end()){ 
168                                         path = m->hasPath(it->second);
169                                         //if the user has not given a path then, add inputdir. else leave path alone.
170                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
171                                 }
172                         }
173
174                         //check for required parameters
175                         fastafile = validParameter.validFile(parameters, "fasta", true);
176                         if (fastafile == "not open") { abort = true; }
177                         else if (fastafile == "not found") {  fastafile = "";  }
178                         else { m->setFastaFile(fastafile); }
179                         
180                         namefile = validParameter.validFile(parameters, "name", true);
181                         if (namefile == "not open") { abort = true; }
182                         else if (namefile == "not found") {  namefile = "";  }  
183                         else { m->setNameFile(namefile); }
184                         
185                         groupfile = validParameter.validFile(parameters, "group", true);
186                         if (groupfile == "not open") { abort = true; }
187                         else if (groupfile == "not found") {  groupfile = "";  }        
188                         else { m->setGroupFile(groupfile); }
189                         
190                         alignfile = validParameter.validFile(parameters, "alignreport", true);
191                         if (alignfile == "not open") { abort = true; }
192                         else if (alignfile == "not found") {  alignfile = "";  }
193                         
194                         listfile = validParameter.validFile(parameters, "list", true);
195                         if (listfile == "not open") { abort = true; }
196                         else if (listfile == "not found") {  listfile = "";  }
197                         else { m->setListFile(listfile); }
198                         
199                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
200                         if (taxfile == "not open") { abort = true; }
201                         else if (taxfile == "not found") {  taxfile = "";  }
202                         else { m->setTaxonomyFile(taxfile); }
203             
204             countfile = validParameter.validFile(parameters, "count", true);
205                         if (countfile == "not open") { abort = true; }
206                         else if (countfile == "not found") {  countfile = "";  }
207                         else { m->setCountTableFile(countfile); }
208                         
209                         if ((countfile == "") && (fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == ""))  { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
210                         
211                         int okay = 1;
212                         if (outputDir != "") { okay++; }
213                         if (inputDir != "") { okay++; }
214                         
215                         if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true;  }
216                 }
217
218         }
219         catch(exception& e) {
220                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
221                 exit(1);
222         }
223 }
224 //**********************************************************************************************************************
225
226 int ListSeqsCommand::execute(){
227         try {
228                 
229                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
230                 
231                 //read functions fill names vector
232                 if (fastafile != "")            {       inputFileName = fastafile;      readFasta();    }
233                 else if (namefile != "")        {       inputFileName = namefile;       readName();             }
234                 else if (groupfile != "")       {       inputFileName = groupfile;      readGroup();    }
235                 else if (alignfile != "")       {       inputFileName = alignfile;      readAlign();    }
236                 else if (listfile != "")        {       inputFileName = listfile;       readList();             }
237                 else if (taxfile != "")         {       inputFileName = taxfile;        readTax();              }
238         else if (countfile != "")       {       inputFileName = countfile;      readCount();    }
239                 
240                 if (m->control_pressed) { outputTypes.clear();  return 0; }
241                 
242                 //sort in alphabetical order
243                 sort(names.begin(), names.end());
244                 
245                 if (outputDir == "") {  outputDir += m->hasPath(inputFileName);  }
246                 
247         map<string, string> variables; 
248         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputFileName));
249                 string outputFileName = getOutputFileName("accnos", variables);
250
251                 ofstream out;
252                 m->openOutputFile(outputFileName, out);
253                 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
254                 
255                 //output to .accnos file
256                 for (int i = 0; i < names.size(); i++) {
257                         
258                         if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(outputFileName); return 0; }
259                         
260                         out << names[i] << endl;
261                 }
262                 out.close();
263                 
264                 if (m->control_pressed) { outputTypes.clear();  m->mothurRemove(outputFileName); return 0; }
265                 
266                 m->setAccnosFile(outputFileName);
267                 
268                 m->mothurOutEndLine();
269                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
270                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
271                 m->mothurOutEndLine();
272                 
273                 //set accnos file as new current accnosfile
274                 string current = "";
275                 itTypes = outputTypes.find("accnos");
276                 if (itTypes != outputTypes.end()) {
277                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
278                 }
279                 
280                 return 0;               
281         }
282
283         catch(exception& e) {
284                 m->errorOut(e, "ListSeqsCommand", "execute");
285                 exit(1);
286         }
287 }
288
289 //**********************************************************************************************************************
290 int ListSeqsCommand::readFasta(){
291         try {
292                 
293                 ifstream in;
294                 m->openInputFile(fastafile, in);
295                 string name;
296                 
297                 //ofstream out;
298                 //string newFastaName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "numsAdded.fasta";
299                 //m->openOutputFile(newFastaName, out);
300                 int count = 1;
301                 //string lastName = "";
302                 
303                 while(!in.eof()){
304                         
305                         if (m->control_pressed) { in.close(); return 0; }
306                         
307                         Sequence currSeq(in);
308                         name = currSeq.getName();
309                         
310                         if (name != "") {  names.push_back(name);  }
311                         
312                         m->gobble(in);
313                         if (m->debug) { count++; cout << "[DEBUG]: count = " + toString(count) + ", name = " + currSeq.getName() + "\n"; }
314                 }
315                 in.close();     
316                 //out.close();
317                 
318                 return 0;
319
320         }
321         catch(exception& e) {
322                 m->errorOut(e, "ListSeqsCommand", "readFasta");
323                 exit(1);
324         }
325 }
326 //**********************************************************************************************************************
327 int ListSeqsCommand::readList(){
328         try {
329                 ifstream in;
330                 m->openInputFile(listfile, in);
331                 
332                 if(!in.eof()){
333                         //read in list vector
334                         ListVector list(in);
335                         
336                         //for each bin
337                         for (int i = 0; i < list.getNumBins(); i++) {
338                                 string binnames = list.get(i);
339                                 
340                                 if (m->control_pressed) { in.close(); return 0; }
341                                 
342                                 m->splitAtComma(binnames, names);
343                         }
344                 }
345                 in.close();     
346                 
347                 return 0;
348                 
349         }
350         catch(exception& e) {
351                 m->errorOut(e, "ListSeqsCommand", "readList");
352                 exit(1);
353         }
354 }
355
356 //**********************************************************************************************************************
357 int ListSeqsCommand::readName(){
358         try {
359                 
360                 ifstream in;
361                 m->openInputFile(namefile, in);
362                 string name, firstCol, secondCol;
363                 
364                 while(!in.eof()){
365                 
366                         if (m->control_pressed) { in.close(); return 0; }
367
368                         in >> firstCol;                         
369                         in >> secondCol;                        
370                         
371                         //parse second column saving each name
372                         m->splitAtComma(secondCol, names);
373                         
374                         m->gobble(in);
375                 }
376                 in.close();
377                 return 0;
378                 
379         }
380         catch(exception& e) {
381                 m->errorOut(e, "ListSeqsCommand", "readName");
382                 exit(1);
383         }
384 }
385
386 //**********************************************************************************************************************
387 int ListSeqsCommand::readGroup(){
388         try {
389         
390                 ifstream in;
391                 m->openInputFile(groupfile, in);
392                 string name, group;
393                 
394                 while(!in.eof()){
395                         
396                         if (m->control_pressed) { in.close(); return 0; }
397                         
398                         in >> name;     m->gobble(in);                  //read from first column
399                         in >> group;                    //read from second column
400                         
401                         names.push_back(name);
402                                         
403                         m->gobble(in);
404                 }
405                 in.close();
406                 return 0;
407
408         }
409         catch(exception& e) {
410                 m->errorOut(e, "ListSeqsCommand", "readGroup");
411                 exit(1);
412         }
413 }
414 //**********************************************************************************************************************
415 int ListSeqsCommand::readCount(){
416         try {
417                 CountTable ct;
418                 ct.readTable(countfile, false);
419         
420         if (m->control_pressed) { return 0; }
421         
422         names = ct.getNamesOfSeqs();
423         
424         return 0;
425         
426         }
427         catch(exception& e) {
428                 m->errorOut(e, "ListSeqsCommand", "readCount");
429                 exit(1);
430         }
431 }
432 //**********************************************************************************************************************
433 //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
434 int ListSeqsCommand::readAlign(){
435         try {
436         
437                 ifstream in;
438                 m->openInputFile(alignfile, in);
439                 string name, junk;
440                 
441                 //read column headers
442                 for (int i = 0; i < 16; i++) {  
443                         if (!in.eof())  {       in >> junk;             }
444                         else                    {       break;                  }
445                 }
446                 //m->getline(in);
447                 
448                 while(!in.eof()){
449                 
450                         if (m->control_pressed) { in.close(); return 0; }
451
452                         in >> name;                             //read from first column
453                         //m->getline(in);
454                         //read rest
455                         for (int i = 0; i < 15; i++) {  
456                                 if (!in.eof())  {       in >> junk;             }
457                                 else                    {       break;                  }
458                         }
459                         
460                         names.push_back(name);
461                                         
462                         m->gobble(in);
463                 }
464                 in.close();
465                 
466                 return 0;
467
468                 
469         }
470         catch(exception& e) {
471                 m->errorOut(e, "ListSeqsCommand", "readAlign");
472                 exit(1);
473         }
474 }
475 //**********************************************************************************************************************
476 int ListSeqsCommand::readTax(){
477         try {
478                 
479                 ifstream in;
480                 m->openInputFile(taxfile, in);
481                 string name, firstCol, secondCol;
482                 
483                 while(!in.eof()){
484                 
485                         if (m->control_pressed) { in.close(); return 0; }
486
487                         in >> firstCol;                         
488                         in >> secondCol;                        
489                         
490                         names.push_back(firstCol);
491                         
492                         m->gobble(in);
493                         
494                 }
495                 in.close();
496                 
497                 return 0;
498                 
499         }
500         catch(exception& e) {
501                 m->errorOut(e, "ListSeqsCommand", "readTax");
502                 exit(1);
503         }
504 }
505 //**********************************************************************************************************************