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"
12 #include <unordered_map>
13 #include <unordered_set>
17 //**********************************************************************************************************************
18 vector<string> DeconvoluteCommand::setParameters(){
20 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta-name",false,true,true); parameters.push_back(pfasta);
21 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","name",false,false,true); parameters.push_back(pname);
22 CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","count",false,false,true); parameters.push_back(pcount);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "DeconvoluteCommand", "setParameters");
35 //**********************************************************************************************************************
36 string DeconvoluteCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The unique.seqs command reads a fastafile and creates a name or count file.\n";
40 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";
41 helpString += "If the sequence is unique the second column will just contain its name. \n";
42 helpString += "The unique.seqs command parameters are fasta and name. fasta is required, unless there is a valid current fasta file.\n";
43 helpString += "The unique.seqs command should be in the following format: \n";
44 helpString += "unique.seqs(fasta=yourFastaFile) \n";
48 m->errorOut(e, "DeconvoluteCommand", "getHelpString");
52 //**********************************************************************************************************************
53 string DeconvoluteCommand::getOutputPattern(string type) {
57 if (type == "fasta") { pattern = "[filename],unique,[extension]"; }
58 else if (type == "name") { pattern = "[filename],names-[filename],[tag],names"; }
59 else if (type == "count") { pattern = "[filename],count_table-[filename],[tag],count_table"; }
60 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
65 m->errorOut(e, "DeconvoluteCommand", "getOutputPattern");
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 map<string, string> variables;
196 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
197 string outNameFile = getOutputFileName("name", variables);
198 string outCountFile = getOutputFileName("count", variables);
199 variables["[extension]"] = m->getExtension(inFastaName);
200 string outFastaFile = getOutputFileName("fasta", variables);
202 map<string, string> nameMap;
203 map<string, string>::iterator itNames;
204 if (oldNameMapFName != "") {
205 m->readNames(oldNameMapFName, nameMap);
206 if (oldNameMapFName == outNameFile){
207 //prepare filenames and open files
208 map<string, string> mvariables;
209 mvariables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
210 mvariables["[tag]"] = "unique";
211 outNameFile = getOutputFileName("name", mvariables);
215 if (countfile != "") {
216 ct.readTable(countfile, true, false);
217 if (countfile == outCountFile){
218 //prepare filenames and open files
219 map<string, string> mvariables;
220 mvariables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
221 mvariables["[tag]"] = "unique";
222 outCountFile = getOutputFileName("count", mvariables); }
225 if (m->control_pressed) { return 0; }
228 m->openInputFile(inFastaName, in);
231 m->openOutputFile(outFastaFile, outFasta);
233 hash<string> hash_fn;
234 multimap<size_t,pair<streampos,string>> sequenceStrings; //sequenceString -> list of names. "atgc...." -> seq1,seq2,seq3.
235 unordered_set<string> nameInFastaFile; //for sanity checking
236 unordered_set<string>::iterator itname;
240 if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
241 streampos seq_start = in.tellg();
244 if (seq.getName() != "") {
247 itname = nameInFastaFile.find(seq.getName());
248 if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName()); }
249 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(); }
251 auto itStrings = in_fasta_from_hash_map(sequenceStrings,seq,in);
252 if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
253 //output to unique fasta file
254 seq.printSequence(outFasta);
256 if (oldNameMapFName != "") {
257 itNames = nameMap.find(seq.getName());
259 if (itNames == nameMap.end()) { //namefile and fastafile do not match
260 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct.");
261 m->mothurOutEndLine();
263 sequenceStrings.emplace(hash_fn(seq.getAligned()),
264 make_pair(seq_start,seq.getName()));
267 if (countfile != "") {
268 ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
270 sequenceStrings.emplace(hash_fn(seq.getAligned()),
275 }else { //this is a dup
276 if (oldNameMapFName != "" &&
277 nameMap.find(seq.getName()) == nameMap.end()) { //namefile and fastafile do not match
278 m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct.");
279 m->mothurOutEndLine();
280 } else if (countfile != "") {
281 int num = ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
282 if (num != 0) { //its in the table
283 ct.mergeCounts(primary_seqname(itStrings->second.second), seq.getName()); //merges counts and saves in uniques name
286 itStrings->second.second += "," + seq.getName();
294 if(count % 1000 == 0) { m->mothurOutJustToScreen(toString(count) + "\t" + toString(sequenceStrings.size()) + "\n"); }
297 if(count % 1000 != 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
302 if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
304 //print new names file
306 if (countfile == "") { m->openOutputFile(outNameFile, outNames); outputNames.push_back(outNameFile); outputTypes["name"].push_back(outNameFile); }
307 else { m->openOutputFile(outCountFile, outNames); ct.printHeaders(outNames); outputTypes["count"].push_back(outCountFile); outputNames.push_back(outCountFile); }
309 for (auto it = sequenceStrings.begin(); it != sequenceStrings.end(); it++) {
310 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; }
312 if (countfile == "") {
314 outNames << primary_seqname(it->second.second) << '\t' << it->second.second << endl;
317 ct.printSeq(outNames,primary_seqname(it->second.second);
322 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
324 m->mothurOutEndLine();
325 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
326 outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
327 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
328 m->mothurOutEndLine();
330 //set fasta file as new current fastafile
332 itTypes = outputTypes.find("fasta");
333 if (itTypes != outputTypes.end()) {
334 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
337 itTypes = outputTypes.find("name");
338 if (itTypes != outputTypes.end()) {
339 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
342 itTypes = outputTypes.find("count");
343 if (itTypes != outputTypes.end()) {
344 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
349 catch(exception& e) {
350 m->errorOut(e, "DeconvoluteCommand", "execute");
354 /**************************************************************************************/
356 multimap<size_t,std::pair<std::streampos,string>>::iterator
357 in_fasta_from_hash_map(multimap<size_t,std::pair<std::streampos,string>>& seq_strings,
359 ifstream& fasta_file) {
360 streampos cur_pos = fasta_file.tellg();
361 const std::hash<std::string> hash_fn;
362 auto it_range = seq_strings.equal_range(hash_fn(seq.getAligned()));
363 // If the hash of the aligned sequence is not in the map, it can't
364 // possibly be a duplicate
365 if (it_range.first == seq_strings.end()) {
366 return seq_strings.end();
368 // If it is, it might be a hash collision. Read the file to
369 // determine if that's the case
370 for (auto it = it_range.first; it != it_range.second; it++) {
371 streampos pos = it->second.first;
372 fasta_file.seekg(pos);
374 Sequence old_seq(fasta_file);
375 if (old_seq.getAligned() == seq.getAligned()) {
376 fasta_file.seekg(cur_pos);
380 fasta_file.seekg(cur_pos);
381 return seq_strings.end();
385 string primary_seqname(string name) {
386 int pos = name.find_first_of(',');
387 if (pos == string::npos) {
390 return name.substr(0,pos);