]> git.donarmstrong.com Git - mothur.git/blob - listseqscommand.cpp
added checks for ^C to quit command instead of program
[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                 if (m->control_pressed) { return 0; }
155                 
156                 //sort in alphabetical order
157                 sort(names.begin(), names.end());
158                 
159                 if (outputDir == "") {  outputDir += hasPath(inputFileName);  }
160                 
161                 string outputFileName = outputDir + getRootName(getSimpleName(inputFileName)) + "accnos";
162
163                 ofstream out;
164                 openOutputFile(outputFileName, out);
165                 
166                 //output to .accnos file
167                 for (int i = 0; i < names.size(); i++) {
168                         
169                         if (m->control_pressed) { out.close(); remove(outputFileName.c_str()); return 0; }
170                         
171                         out << names[i] << endl;
172                 }
173                 out.close();
174                 
175                 if (m->control_pressed) { remove(outputFileName.c_str()); return 0; }
176
177                 m->mothurOutEndLine();
178                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
179                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
180                 m->mothurOutEndLine();
181                 
182                 return 0;               
183         }
184
185         catch(exception& e) {
186                 m->errorOut(e, "ListSeqsCommand", "execute");
187                 exit(1);
188         }
189 }
190
191 //**********************************************************************************************************************
192 int ListSeqsCommand::readFasta(){
193         try {
194                 
195                 ifstream in;
196                 openInputFile(fastafile, in);
197                 string name;
198                 
199                 while(!in.eof()){
200                         
201                         if (m->control_pressed) { in.close(); return 0; }
202                         
203                         Sequence currSeq(in);
204                         name = currSeq.getName();
205                         
206                         if (name != "") {  names.push_back(name);  }
207                         
208                         gobble(in);
209                 }
210                 in.close();     
211                 
212                 return 0;
213
214         }
215         catch(exception& e) {
216                 m->errorOut(e, "ListSeqsCommand", "readFasta");
217                 exit(1);
218         }
219 }
220 //**********************************************************************************************************************
221 int ListSeqsCommand::readList(){
222         try {
223                 ifstream in;
224                 openInputFile(listfile, in);
225                 
226                 if(!in.eof()){
227                         //read in list vector
228                         ListVector list(in);
229                         
230                         //for each bin
231                         for (int i = 0; i < list.getNumBins(); i++) {
232                                 string binnames = list.get(i);
233                                 
234                                 if (m->control_pressed) { in.close(); return 0; }
235                                 
236                                 while (binnames.find_first_of(',') != -1) { 
237                                         string name = binnames.substr(0,binnames.find_first_of(','));
238                                         binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
239                                         names.push_back(name);
240                                 }
241                         
242                                 names.push_back(binnames);
243                         }
244                 }
245                 in.close();     
246                 
247                 return 0;
248                 
249         }
250         catch(exception& e) {
251                 m->errorOut(e, "ListSeqsCommand", "readList");
252                 exit(1);
253         }
254 }
255
256 //**********************************************************************************************************************
257 int ListSeqsCommand::readName(){
258         try {
259                 
260                 ifstream in;
261                 openInputFile(namefile, in);
262                 string name, firstCol, secondCol;
263                 
264                 while(!in.eof()){
265                 
266                         if (m->control_pressed) { in.close(); return 0; }
267
268                         in >> firstCol;                         
269                         in >> secondCol;                        
270                         
271                         //parse second column saving each name
272                         while (secondCol.find_first_of(',') != -1) { 
273                                 name = secondCol.substr(0,secondCol.find_first_of(','));
274                                 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
275                                 names.push_back(name);
276                         }
277                         
278                         //get name after last ,
279                         names.push_back(secondCol);
280                         
281                         gobble(in);
282                 }
283                 in.close();
284                 return 0;
285                 
286         }
287         catch(exception& e) {
288                 m->errorOut(e, "ListSeqsCommand", "readName");
289                 exit(1);
290         }
291 }
292
293 //**********************************************************************************************************************
294 int ListSeqsCommand::readGroup(){
295         try {
296         
297                 ifstream in;
298                 openInputFile(groupfile, in);
299                 string name, group;
300                 
301                 while(!in.eof()){
302                         
303                         if (m->control_pressed) { in.close(); return 0; }
304                         
305                         in >> name;                             //read from first column
306                         in >> group;                    //read from second column
307                         
308                         names.push_back(name);
309                                         
310                         gobble(in);
311                 }
312                 in.close();
313                 return 0;
314
315         }
316         catch(exception& e) {
317                 m->errorOut(e, "ListSeqsCommand", "readGroup");
318                 exit(1);
319         }
320 }
321
322 //**********************************************************************************************************************
323 //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
324 int ListSeqsCommand::readAlign(){
325         try {
326         
327                 ifstream in;
328                 openInputFile(alignfile, in);
329                 string name, junk;
330                 
331                 //read column headers
332                 for (int i = 0; i < 16; i++) {  
333                         if (!in.eof())  {       in >> junk;             }
334                         else                    {       break;                  }
335                 }
336                 
337                 
338                 while(!in.eof()){
339                 
340                         if (m->control_pressed) { in.close(); return 0; }
341
342                         in >> name;                             //read from first column
343                         
344                         //read rest
345                         for (int i = 0; i < 15; i++) {  
346                                 if (!in.eof())  {       in >> junk;             }
347                                 else                    {       break;                  }
348                         }
349                         
350                         names.push_back(name);
351                                         
352                         gobble(in);
353                 }
354                 in.close();
355                 
356                 return 0;
357
358                 
359         }
360         catch(exception& e) {
361                 m->errorOut(e, "ListSeqsCommand", "readAlign");
362                 exit(1);
363         }
364 }
365 //**********************************************************************************************************************