]> git.donarmstrong.com Git - mothur.git/blob - getrabundcommand.cpp
added sorted option to get.rabund command
[mothur.git] / getrabundcommand.cpp
1 /*
2  *  getrabundcommand.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 "getrabundcommand.h"
11
12 //**********************************************************************************************************************
13
14 GetRAbundCommand::GetRAbundCommand(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","sorted"};
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() == "") { mothurOut("You must read a listfile before you can use the get.rabund command."); mothurOutEndLine(); abort = true; }
42                         
43                         //check for optional parameter and set defaults
44                         // ...at some point should added some additional type checking...
45                         
46                         string temp;
47                         temp = validParameter.validFile(parameters, "sorted", false);                   if (temp == "not found") { temp = "T"; }
48                         sorted = isTrue(temp);
49                         
50                         line = validParameter.validFile(parameters, "line", false);                             
51                         if (line == "not found") { line = "";  }
52                         else { 
53                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
54                                 else { allLines = 1;  }
55                         }
56                         
57                         label = validParameter.validFile(parameters, "label", false);                   
58                         if (label == "not found") { label = ""; }
59                         else { 
60                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
61                                 else { allLines = 1;  }
62                         }
63                         
64                         //make sure user did not use both the line and label parameters
65                         if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
66                         //if the user has not specified any line or labels use the ones from read.otu
67                         else if((line == "") && (label == "")) {  
68                                 allLines = globaldata->allLines; 
69                                 labels = globaldata->labels; 
70                                 lines = globaldata->lines;
71                         }
72                                 
73                         if (abort == false) {
74                                 filename = getRootName(globaldata->inputFileName) + "rabund";
75                                 openOutputFile(filename, out);
76                         }
77                 }
78
79         }
80         catch(exception& e) {
81                 errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
82                 exit(1);
83         }                       
84 }
85 //**********************************************************************************************************************
86
87 void GetRAbundCommand::help(){
88         try {
89                 mothurOut("The get.rabund command can only be executed after a successful read.otu of a listfile.\n");
90                 mothurOut("The get.rabund command parameters are line, label and sorted.  No parameters are required, and you may not use line and label at the same time.\n");
91                 mothurOut("The line and label allow you to select what distance levels you would like included in your .rabund file, and are separated by dashes.\n");
92                 mothurOut("The sorted parameters allows you to print the rabund results sorted by abundance or not.  The default is sorted.\n");
93                 mothurOut("The get.rabund command should be in the following format: get.rabund(line=yourLines, label=yourLabels, sorted=yourSorted).\n");
94                 mothurOut("Example get.rabund(line=1-3-5, sorted=F).\n");
95                 mothurOut("The default value for line and label are all lines in your inputfile.\n");
96                 mothurOut("The get.rabund command outputs a .rabund file containing the lines you selected.\n");
97                 mothurOut("Note: No spaces between parameter labels (i.e. line), '=' and parameters (i.e.yourLines).\n\n");
98         }
99         catch(exception& e) {
100                 errorOut(e, "GetRAbundCommand", "help");
101                 exit(1);
102         }
103 }
104
105 //**********************************************************************************************************************
106
107 GetRAbundCommand::~GetRAbundCommand(){
108         if (abort == false) {  globaldata->gListVector = NULL; }
109 }
110
111 //**********************************************************************************************************************
112
113 int GetRAbundCommand::execute(){
114         try {
115         
116                 if (abort == true) { return 0; }
117                 
118                 int count = 1;
119                 
120                 //read first line
121                 read = new ReadOTUFile(globaldata->inputFileName);      
122                 read->read(&*globaldata); 
123                         
124                 input = globaldata->ginput;
125                 list = globaldata->gListVector;
126                 string lastLabel = list->getLabel();
127                 
128                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
129                 set<string> processedLabels;
130                 set<string> userLabels = labels;
131                 set<int> userLines = lines;
132
133                 
134                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
135                         
136                         if(allLines == 1 || lines.count(count) == 1 || labels.count(list->getLabel()) == 1){
137                                         mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
138                                         rabund = new RAbundVector();                            
139                                         *rabund = (list->getRAbundVector());
140                                         
141                                         if(sorted)      {   rabund->print(out);                         }
142                                         else            {       rabund->nonSortedPrint(out);    }
143                                         
144                                         delete rabund;
145                                                                                                                         
146                                         processedLabels.insert(list->getLabel());
147                                         userLabels.erase(list->getLabel());
148                                         userLines.erase(count);
149                         }
150                         
151                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
152                                         delete list;
153                                         list = input->getListVector(lastLabel);
154                                         
155                                         mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
156                                         rabund = new RAbundVector();
157                                         *rabund = (list->getRAbundVector());
158                                         
159                                         if(sorted)      {   rabund->print(out);                         }
160                                         else            {       rabund->nonSortedPrint(out);    }
161
162                                         delete rabund;
163
164                                         processedLabels.insert(list->getLabel());
165                                         userLabels.erase(list->getLabel());
166                         }
167                         
168                         lastLabel = list->getLabel();           
169                         
170                         delete list;
171                         list = input->getListVector();
172                         count++;
173                 }
174                 
175                 //output error messages about any remaining user labels
176                 set<string>::iterator it;
177                 bool needToRun = false;
178                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
179                         mothurOut("Your file does not include the label " + *it); 
180                         if (processedLabels.count(lastLabel) != 1) {
181                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
182                                 needToRun = true;
183                         }else {
184                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
185                         }
186                 }
187                 
188                 //run last line if you need to
189                 if (needToRun == true)  {
190                         delete list;
191                         list = input->getListVector(lastLabel);
192                         
193                         mothurOut(list->getLabel() + "\t" + toString(count)); mothurOutEndLine();
194                         rabund = new RAbundVector();
195                         *rabund = (list->getRAbundVector());
196                         
197                         if(sorted)      {   rabund->print(out);                         }
198                         else            {       rabund->nonSortedPrint(out);    }
199
200                         delete rabund;
201                         delete list;
202                 }
203
204                 out.close(); 
205                 return 0;               
206         }
207
208         catch(exception& e) {
209                 errorOut(e, "GetRAbundCommand", "execute");
210                 exit(1);
211         }
212 }
213
214 //**********************************************************************************************************************
215
216