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",false,true); parameters.push_back(plist);
16 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
17 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "ParseListCommand", "setParameters");
30 //**********************************************************************************************************************
31 string ParseListCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The parse.list command reads a list and group file and generates a list file for each group in the groupfile. \n";
35 helpString += "The parse.list command parameters are list, group and label.\n";
36 helpString += "The list and group parameters are required.\n";
37 helpString += "The label parameter is used to read specific labels in your input you want to use.\n";
38 helpString += "The parse.list command should be used in the following format: parse.list(list=yourListFile, group=yourGroupFile, label=yourLabels).\n";
39 helpString += "Example: parse.list(list=abrecovery.fn.list, group=abrecovery.groups, label=0.03).\n";
40 helpString += "Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n";
44 m->errorOut(e, "ParseListCommand", "getHelpString");
48 //**********************************************************************************************************************
49 string ParseListCommand::getOutputFileNameTag(string type, string inputName=""){
51 string outputFileName = "";
52 map<string, vector<string> >::iterator it;
54 //is this a type this command creates
55 it = outputTypes.find(type);
56 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
58 if (type == "list") { outputFileName = "list"; }
59 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
61 return outputFileName;
64 m->errorOut(e, "ParseListCommand", "getOutputFileNameTag");
69 //**********************************************************************************************************************
70 ParseListCommand::ParseListCommand(){
72 abort = true; calledHelp = true;
74 vector<string> tempOutNames;
75 outputTypes["list"] = tempOutNames;
78 m->errorOut(e, "ParseListCommand", "ParseListCommand");
82 //**********************************************************************************************************************
83 ParseListCommand::ParseListCommand(string option) {
85 abort = false; calledHelp = false;
88 //allow user to run help
89 if(option == "help") { help(); abort = true; calledHelp = true; }
90 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
93 vector<string> myArray = setParameters();
95 OptionParser parser(option);
96 map<string, string> parameters = parser.getParameters();
98 ValidParameters validParameter;
99 map<string, string>::iterator it;
101 //check to make sure all parameters are valid for command
102 for (it = parameters.begin(); it != parameters.end(); it++) {
103 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
106 //initialize outputTypes
107 vector<string> tempOutNames;
108 outputTypes["list"] = tempOutNames;
110 //if the user changes the input directory command factory will send this info to us in the output parameter
111 string inputDir = validParameter.validFile(parameters, "inputdir", false);
112 if (inputDir == "not found"){ inputDir = ""; }
115 it = parameters.find("list");
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["list"] = 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; }
133 //if the user changes the output directory command factory will send this info to us in the output parameter
134 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
136 //check for required parameters
137 listfile = validParameter.validFile(parameters, "list", true);
138 if (listfile == "not open") { abort = true; }
139 else if (listfile == "not found") {
140 listfile = m->getListFile();
141 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
143 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
147 }else { m->setListFile(listfile); }
149 groupfile = validParameter.validFile(parameters, "group", true);
150 if (groupfile == "not open") { abort = true; }
151 else if (groupfile == "not found") {
152 groupfile = m->getListFile();
153 if (groupfile != "") {
154 m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine();
155 groupMap = new GroupMap(groupfile);
157 int error = groupMap->readMap();
158 if (error == 1) { abort = true; }
159 }else { m->mothurOut("No valid current group file. You must provide a group file."); m->mothurOutEndLine(); abort = true; }
161 m->setGroupFile(groupfile);
162 groupMap = new GroupMap(groupfile);
164 int error = groupMap->readMap();
165 if (error == 1) { abort = true; }
168 //do you have all files needed
169 if ((listfile == "") || (groupfile == "")) { m->mothurOut("You must enter both a listfile and groupfile for the parse.list command. "); m->mothurOutEndLine(); abort = true; }
171 //check for optional parameter and set defaults
172 // ...at some point should added some additional type checking...
173 label = validParameter.validFile(parameters, "label", false);
174 if (label == "not found") { label = ""; allLines = 1; }
176 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
177 else { allLines = 1; }
182 catch(exception& e) {
183 m->errorOut(e, "ParseListCommand", "ParseListCommand");
187 //**********************************************************************************************************************
188 int ParseListCommand::execute(){
191 if (abort == true) { if (calledHelp) { return 0; } return 2; }
194 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
196 //fill filehandles with neccessary ofstreams
199 vector<string> gGroups = groupMap->getNamesOfGroups();
200 for (i=0; i<gGroups.size(); i++) {
202 filehandles[gGroups[i]] = temp;
204 string filename = fileroot + gGroups[i] + "." + getOutputFileNameTag("list");
205 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
206 m->openOutputFile(filename, *temp);
209 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
210 set<string> processedLabels;
211 set<string> userLabels = labels;
213 input = new InputData(listfile, "list");
214 list = input->getListVector();
215 string lastLabel = list->getLabel();
217 if (m->control_pressed) {
218 delete input; delete list; delete groupMap;
219 vector<string> gGroups = groupMap->getNamesOfGroups();
220 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
221 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
225 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
227 if (m->control_pressed) {
228 delete input; delete list; delete groupMap;
229 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
230 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
234 if(allLines == 1 || labels.count(list->getLabel()) == 1){
236 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
239 processedLabels.insert(list->getLabel());
240 userLabels.erase(list->getLabel());
243 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
244 string saveLabel = list->getLabel();
247 list = input->getListVector(lastLabel); //get new list vector to process
249 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
252 processedLabels.insert(list->getLabel());
253 userLabels.erase(list->getLabel());
255 //restore real lastlabel to save below
256 list->setLabel(saveLabel);
260 lastLabel = list->getLabel();
263 list = input->getListVector(); //get new list vector to process
266 if (m->control_pressed) {
267 delete input; delete groupMap;
268 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
269 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
273 //output error messages about any remaining user labels
274 set<string>::iterator it;
275 bool needToRun = false;
276 for (it = userLabels.begin(); it != userLabels.end(); it++) {
277 m->mothurOut("Your file does not include the label " + *it);
278 if (processedLabels.count(lastLabel) != 1) {
279 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
282 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
287 if (m->control_pressed) {
288 delete input; delete groupMap;
289 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
290 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
294 //run last label if you need to
295 if (needToRun == true) {
296 if (list != NULL) { delete list; }
297 list = input->getListVector(lastLabel); //get new list vector to process
299 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
305 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
306 (*(filehandles[it3->first])).close();
314 if (m->control_pressed) {
315 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
319 //set fasta file as new current fastafile
321 itTypes = outputTypes.find("list");
322 if (itTypes != outputTypes.end()) {
323 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
326 m->mothurOutEndLine();
327 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
328 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
329 m->mothurOutEndLine();
333 catch(exception& e) {
334 m->errorOut(e, "ParseListCommand", "execute");
338 /**********************************************************************************************************************/
339 int ParseListCommand::parse(ListVector* thisList) {
342 map<string, string> groupVector;
343 map<string, string>::iterator itGroup;
344 map<string, int> groupNumBins;
347 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
348 groupNumBins[it3->first] = 0;
349 groupVector[it3->first] = "";
353 for (int i = 0; i < thisList->getNumBins(); i++) {
354 if (m->control_pressed) { return 0; }
356 map<string, string> groupBins;
357 string bin = list->get(i);
359 vector<string> names;
360 m->splitAtComma(bin, names); //parses bin into individual sequence names
362 //parse bin into list of sequences in each group
363 for (int j = 0; j < names.size(); j++) {
364 string group = groupMap->getGroup(names[j]);
366 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
368 itGroup = groupBins.find(group);
369 if(itGroup == groupBins.end()) {
370 groupBins[group] = names[j]; //add first name
371 groupNumBins[group]++;
372 }else{ //add another name
373 groupBins[group] = groupBins[group] + "," + names[j];
377 //print parsed bin info to files
378 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
379 groupVector[itGroup->first] += itGroup->second + '\t';
385 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
386 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
392 catch(exception& e) {
393 m->errorOut(e, "ParseListCommand", "parse");
398 /**********************************************************************************************************************/