]> git.donarmstrong.com Git - mothur.git/blob - getrabundcommand.cpp
removed line option
[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                                         delete list;
137                                         list = input->getListVector(lastLabel);
138                                         
139                                         mothurOut(list->getLabel()); mothurOutEndLine();
140                                         rabund = new RAbundVector();
141                                         *rabund = (list->getRAbundVector());
142                                         
143                                         if(sorted)      {   rabund->print(out);                         }
144                                         else            {       rabund->nonSortedPrint(out);    }
145
146                                         delete rabund;
147
148                                         processedLabels.insert(list->getLabel());
149                                         userLabels.erase(list->getLabel());
150                         }
151                         
152                         lastLabel = list->getLabel();           
153                         
154                         delete list;
155                         list = input->getListVector();
156                 }
157                 
158                 //output error messages about any remaining user labels
159                 set<string>::iterator it;
160                 bool needToRun = false;
161                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
162                         mothurOut("Your file does not include the label " + *it); 
163                         if (processedLabels.count(lastLabel) != 1) {
164                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
165                                 needToRun = true;
166                         }else {
167                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
168                         }
169                 }
170                 
171                 //run last label if you need to
172                 if (needToRun == true)  {
173                         if (list != NULL) {     delete list;    }
174                         list = input->getListVector(lastLabel);
175                         
176                         mothurOut(list->getLabel()); mothurOutEndLine();
177                         rabund = new RAbundVector();
178                         *rabund = (list->getRAbundVector());
179                         
180                         if(sorted)      {   rabund->print(out);                         }
181                         else            {       rabund->nonSortedPrint(out);    }
182
183                         delete rabund;
184                         delete list;
185                 }
186
187                 out.close(); 
188                 return 0;               
189         }
190
191         catch(exception& e) {
192                 errorOut(e, "GetRAbundCommand", "execute");
193                 exit(1);
194         }
195 }
196
197 //**********************************************************************************************************************
198
199