]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[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\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                 
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                         
155                         namefile = validParameter.validFile(parameters, "name", true);
156                         if (namefile == "not open") { abort = true; }
157                         else if (namefile == "not found") {  namefile = "";  }  
158                         
159                         groupfile = validParameter.validFile(parameters, "group", true);
160                         if (groupfile == "not open") { abort = true; }
161                         else if (groupfile == "not found") {  groupfile = "";  }        
162                         
163                         alignfile = validParameter.validFile(parameters, "alignreport", true);
164                         if (alignfile == "not open") { abort = true; }
165                         else if (alignfile == "not found") {  alignfile = "";  }
166                         
167                         listfile = validParameter.validFile(parameters, "list", true);
168                         if (listfile == "not open") { abort = true; }
169                         else if (listfile == "not found") {  listfile = "";  }
170
171                         taxfile = validParameter.validFile(parameters, "taxonomy", true);
172                         if (taxfile == "not open") { abort = true; }
173                         else if (taxfile == "not found") {  taxfile = "";  }
174                         
175                         if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "") && (taxfile == ""))  { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
176                         
177                         int okay = 1;
178                         if (outputDir != "") { okay++; }
179                         if (inputDir != "") { okay++; }
180                         
181                         if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true;  }
182                 }
183
184         }
185         catch(exception& e) {
186                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
187                 exit(1);
188         }
189 }
190 //**********************************************************************************************************************
191
192 int ListSeqsCommand::execute(){
193         try {
194                 
195                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
196                 
197                 //read functions fill names vector
198                 if (fastafile != "")            {       inputFileName = fastafile;      readFasta();    }
199                 else if (namefile != "")        {       inputFileName = namefile;       readName();             }
200                 else if (groupfile != "")       {       inputFileName = groupfile;      readGroup();    }
201                 else if (alignfile != "")       {       inputFileName = alignfile;      readAlign();    }
202                 else if (listfile != "")        {       inputFileName = listfile;       readList();             }
203                 else if (taxfile != "")         {       inputFileName = taxfile;        readTax();              }
204                 
205                 if (m->control_pressed) { outputTypes.clear();  return 0; }
206                 
207                 //sort in alphabetical order
208                 sort(names.begin(), names.end());
209                 
210                 if (outputDir == "") {  outputDir += m->hasPath(inputFileName);  }
211                 
212                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "accnos";
213
214                 ofstream out;
215                 m->openOutputFile(outputFileName, out);
216                 outputNames.push_back(outputFileName); outputTypes["accnos"].push_back(outputFileName);
217                 
218                 //output to .accnos file
219                 for (int i = 0; i < names.size(); i++) {
220                         
221                         if (m->control_pressed) { outputTypes.clear(); out.close(); remove(outputFileName.c_str()); return 0; }
222                         
223                         out << names[i] << endl;
224                 }
225                 out.close();
226                 
227                 if (m->control_pressed) { outputTypes.clear();  remove(outputFileName.c_str()); return 0; }
228                 
229                 m->setAccnosFile(outputFileName);
230                 
231                 m->mothurOutEndLine();
232                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
233                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
234                 m->mothurOutEndLine();
235                 
236                 //set accnos file as new current accnosfile
237                 string current = "";
238                 itTypes = outputTypes.find("accnos");
239                 if (itTypes != outputTypes.end()) {
240                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
241                 }
242                 
243                 return 0;               
244         }
245
246         catch(exception& e) {
247                 m->errorOut(e, "ListSeqsCommand", "execute");
248                 exit(1);
249         }
250 }
251
252 //**********************************************************************************************************************
253 int ListSeqsCommand::readFasta(){
254         try {
255                 
256                 ifstream in;
257                 m->openInputFile(fastafile, in);
258                 string name;
259                 
260                 while(!in.eof()){
261                         
262                         if (m->control_pressed) { in.close(); return 0; }
263                         
264                         Sequence currSeq(in);
265                         name = currSeq.getName();
266                         
267                         if (name != "") {  names.push_back(name);  }
268                         
269                         m->gobble(in);
270                 }
271                 in.close();     
272                 
273                 return 0;
274
275         }
276         catch(exception& e) {
277                 m->errorOut(e, "ListSeqsCommand", "readFasta");
278                 exit(1);
279         }
280 }
281 //**********************************************************************************************************************
282 int ListSeqsCommand::readList(){
283         try {
284                 ifstream in;
285                 m->openInputFile(listfile, in);
286                 
287                 if(!in.eof()){
288                         //read in list vector
289                         ListVector list(in);
290                         
291                         //for each bin
292                         for (int i = 0; i < list.getNumBins(); i++) {
293                                 string binnames = list.get(i);
294                                 
295                                 if (m->control_pressed) { in.close(); return 0; }
296                                 
297                                 while (binnames.find_first_of(',') != -1) { 
298                                         string name = binnames.substr(0,binnames.find_first_of(','));
299                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
300                                         names.push_back(name);
301                                 }
302                         
303                                 names.push_back(binnames);
304                         }
305                 }
306                 in.close();     
307                 
308                 return 0;
309                 
310         }
311         catch(exception& e) {
312                 m->errorOut(e, "ListSeqsCommand", "readList");
313                 exit(1);
314         }
315 }
316
317 //**********************************************************************************************************************
318 int ListSeqsCommand::readName(){
319         try {
320                 
321                 ifstream in;
322                 m->openInputFile(namefile, in);
323                 string name, firstCol, secondCol;
324                 
325                 while(!in.eof()){
326                 
327                         if (m->control_pressed) { in.close(); return 0; }
328
329                         in >> firstCol;                         
330                         in >> secondCol;                        
331                         
332                         //parse second column saving each name
333                         while (secondCol.find_first_of(',') != -1) { 
334                                 name = secondCol.substr(0,secondCol.find_first_of(','));
335                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
336                                 names.push_back(name);
337                         }
338                         
339                         //get name after last ,
340                         names.push_back(secondCol);
341                         
342                         m->gobble(in);
343                 }
344                 in.close();
345                 return 0;
346                 
347         }
348         catch(exception& e) {
349                 m->errorOut(e, "ListSeqsCommand", "readName");
350                 exit(1);
351         }
352 }
353
354 //**********************************************************************************************************************
355 int ListSeqsCommand::readGroup(){
356         try {
357         
358                 ifstream in;
359                 m->openInputFile(groupfile, in);
360                 string name, group;
361                 
362                 while(!in.eof()){
363                         
364                         if (m->control_pressed) { in.close(); return 0; }
365                         
366                         in >> name;     m->gobble(in);                  //read from first column
367                         in >> group;                    //read from second column
368                         
369                         names.push_back(name);
370                                         
371                         m->gobble(in);
372                 }
373                 in.close();
374                 return 0;
375
376         }
377         catch(exception& e) {
378                 m->errorOut(e, "ListSeqsCommand", "readGroup");
379                 exit(1);
380         }
381 }
382
383 //**********************************************************************************************************************
384 //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
385 int ListSeqsCommand::readAlign(){
386         try {
387         
388                 ifstream in;
389                 m->openInputFile(alignfile, in);
390                 string name, junk;
391                 
392                 //read column headers
393                 for (int i = 0; i < 16; i++) {  
394                         if (!in.eof())  {       in >> junk;             }
395                         else                    {       break;                  }
396                 }
397                 
398                 
399                 while(!in.eof()){
400                 
401                         if (m->control_pressed) { in.close(); return 0; }
402
403                         in >> name;                             //read from first column
404                         
405                         //read rest
406                         for (int i = 0; i < 15; i++) {  
407                                 if (!in.eof())  {       in >> junk;             }
408                                 else                    {       break;                  }
409                         }
410                         
411                         names.push_back(name);
412                                         
413                         m->gobble(in);
414                 }
415                 in.close();
416                 
417                 return 0;
418
419                 
420         }
421         catch(exception& e) {
422                 m->errorOut(e, "ListSeqsCommand", "readAlign");
423                 exit(1);
424         }
425 }
426 //**********************************************************************************************************************
427 int ListSeqsCommand::readTax(){
428         try {
429                 
430                 ifstream in;
431                 m->openInputFile(taxfile, in);
432                 string name, firstCol, secondCol;
433                 
434                 while(!in.eof()){
435                 
436                         if (m->control_pressed) { in.close(); return 0; }
437
438                         in >> firstCol;                         
439                         in >> secondCol;                        
440                         
441                         names.push_back(firstCol);
442                         
443                         m->gobble(in);
444                         
445                 }
446                 in.close();
447                 
448                 return 0;
449                 
450         }
451         catch(exception& e) {
452                 m->errorOut(e, "ListSeqsCommand", "readTax");
453                 exit(1);
454         }
455 }
456 //**********************************************************************************************************************