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