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(){
28 abort = true; calledHelp = true;
29 vector<string> tempOutNames;
30 outputTypes["list"] = tempOutNames;
33 m->errorOut(e, "ParseListCommand", "ParseListCommand");
37 //**********************************************************************************************************************
38 vector<string> ParseListCommand::getRequiredParameters(){
40 string Array[] = {"list","group"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "ParseListCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> ParseListCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "ParseListCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 ParseListCommand::ParseListCommand(string option) {
63 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 //valid paramters for this command
71 string Array[] = {"list","group", "label", "outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
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") { listfile = ""; }
120 groupfile = validParameter.validFile(parameters, "group", true);
121 if (groupfile == "not open") { abort = true; }
122 else if (groupfile == "not found") { groupfile = ""; }
124 groupMap = new GroupMap(groupfile);
126 int error = groupMap->readMap();
127 if (error == 1) { abort = true; }
130 //do you have all files needed
131 if ((listfile == "") || (groupfile == "")) { m->mothurOut("You must enter both a listfile and groupfile for the parse.list command. "); m->mothurOutEndLine(); abort = true; }
133 //check for optional parameter and set defaults
134 // ...at some point should added some additional type checking...
135 label = validParameter.validFile(parameters, "label", false);
136 if (label == "not found") { label = ""; allLines = 1; }
138 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
139 else { allLines = 1; }
144 catch(exception& e) {
145 m->errorOut(e, "ParseListCommand", "ParseListCommand");
149 //**********************************************************************************************************************
150 void ParseListCommand::help(){
152 m->mothurOut("The parse.list command reads a list and group file and generates a list file for each group in the groupfile. \n");
153 m->mothurOut("The parse.list command parameters are list, group and label.\n");
154 m->mothurOut("The list and group parameters are required.\n");
155 m->mothurOut("The label parameter is used to read specific labels in your input you want to use.\n");
156 m->mothurOut("The parse.list command should be used in the following format: parse.list(list=yourListFile, group=yourGroupFile, label=yourLabels).\n");
157 m->mothurOut("Example: parse.list(list=abrecovery.fn.list, group=abrecovery.groups, label=0.03).\n");
158 m->mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
161 catch(exception& e) {
162 m->errorOut(e, "ParseListCommand", "help");
166 //**********************************************************************************************************************
167 ParseListCommand::~ParseListCommand(){}
168 //**********************************************************************************************************************
169 int ParseListCommand::execute(){
172 if (abort == true) { if (calledHelp) { return 0; } return 2; }
175 string fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
177 //fill filehandles with neccessary ofstreams
180 for (i=0; i<groupMap->namesOfGroups.size(); i++) {
182 filehandles[groupMap->namesOfGroups[i]] = temp;
184 string filename = fileroot + groupMap->namesOfGroups[i] + ".list";
185 outputNames.push_back(filename); outputTypes["list"].push_back(filename);
186 m->openOutputFile(filename, *temp);
189 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
190 set<string> processedLabels;
191 set<string> userLabels = labels;
193 input = new InputData(listfile, "list");
194 list = input->getListVector();
195 string lastLabel = list->getLabel();
197 if (m->control_pressed) {
198 delete input; delete list; delete groupMap;
199 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
200 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
204 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
206 if (m->control_pressed) {
207 delete input; delete list; delete groupMap;
208 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
209 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
213 if(allLines == 1 || labels.count(list->getLabel()) == 1){
215 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
218 processedLabels.insert(list->getLabel());
219 userLabels.erase(list->getLabel());
222 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
223 string saveLabel = list->getLabel();
226 list = input->getListVector(lastLabel); //get new list vector to process
228 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
231 processedLabels.insert(list->getLabel());
232 userLabels.erase(list->getLabel());
234 //restore real lastlabel to save below
235 list->setLabel(saveLabel);
239 lastLabel = list->getLabel();
242 list = input->getListVector(); //get new list vector to process
245 if (m->control_pressed) {
246 delete input; delete groupMap;
247 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
248 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
252 //output error messages about any remaining user labels
253 set<string>::iterator it;
254 bool needToRun = false;
255 for (it = userLabels.begin(); it != userLabels.end(); it++) {
256 m->mothurOut("Your file does not include the label " + *it);
257 if (processedLabels.count(lastLabel) != 1) {
258 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
261 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
266 if (m->control_pressed) {
267 delete input; delete groupMap;
268 for (i=0; i<groupMap->namesOfGroups.size(); i++) { (*(filehandles[groupMap->namesOfGroups[i]])).close(); delete filehandles[groupMap->namesOfGroups[i]]; }
269 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
273 //run last label if you need to
274 if (needToRun == true) {
275 if (list != NULL) { delete list; }
276 list = input->getListVector(lastLabel); //get new list vector to process
278 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
284 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
285 (*(filehandles[it3->first])).close();
293 if (m->control_pressed) {
294 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear();
298 //set fasta file as new current fastafile
300 itTypes = outputTypes.find("list");
301 if (itTypes != outputTypes.end()) {
302 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
305 m->mothurOutEndLine();
306 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
307 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
308 m->mothurOutEndLine();
312 catch(exception& e) {
313 m->errorOut(e, "ParseListCommand", "execute");
317 /**********************************************************************************************************************/
318 int ParseListCommand::parse(ListVector* thisList) {
321 map<string, string> groupVector;
322 map<string, string>::iterator itGroup;
323 map<string, int> groupNumBins;
326 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
327 groupNumBins[it3->first] = 0;
328 groupVector[it3->first] = "";
332 for (int i = 0; i < thisList->getNumBins(); i++) {
333 if (m->control_pressed) { return 0; }
335 map<string, string> groupBins;
336 string bin = list->get(i);
338 vector<string> names;
339 m->splitAtComma(bin, names); //parses bin into individual sequence names
341 //parse bin into list of sequences in each group
342 for (int j = 0; j < names.size(); j++) {
343 string group = groupMap->getGroup(names[j]);
345 if (group == "not found") { m->mothurOut(names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); exit(1); }
347 itGroup = groupBins.find(group);
348 if(itGroup == groupBins.end()) {
349 groupBins[group] = names[j]; //add first name
350 groupNumBins[group]++;
351 }else{ //add another name
352 groupBins[group] = groupBins[group] + "," + names[j];
356 //print parsed bin info to files
357 for (itGroup = groupBins.begin(); itGroup != groupBins.end(); itGroup++) {
358 groupVector[itGroup->first] += itGroup->second + '\t';
364 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
365 (*(filehandles[it3->first])) << thisList->getLabel() << '\t' << groupNumBins[it3->first] << '\t' << groupVector[it3->first] << endl; // label numBins listvector for that group
371 catch(exception& e) {
372 m->errorOut(e, "ParseListCommand", "parse");
377 /**********************************************************************************************************************/