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; }
71 vector<string> myArray = setParameters();
73 OptionParser parser(option);
74 map<string, string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 map<string, string>::iterator it;
79 //check to make sure all parameters are valid for command
80 for (it = parameters.begin(); it != parameters.end(); it++) {
81 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["list"] = tempOutNames;
88 //if the user changes the input directory command factory will send this info to us in the output parameter
89 string inputDir = validParameter.validFile(parameters, "inputdir", false);
90 if (inputDir == "not found"){ inputDir = ""; }
93 it = parameters.find("list");
94 //user has given a template file
95 if(it != parameters.end()){
96 path = m->hasPath(it->second);
97 //if the user has not given a path then, add inputdir. else leave path alone.
98 if (path == "") { parameters["list"] = inputDir + it->second; }
101 it = parameters.find("group");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["group"] = inputDir + it->second; }
111 //if the user changes the output directory command factory will send this info to us in the output parameter
112 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
114 //check for required parameters
115 listfile = validParameter.validFile(parameters, "list", true);
116 if (listfile == "not open") { abort = true; }
117 else if (listfile == "not found") {
118 listfile = m->getListFile();
119 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
121 m->mothurOut("No valid current list file. You must provide a list file."); m->mothurOutEndLine();
127 groupfile = validParameter.validFile(parameters, "group", true);
128 if (groupfile == "not open") { abort = true; }
129 else if (groupfile == "not found") {
130 groupfile = m->getListFile();
131 if (groupfile != "") {
132 m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine();
133 groupMap = new GroupMap(groupfile);
135 int error = groupMap->readMap();
136 if (error == 1) { abort = true; }
137 }else { m->mothurOut("No valid current group file. You must provide a group file."); m->mothurOutEndLine(); abort = true; }
139 groupMap = new GroupMap(groupfile);
141 int error = groupMap->readMap();
142 if (error == 1) { abort = true; }
145 //do you have all files needed
146 if ((listfile == "") || (groupfile == "")) { m->mothurOut("You must enter both a listfile and groupfile for the parse.list command. "); m->mothurOutEndLine(); abort = true; }
148 //check for optional parameter and set defaults
149 // ...at some point should added some additional type checking...
150 label = validParameter.validFile(parameters, "label", false);
151 if (label == "not found") { label = ""; allLines = 1; }
153 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
154 else { allLines = 1; }
159 catch(exception& e) {
160 m->errorOut(e, "ParseListCommand", "ParseListCommand");
164 //**********************************************************************************************************************
165 int ParseListCommand::execute(){
168 if (abort == true) { if (calledHelp) { return 0; } return 2; }
171 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
173 //fill filehandles with neccessary ofstreams
176 for (i=0; i<groupMap->namesOfGroups.size(); i++) {
178 filehandles[groupMap->namesOfGroups[i]] = temp;
180 string filename = fileroot + groupMap->namesOfGroups[i] + ".list";
181 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
182 m->openOutputFile(filename, *temp);
185 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
186 set<string> processedLabels;
187 set<string> userLabels = labels;
189 input = new InputData(listfile, "list");
190 list = input->getListVector();
191 string lastLabel = list->getLabel();
193 if (m->control_pressed) {
194 delete input; delete list; delete groupMap;
195 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
196 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
200 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
202 if (m->control_pressed) {
203 delete input; delete list; delete groupMap;
204 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
205 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
209 if(allLines == 1 || labels.count(list->getLabel()) == 1){
211 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
214 processedLabels.insert(list->getLabel());
215 userLabels.erase(list->getLabel());
218 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
219 string saveLabel = list->getLabel();
222 list = input->getListVector(lastLabel); //get new list vector to process
224 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
227 processedLabels.insert(list->getLabel());
228 userLabels.erase(list->getLabel());
230 //restore real lastlabel to save below
231 list->setLabel(saveLabel);
235 lastLabel = list->getLabel();
238 list = input->getListVector(); //get new list vector to process
241 if (m->control_pressed) {
242 delete input; delete groupMap;
243 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
244 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
248 //output error messages about any remaining user labels
249 set<string>::iterator it;
250 bool needToRun = false;
251 for (it = userLabels.begin(); it != userLabels.end(); it++) {
252 m->mothurOut("Your file does not include the label " + *it);
253 if (processedLabels.count(lastLabel) != 1) {
254 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
257 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
262 if (m->control_pressed) {
263 delete input; delete groupMap;
264 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
265 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
269 //run last label if you need to
270 if (needToRun == true) {
271 if (list != NULL) { delete list; }
272 list = input->getListVector(lastLabel); //get new list vector to process
274 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
280 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
281 (*(filehandles[it3->first])).close();
289 if (m->control_pressed) {
290 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
294 //set fasta file as new current fastafile
296 itTypes = outputTypes.find("list");
297 if (itTypes != outputTypes.end()) {
298 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
301 m->mothurOutEndLine();
302 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
303 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
304 m->mothurOutEndLine();
308 catch(exception& e) {
309 m->errorOut(e, "ParseListCommand", "execute");
313 /**********************************************************************************************************************/
314 int ParseListCommand::parse(ListVector* thisList) {
317 map<string, string> groupVector;
318 map<string, string>::iterator itGroup;
319 map<string, int> groupNumBins;
322 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
323 groupNumBins[it3->first] = 0;
324 groupVector[it3->first] = "";
328 for (int i = 0; i < thisList->getNumBins(); i++) {
329 if (m->control_pressed) { return 0; }
331 map<string, string> groupBins;
332 string bin = list->get(i);
334 vector<string> names;
335 m->splitAtComma(bin, names); //parses bin into individual sequence names
337 //parse bin into list of sequences in each group
338 for (int j = 0; j < names.size(); j++) {
339 string group = groupMap->getGroup(names[j]);
341 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
343 itGroup = groupBins.find(group);
344 if(itGroup == groupBins.end()) {
345 groupBins[group] = names[j]; //add first name
346 groupNumBins[group]++;
347 }else{ //add another name
348 groupBins[group] = groupBins[group] + "," + names[j];
352 //print parsed bin info to files
353 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
354 groupVector[itGroup->first] += itGroup->second + '\t';
360 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
361 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
367 catch(exception& e) {
368 m->errorOut(e, "ParseListCommand", "parse");
373 /**********************************************************************************************************************/