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])); }
83 //make sure there is at least one valid file left
84 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
87 //if the user changes the output directory command factory will send this info to us in the output parameter
88 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
90 groups = validParameter.validFile(parameters, "groups", false);
91 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
92 else { m->splitAtDash(groups, groupsNames); }
94 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; }
99 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
104 //**********************************************************************************************************************
106 MakeGroupCommand::~MakeGroupCommand(){ }
108 //**********************************************************************************************************************
110 void MakeGroupCommand::help(){
112 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
113 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
114 m->mothurOut("The make.group command should be in the following format: \n");
115 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
116 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
117 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
119 catch(exception& e) {
120 m->errorOut(e, "MakeGroupCommand", "help");
126 //**********************************************************************************************************************
128 int MakeGroupCommand::execute(){
130 if (abort == true) { return 0; }
132 if (outputDir == "") { outputDir = m->hasPath(fastaFileNames[0]); }
134 filename = outputDir + filename;
137 m->openOutputFile(filename, out);
139 for (int i = 0; i < fastaFileNames.size(); i++) {
141 if (m->control_pressed) { out.close(); remove(filename.c_str()); return 0; }
144 m->openInputFile(fastaFileNames[i], in);
148 Sequence seq(in, "no align"); m->gobble(in);
150 if (m->control_pressed) { in.close(); out.close(); remove(filename.c_str()); return 0; }
152 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
159 m->mothurOutEndLine();
160 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine();
161 m->mothurOutEndLine();
165 catch(exception& e) {
166 m->errorOut(e, "MakeGroupCommand", "execute");
170 //**********************************************************************************************************************