5 * Created by westcott on 5/7/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "makegroupcommand.h"
11 #include "sequence.hpp"
14 //**********************************************************************************************************************
15 vector<string> MakeGroupCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
18 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
19 CommandParameter poutput("output", "String", "", "", "", "", "",false,false); parameters.push_back(poutput);
20 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "MakeGroupCommand", "setParameters");
32 //**********************************************************************************************************************
33 string MakeGroupCommand::getHelpString(){
35 string helpString = "";
36 helpString += "The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n";
37 helpString += "The make.group command parameters are fasta, groups and output. Fasta and group are required.\n";
38 helpString += "The output parameter allows you to specify the name of groupfile created. \n";
39 helpString += "The make.group command should be in the following format: \n";
40 helpString += "make.group(fasta=yourFastaFiles, groups=yourGroups). \n";
41 helpString += "Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n";
42 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n";
46 m->errorOut(e, "MakeGroupCommand", "getHelpString");
50 //**********************************************************************************************************************
51 string MakeGroupCommand::getOutputFileNameTag(string type, string inputName=""){
53 string outputFileName = "";
54 map<string, vector<string> >::iterator it;
56 //is this a type this command creates
57 it = outputTypes.find(type);
58 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
60 if (type == "group") { outputFileName = "groups"; }
61 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
63 return outputFileName;
66 m->errorOut(e, "MakeGroupCommand", "getOutputFileNameTag");
70 //**********************************************************************************************************************
71 MakeGroupCommand::MakeGroupCommand(){
73 abort = true; calledHelp = true;
75 vector<string> tempOutNames;
76 outputTypes["group"] = tempOutNames;
79 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
84 //**********************************************************************************************************************
86 MakeGroupCommand::MakeGroupCommand(string option) {
89 abort = false; calledHelp = false;
91 //allow user to run help
92 if(option == "help") { help(); abort = true; calledHelp = true; }
93 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
96 vector<string> myArray = setParameters();
98 OptionParser parser(option);
99 map<string, string> parameters = parser.getParameters();
101 ValidParameters validParameter;
102 map<string, string>::iterator it;
104 //check to make sure all parameters are valid for command
105 for (it = parameters.begin(); it != parameters.end(); it++) {
106 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
109 //initialize outputTypes
110 vector<string> tempOutNames;
111 outputTypes["group"] = tempOutNames;
113 //if the user changes the input directory command factory will send this info to us in the output parameter
114 string inputDir = validParameter.validFile(parameters, "inputdir", false);
115 if (inputDir == "not found"){ inputDir = ""; }
117 fastaFileName = validParameter.validFile(parameters, "fasta", false);
118 if (fastaFileName == "not found") { //if there is a current fasta file, use it
119 string filename = m->getFastaFile();
120 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
121 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
123 m->splitAtDash(fastaFileName, fastaFileNames);
125 //go through files and make sure they are good, if not, then disregard them
126 for (int i = 0; i < fastaFileNames.size(); i++) {
129 if (fastaFileNames[i] == "current") {
130 fastaFileNames[i] = m->getFastaFile();
131 if (fastaFileNames[i] != "") {
132 m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine();
133 filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));
136 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
137 //erase from file list
138 fastaFileNames.erase(fastaFileNames.begin()+i);
144 if (inputDir != "") {
145 string path = m->hasPath(fastaFileNames[i]);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
151 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
153 //if you can't open it, try default location
154 if (ableToOpen == 1) {
155 if (m->getDefaultPath() != "") { //default path is set
156 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
157 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
159 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
161 fastaFileNames[i] = tryPath;
165 //if you can't open it, try default location
166 if (ableToOpen == 1) {
167 if (m->getOutputDir() != "") { //default path is set
168 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
169 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
171 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
173 fastaFileNames[i] = tryPath;
178 if (ableToOpen == 1) {
179 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
180 //erase from file list
181 fastaFileNames.erase(fastaFileNames.begin()+i);
183 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); m->setFastaFile(fastaFileNames[i]); }
187 //prevent giantic file name
188 if (fastaFileNames.size() > 3) { filename = outputDir + "merge." + getOutputFileNameTag("group"); }
189 else { filename += getOutputFileNameTag("group"); }
191 //make sure there is at least one valid file left
192 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
195 //if the user changes the output directory command factory will send this info to us in the output parameter
196 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
198 output = validParameter.validFile(parameters, "output", false);
199 if (output == "not found") { output = ""; }
200 else{ filename = output; }
202 groups = validParameter.validFile(parameters, "groups", false);
203 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
204 else { m->splitAtDash(groups, groupsNames); }
206 if (groupsNames.size() != fastaFileNames.size()) { m->mothurOut("You do not have the same number of valid fastfile files as groups. This could be because we could not open a fastafile."); m->mothurOutEndLine(); abort = true; }
209 catch(exception& e) {
210 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
214 //**********************************************************************************************************************
216 int MakeGroupCommand::execute(){
218 if (abort == true) { if (calledHelp) { return 0; } return 2; }
220 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
222 filename = outputDir + filename;
225 m->openOutputFile(filename, out);
227 for (int i = 0; i < fastaFileNames.size(); i++) {
229 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); return 0; }
232 m->openInputFile(fastaFileNames[i], in);
236 Sequence seq(in, "no align"); m->gobble(in);
238 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); m->mothurRemove(filename); return 0; }
240 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
247 m->mothurOutEndLine();
248 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename);
249 m->mothurOutEndLine();
251 //set group file as new current groupfile
253 itTypes = outputTypes.find("group");
254 if (itTypes != outputTypes.end()) {
255 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
260 catch(exception& e) {
261 m->errorOut(e, "MakeGroupCommand", "execute");
265 //**********************************************************************************************************************