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; }
35 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
39 //valid paramters for this command
40 string Array[] = {"list","order","shared","relabund","label","group","sabund", "rabund","groups","ordergroup","outputdir","inputdir"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43 OptionParser parser(option);
44 map<string, string> parameters = parser.getParameters();
46 ValidParameters validParameter;
47 map<string, string>::iterator it;
49 //check to make sure all parameters are valid for command
50 for (it = parameters.begin(); it != parameters.end(); it++) {
51 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
54 //initialize outputTypes
55 vector<string> tempOutNames;
56 outputTypes["rabund"] = tempOutNames;
57 outputTypes["shared"] = tempOutNames;
59 globaldata->newRead();
61 //if the user changes the input directory command factory will send this info to us in the output parameter
62 string inputDir = validParameter.validFile(parameters, "inputdir", false);
63 if (inputDir == "not found"){ inputDir = ""; }
66 it = parameters.find("list");
67 //user has given a template file
68 if(it != parameters.end()){
69 path = m->hasPath(it->second);
70 //if the user has not given a path then, add inputdir. else leave path alone.
71 if (path == "") { parameters["list"] = inputDir + it->second; }
74 it = parameters.find("order");
75 //user has given a template file
76 if(it != parameters.end()){
77 path = m->hasPath(it->second);
78 //if the user has not given a path then, add inputdir. else leave path alone.
79 if (path == "") { parameters["order"] = inputDir + it->second; }
82 it = parameters.find("shared");
83 //user has given a template file
84 if(it != parameters.end()){
85 path = m->hasPath(it->second);
86 //if the user has not given a path then, add inputdir. else leave path alone.
87 if (path == "") { parameters["shared"] = inputDir + it->second; }
90 it = parameters.find("group");
91 //user has given a template file
92 if(it != parameters.end()){
93 path = m->hasPath(it->second);
94 //if the user has not given a path then, add inputdir. else leave path alone.
95 if (path == "") { parameters["group"] = inputDir + it->second; }
98 it = parameters.find("sabund");
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["sabund"] = inputDir + it->second; }
106 it = parameters.find("rabund");
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["rabund"] = inputDir + it->second; }
114 it = parameters.find("ordergroup");
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["ordergroup"] = inputDir + it->second; }
122 it = parameters.find("relabund");
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["relabund"] = inputDir + it->second; }
132 //if the user changes the output directory command factory will send this info to us in the output parameter
133 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
135 //check for required parameters
136 listfile = validParameter.validFile(parameters, "list", true);
137 if (listfile == "not open") { abort = true; }
138 else if (listfile == "not found") { listfile = ""; }
139 else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
141 sabundfile = validParameter.validFile(parameters, "sabund", true);
142 if (sabundfile == "not open") { abort = true; }
143 else if (sabundfile == "not found") { sabundfile = ""; }
144 else { globaldata->setSabundFile(sabundfile); globaldata->setFormat("sabund"); }
146 rabundfile = validParameter.validFile(parameters, "rabund", true);
147 if (rabundfile == "not open") { abort = true; }
148 else if (rabundfile == "not found") { rabundfile = ""; }
149 else { globaldata->setRabundFile(rabundfile); globaldata->setFormat("rabund");}
151 ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
152 if (ordergroupfile == "not open") { abort = true; }
153 else if (ordergroupfile == "not found") { ordergroupfile = ""; }
154 else { globaldata->setOrderGroupFile(ordergroupfile); }
156 sharedfile = validParameter.validFile(parameters, "shared", true);
157 if (sharedfile == "not open") { abort = true; }
158 else if (sharedfile == "not found") { sharedfile = ""; }
159 else { globaldata->setSharedFile(sharedfile); globaldata->setFormat("sharedfile"); }
161 relAbundfile = validParameter.validFile(parameters, "relabund", true);
162 if (relAbundfile == "not open") { abort = true; }
163 else if (relAbundfile == "not found") { relAbundfile = ""; }
164 else { globaldata->setRelAbundFile(relAbundfile); globaldata->setFormat("relabund"); }
167 groupfile = validParameter.validFile(parameters, "group", true);
168 if (groupfile == "not open") { abort = true; }
169 else if (groupfile == "not found") { groupfile = ""; }
171 globaldata->setGroupFile(groupfile);
172 groupMap = new GroupMap(groupfile);
174 int error = groupMap->readMap();
175 if (error == 1) { abort = true; }
177 globaldata->gGroupmap = groupMap;
180 groups = validParameter.validFile(parameters, "groups", false);
181 if (groups == "not found") { groups = ""; }
183 m->splitAtDash(groups, Groups);
184 globaldata->Groups = Groups;
187 //you are doing a list and group shared
188 if ((listfile != "") && (groupfile != "")) { globaldata->setFormat("shared"); }
190 //you have not given a file
191 if ((listfile == "") && (sharedfile == "") && (rabundfile == "") && (sabundfile == "") && (relAbundfile == "")) {
192 m->mothurOut("You must enter either a listfile, rabundfile, sabundfile, relabund or a sharedfile with the read.otu command. "); m->mothurOutEndLine(); abort = true;
195 //check for optional parameter and set defaults
196 // ...at some point should added some additional type checking...
197 label = validParameter.validFile(parameters, "label", false);
198 if (label == "not found") { label = ""; }
200 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
201 else { allLines = 1; }
202 globaldata->labels = labels;
205 globaldata->allLines = allLines;
207 orderfile = validParameter.validFile(parameters, "order", true);
208 if (orderfile == "not open") { abort = true; }
209 else if (orderfile == "not found") { orderfile = ""; }
210 else { globaldata->setOrderFile(orderfile); }
213 if (abort == false) {
214 //gets whichever one of the above is set
215 filename = globaldata->inputFileName;
221 catch(exception& e) {
222 m->errorOut(e, "ReadOtuCommand", "ReadOtuCommand");
226 ///**********************************************************************************************************************
228 int ReadOtuCommand::execute(){
231 if (abort == true) { if (calledHelp) { return 0; } return 2; }
232 m->mothurOut(getHelpString()); m->mothurOutEndLine();
235 if (globaldata->getFormat() == "shared") {
237 shared = new SharedCommand(outputDir);
238 int okay = shared->execute();
240 //problem with shared
242 globaldata->setListFile("");
243 globaldata->setGroupFile("");
244 globaldata->setSharedFile("");
245 }else { //shared command outputs the filenames
246 //m->mothurOutEndLine();
247 //m->mothurOut("Output File Name: "); m->mothurOutEndLine();
248 //m->mothurOut(globaldata->getSharedFile()); m->mothurOutEndLine();
249 //m->mothurOutEndLine();
252 outputTypes = shared->getOutputFiles();
254 //set rabund file as new current rabundfile
256 itTypes = outputTypes.find("rabund");
257 if (itTypes != outputTypes.end()) {
258 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
261 itTypes = outputTypes.find("shared");
262 if (itTypes != outputTypes.end()) {
263 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
272 catch(exception& e) {
273 m->errorOut(e, "ReadOtuCommand", "execute");
277 //**********************************************************************************************************************