1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* countgroupscommand.cpp
* Mothur
*
* Created by westcott on 8/9/11.
* Copyright 2011 Schloss Lab. All rights reserved.
*
*/
#include "countgroupscommand.h"
#include "sharedutilities.h"
#include "inputdata.h"
//**********************************************************************************************************************
vector<string> CountGroupsCommand::setParameters(){
try {
CommandParameter pshared("shared", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pshared);
CommandParameter pgroup("group", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pgroup);
CommandParameter pcount("count", "InputTypes", "", "", "sharedGroup", "sharedGroup", "none","summary",false,false,true); parameters.push_back(pcount);
CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos);
CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
m->errorOut(e, "CountGroupsCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string CountGroupsCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "summary") { pattern = "[filename],count.summary"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "PrimerDesignCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
string CountGroupsCommand::getHelpString(){
try {
string helpString = "";
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";
helpString += "The count.groups command parameters are accnos, group, shared and groups. You must provide a group or shared file.\n";
helpString += "The accnos parameter allows you to provide a file containing the list of groups.\n";
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";
helpString += "The count.groups command should be in the following format: count.groups(accnos=yourAccnos, group=yourGroupFile).\n";
helpString += "Example count.groups(accnos=amazon.accnos, group=amazon.groups).\n";
helpString += "or count.groups(groups=pasture, group=amazon.groups).\n";
helpString += "Note: No spaces between parameter labels (i.e. group), '=' and parameters (i.e.yourGroupFile).\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "CountGroupsCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
CountGroupsCommand::CountGroupsCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["summary"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
exit(1);
}
}
//**********************************************************************************************************************
CountGroupsCommand::CountGroupsCommand(string option) {
try {
abort = false; calledHelp = false;
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter;
map<string,string>::iterator it;
//check to make sure all parameters are valid for command
for (it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
//if the user changes the output directory command factory will send this info to us in the output parameter
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("accnos");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["accnos"] = inputDir + it->second; }
}
it = parameters.find("group");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["group"] = inputDir + it->second; }
}
it = parameters.find("shared");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["shared"] = inputDir + it->second; }
}
it = parameters.find("count");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["count"] = inputDir + it->second; }
}
}
vector<string> tempOutNames;
outputTypes["summary"] = tempOutNames;
//check for required parameters
accnosfile = validParameter.validFile(parameters, "accnos", true);
if (accnosfile == "not open") { abort = true; }
else if (accnosfile == "not found") { accnosfile = ""; }
else { m->setAccnosFile(accnosfile); }
groups = validParameter.validFile(parameters, "groups", false);
if (groups == "not found") { groups = ""; }
else {
m->splitAtDash(groups, Groups);
m->setGroups(Groups);
}
sharedfile = validParameter.validFile(parameters, "shared", true);
if (sharedfile == "not open") { sharedfile = ""; abort = true; }
else if (sharedfile == "not found") { sharedfile = ""; }
else { m->setSharedFile(sharedfile); }
groupfile = validParameter.validFile(parameters, "group", true);
if (groupfile == "not open") { groupfile = ""; abort = true; }
else if (groupfile == "not found") { groupfile = ""; }
else { m->setGroupFile(groupfile); }
countfile = validParameter.validFile(parameters, "count", true);
if (countfile == "not open") { countfile = ""; abort = true; }
else if (countfile == "not found") { countfile = ""; }
else {
m->setCountTableFile(countfile);
CountTable ct;
if (!ct.testGroups(countfile)) { m->mothurOut("[ERROR]: Your count file does not have any group information, aborting."); m->mothurOutEndLine(); abort=true; }
}
if ((groupfile != "") && (countfile != "")) {
m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
}
if ((sharedfile == "") && (groupfile == "") && (countfile == "")) {
//give priority to shared, then group
sharedfile = m->getSharedFile();
if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
else {
groupfile = m->getGroupFile();
if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
else {
countfile = m->getCountTableFile();
if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("You have no current groupfile, countfile or sharedfile and one is required."); m->mothurOutEndLine(); abort = true;
}
}
}
}
if ((accnosfile == "") && (Groups.size() == 0)) { Groups.push_back("all"); m->setGroups(Groups); }
}
}
catch(exception& e) {
m->errorOut(e, "CountGroupsCommand", "CountGroupsCommand");
exit(1);
}
}
//**********************************************************************************************************************
int CountGroupsCommand::execute(){
try {
if (abort == true) { if (calledHelp) { return 0; } return 2; }
//get groups you want to remove
if (accnosfile != "") { m->readAccnos(accnosfile, Groups); m->setGroups(Groups); }
if (groupfile != "") {
map<string, string> variables;
string thisOutputDir = outputDir;
if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
string outputFileName = getOutputFileName("summary", variables);
outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
ofstream out;
m->openOutputFile(outputFileName, out);
GroupMap groupMap(groupfile);
groupMap.readMap();
//make sure groups are valid
//takes care of user setting groupNames that are invalid or setting groups=all
SharedUtil util;
vector<string> nameGroups = groupMap.getNamesOfGroups();
util.setGroups(Groups, nameGroups);
int total = 0;
for (int i = 0; i < Groups.size(); i++) {
int num = groupMap.getNumSeqs(Groups[i]);
total += num;
m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
out << Groups[i] << '\t' << num << endl;
}
out.close();
m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
}
if (m->control_pressed) { return 0; }
if (countfile != "") {
map<string, string> variables;
string thisOutputDir = outputDir;
if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
string outputFileName = getOutputFileName("summary", variables);
outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
ofstream out;
m->openOutputFile(outputFileName, out);
CountTable ct;
ct.readTable(countfile, true, false);
//make sure groups are valid
//takes care of user setting groupNames that are invalid or setting groups=all
SharedUtil util;
vector<string> nameGroups = ct.getNamesOfGroups();
util.setGroups(Groups, nameGroups);
int total = 0;
for (int i = 0; i < Groups.size(); i++) {
int num = ct.getGroupCount(Groups[i]);
total += num;
m->mothurOut(Groups[i] + " contains " + toString(num) + "."); m->mothurOutEndLine();
out << Groups[i] << '\t' << num << endl;
}
out.close();
m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
}
if (m->control_pressed) { return 0; }
if (sharedfile != "") {
InputData input(sharedfile, "sharedfile");
vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
map<string, string> variables;
string thisOutputDir = outputDir;
if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
string outputFileName = getOutputFileName("summary", variables);
outputNames.push_back(outputFileName); outputTypes["summary"].push_back(outputFileName);
ofstream out;
m->openOutputFile(outputFileName, out);
int total = 0;
for (int i = 0; i < lookup.size(); i++) {
int num = lookup[i]->getNumSeqs();
total += num;
m->mothurOut(lookup[i]->getGroup() + " contains " + toString(num) + "."); m->mothurOutEndLine();
delete lookup[i];
out << lookup[i]->getGroup() << '\t' << num << endl;
}
out.close();
m->mothurOut("\nTotal seqs: " + toString(total) + "."); m->mothurOutEndLine();
}
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "CountGroupsCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
|