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