2 * splitgroupscommand.cpp
5 * Created by westcott on 9/20/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "splitgroupscommand.h"
11 #include "sharedutilities.h"
12 #include "sequenceparser.h"
14 //**********************************************************************************************************************
15 vector<string> SplitGroupCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
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, "SplitGroupCommand", "setParameters");
33 //**********************************************************************************************************************
34 string SplitGroupCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The split.group command reads a group file, and parses your fasta and names files by groups. \n";
38 helpString += "The split.group command parameters are fasta, name, group and groups.\n";
39 helpString += "The fasta and group parameters are required.\n";
40 helpString += "The groups parameter allows you to select groups to create files for. \n";
41 helpString += "For example if you set groups=A-B-C, you will get a .A.fasta, .A.names, .B.fasta, .B.names, .C.fasta, .C.names files. \n";
42 helpString += "If you want .fasta and .names files for all groups, set groups=all. \n";
43 helpString += "The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n";
44 helpString += "Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n";
45 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
49 m->errorOut(e, "SplitGroupCommand", "getHelpString");
53 //**********************************************************************************************************************
54 string SplitGroupCommand::getOutputFileNameTag(string type, string inputName=""){
56 string outputFileName = "";
57 map<string, vector<string> >::iterator it;
59 //is this a type this command creates
60 it = outputTypes.find(type);
61 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
63 if (type == "fasta") { outputFileName = "fasta"; }
64 else if (type == "name") { outputFileName = "names"; }
65 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
67 return outputFileName;
70 m->errorOut(e, "SplitGroupCommand", "getOutputFileNameTag");
74 //**********************************************************************************************************************
75 SplitGroupCommand::SplitGroupCommand(){
77 abort = true; calledHelp = true;
79 vector<string> tempOutNames;
80 outputTypes["fasta"] = tempOutNames;
81 outputTypes["name"] = tempOutNames;
84 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
88 //**********************************************************************************************************************
89 SplitGroupCommand::SplitGroupCommand(string option) {
91 abort = false; calledHelp = false;
93 //allow user to run help
94 if(option == "help") { help(); abort = true; calledHelp = true; }
95 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
98 vector<string> myArray = setParameters();
100 OptionParser parser(option);
101 map<string, string> parameters = parser.getParameters();
103 ValidParameters validParameter;
104 map<string, string>::iterator it;
106 //check to make sure all parameters are valid for command
107 for (it = parameters.begin(); it != parameters.end(); it++) {
108 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
111 //initialize outputTypes
112 vector<string> tempOutNames;
113 outputTypes["fasta"] = tempOutNames;
114 outputTypes["name"] = tempOutNames;
116 //if the user changes the input directory command factory will send this info to us in the output parameter
117 string inputDir = validParameter.validFile(parameters, "inputdir", false);
118 if (inputDir == "not found"){ inputDir = ""; }
121 it = parameters.find("group");
122 //user has given a template file
123 if(it != parameters.end()){
124 path = m->hasPath(it->second);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { parameters["group"] = inputDir + it->second; }
129 it = parameters.find("fasta");
130 //user has given a template file
131 if(it != parameters.end()){
132 path = m->hasPath(it->second);
133 //if the user has not given a path then, add inputdir. else leave path alone.
134 if (path == "") { parameters["fasta"] = inputDir + it->second; }
137 it = parameters.find("name");
138 //user has given a template file
139 if(it != parameters.end()){
140 path = m->hasPath(it->second);
141 //if the user has not given a path then, add inputdir. else leave path alone.
142 if (path == "") { parameters["name"] = inputDir + it->second; }
148 namefile = validParameter.validFile(parameters, "name", true);
149 if (namefile == "not open") { namefile = ""; abort = true; }
150 else if (namefile == "not found") { namefile = ""; }
151 else { m->setNameFile(namefile); }
153 fastafile = validParameter.validFile(parameters, "fasta", true);
154 if (fastafile == "not open") { abort = true; }
155 else if (fastafile == "not found") {
156 fastafile = m->getFastaFile();
157 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
158 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
159 }else { m->setFastaFile(fastafile); }
161 groupfile = validParameter.validFile(parameters, "group", true);
162 if (groupfile == "not open") { groupfile = ""; abort = true; }
163 else if (groupfile == "not found") {
164 groupfile = m->getGroupFile();
165 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
166 else { m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
167 }else { m->setGroupFile(groupfile); }
169 groups = validParameter.validFile(parameters, "groups", false);
170 if (groups == "not found") { groups = ""; }
171 else { m->splitAtDash(groups, Groups); }
173 //if the user changes the output directory command factory will send this info to us in the output parameter
174 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(groupfile); }
176 if (namefile == "") {
177 vector<string> files; files.push_back(fastafile);
178 parser.getNameFile(files);
183 catch(exception& e) {
184 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
188 //**********************************************************************************************************************
189 int SplitGroupCommand::execute(){
192 if (abort == true) { if (calledHelp) { return 0; } return 2; }
194 SequenceParser* parser;
196 if (namefile == "") { parser = new SequenceParser(groupfile, fastafile); }
197 else { parser = new SequenceParser(groupfile, fastafile, namefile); }
199 if (m->control_pressed) { delete parser; return 0; }
201 vector<string> namesGroups = parser->getNamesOfGroups();
202 SharedUtil util; util.setGroups(Groups, namesGroups);
204 string fastafileRoot = outputDir + m->getRootName(m->getSimpleName(fastafile));
205 string namefileRoot = outputDir + m->getRootName(m->getSimpleName(namefile));
207 m->mothurOutEndLine();
208 for (int i = 0; i < Groups.size(); i++) {
210 m->mothurOut("Processing group: " + Groups[i]); m->mothurOutEndLine();
212 string newFasta = fastafileRoot + Groups[i] + "." + getOutputFileNameTag("fasta");
213 string newName = namefileRoot + Groups[i] + "." + getOutputFileNameTag("name");
215 parser->getSeqs(Groups[i], newFasta, false);
216 outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta);
217 if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
219 if (namefile != "") {
220 parser->getNameMap(Groups[i], newName);
221 outputNames.push_back(newName); outputTypes["name"].push_back(newName);
224 if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
229 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
232 itTypes = outputTypes.find("fasta");
233 if (itTypes != outputTypes.end()) {
234 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
237 itTypes = outputTypes.find("name");
238 if (itTypes != outputTypes.end()) {
239 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
242 m->mothurOutEndLine();
243 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
244 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
245 m->mothurOutEndLine();
249 catch(exception& e) {
250 m->errorOut(e, "SplitGroupCommand", "execute");
254 //**********************************************************************************************************************