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",false,false); parameters.push_back(pshared);
18 CommandParameter pgroup("group", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none",false,false); parameters.push_back(pgroup);
19 CommandParameter pcount("count", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none",false,false); 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::getHelpString(){
37 string helpString = "";
38 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";
39 helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
40 helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
41 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";
42 helpString += "The count.groups command should be in the following format: count.groups(accnos=yourAccnos, group=yourGroupFile).\n";
43 helpString += "Example count.groups(accnos=amazon.accnos, group=amazon.groups).\n";
44 helpString += "or count.groups(groups=pasture, group=amazon.groups).\n";
45 helpString += "Note: No spaces between parameter labels (i.e. group), '=' and parameters (i.e.yourGroupFile).\n";
49 m->errorOut(e, "CountGroupsCommand", "getHelpString");
53 //**********************************************************************************************************************
54 CountGroupsCommand::CountGroupsCommand(){
56 abort = true; calledHelp = true;
60 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
64 //**********************************************************************************************************************
65 CountGroupsCommand::CountGroupsCommand(string option) {
67 abort = false; calledHelp = false;
69 //allow user to run help
70 if(option == "help") { help(); abort = true; calledHelp = true; }
71 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
74 vector<string> myArray = setParameters();
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string,string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //if the user changes the output directory command factory will send this info to us in the output parameter
88 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("accnos");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["accnos"] = inputDir + it->second; }
103 it = parameters.find("group");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["group"] = inputDir + it->second; }
111 it = parameters.find("shared");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["shared"] = inputDir + it->second; }
119 it = parameters.find("count");
120 //user has given a template file
121 if(it != parameters.end()){
122 path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { parameters["count"] = inputDir + it->second; }
129 //check for required parameters
130 accnosfile = validParameter.validFile(parameters, "accnos", true);
131 if (accnosfile == "not open") { abort = true; }
132 else if (accnosfile == "not found") { accnosfile = ""; }
133 else { m->setAccnosFile(accnosfile); }
135 groups = validParameter.validFile(parameters, "groups", false);
136 if (groups == "not found") { groups = ""; }
138 m->splitAtDash(groups, Groups);
139 m->setGroups(Groups);
142 sharedfile = validParameter.validFile(parameters, "shared", true);
143 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
144 else if (sharedfile == "not found") { sharedfile = ""; }
145 else { m->setSharedFile(sharedfile); }
147 groupfile = validParameter.validFile(parameters, "group", true);
148 if (groupfile == "not open") { groupfile = ""; abort = true; }
149 else if (groupfile == "not found") { groupfile = ""; }
150 else { m->setGroupFile(groupfile); }
152 countfile = validParameter.validFile(parameters, "count", true);
153 if (countfile == "not open") { countfile = ""; abort = true; }
154 else if (countfile == "not found") { countfile = ""; }
156 m->setCountTableFile(countfile);
158 if (!ct.testGroups(countfile)) { m->mothurOut("[ERROR]: Your count file does not have any group information, aborting."); m->mothurOutEndLine(); abort=true; }
161 if ((groupfile != "") && (countfile != "")) {
162 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
166 if ((sharedfile == "") && (groupfile == "") && (countfile == "")) {
167 //give priority to shared, then group
168 sharedfile = m->getSharedFile();
169 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
171 groupfile = m->getGroupFile();
172 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
174 countfile = m->getCountTableFile();
175 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
177 m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
183 if ((accnosfile == "") && (Groups.size() == 0)) { Groups.push_back("all"); m->setGroups(Groups); }
187 catch(exception& e) {
188 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
192 //**********************************************************************************************************************
194 int CountGroupsCommand::execute(){
197 if (abort == true) { if (calledHelp) { return 0; } return 2; }
199 //get groups you want to remove
200 if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
202 if (groupfile != "") {
203 GroupMap groupMap(groupfile);
206 //make sure groups are valid
207 //takes care of user setting groupNames that are invalid or setting groups=all
209 vector<string> nameGroups = groupMap.getNamesOfGroups();
210 util.setGroups(Groups, nameGroups);
213 for (int i = 0; i < Groups.size(); i++) {
214 int num = groupMap.getNumSeqs(Groups[i]);
216 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
219 m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
222 if (m->control_pressed) { return 0; }
224 if (countfile != "") {
226 ct.readTable(countfile);
228 //make sure groups are valid
229 //takes care of user setting groupNames that are invalid or setting groups=all
231 vector<string> nameGroups = ct.getNamesOfGroups();
232 util.setGroups(Groups, nameGroups);
235 for (int i = 0; i < Groups.size(); i++) {
236 int num = ct.getGroupCount(Groups[i]);
238 m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
241 m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
244 if (m->control_pressed) { return 0; }
246 if (sharedfile != "") {
247 InputData input(sharedfile, "sharedfile");
248 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
251 for (int i = 0; i < lookup.size(); i++) {
252 int num = lookup[i]->getNumSeqs();
254 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(num) + "."); m->mothurOutEndLine();
258 m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
264 catch(exception& e) {
265 m->errorOut(e, "CountGroupsCommand", "execute");
269 //**********************************************************************************************************************