]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
1.19.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                 
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                 //ofstream out;
261                 //string newFastaName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "numsAdded.fasta";
262                 //m->openOutputFile(newFastaName, out);
263                 //int count = 1;
264                 //string lastName = "";
265                 
266                 while(!in.eof()){
267                         
268                         if (m->control_pressed) { in.close(); return 0; }
269                         
270                         Sequence currSeq(in);
271                         name = currSeq.getName();
272                         //if (lastName == "") { lastName = name; }
273                         //if (name != lastName) { count = 1; }
274                 //      lastName = name;
275                         
276                         //Sequence newSeq(name+"_"+toString(count), currSeq.getAligned());
277                         //newSeq.printSequence(out);
278                         
279                         if (name != "") {  names.push_back(name);  }
280                         
281                         m->gobble(in);
282                         //count++;
283                 }
284                 in.close();     
285                 //out.close();
286                 
287                 return 0;
288
289         }
290         catch(exception& e) {
291                 m->errorOut(e, "ListSeqsCommand", "readFasta");
292                 exit(1);
293         }
294 }
295 //**********************************************************************************************************************
296 int ListSeqsCommand::readList(){
297         try {
298                 ifstream in;
299                 m->openInputFile(listfile, in);
300                 
301                 if(!in.eof()){
302                         //read in list vector
303                         ListVector list(in);
304                         
305                         //for each bin
306                         for (int i = 0; i < list.getNumBins(); i++) {
307                                 string binnames = list.get(i);
308                                 
309                                 if (m->control_pressed) { in.close(); return 0; }
310                                 
311                                 while (binnames.find_first_of(',') != -1) { 
312                                         string name = binnames.substr(0,binnames.find_first_of(','));
313                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
314                                         names.push_back(name);
315                                 }
316                         
317                                 names.push_back(binnames);
318                         }
319                 }
320                 in.close();     
321                 
322                 return 0;
323                 
324         }
325         catch(exception& e) {
326                 m->errorOut(e, "ListSeqsCommand", "readList");
327                 exit(1);
328         }
329 }
330
331 //**********************************************************************************************************************
332 int ListSeqsCommand::readName(){
333         try {
334                 
335                 ifstream in;
336                 m->openInputFile(namefile, in);
337                 string name, firstCol, secondCol;
338                 
339                 while(!in.eof()){
340                 
341                         if (m->control_pressed) { in.close(); return 0; }
342
343                         in >> firstCol;                         
344                         in >> secondCol;                        
345                         
346                         //parse second column saving each name
347                         while (secondCol.find_first_of(',') != -1) { 
348                                 name = secondCol.substr(0,secondCol.find_first_of(','));
349                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
350                                 names.push_back(name);
351                         }
352                         
353                         //get name after last ,
354                         names.push_back(secondCol);
355                         
356                         m->gobble(in);
357                 }
358                 in.close();
359                 return 0;
360                 
361         }
362         catch(exception& e) {
363                 m->errorOut(e, "ListSeqsCommand", "readName");
364                 exit(1);
365         }
366 }
367
368 //**********************************************************************************************************************
369 int ListSeqsCommand::readGroup(){
370         try {
371         
372                 ifstream in;
373                 m->openInputFile(groupfile, in);
374                 string name, group;
375                 
376                 while(!in.eof()){
377                         
378                         if (m->control_pressed) { in.close(); return 0; }
379                         
380                         in >> name;     m->gobble(in);                  //read from first column
381                         in >> group;                    //read from second column
382                         
383                         names.push_back(name);
384                                         
385                         m->gobble(in);
386                 }
387                 in.close();
388                 return 0;
389
390         }
391         catch(exception& e) {
392                 m->errorOut(e, "ListSeqsCommand", "readGroup");
393                 exit(1);
394         }
395 }
396
397 //**********************************************************************************************************************
398 //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
399 int ListSeqsCommand::readAlign(){
400         try {
401         
402                 ifstream in;
403                 m->openInputFile(alignfile, in);
404                 string name, junk;
405                 
406                 //read column headers
407                 for (int i = 0; i < 16; i++) {  
408                         if (!in.eof())  {       in >> junk;             }
409                         else                    {       break;                  }
410                 }
411                 
412                 
413                 while(!in.eof()){
414                 
415                         if (m->control_pressed) { in.close(); return 0; }
416
417                         in >> name;                             //read from first column
418                         
419                         //read rest
420                         for (int i = 0; i < 15; i++) {  
421                                 if (!in.eof())  {       in >> junk;             }
422                                 else                    {       break;                  }
423                         }
424                         
425                         names.push_back(name);
426                                         
427                         m->gobble(in);
428                 }
429                 in.close();
430                 
431                 return 0;
432
433                 
434         }
435         catch(exception& e) {
436                 m->errorOut(e, "ListSeqsCommand", "readAlign");
437                 exit(1);
438         }
439 }
440 //**********************************************************************************************************************
441 int ListSeqsCommand::readTax(){
442         try {
443                 
444                 ifstream in;
445                 m->openInputFile(taxfile, in);
446                 string name, firstCol, secondCol;
447                 
448                 while(!in.eof()){
449                 
450                         if (m->control_pressed) { in.close(); return 0; }
451
452                         in >> firstCol;                         
453                         in >> secondCol;                        
454                         
455                         names.push_back(firstCol);
456                         
457                         m->gobble(in);
458                         
459                 }
460                 in.close();
461                 
462                 return 0;
463                 
464         }
465         catch(exception& e) {
466                 m->errorOut(e, "ListSeqsCommand", "readTax");
467                 exit(1);
468         }
469 }
470 //**********************************************************************************************************************