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 //**********************************************************************************************************************
15 MakeGroupCommand::MakeGroupCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string AlignArray[] = {"fasta","groups","outputdir","inputdir"};
27 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
29 OptionParser parser(option);
30 map<string, string> parameters = parser.getParameters();
32 ValidParameters validParameter;
33 map<string, string>::iterator it;
35 //check to make sure all parameters are valid for command
36 for (it = parameters.begin(); it != parameters.end(); it++) {
37 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
40 //if the user changes the output directory command factory will send this info to us in the output parameter
41 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
44 //if the user changes the input directory command factory will send this info to us in the output parameter
45 string inputDir = validParameter.validFile(parameters, "inputdir", false);
46 if (inputDir == "not found"){ inputDir = ""; }
50 fastaFileName = validParameter.validFile(parameters, "fasta", false);
51 if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
53 splitAtDash(fastaFileName, fastaFileNames);
55 //go through files and make sure they are good, if not, then disregard them
56 for (int i = 0; i < fastaFileNames.size(); i++) {
58 string path = hasPath(fastaFileNames[i]);
59 //if the user has not given a path then, add inputdir. else leave path alone.
60 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
66 ableToOpen = openInputFile(fastaFileNames[i], in);
69 if (ableToOpen == 1) {
70 //erase from file list
71 fastaFileNames.erase(fastaFileNames.begin()+i);
73 }else{ filename += getRootName(getSimpleName(fastaFileNames[i])); }
79 //make sure there is at least one valid file left
80 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
83 groups = validParameter.validFile(parameters, "groups", false);
84 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
85 else { splitAtDash(groups, groupsNames); }
87 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; }
92 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
97 //**********************************************************************************************************************
99 MakeGroupCommand::~MakeGroupCommand(){ }
101 //**********************************************************************************************************************
103 void MakeGroupCommand::help(){
105 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
106 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
107 m->mothurOut("The make.group command should be in the following format: \n");
108 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
109 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
110 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
112 catch(exception& e) {
113 m->errorOut(e, "MakeGroupCommand", "help");
119 //**********************************************************************************************************************
121 int MakeGroupCommand::execute(){
123 if (abort == true) { return 0; }
126 openOutputFile(filename, out);
128 for (int i = 0; i < fastaFileNames.size(); i++) {
130 if (m->control_pressed) { out.close(); remove(filename.c_str()); return 0; }
133 openInputFile(fastaFileNames[i], in);
137 Sequence seq(in, "no align"); gobble(in);
139 if (m->control_pressed) { in.close(); out.close(); remove(filename.c_str()); return 0; }
141 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
148 m->mothurOutEndLine();
149 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine();
150 m->mothurOutEndLine();
154 catch(exception& e) {
155 m->errorOut(e, "MakeGroupCommand", "execute");
159 //**********************************************************************************************************************