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"
13 //**********************************************************************************************************************
14 vector<string> SplitGroupCommand::getValidParameters(){
16 string Array[] = {"name","group","groups","fasta","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "SplitGroupCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 vector<string> SplitGroupCommand::getRequiredParameters(){
28 string Array[] = {"fasta","group"};
29 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
33 m->errorOut(e, "SplitGroupCommand", "getRequiredParameters");
37 //**********************************************************************************************************************
38 vector<string> SplitGroupCommand::getRequiredFiles(){
40 vector<string> myArray;
44 m->errorOut(e, "SplitGroupCommand", "getRequiredFiles");
48 //**********************************************************************************************************************
49 SplitGroupCommand::SplitGroupCommand(){
51 abort = true; calledHelp = true;
52 vector<string> tempOutNames;
53 outputTypes["fasta"] = tempOutNames;
54 outputTypes["name"] = tempOutNames;
57 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
61 //**********************************************************************************************************************
62 SplitGroupCommand::SplitGroupCommand(string option) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 //valid paramters for this command
71 string Array[] = {"name","group","groups","fasta","outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string, string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string, string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
88 outputTypes["name"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("group");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["group"] = inputDir + it->second; }
103 it = parameters.find("fasta");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["fasta"] = inputDir + it->second; }
111 it = parameters.find("name");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["name"] = inputDir + it->second; }
122 namefile = validParameter.validFile(parameters, "name", true);
123 if (namefile == "not open") { abort = true; }
124 else if (namefile == "not found") { namefile = ""; }
126 fastafile = validParameter.validFile(parameters, "fasta", true);
127 if (fastafile == "not open") { abort = true; }
128 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
130 groupfile = validParameter.validFile(parameters, "group", true);
131 if (groupfile == "not open") { groupfile = ""; abort = true; }
132 else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
134 groups = validParameter.validFile(parameters, "groups", false);
135 if (groups == "not found") { groups = ""; }
136 else { m->splitAtDash(groups, Groups); }
138 //if the user changes the output directory command factory will send this info to us in the output parameter
139 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(groupfile); }
143 catch(exception& e) {
144 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
148 //**********************************************************************************************************************
149 void SplitGroupCommand::help(){
151 m->mothurOut("The split.group command reads a group file, and parses your fasta and names files by groups. \n");
152 m->mothurOut("The split.group command parameters are fasta, name, group and groups.\n");
153 m->mothurOut("The fasta and group parameters are required.\n");
154 m->mothurOut("The groups parameter allows you to select groups to create files for. \n");
155 m->mothurOut("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");
156 m->mothurOut("If you want .fasta and .names files for all groups, set groups=all. \n");
157 m->mothurOut("The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n");
158 m->mothurOut("Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n");
159 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
162 catch(exception& e) {
163 m->errorOut(e, "SplitGroupCommand", "help");
167 //**********************************************************************************************************************
168 SplitGroupCommand::~SplitGroupCommand(){ }
169 //**********************************************************************************************************************
170 int SplitGroupCommand::execute(){
173 if (abort == true) { if (calledHelp) { return 0; } return 2; }
175 groupMap = new GroupMap(groupfile);
178 SharedUtil util; util.setGroups(Groups, groupMap->namesOfGroups);
180 if (namefile != "") { readNames(); }
185 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
188 itTypes = outputTypes.find("fasta");
189 if (itTypes != outputTypes.end()) {
190 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
193 itTypes = outputTypes.find("name");
194 if (itTypes != outputTypes.end()) {
195 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
198 m->mothurOutEndLine();
199 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
200 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
201 m->mothurOutEndLine();
205 catch(exception& e) {
206 m->errorOut(e, "SplitGroupCommand", "execute");
210 /**********************************************************************************************************************/
211 int SplitGroupCommand::readNames() {
215 m->openInputFile(namefile, in);
218 if (m->control_pressed) { break; }
220 string firstCol, secondCol;
221 in >> firstCol >> secondCol; m->gobble(in);
223 vector<string> names;
224 m->splitAtComma(secondCol, names);
226 nameMap[firstCol] = names;
233 catch(exception& e) {
234 m->errorOut(e, "SplitGroupCommand", "readNames");
239 /**********************************************************************************************************************/
240 int SplitGroupCommand::splitFasta() {
243 string filerootFasta = outputDir + m->getRootName(m->getSimpleName(fastafile));
244 string filerootName = outputDir + m->getRootName(m->getSimpleName(namefile));
247 map<string, ofstream*> filehandles;
248 map<string, ofstream*>::iterator it3;
250 for (int i=0; i<Groups.size(); i++) {
252 filehandles[Groups[i]+"fasta"] = temp;
253 m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
254 outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta");
256 if (namefile != "") {
257 temp2 = new ofstream;
258 filehandles[Groups[i]+"name"] = temp2;
259 m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
260 outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names");
266 m->openInputFile(fastafile, in);
269 if (m->control_pressed) { break; }
271 Sequence seq(in); m->gobble(in);
273 if (seq.getName() != "") {
275 itNames = nameMap.find(seq.getName());
277 if (itNames == nameMap.end()) {
278 if (namefile != "") { m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine(); }
279 else { //no names file given
280 string group = groupMap->getGroup(seq.getName());
282 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
283 seq.printSequence(*(filehandles[group+"fasta"]));
284 }else if(group == "not found") {
285 m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
289 vector<string> names = itNames->second;
290 map<string, string> group2Names;
291 map<string, string>::iterator it;
293 for (int i = 0; i < names.size(); i++) { //build strings for new group names file, will select rep below
294 string group = groupMap->getGroup(names[i]);
296 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
297 it = group2Names.find(group);
298 if (it == group2Names.end()) {
299 group2Names[group] = names[i] + ",";
301 group2Names[group] += names[i] + ",";
303 }else if(group == "not found") {
304 m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
308 for (map<string, string>::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) {
309 //edit names string, by grabbing the first guy as the rep and removing the last comma
310 string newNames = itGroups->second;
311 newNames = newNames.substr(0, newNames.length()-1);
314 int pos = newNames.find_first_of(',');
315 if (pos == newNames.npos) { //only one sequence so it represents itself
318 repName = newNames.substr(0, pos);
321 *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl;
322 *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl;
330 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
331 (*(filehandles[it3->first])).close();
335 vector<string> newOutputNames;
337 for (int i = 0; i < outputNames.size(); i++) {
338 if (m->isBlank(outputNames[i])) {
339 remove(outputNames[i].c_str());
340 }else { newOutputNames.push_back(outputNames[i]); }
342 outputNames = newOutputNames;
347 catch(exception& e) {
348 m->errorOut(e, "SplitGroupCommand", "splitFasta");
352 /**********************************************************************************************************************/