]> git.donarmstrong.com Git - mothur.git/blob - getgroupcommand.cpp
working on current change
[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::setParameters(){        
14         try {
15                 CommandParameter pshared("shared", "InputTypes", "", "current", "none", "none", "none",false,true); parameters.push_back(pshared);
16                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
17                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
18                 
19                 vector<string> myArray;
20                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
21                 return myArray;
22         }
23         catch(exception& e) {
24                 m->errorOut(e, "GetgroupCommand", "setParameters");
25                 exit(1);
26         }
27 }
28 //**********************************************************************************************************************
29 string GetgroupCommand::getHelpString(){        
30         try {
31                 string helpString = "";
32                 helpString += "The get.group command parameter is shared and it's required if you have no valid current file.\n";
33                 //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");
34                 helpString += "You may not use any parameters with the get.group command.\n";
35                 helpString += "The get.group command should be in the following format: \n";
36                 helpString += "get.group()\n";
37                 helpString += "Example get.group().\n";
38                 return helpString;
39         }
40         catch(exception& e) {
41                 m->errorOut(e, "GetgroupCommand", "getHelpString");
42                 exit(1);
43         }
44 }
45 //**********************************************************************************************************************
46 GetgroupCommand::GetgroupCommand(){     
47         try {
48                 abort = true; calledHelp = true; 
49                 setParameters();
50                 vector<string> tempOutNames;
51                 outputTypes["bootgroup"] = tempOutNames;
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
55                 exit(1);
56         }
57 }
58 //**********************************************************************************************************************
59 GetgroupCommand::GetgroupCommand(string option)  {
60         try {
61                 abort = false; calledHelp = false;   
62                 
63                 //allow user to run help
64                 if(option == "help") { help(); abort = true; calledHelp = true; }
65                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
66                 
67                 else {
68                         vector<string> myArray = setParameters();
69                         
70                         OptionParser parser(option);
71                         map<string,string> parameters = parser.getParameters();
72                         map<string,string>::iterator it;
73                         
74                         ValidParameters validParameter;
75                         //check to make sure all parameters are valid for command
76                         for (it = parameters.begin(); it != parameters.end(); it++) { 
77                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
78                         }
79                         
80                         //initialize outputTypes
81                         vector<string> tempOutNames;
82                         outputTypes["bootgroup"] = tempOutNames;
83                         
84                         //if the user changes the input directory command factory will send this info to us in the output parameter 
85                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
86                         if (inputDir == "not found"){   inputDir = "";          }
87                         else {
88                                 string path;
89                                 it = parameters.find("shared");
90                                 //user has given a template file
91                                 if(it != parameters.end()){ 
92                                         path = m->hasPath(it->second);
93                                         //if the user has not given a path then, add inputdir. else leave path alone.
94                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
95                                 }
96                         }
97                         
98                         //get shared file
99                         sharedfile = validParameter.validFile(parameters, "shared", true);
100                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
101                         else if (sharedfile == "not found") { 
102                                 //if there is a current shared file, use it
103                                 sharedfile = m->getSharedFile(); 
104                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
105                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
106                         }else { m->setSharedFile(sharedfile); }
107                         
108                         
109                         //if the user changes the output directory command factory will send this info to us in the output parameter 
110                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(sharedfile);             }
111                 }
112         }
113         catch(exception& e) {
114                 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
115                 exit(1);
116         }
117 }
118 //**********************************************************************************************************************
119
120 int GetgroupCommand::execute(){
121         try {
122         
123                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
124                 
125                 //open shared file
126                 m->openInputFile(sharedfile, in);
127                         
128                 //open output file
129                 outputFile = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "bootGroups";
130                 m->openOutputFile(outputFile, out);
131                         
132                 int num, inputData, count;
133                 count = 0;  
134                 string holdLabel, nextLabel, groupN, label;
135                 
136                 //read in first row since you know there is at least 1 group.
137                 in >> label >> groupN >> num;
138                 holdLabel = label;
139                 
140                 //output first group
141                 m->mothurOut(groupN); m->mothurOutEndLine();
142                 out << groupN << '\t' << groupN << endl;        
143                         
144                 //get rest of line
145                 for(int i=0;i<num;i++){
146                         in >> inputData;
147                 }
148                 
149                 if (m->control_pressed) { outputTypes.clear(); in.close();  out.close(); remove(outputFile.c_str());   return 0; }
150
151                 if (in.eof() != true) { in >> nextLabel; }
152                 
153                 //read the rest of the groups info in
154                 while ((nextLabel == holdLabel) && (in.eof() != true)) {
155                         if (m->control_pressed) {  outputTypes.clear(); in.close();  out.close(); remove(outputFile.c_str());   return 0; }
156                         
157                         in >> groupN >> num;
158                         count++;
159                         
160                         //output next group
161                         m->mothurOut(groupN); m->mothurOutEndLine();
162                         out << groupN << '\t' << groupN << endl;                                
163                         
164                         //fill vector.  
165                         for(int i=0;i<num;i++){
166                                 in >> inputData;
167                         }
168                         
169                         if (in.eof() != true) { in >> nextLabel; }
170                 }
171                 
172                 in.close();
173                 out.close();
174                 
175                 if (m->control_pressed) {  remove(outputFile.c_str());   return 0; }
176                 
177                 m->mothurOutEndLine();
178                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
179                 m->mothurOut(outputFile); m->mothurOutEndLine();        outputNames.push_back(outputFile); outputTypes["bootgroup"].push_back(outputFile);
180                 m->mothurOutEndLine();
181                 
182                 return 0;       
183         }
184
185         catch(exception& e) {
186                 m->errorOut(e, "GetgroupCommand", "execute");
187                 exit(1);
188         }
189 }
190
191