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