5 * Created by Sarah Westcott on 1/20/09.
6 * Copyright 2009 Schloss Lab UMASS AMherst. All rights reserved.
10 #include "readotucommand.h"
13 //**********************************************************************************************************************
14 ReadOtuCommand::ReadOtuCommand(){
16 abort = true; calledHelp = true;
18 vector<string> tempOutNames;
19 outputTypes["rabund"] = tempOutNames;
20 outputTypes["shared"] = tempOutNames;
23 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
27 //**********************************************************************************************************************
28 ReadOtuCommand::ReadOtuCommand(string option) {
30 abort = false; calledHelp = false;
33 //allow user to run help
34 if(option == "help") { help(); abort = true; calledHelp = true; }
38 //valid paramters for this command
39 string Array[] = {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
40 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42 OptionParser parser(option);
43 map<string, string> parameters = parser.getParameters();
45 ValidParameters validParameter;
46 map<string, string>::iterator it;
48 //check to make sure all parameters are valid for command
49 for (it = parameters.begin(); it != parameters.end(); it++) {
50 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
53 //initialize outputTypes
54 vector<string> tempOutNames;
55 outputTypes["rabund"] = tempOutNames;
56 outputTypes["shared"] = tempOutNames;
58 globaldata->newRead();
60 //if the user changes the input directory command factory will send this info to us in the output parameter
61 string inputDir = validParameter.validFile(parameters, "inputdir", false);
62 if (inputDir == "not found"){ inputDir = ""; }
65 it = parameters.find("list");
66 //user has given a template file
67 if(it != parameters.end()){
68 path = m->hasPath(it->second);
69 //if the user has not given a path then, add inputdir. else leave path alone.
70 if (path == "") { parameters["list"] = inputDir + it->second; }
73 it = parameters.find("order");
74 //user has given a template file
75 if(it != parameters.end()){
76 path = m->hasPath(it->second);
77 //if the user has not given a path then, add inputdir. else leave path alone.
78 if (path == "") { parameters["order"] = inputDir + it->second; }
81 it = parameters.find("shared");
82 //user has given a template file
83 if(it != parameters.end()){
84 path = m->hasPath(it->second);
85 //if the user has not given a path then, add inputdir. else leave path alone.
86 if (path == "") { parameters["shared"] = inputDir + it->second; }
89 it = parameters.find("group");
90 //user has given a template file
91 if(it != parameters.end()){
92 path = m->hasPath(it->second);
93 //if the user has not given a path then, add inputdir. else leave path alone.
94 if (path == "") { parameters["group"] = inputDir + it->second; }
97 it = parameters.find("sabund");
98 //user has given a template file
99 if(it != parameters.end()){
100 path = m->hasPath(it->second);
101 //if the user has not given a path then, add inputdir. else leave path alone.
102 if (path == "") { parameters["sabund"] = inputDir + it->second; }
105 it = parameters.find("rabund");
106 //user has given a template file
107 if(it != parameters.end()){
108 path = m->hasPath(it->second);
109 //if the user has not given a path then, add inputdir. else leave path alone.
110 if (path == "") { parameters["rabund"] = inputDir + it->second; }
113 it = parameters.find("ordergroup");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["ordergroup"] = inputDir + it->second; }
121 it = parameters.find("relabund");
122 //user has given a template file
123 if(it != parameters.end()){
124 path = m->hasPath(it->second);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { parameters["relabund"] = inputDir + it->second; }
131 //if the user changes the output directory command factory will send this info to us in the output parameter
132 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
134 //check for required parameters
135 listfile = validParameter.validFile(parameters, "list", true);
136 if (listfile == "not open") { abort = true; }
137 else if (listfile == "not found") { listfile = ""; }
138 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
140 sabundfile = validParameter.validFile(parameters, "sabund", true);
141 if (sabundfile == "not open") { abort = true; }
142 else if (sabundfile == "not found") { sabundfile = ""; }
143 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
145 rabundfile = validParameter.validFile(parameters, "rabund", true);
146 if (rabundfile == "not open") { abort = true; }
147 else if (rabundfile == "not found") { rabundfile = ""; }
148 else { globaldata->setRabundFile(rabundfile); globaldata->setFormat("rabund");}
150 ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
151 if (ordergroupfile == "not open") { abort = true; }
152 else if (ordergroupfile == "not found") { ordergroupfile = ""; }
153 else { globaldata->setOrderGroupFile(ordergroupfile); }
155 sharedfile = validParameter.validFile(parameters, "shared", true);
156 if (sharedfile == "not open") { abort = true; }
157 else if (sharedfile == "not found") { sharedfile = ""; }
158 else { globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile"); }
160 relAbundfile = validParameter.validFile(parameters, "relabund", true);
161 if (relAbundfile == "not open") { abort = true; }
162 else if (relAbundfile == "not found") { relAbundfile = ""; }
163 else { globaldata->setRelAbundFile(relAbundfile); globaldata->setFormat("relabund"); }
166 groupfile = validParameter.validFile(parameters, "group", true);
167 if (groupfile == "not open") { abort = true; }
168 else if (groupfile == "not found") { groupfile = ""; }
170 globaldata->setGroupFile(groupfile);
171 groupMap = new GroupMap(groupfile);
173 int error = groupMap->readMap();
174 if (error == 1) { abort = true; }
176 globaldata->gGroupmap = groupMap;
179 groups = validParameter.validFile(parameters, "groups", false);
180 if (groups == "not found") { groups = ""; }
182 m->splitAtDash(groups, Groups);
183 globaldata->Groups = Groups;
186 //you are doing a list and group shared
187 if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
189 //you have not given a file
190 if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "") && (relAbundfile == "")) {
191 m->mothurOut("You must enter either a listfile, rabundfile, sabundfile, relabund or a sharedfile with the read.otu command. "); m->mothurOutEndLine(); abort = true;
194 //check for optional parameter and set defaults
195 // ...at some point should added some additional type checking...
196 label = validParameter.validFile(parameters, "label", false);
197 if (label == "not found") { label = ""; }
199 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
200 else { allLines = 1; }
201 globaldata->labels = labels;
204 globaldata->allLines = allLines;
206 orderfile = validParameter.validFile(parameters, "order", true);
207 if (orderfile == "not open") { abort = true; }
208 else if (orderfile == "not found") { orderfile = ""; }
209 else { globaldata->setOrderFile(orderfile); }
212 if (abort == false) {
213 //gets whichever one of the above is set
214 filename = globaldata->inputFileName;
220 catch(exception& e) {
221 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
225 ///**********************************************************************************************************************
227 int ReadOtuCommand::execute(){
230 if (abort == true) { if (calledHelp) { return 0; } return 2; }
231 m->mothurOut(getHelpString()); m->mothurOutEndLine();
234 if (globaldata->getFormat() == "shared") {
236 shared = new SharedCommand(outputDir);
237 int okay = shared->execute();
239 //problem with shared
241 globaldata->setListFile("");
242 globaldata->setGroupFile("");
243 globaldata->setSharedFile("");
244 }else { //shared command outputs the filenames
245 //m->mothurOutEndLine();
246 //m->mothurOut("Output File Name: "); m->mothurOutEndLine();
247 //m->mothurOut(globaldata->getSharedFile()); m->mothurOutEndLine();
248 //m->mothurOutEndLine();
251 outputTypes = shared->getOutputFiles();
253 //set rabund file as new current rabundfile
255 itTypes = outputTypes.find("rabund");
256 if (itTypes != outputTypes.end()) {
257 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
260 itTypes = outputTypes.find("shared");
261 if (itTypes != outputTypes.end()) {
262 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
271 catch(exception& e) {
272 m->errorOut(e, "ReadOtuCommand", "execute");
276 //**********************************************************************************************************************