5 * Created by westcott on 5/7/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "makegroupcommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> MakeGroupCommand::getValidParameters(){
16 string Array[] = {"fasta", "groups","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "MakeGroupCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 MakeGroupCommand::MakeGroupCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["group"] = tempOutNames;
33 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
37 //**********************************************************************************************************************
38 vector<string> MakeGroupCommand::getRequiredParameters(){
40 string Array[] = {"fasta","groups"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "MakeGroupCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> MakeGroupCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "MakeGroupCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
62 MakeGroupCommand::MakeGroupCommand(string option) {
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string AlignArray[] = {"fasta","groups","outputdir","inputdir"};
74 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
76 OptionParser parser(option);
77 map<string, string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["group"] = 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 = ""; }
97 fastaFileName = validParameter.validFile(parameters, "fasta", false);
98 if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
100 m->splitAtDash(fastaFileName, fastaFileNames);
102 //go through files and make sure they are good, if not, then disregard them
103 for (int i = 0; i < fastaFileNames.size(); i++) {
104 if (inputDir != "") {
105 string path = m->hasPath(fastaFileNames[i]);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
111 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
113 //if you can't open it, try default location
114 if (ableToOpen == 1) {
115 if (m->getDefaultPath() != "") { //default path is set
116 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
117 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
119 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
121 fastaFileNames[i] = tryPath;
125 //if you can't open it, try default location
126 if (ableToOpen == 1) {
127 if (m->getOutputDir() != "") { //default path is set
128 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
129 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
131 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
133 fastaFileNames[i] = tryPath;
138 if (ableToOpen == 1) {
139 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
140 //erase from file list
141 fastaFileNames.erase(fastaFileNames.begin()+i);
143 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); }
146 //prevent giantic file name
147 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
148 else { filename += "groups"; }
150 //make sure there is at least one valid file left
151 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
154 //if the user changes the output directory command factory will send this info to us in the output parameter
155 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
157 groups = validParameter.validFile(parameters, "groups", false);
158 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
159 else { m->splitAtDash(groups, groupsNames); }
161 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; }
165 catch(exception& e) {
166 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
171 //**********************************************************************************************************************
173 MakeGroupCommand::~MakeGroupCommand(){ }
175 //**********************************************************************************************************************
177 void MakeGroupCommand::help(){
179 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
180 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
181 m->mothurOut("The make.group command should be in the following format: \n");
182 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
183 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
184 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
186 catch(exception& e) {
187 m->errorOut(e, "MakeGroupCommand", "help");
193 //**********************************************************************************************************************
195 int MakeGroupCommand::execute(){
197 if (abort == true) { return 0; }
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();
232 catch(exception& e) {
233 m->errorOut(e, "MakeGroupCommand", "execute");
237 //**********************************************************************************************************************