X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=preclustercommand.cpp;h=215b5bd07981a39b79af57bff95da745b3aa0632;hb=bc4c4d63e983a3e91e74b5038bfec6705e1a3a2e;hp=66fd8500e3c7d583604106e0408e15bc36dc4a92;hpb=5b9b3e01150055e3b4bb1a911ea4c61d0b755e89;p=mothur.git diff --git a/preclustercommand.cpp b/preclustercommand.cpp index 66fd850..215b5bd 100644 --- a/preclustercommand.cpp +++ b/preclustercommand.cpp @@ -13,7 +13,7 @@ inline bool comparePriority(seqPNode first, seqPNode second) { return (first.numIdentical > second.numIdentical); } //********************************************************************************************************************** -PreClusterCommand::PreClusterCommand(string option){ +PreClusterCommand::PreClusterCommand(string option) { try { abort = false; @@ -22,31 +22,59 @@ PreClusterCommand::PreClusterCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta", "name", "diffs"}; + string Array[] = {"fasta", "name", "diffs", "outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command for (map::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; } } + //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 = ""; } + else { + string path; + it = parameters.find("fasta"); + //user has given a template file + if(it != parameters.end()){ + path = hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["fasta"] = inputDir + it->second; } + } + + it = parameters.find("name"); + //user has given a template file + if(it != parameters.end()){ + path = hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["name"] = inputDir + it->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); - if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; } + if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the pre.cluster command."); m->mothurOutEndLine(); abort = true; } else if (fastafile == "not open") { abort = true; } + //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 = ""; + outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it + } + //check for optional parameter and set defaults // ...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 { readNameFile(); } string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; } convert(temp, diffs); @@ -54,7 +82,7 @@ PreClusterCommand::PreClusterCommand(string option){ } catch(exception& e) { - errorOut(e, "PreClusterCommand", "PreClusterCommand"); + m->errorOut(e, "PreClusterCommand", "PreClusterCommand"); exit(1); } } @@ -65,18 +93,18 @@ PreClusterCommand::~PreClusterCommand(){} void PreClusterCommand::help(){ try { - mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n"); - mothurOut("The pre.cluster command outputs a new fasta and name file.\n"); - mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n"); - 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"); - mothurOut("The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n"); - mothurOut("The pre.cluster command should be in the following format: \n"); - mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n"); - mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n"); - mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); + m->mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n"); + m->mothurOut("The pre.cluster command outputs a new fasta and name file.\n"); + m->mothurOut("The pre.cluster command parameters are fasta, names and diffs. 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 diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n"); + m->mothurOut("The pre.cluster command should be in the following format: \n"); + m->mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n"); + m->mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); } catch(exception& e) { - errorOut(e, "PreClusterCommand", "help"); + m->errorOut(e, "PreClusterCommand", "help"); exit(1); } } @@ -88,10 +116,12 @@ int PreClusterCommand::execute(){ if (abort == true) { return 0; } //reads fasta file and return number of seqs - int numSeqs = readNamesFASTA(); //fills alignSeqs and makes all seqs active + int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active + + if (m->control_pressed) { return 0; } - if (numSeqs == 0) { mothurOut("Error reading fasta file...please correct."); mothurOutEndLine(); return 0; } - if (diffs > length) { mothurOut("Error: diffs is greater than your sequence length."); mothurOutEndLine(); return 0; } + if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; } + if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0; } //clear sizes since you only needed this info to build the alignSeqs seqPNode structs // sizes.clear(); @@ -99,12 +129,9 @@ int PreClusterCommand::execute(){ //sort seqs by number of identical seqs sort(alignSeqs.begin(), alignSeqs.end(), comparePriority); - //go through active list and cluster everthing you can, until all nodes are inactive - //taking advantage of the fact that maps are already sorted -// map::iterator itActive; -// map::iterator it2Active; int count = 0; - + + //think about running through twice... for (int i = 0; i < numSeqs; i++) { //are you active @@ -114,6 +141,9 @@ int PreClusterCommand::execute(){ //try to merge it with all smaller seqs for (int j = i+1; j < numSeqs; j++) { + + if (m->control_pressed) { return 0; } + if (alignSeqs[j].active) { //this sequence has not been merged yet //are you within "diff" bases int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned()); @@ -135,97 +165,90 @@ int PreClusterCommand::execute(){ }//end if active i if(i % 100 == 0) { cout << i << '\t' << numSeqs - count << '\t' << count << endl; } } - - string newFastaFile = getRootName(fastafile) + "precluster" + getExtension(fastafile); - string newNamesFile = getRootName(fastafile) + "precluster.names"; + string fileroot = outputDir + getRootName(getSimpleName(fastafile)); - mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine(); - mothurOut("pre.cluster removed " + toString(count) + " sequences."); mothurOutEndLine(); + string newFastaFile = fileroot + "precluster" + getExtension(fastafile); + string newNamesFile = fileroot + "precluster.names"; + + if (m->control_pressed) { return 0; } + + m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine(); + m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); printData(newFastaFile, newNamesFile); + + if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; } + + m->mothurOutEndLine(); + m->mothurOut("Output File Names: "); m->mothurOutEndLine(); + m->mothurOut(newFastaFile); m->mothurOutEndLine(); + m->mothurOut(newNamesFile); m->mothurOutEndLine(); + m->mothurOutEndLine(); return 0; } catch(exception& e) { - errorOut(e, "PreClusterCommand", "execute"); - exit(1); - } -} -/**************************************************************************************************/ -int PreClusterCommand::readFASTA(){ - try { -// ifstream inFasta; -// openInputFile(fastafile, inFasta); -// length = 0; -//// map::iterator it; -// -// while (!inFasta.eof()) { -// Sequence temp(inFasta); //read seq -// -// if (temp.getName() != "") { -// if (namefile != "") { -// //make sure fasta and name files match -// it = names.find(temp.getName()); -// if (it == names.end()) { mothurOut(temp.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); } -// }else { sizes[temp.getName()] = 1; } -// -// seqPNode tempNode(sizes[temp.getName()], temp); -// alignSeqs.push_back(tempNode); -// active[temp.getName()] = true; -// } -// gobble(inFasta); -// } -// inFasta.close(); -// -// if (alignSeqs.size() != 0) { length = alignSeqs[0].seq.getAligned().length(); } -// - return alignSeqs.size(); - } - catch(exception& e) { - errorOut(e, "PreClusterCommand", "readFASTA"); + m->errorOut(e, "PreClusterCommand", "execute"); exit(1); } } + /**************************************************************************************************/ -int PreClusterCommand::readNamesFASTA(){ +//this requires the names and fasta file to be in the same order + +int PreClusterCommand::readFASTA(){ try { - ifstream inNames; + //ifstream inNames; ifstream inFasta; - openInputFile(namefile, inNames); + //openInputFile(namefile, inNames); openInputFile(fastafile, inFasta); - string firstCol, secondCol, nameString; + //string firstCol, secondCol, nameString; length = 0; - while (inFasta && inNames) { - - inNames >> firstCol >> secondCol; - nameString = secondCol; + while (!inFasta.eof()) { - gobble(inNames); - int size = 1; - while (secondCol.find_first_of(',') != -1) { - size++; - secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length()); + if (m->control_pressed) { inFasta.close(); return 0; } + + //inNames >> firstCol >> secondCol; + //nameString = secondCol; + + //gobble(inNames); + //int size = 1; + //while (secondCol.find_first_of(',') != -1) { + // size++; + // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length()); + //} + + Sequence seq(inFasta); gobble(inFasta); + + if (seq.getName() != "") { //can get "" if commented line is at end of fasta file + if (namefile != "") { + itSize = sizes.find(seq.getName()); + + if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); } + else{ + seqPNode tempNode(itSize->second, seq, names[seq.getName()]); + alignSeqs.push_back(tempNode); + if (seq.getAligned().length() > length) { length = alignSeqs[0].seq.getAligned().length(); } + } + }else { //no names file, you are identical to yourself + seqPNode tempNode(1, seq, seq.getName()); + alignSeqs.push_back(tempNode); + if (seq.getAligned().length() > length) { length = alignSeqs[0].seq.getAligned().length(); } + } } - Sequence seq(inFasta); - if (seq.getName() != firstCol) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); } - else{ - seqPNode tempNode(size, seq, nameString); - alignSeqs.push_back(tempNode); - if (seq.getAligned().length() > length) { length = alignSeqs[0].seq.getAligned().length(); } - } } inFasta.close(); - inNames.close(); + //inNames.close(); return alignSeqs.size(); } catch(exception& e) { - errorOut(e, "PreClusterCommand", "readNamesFASTA"); + m->errorOut(e, "PreClusterCommand", "readFASTA"); exit(1); } } @@ -245,7 +268,7 @@ int PreClusterCommand::calcMisMatches(string seq1, string seq2){ return numBad; } catch(exception& e) { - errorOut(e, "PreClusterCommand", "calcMisMatches"); + m->errorOut(e, "PreClusterCommand", "calcMisMatches"); exit(1); } } @@ -273,7 +296,33 @@ void PreClusterCommand::printData(string newfasta, string newname){ } catch(exception& e) { - errorOut(e, "PreClusterCommand", "printData"); + m->errorOut(e, "PreClusterCommand", "printData"); + exit(1); + } +} + +/**************************************************************************************************/ + +void PreClusterCommand::readNameFile(){ + try { + ifstream in; + openInputFile(namefile, in); + string firstCol, secondCol; + + while (!in.eof()) { + in >> firstCol >> secondCol; gobble(in); + names[firstCol] = secondCol; + int size = 1; + while (secondCol.find_first_of(',') != -1) { + size++; + secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length()); + } + sizes[firstCol] = size; + } + in.close(); + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "readNameFile"); exit(1); } }