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