]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
added hcluster command and fixed some bugs, namely one with smart distancing.
[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                 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"};
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() == "") && (globaldata->getRabundFile() == "")) { mothurOut("You must read a list or rabund before you can use the get.sabund command."); mothurOutEndLine(); abort = true; }
41                         
42                         //check for optional parameter and set defaults
43                         // ...at some point should added some additional type checking...
44                         label = validParameter.validFile(parameters, "label", false);                   
45                         if (label == "not found") { label = ""; }
46                         else { 
47                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
48                                 else { allLines = 1;  }
49                         }
50                         
51                         //if the user has not specified any labels use the ones from read.otu
52                         if(label == "") {  
53                                 allLines = globaldata->allLines; 
54                                 labels = globaldata->labels; 
55                         }
56                                 
57                         if (abort == false) {
58                                 filename = getRootName(globaldata->inputFileName) + "sabund";
59                                 openOutputFile(filename, out);
60                         }
61                 }
62
63         }
64         catch(exception& e) {
65                 errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70
71 void GetSAbundCommand::help(){
72         try {
73                 mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
74                 mothurOut("The get.sabund command parameters is label.  No parameters are required.\n");
75                 mothurOut("The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n");
76                 mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
77                 mothurOut("Example get.sabund().\n");
78                 mothurOut("The default value for label is all labels in your inputfile.\n");
79                 mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
80                 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
81         }
82         catch(exception& e) {
83                 errorOut(e, "GetSAbundCommand", "help");
84                 exit(1);
85         }
86 }
87
88 //**********************************************************************************************************************
89
90 GetSAbundCommand::~GetSAbundCommand(){
91 }
92
93 //**********************************************************************************************************************
94
95 int GetSAbundCommand::execute(){
96         try {
97                 
98                 if (abort == true) { return 0; }
99         
100                 //using order vector so you don't have to distinguish between the list and rabund files
101                 read = new ReadOTUFile(globaldata->inputFileName);      
102                 read->read(&*globaldata); 
103                 
104                 order = globaldata->gorder;
105                 string lastLabel = order->getLabel();
106                 input = globaldata->ginput;
107                                                 
108                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
109                 set<string> processedLabels;
110                 set<string> userLabels = labels;
111                 
112                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
113                         
114                         if(allLines == 1 || labels.count(order->getLabel()) == 1){
115                                         mothurOut(order->getLabel());  mothurOutEndLine();
116                                         sabund = new SAbundVector();
117                                         *sabund = (order->getSAbundVector());
118                                         sabund->print(out);
119                                         delete sabund;
120
121                                         processedLabels.insert(order->getLabel());
122                                         userLabels.erase(order->getLabel());
123                         }
124                         
125                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
126                                         string saveLabel = order->getLabel();
127                                         
128                                         delete order;           
129                                         order = (input->getOrderVector(lastLabel));
130                                         
131                                         mothurOut(order->getLabel());  mothurOutEndLine();
132                                         sabund = new SAbundVector();
133                                         *sabund = (order->getSAbundVector());
134                                         sabund->print(out);
135                                         delete sabund;
136
137                                         processedLabels.insert(order->getLabel());
138                                         userLabels.erase(order->getLabel());
139                                         
140                                         //restore real lastlabel to save below
141                                         order->setLabel(saveLabel);
142                         }
143                         
144                         
145                         lastLabel = order->getLabel();  
146                         
147                         delete order;           
148                         order = (input->getOrderVector());
149                 }
150                 
151                 //output error messages about any remaining user labels
152                 set<string>::iterator it;
153                 bool needToRun = false;
154                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
155                         mothurOut("Your file does not include the label " + *it); 
156                         if (processedLabels.count(lastLabel) != 1) {
157                                 mothurOut(". I will use " + lastLabel + ".");  mothurOutEndLine();
158                                 needToRun = true;
159                         }else {
160                                 mothurOut(". Please refer to " + lastLabel + ".");  mothurOutEndLine();
161                         }
162                 }
163                 
164                 //run last label if you need to
165                 if (needToRun == true)  {
166                         if (order != NULL) {    delete order;   }
167                         order = (input->getOrderVector(lastLabel));
168                         
169                         mothurOut(order->getLabel());  mothurOutEndLine();
170                         sabund = new SAbundVector();
171                         *sabund = (order->getSAbundVector());
172                         sabund->print(out);
173                         delete sabund;
174                         delete order;
175                 }
176                 globaldata->gorder = NULL;
177
178                 out.close();
179                 return 0;               
180         }
181
182         catch(exception& e) {
183                 errorOut(e, "GetSAbundCommand", "execute");
184                 exit(1);
185         }
186 }
187
188 //**********************************************************************************************************************
189
190