]> git.donarmstrong.com Git - mothur.git/blob - getgroupcommand.cpp
added [ERROR] flag if command aborts
[mothur.git] / getgroupcommand.cpp
1 /*
2  *  getgroupcommand.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 2/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "getgroupcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetgroupCommand::getValidParameters(){   
14         try {
15                 string Array[] =  {"outputdir","inputdir"};
16                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
17                 return myArray;
18         }
19         catch(exception& e) {
20                 m->errorOut(e, "GetgroupCommand", "getValidParameters");
21                 exit(1);
22         }
23 }
24 //**********************************************************************************************************************
25 GetgroupCommand::GetgroupCommand(){     
26         try {
27                 abort = true; calledHelp = true; 
28                 vector<string> tempOutNames;
29                 outputTypes["bootgroup"] = tempOutNames;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 vector<string> GetgroupCommand::getRequiredParameters(){        
38         try {
39                 vector<string> myArray;
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "GetgroupCommand", "getRequiredParameters");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 vector<string> GetgroupCommand::getRequiredFiles(){     
49         try {
50                 string Array[] =  {"shared"};
51                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52                 return myArray;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "GetgroupCommand", "getRequiredFiles");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60 GetgroupCommand::GetgroupCommand(string option)  {
61         try {
62                 globaldata = GlobalData::getInstance();
63                 abort = false; calledHelp = false;   
64                 
65                 //allow user to run help
66                 if(option == "help") { help(); abort = true; calledHelp = true; }
67                 
68                 else {
69                         //valid paramters for this command
70                         string Array[] =  {"outputdir","inputdir"};
71                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
72                         
73                         OptionParser parser(option);
74                         map<string,string> parameters = parser.getParameters();
75                         
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;  }
80                         }
81                         
82                         //initialize outputTypes
83                         vector<string> tempOutNames;
84                         outputTypes["bootgroup"] = tempOutNames;
85                         
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 = "";         }
88                         
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; }
90                                 
91                         if (abort == false) {
92                                 //open shared file
93                                 sharedfile = globaldata->getSharedFile();
94                                 m->openInputFile(sharedfile, in);
95                 
96                                 //open output file
97                                 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
98                                 outputFile = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "bootGroups";
99                                 m->openOutputFile(outputFile, out);
100
101                         }
102                 }
103         }
104         catch(exception& e) {
105                 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
106                 exit(1);
107         }
108 }
109 //**********************************************************************************************************************
110
111 void GetgroupCommand::help(){
112         try {
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");
119                 
120         }
121         catch(exception& e) {
122                 m->errorOut(e, "GetgroupCommand", "help");
123                 exit(1);
124         }
125 }
126
127 //**********************************************************************************************************************
128
129 GetgroupCommand::~GetgroupCommand(){
130 }
131
132 //**********************************************************************************************************************
133
134 int GetgroupCommand::execute(){
135         try {
136         
137                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
138         
139                 int num, inputData, count;
140                 count = 0;  
141                 string holdLabel, nextLabel, groupN, label;
142                 
143                 //read in first row since you know there is at least 1 group.
144                 in >> label >> groupN >> num;
145                 holdLabel = label;
146                 
147                 //output first group
148                 m->mothurOut(groupN); m->mothurOutEndLine();
149                 out << groupN << '\t' << groupN << endl;        
150                         
151                 //get rest of line
152                 for(int i=0;i<num;i++){
153                         in >> inputData;
154                 }
155                 
156                 if (m->control_pressed) { outputTypes.clear(); in.close();  out.close(); remove(outputFile.c_str());   return 0; }
157
158                 if (in.eof() != true) { in >> nextLabel; }
159                 
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; }
163                         
164                         in >> groupN >> num;
165                         count++;
166                         
167                         //output next group
168                         m->mothurOut(groupN); m->mothurOutEndLine();
169                         out << groupN << '\t' << groupN << endl;                                
170                         
171                         //fill vector.  
172                         for(int i=0;i<num;i++){
173                                 in >> inputData;
174                         }
175                         
176                         if (in.eof() != true) { in >> nextLabel; }
177                 }
178                 
179                 in.close();
180                 out.close();
181                 
182                 if (m->control_pressed) {  remove(outputFile.c_str());   return 0; }
183                 
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();
188                 
189                 return 0;       
190         }
191
192         catch(exception& e) {
193                 m->errorOut(e, "GetgroupCommand", "execute");
194                 exit(1);
195         }
196 }
197
198