5 * Created by Sarah Westcott on 1/20/09.
6 * Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
10 #include "readotucommand.h"
12 //**********************************************************************************************************************
13 vector<string> ReadOtuCommand::getValidParameters(){
15 string Array[] = {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "ReadOtuCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 ReadOtuCommand::ReadOtuCommand(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["rabund"] = tempOutNames;
30 outputTypes["shared"] = tempOutNames;
33 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
37 //**********************************************************************************************************************
38 vector<string> ReadOtuCommand::getRequiredParameters(){
40 string Array[] = {"list","shared","relabund","sabund","rabund","or"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "ChopSeqsCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> ReadOtuCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "ReadOtuCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 ReadOtuCommand::ReadOtuCommand(string option) {
63 globaldata = GlobalData::getInstance();
64 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
71 //valid paramters for this command
72 string Array[] = {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string, string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["rabund"] = tempOutNames;
89 outputTypes["shared"] = tempOutNames;
91 globaldata->newRead();
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("list");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["list"] = inputDir + it->second; }
106 it = parameters.find("order");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["order"] = inputDir + it->second; }
114 it = parameters.find("shared");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["shared"] = inputDir + it->second; }
122 it = parameters.find("group");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["group"] = inputDir + it->second; }
130 it = parameters.find("sabund");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["sabund"] = inputDir + it->second; }
138 it = parameters.find("rabund");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["rabund"] = inputDir + it->second; }
146 it = parameters.find("ordergroup");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["ordergroup"] = inputDir + it->second; }
154 it = parameters.find("relabund");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["relabund"] = inputDir + it->second; }
164 //if the user changes the output directory command factory will send this info to us in the output parameter
165 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
167 //check for required parameters
168 listfile = validParameter.validFile(parameters, "list", true);
169 if (listfile == "not open") { abort = true; }
170 else if (listfile == "not found") { listfile = ""; }
171 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
173 sabundfile = validParameter.validFile(parameters, "sabund", true);
174 if (sabundfile == "not open") { abort = true; }
175 else if (sabundfile == "not found") { sabundfile = ""; }
176 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
178 rabundfile = validParameter.validFile(parameters, "rabund", true);
179 if (rabundfile == "not open") { abort = true; }
180 else if (rabundfile == "not found") { rabundfile = ""; }
181 else { globaldata->setRabundFile(rabundfile); globaldata->setFormat("rabund");}
183 ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
184 if (ordergroupfile == "not open") { abort = true; }
185 else if (ordergroupfile == "not found") { ordergroupfile = ""; }
186 else { globaldata->setOrderGroupFile(ordergroupfile); }
188 sharedfile = validParameter.validFile(parameters, "shared", true);
189 if (sharedfile == "not open") { abort = true; }
190 else if (sharedfile == "not found") { sharedfile = ""; }
191 else { globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile"); }
193 relAbundfile = validParameter.validFile(parameters, "relabund", true);
194 if (relAbundfile == "not open") { abort = true; }
195 else if (relAbundfile == "not found") { relAbundfile = ""; }
196 else { globaldata->setRelAbundFile(relAbundfile); globaldata->setFormat("relabund"); }
199 groupfile = validParameter.validFile(parameters, "group", true);
200 if (groupfile == "not open") { abort = true; }
201 else if (groupfile == "not found") { groupfile = ""; }
203 globaldata->setGroupFile(groupfile);
204 groupMap = new GroupMap(groupfile);
206 int error = groupMap->readMap();
207 if (error == 1) { abort = true; }
209 globaldata->gGroupmap = groupMap;
212 groups = validParameter.validFile(parameters, "groups", false);
213 if (groups == "not found") { groups = ""; }
215 m->splitAtDash(groups, Groups);
216 globaldata->Groups = Groups;
219 //you are doing a list and group shared
220 if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
222 //you have not given a file
223 if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "") && (relAbundfile == "")) {
224 m->mothurOut("You must enter either a listfile, rabundfile, sabundfile, relabund or a sharedfile with the read.otu command. "); m->mothurOutEndLine(); abort = true;
227 //check for optional parameter and set defaults
228 // ...at some point should added some additional type checking...
229 label = validParameter.validFile(parameters, "label", false);
230 if (label == "not found") { label = ""; }
232 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
233 else { allLines = 1; }
234 globaldata->labels = labels;
237 globaldata->allLines = allLines;
239 orderfile = validParameter.validFile(parameters, "order", true);
240 if (orderfile == "not open") { abort = true; }
241 else if (orderfile == "not found") { orderfile = ""; }
242 else { globaldata->setOrderFile(orderfile); }
245 if (abort == false) {
246 //gets whichever one of the above is set
247 filename = globaldata->inputFileName;
252 catch(exception& e) {
253 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
257 //**********************************************************************************************************************
259 void ReadOtuCommand::help(){
261 m->mothurOut("The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, \n");
262 m->mothurOut("collect.shared, rarefaction.shared, summary.shared heatmap.bin, heatmap.sim or venn command. Mothur will generate a .list, .rabund and .sabund upon completion of the cluster command \n");
263 m->mothurOut("or you may use your own. The read.otu command parameter options are list, rabund, sabund, shared, relabund, group, order, ordergroup, label and groups.\n");
264 m->mothurOut("The read.otu command can be used in two ways. The first is to read a list, rabund or sabund and run the collect.single, rarefaction.single or summary.single.\n");
265 m->mothurOut("For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels).\n");
266 m->mothurOut("The list, rabund or sabund parameter is required, but you may only use one of them.\n");
267 m->mothurOut("The label parameter is used to read specific labels in your input.\n");
268 m->mothurOut("The second way to use the read.otu command is to read a list and a group, or a shared so you can use the collect.shared, rarefaction.shared or summary.shared commands.\n");
269 m->mothurOut("In this case the read.otu command should be in the following format: read.otu(list=yourListFile, group=yourGroupFile) or read.otu(shared=yourSharedFile). \n");
270 m->mothurOut("The list parameter and group paramaters or the shared paremeter is required. When using the command the second way with a list and group file read.otu command parses the .list file\n");
271 m->mothurOut("and separates it into groups. It outputs a .shared file containing the OTU information for each group. The read.otu command also outputs a .rabund file for each group. \n");
272 m->mothurOut("You can use the groups parameter to choose only specific groups to be used in the .shared and .rabund files. \n");
273 m->mothurOut("You can use the ordergroup parameter to provide a file containing a list of group names in the order you would like them to appear in your shared file. \n");
274 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
277 catch(exception& e) {
278 m->errorOut(e, "ReadOtuCommand", "help");
285 //**********************************************************************************************************************
286 ReadOtuCommand::~ReadOtuCommand(){}
287 //**********************************************************************************************************************
289 int ReadOtuCommand::execute(){
292 if (abort == true) { if (calledHelp) { return 0; } return 2; }
294 if (globaldata->getFormat() == "shared") {
296 shared = new SharedCommand(outputDir);
297 int okay = shared->execute();
299 //problem with shared
301 globaldata->setListFile("");
302 globaldata->setGroupFile("");
303 globaldata->setSharedFile("");
304 }else { //shared command outputs the filenames
305 //m->mothurOutEndLine();
306 //m->mothurOut("Output File Name: "); m->mothurOutEndLine();
307 //m->mothurOut(globaldata->getSharedFile()); m->mothurOutEndLine();
308 //m->mothurOutEndLine();
311 outputTypes = shared->getOutputFiles();
317 catch(exception& e) {
318 m->errorOut(e, "ReadOtuCommand", "execute");
322 //**********************************************************************************************************************