X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=distancecommand.cpp;h=065c14a1671a8abe78e1dda115208936185f496e;hb=cd37904452dc95b183ff313ff05720c562902487;hp=6fee651cb8963680ec294051e671cfc57b9c6135;hpb=cdcf99b7760701e6869ca8e4e6e91c8e8c4ae186;p=mothur.git diff --git a/distancecommand.cpp b/distancecommand.cpp index 6fee651..065c14a 100644 --- a/distancecommand.cpp +++ b/distancecommand.cpp @@ -16,42 +16,83 @@ //********************************************************************************************************************** -DistanceCommand::DistanceCommand(){ +DistanceCommand::DistanceCommand(string option){ try { - globaldata = GlobalData::getInstance(); - validCalculator = new ValidCalculators(); - countends = globaldata->getCountEnds(); - convert(globaldata->getProcessors(), processors); - convert(globaldata->getCutOff(), cutoff); + abort = false; + Estimators.clear(); - int i; - if (countends == "T") { - for (i=0; iEstimators.size(); i++) { - if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) { - if (globaldata->Estimators[i] == "nogaps") { - distCalculator = new ignoreGaps(); - }else if (globaldata->Estimators[i] == "eachgap") { - distCalculator = new eachGapDist(); - }else if (globaldata->Estimators[i] == "onegap") { - distCalculator = new oneGapDist(); } - } + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"fasta", "phylip", "calc", "countends", "cutoff", "processors"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + + //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; } + } + + //check for required parameters + fastafile = validParameter.validFile(parameters, "fasta", true); + if (fastafile == "not found") { cout << "fasta is a required parameter for the dist.seqs command." << endl; abort = true; } + else if (fastafile == "not open") { abort = true; } + else{ + ifstream inFASTA; + openInputFile(fastafile, inFASTA); + alignDB = SequenceDB(inFASTA); } - }else { - for (i=0; iEstimators.size(); i++) { - if (validCalculator->isValidCalculator("distance", globaldata->Estimators[i]) == true) { - if (globaldata->Estimators[i] == "nogaps") { - distCalculator = new ignoreGaps(); - }else if (globaldata->Estimators[i] == "eachgap") { - distCalculator = new eachGapIgnoreTermGapDist(); - }else if (globaldata->Estimators[i] == "onegap") { - distCalculator = new oneGapIgnoreTermGapDist(); + + //check for optional parameter and set defaults + // ...at some point should added some additional type checking... + calc = validParameter.validFile(parameters, "calc", false); + if (calc == "not found") { calc = "onegap"; } + else { + if (calc == "default") { calc = "onegap"; } + } + splitAtDash(calc, Estimators); + + string temp; + temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; } + convert(temp, countends); + + temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; } + convert(temp, cutoff); + + temp = validParameter.validFile(parameters, "processors", false); if(temp == "not found"){ temp = "1"; } + convert(temp, processors); + + phylip = validParameter.validFile(parameters, "phylip", false); if(phylip == "not found"){ phylip = "F"; } + + + ValidCalculators validCalculator; + + if (isTrue(countends) == true) { + for (int i=0; isetCalc(""); } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function DistanceCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; @@ -62,122 +103,106 @@ DistanceCommand::DistanceCommand(){ exit(1); } } + +//********************************************************************************************************************** + +DistanceCommand::~DistanceCommand(){ + + for(int i=0;istart = int (sqrt(float(i)/float(processors)) * numSeqs); + lines[i]->end = int (sqrt(float(i+1)/float(processors)) * numSeqs); } - else if(processors == 4){ - int pid1 = fork(); - if(pid1 > 0){ - int pid2 = fork(); - if(pid2 > 0){ - driver(distCalculator, seqDB, 0, numSeqs / 2, distFile + "tempa", cutoff); - appendFiles(distFile+"tempa", distFile); - remove((distFile + "tempa").c_str()); - } - else{ - driver(distCalculator, seqDB, numSeqs / 2, (numSeqs/sqrt(2)), distFile + "tempb", cutoff); - appendFiles(distFile+"tempb", distFile); - remove((distFile + "tempb").c_str()); - } - wait(NULL); - } - else{ - int pid3 = fork(); - if(pid3 > 0){ - driver(distCalculator, seqDB, (numSeqs/sqrt(2)), (sqrt(3) * numSeqs / 2), distFile + "tempc", cutoff); - appendFiles(distFile+"tempc", distFile); - remove((distFile + "tempc").c_str()); - } - else{ - driver(distCalculator, seqDB, (sqrt(3) * numSeqs / 2), numSeqs, distFile + "tempd", cutoff); - appendFiles(distFile+"tempd", distFile); - remove((distFile + "tempd").c_str()); - } - wait(NULL); - } - wait(NULL); + + createProcesses(outputFile); + + map::iterator it = processIDS.begin(); + rename((outputFile + toString(it->second) + ".temp").c_str(), outputFile.c_str()); + it++; + + //append and remove temp files + for (; it != processIDS.end(); it++) { + appendFiles((outputFile + toString(it->second) + ".temp"), outputFile); + remove((outputFile + toString(it->second) + ".temp").c_str()); } - wait(NULL); - # else - driver(distCalculator, seqDB, 0, numSeqs, distFile, cutoff); - # endif - + } +#else + ifstream inFASTA + driver(0, numSeqs, outputFile, cutoff); +#endif + delete distCalculator; - + return 0; - + } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; @@ -188,35 +213,80 @@ int DistanceCommand::execute(){ exit(1); } } +/**************************************************************************************************/ +void DistanceCommand::createProcesses(string filename) { + try { +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + int process = 0; + processIDS.clear(); + + //loop through and create all the processes you want + while (process != processors) { + int pid = fork(); + + if (pid > 0) { + processIDS[lines[process]->end] = pid; //create map from line number to pid so you can append files in correct order later + process++; + }else if (pid == 0){ + driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", cutoff); + exit(0); + }else { cout << "unable to spawn the necessary processes." << endl; exit(0); } + } + + //force parent to wait until all the processes are done + for (map::iterator it = processIDS.begin(); it != processIDS.end(); it++) { + int temp = it->second; + wait(&temp); + } +#endif + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the DistanceCommand class Function createProcesses. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the DistanceCommand class function createProcesses. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } +} /**************************************************************************************************/ /////// need to fix to work with calcs and sequencedb -int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLine, int endLine, string dFileName, float cutoff){ +int DistanceCommand::driver(int startLine, int endLine, string dFileName, float cutoff){ try { + int startTime = time(NULL); - - ofstream distFile(dFileName.c_str(), ios::trunc); - distFile.setf(ios::fixed, ios::showpoint); - distFile << setprecision(4); - - for(int i=startLine;icalcDist(align->get(i), align->get(j)); + distCalculator->calcDist(alignDB.get(i), alignDB.get(j)); double dist = distCalculator->getDist(); - + if(dist <= cutoff){ - distFile << align->get(i).getName() << ' ' << align->get(j).getName() << ' ' << dist << endl; + if (!isTrue(phylip)) { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; } } - + if (isTrue(phylip)) { outFile << dist << '\t'; } + } + + if (isTrue(phylip) == true) { outFile << endl; } + if(i % 100 == 0){ cout << i << '\t' << time(NULL) - startTime << endl; } - + } cout << endLine-1 << '\t' << time(NULL) - startTime << endl; - + + outFile.close(); + return 1; } catch(exception& e) { @@ -227,7 +297,7 @@ int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLi cout << "An unknown error has occurred in the DistanceCommand class function driver. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; exit(1); } - + } /**************************************************************************************************/ @@ -235,7 +305,7 @@ void DistanceCommand::appendFiles(string temp, string filename) { try{ ofstream output; ifstream input; - + //open output file in append mode openOutputFileAppend(filename, output); @@ -250,7 +320,7 @@ void DistanceCommand::appendFiles(string temp, string filename) { output << line << endl; // Appending back newline char } } - + input.close(); output.close(); }