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],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);
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; }
209 map<string, string> variables;
210 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listfile));
212 //fill filehandles with neccessary ofstreams
215 vector<string> gGroups;
216 if (groupfile != "") { gGroups = groupMap->getNamesOfGroups(); }
217 else { gGroups = ct.getNamesOfGroups(); }
219 for (i=0; i<gGroups.size(); i++) {
221 filehandles[gGroups[i]] = temp;
223 variables["[group]"] = gGroups[i];
224 string filename = getOutputFileName("list",variables);
225 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
226 m->openOutputFile(filename, *temp);
229 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
230 set<string> processedLabels;
231 set<string> userLabels = labels;
233 InputData input(listfile, "list");
234 list = input.getListVector();
235 string lastLabel = list->getLabel();
237 if (m->control_pressed) {
238 delete list; if (groupfile != "") { delete groupMap; }
239 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
240 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
244 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
246 if (m->control_pressed) {
247 delete list; if (groupfile != "") { delete groupMap; }
248 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
249 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
253 if(allLines == 1 || labels.count(list->getLabel()) == 1){
255 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
258 processedLabels.insert(list->getLabel());
259 userLabels.erase(list->getLabel());
262 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
263 string saveLabel = list->getLabel();
265 list = input.getListVector(lastLabel); //get new list vector to process
267 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
270 processedLabels.insert(list->getLabel());
271 userLabels.erase(list->getLabel());
273 //restore real lastlabel to save below
274 list->setLabel(saveLabel);
278 lastLabel = list->getLabel();
281 list = input.getListVector(); //get new list vector to process
284 if (m->control_pressed) {
285 if (groupfile != "") { delete groupMap; }
286 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
287 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
291 //output error messages about any remaining user labels
292 set<string>::iterator it;
293 bool needToRun = false;
294 for (it = userLabels.begin(); it != userLabels.end(); it++) {
295 m->mothurOut("Your file does not include the label " + *it);
296 if (processedLabels.count(lastLabel) != 1) {
297 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
300 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
305 if (m->control_pressed) {
306 if (groupfile != "") { delete groupMap; }
307 for (i=0; i<gGroups.size(); i++) { (*(filehandles[gGroups[i]])).close(); delete filehandles[gGroups[i]]; }
308 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
312 //run last label if you need to
313 if (needToRun == true) {
314 if (list != NULL) { delete list; }
315 list = input.getListVector(lastLabel); //get new list vector to process
317 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
323 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
324 (*(filehandles[it3->first])).close();
328 if (groupfile != "") { delete groupMap; }
330 if (m->control_pressed) {
331 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
335 //set fasta file as new current fastafile
337 itTypes = outputTypes.find("list");
338 if (itTypes != outputTypes.end()) {
339 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
342 m->mothurOutEndLine();
343 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
344 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
345 m->mothurOutEndLine();
349 catch(exception& e) {
350 m->errorOut(e, "ParseListCommand", "execute");
354 /**********************************************************************************************************************/
355 int ParseListCommand::parse(ListVector* thisList) {
358 map<string, string> groupVector;
359 map<string, string>::iterator itGroup;
360 map<string, int> groupNumBins;
363 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
364 groupNumBins[it3->first] = 0;
365 groupVector[it3->first] = "";
369 for (int i = 0; i < thisList->getNumBins(); i++) {
370 if (m->control_pressed) { return 0; }
372 map<string, string> groupBins;
373 string bin = list->get(i);
375 vector<string> names;
376 m->splitAtComma(bin, names); //parses bin into individual sequence names
378 //parse bin into list of sequences in each group
379 for (int j = 0; j < names.size(); j++) {
380 if (groupfile != "") {
381 string group = groupMap->getGroup(names[j]);
383 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
385 itGroup = groupBins.find(group);
386 if(itGroup == groupBins.end()) {
387 groupBins[group] = names[j]; //add first name
388 groupNumBins[group]++;
389 }else{ //add another name
390 groupBins[group] = groupBins[group] + "," + names[j];
393 vector<string> thisSeqsGroups = ct.getGroups(names[j]);
395 for (int k = 0; k < thisSeqsGroups.size(); k++) {
396 string group = thisSeqsGroups[k];
397 itGroup = groupBins.find(group);
398 if(itGroup == groupBins.end()) {
399 groupBins[group] = names[j]; //add first name
400 groupNumBins[group]++;
401 }else{ //add another name
402 groupBins[group] = groupBins[group] + "," + names[j];
409 //print parsed bin info to files
410 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
411 groupVector[itGroup->first] += itGroup->second + '\t';
417 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
418 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
424 catch(exception& e) {
425 m->errorOut(e, "ParseListCommand", "parse");
430 /**********************************************************************************************************************/