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 input directory command factory will send this info to us in the output parameter
41 string inputDir = validParameter.validFile(parameters, "inputdir", false);
42 if (inputDir == "not found"){ inputDir = ""; }
46 fastaFileName = validParameter.validFile(parameters, "fasta", false);
47 if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
49 m->splitAtDash(fastaFileName, fastaFileNames);
51 //go through files and make sure they are good, if not, then disregard them
52 for (int i = 0; i < fastaFileNames.size(); i++) {
54 string path = m->hasPath(fastaFileNames[i]);
55 //if the user has not given a path then, add inputdir. else leave path alone.
56 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
60 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
62 //if you can't open it, try default location
63 if (ableToOpen == 1) {
64 if (m->getDefaultPath() != "") { //default path is set
65 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
66 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
67 ableToOpen = m->openInputFile(tryPath, in, "noerror");
68 fastaFileNames[i] = tryPath;
73 if (ableToOpen == 1) {
74 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
75 //erase from file list
76 fastaFileNames.erase(fastaFileNames.begin()+i);
78 }else{ filename += m->getRootName(m->getSimpleName(fastaFileNames[i])); }
81 //prevent giantic file name
82 if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
83 else { filename += "groups"; }
85 //make sure there is at least one valid file left
86 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
89 //if the user changes the output directory command factory will send this info to us in the output parameter
90 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
92 groups = validParameter.validFile(parameters, "groups", false);
93 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
94 else { m->splitAtDash(groups, groupsNames); }
96 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; }
100 catch(exception& e) {
101 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
106 //**********************************************************************************************************************
108 MakeGroupCommand::~MakeGroupCommand(){ }
110 //**********************************************************************************************************************
112 void MakeGroupCommand::help(){
114 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
115 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
116 m->mothurOut("The make.group command should be in the following format: \n");
117 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
118 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
119 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
121 catch(exception& e) {
122 m->errorOut(e, "MakeGroupCommand", "help");
128 //**********************************************************************************************************************
130 int MakeGroupCommand::execute(){
132 if (abort == true) { return 0; }
134 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
136 filename = outputDir + filename;
139 m->openOutputFile(filename, out);
141 for (int i = 0; i < fastaFileNames.size(); i++) {
143 if (m->control_pressed) { out.close(); remove(filename.c_str()); return 0; }
146 m->openInputFile(fastaFileNames[i], in);
150 Sequence seq(in, "no align"); m->gobble(in);
152 if (m->control_pressed) { in.close(); out.close(); remove(filename.c_str()); return 0; }
154 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
161 m->mothurOutEndLine();
162 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine();
163 m->mothurOutEndLine();
167 catch(exception& e) {
168 m->errorOut(e, "MakeGroupCommand", "execute");
172 //**********************************************************************************************************************