5 * Created by westcott on 6/1/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "countseqscommand.h"
12 #include "sharedutilities.h"
14 //**********************************************************************************************************************
15 vector<string> CountSeqsCommand::setParameters(){
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pname);
18 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
19 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
20 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "CountSeqsCommand", "setParameters");
32 //**********************************************************************************************************************
33 string CountSeqsCommand::getHelpString(){
35 string helpString = "";
36 helpString += "The count.seqs command reads a name file and outputs a .seq.count file. You may also provide a group file to get the counts broken down by group.\n";
37 helpString += "The groups parameter allows you to indicate which groups you want to include in the counts, by default all groups in your groupfile are used.\n";
38 helpString += "When you use the groups parameter and a sequence does not represent any sequences from the groups you specify it is not included in the .count.summary file.\n";
39 helpString += "The count.seqs command should be in the following format: count.seqs(name=yourNameFile).\n";
40 helpString += "Example count.seqs(name=amazon.names).\n";
41 helpString += "Note: No spaces between parameter labels (i.e. name), '=' and parameters (i.e.yourNameFile).\n";
45 m->errorOut(e, "CountSeqsCommand", "getHelpString");
49 //**********************************************************************************************************************
50 string CountSeqsCommand::getOutputFileNameTag(string type, string inputName=""){
52 string outputFileName = "";
53 map<string, vector<string> >::iterator it;
55 //is this a type this command creates
56 it = outputTypes.find(type);
57 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
59 if (type == "summary") { outputFileName = "seq.count"; }
60 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
62 return outputFileName;
65 m->errorOut(e, "CountSeqsCommand", "getOutputFileNameTag");
69 //**********************************************************************************************************************
70 CountSeqsCommand::CountSeqsCommand(){
72 abort = true; calledHelp = true;
74 vector<string> tempOutNames;
75 outputTypes["summary"] = tempOutNames;
78 m->errorOut(e, "CountSeqsCommand", "CountSeqsCommand");
82 //**********************************************************************************************************************
84 CountSeqsCommand::CountSeqsCommand(string option) {
86 abort = false; calledHelp = false;
88 //allow user to run help
89 if(option == "help") { help(); abort = true; calledHelp = true; }
90 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
92 vector<string> myArray = setParameters();
94 OptionParser parser(option);
95 map<string,string> parameters = parser.getParameters();
97 ValidParameters validParameter;
98 map<string,string>::iterator it;
100 //check to make sure all parameters are valid for command
101 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
102 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
105 //initialize outputTypes
106 vector<string> tempOutNames;
107 outputTypes["summary"] = tempOutNames;
110 //if the user changes the input directory command factory will send this info to us in the output parameter
111 string inputDir = validParameter.validFile(parameters, "inputdir", false);
112 if (inputDir == "not found"){ inputDir = ""; }
115 it = parameters.find("name");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["name"] = inputDir + it->second; }
123 it = parameters.find("group");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["group"] = inputDir + it->second; }
132 //check for required parameters
133 namefile = validParameter.validFile(parameters, "name", true);
134 if (namefile == "not open") { namefile = ""; abort = true; }
135 else if (namefile == "not found"){
136 namefile = m->getNameFile();
137 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
138 else { m->mothurOut("You have no current namefile and the name parameter is required."); m->mothurOutEndLine(); abort = true; }
139 }else { m->setNameFile(namefile); }
141 groupfile = validParameter.validFile(parameters, "group", true);
142 if (groupfile == "not open") { abort = true; }
143 else if (groupfile == "not found") { groupfile = ""; }
144 else { m->setGroupFile(groupfile); }
146 groups = validParameter.validFile(parameters, "groups", false);
147 if (groups == "not found") { groups = "all"; }
148 m->splitAtDash(groups, Groups);
150 //if the user changes the output directory command factory will send this info to us in the output parameter
151 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(namefile); }
156 catch(exception& e) {
157 m->errorOut(e, "CountSeqsCommand", "CountSeqsCommand");
161 //**********************************************************************************************************************
163 int CountSeqsCommand::execute(){
166 if (abort == true) { if (calledHelp) { return 0; } return 2; }
169 string outputFileName = outputDir + m->getRootName(m->getSimpleName(namefile)) + getOutputFileNameTag("summary");
170 m->openOutputFile(outputFileName, out); outputTypes["summary"].push_back(outputFileName);
171 out << "Representative_Sequence\ttotal\t";
174 if (groupfile != "") {
175 groupMap = new GroupMap(groupfile); groupMap->readMap();
177 //make sure groups are valid. takes care of user setting groupNames that are invalid or setting groups=all
178 SharedUtil* util = new SharedUtil();
179 vector<string> nameGroups = groupMap->getNamesOfGroups();
180 util->setGroups(Groups, nameGroups);
183 //sort groupNames so that the group title match the counts below, this is needed because the map object automatically sorts
184 sort(Groups.begin(), Groups.end());
187 for (int i = 0; i < Groups.size(); i++) {
188 out << Groups[i] << '\t';
195 m->openInputFile(namefile, in);
199 if (m->control_pressed) { break; }
201 string firstCol, secondCol;
202 in >> firstCol >> secondCol; m->gobble(in);
204 vector<string> names;
205 m->splitAtChar(secondCol, names, ',');
207 if (groupfile != "") {
209 map<string, int> groupCounts;
211 for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; }
213 //get counts for each of the users groups
214 for (int i = 0; i < names.size(); i++) {
215 string group = groupMap->getGroup(names[i]);
217 if (group == "not found") { m->mothurOut("[ERROR]: " + names[i] + " is not in your groupfile, please correct."); m->mothurOutEndLine(); }
219 map<string, int>::iterator it = groupCounts.find(group);
221 //if not found, then this sequence is not from a group we care about
222 if (it != groupCounts.end()) {
230 out << firstCol << '\t' << total << '\t';
231 for (map<string, int>::iterator it = groupCounts.begin(); it != groupCounts.end(); it++) {
232 out << it->second << '\t';
237 out << firstCol << '\t' << names.size() << endl;
240 total += names.size();
244 if (groupfile != "") { delete groupMap; }
246 if (m->control_pressed) { m->mothurRemove(outputFileName); return 0; }
248 m->mothurOutEndLine();
249 m->mothurOut("Total number of sequences: " + toString(total)); m->mothurOutEndLine();
250 m->mothurOutEndLine();
251 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
252 m->mothurOut(outputFileName); m->mothurOutEndLine();
253 m->mothurOutEndLine();
258 catch(exception& e) {
259 m->errorOut(e, "CountSeqsCommand", "execute");
263 //**********************************************************************************************************************