]> git.donarmstrong.com Git - mothur.git/blob - getrabundcommand.cpp
2ed5b7be71e13b5369f4cc71aaab72258960d41f
[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                 labels.clear();
20                 
21                 //allow user to run help
22                 if(option == "help") { help(); abort = true; }
23                 
24                 else {
25                         //valid paramters for this command
26                         string Array[] =  {"label","sorted"};
27                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28                         
29                         OptionParser parser(option);
30                         map<string,string> parameters = parser.getParameters();
31                         
32                         ValidParameters validParameter;
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                         //make sure the user has already run the read.otu command
40                         if (globaldata->getListFile() == "") { mothurOut("You must read a listfile before you can use the get.rabund command."); mothurOutEndLine(); abort = true; }
41                         
42                         //check for optional parameter and set defaults
43                         // ...at some point should added some additional type checking...
44                         
45                         string temp;
46                         temp = validParameter.validFile(parameters, "sorted", false);                   if (temp == "not found") { temp = "T"; }
47                         sorted = isTrue(temp);
48                         
49                         label = validParameter.validFile(parameters, "label", false);                   
50                         if (label == "not found") { label = ""; }
51                         else { 
52                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
53                                 else { allLines = 1;  }
54                         }
55                         
56                         //if the user has not specified any labels use the ones from read.otu
57                         if(label == "") {  
58                                 allLines = globaldata->allLines; 
59                                 labels = globaldata->labels; 
60                         }
61                                 
62                         if (abort == false) {
63                                 filename = getRootName(globaldata->inputFileName) + "rabund";
64                                 openOutputFile(filename, out);
65                         }
66                 }
67
68         }
69         catch(exception& e) {
70                 errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
71                 exit(1);
72         }                       
73 }
74 //**********************************************************************************************************************
75
76 void GetRAbundCommand::help(){
77         try {
78                 mothurOut("The get.rabund command can only be executed after a successful read.otu of a listfile.\n");
79                 mothurOut("The get.rabund command parameters are label and sorted.  No parameters are required.\n");
80                 mothurOut("The label parameter allows you to select what distance levels you would like included in your .rabund file, and are separated by dashes.\n");
81                 mothurOut("The sorted parameters allows you to print the rabund results sorted by abundance or not.  The default is sorted.\n");
82                 mothurOut("The get.rabund command should be in the following format: get.rabund(label=yourLabels, sorted=yourSorted).\n");
83                 mothurOut("Example get.rabund(sorted=F).\n");
84                 mothurOut("The default value for label is all labels in your inputfile.\n");
85                 mothurOut("The get.rabund command outputs a .rabund file containing the lines you selected.\n");
86                 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
87         }
88         catch(exception& e) {
89                 errorOut(e, "GetRAbundCommand", "help");
90                 exit(1);
91         }
92 }
93
94 //**********************************************************************************************************************
95
96 GetRAbundCommand::~GetRAbundCommand(){
97         if (abort == false) {  globaldata->gListVector = NULL; }
98 }
99
100 //**********************************************************************************************************************
101
102 int GetRAbundCommand::execute(){
103         try {
104         
105                 if (abort == true) { return 0; }
106                 
107                 //read first line
108                 read = new ReadOTUFile(globaldata->inputFileName);      
109                 read->read(&*globaldata); 
110                         
111                 input = globaldata->ginput;
112                 list = globaldata->gListVector;
113                 string lastLabel = list->getLabel();
114                 
115                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
116                 set<string> processedLabels;
117                 set<string> userLabels = labels;
118                 
119                 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
120                         
121                         if(allLines == 1 || labels.count(list->getLabel()) == 1){
122                                         mothurOut(list->getLabel()); mothurOutEndLine();
123                                         rabund = new RAbundVector();                            
124                                         *rabund = (list->getRAbundVector());
125                                         
126                                         if(sorted)      {   rabund->print(out);                         }
127                                         else            {       rabund->nonSortedPrint(out);    }
128                                         
129                                         delete rabund;
130                                                                                                                         
131                                         processedLabels.insert(list->getLabel());
132                                         userLabels.erase(list->getLabel());
133                         }
134                         
135                         if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
136                                         string saveLabel = list->getLabel();
137                                         
138                                         delete list;
139                                         list = input->getListVector(lastLabel);
140                                         
141                                         mothurOut(list->getLabel()); mothurOutEndLine();
142                                         rabund = new RAbundVector();
143                                         *rabund = (list->getRAbundVector());
144                                         
145                                         if(sorted)      {   rabund->print(out);                         }
146                                         else            {       rabund->nonSortedPrint(out);    }
147
148                                         delete rabund;
149
150                                         processedLabels.insert(list->getLabel());
151                                         userLabels.erase(list->getLabel());
152                                         
153                                         //restore real lastlabel to save below
154                                         list->setLabel(saveLabel);
155                         }
156                         
157                         lastLabel = list->getLabel();           
158                         
159                         delete list;
160                         list = input->getListVector();
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 label if you need to
177                 if (needToRun == true)  {
178                         if (list != NULL) {     delete list;    }
179                         list = input->getListVector(lastLabel);
180                         
181                         mothurOut(list->getLabel()); mothurOutEndLine();
182                         rabund = new RAbundVector();
183                         *rabund = (list->getRAbundVector());
184                         
185                         if(sorted)      {   rabund->print(out);                         }
186                         else            {       rabund->nonSortedPrint(out);    }
187
188                         delete rabund;
189                         delete list;
190                 }
191
192                 out.close(); 
193                 return 0;               
194         }
195
196         catch(exception& e) {
197                 errorOut(e, "GetRAbundCommand", "execute");
198                 exit(1);
199         }
200 }
201
202 //**********************************************************************************************************************
203
204