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(){
52 //initialize outputTypes
53 vector<string> tempOutNames;
54 outputTypes["fasta"] = tempOutNames;
55 outputTypes["name"] = tempOutNames;
58 m->errorOut(e, "SplitGroupCommand", "SplitGroupCommand");
62 //**********************************************************************************************************************
63 SplitGroupCommand::SplitGroupCommand(string option) {
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"name","group","groups","fasta","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string, string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["fasta"] = tempOutNames;
89 outputTypes["name"] = tempOutNames;
91 //if the user changes the input directory command factory will send this info to us in the output parameter
92 string inputDir = validParameter.validFile(parameters, "inputdir", false);
93 if (inputDir == "not found"){ inputDir = ""; }
96 it = parameters.find("group");
97 //user has given a template file
98 if(it != parameters.end()){
99 path = m->hasPath(it->second);
100 //if the user has not given a path then, add inputdir. else leave path alone.
101 if (path == "") { parameters["group"] = inputDir + it->second; }
104 it = parameters.find("fasta");
105 //user has given a template file
106 if(it != parameters.end()){
107 path = m->hasPath(it->second);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { parameters["fasta"] = inputDir + it->second; }
112 it = parameters.find("name");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["name"] = inputDir + it->second; }
123 namefile = validParameter.validFile(parameters, "name", true);
124 if (namefile == "not open") { abort = true; }
125 else if (namefile == "not found") { namefile = ""; }
127 fastafile = validParameter.validFile(parameters, "fasta", true);
128 if (fastafile == "not open") { abort = true; }
129 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
131 groupfile = validParameter.validFile(parameters, "group", true);
132 if (groupfile == "not open") { groupfile = ""; abort = true; }
133 else if (groupfile == "not found") { groupfile = ""; m->mothurOut("group is a required parameter for the split.group command. "); m->mothurOutEndLine(); abort = true; }
135 groups = validParameter.validFile(parameters, "groups", false);
136 if (groups == "not found") { groups = ""; }
137 else { m->splitAtDash(groups, Groups); }
139 //if the user changes the output directory command factory will send this info to us in the output parameter
140 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(groupfile); }
144 catch(exception& e) {
145 m->errorOut(e, "SplitGroupCommand", "SplitAbundCommand");
149 //**********************************************************************************************************************
150 void SplitGroupCommand::help(){
152 m->mothurOut("The split.group command reads a group file, and parses your fasta and names files by groups. \n");
153 m->mothurOut("The split.group command parameters are fasta, name, group and groups.\n");
154 m->mothurOut("The fasta and group parameters are required.\n");
155 m->mothurOut("The groups parameter allows you to select groups to create files for. \n");
156 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");
157 m->mothurOut("If you want .fasta and .names files for all groups, set groups=all. \n");
158 m->mothurOut("The split.group command should be used in the following format: split.group(fasta=yourFasta, group=yourGroupFile).\n");
159 m->mothurOut("Example: split.group(fasta=abrecovery.fasta, group=abrecovery.groups).\n");
160 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
163 catch(exception& e) {
164 m->errorOut(e, "SplitGroupCommand", "help");
168 //**********************************************************************************************************************
169 SplitGroupCommand::~SplitGroupCommand(){ }
170 //**********************************************************************************************************************
171 int SplitGroupCommand::execute(){
174 if (abort == true) { return 0; }
176 groupMap = new GroupMap(groupfile);
179 SharedUtil util; util.setGroups(Groups, groupMap->namesOfGroups);
181 if (namefile != "") { readNames(); }
186 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
188 m->mothurOutEndLine();
189 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
190 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
191 m->mothurOutEndLine();
195 catch(exception& e) {
196 m->errorOut(e, "SplitGroupCommand", "execute");
200 /**********************************************************************************************************************/
201 int SplitGroupCommand::readNames() {
205 m->openInputFile(namefile, in);
208 if (m->control_pressed) { break; }
210 string firstCol, secondCol;
211 in >> firstCol >> secondCol; m->gobble(in);
213 vector<string> names;
214 m->splitAtComma(secondCol, names);
216 nameMap[firstCol] = names;
223 catch(exception& e) {
224 m->errorOut(e, "SplitGroupCommand", "readNames");
229 /**********************************************************************************************************************/
230 int SplitGroupCommand::splitFasta() {
233 string filerootFasta = outputDir + m->getRootName(m->getSimpleName(fastafile));
234 string filerootName = outputDir + m->getRootName(m->getSimpleName(namefile));
237 map<string, ofstream*> filehandles;
238 map<string, ofstream*>::iterator it3;
240 for (int i=0; i<Groups.size(); i++) {
242 filehandles[Groups[i]+"fasta"] = temp;
243 m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
244 outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta");
246 if (namefile != "") {
247 temp2 = new ofstream;
248 filehandles[Groups[i]+"name"] = temp2;
249 m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
250 outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names");
256 m->openInputFile(fastafile, in);
259 if (m->control_pressed) { break; }
261 Sequence seq(in); m->gobble(in);
263 if (seq.getName() != "") {
265 itNames = nameMap.find(seq.getName());
267 if (itNames == nameMap.end()) {
268 if (namefile != "") { m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine(); }
269 else { //no names file given
270 string group = groupMap->getGroup(seq.getName());
272 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
273 seq.printSequence(*(filehandles[group+"fasta"]));
274 }else if(group == "not found") {
275 m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
279 vector<string> names = itNames->second;
280 map<string, string> group2Names;
281 map<string, string>::iterator it;
283 for (int i = 0; i < names.size(); i++) { //build strings for new group names file, will select rep below
284 string group = groupMap->getGroup(names[i]);
286 if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
287 it = group2Names.find(group);
288 if (it == group2Names.end()) {
289 group2Names[group] = names[i] + ",";
291 group2Names[group] += names[i] + ",";
293 }else if(group == "not found") {
294 m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
298 for (map<string, string>::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) {
299 //edit names string, by grabbing the first guy as the rep and removing the last comma
300 string newNames = itGroups->second;
301 newNames = newNames.substr(0, newNames.length()-1);
304 int pos = newNames.find_first_of(',');
305 if (pos == newNames.npos) { //only one sequence so it represents itself
308 repName = newNames.substr(0, pos);
311 *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl;
312 *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl;
320 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
321 (*(filehandles[it3->first])).close();
328 catch(exception& e) {
329 m->errorOut(e, "SplitGroupCommand", "splitFasta");
333 /**********************************************************************************************************************/