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(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["rabund"] = tempOutNames;
31 outputTypes["shared"] = tempOutNames;
34 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
38 //**********************************************************************************************************************
39 vector<string> ReadOtuCommand::getRequiredParameters(){
41 string Array[] = {"list","shared","relabund","sabund","rabund","or"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "ChopSeqsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ReadOtuCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "ReadOtuCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 ReadOtuCommand::ReadOtuCommand(string option) {
64 globaldata = GlobalData::getInstance();
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string, string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["rabund"] = tempOutNames;
90 outputTypes["shared"] = tempOutNames;
92 globaldata->newRead();
94 //if the user changes the input directory command factory will send this info to us in the output parameter
95 string inputDir = validParameter.validFile(parameters, "inputdir", false);
96 if (inputDir == "not found"){ inputDir = ""; }
99 it = parameters.find("list");
100 //user has given a template file
101 if(it != parameters.end()){
102 path = m->hasPath(it->second);
103 //if the user has not given a path then, add inputdir. else leave path alone.
104 if (path == "") { parameters["list"] = inputDir + it->second; }
107 it = parameters.find("order");
108 //user has given a template file
109 if(it != parameters.end()){
110 path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["order"] = inputDir + it->second; }
115 it = parameters.find("shared");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["shared"] = inputDir + it->second; }
123 it = parameters.find("group");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["group"] = inputDir + it->second; }
131 it = parameters.find("sabund");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["sabund"] = inputDir + it->second; }
139 it = parameters.find("rabund");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["rabund"] = inputDir + it->second; }
147 it = parameters.find("ordergroup");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["ordergroup"] = inputDir + it->second; }
155 it = parameters.find("relabund");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["relabund"] = inputDir + it->second; }
165 //if the user changes the output directory command factory will send this info to us in the output parameter
166 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
168 //check for required parameters
169 listfile = validParameter.validFile(parameters, "list", true);
170 if (listfile == "not open") { abort = true; }
171 else if (listfile == "not found") { listfile = ""; }
172 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
174 sabundfile = validParameter.validFile(parameters, "sabund", true);
175 if (sabundfile == "not open") { abort = true; }
176 else if (sabundfile == "not found") { sabundfile = ""; }
177 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
179 rabundfile = validParameter.validFile(parameters, "rabund", true);
180 if (rabundfile == "not open") { abort = true; }
181 else if (rabundfile == "not found") { rabundfile = ""; }
182 else { globaldata->setRabundFile(rabundfile); globaldata->setFormat("rabund");}
184 ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
185 if (ordergroupfile == "not open") { abort = true; }
186 else if (ordergroupfile == "not found") { ordergroupfile = ""; }
187 else { globaldata->setOrderGroupFile(ordergroupfile); }
189 sharedfile = validParameter.validFile(parameters, "shared", true);
190 if (sharedfile == "not open") { abort = true; }
191 else if (sharedfile == "not found") { sharedfile = ""; }
192 else { globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile"); }
194 relAbundfile = validParameter.validFile(parameters, "relabund", true);
195 if (relAbundfile == "not open") { abort = true; }
196 else if (relAbundfile == "not found") { relAbundfile = ""; }
197 else { globaldata->setRelAbundFile(relAbundfile); globaldata->setFormat("relabund"); }
200 groupfile = validParameter.validFile(parameters, "group", true);
201 if (groupfile == "not open") { abort = true; }
202 else if (groupfile == "not found") { groupfile = ""; }
204 globaldata->setGroupFile(groupfile);
205 groupMap = new GroupMap(groupfile);
207 int error = groupMap->readMap();
208 if (error == 1) { abort = true; }
210 globaldata->gGroupmap = groupMap;
213 groups = validParameter.validFile(parameters, "groups", false);
214 if (groups == "not found") { groups = ""; }
216 m->splitAtDash(groups, Groups);
217 globaldata->Groups = Groups;
220 //you are doing a list and group shared
221 if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
223 //you have not given a file
224 if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "") && (relAbundfile == "")) {
225 m->mothurOut("You must enter either a listfile, rabundfile, sabundfile, relabund or a sharedfile with the read.otu command. "); m->mothurOutEndLine(); abort = true;
228 //check for optional parameter and set defaults
229 // ...at some point should added some additional type checking...
230 label = validParameter.validFile(parameters, "label", false);
231 if (label == "not found") { label = ""; }
233 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
234 else { allLines = 1; }
235 globaldata->labels = labels;
238 globaldata->allLines = allLines;
240 orderfile = validParameter.validFile(parameters, "order", true);
241 if (orderfile == "not open") { abort = true; }
242 else if (orderfile == "not found") { orderfile = ""; }
243 else { globaldata->setOrderFile(orderfile); }
246 if (abort == false) {
247 //gets whichever one of the above is set
248 filename = globaldata->inputFileName;
253 catch(exception& e) {
254 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
258 //**********************************************************************************************************************
260 void ReadOtuCommand::help(){
262 m->mothurOut("The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, \n");
263 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");
264 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");
265 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");
266 m->mothurOut("For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels).\n");
267 m->mothurOut("The list, rabund or sabund parameter is required, but you may only use one of them.\n");
268 m->mothurOut("The label parameter is used to read specific labels in your input.\n");
269 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");
270 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");
271 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");
272 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");
273 m->mothurOut("You can use the groups parameter to choose only specific groups to be used in the .shared and .rabund files. \n");
274 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");
275 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
278 catch(exception& e) {
279 m->errorOut(e, "ReadOtuCommand", "help");
286 //**********************************************************************************************************************
287 ReadOtuCommand::~ReadOtuCommand(){}
288 //**********************************************************************************************************************
290 int ReadOtuCommand::execute(){
293 if (abort == true) { return 0; }
295 if (globaldata->getFormat() == "shared") {
297 shared = new SharedCommand(outputDir);
298 int okay = shared->execute();
300 //problem with shared
302 globaldata->setListFile("");
303 globaldata->setGroupFile("");
304 globaldata->setSharedFile("");
305 }else { //shared command outputs the filenames
306 //m->mothurOutEndLine();
307 //m->mothurOut("Output File Name: "); m->mothurOutEndLine();
308 //m->mothurOut(globaldata->getSharedFile()); m->mothurOutEndLine();
309 //m->mothurOutEndLine();
312 outputTypes = shared->getOutputFiles();
318 catch(exception& e) {
319 m->errorOut(e, "ReadOtuCommand", "execute");
323 //**********************************************************************************************************************