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(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["fasta"] = tempOutNames;
30 outputTypes["name"] = tempOutNames;
33 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
37 //**********************************************************************************************************************
38 vector<string> DeconvoluteCommand::getRequiredParameters(){
40 string Array[] = {"fasta"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "DeconvoluteCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> DeconvoluteCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "DeconvoluteCommand", "getRequiredFiles");
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 //valid paramters for this command
70 string Array[] = {"fasta", "name","outputdir","inputdir"};
71 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73 OptionParser parser(option);
74 map<string,string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 map<string, string>::iterator it;
79 //check to make sure all parameters are valid for command
80 for (it = parameters.begin(); it != parameters.end(); it++) {
81 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["fasta"] = tempOutNames;
87 outputTypes["name"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("fasta");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["fasta"] = inputDir + it->second; }
102 it = parameters.find("name");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["name"] = inputDir + it->second; }
112 //check for required parameters
113 inFastaName = validParameter.validFile(parameters, "fasta", true);
114 if (inFastaName == "not open") { abort = true; }
115 else if (inFastaName == "not found") { inFastaName = ""; m->mothurOut("fasta is a required parameter for the unique.seqs command."); m->mothurOutEndLine(); abort = true; }
117 //if the user changes the output directory command factory will send this info to us in the output parameter
118 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
120 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
123 oldNameMapFName = validParameter.validFile(parameters, "name", true);
124 if (oldNameMapFName == "not open") { abort = true; }
125 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
129 catch(exception& e) {
130 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
134 //**********************************************************************************************************************
136 void DeconvoluteCommand::help(){
138 m->mothurOut("The unique.seqs command reads a fastafile and creates a namesfile.\n");
139 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");
140 m->mothurOut("If the sequence is unique the second column will just contain its name. \n");
141 m->mothurOut("The unique.seqs command parameter is fasta and it is required.\n");
142 m->mothurOut("The unique.seqs command should be in the following format: \n");
143 m->mothurOut("unique.seqs(fasta=yourFastaFile) \n");
145 catch(exception& e) {
146 m->errorOut(e, "DeconvoluteCommand", "help");
151 /**************************************************************************************/
152 int DeconvoluteCommand::execute() {
155 if (abort == true) { if (calledHelp) { return 0; } return 2; }
157 //prepare filenames and open files
158 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
159 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
163 if(oldNameMapFName == "") { fastamap.readFastaFile(inFastaName); }
164 else { fastamap.readFastaFile(inFastaName, oldNameMapFName); }
166 if (m->control_pressed) { return 0; }
168 fastamap.printCondensedFasta(outFastaFile);
169 fastamap.printNamesFile(outNameFile);
171 if (m->control_pressed) { outputTypes.clear(); remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
173 m->mothurOutEndLine();
174 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
175 m->mothurOut(outFastaFile); m->mothurOutEndLine();
176 m->mothurOut(outNameFile); m->mothurOutEndLine();
177 outputNames.push_back(outFastaFile); outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile); outputTypes["name"].push_back(outNameFile);
178 m->mothurOutEndLine();
184 catch(exception& e) {
185 m->errorOut(e, "DeconvoluteCommand", "execute");
189 /**************************************************************************************/