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