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 ReadOtuCommand::ReadOtuCommand(string option) {
15 globaldata = GlobalData::getInstance();
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string, string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string, string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 globaldata->newRead();
40 //if the user changes the input directory command factory will send this info to us in the output parameter
41 string inputDir = validParameter.validFile(parameters, "inputdir", false);
42 if (inputDir == "not found"){ inputDir = ""; }
45 it = parameters.find("list");
46 //user has given a template file
47 if(it != parameters.end()){
48 path = m->hasPath(it->second);
49 //if the user has not given a path then, add inputdir. else leave path alone.
50 if (path == "") { parameters["list"] = inputDir + it->second; }
53 it = parameters.find("order");
54 //user has given a template file
55 if(it != parameters.end()){
56 path = m->hasPath(it->second);
57 //if the user has not given a path then, add inputdir. else leave path alone.
58 if (path == "") { parameters["order"] = inputDir + it->second; }
61 it = parameters.find("shared");
62 //user has given a template file
63 if(it != parameters.end()){
64 path = m->hasPath(it->second);
65 //if the user has not given a path then, add inputdir. else leave path alone.
66 if (path == "") { parameters["shared"] = inputDir + it->second; }
69 it = parameters.find("group");
70 //user has given a template file
71 if(it != parameters.end()){
72 path = m->hasPath(it->second);
73 //if the user has not given a path then, add inputdir. else leave path alone.
74 if (path == "") { parameters["group"] = inputDir + it->second; }
77 it = parameters.find("sabund");
78 //user has given a template file
79 if(it != parameters.end()){
80 path = m->hasPath(it->second);
81 //if the user has not given a path then, add inputdir. else leave path alone.
82 if (path == "") { parameters["sabund"] = inputDir + it->second; }
85 it = parameters.find("rabund");
86 //user has given a template file
87 if(it != parameters.end()){
88 path = m->hasPath(it->second);
89 //if the user has not given a path then, add inputdir. else leave path alone.
90 if (path == "") { parameters["rabund"] = inputDir + it->second; }
93 it = parameters.find("ordergroup");
94 //user has given a template file
95 if(it != parameters.end()){
96 path = m->hasPath(it->second);
97 //if the user has not given a path then, add inputdir. else leave path alone.
98 if (path == "") { parameters["ordergroup"] = inputDir + it->second; }
101 it = parameters.find("relabund");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["relabund"] = inputDir + it->second; }
111 //if the user changes the output directory command factory will send this info to us in the output parameter
112 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
114 //check for required parameters
115 listfile = validParameter.validFile(parameters, "list", true);
116 if (listfile == "not open") { abort = true; }
117 else if (listfile == "not found") { listfile = ""; }
118 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
120 sabundfile = validParameter.validFile(parameters, "sabund", true);
121 if (sabundfile == "not open") { abort = true; }
122 else if (sabundfile == "not found") { sabundfile = ""; }
123 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
125 rabundfile = validParameter.validFile(parameters, "rabund", true);
126 if (rabundfile == "not open") { abort = true; }
127 else if (rabundfile == "not found") { rabundfile = ""; }
128 else { globaldata->setRabundFile(rabundfile); globaldata->setFormat("rabund");}
130 ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
131 if (ordergroupfile == "not open") { abort = true; }
132 else if (ordergroupfile == "not found") { ordergroupfile = ""; }
133 else { globaldata->setOrderGroupFile(ordergroupfile); }
135 sharedfile = validParameter.validFile(parameters, "shared", true);
136 if (sharedfile == "not open") { abort = true; }
137 else if (sharedfile == "not found") { sharedfile = ""; }
138 else { globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile"); }
140 relAbundfile = validParameter.validFile(parameters, "relabund", true);
141 if (relAbundfile == "not open") { abort = true; }
142 else if (relAbundfile == "not found") { relAbundfile = ""; }
143 else { globaldata->setRelAbundFile(relAbundfile); globaldata->setFormat("relabund"); }
146 groupfile = validParameter.validFile(parameters, "group", true);
147 if (groupfile == "not open") { abort = true; }
148 else if (groupfile == "not found") { groupfile = ""; }
150 globaldata->setGroupFile(groupfile);
151 groupMap = new GroupMap(groupfile);
153 int error = groupMap->readMap();
154 if (error == 1) { abort = true; }
156 globaldata->gGroupmap = groupMap;
159 groups = validParameter.validFile(parameters, "groups", false);
160 if (groups == "not found") { groups = ""; }
162 m->splitAtDash(groups, Groups);
163 globaldata->Groups = Groups;
166 //you are doing a list and group shared
167 if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
169 //you have not given a file
170 if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "") && (relAbundfile == "")) {
171 m->mothurOut("You must enter either a listfile, rabundfile, sabundfile, relabund or a sharedfile with the read.otu command. "); m->mothurOutEndLine(); abort = true;
174 //check for optional parameter and set defaults
175 // ...at some point should added some additional type checking...
176 label = validParameter.validFile(parameters, "label", false);
177 if (label == "not found") { label = ""; }
179 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
180 else { allLines = 1; }
181 globaldata->labels = labels;
184 globaldata->allLines = allLines;
186 orderfile = validParameter.validFile(parameters, "order", true);
187 if (orderfile == "not open") { abort = true; }
188 else if (orderfile == "not found") { orderfile = ""; }
189 else { globaldata->setOrderFile(orderfile); }
192 if (abort == false) {
193 //gets whichever one of the above is set
194 filename = globaldata->inputFileName;
199 catch(exception& e) {
200 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
204 //**********************************************************************************************************************
206 void ReadOtuCommand::help(){
208 m->mothurOut("The read.otu command must be run before you execute a collect.single, rarefaction.single, summary.single, \n");
209 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");
210 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");
211 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");
212 m->mothurOut("For this use the read.otu command should be in the following format: read.otu(list=yourListFile, order=yourOrderFile, label=yourLabels).\n");
213 m->mothurOut("The list, rabund or sabund parameter is required, but you may only use one of them.\n");
214 m->mothurOut("The label parameter is used to read specific labels in your input.\n");
215 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");
216 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");
217 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");
218 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");
219 m->mothurOut("You can use the groups parameter to choose only specific groups to be used in the .shared and .rabund files. \n");
220 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");
221 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
224 catch(exception& e) {
225 m->errorOut(e, "ReadOtuCommand", "help");
232 //**********************************************************************************************************************
233 ReadOtuCommand::~ReadOtuCommand(){}
234 //**********************************************************************************************************************
236 int ReadOtuCommand::execute(){
239 if (abort == true) { return 0; }
241 if (globaldata->getFormat() == "shared") {
243 shared = new SharedCommand(outputDir);
244 int okay = shared->execute();
246 //problem with shared
248 globaldata->setListFile("");
249 globaldata->setGroupFile("");
250 globaldata->setSharedFile("");
251 }else { //shared command outputs the filenames
252 //m->mothurOutEndLine();
253 //m->mothurOut("Output File Name: "); m->mothurOutEndLine();
254 //m->mothurOut(globaldata->getSharedFile()); m->mothurOutEndLine();
255 //m->mothurOutEndLine();
262 catch(exception& e) {
263 m->errorOut(e, "ReadOtuCommand", "execute");
267 //**********************************************************************************************************************