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 paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);
20 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
21 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "CountGroupsCommand", "setParameters");
33 //**********************************************************************************************************************
34 string CountGroupsCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The count.groups command counts sequences from a specific group or set of groups from the following file types: group or shared file.\n";
38 helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
39 helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
40 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";
41 helpString += "The count.groups command should be in the following format: count.groups(accnos=yourAccnos, group=yourGroupFile).\n";
42 helpString += "Example count.groups(accnos=amazon.accnos, group=amazon.groups).\n";
43 helpString += "or count.groups(groups=pasture, group=amazon.groups).\n";
44 helpString += "Note: No spaces between parameter labels (i.e. group), '=' and parameters (i.e.yourGroupFile).\n";
48 m->errorOut(e, "CountGroupsCommand", "getHelpString");
52 //**********************************************************************************************************************
53 CountGroupsCommand::CountGroupsCommand(){
55 abort = true; calledHelp = true;
59 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
63 //**********************************************************************************************************************
64 CountGroupsCommand::CountGroupsCommand(string option) {
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
70 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73 vector<string> myArray = setParameters();
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //if the user changes the output directory command factory will send this info to us in the output parameter
87 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("accnos");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["accnos"] = inputDir + it->second; }
102 it = parameters.find("group");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["group"] = inputDir + it->second; }
110 it = parameters.find("shared");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["shared"] = inputDir + it->second; }
120 //check for required parameters
121 accnosfile = validParameter.validFile(parameters, "accnos", true);
122 if (accnosfile == "not open") { abort = true; }
123 else if (accnosfile == "not found") { accnosfile = ""; }
124 else { m->setAccnosFile(accnosfile); }
126 groups = validParameter.validFile(parameters, "groups", false);
127 if (groups == "not found") { groups = ""; }
129 m->splitAtDash(groups, Groups);
130 m->setGroups(Groups);
133 sharedfile = validParameter.validFile(parameters, "shared", true);
134 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
135 else if (sharedfile == "not found") { sharedfile = ""; }
136 else { m->setSharedFile(sharedfile); }
138 groupfile = validParameter.validFile(parameters, "group", true);
139 if (groupfile == "not open") { groupfile = ""; abort = true; }
140 else if (groupfile == "not found") { groupfile = ""; }
141 else { m->setGroupFile(groupfile); }
143 if ((sharedfile == "") && (groupfile == "")) {
144 //give priority to shared, then group
145 sharedfile = m->getSharedFile();
146 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
148 groupfile = m->getGroupFile();
149 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
151 m->mothurOut("You have no current groupfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
156 if ((accnosfile == "") && (Groups.size() == 0)) { Groups.push_back("all"); m->setGroups(Groups); }
160 catch(exception& e) {
161 m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
165 //**********************************************************************************************************************
167 int CountGroupsCommand::execute(){
170 if (abort == true) { if (calledHelp) { return 0; } return 2; }
172 //get groups you want to remove
173 if (accnosfile != "") { readAccnos(); }
175 if (groupfile != "") {
176 GroupMap groupMap(groupfile);
179 //make sure groups are valid
180 //takes care of user setting groupNames that are invalid or setting groups=all
182 vector<string> nameGroups = groupMap.getNamesOfGroups();
183 util.setGroups(Groups, nameGroups);
185 for (int i = 0; i < Groups.size(); i++) {
186 m->mothurOut(Groups[i] + " contains " + toString(groupMap.getNumSeqs(Groups[i])) + "."); m->mothurOutEndLine();
190 if (m->control_pressed) { return 0; }
192 if (sharedfile != "") {
193 InputData input(sharedfile, "sharedfile");
194 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
196 for (int i = 0; i < lookup.size(); i++) {
197 m->mothurOut(lookup[i]->getGroup() + " contains " + toString(lookup[i]->getNumSeqs()) + "."); m->mothurOutEndLine();
205 catch(exception& e) {
206 m->errorOut(e, "CountGroupsCommand", "execute");
210 //**********************************************************************************************************************
211 void CountGroupsCommand::readAccnos(){
216 m->openInputFile(accnosfile, in);
222 Groups.push_back(name);
228 m->setGroups(Groups);
231 catch(exception& e) {
232 m->errorOut(e, "CountGroupsCommand", "readAccnos");
236 //**********************************************************************************************************************