]> git.donarmstrong.com Git - mothur.git/blob - getlabelcommand.cpp
removed read.dist, read.otu, read.tree and globaldata. added current to defaults...
[mothur.git] / getlabelcommand.cpp
1 /*
2  *  GetlabelCommand.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 1/30/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "getlabelcommand.h"
11
12
13 //**********************************************************************************************************************
14 vector<string> GetlabelCommand::setParameters(){        
15         try {
16                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
17                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);
18                 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
19                 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);            
20                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "GetlabelCommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 GetlabelCommand::GetlabelCommand(){     
34         try {
35                 abort = true; calledHelp = true; 
36                 setParameters();
37         }
38         catch(exception& e) {
39                 m->errorOut(e, "GetlabelCommand", "CollectCommand");
40                 exit(1);
41         }
42 }
43 //**********************************************************************************************************************
44 string GetlabelCommand::getHelpString(){        
45         try {
46                 string helpString = "";
47                 helpString += "The get.label command parameters are list, sabund, rabund and shared file. \n";
48                 helpString += "The get.label command should be in the following format: \n";
49                 helpString += "get.label()\n";
50                 helpString += "Example get.label().\n";
51                 return helpString;
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "GetlabelCommand", "getHelpString");
55                 exit(1);
56         }
57 }
58 //**********************************************************************************************************************
59
60 GetlabelCommand::GetlabelCommand(string option)  {
61         try {
62                 abort = false; calledHelp = false;   
63                 
64                 //allow user to run help
65                 if(option == "help") { help(); abort = true; calledHelp = true; }
66                 
67                 else {
68                         vector<string> myArray = setParameters();
69                         
70                         OptionParser parser(option);
71                         map<string,string> parameters = parser.getParameters();
72                         map<string,string>::iterator it;
73                         
74                         ValidParameters validParameter;
75                         
76                         //check to make sure all parameters are valid for command
77                         for (it = parameters.begin(); it != parameters.end(); it++) { 
78                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
79                         }
80                         
81                         //if the user changes the input directory command factory will send this info to us in the output parameter 
82                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
83                         if (inputDir == "not found"){   inputDir = "";          }
84                         else {
85                                 string path;
86                                 it = parameters.find("shared");
87                                 //user has given a template file
88                                 if(it != parameters.end()){ 
89                                         path = m->hasPath(it->second);
90                                         //if the user has not given a path then, add inputdir. else leave path alone.
91                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
92                                 }
93                                 
94                                 it = parameters.find("rabund");
95                                 //user has given a template file
96                                 if(it != parameters.end()){ 
97                                         path = m->hasPath(it->second);
98                                         //if the user has not given a path then, add inputdir. else leave path alone.
99                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
100                                 }
101                                 
102                                 it = parameters.find("sabund");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
108                                 }
109                                 
110                                 it = parameters.find("list");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
116                                 }
117                         }
118                         
119                         //check for required parameters
120                         listfile = validParameter.validFile(parameters, "list", true);
121                         if (listfile == "not open") { listfile = ""; abort = true; }
122                         else if (listfile == "not found") { listfile = ""; }
123                         else {  format = "list"; inputfile = listfile; }
124                         
125                         sabundfile = validParameter.validFile(parameters, "sabund", true);
126                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }        
127                         else if (sabundfile == "not found") { sabundfile = ""; }
128                         else {  format = "sabund"; inputfile = sabundfile; }
129                         
130                         rabundfile = validParameter.validFile(parameters, "rabund", true);
131                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
132                         else if (rabundfile == "not found") { rabundfile = ""; }
133                         else {  format = "rabund"; inputfile = rabundfile; }
134                         
135                         sharedfile = validParameter.validFile(parameters, "shared", true);
136                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
137                         else if (sharedfile == "not found") { sharedfile = ""; }
138                         else {  format = "sharedfile"; inputfile = sharedfile; }
139                         
140                         if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) { 
141                                 //is there are current file available for any of these?
142                                 //give priority to shared, then list, then rabund, then sabund
143                                 //if there is a current shared file, use it
144                                 sharedfile = m->getSharedFile(); 
145                                 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
146                                 else { 
147                                         listfile = m->getListFile(); 
148                                         if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
149                                         else { 
150                                                 rabundfile = m->getRabundFile(); 
151                                                 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
152                                                 else { 
153                                                         sabundfile = m->getSabundFile(); 
154                                                         if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
155                                                         else { 
156                                                                 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file before you can use the collect.single command."); m->mothurOutEndLine(); 
157                                                                 abort = true;
158                                                         }
159                                                 }
160                                         }
161                                 }
162                         }
163                         
164                 }
165
166         }
167         catch(exception& e) {
168                 m->errorOut(e, "GetlabelCommand", "GetlabelCommand");
169                 exit(1);
170         }
171 }
172 //**********************************************************************************************************************
173
174 int GetlabelCommand::execute(){
175         try {
176                 
177                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
178                 
179                 InputData* input = new InputData(inputfile, format);
180                 OrderVector* order = input->getOrderVector();
181                 string label = order->getLabel();
182                 
183                 while (order != NULL) {
184                         
185                         if (m->control_pressed) { delete input;  delete order; return 0; }
186                         
187                         m->mothurOut(label); m->mothurOutEndLine();
188                         
189                         label = order->getLabel();      
190                         
191                         delete order;           
192                         order = input->getOrderVector();
193                 }
194                 
195                 delete input; 
196                 
197                 return 0;       
198         }
199
200         catch(exception& e) {
201                 m->errorOut(e, "GetlabelCommand", "execute");
202                 exit(1);
203         }
204 }
205 //**********************************************************************************************************************
206
207