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 DeconvoluteCommand::DeconvoluteCommand(string option) {
17 //allow user to run help
18 if(option == "help") { help(); abort = true; }
21 //valid paramters for this command
22 string Array[] = {"fasta", "name","outputdir","inputdir"};
23 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25 OptionParser parser(option);
26 map<string,string> parameters = parser.getParameters();
28 ValidParameters validParameter;
29 map<string, string>::iterator it;
31 //check to make sure all parameters are valid for command
32 for (it = parameters.begin(); it != parameters.end(); it++) {
33 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
36 //if the user changes the input directory command factory will send this info to us in the output parameter
37 string inputDir = validParameter.validFile(parameters, "inputdir", false);
38 if (inputDir == "not found"){ inputDir = ""; }
41 it = parameters.find("fasta");
42 //user has given a template file
43 if(it != parameters.end()){
44 path = hasPath(it->second);
45 //if the user has not given a path then, add inputdir. else leave path alone.
46 if (path == "") { parameters["fasta"] = inputDir + it->second; }
49 it = parameters.find("name");
50 //user has given a template file
51 if(it != parameters.end()){
52 path = hasPath(it->second);
53 //if the user has not given a path then, add inputdir. else leave path alone.
54 if (path == "") { parameters["name"] = inputDir + it->second; }
59 //check for required parameters
60 inFastaName = validParameter.validFile(parameters, "fasta", true);
61 if (inFastaName == "not open") { abort = true; }
62 else if (inFastaName == "not found") { inFastaName = ""; m->mothurOut("fasta is a required parameter for the unique.seqs command."); m->mothurOutEndLine(); abort = true; }
64 //if the user changes the output directory command factory will send this info to us in the output parameter
65 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
67 outputDir += hasPath(inFastaName); //if user entered a file with a path then preserve it
70 oldNameMapFName = validParameter.validFile(parameters, "name", true);
71 if (oldNameMapFName == "not open") { abort = true; }
72 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
77 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
81 //**********************************************************************************************************************
83 void DeconvoluteCommand::help(){
85 m->mothurOut("The unique.seqs command reads a fastafile and creates a namesfile.\n");
86 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");
87 m->mothurOut("If the sequence is unique the second column will just contain its name. \n");
88 m->mothurOut("The unique.seqs command parameter is fasta and it is required.\n");
89 m->mothurOut("The unique.seqs command should be in the following format: \n");
90 m->mothurOut("unique.seqs(fasta=yourFastaFile) \n");
93 m->errorOut(e, "DeconvoluteCommand", "help");
98 /**************************************************************************************/
99 int DeconvoluteCommand::execute() {
102 if (abort == true) { return 0; }
104 //prepare filenames and open files
105 string outNameFile = outputDir + getRootName(getSimpleName(inFastaName)) + "names";
106 string outFastaFile = outputDir + getRootName(getSimpleName(inFastaName)) + "unique" + getExtension(inFastaName);
110 if(oldNameMapFName == "") { fastamap.readFastaFile(inFastaName); }
111 else { fastamap.readFastaFile(inFastaName, oldNameMapFName); }
113 if (m->control_pressed) { return 0; }
115 fastamap.printCondensedFasta(outFastaFile);
116 fastamap.printNamesFile(outNameFile);
118 if (m->control_pressed) { remove(outFastaFile.c_str()); remove(outNameFile.c_str()); return 0; }
120 m->mothurOutEndLine();
121 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
122 m->mothurOut(outFastaFile); m->mothurOutEndLine();
123 m->mothurOut(outNameFile); m->mothurOutEndLine();
124 m->mothurOutEndLine();
130 catch(exception& e) {
131 m->errorOut(e, "DeconvoluteCommand", "execute");
135 /**************************************************************************************/