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::getValidParameters(){
15 string Array[] = {"list","group", "label", "outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "ParseListCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 ParseListCommand::ParseListCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["list"] = tempOutNames;
34 m->errorOut(e, "ParseListCommand", "ParseListCommand");
38 //**********************************************************************************************************************
39 vector<string> ParseListCommand::getRequiredParameters(){
41 string Array[] = {"list","group"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "ParseListCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ParseListCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "ParseListCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 ParseListCommand::ParseListCommand(string option) {
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"list","group", "label", "outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string, string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["list"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("list");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["list"] = inputDir + it->second; }
103 it = parameters.find("group");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["group"] = inputDir + it->second; }
113 //if the user changes the output directory command factory will send this info to us in the output parameter
114 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
116 //check for required parameters
117 listfile = validParameter.validFile(parameters, "list", true);
118 if (listfile == "not open") { abort = true; }
119 else if (listfile == "not found") { listfile = ""; }
121 groupfile = validParameter.validFile(parameters, "group", true);
122 if (groupfile == "not open") { abort = true; }
123 else if (groupfile == "not found") { groupfile = ""; }
125 groupMap = new GroupMap(groupfile);
127 int error = groupMap->readMap();
128 if (error == 1) { abort = true; }
131 //do you have all files needed
132 if ((listfile == "") || (groupfile == "")) { m->mothurOut("You must enter both a listfile and groupfile for the parse.list command. "); m->mothurOutEndLine(); abort = true; }
134 //check for optional parameter and set defaults
135 // ...at some point should added some additional type checking...
136 label = validParameter.validFile(parameters, "label", false);
137 if (label == "not found") { label = ""; allLines = 1; }
139 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
140 else { allLines = 1; }
145 catch(exception& e) {
146 m->errorOut(e, "ParseListCommand", "ParseListCommand");
150 //**********************************************************************************************************************
151 void ParseListCommand::help(){
153 m->mothurOut("The parse.list command reads a list and group file and generates a list file for each group in the groupfile. \n");
154 m->mothurOut("The parse.list command parameters are list, group and label.\n");
155 m->mothurOut("The list and group parameters are required.\n");
156 m->mothurOut("The label parameter is used to read specific labels in your input you want to use.\n");
157 m->mothurOut("The parse.list command should be used in the following format: parse.list(list=yourListFile, group=yourGroupFile, label=yourLabels).\n");
158 m->mothurOut("Example: parse.list(list=abrecovery.fn.list, group=abrecovery.groups, label=0.03).\n");
159 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
162 catch(exception& e) {
163 m->errorOut(e, "ParseListCommand", "help");
167 //**********************************************************************************************************************
168 ParseListCommand::~ParseListCommand(){}
169 //**********************************************************************************************************************
170 int ParseListCommand::execute(){
173 if (abort == true) { return 0; }
176 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
178 //fill filehandles with neccessary ofstreams
181 for (i=0; i<groupMap->namesOfGroups.size(); i++) {
183 filehandles[groupMap->namesOfGroups[i]] = temp;
185 string filename = fileroot + groupMap->namesOfGroups[i] + ".list";
186 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
187 m->openOutputFile(filename, *temp);
190 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
191 set<string> processedLabels;
192 set<string> userLabels = labels;
194 input = new InputData(listfile, "list");
195 list = input->getListVector();
196 string lastLabel = list->getLabel();
198 if (m->control_pressed) {
199 delete input; delete list; delete groupMap;
200 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
201 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
205 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
207 if (m->control_pressed) {
208 delete input; delete list; delete groupMap;
209 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
210 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
214 if(allLines == 1 || labels.count(list->getLabel()) == 1){
216 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
219 processedLabels.insert(list->getLabel());
220 userLabels.erase(list->getLabel());
223 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
224 string saveLabel = list->getLabel();
227 list = input->getListVector(lastLabel); //get new list vector to process
229 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
232 processedLabels.insert(list->getLabel());
233 userLabels.erase(list->getLabel());
235 //restore real lastlabel to save below
236 list->setLabel(saveLabel);
240 lastLabel = list->getLabel();
243 list = input->getListVector(); //get new list vector to process
246 if (m->control_pressed) {
247 delete input; delete groupMap;
248 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
249 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
253 //output error messages about any remaining user labels
254 set<string>::iterator it;
255 bool needToRun = false;
256 for (it = userLabels.begin(); it != userLabels.end(); it++) {
257 m->mothurOut("Your file does not include the label " + *it);
258 if (processedLabels.count(lastLabel) != 1) {
259 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
262 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
267 if (m->control_pressed) {
268 delete input; delete groupMap;
269 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
270 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
274 //run last label if you need to
275 if (needToRun == true) {
276 if (list != NULL) { delete list; }
277 list = input->getListVector(lastLabel); //get new list vector to process
279 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
285 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
286 (*(filehandles[it3->first])).close();
294 if (m->control_pressed) {
295 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
299 m->mothurOutEndLine();
300 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
301 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
302 m->mothurOutEndLine();
306 catch(exception& e) {
307 m->errorOut(e, "ParseListCommand", "execute");
311 /**********************************************************************************************************************/
312 int ParseListCommand::parse(ListVector* thisList) {
315 map<string, string> groupVector;
316 map<string, string>::iterator itGroup;
317 map<string, int> groupNumBins;
320 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
321 groupNumBins[it3->first] = 0;
322 groupVector[it3->first] = "";
326 for (int i = 0; i < thisList->getNumBins(); i++) {
327 if (m->control_pressed) { return 0; }
329 map<string, string> groupBins;
330 string bin = list->get(i);
332 vector<string> names;
333 m->splitAtComma(bin, names); //parses bin into individual sequence names
335 //parse bin into list of sequences in each group
336 for (int j = 0; j < names.size(); j++) {
337 string group = groupMap->getGroup(names[j]);
339 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
341 itGroup = groupBins.find(group);
342 if(itGroup == groupBins.end()) {
343 groupBins[group] = names[j]; //add first name
344 groupNumBins[group]++;
345 }else{ //add another name
346 groupBins[group] = groupBins[group] + "," + names[j];
350 //print parsed bin info to files
351 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
352 groupVector[itGroup->first] += itGroup->second + '\t';
358 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
359 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
365 catch(exception& e) {
366 m->errorOut(e, "ParseListCommand", "parse");
371 /**********************************************************************************************************************/