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 MakeGroupCommand::MakeGroupCommand(){
53 abort = true; calledHelp = true;
55 vector<string> tempOutNames;
56 outputTypes["group"] = tempOutNames;
59 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
64 //**********************************************************************************************************************
66 MakeGroupCommand::MakeGroupCommand(string option) {
69 abort = false; calledHelp = false;
71 //allow user to run help
72 if(option == "help") { help(); abort = true; calledHelp = true; }
73 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
76 vector<string> myArray = setParameters();
78 OptionParser parser(option);
79 map<string, string> parameters = parser.getParameters();
81 ValidParameters validParameter;
82 map<string, string>::iterator it;
84 //check to make sure all parameters are valid for command
85 for (it = parameters.begin(); it != parameters.end(); it++) {
86 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["group"] = tempOutNames;
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
97 fastaFileName = validParameter.validFile(parameters, "fasta", false);
98 if (fastaFileName == "not found") { //if there is a current fasta file, use it
99 string filename = m->getFastaFile();
100 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
101 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
103 m->splitAtDash(fastaFileName, fastaFileNames);
105 //go through files and make sure they are good, if not, then disregard them
106 for (int i = 0; i < fastaFileNames.size(); i++) {
109 if (fastaFileNames[i] == "current") {
110 fastaFileNames[i] = m->getFastaFile();
111 if (fastaFileNames[i] != "") {
112 m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine();
113 filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));
116 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
117 //erase from file list
118 fastaFileNames.erase(fastaFileNames.begin()+i);
124 if (inputDir != "") {
125 string path = m->hasPath(fastaFileNames[i]);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
131 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
133 //if you can't open it, try default location
134 if (ableToOpen == 1) {
135 if (m->getDefaultPath() != "") { //default path is set
136 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
137 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
139 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
141 fastaFileNames[i] = tryPath;
145 //if you can't open it, try default location
146 if (ableToOpen == 1) {
147 if (m->getOutputDir() != "") { //default path is set
148 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
149 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
151 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
153 fastaFileNames[i] = tryPath;
158 if (ableToOpen == 1) {
159 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
160 //erase from file list
161 fastaFileNames.erase(fastaFileNames.begin()+i);
163 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); m->setFastaFile(fastaFileNames[i]); }
167 //prevent giantic file name
168 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
169 else { filename += "groups"; }
171 //make sure there is at least one valid file left
172 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
175 //if the user changes the output directory command factory will send this info to us in the output parameter
176 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
178 output = validParameter.validFile(parameters, "output", false);
179 if (output == "not found") { output = ""; }
180 else{ filename = output; }
182 groups = validParameter.validFile(parameters, "groups", false);
183 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
184 else { m->splitAtDash(groups, groupsNames); }
186 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; }
189 catch(exception& e) {
190 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
194 //**********************************************************************************************************************
196 int MakeGroupCommand::execute(){
198 if (abort == true) { if (calledHelp) { return 0; } return 2; }
200 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
202 filename = outputDir + filename;
205 m->openOutputFile(filename, out);
207 for (int i = 0; i < fastaFileNames.size(); i++) {
209 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); return 0; }
212 m->openInputFile(fastaFileNames[i], in);
216 Sequence seq(in, "no align"); m->gobble(in);
218 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); m->mothurRemove(filename); return 0; }
220 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
227 m->mothurOutEndLine();
228 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename);
229 m->mothurOutEndLine();
231 //set group file as new current groupfile
233 itTypes = outputTypes.find("group");
234 if (itTypes != outputTypes.end()) {
235 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
240 catch(exception& e) {
241 m->errorOut(e, "MakeGroupCommand", "execute");
245 //**********************************************************************************************************************