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 pgroup("group", "InputTypes", "", "", "none", "none", "none","",false,true,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::getOutputPattern(string type) {
53 if (type == "list") { pattern = "[filename],[group],list"; }
54 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
59 m->errorOut(e, "ParseListCommand", "getOutputPattern");
63 //**********************************************************************************************************************
64 ParseListCommand::ParseListCommand(){
66 abort = true; calledHelp = true;
68 vector<string> tempOutNames;
69 outputTypes["list"] = tempOutNames;
72 m->errorOut(e, "ParseListCommand", "ParseListCommand");
76 //**********************************************************************************************************************
77 ParseListCommand::ParseListCommand(string option) {
79 abort = false; calledHelp = false;
82 //allow user to run help
83 if(option == "help") { help(); abort = true; calledHelp = true; }
84 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string, string> parameters = parser.getParameters();
92 ValidParameters validParameter;
93 map<string, string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["list"] = tempOutNames;
104 //if the user changes the input directory command factory will send this info to us in the output parameter
105 string inputDir = validParameter.validFile(parameters, "inputdir", false);
106 if (inputDir == "not found"){ inputDir = ""; }
109 it = parameters.find("list");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["list"] = inputDir + it->second; }
117 it = parameters.find("group");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["group"] = inputDir + it->second; }
127 //if the user changes the output directory command factory will send this info to us in the output parameter
128 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
130 //check for required parameters
131 listfile = validParameter.validFile(parameters, "list", true);
132 if (listfile == "not open") { abort = true; }
133 else if (listfile == "not found") {
134 listfile = m->getListFile();
135 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
137 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
141 }else { m->setListFile(listfile); }
143 groupfile = validParameter.validFile(parameters, "group", true);
144 if (groupfile == "not open") { abort = true; }
145 else if (groupfile == "not found") {
146 groupfile = m->getListFile();
147 if (groupfile != "") {
148 m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine();
149 groupMap = new GroupMap(groupfile);
151 int error = groupMap->readMap();
152 if (error == 1) { abort = true; }
153 }else { m->mothurOut("No valid current group file. You must provide a group file."); m->mothurOutEndLine(); abort = true; }
155 m->setGroupFile(groupfile);
156 groupMap = new GroupMap(groupfile);
158 int error = groupMap->readMap();
159 if (error == 1) { abort = true; }
162 //do you have all files needed
163 if ((listfile == "") || (groupfile == "")) { m->mothurOut("You must enter both a listfile and groupfile for the parse.list command. "); m->mothurOutEndLine(); abort = true; }
165 //check for optional parameter and set defaults
166 // ...at some point should added some additional type checking...
167 label = validParameter.validFile(parameters, "label", false);
168 if (label == "not found") { label = ""; allLines = 1; }
170 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
171 else { allLines = 1; }
176 catch(exception& e) {
177 m->errorOut(e, "ParseListCommand", "ParseListCommand");
181 //**********************************************************************************************************************
182 int ParseListCommand::execute(){
185 if (abort == true) { if (calledHelp) { return 0; } return 2; }
188 map<string, string> variables;
189 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listfile));
191 //fill filehandles with neccessary ofstreams
194 vector<string> gGroups = groupMap->getNamesOfGroups();
195 for (i=0; i<gGroups.size(); i++) {
197 filehandles[gGroups[i]] = temp;
199 variables["[group]"] = gGroups[i];
200 string filename = getOutputFileName("list",variables);
201 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
202 m->openOutputFile(filename, *temp);
205 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
206 set<string> processedLabels;
207 set<string> userLabels = labels;
209 input = new InputData(listfile, "list");
210 list = input->getListVector();
211 string lastLabel = list->getLabel();
213 if (m->control_pressed) {
214 delete input; delete list; delete groupMap;
215 vector<string> gGroups = groupMap->getNamesOfGroups();
216 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
217 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
221 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
223 if (m->control_pressed) {
224 delete input; delete list; delete groupMap;
225 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
226 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
230 if(allLines == 1 || labels.count(list->getLabel()) == 1){
232 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
235 processedLabels.insert(list->getLabel());
236 userLabels.erase(list->getLabel());
239 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
240 string saveLabel = list->getLabel();
243 list = input->getListVector(lastLabel); //get new list vector to process
245 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
248 processedLabels.insert(list->getLabel());
249 userLabels.erase(list->getLabel());
251 //restore real lastlabel to save below
252 list->setLabel(saveLabel);
256 lastLabel = list->getLabel();
259 list = input->getListVector(); //get new list vector to process
262 if (m->control_pressed) {
263 delete input; delete groupMap;
264 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
265 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
269 //output error messages about any remaining user labels
270 set<string>::iterator it;
271 bool needToRun = false;
272 for (it = userLabels.begin(); it != userLabels.end(); it++) {
273 m->mothurOut("Your file does not include the label " + *it);
274 if (processedLabels.count(lastLabel) != 1) {
275 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
278 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
283 if (m->control_pressed) {
284 delete input; delete groupMap;
285 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
286 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
290 //run last label if you need to
291 if (needToRun == true) {
292 if (list != NULL) { delete list; }
293 list = input->getListVector(lastLabel); //get new list vector to process
295 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
301 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
302 (*(filehandles[it3->first])).close();
310 if (m->control_pressed) {
311 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
315 //set fasta file as new current fastafile
317 itTypes = outputTypes.find("list");
318 if (itTypes != outputTypes.end()) {
319 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
322 m->mothurOutEndLine();
323 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
324 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
325 m->mothurOutEndLine();
329 catch(exception& e) {
330 m->errorOut(e, "ParseListCommand", "execute");
334 /**********************************************************************************************************************/
335 int ParseListCommand::parse(ListVector* thisList) {
338 map<string, string> groupVector;
339 map<string, string>::iterator itGroup;
340 map<string, int> groupNumBins;
343 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
344 groupNumBins[it3->first] = 0;
345 groupVector[it3->first] = "";
349 for (int i = 0; i < thisList->getNumBins(); i++) {
350 if (m->control_pressed) { return 0; }
352 map<string, string> groupBins;
353 string bin = list->get(i);
355 vector<string> names;
356 m->splitAtComma(bin, names); //parses bin into individual sequence names
358 //parse bin into list of sequences in each group
359 for (int j = 0; j < names.size(); j++) {
360 string group = groupMap->getGroup(names[j]);
362 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
364 itGroup = groupBins.find(group);
365 if(itGroup == groupBins.end()) {
366 groupBins[group] = names[j]; //add first name
367 groupNumBins[group]++;
368 }else{ //add another name
369 groupBins[group] = groupBins[group] + "," + names[j];
373 //print parsed bin info to files
374 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
375 groupVector[itGroup->first] += itGroup->second + '\t';
381 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
382 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
388 catch(exception& e) {
389 m->errorOut(e, "ParseListCommand", "parse");
394 /**********************************************************************************************************************/