]> git.donarmstrong.com Git - mothur.git/blob - binsequencecommand.cpp
added checks for ^C to quit command instead of program
[mothur.git] / binsequencecommand.cpp
1 /*
2  *  binsequencecommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/3/09.
6  *  Copyright 2009 Schloss Lab UMASS Amhers. All rights reserved.
7  *
8  */
9
10 #include "binsequencecommand.h"
11
12 //**********************************************************************************************************************
13 BinSeqCommand::BinSeqCommand(string option) {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                 allLines = 1;
18                 labels.clear();
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 AlignArray[] =  {"fasta","label","name", "group","outputdir","inputdir"};
26                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/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 (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"){  
41                                 outputDir = ""; 
42                                 outputDir += hasPath(globaldata->getListFile()); //if user entered a file with a path then preserve it  
43                         }
44
45                         
46                         //make sure the user has already run the read.otu command
47                         if (globaldata->getListFile() == "") { 
48                                 m->mothurOut("You must read a listfile before running the bin.seqs command."); 
49                                 m->mothurOutEndLine(); 
50                                 abort = true; 
51                         }
52                         
53                         //if the user changes the input directory command factory will send this info to us in the output parameter 
54                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
55                         if (inputDir == "not found"){   inputDir = "";          }
56                         else {
57                                 string path;
58                                 it = parameters.find("fasta");
59                                 //user has given a template file
60                                 if(it != parameters.end()){ 
61                                         path = hasPath(it->second);
62                                         //if the user has not given a path then, add inputdir. else leave path alone.
63                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
64                                 }
65                                 
66                                 it = parameters.find("name");
67                                 //user has given a template file
68                                 if(it != parameters.end()){ 
69                                         path = hasPath(it->second);
70                                         //if the user has not given a path then, add inputdir. else leave path alone.
71                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
72                                 }
73                                 
74                                 it = parameters.find("group");
75                                 //user has given a template file
76                                 if(it != parameters.end()){ 
77                                         path = hasPath(it->second);
78                                         //if the user has not given a path then, add inputdir. else leave path alone.
79                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
80                                 }
81                         }
82
83                         
84                         //check for required parameters
85                         fastafile = validParameter.validFile(parameters, "fasta", true);
86                         if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the bin.seqs command.");  m->mothurOutEndLine(); abort = true; }
87                         else if (fastafile == "not open") { abort = true; }     
88                 
89                         //check for optional parameter and set defaults
90                         // ...at some point should added some additional type checking...
91                         
92                         label = validParameter.validFile(parameters, "label", false);                   
93                         if (label == "not found") { label = ""; }
94                         else { 
95                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
96                                 else { allLines = 1;  }
97                         }
98                         
99                         //if the user has not specified any labels use the ones from read.otu
100                         if (label == "") {  
101                                 allLines = globaldata->allLines; 
102                                 labels = globaldata->labels; 
103                         }
104                         
105                         namesfile = validParameter.validFile(parameters, "name", true);
106                         if (namesfile == "not open") { abort = true; }  
107                         else if (namesfile == "not found") { namesfile = ""; }
108
109                         groupfile = validParameter.validFile(parameters, "group", true);
110                         if (groupfile == "not open") { abort = true; }
111                         else if (groupfile == "not found") { groupfile = ""; }
112                         
113                         if (abort == false) { 
114 //                              openInputFile(fastafile, in);
115                                 fasta = new FastaMap();
116                                 if (groupfile != "") {
117                                         groupMap = new GroupMap(groupfile);
118                                         
119                                         int error = groupMap->readMap();
120                                         if (error == 1) { delete groupMap; abort = true; }
121                                 }
122                         }
123         
124                 }
125         }
126         catch(exception& e) {
127                 m->errorOut(e, "BinSeqCommand", "BinSeqCommand");
128                 exit(1);
129         }
130 }
131 //**********************************************************************************************************************
132
133 void BinSeqCommand::help(){
134         try {
135                 m->mothurOut("The bin.seqs command can only be executed after a successful read.otu command of a listfile.\n");
136                 m->mothurOut("The bin.seqs command parameters are fasta, name, label and group.  The fasta parameter is required.\n");
137                 m->mothurOut("The label parameter allows you to select what distance levels you would like a output files created for, and are separated by dashes.\n");
138                 m->mothurOut("The bin.seqs command should be in the following format: bin.seqs(fasta=yourFastaFile, name=yourNamesFile, group=yourGroupFile, label=yourLabels).\n");
139                 m->mothurOut("Example bin.seqs(fasta=amazon.fasta, group=amazon.groups, name=amazon.names).\n");
140                 m->mothurOut("The default value for label is all lines in your inputfile.\n");
141                 m->mothurOut("The bin.seqs command outputs a .fasta file for each distance you specify appending the OTU number to each name.\n");
142                 m->mothurOut("If you provide a groupfile, then it also appends the sequences group to the name.\n");
143                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
144         }
145         catch(exception& e) {
146                 m->errorOut(e, "BinSeqCommand", "help");
147                 exit(1);
148         }
149 }
150
151 //**********************************************************************************************************************
152
153 BinSeqCommand::~BinSeqCommand(){
154         //made new in execute
155         if (abort == false) {
156                 delete input;  globaldata->ginput = NULL;
157                 delete read;
158                 globaldata->gListVector = NULL;
159                 delete fasta;
160                 if (groupfile != "") {  delete groupMap;  globaldata->gGroupmap = NULL; }
161         }
162 }
163
164 //**********************************************************************************************************************
165
166 int BinSeqCommand::execute(){
167         try {
168                 if (abort == true) {    return 0;       }
169         
170                 int error = 0;
171                 
172                 //read fastafile
173                 fasta->readFastaFile(fastafile);
174                 
175                 
176                 //set format to list so input can get listvector
177 //              globaldata->setFormat("list");
178                 
179                 //if user gave a namesfile then use it
180                 if (namesfile != "") {
181                         readNamesFile();
182                 }
183                 
184                 
185                 //read list file
186                 read = new ReadOTUFile(globaldata->getListFile());      
187                 read->read(&*globaldata); 
188                 
189                 input = globaldata->ginput;
190                 list = globaldata->gListVector;
191                 string lastLabel = list->getLabel();
192                 
193                 if (m->control_pressed) {  return 0; }
194                 
195                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
196                 set<string> processedLabels;
197                 set<string> userLabels = labels;
198
199                                 
200                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
201                         
202                         if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
203                         
204                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
205                                 
206                                 error = process(list);  
207                                 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }   
208                                                         
209                                 processedLabels.insert(list->getLabel());
210                                 userLabels.erase(list->getLabel());
211                         }
212                         
213                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
214                                 string saveLabel = list->getLabel();
215                                 
216                                 delete list;
217                                 list = input->getListVector(lastLabel);
218                                 
219                                 error = process(list);  
220                                 if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }
221                                                                                                         
222                                 processedLabels.insert(list->getLabel());
223                                 userLabels.erase(list->getLabel());
224                                 
225                                 //restore real lastlabel to save below
226                                 list->setLabel(saveLabel);
227                         }
228                         
229                         lastLabel = list->getLabel();                   
230                         
231                         delete list;
232                         list = input->getListVector();
233                 }
234                 
235                 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
236
237                 //output error messages about any remaining user labels
238                 set<string>::iterator it;
239                 bool needToRun = false;
240                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
241                         m->mothurOut("Your file does not include the label " + *it); 
242                         if (processedLabels.count(lastLabel) != 1) {
243                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
244                                 needToRun = true;
245                         }else {
246                                 m->mothurOut(". Please refer to " + lastLabel + ".");  m->mothurOutEndLine();
247                         }
248                 }
249                 
250                 //run last label if you need to
251                 if (needToRun == true)  {
252                         if (list != NULL) {             delete list;    }
253                         list = input->getListVector(lastLabel);
254                                 
255                         error = process(list);  
256                         if (error == 1) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }
257                         
258                         delete list;  
259                 }
260                 
261                 if(m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str());         } return 0; }   
262
263                 m->mothurOutEndLine();
264                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
265                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
266                 m->mothurOutEndLine();
267
268                 
269                 return 0;
270         }
271         catch(exception& e) {
272                 m->errorOut(e, "BinSeqCommand", "execute");
273                 exit(1);
274         }
275 }
276
277 //**********************************************************************************************************************
278 void BinSeqCommand::readNamesFile() {
279         try {
280                 vector<string> dupNames;
281                 openInputFile(namesfile, inNames);
282                 
283                 string name, names, sequence;
284         
285                 while(inNames){
286                         inNames >> name;                        //read from first column  A
287                         inNames >> names;               //read from second column  A,B,C,D
288                         
289                         dupNames.clear();
290                         
291                         //parse names into vector
292                         splitAtComma(names, dupNames);
293                         
294                         //store names in fasta map
295                         sequence = fasta->getSequence(name);
296                         for (int i = 0; i < dupNames.size(); i++) {
297                                 fasta->push_back(dupNames[i], sequence);
298                         }
299                 
300                         gobble(inNames);
301                 }
302                 inNames.close();
303
304         }
305         catch(exception& e) {
306                 m->errorOut(e, "BinSeqCommand", "readNamesFile");
307                 exit(1);
308         }
309 }
310 //**********************************************************************************************************************
311 //return 1 if error, 0 otherwise
312 int BinSeqCommand::process(ListVector* list) {
313         try {
314                                 string binnames, name, sequence;
315                                 
316                                 string outputFileName = outputDir + getRootName(getSimpleName(globaldata->getListFile())) + list->getLabel() + ".fasta";
317                                 openOutputFile(outputFileName, out);
318                                 
319                                 //save to output list of output file names
320                                 outputNames.push_back(outputFileName);
321
322                                 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
323                                 
324                                 //for each bin in the list vector
325                                 for (int i = 0; i < list->size(); i++) {
326                                         
327                                         if (m->control_pressed) {  return 1; }
328                                         
329                                         binnames = list->get(i);
330                                         while (binnames.find_first_of(',') != -1) { 
331                                                 name = binnames.substr(0,binnames.find_first_of(','));
332                                                 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
333                                                 
334                                                 //do work for that name
335                                                 sequence = fasta->getSequence(name);
336                                                 if (sequence != "not found") {
337                                                         //if you don't have groups
338                                                         if (groupfile == "") {
339                                                                 name = name + "|" + toString(i+1);
340                                                                 out << ">" << name << endl;
341                                                                 out << sequence << endl;
342                                                         }else {//if you do have groups
343                                                                 string group = groupMap->getGroup(name);
344                                                                 if (group == "not found") {  
345                                                                         m->mothurOut(name + " is missing from your group file. Please correct. ");  m->mothurOutEndLine();
346                                                                         return 1;
347                                                                 }else{
348                                                                         name = name + "|" + group + "|" + toString(i+1);
349                                                                         out << ">" << name << endl;
350                                                                         out << sequence << endl;
351                                                                 }
352                                                         }
353                                                 }else { 
354                                                         m->mothurOut(name + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
355                                                         return 1;
356                                                 }
357                                                 
358                                         }
359                                         
360                                         //get last name
361                                         sequence = fasta->getSequence(binnames);
362                                         if (sequence != "not found") {
363                                                 //if you don't have groups
364                                                 if (groupfile == "") {
365                                                         binnames = binnames + "|" + toString(i+1);
366                                                         out << ">" << binnames << endl;
367                                                         out << sequence << endl;
368                                                 }else {//if you do have groups
369                                                         string group = groupMap->getGroup(binnames);
370                                                         if (group == "not found") {  
371                                                                 m->mothurOut(binnames + " is missing from your group file. Please correct. "); m->mothurOutEndLine();
372                                                                 return 1;
373                                                         }else{
374                                                                 binnames = binnames + "|" + group + "|" + toString(i+1);
375                                                                 out << ">" << binnames << endl;
376                                                                 out << sequence << endl;
377                                                         }
378                                                 }
379                                         }else { 
380                                                 m->mothurOut(binnames + " is missing from your fasta or name file. Please correct. "); m->mothurOutEndLine();
381                                                 return 1;
382                                         }
383                                 }
384                                         
385                                 out.close();
386                                 return 0;
387
388         }
389         catch(exception& e) {
390                 m->errorOut(e, "BinSeqCommand", "process");
391                 exit(1);
392         }
393 }
394 //**********************************************************************************************************************
395
396