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(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["bootgroup"] = tempOutNames;
33 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
37 //**********************************************************************************************************************
38 vector<string> GetgroupCommand::getRequiredParameters(){
40 vector<string> myArray;
44 m->errorOut(e, "GetgroupCommand", "getRequiredParameters");
48 //**********************************************************************************************************************
49 vector<string> GetgroupCommand::getRequiredFiles(){
51 string Array[] = {"shared"};
52 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
56 m->errorOut(e, "GetgroupCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 GetgroupCommand::GetgroupCommand(string option) {
63 globaldata = GlobalData::getInstance();
66 //allow user to run help
67 if(option == "help") { help(); abort = true; }
70 //valid paramters for this command
71 string Array[] = {"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 //check to make sure all parameters are valid for command
79 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
80 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
83 //initialize outputTypes
84 vector<string> tempOutNames;
85 outputTypes["bootgroup"] = tempOutNames;
87 //if the user changes the output directory command factory will send this info to us in the output parameter
88 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
90 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; }
94 sharedfile = globaldata->getSharedFile();
95 m->openInputFile(sharedfile, in);
98 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
99 outputFile = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "bootGroups";
100 m->openOutputFile(outputFile, out);
105 catch(exception& e) {
106 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
110 //**********************************************************************************************************************
112 void GetgroupCommand::help(){
114 m->mothurOut("The get.group command can only be executed after a successful read.otu command.\n");
115 //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");
116 m->mothurOut("You may not use any parameters with the get.group command.\n");
117 m->mothurOut("The get.group command should be in the following format: \n");
118 m->mothurOut("get.group()\n");
119 m->mothurOut("Example get.group().\n");
122 catch(exception& e) {
123 m->errorOut(e, "GetgroupCommand", "help");
128 //**********************************************************************************************************************
130 GetgroupCommand::~GetgroupCommand(){
133 //**********************************************************************************************************************
135 int GetgroupCommand::execute(){
138 if (abort == true) { return 0; }
140 int num, inputData, count;
142 string holdLabel, nextLabel, groupN, label;
144 //read in first row since you know there is at least 1 group.
145 in >> label >> groupN >> num;
149 m->mothurOut(groupN); m->mothurOutEndLine();
150 out << groupN << '\t' << groupN << endl;
153 for(int i=0;i<num;i++){
157 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(outputFile.c_str()); return 0; }
159 if (in.eof() != true) { in >> nextLabel; }
161 //read the rest of the groups info in
162 while ((nextLabel == holdLabel) && (in.eof() != true)) {
163 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(outputFile.c_str()); return 0; }
169 m->mothurOut(groupN); m->mothurOutEndLine();
170 out << groupN << '\t' << groupN << endl;
173 for(int i=0;i<num;i++){
177 if (in.eof() != true) { in >> nextLabel; }
183 if (m->control_pressed) { remove(outputFile.c_str()); return 0; }
185 m->mothurOutEndLine();
186 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
187 m->mothurOut(outputFile); m->mothurOutEndLine(); outputNames.push_back(outputFile); outputTypes["bootgroup"].push_back(outputFile);
188 m->mothurOutEndLine();
193 catch(exception& e) {
194 m->errorOut(e, "GetgroupCommand", "execute");