X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=clusterfragmentscommand.cpp;h=ef199ccdf0a11efba99eaf0299ff50413b70b89f;hp=0c4dec353a97d3e02bccd69f5af5126e051da617;hb=1a20e24ee786195ab0e1cccd4f5aede7a88f3f4e;hpb=61798fe609675abfedf511e542cc48c56a531199 diff --git a/clusterfragmentscommand.cpp b/clusterfragmentscommand.cpp index 0c4dec3..ef199cc 100644 --- a/clusterfragmentscommand.cpp +++ b/clusterfragmentscommand.cpp @@ -8,6 +8,7 @@ */ #include "clusterfragmentscommand.h" +#include "needlemanoverlap.hpp" //********************************************************************************************************************** //sort by unaligned @@ -25,17 +26,90 @@ inline bool comparePriority(seqRNode first, seqRNode second) { return better; } //********************************************************************************************************************** +vector ClusterFragmentsCommand::setParameters(){ + try { + CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta-name",false,true,true); parameters.push_back(pfasta); + CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","name",false,false,true); parameters.push_back(pname); + CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","count",false,false,true); parameters.push_back(pcount); + CommandParameter pdiffs("diffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pdiffs); + CommandParameter ppercent("percent", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ppercent); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } + return myArray; + } + catch(exception& e) { + m->errorOut(e, "ClusterFragmentsCommand", "setParameters"); + exit(1); + } +} +//********************************************************************************************************************** +string ClusterFragmentsCommand::getHelpString(){ + try { + string helpString = ""; + helpString += "The cluster.fragments command groups sequences that are part of a larger sequence.\n"; + helpString += "The cluster.fragments command outputs a new fasta and name or count file.\n"; + helpString += "The cluster.fragments command parameters are fasta, name, count, diffs and percent. The fasta parameter is required, unless you have a valid current file. \n"; + helpString += "The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n"; + helpString += "The diffs parameter allows you to set the number of differences allowed, default=0. \n"; + helpString += "The percent parameter allows you to set percentage of differences allowed, default=0. percent=2 means if the number of difference is less than or equal to two percent of the length of the fragment, then cluster.\n"; + helpString += "You may use diffs and percent at the same time to say something like: If the number or differences is greater than 1 or more than 2% of the fragment length, don't merge. \n"; + helpString += "The cluster.fragments command should be in the following format: \n"; + helpString += "cluster.fragments(fasta=yourFastaFile, names=yourNamesFile) \n"; + helpString += "Example cluster.fragments(fasta=amazon.fasta).\n"; + helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n"; + return helpString; + } + catch(exception& e) { + m->errorOut(e, "ClusterFragmentsCommand", "getHelpString"); + exit(1); + } +} +//********************************************************************************************************************** +string ClusterFragmentsCommand::getOutputPattern(string type) { + try { + string pattern = ""; + + if (type == "fasta") { pattern = "[filename],fragclust.fasta"; } + else if (type == "name") { pattern = "[filename],fragclust.names"; } + else if (type == "count") { pattern = "[filename],fragclust.count_table"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } + + return pattern; + } + catch(exception& e) { + m->errorOut(e, "ClusterFragmentsCommand", "getOutputPattern"); + exit(1); + } +} +//********************************************************************************************************************** +ClusterFragmentsCommand::ClusterFragmentsCommand(){ + try { + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["count"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "ClusterFragmentsCommand", "ClusterFragmentsCommand"); + exit(1); + } +} +//********************************************************************************************************************** ClusterFragmentsCommand::ClusterFragmentsCommand(string option) { try { - abort = false; + abort = false; calledHelp = false; //allow user to run help - if(option == "help") { help(); abort = true; } + if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} else { - //valid paramters for this command - string Array[] = {"fasta","name","outputdir","inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); @@ -48,6 +122,12 @@ ClusterFragmentsCommand::ClusterFragmentsCommand(string option) { if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; } } + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["count"] = tempOutNames; + //if the user changes the input directory command factory will send this info to us in the output parameter string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; } @@ -68,12 +148,25 @@ ClusterFragmentsCommand::ClusterFragmentsCommand(string option) { //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["name"] = inputDir + it->second; } } + + it = parameters.find("count"); + //user has given a template file + if(it != parameters.end()){ + path = m->hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["count"] = inputDir + it->second; } + } } //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); - if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the cluster.fragments command."); m->mothurOutEndLine(); abort = true; } - else if (fastafile == "not open") { abort = true; } + if (fastafile == "not found") { + fastafile = m->getFastaFile(); + if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); } + else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; } + } + else if (fastafile == "not open") { fastafile = ""; abort = true; } + else { m->setFastaFile(fastafile); } //if the user changes the output directory command factory will send this info to us in the output parameter outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); } @@ -82,8 +175,30 @@ ClusterFragmentsCommand::ClusterFragmentsCommand(string option) { // ...at some point should added some additional type checking... namefile = validParameter.validFile(parameters, "name", true); if (namefile == "not found") { namefile = ""; } - else if (namefile == "not open") { abort = true; } - else { readNameFile(); } + else if (namefile == "not open") { namefile = ""; abort = true; } + else { readNameFile(); m->setNameFile(namefile); } + + countfile = validParameter.validFile(parameters, "count", true); + if (countfile == "not open") { abort = true; countfile = ""; } + else if (countfile == "not found") { countfile = ""; } + else { ct.readTable(countfile); m->setCountTableFile(countfile); } + + if ((countfile != "") && (namefile != "")) { m->mothurOut("When executing a cluster.fragments command you must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; } + + string temp; + temp = validParameter.validFile(parameters, "diffs", false); if (temp == "not found"){ temp = "0"; } + m->mothurConvert(temp, diffs); + + temp = validParameter.validFile(parameters, "percent", false); if (temp == "not found"){ temp = "0"; } + m->mothurConvert(temp, percent); + + if (countfile == "") { + if (namefile == "") { + vector files; files.push_back(fastafile); + parser.getNameFile(files); + } + } + } } @@ -92,31 +207,11 @@ ClusterFragmentsCommand::ClusterFragmentsCommand(string option) { exit(1); } } - -//********************************************************************************************************************** -ClusterFragmentsCommand::~ClusterFragmentsCommand(){} -//********************************************************************************************************************** -void ClusterFragmentsCommand::help(){ - try { - m->mothurOut("The cluster.fragments command groups sequences that are part of a larger sequence.\n"); - m->mothurOut("The cluster.fragments command outputs a new fasta and name file.\n"); - m->mothurOut("The cluster.fragments command parameters are fasta and name. The fasta parameter is required. \n"); - m->mothurOut("The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n"); - m->mothurOut("The cluster.fragments command should be in the following format: \n"); - m->mothurOut("cluster.fragments(fasta=yourFastaFile, names=yourNamesFile) \n"); - m->mothurOut("Example cluster.fragments(fasta=amazon.fasta).\n"); - m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); - } - catch(exception& e) { - m->errorOut(e, "ClusterFragmentsCommand", "help"); - exit(1); - } -} //********************************************************************************************************************** int ClusterFragmentsCommand::execute(){ try { - if (abort == true) { return 0; } + if (abort == true) { if (calledHelp) { return 0; } return 2; } int start = time(NULL); @@ -147,14 +242,15 @@ int ClusterFragmentsCommand::execute(){ if (alignSeqs[j].active) { //this sequence has not been merged yet string jBases = alignSeqs[j].seq.getUnaligned(); - - int pos = iBases.find(jBases); - - if (pos != string::npos) { - //merge - alignSeqs[i].names += ',' + alignSeqs[j].names; - alignSeqs[i].numIdentical += alignSeqs[j].numIdentical; - + + if (isFragment(iBases, jBases)) { + if (countfile != "") { + ct.mergeCounts(alignSeqs[i].names, alignSeqs[j].names); + }else { + //merge + alignSeqs[i].names += ',' + alignSeqs[j].names; + alignSeqs[i].numIdentical += alignSeqs[j].numIdentical; + } alignSeqs[j].active = 0; alignSeqs[j].numIdentical = 0; count++; @@ -173,9 +269,11 @@ int ClusterFragmentsCommand::execute(){ string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile)); - - string newFastaFile = fileroot + "fragclust.fasta"; - string newNamesFile = fileroot + "names"; + map variables; + variables["[filename]"] = fileroot; + string newFastaFile = getOutputFileName("fasta", variables); + string newNamesFile = getOutputFileName("name", variables); + if (countfile != "") { newNamesFile = getOutputFileName("count", variables); } if (m->control_pressed) { return 0; } @@ -187,13 +285,31 @@ int ClusterFragmentsCommand::execute(){ m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); - if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; } + if (m->control_pressed) { m->mothurRemove(newFastaFile); m->mothurRemove(newNamesFile); return 0; } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); m->mothurOut(newFastaFile); m->mothurOutEndLine(); m->mothurOut(newNamesFile); m->mothurOutEndLine(); + outputNames.push_back(newFastaFile); outputNames.push_back(newNamesFile); outputTypes["fasta"].push_back(newFastaFile); outputTypes["name"].push_back(newNamesFile); m->mothurOutEndLine(); + + //set fasta file as new current fastafile + string current = ""; + itTypes = outputTypes.find("fasta"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); } + } + + itTypes = outputTypes.find("name"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); } + } + + itTypes = outputTypes.find("count"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); } + } return 0; @@ -203,7 +319,63 @@ int ClusterFragmentsCommand::execute(){ exit(1); } } - +//*************************************************************************************************************** +bool ClusterFragmentsCommand::isFragment(string seq1, string seq2){ + try { + bool fragment = false; + + //exact match + int pos = seq1.find(seq2); + if (pos != string::npos) { return true; } + //no match, no diffs wanted + else if ((diffs == 0) && (percent == 0)) { return false; } + else { //try aligning and see if you can find it + + //find number of acceptable differences for this sequence fragment + int totalDiffs; + if (diffs == 0) { //you didnt set diffs you want a percentage + totalDiffs = floor((seq2.length() * (percent / 100.0))); + }else if (percent == 0) { //you didn't set percent you want diffs + totalDiffs = diffs; + }else if ((percent != 0) && (diffs != 0)) { //you want both, set total diffs to smaller of 2 + totalDiffs = diffs; + int percentDiff = floor((seq2.length() * (percent / 100.0))); + if (percentDiff < totalDiffs) { totalDiffs = percentDiff; } + } + + Alignment* alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (seq1.length()+totalDiffs+1)); + + //use needleman to align + alignment->align(seq2, seq1); + string tempSeq2 = alignment->getSeqAAln(); + string temp = alignment->getSeqBAln(); + + delete alignment; + + //chop gap ends + int startPos = 0; + int endPos = tempSeq2.length()-1; + for (int i = 0; i < tempSeq2.length(); i++) { if (isalpha(tempSeq2[i])) { startPos = i; break; } } + for (int i = tempSeq2.length()-1; i >= 0; i--) { if (isalpha(tempSeq2[i])) { endPos = i; break; } } + + //count number of diffs + int numDiffs = 0; + for (int i = startPos; i <= endPos; i++) { + if (tempSeq2[i] != temp[i]) { numDiffs++; } + } + + if (numDiffs <= totalDiffs) { fragment = true; } + + } + + return fragment; + + } + catch(exception& e) { + m->errorOut(e, "ClusterFragmentsCommand", "isFragment"); + exit(1); + } +} /**************************************************************************************************/ int ClusterFragmentsCommand::readFASTA(){ try { @@ -225,7 +397,10 @@ int ClusterFragmentsCommand::readFASTA(){ else{ seqRNode tempNode(itSize->second, seq, names[seq.getName()], seq.getUnaligned().length()); alignSeqs.push_back(tempNode); - } + } + }else if(countfile != "") { + seqRNode tempNode(ct.getNumSeqs(seq.getName()), seq, seq.getName(), seq.getUnaligned().length()); + alignSeqs.push_back(tempNode); }else { //no names file, you are identical to yourself seqRNode tempNode(1, seq, seq.getName(), seq.getUnaligned().length()); alignSeqs.push_back(tempNode); @@ -249,17 +424,18 @@ void ClusterFragmentsCommand::printData(string newfasta, string newname){ ofstream outNames; m->openOutputFile(newfasta, outFasta); - m->openOutputFile(newname, outNames); + if (countfile == "") { m->openOutputFile(newname, outNames); } for (int i = 0; i < alignSeqs.size(); i++) { if (alignSeqs[i].numIdentical != 0) { alignSeqs[i].seq.printSequence(outFasta); - outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl; + if (countfile == "") { outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl; } } } outFasta.close(); - outNames.close(); + if (countfile == "") { outNames.close(); } + else { ct.printTable(newname); } } catch(exception& e) { m->errorOut(e, "ClusterFragmentsCommand", "printData"); @@ -291,6 +467,5 @@ void ClusterFragmentsCommand::readNameFile(){ exit(1); } } - /**************************************************************************************************/