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(){
28 abort = true; calledHelp = true;
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) {
65 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
72 //valid paramters for this command
73 string AlignArray[] = {"fasta","groups","output","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 = ""; }
95 fastaFileName = validParameter.validFile(parameters, "fasta", false);
96 if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
98 m->splitAtDash(fastaFileName, fastaFileNames);
100 //go through files and make sure they are good, if not, then disregard them
101 for (int i = 0; i < fastaFileNames.size(); i++) {
102 if (inputDir != "") {
103 string path = m->hasPath(fastaFileNames[i]);
104 //if the user has not given a path then, add inputdir. else leave path alone.
105 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
109 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
111 //if you can't open it, try default location
112 if (ableToOpen == 1) {
113 if (m->getDefaultPath() != "") { //default path is set
114 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
115 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
117 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
119 fastaFileNames[i] = tryPath;
123 //if you can't open it, try default location
124 if (ableToOpen == 1) {
125 if (m->getOutputDir() != "") { //default path is set
126 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
127 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
129 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
131 fastaFileNames[i] = tryPath;
136 if (ableToOpen == 1) {
137 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
138 //erase from file list
139 fastaFileNames.erase(fastaFileNames.begin()+i);
141 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); }
144 //prevent giantic file name
145 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
146 else { filename += "groups"; }
148 //make sure there is at least one valid file left
149 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
152 //if the user changes the output directory command factory will send this info to us in the output parameter
153 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
155 output = validParameter.validFile(parameters, "output", false);
156 if (output == "not found") { output = ""; }
157 else{ filename = output; }
159 groups = validParameter.validFile(parameters, "groups", false);
160 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
161 else { m->splitAtDash(groups, groupsNames); }
163 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 cout << "here" << endl;
167 catch(exception& e) {
168 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
173 //**********************************************************************************************************************
175 MakeGroupCommand::~MakeGroupCommand(){ }
177 //**********************************************************************************************************************
179 void MakeGroupCommand::help(){
181 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
182 m->mothurOut("The make.group command parameters are fasta, groups and output. Fasta and group are required.\n");
183 m->mothurOut("The output parameter allows you to specify the name of groupfile created. \n");
184 m->mothurOut("The make.group command should be in the following format: \n");
185 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups). \n");
186 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
187 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
189 catch(exception& e) {
190 m->errorOut(e, "MakeGroupCommand", "help");
196 //**********************************************************************************************************************
198 int MakeGroupCommand::execute(){
200 if (abort == true) { if (calledHelp) { return 0; } return 2; }
202 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
204 filename = outputDir + filename;
207 m->openOutputFile(filename, out);
209 for (int i = 0; i < fastaFileNames.size(); i++) {
211 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); return 0; }
214 m->openInputFile(fastaFileNames[i], in);
218 Sequence seq(in, "no align"); m->gobble(in);
220 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(filename.c_str()); return 0; }
222 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
229 m->mothurOutEndLine();
230 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename);
231 m->mothurOutEndLine();
235 catch(exception& e) {
236 m->errorOut(e, "MakeGroupCommand", "execute");
240 //**********************************************************************************************************************