]> git.donarmstrong.com Git - mothur.git/blob - getlabelcommand.cpp
changed random forest output filename
[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, true); parameters.push_back(plist);
17                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false, true); parameters.push_back(prabund);
18                 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false, true); parameters.push_back(psabund);
19                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "GetlabelCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 GetlabelCommand::GetlabelCommand(){     
33         try {
34                 abort = true; calledHelp = true; 
35                 setParameters();
36         }
37         catch(exception& e) {
38                 m->errorOut(e, "GetlabelCommand", "CollectCommand");
39                 exit(1);
40         }
41 }
42 //**********************************************************************************************************************
43 string GetlabelCommand::getHelpString(){        
44         try {
45                 string helpString = "";
46                 helpString += "The get.label command parameters are list, sabund and rabund file. \n";
47                 helpString += "The get.label command should be in the following format: \n";
48                 helpString += "get.label()\n";
49                 helpString += "Example get.label().\n";
50                 return helpString;
51         }
52         catch(exception& e) {
53                 m->errorOut(e, "GetlabelCommand", "getHelpString");
54                 exit(1);
55         }
56 }
57 //**********************************************************************************************************************
58
59 GetlabelCommand::GetlabelCommand(string option)  {
60         try {
61                 abort = false; calledHelp = false;   
62                 
63                 //allow user to run help
64                 if(option == "help") { help(); abort = true; calledHelp = true; }
65                 else if(option == "citation") { citation(); 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                                 
87                                 it = parameters.find("rabund");
88                                 //user has given a template file
89                                 if(it != parameters.end()){ 
90                                         path = m->hasPath(it->second);
91                                         //if the user has not given a path then, add inputdir. else leave path alone.
92                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
93                                 }
94                                 
95                                 it = parameters.find("sabund");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
101                                 }
102                                 
103                                 it = parameters.find("list");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
109                                 }
110                         }
111                         
112                         //check for required parameters
113                         listfile = validParameter.validFile(parameters, "list", true);
114                         if (listfile == "not open") { listfile = ""; abort = true; }
115                         else if (listfile == "not found") { listfile = ""; }
116                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
117                         
118                         sabundfile = validParameter.validFile(parameters, "sabund", true);
119                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }        
120                         else if (sabundfile == "not found") { sabundfile = ""; }
121                         else {  format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
122                         
123                         rabundfile = validParameter.validFile(parameters, "rabund", true);
124                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
125                         else if (rabundfile == "not found") { rabundfile = ""; }
126                         else {  format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
127                         
128                         if ((listfile == "") && (rabundfile == "") && (sabundfile == "")) { 
129                                 //is there are current file available for any of these?
130                                 //give priority to list, then rabund, then sabund
131                                 //if there is a current shared file, use it
132                                 
133                                 listfile = m->getListFile(); 
134                                 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
135                                 else { 
136                                         rabundfile = m->getRabundFile(); 
137                                         if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
138                                         else { 
139                                                 sabundfile = m->getSabundFile(); 
140                                                 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
141                                                 else { 
142                                                         m->mothurOut("No valid current files. You must provide a list, sabund or rabund file."); m->mothurOutEndLine(); 
143                                                         abort = true;
144                                                 }
145                                         }
146                                 }
147                         }
148                 }
149
150         }
151         catch(exception& e) {
152                 m->errorOut(e, "GetlabelCommand", "GetlabelCommand");
153                 exit(1);
154         }
155 }
156 //**********************************************************************************************************************
157
158 int GetlabelCommand::execute(){
159         try {
160                 
161                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
162                 
163                 InputData* input = new InputData(inputfile, format);
164                 OrderVector* order = input->getOrderVector();
165                 string label = order->getLabel();
166                 
167                 while (order != NULL) {
168                         
169                         if (m->control_pressed) { delete input;  delete order; return 0; }
170                         
171                         label = order->getLabel();      
172                         
173                         m->mothurOut(label); m->mothurOutEndLine();
174                         
175                         delete order;           
176                         order = input->getOrderVector();
177                 }
178                 
179                 delete input; 
180                 
181                 return 0;       
182         }
183
184         catch(exception& e) {
185                 m->errorOut(e, "GetlabelCommand", "execute");
186                 exit(1);
187         }
188 }
189 //**********************************************************************************************************************
190
191