]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
created mothurOut class to handle logfiles
[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 == ""))  { m->mothurOut("You must provide a file."); m->mothurOutEndLine(); abort = true; }
111                         
112                         int okay = 1;
113                         if (outputDir != "") { okay++; }
114                         
115                         if (parameters.size() > okay) { m->mothurOut("You may only enter one file."); m->mothurOutEndLine(); abort = true;  }
116                 }
117
118         }
119         catch(exception& e) {
120                 m->errorOut(e, "ListSeqsCommand", "ListSeqsCommand");
121                 exit(1);
122         }
123 }
124 //**********************************************************************************************************************
125
126 void ListSeqsCommand::help(){
127         try {
128                 m->mothurOut("The list.seqs command reads a fasta, name, group, list or alignreport file and outputs a .accnos file containing sequence names.\n");
129                 m->mothurOut("The list.seqs command parameters are fasta, name, group and alignreport.  You must provide one of these parameters.\n");
130                 m->mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n");
131                 m->mothurOut("Example list.seqs(fasta=amazon.fasta).\n");
132                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
133         }
134         catch(exception& e) {
135                 m->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                 m->mothurOutEndLine();
171                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
172                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
173                 m->mothurOutEndLine();
174                 
175                 return 0;               
176         }
177
178         catch(exception& e) {
179                 m->errorOut(e, "ListSeqsCommand", "execute");
180                 exit(1);
181         }
182 }
183
184 //**********************************************************************************************************************
185 void ListSeqsCommand::readFasta(){
186         try {
187                 
188                 ifstream in;
189                 openInputFile(fastafile, in);
190                 string name;
191                 
192                 while(!in.eof()){
193                         Sequence currSeq(in);
194                         name = currSeq.getName();
195                         
196                         if (name != "") {  names.push_back(name);  }
197                         
198                         gobble(in);
199                 }
200                 in.close();             
201
202         }
203         catch(exception& e) {
204                 m->errorOut(e, "ListSeqsCommand", "readFasta");
205                 exit(1);
206         }
207 }
208 //**********************************************************************************************************************
209 void ListSeqsCommand::readList(){
210         try {
211                 ifstream in;
212                 openInputFile(listfile, in);
213                 
214                 if(!in.eof()){
215                         //read in list vector
216                         ListVector list(in);
217                         
218                         //for each bin
219                         for (int i = 0; i < list.getNumBins(); i++) {
220                                 string binnames = list.get(i);
221                                 
222                                 while (binnames.find_first_of(',') != -1) { 
223                                         string name = binnames.substr(0,binnames.find_first_of(','));
224                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
225                                         names.push_back(name);
226                                 }
227                         
228                                 names.push_back(binnames);
229                         }
230                 }
231                 in.close();     
232                 
233         }
234         catch(exception& e) {
235                 m->errorOut(e, "ListSeqsCommand", "readList");
236                 exit(1);
237         }
238 }
239
240 //**********************************************************************************************************************
241 void ListSeqsCommand::readName(){
242         try {
243                 
244                 ifstream in;
245                 openInputFile(namefile, in);
246                 string name, firstCol, secondCol;
247                 
248                 while(!in.eof()){
249
250                         in >> firstCol;                         
251                         in >> secondCol;                        
252                         
253                         //parse second column saving each name
254                         while (secondCol.find_first_of(',') != -1) { 
255                                 name = secondCol.substr(0,secondCol.find_first_of(','));
256                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
257                                 names.push_back(name);
258                         }
259                         
260                         //get name after last ,
261                         names.push_back(secondCol);
262                         
263                         gobble(in);
264                 }
265                 in.close();
266                 
267         }
268         catch(exception& e) {
269                 m->errorOut(e, "ListSeqsCommand", "readName");
270                 exit(1);
271         }
272 }
273
274 //**********************************************************************************************************************
275 void ListSeqsCommand::readGroup(){
276         try {
277         
278                 ifstream in;
279                 openInputFile(groupfile, in);
280                 string name, group;
281                 
282                 while(!in.eof()){
283
284                         in >> name;                             //read from first column
285                         in >> group;                    //read from second column
286                         
287                         names.push_back(name);
288                                         
289                         gobble(in);
290                 }
291                 in.close();
292
293         }
294         catch(exception& e) {
295                 m->errorOut(e, "ListSeqsCommand", "readGroup");
296                 exit(1);
297         }
298 }
299
300 //**********************************************************************************************************************
301 //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
302 void ListSeqsCommand::readAlign(){
303         try {
304         
305                 ifstream in;
306                 openInputFile(alignfile, in);
307                 string name, junk;
308                 
309                 //read column headers
310                 for (int i = 0; i < 16; i++) {  
311                         if (!in.eof())  {       in >> junk;             }
312                         else                    {       break;                  }
313                 }
314                 
315                 
316                 while(!in.eof()){
317
318                         in >> name;                             //read from first column
319                         
320                         //read rest
321                         for (int i = 0; i < 15; i++) {  
322                                 if (!in.eof())  {       in >> junk;             }
323                                 else                    {       break;                  }
324                         }
325                         
326                         names.push_back(name);
327                                         
328                         gobble(in);
329                 }
330                 in.close();
331
332                 
333         }
334         catch(exception& e) {
335                 m->errorOut(e, "ListSeqsCommand", "readAlign");
336                 exit(1);
337         }
338 }
339 //**********************************************************************************************************************