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","group",false,true,true); parameters.push_back(pfasta);
18 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false,true); 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::getOutputPattern(string type) {
55 if (type == "group") { pattern = "[filename],groups"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
61 m->errorOut(e, "MakeGroupCommand", "getOutputPattern");
65 //**********************************************************************************************************************
66 MakeGroupCommand::MakeGroupCommand(){
68 abort = true; calledHelp = true;
70 vector<string> tempOutNames;
71 outputTypes["group"] = tempOutNames;
74 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
79 //**********************************************************************************************************************
81 MakeGroupCommand::MakeGroupCommand(string option) {
84 abort = false; calledHelp = false;
86 //allow user to run help
87 if(option == "help") { help(); abort = true; calledHelp = true; }
88 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
91 vector<string> myArray = setParameters();
93 OptionParser parser(option);
94 map<string, string> parameters = parser.getParameters();
96 ValidParameters validParameter;
97 map<string, string>::iterator it;
99 //check to make sure all parameters are valid for command
100 for (it = parameters.begin(); it != parameters.end(); it++) {
101 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
104 //initialize outputTypes
105 vector<string> tempOutNames;
106 outputTypes["group"] = tempOutNames;
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
110 if (inputDir == "not found"){ inputDir = ""; }
112 fastaFileName = validParameter.validFile(parameters, "fasta", false);
113 if (fastaFileName == "not found") { //if there is a current fasta file, use it
114 string filename = m->getFastaFile();
115 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
116 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
118 m->splitAtDash(fastaFileName, fastaFileNames);
120 //go through files and make sure they are good, if not, then disregard them
121 for (int i = 0; i < fastaFileNames.size(); i++) {
124 if (fastaFileNames[i] == "current") {
125 fastaFileNames[i] = m->getFastaFile();
126 if (fastaFileNames[i] != "") {
127 m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine();
128 filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));
131 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
132 //erase from file list
133 fastaFileNames.erase(fastaFileNames.begin()+i);
139 if (inputDir != "") {
140 string path = m->hasPath(fastaFileNames[i]);
141 //if the user has not given a path then, add inputdir. else leave path alone.
142 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
146 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
148 //if you can't open it, try default location
149 if (ableToOpen == 1) {
150 if (m->getDefaultPath() != "") { //default path is set
151 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
152 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
154 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
156 fastaFileNames[i] = tryPath;
160 //if you can't open it, try default location
161 if (ableToOpen == 1) {
162 if (m->getOutputDir() != "") { //default path is set
163 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
164 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
166 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
168 fastaFileNames[i] = tryPath;
173 if (ableToOpen == 1) {
174 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
175 //erase from file list
176 fastaFileNames.erase(fastaFileNames.begin()+i);
178 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); m->setFastaFile(fastaFileNames[i]); }
182 //prevent giantic file name
183 map<string, string> variables;
184 variables["[filename]"] = filename;
185 if (fastaFileNames.size() > 3) { variables["[filename]"] = outputDir + "merge"; }
186 filename = getOutputFileName("group",variables);
188 //make sure there is at least one valid file left
189 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
192 //if the user changes the output directory command factory will send this info to us in the output parameter
193 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
195 output = validParameter.validFile(parameters, "output", false);
196 if (output == "not found") { output = ""; }
197 else{ filename = output; }
199 groups = validParameter.validFile(parameters, "groups", false);
200 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
201 else { m->splitAtDash(groups, groupsNames); }
203 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; }
206 catch(exception& e) {
207 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
211 //**********************************************************************************************************************
213 int MakeGroupCommand::execute(){
215 if (abort == true) { if (calledHelp) { return 0; } return 2; }
217 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
219 filename = outputDir + filename;
222 m->openOutputFile(filename, out);
224 for (int i = 0; i < fastaFileNames.size(); i++) {
226 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); return 0; }
229 m->openInputFile(fastaFileNames[i], in);
233 Sequence seq(in, "no align"); m->gobble(in);
235 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); m->mothurRemove(filename); return 0; }
237 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
244 m->mothurOutEndLine();
245 m->mothurOut("Output File Names: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename);
246 m->mothurOutEndLine();
248 //set group file as new current groupfile
250 itTypes = outputTypes.find("group");
251 if (itTypes != outputTypes.end()) {
252 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
257 catch(exception& e) {
258 m->errorOut(e, "MakeGroupCommand", "execute");
262 //**********************************************************************************************************************