]> git.donarmstrong.com Git - mothur.git/blob - getgroupcommand.cpp
moved utilities out of mothur.h and into mothurOut class.
[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 GetgroupCommand::GetgroupCommand(string option)  {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                 
18                 //allow user to run help
19                 if(option == "help") { help(); abort = true; }
20                 
21                 else {
22                         //valid paramters for this command
23                         string Array[] =  {"outputdir","inputdir"};
24                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25                         
26                         OptionParser parser(option);
27                         map<string,string> parameters = parser.getParameters();
28                         
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;  }
33                         }
34                         
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 = "";         }
37                         
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; }
39                                 
40                         if (abort == false) {
41                                 //open shared file
42                                 sharedfile = globaldata->getSharedFile();
43                                 m->openInputFile(sharedfile, in);
44                 
45                                 //open output file
46                                 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
47                                 outputFile = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "bootGroups";
48                                 m->openOutputFile(outputFile, out);
49
50                         }
51                 }
52         }
53         catch(exception& e) {
54                 m->errorOut(e, "GetgroupCommand", "GetgroupCommand");
55                 exit(1);
56         }
57 }
58 //**********************************************************************************************************************
59
60 void GetgroupCommand::help(){
61         try {
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");
68                 
69         }
70         catch(exception& e) {
71                 m->errorOut(e, "GetgroupCommand", "help");
72                 exit(1);
73         }
74 }
75
76 //**********************************************************************************************************************
77
78 GetgroupCommand::~GetgroupCommand(){
79 }
80
81 //**********************************************************************************************************************
82
83 int GetgroupCommand::execute(){
84         try {
85         
86                 if (abort == true) { return 0; }
87         
88                 int num, inputData, count;
89                 count = 0;  
90                 string holdLabel, nextLabel, groupN, label;
91                 
92                 //read in first row since you know there is at least 1 group.
93                 in >> label >> groupN >> num;
94                 holdLabel = label;
95                 
96                 //output first group
97                 m->mothurOut(groupN); m->mothurOutEndLine();
98                 out << groupN << '\t' << groupN << endl;        
99                         
100                 //get rest of line
101                 for(int i=0;i<num;i++){
102                         in >> inputData;
103                 }
104                 
105                 if (m->control_pressed) { in.close();  out.close(); remove(outputFile.c_str());   return 0; }
106
107                 if (in.eof() != true) { in >> nextLabel; }
108                 
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; }
112                         
113                         in >> groupN >> num;
114                         count++;
115                         
116                         //output next group
117                         m->mothurOut(groupN); m->mothurOutEndLine();
118                         out << groupN << '\t' << groupN << endl;                                
119                         
120                         //fill vector.  
121                         for(int i=0;i<num;i++){
122                                 in >> inputData;
123                         }
124                         
125                         if (in.eof() != true) { in >> nextLabel; }
126                 }
127                 
128                 in.close();
129                 out.close();
130                 
131                 if (m->control_pressed) {  remove(outputFile.c_str());   return 0; }
132                 
133                 m->mothurOutEndLine();
134                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
135                 m->mothurOut(outputFile); m->mothurOutEndLine();        
136                 m->mothurOutEndLine();
137                 
138                 return 0;       
139         }
140
141         catch(exception& e) {
142                 m->errorOut(e, "GetgroupCommand", "execute");
143                 exit(1);
144         }
145 }
146
147