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; }
69 vector<string> myArray = setParameters();
71 OptionParser parser(option);
72 map<string,string> parameters = parser.getParameters();
74 ValidParameters validParameter;
75 map<string, string>::iterator it;
77 //check to make sure all parameters are valid for command
78 for (it = parameters.begin(); it != parameters.end(); it++) {
79 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
82 //initialize outputTypes
83 vector<string> tempOutNames;
84 outputTypes["fasta"] = tempOutNames;
85 outputTypes["name"] = tempOutNames;
87 //if the user changes the input directory command factory will send this info to us in the output parameter
88 string inputDir = validParameter.validFile(parameters, "inputdir", false);
89 if (inputDir == "not found"){ inputDir = ""; }
92 it = parameters.find("fasta");
93 //user has given a template file
94 if(it != parameters.end()){
95 path = m->hasPath(it->second);
96 //if the user has not given a path then, add inputdir. else leave path alone.
97 if (path == "") { parameters["fasta"] = inputDir + it->second; }
100 it = parameters.find("name");
101 //user has given a template file
102 if(it != parameters.end()){
103 path = m->hasPath(it->second);
104 //if the user has not given a path then, add inputdir. else leave path alone.
105 if (path == "") { parameters["name"] = inputDir + it->second; }
110 //check for required parameters
111 inFastaName = validParameter.validFile(parameters, "fasta", true);
112 if (inFastaName == "not open") { abort = true; }
113 else if (inFastaName == "not found") {
114 inFastaName = m->getFastaFile();
115 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
116 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
119 //if the user changes the output directory command factory will send this info to us in the output parameter
120 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
122 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
125 oldNameMapFName = validParameter.validFile(parameters, "name", true);
126 if (oldNameMapFName == "not open") { abort = true; }
127 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
131 catch(exception& e) {
132 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
136 /**************************************************************************************/
137 int DeconvoluteCommand::execute() {
140 if (abort == true) { if (calledHelp) { return 0; } return 2; }
142 //prepare filenames and open files
143 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
144 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
148 if(oldNameMapFName == "") { fastamap.readFastaFile(inFastaName); }
149 else { fastamap.readFastaFile(inFastaName, oldNameMapFName); }
151 if (m->control_pressed) { return 0; }
153 fastamap.printCondensedFasta(outFastaFile);
154 fastamap.printNamesFile(outNameFile);
156 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
158 m->mothurOutEndLine();
159 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
160 m->mothurOut(outFastaFile); m->mothurOutEndLine();
161 m->mothurOut(outNameFile); m->mothurOutEndLine();
162 outputNames.push_back(outFastaFile); outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile); outputTypes["name"].push_back(outNameFile);
163 m->mothurOutEndLine();
165 //set fasta file as new current fastafile
167 itTypes = outputTypes.find("fasta");
168 if (itTypes != outputTypes.end()) {
169 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
172 itTypes = outputTypes.find("name");
173 if (itTypes != outputTypes.end()) {
174 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
179 catch(exception& e) {
180 m->errorOut(e, "DeconvoluteCommand", "execute");
184 /**************************************************************************************/