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 SplitGroupCommand::SplitGroupCommand(string option) {
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"name","group","groups","fasta","outputdir","inputdir"};
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string, string> parameters = parser.getParameters();
29 ValidParameters validParameter;
30 map<string, string>::iterator it;
32 //check to make sure all parameters are valid for command
33 for (it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 //if the user changes the input directory command factory will send this info to us in the output parameter
38 string inputDir = validParameter.validFile(parameters, "inputdir", false);
39 if (inputDir == "not found"){ inputDir = ""; }
42 it = parameters.find("group");
43 //user has given a template file
44 if(it != parameters.end()){
45 path = m->hasPath(it->second);
46 //if the user has not given a path then, add inputdir. else leave path alone.
47 if (path == "") { parameters["group"] = inputDir + it->second; }
50 it = parameters.find("fasta");
51 //user has given a template file
52 if(it != parameters.end()){
53 path = m->hasPath(it->second);
54 //if the user has not given a path then, add inputdir. else leave path alone.
55 if (path == "") { parameters["fasta"] = inputDir + it->second; }
58 it = parameters.find("name");
59 //user has given a template file
60 if(it != parameters.end()){
61 path = m->hasPath(it->second);
62 //if the user has not given a path then, add inputdir. else leave path alone.
63 if (path == "") { parameters["name"] = inputDir + it->second; }
69 namefile = validParameter.validFile(parameters, "name", true);
70 if (namefile == "not open") { abort = true; }
71 else if (namefile == "not found") { namefile = ""; }
73 fastafile = validParameter.validFile(parameters, "fasta", true);
74 if (fastafile == "not open") { abort = true; }
75 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
77 groupfile = validParameter.validFile(parameters, "group", true);
78 if (groupfile == "not open") { groupfile = ""; abort = true; }
79 else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
81 groups = validParameter.validFile(parameters, "groups", false);
82 if (groups == "not found") { groups = ""; }
83 else { m->splitAtDash(groups, Groups); }
85 //if the user changes the output directory command factory will send this info to us in the output parameter
86 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(groupfile); }
91 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
95 //**********************************************************************************************************************
96 void SplitGroupCommand::help(){
98 m->mothurOut("The split.group command reads a group file, and parses your fasta and names files by groups. \n");
99 m->mothurOut("The split.group command parameters are fasta, name, group and groups.\n");
100 m->mothurOut("The fasta and group parameters are required.\n");
101 m->mothurOut("The groups parameter allows you to select groups to create files for. \n");
102 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");
103 m->mothurOut("If you want .fasta and .names files for all groups, set groups=all. \n");
104 m->mothurOut("The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n");
105 m->mothurOut("Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n");
106 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
109 catch(exception& e) {
110 m->errorOut(e, "SplitGroupCommand", "help");
114 //**********************************************************************************************************************
115 SplitGroupCommand::~SplitGroupCommand(){ }
116 //**********************************************************************************************************************
117 int SplitGroupCommand::execute(){
120 if (abort == true) { return 0; }
122 groupMap = new GroupMap(groupfile);
125 SharedUtil util; util.setGroups(Groups, groupMap->namesOfGroups);
127 if (namefile != "") { readNames(); }
132 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
134 m->mothurOutEndLine();
135 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
136 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
137 m->mothurOutEndLine();
141 catch(exception& e) {
142 m->errorOut(e, "SplitGroupCommand", "execute");
146 /**********************************************************************************************************************/
147 int SplitGroupCommand::readNames() {
151 m->openInputFile(namefile, in);
154 if (m->control_pressed) { break; }
156 string firstCol, secondCol;
157 in >> firstCol >> secondCol; m->gobble(in);
159 vector<string> names;
160 m->splitAtComma(secondCol, names);
162 nameMap[firstCol] = names;
169 catch(exception& e) {
170 m->errorOut(e, "SplitGroupCommand", "readNames");
175 /**********************************************************************************************************************/
176 int SplitGroupCommand::splitFasta() {
179 string filerootFasta = outputDir + m->getRootName(m->getSimpleName(fastafile));
180 string filerootName = outputDir + m->getRootName(m->getSimpleName(namefile));
183 map<string, ofstream*> filehandles;
184 map<string, ofstream*>::iterator it3;
186 for (int i=0; i<Groups.size(); i++) {
188 filehandles[Groups[i]+"fasta"] = temp;
189 m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
190 outputNames.push_back(filerootFasta + Groups[i] + ".fasta");
192 if (namefile != "") {
193 temp2 = new ofstream;
194 filehandles[Groups[i]+"name"] = temp2;
195 m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
196 outputNames.push_back(filerootName + Groups[i] + ".names");
202 m->openInputFile(fastafile, in);
205 if (m->control_pressed) { break; }
207 Sequence seq(in); m->gobble(in);
209 if (seq.getName() != "") {
211 itNames = nameMap.find(seq.getName());
213 if (itNames == nameMap.end()) {
214 if (namefile != "") { m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine(); }
215 else { //no names file given
216 string group = groupMap->getGroup(seq.getName());
218 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
219 seq.printSequence(*(filehandles[group+"fasta"]));
220 }else if(group == "not found") {
221 m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
225 vector<string> names = itNames->second;
226 map<string, string> group2Names;
227 map<string, string>::iterator it;
229 for (int i = 0; i < names.size(); i++) { //build strings for new group names file, will select rep below
230 string group = groupMap->getGroup(names[i]);
232 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
233 it = group2Names.find(group);
234 if (it == group2Names.end()) {
235 group2Names[group] = names[i] + ",";
237 group2Names[group] += names[i] + ",";
239 }else if(group == "not found") {
240 m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
244 for (map<string, string>::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) {
245 //edit names string, by grabbing the first guy as the rep and removing the last comma
246 string newNames = itGroups->second;
247 newNames = newNames.substr(0, newNames.length()-1);
250 int pos = newNames.find_first_of(',');
251 if (pos == newNames.npos) { //only one sequence so it represents itself
254 repName = newNames.substr(0, pos);
257 *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl;
258 *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl;
266 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
267 (*(filehandles[it3->first])).close();
274 catch(exception& e) {
275 m->errorOut(e, "SplitGroupCommand", "splitFasta");
279 /**********************************************************************************************************************/