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();
118 ableToOpen = m->openInputFile(tryPath, in, "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();
128 ableToOpen = m->openInputFile(tryPath, in, "noerror");
129 fastaFileNames[i] = tryPath;
134 if (ableToOpen == 1) {
135 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
136 //erase from file list
137 fastaFileNames.erase(fastaFileNames.begin()+i);
139 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); }
142 //prevent giantic file name
143 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
144 else { filename += "groups"; }
146 //make sure there is at least one valid file left
147 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
150 //if the user changes the output directory command factory will send this info to us in the output parameter
151 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
153 groups = validParameter.validFile(parameters, "groups", false);
154 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
155 else { m->splitAtDash(groups, groupsNames); }
157 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; }
161 catch(exception& e) {
162 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
167 //**********************************************************************************************************************
169 MakeGroupCommand::~MakeGroupCommand(){ }
171 //**********************************************************************************************************************
173 void MakeGroupCommand::help(){
175 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
176 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
177 m->mothurOut("The make.group command should be in the following format: \n");
178 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
179 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
180 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
182 catch(exception& e) {
183 m->errorOut(e, "MakeGroupCommand", "help");
189 //**********************************************************************************************************************
191 int MakeGroupCommand::execute(){
193 if (abort == true) { return 0; }
195 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
197 filename = outputDir + filename;
200 m->openOutputFile(filename, out);
202 for (int i = 0; i < fastaFileNames.size(); i++) {
204 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); return 0; }
207 m->openInputFile(fastaFileNames[i], in);
211 Sequence seq(in, "no align"); m->gobble(in);
213 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); remove(filename.c_str()); return 0; }
215 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
222 m->mothurOutEndLine();
223 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["group"].push_back(filename);
224 m->mothurOutEndLine();
228 catch(exception& e) {
229 m->errorOut(e, "MakeGroupCommand", "execute");
233 //**********************************************************************************************************************