X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=preclustercommand.cpp;h=14409f5a0a6aa86a166f42ff6f99423777237759;hb=4f9a6e14a608172f8a97f0297a3b8e6ea267c518;hp=fa5329b1e4094b4c87d60b5b4dfbd211f1bc1aeb;hpb=e057ab99eca7d45544c83ca6665a042b934b661b;p=mothur.git diff --git a/preclustercommand.cpp b/preclustercommand.cpp index fa5329b..14409f5 100644 --- a/preclustercommand.cpp +++ b/preclustercommand.cpp @@ -12,13 +12,61 @@ //********************************************************************************************************************** inline bool comparePriority(seqPNode first, seqPNode second) { return (first.numIdentical > second.numIdentical); } //********************************************************************************************************************** +vector PreClusterCommand::getValidParameters(){ + try { + string Array[] = {"fasta", "name", "diffs", "outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "getValidParameters"); + exit(1); + } +} +//********************************************************************************************************************** +PreClusterCommand::PreClusterCommand(){ + try { + abort = true; calledHelp = true; + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "PreClusterCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector PreClusterCommand::getRequiredParameters(){ + try { + string Array[] = {"fasta"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector PreClusterCommand::getRequiredFiles(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "getRequiredFiles"); + exit(1); + } +} +//********************************************************************************************************************** -PreClusterCommand::PreClusterCommand(string option){ +PreClusterCommand::PreClusterCommand(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 { //valid paramters for this command @@ -36,6 +84,11 @@ PreClusterCommand::PreClusterCommand(string option){ if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; } } + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = 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 = ""; } @@ -44,7 +97,7 @@ PreClusterCommand::PreClusterCommand(string option){ it = parameters.find("fasta"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->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; } } @@ -52,7 +105,7 @@ PreClusterCommand::PreClusterCommand(string option){ it = parameters.find("name"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->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; } } @@ -60,13 +113,13 @@ PreClusterCommand::PreClusterCommand(string option){ //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 + outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it } //check for optional parameter and set defaults @@ -82,7 +135,7 @@ PreClusterCommand::PreClusterCommand(string option){ } catch(exception& e) { - errorOut(e, "PreClusterCommand", "PreClusterCommand"); + m->errorOut(e, "PreClusterCommand", "PreClusterCommand"); exit(1); } } @@ -93,18 +146,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); } } @@ -113,13 +166,17 @@ void PreClusterCommand::help(){ int PreClusterCommand::execute(){ try { - if (abort == true) { return 0; } + if (abort == true) { if (calledHelp) { return 0; } return 2; } + + int start = time(NULL); //reads fasta file and return number of seqs 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(); @@ -139,6 +196,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()); @@ -157,25 +217,40 @@ int PreClusterCommand::execute(){ //remove from active list alignSeqs[i].active = 0; + }//end if active i - if(i % 100 == 0) { cout << i << '\t' << numSeqs - count << '\t' << count << endl; } + if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); } } - string fileroot = outputDir + getRootName(getSimpleName(fastafile)); + if(numSeqs % 100 != 0) { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); } + + + string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile)); - string newFastaFile = fileroot + "precluster" + getExtension(fastafile); + string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile); string newNamesFile = fileroot + "precluster.names"; + if (m->control_pressed) { return 0; } - mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine(); - mothurOut("pre.cluster removed " + toString(count) + " sequences."); mothurOutEndLine(); + m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine(); + m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); printData(newFastaFile, newNamesFile); + + 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; } + + m->mothurOutEndLine(); + m->mothurOut("Output File Names: "); m->mothurOutEndLine(); + m->mothurOut(newFastaFile); m->mothurOutEndLine(); outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile); + m->mothurOut(newNamesFile); m->mothurOutEndLine(); outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile); + m->mothurOutEndLine(); return 0; } catch(exception& e) { - errorOut(e, "PreClusterCommand", "execute"); + m->errorOut(e, "PreClusterCommand", "execute"); exit(1); } } @@ -189,40 +264,42 @@ int PreClusterCommand::readFASTA(){ //ifstream inNames; ifstream inFasta; - //openInputFile(namefile, inNames); - openInputFile(fastafile, inFasta); + //m->openInputFile(namefile, inNames); + m->openInputFile(fastafile, inFasta); //string firstCol, secondCol, nameString; length = 0; while (!inFasta.eof()) { - + + if (m->control_pressed) { inFasta.close(); return 0; } + //inNames >> firstCol >> secondCol; //nameString = secondCol; - //gobble(inNames); + //m->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); + Sequence seq(inFasta); m->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()) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); } + 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(); } + if (seq.getAligned().length() > length) { length = 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(); } + if (seq.getAligned().length() > length) { length = seq.getAligned().length(); } } } } @@ -232,7 +309,7 @@ int PreClusterCommand::readFASTA(){ } catch(exception& e) { - errorOut(e, "PreClusterCommand", "readFASTA"); + m->errorOut(e, "PreClusterCommand", "readFASTA"); exit(1); } } @@ -252,7 +329,7 @@ int PreClusterCommand::calcMisMatches(string seq1, string seq2){ return numBad; } catch(exception& e) { - errorOut(e, "PreClusterCommand", "calcMisMatches"); + m->errorOut(e, "PreClusterCommand", "calcMisMatches"); exit(1); } } @@ -264,8 +341,8 @@ void PreClusterCommand::printData(string newfasta, string newname){ ofstream outFasta; ofstream outNames; - openOutputFile(newfasta, outFasta); - openOutputFile(newname, outNames); + m->openOutputFile(newfasta, outFasta); + m->openOutputFile(newname, outNames); for (int i = 0; i < alignSeqs.size(); i++) { @@ -280,33 +357,32 @@ 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); + m->openInputFile(namefile, in); string firstCol, secondCol; while (!in.eof()) { - in >> firstCol >> secondCol; gobble(in); + in >> firstCol >> secondCol; m->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()); + + for(int i=0;ierrorOut(e, "PreClusterCommand", "readNameFile"); exit(1); } }