]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
fixed some issues while testing 1.6
[mothur.git] / getsabundcommand.cpp
1 /*
2  *  getsabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "getsabundcommand.h"
11
12 //**********************************************************************************************************************
13
14 GetSAbundCommand::GetSAbundCommand(string option){
15         try {
16                 globaldata = GlobalData::getInstance();
17                 abort = false;
18                 allLines = 1;
19                 lines.clear();
20                 labels.clear();
21                 
22                 //allow user to run help
23                 if(option == "help") { help(); abort = true; }
24                 
25                 else {
26                         //valid paramters for this command
27                         string Array[] =  {"line","label"};
28                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
29                         
30                         OptionParser parser(option);
31                         map<string,string> parameters = parser.getParameters();
32                         
33                         ValidParameters validParameter;
34                         
35                         //check to make sure all parameters are valid for command
36                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
37                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
38                         }
39                         
40                         //make sure the user has already run the read.otu command
41                         if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { mothurOut("You must read a list or rabund before you can use the get.sabund command."); mothurOutEndLine(); abort = true; }
42                         
43                         //check for optional parameter and set defaults
44                         // ...at some point should added some additional type checking...
45                         line = validParameter.validFile(parameters, "line", false);                             
46                         if (line == "not found") { line = "";  }
47                         else { 
48                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
49                                 else { allLines = 1;  }
50                         }
51                         
52                         label = validParameter.validFile(parameters, "label", false);                   
53                         if (label == "not found") { label = ""; }
54                         else { 
55                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
56                                 else { allLines = 1;  }
57                         }
58                         
59                         //make sure user did not use both the line and label parameters
60                         if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
61                         //if the user has not specified any line or labels use the ones from read.otu
62                         else if((line == "") && (label == "")) {  
63                                 allLines = globaldata->allLines; 
64                                 labels = globaldata->labels; 
65                                 lines = globaldata->lines;
66                         }
67                                 
68                         if (abort == false) {
69                                 filename = getRootName(globaldata->inputFileName) + "sabund";
70                                 openOutputFile(filename, out);
71                         }
72                 }
73
74         }
75         catch(exception& e) {
76                 errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
77                 exit(1);
78         }
79 }
80 //**********************************************************************************************************************
81
82 void GetSAbundCommand::help(){
83         try {
84                 mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
85                 mothurOut("The get.sabund command parameters are line and label.  No parameters are required, and you may not use line and label at the same time.\n");
86                 mothurOut("The line and label allow you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n");
87                 mothurOut("The get.sabund command should be in the following format: get.sabund(line=yourLines, label=yourLabels).\n");
88                 mothurOut("Example get.sabund(line=1-3-5).\n");
89                 mothurOut("The default value for line and label are all lines in your inputfile.\n");
90                 mothurOut("The get.sabund command outputs a .sabund file containing the lines you selected.\n");
91                 mothurOut("Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines).\n\n");
92         }
93         catch(exception& e) {
94                 errorOut(e, "GetSAbundCommand", "help");
95                 exit(1);
96         }
97 }
98
99 //**********************************************************************************************************************
100
101 GetSAbundCommand::~GetSAbundCommand(){
102 }
103
104 //**********************************************************************************************************************
105
106 int GetSAbundCommand::execute(){
107         try {
108                 
109                 if (abort == true) { return 0; }
110         
111                 int count = 1;
112                 
113                 //using order vector so you don't have to distinguish between the list and rabund files
114                 read = new ReadOTUFile(globaldata->inputFileName);      
115                 read->read(&*globaldata); 
116                 
117                 order = globaldata->gorder;
118                 string lastLabel = order->getLabel();
119                 input = globaldata->ginput;
120                                                 
121                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
122                 set<string> processedLabels;
123                 set<string> userLabels = labels;
124                 set<int> userLines = lines;
125
126                 
127                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
128                         
129                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
130                                         mothurOut(order->getLabel());  mothurOutEndLine();
131                                         sabund = new SAbundVector();
132                                         *sabund = (order->getSAbundVector());
133                                         sabund->print(out);
134                                         delete sabund;
135
136                                         processedLabels.insert(order->getLabel());
137                                         userLabels.erase(order->getLabel());
138                                         userLines.erase(count);
139                         }
140                         
141                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
142                                         delete order;           
143                                         order = (input->getOrderVector(lastLabel));
144                                         
145                                         mothurOut(order->getLabel());  mothurOutEndLine();
146                                         sabund = new SAbundVector();
147                                         *sabund = (order->getSAbundVector());
148                                         sabund->print(out);
149                                         delete sabund;
150
151                                         processedLabels.insert(order->getLabel());
152                                         userLabels.erase(order->getLabel());
153                         }
154                         
155                         
156                         lastLabel = order->getLabel();  
157                         
158                         delete order;           
159                         order = (input->getOrderVector());
160                         count++;
161                 }
162                 
163                 //output error messages about any remaining user labels
164                 set<string>::iterator it;
165                 bool needToRun = false;
166                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
167                         mothurOut("Your file does not include the label " + *it); 
168                         if (processedLabels.count(lastLabel) != 1) {
169                                 mothurOut(". I will use " + lastLabel + ".");  mothurOutEndLine();
170                                 needToRun = true;
171                         }else {
172                                 mothurOut(". Please refer to " + lastLabel + ".");  mothurOutEndLine();
173                         }
174                 }
175                 
176                 //run last line if you need to
177                 if (needToRun == true)  {
178                         if (order != NULL) {    delete order;   }
179                         order = (input->getOrderVector(lastLabel));
180                         
181                         mothurOut(order->getLabel());  mothurOutEndLine();
182                         sabund = new SAbundVector();
183                         *sabund = (order->getSAbundVector());
184                         sabund->print(out);
185                         delete sabund;
186                         delete order;
187                 }
188                 globaldata->gorder = NULL;
189
190                 out.close();
191                 return 0;               
192         }
193
194         catch(exception& e) {
195                 errorOut(e, "GetSAbundCommand", "execute");
196                 exit(1);
197         }
198 }
199
200 //**********************************************************************************************************************
201
202