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