5 * Created by Thomas Ryabin on 2/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getgroupcommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetgroupCommand::setParameters(){
15 CommandParameter pshared("shared", "InputTypes", "", "current", "none", "none", "none",false,true); parameters.push_back(pshared);
16 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
17 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
19 vector<string> myArray;
20 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
24 m->errorOut(e, "GetgroupCommand", "setParameters");
28 //**********************************************************************************************************************
29 string GetgroupCommand::getHelpString(){
31 string helpString = "";
32 helpString += "The get.group command parameter is shared and it's required if you have no valid current file.\n";
33 //m->mothurOut("The get.group command outputs a .bootGroups file to you can use in addition to the tree file generated by the bootstrap.shared command to run the consensus command.\n");
34 helpString += "You may not use any parameters with the get.group command.\n";
35 helpString += "The get.group command should be in the following format: \n";
36 helpString += "get.group()\n";
37 helpString += "Example get.group().\n";
41 m->errorOut(e, "GetgroupCommand", "getHelpString");
45 //**********************************************************************************************************************
46 GetgroupCommand::GetgroupCommand(){
48 abort = true; calledHelp = true;
50 vector<string> tempOutNames;
51 outputTypes["bootgroup"] = tempOutNames;
54 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
58 //**********************************************************************************************************************
59 GetgroupCommand::GetgroupCommand(string option) {
61 abort = false; calledHelp = false;
63 //allow user to run help
64 if(option == "help") { help(); abort = true; calledHelp = true; }
67 vector<string> myArray = setParameters();
69 OptionParser parser(option);
70 map<string,string> parameters = parser.getParameters();
71 map<string,string>::iterator it;
73 ValidParameters validParameter;
74 //check to make sure all parameters are valid for command
75 for (it = parameters.begin(); it != parameters.end(); it++) {
76 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
79 //initialize outputTypes
80 vector<string> tempOutNames;
81 outputTypes["bootgroup"] = tempOutNames;
83 //if the user changes the input directory command factory will send this info to us in the output parameter
84 string inputDir = validParameter.validFile(parameters, "inputdir", false);
85 if (inputDir == "not found"){ inputDir = ""; }
88 it = parameters.find("shared");
89 //user has given a template file
90 if(it != parameters.end()){
91 path = m->hasPath(it->second);
92 //if the user has not given a path then, add inputdir. else leave path alone.
93 if (path == "") { parameters["shared"] = inputDir + it->second; }
98 sharedfile = validParameter.validFile(parameters, "shared", true);
99 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
100 else if (sharedfile == "not found") {
101 //if there is a current shared file, use it
102 sharedfile = m->getSharedFile();
103 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
104 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
108 //if the user changes the output directory command factory will send this info to us in the output parameter
109 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(sharedfile); }
112 catch(exception& e) {
113 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
117 //**********************************************************************************************************************
119 int GetgroupCommand::execute(){
122 if (abort == true) { if (calledHelp) { return 0; } return 2; }
125 m->openInputFile(sharedfile, in);
128 outputFile = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "bootGroups";
129 m->openOutputFile(outputFile, out);
131 int num, inputData, count;
133 string holdLabel, nextLabel, groupN, label;
135 //read in first row since you know there is at least 1 group.
136 in >> label >> groupN >> num;
140 m->mothurOut(groupN); m->mothurOutEndLine();
141 out << groupN << '\t' << groupN << endl;
144 for(int i=0;i<num;i++){
148 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(outputFile.c_str()); return 0; }
150 if (in.eof() != true) { in >> nextLabel; }
152 //read the rest of the groups info in
153 while ((nextLabel == holdLabel) && (in.eof() != true)) {
154 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(outputFile.c_str()); return 0; }
160 m->mothurOut(groupN); m->mothurOutEndLine();
161 out << groupN << '\t' << groupN << endl;
164 for(int i=0;i<num;i++){
168 if (in.eof() != true) { in >> nextLabel; }
174 if (m->control_pressed) { remove(outputFile.c_str()); return 0; }
176 m->mothurOutEndLine();
177 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
178 m->mothurOut(outputFile); m->mothurOutEndLine(); outputNames.push_back(outputFile); outputTypes["bootgroup"].push_back(outputFile);
179 m->mothurOutEndLine();
184 catch(exception& e) {
185 m->errorOut(e, "GetgroupCommand", "execute");