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::getValidParameters(){
15 string Array[] = {"outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "GetgroupCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 GetgroupCommand::GetgroupCommand(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["bootgroup"] = tempOutNames;
32 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
36 //**********************************************************************************************************************
37 vector<string> GetgroupCommand::getRequiredParameters(){
39 vector<string> myArray;
43 m->errorOut(e, "GetgroupCommand", "getRequiredParameters");
47 //**********************************************************************************************************************
48 vector<string> GetgroupCommand::getRequiredFiles(){
50 string Array[] = {"shared"};
51 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 m->errorOut(e, "GetgroupCommand", "getRequiredFiles");
59 //**********************************************************************************************************************
60 GetgroupCommand::GetgroupCommand(string option) {
62 globaldata = GlobalData::getInstance();
63 abort = false; calledHelp = false;
65 //allow user to run help
66 if(option == "help") { help(); abort = true; calledHelp = true; }
69 //valid paramters for this command
70 string Array[] = {"outputdir","inputdir"};
71 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73 OptionParser parser(option);
74 map<string,string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 //check to make sure all parameters are valid for command
78 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
79 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
82 //initialize outputTypes
83 vector<string> tempOutNames;
84 outputTypes["bootgroup"] = tempOutNames;
86 //if the user changes the output directory command factory will send this info to us in the output parameter
87 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
89 if ((globaldata->getSharedFile() == "")) { m->mothurOut("You must use the read.otu command to read a groupfile or a sharedfile before you can use the get.group command."); m->mothurOutEndLine(); abort = true; }
93 sharedfile = globaldata->getSharedFile();
94 m->openInputFile(sharedfile, in);
97 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
98 outputFile = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "bootGroups";
99 m->openOutputFile(outputFile, out);
104 catch(exception& e) {
105 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
109 //**********************************************************************************************************************
111 void GetgroupCommand::help(){
113 m->mothurOut("The get.group command can only be executed after a successful read.otu command.\n");
114 //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");
115 m->mothurOut("You may not use any parameters with the get.group command.\n");
116 m->mothurOut("The get.group command should be in the following format: \n");
117 m->mothurOut("get.group()\n");
118 m->mothurOut("Example get.group().\n");
121 catch(exception& e) {
122 m->errorOut(e, "GetgroupCommand", "help");
127 //**********************************************************************************************************************
129 GetgroupCommand::~GetgroupCommand(){
132 //**********************************************************************************************************************
134 int GetgroupCommand::execute(){
137 if (abort == true) { if (calledHelp) { return 0; } return 2; }
139 int num, inputData, count;
141 string holdLabel, nextLabel, groupN, label;
143 //read in first row since you know there is at least 1 group.
144 in >> label >> groupN >> num;
148 m->mothurOut(groupN); m->mothurOutEndLine();
149 out << groupN << '\t' << groupN << endl;
152 for(int i=0;i<num;i++){
156 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(outputFile.c_str()); return 0; }
158 if (in.eof() != true) { in >> nextLabel; }
160 //read the rest of the groups info in
161 while ((nextLabel == holdLabel) && (in.eof() != true)) {
162 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(outputFile.c_str()); return 0; }
168 m->mothurOut(groupN); m->mothurOutEndLine();
169 out << groupN << '\t' << groupN << endl;
172 for(int i=0;i<num;i++){
176 if (in.eof() != true) { in >> nextLabel; }
182 if (m->control_pressed) { remove(outputFile.c_str()); return 0; }
184 m->mothurOutEndLine();
185 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
186 m->mothurOut(outputFile); m->mothurOutEndLine(); outputNames.push_back(outputFile); outputTypes["bootgroup"].push_back(outputFile);
187 m->mothurOutEndLine();
192 catch(exception& e) {
193 m->errorOut(e, "GetgroupCommand", "execute");