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 SplitGroupCommand::SplitGroupCommand(){
56 abort = true; calledHelp = true;
58 vector<string> tempOutNames;
59 outputTypes["fasta"] = tempOutNames;
60 outputTypes["name"] = tempOutNames;
63 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
67 //**********************************************************************************************************************
68 SplitGroupCommand::SplitGroupCommand(string option) {
70 abort = false; calledHelp = false;
72 //allow user to run help
73 if(option == "help") { help(); abort = true; calledHelp = true; }
74 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
77 vector<string> myArray = setParameters();
79 OptionParser parser(option);
80 map<string, string> parameters = parser.getParameters();
82 ValidParameters validParameter;
83 map<string, string>::iterator it;
85 //check to make sure all parameters are valid for command
86 for (it = parameters.begin(); it != parameters.end(); it++) {
87 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["fasta"] = tempOutNames;
93 outputTypes["name"] = tempOutNames;
95 //if the user changes the input directory command factory will send this info to us in the output parameter
96 string inputDir = validParameter.validFile(parameters, "inputdir", false);
97 if (inputDir == "not found"){ inputDir = ""; }
100 it = parameters.find("group");
101 //user has given a template file
102 if(it != parameters.end()){
103 path = m->hasPath(it->second);
104 //if the user has not given a path then, add inputdir. else leave path alone.
105 if (path == "") { parameters["group"] = inputDir + it->second; }
108 it = parameters.find("fasta");
109 //user has given a template file
110 if(it != parameters.end()){
111 path = m->hasPath(it->second);
112 //if the user has not given a path then, add inputdir. else leave path alone.
113 if (path == "") { parameters["fasta"] = inputDir + it->second; }
116 it = parameters.find("name");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["name"] = inputDir + it->second; }
127 namefile = validParameter.validFile(parameters, "name", true);
128 if (namefile == "not open") { namefile = ""; abort = true; }
129 else if (namefile == "not found") { namefile = ""; }
130 else { m->setNameFile(namefile); }
132 fastafile = validParameter.validFile(parameters, "fasta", true);
133 if (fastafile == "not open") { abort = true; }
134 else if (fastafile == "not found") {
135 fastafile = m->getFastaFile();
136 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
137 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
138 }else { m->setFastaFile(fastafile); }
140 groupfile = validParameter.validFile(parameters, "group", true);
141 if (groupfile == "not open") { groupfile = ""; abort = true; }
142 else if (groupfile == "not found") {
143 groupfile = m->getGroupFile();
144 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
145 else { m->mothurOut("You have no current groupfile and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
146 }else { m->setGroupFile(groupfile); }
148 groups = validParameter.validFile(parameters, "groups", false);
149 if (groups == "not found") { groups = ""; }
150 else { m->splitAtDash(groups, Groups); }
152 //if the user changes the output directory command factory will send this info to us in the output parameter
153 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(groupfile); }
155 if (namefile == "") {
156 vector<string> files; files.push_back(fastafile);
157 parser.getNameFile(files);
162 catch(exception& e) {
163 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
167 //**********************************************************************************************************************
168 int SplitGroupCommand::execute(){
171 if (abort == true) { if (calledHelp) { return 0; } return 2; }
173 SequenceParser* parser;
175 if (namefile == "") { parser = new SequenceParser(groupfile, fastafile); }
176 else { parser = new SequenceParser(groupfile, fastafile, namefile); }
178 if (m->control_pressed) { delete parser; return 0; }
180 vector<string> namesGroups = parser->getNamesOfGroups();
181 SharedUtil util; util.setGroups(Groups, namesGroups);
183 string fastafileRoot = outputDir + m->getRootName(m->getSimpleName(fastafile));
184 string namefileRoot = outputDir + m->getRootName(m->getSimpleName(namefile));
186 m->mothurOutEndLine();
187 for (int i = 0; i < Groups.size(); i++) {
189 m->mothurOut("Processing group: " + Groups[i]); m->mothurOutEndLine();
191 string newFasta = fastafileRoot + Groups[i] + ".fasta";
192 string newName = namefileRoot + Groups[i] + ".names";
194 parser->getSeqs(Groups[i], newFasta, false);
195 outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta);
196 if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
198 if (namefile != "") {
199 parser->getNameMap(Groups[i], newName);
200 outputNames.push_back(newName); outputTypes["name"].push_back(newName);
203 if (m->control_pressed) { delete parser; for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
208 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
211 itTypes = outputTypes.find("fasta");
212 if (itTypes != outputTypes.end()) {
213 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
216 itTypes = outputTypes.find("name");
217 if (itTypes != outputTypes.end()) {
218 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
221 m->mothurOutEndLine();
222 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
223 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
224 m->mothurOutEndLine();
228 catch(exception& e) {
229 m->errorOut(e, "SplitGroupCommand", "execute");
233 //**********************************************************************************************************************