5 * Created by Sarah Westcott on 1/21/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "deconvolutecommand.h"
12 //**********************************************************************************************************************
13 vector<string> DeconvoluteCommand::setParameters(){
15 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
16 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
17 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
18 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20 vector<string> myArray;
21 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
25 m->errorOut(e, "DeconvoluteCommand", "setParameters");
29 //**********************************************************************************************************************
30 string DeconvoluteCommand::getHelpString(){
32 string helpString = "";
33 helpString += "The unique.seqs command reads a fastafile and creates a namesfile.\n";
34 helpString += "It creates a file where the first column is the groupname and the second column is a list of sequence names who have the same sequence. \n";
35 helpString += "If the sequence is unique the second column will just contain its name. \n";
36 helpString += "The unique.seqs command parameters are fasta and name. fasta is required, unless there is a valid current fasta file.\n";
37 helpString += "The unique.seqs command should be in the following format: \n";
38 helpString += "unique.seqs(fasta=yourFastaFile) \n";
42 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
46 //**********************************************************************************************************************
47 DeconvoluteCommand::DeconvoluteCommand(){
49 abort = true; calledHelp = true;
51 vector<string> tempOutNames;
52 outputTypes["fasta"] = tempOutNames;
53 outputTypes["name"] = tempOutNames;
56 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
60 /**************************************************************************************/
61 DeconvoluteCommand::DeconvoluteCommand(string option) {
63 abort = false; calledHelp = false;
65 //allow user to run help
66 if(option == "help") { help(); abort = true; calledHelp = true; }
67 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
70 vector<string> myArray = setParameters();
72 OptionParser parser(option);
73 map<string,string> parameters = parser.getParameters();
75 ValidParameters validParameter;
76 map<string, string>::iterator it;
78 //check to make sure all parameters are valid for command
79 for (it = parameters.begin(); it != parameters.end(); it++) {
80 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
83 //initialize outputTypes
84 vector<string> tempOutNames;
85 outputTypes["fasta"] = tempOutNames;
86 outputTypes["name"] = tempOutNames;
88 //if the user changes the input directory command factory will send this info to us in the output parameter
89 string inputDir = validParameter.validFile(parameters, "inputdir", false);
90 if (inputDir == "not found"){ inputDir = ""; }
93 it = parameters.find("fasta");
94 //user has given a template file
95 if(it != parameters.end()){
96 path = m->hasPath(it->second);
97 //if the user has not given a path then, add inputdir. else leave path alone.
98 if (path == "") { parameters["fasta"] = inputDir + it->second; }
101 it = parameters.find("name");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["name"] = inputDir + it->second; }
111 //check for required parameters
112 inFastaName = validParameter.validFile(parameters, "fasta", true);
113 if (inFastaName == "not open") { abort = true; }
114 else if (inFastaName == "not found") {
115 inFastaName = m->getFastaFile();
116 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
117 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
120 //if the user changes the output directory command factory will send this info to us in the output parameter
121 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
123 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
126 oldNameMapFName = validParameter.validFile(parameters, "name", true);
127 if (oldNameMapFName == "not open") { abort = true; }
128 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
132 catch(exception& e) {
133 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
137 /**************************************************************************************/
138 int DeconvoluteCommand::execute() {
141 if (abort == true) { if (calledHelp) { return 0; } return 2; }
143 //prepare filenames and open files
144 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
145 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
149 if(oldNameMapFName == "") { fastamap.readFastaFile(inFastaName); }
150 else { fastamap.readFastaFile(inFastaName, oldNameMapFName); }
152 if (m->control_pressed) { return 0; }
154 fastamap.printCondensedFasta(outFastaFile);
155 fastamap.printNamesFile(outNameFile);
157 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
159 m->mothurOutEndLine();
160 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
161 m->mothurOut(outFastaFile); m->mothurOutEndLine();
162 m->mothurOut(outNameFile); m->mothurOutEndLine();
163 outputNames.push_back(outFastaFile); outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile); outputTypes["name"].push_back(outNameFile);
164 m->mothurOutEndLine();
166 //set fasta file as new current fastafile
168 itTypes = outputTypes.find("fasta");
169 if (itTypes != outputTypes.end()) {
170 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
173 itTypes = outputTypes.find("name");
174 if (itTypes != outputTypes.end()) {
175 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
180 catch(exception& e) {
181 m->errorOut(e, "DeconvoluteCommand", "execute");
185 /**************************************************************************************/