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 GetgroupCommand::GetgroupCommand(string option) {
15 globaldata = GlobalData::getInstance();
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"outputdir","inputdir"};
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string,string> parameters = parser.getParameters();
29 ValidParameters validParameter;
30 //check to make sure all parameters are valid for command
31 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
32 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
35 //if the user changes the output directory command factory will send this info to us in the output parameter
36 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
38 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; }
42 sharedfile = globaldata->getSharedFile();
43 openInputFile(sharedfile, in);
46 if (outputDir == "") { outputDir += hasPath(sharedfile); }
47 outputFile = outputDir + getRootName(getSimpleName(sharedfile)) + "bootGroups";
48 openOutputFile(outputFile, out);
54 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
58 //**********************************************************************************************************************
60 void GetgroupCommand::help(){
62 m->mothurOut("The get.group command can only be executed after a successful read.otu command.\n");
63 //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");
64 m->mothurOut("You may not use any parameters with the get.group command.\n");
65 m->mothurOut("The get.group command should be in the following format: \n");
66 m->mothurOut("get.group()\n");
67 m->mothurOut("Example get.group().\n");
71 m->errorOut(e, "GetgroupCommand", "help");
76 //**********************************************************************************************************************
78 GetgroupCommand::~GetgroupCommand(){
81 //**********************************************************************************************************************
83 int GetgroupCommand::execute(){
86 if (abort == true) { return 0; }
88 int num, inputData, count;
90 string holdLabel, nextLabel, groupN, label;
92 //read in first row since you know there is at least 1 group.
93 in >> label >> groupN >> num;
97 m->mothurOut(groupN); m->mothurOutEndLine();
98 out << groupN << '\t' << groupN << endl;
101 for(int i=0;i<num;i++){
105 if (m->control_pressed) { in.close(); out.close(); remove(outputFile.c_str()); return 0; }
107 if (in.eof() != true) { in >> nextLabel; }
109 //read the rest of the groups info in
110 while ((nextLabel == holdLabel) && (in.eof() != true)) {
111 if (m->control_pressed) { in.close(); out.close(); remove(outputFile.c_str()); return 0; }
117 m->mothurOut(groupN); m->mothurOutEndLine();
118 out << groupN << '\t' << groupN << endl;
121 for(int i=0;i<num;i++){
125 if (in.eof() != true) { in >> nextLabel; }
131 if (m->control_pressed) { remove(outputFile.c_str()); return 0; }
133 m->mothurOutEndLine();
134 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
135 m->mothurOut(outputFile); m->mothurOutEndLine();
136 m->mothurOutEndLine();
141 catch(exception& e) {
142 m->errorOut(e, "GetgroupCommand", "execute");