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]; }
64 int ableToOpen = openInputFile(fastaFileNames[i], in, "noerror");
66 //if you can't open it, try default location
67 if (ableToOpen == 1) {
68 if (m->getDefaultPath() != "") { //default path is set
69 string tryPath = m->getDefaultPath() + getSimpleName(fastaFileNames[i]);
70 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
71 ableToOpen = openInputFile(tryPath, in, "noerror");
72 fastaFileNames[i] = tryPath;
77 if (ableToOpen == 1) {
78 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
79 //erase from file list
80 fastaFileNames.erase(fastaFileNames.begin()+i);
82 }else{ filename += getRootName(getSimpleName(fastaFileNames[i])); }
87 //make sure there is at least one valid file left
88 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
91 groups = validParameter.validFile(parameters, "groups", false);
92 if (groups == "not found") { m->mothurOut("groups is a required parameter for the make.group command."); m->mothurOutEndLine(); abort = true; }
93 else { splitAtDash(groups, groupsNames); }
95 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 m->errorOut(e, "MakeGroupCommand", "MakeGroupCommand");
105 //**********************************************************************************************************************
107 MakeGroupCommand::~MakeGroupCommand(){ }
109 //**********************************************************************************************************************
111 void MakeGroupCommand::help(){
113 m->mothurOut("The make.group command reads a fasta file or series of fasta files and creates a groupfile.\n");
114 m->mothurOut("The make.group command parameters are fasta and groups, both are required.\n");
115 m->mothurOut("The make.group command should be in the following format: \n");
116 m->mothurOut("make.group(fasta=yourFastaFiles, groups=yourGroups. \n");
117 m->mothurOut("Example make.group(fasta=seqs1.fasta-seq2.fasta-seqs3.fasta, groups=A-B-C)\n");
118 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFiles).\n\n");
120 catch(exception& e) {
121 m->errorOut(e, "MakeGroupCommand", "help");
127 //**********************************************************************************************************************
129 int MakeGroupCommand::execute(){
131 if (abort == true) { return 0; }
134 openOutputFile(filename, out);
136 for (int i = 0; i < fastaFileNames.size(); i++) {
138 if (m->control_pressed) { out.close(); remove(filename.c_str()); return 0; }
141 openInputFile(fastaFileNames[i], in);
145 Sequence seq(in, "no align"); gobble(in);
147 if (m->control_pressed) { in.close(); out.close(); remove(filename.c_str()); return 0; }
149 if (seq.getName() != "") { out << seq.getName() << '\t' << groupsNames[i] << endl; }
156 m->mothurOutEndLine();
157 m->mothurOut("Output File Name: " + filename); m->mothurOutEndLine();
158 m->mothurOutEndLine();
162 catch(exception& e) {
163 m->errorOut(e, "MakeGroupCommand", "execute");
167 //**********************************************************************************************************************