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", "output","groups","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "MakeGroupCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 MakeGroupCommand::MakeGroupCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["group"] = tempOutNames;
34 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
38 //**********************************************************************************************************************
39 vector<string> MakeGroupCommand::getRequiredParameters(){
41 string Array[] = {"fasta","groups"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "MakeGroupCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> MakeGroupCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "MakeGroupCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
63 MakeGroupCommand::MakeGroupCommand(string option) {
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
73 //valid paramters for this command
74 string AlignArray[] = {"fasta","groups","output","outputdir","inputdir"};
75 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
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") { m->mothurOut("fasta is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
99 m->splitAtDash(fastaFileName, fastaFileNames);
101 //go through files and make sure they are good, if not, then disregard them
102 for (int i = 0; i < fastaFileNames.size(); i++) {
103 if (inputDir != "") {
104 string path = m->hasPath(fastaFileNames[i]);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
110 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
112 //if you can't open it, try default location
113 if (ableToOpen == 1) {
114 if (m->getDefaultPath() != "") { //default path is set
115 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
116 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
118 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
120 fastaFileNames[i] = tryPath;
124 //if you can't open it, try default location
125 if (ableToOpen == 1) {
126 if (m->getOutputDir() != "") { //default path is set
127 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
128 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
130 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
132 fastaFileNames[i] = tryPath;
137 if (ableToOpen == 1) {
138 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
139 //erase from file list
140 fastaFileNames.erase(fastaFileNames.begin()+i);
142 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); }
145 //prevent giantic file name
146 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
147 else { filename += "groups"; }
149 //make sure there is at least one valid file left
150 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
153 //if the user changes the output directory command factory will send this info to us in the output parameter
154 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
156 output = validParameter.validFile(parameters, "output", false);
157 if (output == "not found") { output = ""; }
158 else{ filename = output; }
160 groups = validParameter.validFile(parameters, "groups", false);
161 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
162 else { m->splitAtDash(groups, groupsNames); }
164 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; }
166 cout << "here" << endl;
168 catch(exception& e) {
169 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
174 //**********************************************************************************************************************
176 MakeGroupCommand::~MakeGroupCommand(){ }
178 //**********************************************************************************************************************
180 void MakeGroupCommand::help(){
182 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
183 m->mothurOut("The make.group command parameters are fasta, groups and output. Fasta and group are required.\n");
184 m->mothurOut("The output parameter allows you to specify the name of groupfile created. \n");
185 m->mothurOut("The make.group command should be in the following format: \n");
186 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups). \n");
187 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
188 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
190 catch(exception& e) {
191 m->errorOut(e, "MakeGroupCommand", "help");
197 //**********************************************************************************************************************
199 int MakeGroupCommand::execute(){
201 if (abort == true) { return 0; }
203 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
205 filename = outputDir + filename;
208 m->openOutputFile(filename, out);
210 for (int i = 0; i < fastaFileNames.size(); i++) {
212 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); return 0; }
215 m->openInputFile(fastaFileNames[i], in);
219 Sequence seq(in, "no align"); m->gobble(in);
221 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(filename.c_str()); return 0; }
223 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
230 m->mothurOutEndLine();
231 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename);
232 m->mothurOutEndLine();
236 catch(exception& e) {
237 m->errorOut(e, "MakeGroupCommand", "execute");
241 //**********************************************************************************************************************