5 * Created by westcott on 2/24/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "parselistscommand.h"
12 //**********************************************************************************************************************
13 vector<string> ParseListCommand::setParameters(){
15 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none","list",false,true,true); parameters.push_back(plist);
16 CommandParameter pcount("count", "InputTypes", "", "", "CountGroup", "CountGroup", "none","",false,false,true); parameters.push_back(pcount);
17 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "CountGroup", "none","",false,false,true); parameters.push_back(pgroup);
18 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "ParseListCommand", "setParameters");
31 //**********************************************************************************************************************
32 string ParseListCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The parse.list command reads a list and group or count file and generates a list file for each group in the group or count file. \n";
36 helpString += "The parse.list command parameters are list, group, count and label.\n";
37 helpString += "The list and group or count parameters are required.\n";
38 helpString += "If a count file is provided, mothur assumes the list file contains only unique names.\n";
39 helpString += "If a group file is provided, mothur assumes the list file contains all names.\n";
40 helpString += "The label parameter is used to read specific labels in your input you want to use.\n";
41 helpString += "The parse.list command should be used in the following format: parse.list(list=yourListFile, group=yourGroupFile, label=yourLabels).\n";
42 helpString += "Example: parse.list(list=abrecovery.fn.list, group=abrecovery.groups, label=0.03).\n";
43 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n";
47 m->errorOut(e, "ParseListCommand", "getHelpString");
51 //**********************************************************************************************************************
52 string ParseListCommand::getOutputPattern(string type) {
56 if (type == "list") { pattern = "[filename],[group],[distance],list"; }
57 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
62 m->errorOut(e, "ParseListCommand", "getOutputPattern");
66 //**********************************************************************************************************************
67 ParseListCommand::ParseListCommand(){
69 abort = true; calledHelp = true;
71 vector<string> tempOutNames;
72 outputTypes["list"] = tempOutNames;
75 m->errorOut(e, "ParseListCommand", "ParseListCommand");
79 //**********************************************************************************************************************
80 ParseListCommand::ParseListCommand(string option) {
82 abort = false; calledHelp = false;
85 //allow user to run help
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string, string> parameters = parser.getParameters();
95 ValidParameters validParameter;
96 map<string, string>::iterator it;
98 //check to make sure all parameters are valid for command
99 for (it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["list"] = tempOutNames;
107 //if the user changes the input directory command factory will send this info to us in the output parameter
108 string inputDir = validParameter.validFile(parameters, "inputdir", false);
109 if (inputDir == "not found"){ inputDir = ""; }
112 it = parameters.find("list");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["list"] = inputDir + it->second; }
120 it = parameters.find("group");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["group"] = inputDir + it->second; }
128 it = parameters.find("count");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["count"] = inputDir + it->second; }
140 //check for required parameters
141 listfile = validParameter.validFile(parameters, "list", true);
142 if (listfile == "not open") { abort = true; }
143 else if (listfile == "not found") {
144 listfile = m->getListFile();
145 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
147 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
151 }else { m->setListFile(listfile); }
153 //if the user changes the output directory command factory will send this info to us in the output parameter
154 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(listfile); }
156 groupfile = validParameter.validFile(parameters, "group", true);
157 if (groupfile == "not found") { groupfile = ""; groupMap = NULL; }
158 else if (groupfile == "not open") { abort = true; groupfile = ""; groupMap = NULL; }
160 m->setGroupFile(groupfile);
161 groupMap = new GroupMap(groupfile);
163 int error = groupMap->readMap();
164 if (error == 1) { abort = true; }
167 countfile = validParameter.validFile(parameters, "count", true);
168 if (countfile == "not found") { countfile = ""; }
169 else if (countfile == "not open") { abort = true; countfile = ""; }
171 m->setCountTableFile(countfile);
172 ct.readTable(countfile, true, false);
173 if (!ct.hasGroupInfo()) {
175 m->mothurOut("[ERROR]: The parse.list command requires group info to be present in your countfile, quitting."); m->mothurOutEndLine();
180 if ((groupfile != "") && (countfile != "")) {
181 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
182 }else if ((groupfile == "") && (countfile == "")) {
183 m->mothurOut("[ERROR]: you must provide one of the following: group or count."); m->mothurOutEndLine(); abort=true;
186 //check for optional parameter and set defaults
187 // ...at some point should added some additional type checking...
188 label = validParameter.validFile(parameters, "label", false);
189 if (label == "not found") { label = ""; allLines = 1; }
191 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
192 else { allLines = 1; }
197 catch(exception& e) {
198 m->errorOut(e, "ParseListCommand", "ParseListCommand");
202 //**********************************************************************************************************************
203 int ParseListCommand::execute(){
206 if (abort == true) { if (calledHelp) { return 0; } return 2; }
208 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
209 set<string> processedLabels;
210 set<string> userLabels = labels;
212 InputData input(listfile, "list");
213 list = input.getListVector();
214 string lastLabel = list->getLabel();
216 if (m->control_pressed) {
217 delete list; if (groupfile != "") { delete groupMap; }
218 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear(); return 0;
221 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
223 if (m->control_pressed) {
224 delete list; if (groupfile != "") { delete groupMap; }
225 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
229 if(allLines == 1 || labels.count(list->getLabel()) == 1){
231 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
234 processedLabels.insert(list->getLabel());
235 userLabels.erase(list->getLabel());
238 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
239 string saveLabel = list->getLabel();
241 list = input.getListVector(lastLabel); //get new list vector to process
243 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
246 processedLabels.insert(list->getLabel());
247 userLabels.erase(list->getLabel());
249 //restore real lastlabel to save below
250 list->setLabel(saveLabel);
254 lastLabel = list->getLabel();
257 list = input.getListVector(); //get new list vector to process
260 if (m->control_pressed) {
261 if (groupfile != "") { delete groupMap; }
262 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
266 //output error messages about any remaining user labels
267 set<string>::iterator it;
268 bool needToRun = false;
269 for (it = userLabels.begin(); it != userLabels.end(); it++) {
270 m->mothurOut("Your file does not include the label " + *it);
271 if (processedLabels.count(lastLabel) != 1) {
272 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
275 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
280 if (m->control_pressed) {
281 if (groupfile != "") { delete groupMap; }
282 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
286 //run last label if you need to
287 if (needToRun == true) {
288 if (list != NULL) { delete list; }
289 list = input.getListVector(lastLabel); //get new list vector to process
291 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
297 if (groupfile != "") { delete groupMap; }
299 if (m->control_pressed) {
300 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
304 //set fasta file as new current fastafile
306 itTypes = outputTypes.find("list");
307 if (itTypes != outputTypes.end()) {
308 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
311 m->mothurOutEndLine();
312 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
313 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
314 m->mothurOutEndLine();
318 catch(exception& e) {
319 m->errorOut(e, "ParseListCommand", "execute");
323 /**********************************************************************************************************************/
324 int ParseListCommand::parse(ListVector* thisList) {
326 map<string, ofstream*> filehandles;
327 map<string, ofstream*>::iterator it3;
330 map<string, string> variables;
331 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listfile));
332 variables["[distance]"] = thisList->getLabel();
334 //fill filehandles with neccessary ofstreams
336 vector<string> gGroups;
337 if (groupfile != "") { gGroups = groupMap->getNamesOfGroups(); }
338 else { gGroups = ct.getNamesOfGroups(); }
340 for (int i=0; i<gGroups.size(); i++) {
342 filehandles[gGroups[i]] = temp;
344 variables["[group]"] = gGroups[i];
345 string filename = getOutputFileName("list",variables);
346 m->openOutputFile(filename, *temp);
347 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
351 map<string, string> groupVector;
352 map<string, string> groupLabels;
353 map<string, string>::iterator itGroup;
354 map<string, int> groupNumBins;
357 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
358 groupNumBins[it3->first] = 0;
359 groupVector[it3->first] = "";
360 groupLabels[it3->first] = "label\tnumOtus\t";
363 vector<string> binLabels = thisList->getLabels();
364 for (int i = 0; i < thisList->getNumBins(); i++) {
365 if (m->control_pressed) { break; }
367 map<string, string> groupBins;
368 string bin = list->get(i);
370 vector<string> names;
371 m->splitAtComma(bin, names); //parses bin into individual sequence names
373 //parse bin into list of sequences in each group
374 for (int j = 0; j < names.size(); j++) {
375 if (groupfile != "") {
376 string group = groupMap->getGroup(names[j]);
378 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
380 itGroup = groupBins.find(group);
381 if(itGroup == groupBins.end()) {
382 groupBins[group] = names[j]; //add first name
383 groupNumBins[group]++;
384 }else{ //add another name
385 groupBins[group] = groupBins[group] + "," + names[j];
388 vector<string> thisSeqsGroups = ct.getGroups(names[j]);
390 for (int k = 0; k < thisSeqsGroups.size(); k++) {
391 string group = thisSeqsGroups[k];
392 itGroup = groupBins.find(group);
393 if(itGroup == groupBins.end()) {
394 groupBins[group] = names[j]; //add first name
395 groupNumBins[group]++;
396 }else{ //add another name
397 groupBins[group] = groupBins[group] + "," + names[j];
404 //print parsed bin info to files
405 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
406 groupVector[itGroup->first] += itGroup->second + '\t';
407 groupLabels[itGroup->first] += binLabels[i] + '\t';
412 if (m->control_pressed) {
413 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
414 (*(filehandles[it3->first])).close();
421 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
422 (*(filehandles[it3->first])) << groupLabels[it3->first] << endl;
423 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
424 (*(filehandles[it3->first])).close();
431 catch(exception& e) {
432 m->errorOut(e, "ParseListCommand", "parse");
437 /**********************************************************************************************************************/