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