]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[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","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 = 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 = 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 = 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 = 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 = 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
88                         //check for required parameters
89                         fastafile = validParameter.validFile(parameters, "fasta", true);
90                         if (fastafile == "not open") { abort = true; }
91                         else if (fastafile == "not found") {  fastafile = "";  }        
92                         
93                         namefile = validParameter.validFile(parameters, "name", true);
94                         if (namefile == "not open") { abort = true; }
95                         else if (namefile == "not found") {  namefile = "";  }  
96                         
97                         groupfile = validParameter.validFile(parameters, "group", true);
98                         if (groupfile == "not open") { abort = true; }
99                         else if (groupfile == "not found") {  groupfile = "";  }        
100                         
101                         alignfile = validParameter.validFile(parameters, "alignreport", true);
102                         if (alignfile == "not open") { abort = true; }
103                         else if (alignfile == "not found") {  alignfile = "";  }
104                         
105                         listfile = validParameter.validFile(parameters, "list", true);
106                         if (listfile == "not open") { abort = true; }
107                         else if (listfile == "not found") {  listfile = "";  }
108
109                         
110                         if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == ""))  { mothurOut("You must provide a file."); mothurOutEndLine(); abort = true; }
111                         
112                         int okay = 1;
113                         if (outputDir != "") { okay++; }
114                         
115                         if (parameters.size() > okay) { mothurOut("You may only enter one file."); mothurOutEndLine(); abort = true;  }
116                 }
117
118         }
119         catch(exception& e) {
120                 errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
121                 exit(1);
122         }
123 }
124 //**********************************************************************************************************************
125
126 void ListSeqsCommand::help(){
127         try {
128                 mothurOut("The list.seqs command reads a fasta, name, group, list or alignreport file and outputs a .accnos file containing sequence names.\n");
129                 mothurOut("The list.seqs command parameters are fasta, name, group and alignreport.  You must provide one of these parameters.\n");
130                 mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
131                 mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
132                 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
133         }
134         catch(exception& e) {
135                 errorOut(e, "ListSeqsCommand", "help");
136                 exit(1);
137         }
138 }
139
140 //**********************************************************************************************************************
141
142 int ListSeqsCommand::execute(){
143         try {
144                 
145                 if (abort == true) { return 0; }
146                 
147                 //read functions fill names vector
148                 if (fastafile != "")            {       inputFileName = fastafile;      readFasta();    }
149                 else if (namefile != "")        {       inputFileName = namefile;       readName();             }
150                 else if (groupfile != "")       {       inputFileName = groupfile;      readGroup();    }
151                 else if (alignfile != "")       {       inputFileName = alignfile;      readAlign();    }
152                 else if (listfile != "")        {       inputFileName = listfile;       readList();             }
153                 
154                 //sort in alphabetical order
155                 sort(names.begin(), names.end());
156                 
157                 if (outputDir == "") {  outputDir += hasPath(inputFileName);  }
158                 
159                 string outputFileName = outputDir + getRootName(getSimpleName(inputFileName)) + "accnos";
160
161                 ofstream out;
162                 openOutputFile(outputFileName, out);
163                 
164                 //output to .accnos file
165                 for (int i = 0; i < names.size(); i++) {
166                         out << names[i] << endl;
167                 }
168                 out.close();
169                 
170                 return 0;               
171         }
172
173         catch(exception& e) {
174                 errorOut(e, "ListSeqsCommand", "execute");
175                 exit(1);
176         }
177 }
178
179 //**********************************************************************************************************************
180 void ListSeqsCommand::readFasta(){
181         try {
182                 
183                 ifstream in;
184                 openInputFile(fastafile, in);
185                 string name;
186                 
187                 while(!in.eof()){
188                         Sequence currSeq(in);
189                         name = currSeq.getName();
190                         
191                         if (name != "") {  names.push_back(name);  }
192                         
193                         gobble(in);
194                 }
195                 in.close();             
196
197         }
198         catch(exception& e) {
199                 errorOut(e, "ListSeqsCommand", "readFasta");
200                 exit(1);
201         }
202 }
203 //**********************************************************************************************************************
204 void ListSeqsCommand::readList(){
205         try {
206                 ifstream in;
207                 openInputFile(listfile, in);
208                 
209                 if(!in.eof()){
210                         //read in list vector
211                         ListVector list(in);
212                         
213                         //for each bin
214                         for (int i = 0; i < list.getNumBins(); i++) {
215                                 string binnames = list.get(i);
216                                 
217                                 while (binnames.find_first_of(',') != -1) { 
218                                         string name = binnames.substr(0,binnames.find_first_of(','));
219                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
220                                         names.push_back(name);
221                                 }
222                         
223                                 names.push_back(binnames);
224                         }
225                 }
226                 in.close();     
227                 
228         }
229         catch(exception& e) {
230                 errorOut(e, "ListSeqsCommand", "readList");
231                 exit(1);
232         }
233 }
234
235 //**********************************************************************************************************************
236 void ListSeqsCommand::readName(){
237         try {
238                 
239                 ifstream in;
240                 openInputFile(namefile, in);
241                 string name, firstCol, secondCol;
242                 
243                 while(!in.eof()){
244
245                         in >> firstCol;                         
246                         in >> secondCol;                        
247                         
248                         //parse second column saving each name
249                         while (secondCol.find_first_of(',') != -1) { 
250                                 name = secondCol.substr(0,secondCol.find_first_of(','));
251                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
252                                 names.push_back(name);
253                         }
254                         
255                         //get name after last ,
256                         names.push_back(secondCol);
257                         
258                         gobble(in);
259                 }
260                 in.close();
261                 
262         }
263         catch(exception& e) {
264                 errorOut(e, "ListSeqsCommand", "readName");
265                 exit(1);
266         }
267 }
268
269 //**********************************************************************************************************************
270 void ListSeqsCommand::readGroup(){
271         try {
272         
273                 ifstream in;
274                 openInputFile(groupfile, in);
275                 string name, group;
276                 
277                 while(!in.eof()){
278
279                         in >> name;                             //read from first column
280                         in >> group;                    //read from second column
281                         
282                         names.push_back(name);
283                                         
284                         gobble(in);
285                 }
286                 in.close();
287
288         }
289         catch(exception& e) {
290                 errorOut(e, "ListSeqsCommand", "readGroup");
291                 exit(1);
292         }
293 }
294
295 //**********************************************************************************************************************
296 //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
297 void ListSeqsCommand::readAlign(){
298         try {
299         
300                 ifstream in;
301                 openInputFile(alignfile, in);
302                 string name, junk;
303                 
304                 //read column headers
305                 for (int i = 0; i < 16; i++) {  
306                         if (!in.eof())  {       in >> junk;             }
307                         else                    {       break;                  }
308                 }
309                 
310                 
311                 while(!in.eof()){
312
313                         in >> name;                             //read from first column
314                         
315                         //read rest
316                         for (int i = 0; i < 15; i++) {  
317                                 if (!in.eof())  {       in >> junk;             }
318                                 else                    {       break;                  }
319                         }
320                         
321                         names.push_back(name);
322                                         
323                         gobble(in);
324                 }
325                 in.close();
326
327                 
328         }
329         catch(exception& e) {
330                 errorOut(e, "ListSeqsCommand", "readAlign");
331                 exit(1);
332         }
333 }
334 //**********************************************************************************************************************