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","fasta-name",false,true,true); parameters.push_back(pfasta);
17 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","name",false,false,true); parameters.push_back(pname);
18 CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","count",false,false,true); parameters.push_back(pcount);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "DeconvoluteCommand", "setParameters");
31 //**********************************************************************************************************************
32 string DeconvoluteCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The unique.seqs command reads a fastafile and creates a name or count file.\n";
36 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";
37 helpString += "If the sequence is unique the second column will just contain its name. \n";
38 helpString += "The unique.seqs command parameters are fasta and name. fasta is required, unless there is a valid current fasta file.\n";
39 helpString += "The unique.seqs command should be in the following format: \n";
40 helpString += "unique.seqs(fasta=yourFastaFile) \n";
44 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
48 //**********************************************************************************************************************
49 string DeconvoluteCommand::getOutputPattern(string type) {
53 if (type == "fasta") { pattern = "[filename],unique,[extension]"; }
54 else if (type == "name") { pattern = "[filename],names-[filename],[tag],names"; }
55 else if (type == "count") { pattern = "[filename],count_table-[filename],[tag],count_table"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
61 m->errorOut(e, "DeconvoluteCommand", "getOutputPattern");
66 //**********************************************************************************************************************
67 DeconvoluteCommand::DeconvoluteCommand(){
69 abort = true; calledHelp = true;
71 vector<string> tempOutNames;
72 outputTypes["fasta"] = tempOutNames;
73 outputTypes["name"] = tempOutNames;
74 outputTypes["count"] = tempOutNames;
77 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
81 /**************************************************************************************/
82 DeconvoluteCommand::DeconvoluteCommand(string option) {
84 abort = false; calledHelp = false;
86 //allow user to run help
87 if(option == "help") { help(); abort = true; calledHelp = true; }
88 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
91 vector<string> myArray = setParameters();
93 OptionParser parser(option);
94 map<string,string> parameters = parser.getParameters();
96 ValidParameters validParameter;
97 map<string, string>::iterator it;
99 //check to make sure all parameters are valid for command
100 for (it = parameters.begin(); it != parameters.end(); it++) {
101 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
104 //initialize outputTypes
105 vector<string> tempOutNames;
106 outputTypes["fasta"] = tempOutNames;
107 outputTypes["name"] = tempOutNames;
108 outputTypes["count"] = tempOutNames;
110 //if the user changes the input directory command factory will send this info to us in the output parameter
111 string inputDir = validParameter.validFile(parameters, "inputdir", false);
112 if (inputDir == "not found"){ inputDir = ""; }
115 it = parameters.find("fasta");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["fasta"] = inputDir + it->second; }
123 it = parameters.find("name");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["name"] = inputDir + it->second; }
131 it = parameters.find("count");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["count"] = inputDir + it->second; }
141 //check for required parameters
142 inFastaName = validParameter.validFile(parameters, "fasta", true);
143 if (inFastaName == "not open") { abort = true; }
144 else if (inFastaName == "not found") {
145 inFastaName = m->getFastaFile();
146 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
147 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
148 }else { m->setFastaFile(inFastaName); }
150 //if the user changes the output directory command factory will send this info to us in the output parameter
151 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
153 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
156 oldNameMapFName = validParameter.validFile(parameters, "name", true);
157 if (oldNameMapFName == "not open") { oldNameMapFName = ""; abort = true; }
158 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
159 else { m->setNameFile(oldNameMapFName); }
161 countfile = validParameter.validFile(parameters, "count", true);
162 if (countfile == "not open") { abort = true; countfile = ""; }
163 else if (countfile == "not found") { countfile = ""; }
164 else { m->setCountTableFile(countfile); }
166 if ((countfile != "") && (oldNameMapFName != "")) { m->mothurOut("When executing a unique.seqs command you must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
169 if (countfile == "") {
170 if (oldNameMapFName == "") {
171 vector<string> files; files.push_back(inFastaName);
172 parser.getNameFile(files);
179 catch(exception& e) {
180 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
184 /**************************************************************************************/
185 int DeconvoluteCommand::execute() {
188 if (abort == true) { if (calledHelp) { return 0; } return 2; }
190 //prepare filenames and open files
191 map<string, string> variables;
192 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
193 string outNameFile = getOutputFileName("name", variables);
194 string outCountFile = getOutputFileName("count", variables);
195 variables["[extension]"] = m->getExtension(inFastaName);
196 string outFastaFile = getOutputFileName("fasta", variables);
198 map<string, string> nameMap;
199 map<string, string>::iterator itNames;
200 if (oldNameMapFName != "") {
201 m->readNames(oldNameMapFName, nameMap);
202 if (oldNameMapFName == outNameFile){
203 //prepare filenames and open files
204 map<string, string> mvariables;
205 mvariables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
206 mvariables["[tag]"] = "unique";
207 outNameFile = getOutputFileName("name", mvariables);
211 if (countfile != "") {
212 ct.readTable(countfile, true, false);
213 if (countfile == outCountFile){
214 //prepare filenames and open files
215 map<string, string> mvariables;
216 mvariables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
217 mvariables["[tag]"] = "unique";
218 outCountFile = getOutputFileName("count", mvariables); }
221 if (m->control_pressed) { return 0; }
224 m->openInputFile(inFastaName, in);
227 m->openOutputFile(outFastaFile, outFasta);
229 map<string, string> sequenceStrings; //sequenceString -> list of names. "atgc...." -> seq1,seq2,seq3.
230 map<string, string>::iterator itStrings;
231 set<string> nameInFastaFile; //for sanity checking
232 set<string>::iterator itname;
233 vector<string> nameFileOrder;
237 if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
241 if (seq.getName() != "") {
244 itname = nameInFastaFile.find(seq.getName());
245 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName()); }
246 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(); }
248 itStrings = sequenceStrings.find(seq.getAligned());
250 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
251 //output to unique fasta file
252 seq.printSequence(outFasta);
254 if (oldNameMapFName != "") {
255 itNames = nameMap.find(seq.getName());
257 if (itNames == nameMap.end()) { //namefile and fastafile do not match
258 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
260 sequenceStrings[seq.getAligned()] = itNames->second;
261 nameFileOrder.push_back(seq.getAligned());
263 }else if (countfile != "") {
264 ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
265 sequenceStrings[seq.getAligned()] = seq.getName(); nameFileOrder.push_back(seq.getAligned());
266 }else { sequenceStrings[seq.getAligned()] = seq.getName(); nameFileOrder.push_back(seq.getAligned()); }
267 }else { //this is a dup
268 if (oldNameMapFName != "") {
269 itNames = nameMap.find(seq.getName());
271 if (itNames == nameMap.end()) { //namefile and fastafile do not match
272 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
274 sequenceStrings[seq.getAligned()] += "," + itNames->second;
276 }else if (countfile != "") {
277 int num = ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
278 if (num != 0) { //its in the table
279 ct.mergeCounts(itStrings->second, seq.getName()); //merges counts and saves in uniques name
281 }else { sequenceStrings[seq.getAligned()] += "," + seq.getName(); }
289 if(count % 1000 == 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
292 if(count % 1000 != 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
297 if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
299 //print new names file
301 if (countfile == "") { m->openOutputFile(outNameFile, outNames); outputNames.push_back(outNameFile); outputTypes["name"].push_back(outNameFile); }
302 else { m->openOutputFile(outCountFile, outNames); ct.printHeaders(outNames); outputTypes["count"].push_back(outCountFile); outputNames.push_back(outCountFile); }
304 for (int i = 0; i < nameFileOrder.size(); i++) {
305 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); outNames.close(); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
307 itStrings = sequenceStrings.find(nameFileOrder[i]);
309 if (itStrings != sequenceStrings.end()) {
310 if (countfile == "") {
312 int pos = (itStrings->second).find_first_of(',');
314 if (pos == string::npos) { // only reps itself
315 outNames << itStrings->second << '\t' << itStrings->second << endl;
317 outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
319 }else { ct.printSeq(outNames, itStrings->second); }
320 }else{ m->mothurOut("[ERROR]: mismatch in namefile print."); m->mothurOutEndLine(); m->control_pressed = true; }
324 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
326 m->mothurOutEndLine();
327 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
328 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
329 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
330 m->mothurOutEndLine();
332 //set fasta file as new current fastafile
334 itTypes = outputTypes.find("fasta");
335 if (itTypes != outputTypes.end()) {
336 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
339 itTypes = outputTypes.find("name");
340 if (itTypes != outputTypes.end()) {
341 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
344 itTypes = outputTypes.find("count");
345 if (itTypes != outputTypes.end()) {
346 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
351 catch(exception& e) {
352 m->errorOut(e, "DeconvoluteCommand", "execute");
356 /**************************************************************************************/