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::getValidParameters(){
15 string Array[] = {"fasta", "name","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "DeconvoluteCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 DeconvoluteCommand::DeconvoluteCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["fasta"] = tempOutNames;
31 outputTypes["name"] = tempOutNames;
34 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
38 //**********************************************************************************************************************
39 vector<string> DeconvoluteCommand::getRequiredParameters(){
41 string Array[] = {"fasta"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "DeconvoluteCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> DeconvoluteCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "DeconvoluteCommand", "getRequiredFiles");
61 /**************************************************************************************/
62 DeconvoluteCommand::DeconvoluteCommand(string option) {
66 //allow user to run help
67 if(option == "help") { help(); abort = true; }
70 //valid paramters for this command
71 string Array[] = {"fasta", "name","outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string, string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
88 outputTypes["name"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("fasta");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["fasta"] = inputDir + it->second; }
103 it = parameters.find("name");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["name"] = inputDir + it->second; }
113 //check for required parameters
114 inFastaName = validParameter.validFile(parameters, "fasta", true);
115 if (inFastaName == "not open") { abort = true; }
116 else if (inFastaName == "not found") { inFastaName = ""; m->mothurOut("fasta is a required parameter for the unique.seqs command."); m->mothurOutEndLine(); abort = true; }
118 //if the user changes the output directory command factory will send this info to us in the output parameter
119 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
121 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
124 oldNameMapFName = validParameter.validFile(parameters, "name", true);
125 if (oldNameMapFName == "not open") { abort = true; }
126 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
130 catch(exception& e) {
131 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
135 //**********************************************************************************************************************
137 void DeconvoluteCommand::help(){
139 m->mothurOut("The unique.seqs command reads a fastafile and creates a namesfile.\n");
140 m->mothurOut("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");
141 m->mothurOut("If the sequence is unique the second column will just contain its name. \n");
142 m->mothurOut("The unique.seqs command parameter is fasta and it is required.\n");
143 m->mothurOut("The unique.seqs command should be in the following format: \n");
144 m->mothurOut("unique.seqs(fasta=yourFastaFile) \n");
146 catch(exception& e) {
147 m->errorOut(e, "DeconvoluteCommand", "help");
152 /**************************************************************************************/
153 int DeconvoluteCommand::execute() {
156 if (abort == true) { return 0; }
158 //prepare filenames and open files
159 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
160 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
164 if(oldNameMapFName == "") { fastamap.readFastaFile(inFastaName); }
165 else { fastamap.readFastaFile(inFastaName, oldNameMapFName); }
167 if (m->control_pressed) { return 0; }
169 fastamap.printCondensedFasta(outFastaFile);
170 fastamap.printNamesFile(outNameFile);
172 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
174 m->mothurOutEndLine();
175 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
176 m->mothurOut(outFastaFile); m->mothurOutEndLine();
177 m->mothurOut(outNameFile); m->mothurOutEndLine();
178 outputNames.push_back(outFastaFile); outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile); outputTypes["name"].push_back(outNameFile);
179 m->mothurOutEndLine();
185 catch(exception& e) {
186 m->errorOut(e, "DeconvoluteCommand", "execute");
190 /**************************************************************************************/