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 pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "MakeGroupCommand", "setParameters");
31 //**********************************************************************************************************************
32 string MakeGroupCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n";
36 helpString += "The make.group command parameters are fasta, groups and output. Fasta and group are required.\n";
37 helpString += "The output parameter allows you to specify the name of groupfile created. \n";
38 helpString += "The make.group command should be in the following format: \n";
39 helpString += "make.group(fasta=yourFastaFiles, groups=yourGroups). \n";
40 helpString += "Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n";
41 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n";
45 m->errorOut(e, "MakeGroupCommand", "getHelpString");
49 //**********************************************************************************************************************
50 MakeGroupCommand::MakeGroupCommand(){
52 abort = true; calledHelp = true;
54 vector<string> tempOutNames;
55 outputTypes["group"] = tempOutNames;
58 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
63 //**********************************************************************************************************************
65 MakeGroupCommand::MakeGroupCommand(string option) {
68 abort = false; calledHelp = false;
70 //allow user to run help
71 if(option == "help") { help(); abort = true; calledHelp = true; }
72 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
75 vector<string> myArray = setParameters();
77 OptionParser parser(option);
78 map<string, string> parameters = parser.getParameters();
80 ValidParameters validParameter;
81 map<string, string>::iterator it;
83 //check to make sure all parameters are valid for command
84 for (it = parameters.begin(); it != parameters.end(); it++) {
85 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
88 //initialize outputTypes
89 vector<string> tempOutNames;
90 outputTypes["group"] = tempOutNames;
92 //if the user changes the input directory command factory will send this info to us in the output parameter
93 string inputDir = validParameter.validFile(parameters, "inputdir", false);
94 if (inputDir == "not found"){ inputDir = ""; }
96 fastaFileName = validParameter.validFile(parameters, "fasta", false);
97 if (fastaFileName == "not found") { //if there is a current fasta file, use it
98 string filename = m->getFastaFile();
99 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
100 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
102 m->splitAtDash(fastaFileName, fastaFileNames);
104 //go through files and make sure they are good, if not, then disregard them
105 for (int i = 0; i < fastaFileNames.size(); i++) {
108 if (fastaFileNames[i] == "current") {
109 fastaFileNames[i] = m->getFastaFile();
110 if (fastaFileNames[i] != "") {
111 m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine();
112 filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));
115 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
116 //erase from file list
117 fastaFileNames.erase(fastaFileNames.begin()+i);
123 if (inputDir != "") {
124 string path = m->hasPath(fastaFileNames[i]);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
130 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
132 //if you can't open it, try default location
133 if (ableToOpen == 1) {
134 if (m->getDefaultPath() != "") { //default path is set
135 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
136 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
138 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
140 fastaFileNames[i] = tryPath;
144 //if you can't open it, try default location
145 if (ableToOpen == 1) {
146 if (m->getOutputDir() != "") { //default path is set
147 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
148 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
150 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
152 fastaFileNames[i] = tryPath;
157 if (ableToOpen == 1) {
158 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
159 //erase from file list
160 fastaFileNames.erase(fastaFileNames.begin()+i);
162 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); m->setFastaFile(fastaFileNames[i]); }
166 //prevent giantic file name
167 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
168 else { filename += "groups"; }
170 //make sure there is at least one valid file left
171 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
174 //if the user changes the output directory command factory will send this info to us in the output parameter
175 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
177 output = validParameter.validFile(parameters, "output", false);
178 if (output == "not found") { output = ""; }
179 else{ filename = output; }
181 groups = validParameter.validFile(parameters, "groups", false);
182 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
183 else { m->splitAtDash(groups, groupsNames); }
185 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; }
188 catch(exception& e) {
189 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
193 //**********************************************************************************************************************
195 int MakeGroupCommand::execute(){
197 if (abort == true) { if (calledHelp) { return 0; } return 2; }
199 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
201 filename = outputDir + filename;
204 m->openOutputFile(filename, out);
206 for (int i = 0; i < fastaFileNames.size(); i++) {
208 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); return 0; }
211 m->openInputFile(fastaFileNames[i], in);
215 Sequence seq(in, "no align"); m->gobble(in);
217 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(filename.c_str()); return 0; }
219 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
226 m->mothurOutEndLine();
227 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename);
228 m->mothurOutEndLine();
230 //set group file as new current groupfile
232 itTypes = outputTypes.find("group");
233 if (itTypes != outputTypes.end()) {
234 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
239 catch(exception& e) {
240 m->errorOut(e, "MakeGroupCommand", "execute");
244 //**********************************************************************************************************************