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", "", "", "namecount", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none",false,false); 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::getOutputFileNameTag(string type, string inputName=""){
51 string outputFileName = "";
52 map<string, vector<string> >::iterator it;
54 //is this a type this command creates
55 it = outputTypes.find(type);
56 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
58 if (type == "fasta") { outputFileName = "unique" + m->getExtension(inputName); }
59 else if (type == "name") { outputFileName = "names"; }
60 else if (type == "count") { outputFileName = "count_table"; }
61 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
63 return outputFileName;
66 m->errorOut(e, "DeconvoluteCommand", "getOutputFileNameTag");
70 //**********************************************************************************************************************
71 DeconvoluteCommand::DeconvoluteCommand(){
73 abort = true; calledHelp = true;
75 vector<string> tempOutNames;
76 outputTypes["fasta"] = tempOutNames;
77 outputTypes["name"] = tempOutNames;
78 outputTypes["count"] = tempOutNames;
81 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
85 /**************************************************************************************/
86 DeconvoluteCommand::DeconvoluteCommand(string option) {
88 abort = false; calledHelp = false;
90 //allow user to run help
91 if(option == "help") { help(); abort = true; calledHelp = true; }
92 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
95 vector<string> myArray = setParameters();
97 OptionParser parser(option);
98 map<string,string> parameters = parser.getParameters();
100 ValidParameters validParameter;
101 map<string, string>::iterator it;
103 //check to make sure all parameters are valid for command
104 for (it = parameters.begin(); it != parameters.end(); it++) {
105 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
108 //initialize outputTypes
109 vector<string> tempOutNames;
110 outputTypes["fasta"] = tempOutNames;
111 outputTypes["name"] = tempOutNames;
112 outputTypes["count"] = tempOutNames;
114 //if the user changes the input directory command factory will send this info to us in the output parameter
115 string inputDir = validParameter.validFile(parameters, "inputdir", false);
116 if (inputDir == "not found"){ inputDir = ""; }
119 it = parameters.find("fasta");
120 //user has given a template file
121 if(it != parameters.end()){
122 path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { parameters["fasta"] = inputDir + it->second; }
127 it = parameters.find("name");
128 //user has given a template file
129 if(it != parameters.end()){
130 path = m->hasPath(it->second);
131 //if the user has not given a path then, add inputdir. else leave path alone.
132 if (path == "") { parameters["name"] = inputDir + it->second; }
135 it = parameters.find("count");
136 //user has given a template file
137 if(it != parameters.end()){
138 path = m->hasPath(it->second);
139 //if the user has not given a path then, add inputdir. else leave path alone.
140 if (path == "") { parameters["count"] = inputDir + it->second; }
145 //check for required parameters
146 inFastaName = validParameter.validFile(parameters, "fasta", true);
147 if (inFastaName == "not open") { abort = true; }
148 else if (inFastaName == "not found") {
149 inFastaName = m->getFastaFile();
150 if (inFastaName != "") { m->mothurOut("Using " + inFastaName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
151 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
152 }else { m->setFastaFile(inFastaName); }
154 //if the user changes the output directory command factory will send this info to us in the output parameter
155 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
157 outputDir += m->hasPath(inFastaName); //if user entered a file with a path then preserve it
160 oldNameMapFName = validParameter.validFile(parameters, "name", true);
161 if (oldNameMapFName == "not open") { oldNameMapFName = ""; abort = true; }
162 else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; }
163 else { m->setNameFile(oldNameMapFName); }
165 countfile = validParameter.validFile(parameters, "count", true);
166 if (countfile == "not open") { abort = true; countfile = ""; }
167 else if (countfile == "not found") { countfile = ""; }
168 else { m->setCountTableFile(countfile); }
170 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; }
173 if (countfile == "") {
174 if (oldNameMapFName == "") {
175 vector<string> files; files.push_back(inFastaName);
176 parser.getNameFile(files);
183 catch(exception& e) {
184 m->errorOut(e, "DeconvoluteCommand", "DeconvoluteCommand");
188 /**************************************************************************************/
189 int DeconvoluteCommand::execute() {
192 if (abort == true) { if (calledHelp) { return 0; } return 2; }
194 //prepare filenames and open files
195 string outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + getOutputFileNameTag("name");
196 string outCountFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + getOutputFileNameTag("count");
197 string outFastaFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + getOutputFileNameTag("fasta", inFastaName);
199 map<string, string> nameMap;
200 map<string, string>::iterator itNames;
201 if (oldNameMapFName != "") {
202 m->readNames(oldNameMapFName, nameMap);
203 if (oldNameMapFName == outNameFile){ outNameFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique." + getOutputFileNameTag("name"); }
206 if (countfile != "") {
207 ct.readTable(countfile);
208 if (countfile == outCountFile){ outCountFile = outputDir + m->getRootName(m->getSimpleName(inFastaName)) + "unique." + getOutputFileNameTag("count"); }
211 if (m->control_pressed) { return 0; }
214 m->openInputFile(inFastaName, in);
217 m->openOutputFile(outFastaFile, outFasta);
219 map<string, string> sequenceStrings; //sequenceString -> list of names. "atgc...." -> seq1,seq2,seq3.
220 map<string, string>::iterator itStrings;
221 set<string> nameInFastaFile; //for sanity checking
222 set<string>::iterator itname;
223 vector<string> nameFileOrder;
227 if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
231 if (seq.getName() != "") {
234 itname = nameInFastaFile.find(seq.getName());
235 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName()); }
236 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(); }
238 itStrings = sequenceStrings.find(seq.getAligned());
240 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
241 //output to unique fasta file
242 seq.printSequence(outFasta);
244 if (oldNameMapFName != "") {
245 itNames = nameMap.find(seq.getName());
247 if (itNames == nameMap.end()) { //namefile and fastafile do not match
248 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
250 sequenceStrings[seq.getAligned()] = itNames->second;
251 nameFileOrder.push_back(seq.getAligned());
253 }else if (countfile != "") {
254 ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
255 sequenceStrings[seq.getAligned()] = seq.getName(); nameFileOrder.push_back(seq.getAligned());
256 }else { sequenceStrings[seq.getAligned()] = seq.getName(); nameFileOrder.push_back(seq.getAligned()); }
257 }else { //this is a dup
258 if (oldNameMapFName != "") {
259 itNames = nameMap.find(seq.getName());
261 if (itNames == nameMap.end()) { //namefile and fastafile do not match
262 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
264 sequenceStrings[seq.getAligned()] += "," + itNames->second;
266 }else if (countfile != "") {
267 int num = ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
268 if (num != 0) { //its in the table
269 ct.mergeCounts(itStrings->second, seq.getName()); //merges counts and saves in uniques name
271 }else { sequenceStrings[seq.getAligned()] += "," + seq.getName(); }
279 if(count % 1000 == 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
282 if(count % 1000 != 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
287 if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
289 //print new names file
291 if (countfile == "") { m->openOutputFile(outNameFile, outNames); outputNames.push_back(outNameFile); outputTypes["name"].push_back(outNameFile); }
292 else { m->openOutputFile(outCountFile, outNames); ct.printHeaders(outNames); outputTypes["count"].push_back(outCountFile); outputNames.push_back(outCountFile); }
294 for (int i = 0; i < nameFileOrder.size(); i++) {
295 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; }
297 itStrings = sequenceStrings.find(nameFileOrder[i]);
299 if (itStrings != sequenceStrings.end()) {
300 if (countfile == "") {
302 int pos = (itStrings->second).find_first_of(',');
304 if (pos == string::npos) { // only reps itself
305 outNames << itStrings->second << '\t' << itStrings->second << endl;
307 outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
309 }else { ct.printSeq(outNames, itStrings->second); }
310 }else{ m->mothurOut("[ERROR]: mismatch in namefile print."); m->mothurOutEndLine(); m->control_pressed = true; }
314 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
316 m->mothurOutEndLine();
317 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
318 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
319 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
320 m->mothurOutEndLine();
322 //set fasta file as new current fastafile
324 itTypes = outputTypes.find("fasta");
325 if (itTypes != outputTypes.end()) {
326 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
329 itTypes = outputTypes.find("name");
330 if (itTypes != outputTypes.end()) {
331 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
334 itTypes = outputTypes.find("count");
335 if (itTypes != outputTypes.end()) {
336 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
341 catch(exception& e) {
342 m->errorOut(e, "DeconvoluteCommand", "execute");
346 /**************************************************************************************/