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 ParseListCommand::ParseListCommand(){
51 abort = true; calledHelp = true;
53 vector<string> tempOutNames;
54 outputTypes["list"] = tempOutNames;
57 m->errorOut(e, "ParseListCommand", "ParseListCommand");
61 //**********************************************************************************************************************
62 ParseListCommand::ParseListCommand(string option) {
64 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
69 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
72 vector<string> myArray = setParameters();
74 OptionParser parser(option);
75 map<string, string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string, string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["list"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("list");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["list"] = inputDir + it->second; }
102 it = parameters.find("group");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["group"] = inputDir + it->second; }
112 //if the user changes the output directory command factory will send this info to us in the output parameter
113 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
115 //check for required parameters
116 listfile = validParameter.validFile(parameters, "list", true);
117 if (listfile == "not open") { abort = true; }
118 else if (listfile == "not found") {
119 listfile = m->getListFile();
120 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
122 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
126 }else { m->setListFile(listfile); }
128 groupfile = validParameter.validFile(parameters, "group", true);
129 if (groupfile == "not open") { abort = true; }
130 else if (groupfile == "not found") {
131 groupfile = m->getListFile();
132 if (groupfile != "") {
133 m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine();
134 groupMap = new GroupMap(groupfile);
136 int error = groupMap->readMap();
137 if (error == 1) { abort = true; }
138 }else { m->mothurOut("No valid current group file. You must provide a group file."); m->mothurOutEndLine(); abort = true; }
140 m->setGroupFile(groupfile);
141 groupMap = new GroupMap(groupfile);
143 int error = groupMap->readMap();
144 if (error == 1) { abort = true; }
147 //do you have all files needed
148 if ((listfile == "") || (groupfile == "")) { m->mothurOut("You must enter both a listfile and groupfile for the parse.list command. "); m->mothurOutEndLine(); abort = true; }
150 //check for optional parameter and set defaults
151 // ...at some point should added some additional type checking...
152 label = validParameter.validFile(parameters, "label", false);
153 if (label == "not found") { label = ""; allLines = 1; }
155 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
156 else { allLines = 1; }
161 catch(exception& e) {
162 m->errorOut(e, "ParseListCommand", "ParseListCommand");
166 //**********************************************************************************************************************
167 int ParseListCommand::execute(){
170 if (abort == true) { if (calledHelp) { return 0; } return 2; }
173 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
175 //fill filehandles with neccessary ofstreams
178 for (i=0; i<groupMap->namesOfGroups.size(); i++) {
180 filehandles[groupMap->namesOfGroups[i]] = temp;
182 string filename = fileroot + groupMap->namesOfGroups[i] + ".list";
183 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
184 m->openOutputFile(filename, *temp);
187 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
188 set<string> processedLabels;
189 set<string> userLabels = labels;
191 input = new InputData(listfile, "list");
192 list = input->getListVector();
193 string lastLabel = list->getLabel();
195 if (m->control_pressed) {
196 delete input; delete list; delete groupMap;
197 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
198 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
202 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
204 if (m->control_pressed) {
205 delete input; delete list; delete groupMap;
206 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
207 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
211 if(allLines == 1 || labels.count(list->getLabel()) == 1){
213 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
216 processedLabels.insert(list->getLabel());
217 userLabels.erase(list->getLabel());
220 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
221 string saveLabel = list->getLabel();
224 list = input->getListVector(lastLabel); //get new list vector to process
226 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
229 processedLabels.insert(list->getLabel());
230 userLabels.erase(list->getLabel());
232 //restore real lastlabel to save below
233 list->setLabel(saveLabel);
237 lastLabel = list->getLabel();
240 list = input->getListVector(); //get new list vector to process
243 if (m->control_pressed) {
244 delete input; delete groupMap;
245 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
246 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
250 //output error messages about any remaining user labels
251 set<string>::iterator it;
252 bool needToRun = false;
253 for (it = userLabels.begin(); it != userLabels.end(); it++) {
254 m->mothurOut("Your file does not include the label " + *it);
255 if (processedLabels.count(lastLabel) != 1) {
256 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
259 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
264 if (m->control_pressed) {
265 delete input; delete groupMap;
266 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
267 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
271 //run last label if you need to
272 if (needToRun == true) {
273 if (list != NULL) { delete list; }
274 list = input->getListVector(lastLabel); //get new list vector to process
276 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
282 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
283 (*(filehandles[it3->first])).close();
291 if (m->control_pressed) {
292 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
296 //set fasta file as new current fastafile
298 itTypes = outputTypes.find("list");
299 if (itTypes != outputTypes.end()) {
300 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
303 m->mothurOutEndLine();
304 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
305 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
306 m->mothurOutEndLine();
310 catch(exception& e) {
311 m->errorOut(e, "ParseListCommand", "execute");
315 /**********************************************************************************************************************/
316 int ParseListCommand::parse(ListVector* thisList) {
319 map<string, string> groupVector;
320 map<string, string>::iterator itGroup;
321 map<string, int> groupNumBins;
324 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
325 groupNumBins[it3->first] = 0;
326 groupVector[it3->first] = "";
330 for (int i = 0; i < thisList->getNumBins(); i++) {
331 if (m->control_pressed) { return 0; }
333 map<string, string> groupBins;
334 string bin = list->get(i);
336 vector<string> names;
337 m->splitAtComma(bin, names); //parses bin into individual sequence names
339 //parse bin into list of sequences in each group
340 for (int j = 0; j < names.size(); j++) {
341 string group = groupMap->getGroup(names[j]);
343 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
345 itGroup = groupBins.find(group);
346 if(itGroup == groupBins.end()) {
347 groupBins[group] = names[j]; //add first name
348 groupNumBins[group]++;
349 }else{ //add another name
350 groupBins[group] = groupBins[group] + "," + names[j];
354 //print parsed bin info to files
355 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
356 groupVector[itGroup->first] += itGroup->second + '\t';
362 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
363 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
369 catch(exception& e) {
370 m->errorOut(e, "ParseListCommand", "parse");
375 /**********************************************************************************************************************/