2 * countgroupscommand.cpp
5 * Created by westcott on 8/9/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "countgroupscommand.h"
11 #include "sharedutilities.h"
12 #include "inputdata.h"
14 //**********************************************************************************************************************
15 vector<string> CountGroupsCommand::setParameters(){
17 CommandParameter pshared("shared", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pshared);
18 CommandParameter pgroup("group", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pgroup);
19 CommandParameter pcount("count", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pcount);
20 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
21 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
22 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
23 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
25 vector<string> myArray;
26 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
30 m->errorOut(e, "CountGroupsCommand", "setParameters");
34 //**********************************************************************************************************************
35 string CountGroupsCommand::getOutputPattern(string type) {
39 if (type == "summary") { pattern = "[filename],count.summary"; }
40 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
45 m->errorOut(e, "PrimerDesignCommand", "getOutputPattern");
49 //**********************************************************************************************************************
50 string CountGroupsCommand::getHelpString(){
52 string helpString = "";
53 helpString += "The count.groups command counts sequences from a specific group or set of groups from the following file types: group, count or shared file.\n";
54 helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
55 helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
56 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like. You can separate group names with dashes.\n";
57 helpString += "The count.groups command should be in the following format: count.groups(accnos=yourAccnos, group=yourGroupFile).\n";
58 helpString += "Example count.groups(accnos=amazon.accnos, group=amazon.groups).\n";
59 helpString += "or count.groups(groups=pasture, group=amazon.groups).\n";
60 helpString += "Note: No spaces between parameter labels (i.e. group), '=' and parameters (i.e.yourGroupFile).\n";
64 m->errorOut(e, "CountGroupsCommand", "getHelpString");
68 //**********************************************************************************************************************
69 CountGroupsCommand::CountGroupsCommand(){
71 abort = true; calledHelp = true;
73 vector<string> tempOutNames;
74 outputTypes["summary"] = tempOutNames;
77 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
81 //**********************************************************************************************************************
82 CountGroupsCommand::CountGroupsCommand(string option) {
84 abort = false; calledHelp = false;
86 //allow user to run help
87 if(option == "help") { help(); abort = true; calledHelp = true; }
88 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
91 vector<string> myArray = setParameters();
93 OptionParser parser(option);
94 map<string,string> parameters = parser.getParameters();
96 ValidParameters validParameter;
97 map<string,string>::iterator it;
99 //check to make sure all parameters are valid for command
100 for (it = parameters.begin(); it != parameters.end(); it++) {
101 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
104 //if the user changes the output directory command factory will send this info to us in the output parameter
105 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
107 //if the user changes the input directory command factory will send this info to us in the output parameter
108 string inputDir = validParameter.validFile(parameters, "inputdir", false);
109 if (inputDir == "not found"){ inputDir = ""; }
112 it = parameters.find("accnos");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["accnos"] = inputDir + it->second; }
120 it = parameters.find("group");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["group"] = inputDir + it->second; }
128 it = parameters.find("shared");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["shared"] = inputDir + it->second; }
136 it = parameters.find("count");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["count"] = inputDir + it->second; }
145 vector<string> tempOutNames;
146 outputTypes["summary"] = tempOutNames;
148 //check for required parameters
149 accnosfile = validParameter.validFile(parameters, "accnos", true);
150 if (accnosfile == "not open") { abort = true; }
151 else if (accnosfile == "not found") { accnosfile = ""; }
152 else { m->setAccnosFile(accnosfile); }
154 groups = validParameter.validFile(parameters, "groups", false);
155 if (groups == "not found") { groups = ""; }
157 m->splitAtDash(groups, Groups);
158 m->setGroups(Groups);
161 sharedfile = validParameter.validFile(parameters, "shared", true);
162 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
163 else if (sharedfile == "not found") { sharedfile = ""; }
164 else { m->setSharedFile(sharedfile); }
166 groupfile = validParameter.validFile(parameters, "group", true);
167 if (groupfile == "not open") { groupfile = ""; abort = true; }
168 else if (groupfile == "not found") { groupfile = ""; }
169 else { m->setGroupFile(groupfile); }
171 countfile = validParameter.validFile(parameters, "count", true);
172 if (countfile == "not open") { countfile = ""; abort = true; }
173 else if (countfile == "not found") { countfile = ""; }
175 m->setCountTableFile(countfile);
177 if (!ct.testGroups(countfile)) { m->mothurOut("[ERROR]: Your count file does not have any group information, aborting."); m->mothurOutEndLine(); abort=true; }
180 if ((groupfile != "") && (countfile != "")) {
181 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
185 if ((sharedfile == "") && (groupfile == "") && (countfile == "")) {
186 //give priority to shared, then group
187 sharedfile = m->getSharedFile();
188 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
190 groupfile = m->getGroupFile();
191 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
193 countfile = m->getCountTableFile();
194 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
196 m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
202 if ((accnosfile == "") && (Groups.size() == 0)) { Groups.push_back("all"); m->setGroups(Groups); }
206 catch(exception& e) {
207 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
211 //**********************************************************************************************************************
213 int CountGroupsCommand::execute(){
216 if (abort == true) { if (calledHelp) { return 0; } return 2; }
218 //get groups you want to remove
219 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
221 if (groupfile != "") {
222 map<string, string> variables;
223 string thisOutputDir = outputDir;
224 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
225 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
226 string outputFileName = getOutputFileName("summary", variables);
227 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
229 m->openOutputFile(outputFileName, out);
231 GroupMap groupMap(groupfile);
234 //make sure groups are valid
235 //takes care of user setting groupNames that are invalid or setting groups=all
237 vector<string> nameGroups = groupMap.getNamesOfGroups();
238 util.setGroups(Groups, nameGroups);
241 for (int i = 0; i < Groups.size(); i++) {
242 int num = groupMap.getNumSeqs(Groups[i]);
244 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
245 out << Groups[i] << '\t' << num << endl;
248 m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
251 if (m->control_pressed) { return 0; }
253 if (countfile != "") {
254 map<string, string> variables;
255 string thisOutputDir = outputDir;
256 if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
257 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
258 string outputFileName = getOutputFileName("summary", variables);
259 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
261 m->openOutputFile(outputFileName, out);
264 ct.readTable(countfile);
266 //make sure groups are valid
267 //takes care of user setting groupNames that are invalid or setting groups=all
269 vector<string> nameGroups = ct.getNamesOfGroups();
270 util.setGroups(Groups, nameGroups);
273 for (int i = 0; i < Groups.size(); i++) {
274 int num = ct.getGroupCount(Groups[i]);
276 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
277 out << Groups[i] << '\t' << num << endl;
281 m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
284 if (m->control_pressed) { return 0; }
286 if (sharedfile != "") {
287 InputData input(sharedfile, "sharedfile");
288 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
290 map<string, string> variables;
291 string thisOutputDir = outputDir;
292 if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
293 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
294 string outputFileName = getOutputFileName("summary", variables);
295 outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
297 m->openOutputFile(outputFileName, out);
300 for (int i = 0; i < lookup.size(); i++) {
301 int num = lookup[i]->getNumSeqs();
303 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(num) + "."); m->mothurOutEndLine();
305 out << lookup[i]->getGroup() << '\t' << num << endl;
309 m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
312 m->mothurOutEndLine();
313 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
314 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
315 m->mothurOutEndLine();
320 catch(exception& e) {
321 m->errorOut(e, "CountGroupsCommand", "execute");
325 //**********************************************************************************************************************