5 * Created by Sarah Westcott on 1/21/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "deconvolutecommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> DeconvoluteCommand::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "DeconvoluteCommand", "setParameters");
30 //**********************************************************************************************************************
31 string DeconvoluteCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The unique.seqs command reads a fastafile and creates a namesfile.\n";
35 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";
36 helpString += "If the sequence is unique the second column will just contain its name. \n";
37 helpString += "The unique.seqs command parameters are fasta and name. fasta is required, unless there is a valid current fasta file.\n";
38 helpString += "The unique.seqs command should be in the following format: \n";
39 helpString += "unique.seqs(fasta=yourFastaFile) \n";
43 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
47 //**********************************************************************************************************************
48 DeconvoluteCommand::DeconvoluteCommand(){
50 abort = true; calledHelp = true;
52 vector<string> tempOutNames;
53 outputTypes["fasta"] = tempOutNames;
54 outputTypes["name"] = tempOutNames;
57 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
61 /**************************************************************************************/
62 DeconvoluteCommand::DeconvoluteCommand(string option) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
68 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
71 vector<string> myArray = setParameters();
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") {
116 inFastaName = m->getFastaFile();
117 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
118 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
119 }else { m->setFastaFile(inFastaName); }
121 //if the user changes the output directory command factory will send this info to us in the output parameter
122 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
124 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
127 oldNameMapFName = validParameter.validFile(parameters, "name", true);
128 if (oldNameMapFName == "not open") { oldNameMapFName = ""; abort = true; }
129 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
130 else { m->setNameFile(oldNameMapFName); }
132 if (oldNameMapFName == "") {
133 vector<string> files; files.push_back(inFastaName);
134 parser.getNameFile(files);
140 catch(exception& e) {
141 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
145 /**************************************************************************************/
146 int DeconvoluteCommand::execute() {
149 if (abort == true) { if (calledHelp) { return 0; } return 2; }
151 //prepare filenames and open files
152 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "names";
153 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique" + m->getExtension(inFastaName);
155 map<string, string> nameMap;
156 map<string, string>::iterator itNames;
157 if (oldNameMapFName != "") { m->readNames(oldNameMapFName, nameMap); }
159 if (m->control_pressed) { return 0; }
162 m->openInputFile(inFastaName, in);
165 m->openOutputFile(outFastaFile, outFasta);
167 map<string, string> sequenceStrings; //sequenceString -> list of names. "atgc...." -> seq1,seq2,seq3.
168 map<string, string>::iterator itStrings;
169 set<string> nameInFastaFile; //for sanity checking
170 set<string>::iterator itname;
171 vector<string> nameFileOrder;
175 if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
179 if (seq.getName() != "") {
182 itname = nameInFastaFile.find(seq.getName());
183 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName()); }
184 else { m->mothurOut("[ERROR]: You already have a sequence named " + seq.getName() + " in your fasta file, sequence names must be unique, please correct."); m->mothurOutEndLine(); }
186 itStrings = sequenceStrings.find(seq.getAligned());
188 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
189 //output to unique fasta file
190 seq.printSequence(outFasta);
192 if (oldNameMapFName != "") {
193 itNames = nameMap.find(seq.getName());
195 if (itNames == nameMap.end()) { //namefile and fastafile do not match
196 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
198 sequenceStrings[seq.getAligned()] = itNames->second;
199 nameFileOrder.push_back(seq.getAligned());
201 }else { sequenceStrings[seq.getAligned()] = seq.getName(); nameFileOrder.push_back(seq.getAligned()); }
202 }else { //this is a dup
203 if (oldNameMapFName != "") {
204 itNames = nameMap.find(seq.getName());
206 if (itNames == nameMap.end()) { //namefile and fastafile do not match
207 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
209 sequenceStrings[seq.getAligned()] += "," + itNames->second;
211 }else { sequenceStrings[seq.getAligned()] += "," + seq.getName(); }
219 if(count % 1000 == 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
222 if(count % 1000 != 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
227 if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
229 //print new names file
231 m->openOutputFile(outNameFile, outNames);
233 for (int i = 0; i < nameFileOrder.size(); i++) {
234 //for (itStrings = sequenceStrings.begin(); itStrings != sequenceStrings.end(); itStrings++) {
235 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); outNames.close(); m->mothurRemove(outNameFile); return 0; }
237 itStrings = sequenceStrings.find(nameFileOrder[i]);
239 if (itStrings != sequenceStrings.end()) {
241 int pos = (itStrings->second).find_first_of(',');
243 if (pos == string::npos) { // only reps itself
244 outNames << itStrings->second << '\t' << itStrings->second << endl;
246 outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
248 }else{ m->mothurOut("[ERROR]: mismatch in namefile print."); m->mothurOutEndLine(); m->control_pressed = true; }
252 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); m->mothurRemove(outNameFile); return 0; }
254 m->mothurOutEndLine();
255 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
256 m->mothurOut(outFastaFile); m->mothurOutEndLine();
257 m->mothurOut(outNameFile); m->mothurOutEndLine();
258 outputNames.push_back(outFastaFile); outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile); outputTypes["name"].push_back(outNameFile);
259 m->mothurOutEndLine();
261 //set fasta file as new current fastafile
263 itTypes = outputTypes.find("fasta");
264 if (itTypes != outputTypes.end()) {
265 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
268 itTypes = outputTypes.find("name");
269 if (itTypes != outputTypes.end()) {
270 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
275 catch(exception& e) {
276 m->errorOut(e, "DeconvoluteCommand", "execute");
280 /**************************************************************************************/