From: westcott Date: Mon, 8 Jun 2009 16:40:33 +0000 (+0000) Subject: got rid of extra temp files in dist.seqs X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=bede5752eaeedb5a7e339439b6ba2f6670bca2b9 got rid of extra temp files in dist.seqs --- diff --git a/distancecommand.cpp b/distancecommand.cpp index 9297909..67b87f1 100644 --- a/distancecommand.cpp +++ b/distancecommand.cpp @@ -81,21 +81,21 @@ int DistanceCommand::execute(){ int numSeqs = seqDB->getNumSeqs(); cutoff += 0.005; - string phylipFile = ""; - string distFile = getRootName(globaldata->getFastaFile()) + "dist"; - remove(distFile.c_str()); + string outputFile; //doses the user want the phylip formatted file as well if (isTrue(phylip) == true) { - phylipFile = getRootName(globaldata->getFastaFile()) + "phylip.dist"; - remove(phylipFile.c_str()); + outputFile = getRootName(globaldata->getFastaFile()) + "phylip.dist"; + remove(outputFile.c_str()); //output numSeqs to phylip formatted dist file - openOutputFile(phylipFile, phylipOut); - phylipOut << numSeqs << endl; - phylipOut.close(); + openOutputFile(outputFile, outFile); + outFile << numSeqs << endl; + outFile.close(); + }else { //user wants column format + outputFile = getRootName(globaldata->getFastaFile()) + "dist"; + remove(outputFile.c_str()); } - //# if defined (_WIN32) //figure out how to implement the fork and wait commands in windows @@ -106,12 +106,9 @@ int DistanceCommand::execute(){ #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) //if you don't need to fork anything if(processors == 1){ - driver(distCalculator, seqDB, 0, numSeqs, distFile, phylipFile + "tempPhylipA", cutoff); - - if (isTrue(phylip) == true) { - appendFiles((phylipFile + "tempPhylipA"), phylipFile); - remove((phylipFile + "tempPhylipA").c_str()); - } + driver(distCalculator, seqDB, 0, numSeqs, outputFile + ".temp", cutoff); + appendFiles((outputFile + ".temp"), outputFile); + remove((outputFile + ".temp").c_str()); }else{ //you have multiple processors for (int i = 0; i < processors; i++) { @@ -123,26 +120,18 @@ int DistanceCommand::execute(){ cout << lines[0]->start << '\t' << lines[0]->end << endl; cout << lines[1]->start << '\t' << lines[1]->end << endl; - createProcesses(distFile, phylipFile); + createProcesses(outputFile); //append and remove temp files for (it = processIDS.begin(); it != processIDS.end(); it++) { - appendFiles((distFile + toString(it->second) + ".temp"), distFile); - remove((distFile + toString(it->second) + ".temp").c_str()); - - if (isTrue(phylip) == true) { - appendFiles((phylipFile + toString(it->second) + ".temp"), phylipFile); - remove((phylipFile + toString(it->second) + ".temp").c_str()); - } + appendFiles((outputFile + toString(it->second) + ".temp"), outputFile); + remove((outputFile + toString(it->second) + ".temp").c_str()); } } #else - driver(distCalculator, seqDB, 0, numSeqs, distFile, phylipFile + "tempPhylipA", cutoff); - - if (isTrue(phylip) = true) { - appendFiles((phylipFile + "tempPhylipA"), phylipFile); - remove((phylipFile + "tempPhylipA").c_str()); - } + driver(distCalculator, seqDB, 0, numSeqs, outputFile + ".temp", cutoff); + appendFiles((outputFile + ".temp"), outputFile); + remove((outputFile + ".temp").c_str()); #endif delete distCalculator; @@ -160,7 +149,7 @@ int DistanceCommand::execute(){ } } /**************************************************************************************************/ -void DistanceCommand::createProcesses(string column, string phylip) { +void DistanceCommand::createProcesses(string filename) { try { int process = 0; processIDS.clear(); @@ -173,7 +162,7 @@ void DistanceCommand::createProcesses(string column, string phylip) { 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(distCalculator, seqDB, lines[process]->start, lines[process]->end, column + toString(getpid()) + ".temp", phylip + toString(getpid()) + ".temp", cutoff); + driver(distCalculator, seqDB, lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", cutoff); exit(0); }else { cout << "unable to spawn the necessary processes." << endl; exit(0); } } @@ -197,19 +186,15 @@ void DistanceCommand::createProcesses(string column, string phylip) { /**************************************************************************************************/ /////// need to fix to work with calcs and sequencedb -int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLine, int endLine, string dFileName, string pFilename, float cutoff){ +int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLine, int endLine, string dFileName, float cutoff){ try { int startTime = time(NULL); //column file - ofstream distFile(dFileName.c_str(), ios::trunc); - distFile.setf(ios::fixed, ios::showpoint); - distFile << setprecision(4); - - ofstream philFile(pFilename.c_str(), ios::trunc); - philFile.setf(ios::fixed, ios::showpoint); - philFile << setprecision(4); + ofstream outFile(dFileName.c_str(), ios::trunc); + outFile.setf(ios::fixed, ios::showpoint); + outFile << setprecision(4); for(int i=startLine;igetDist(); if(dist <= cutoff){ - distFile << align->get(i)->getName() << ' ' << align->get(j)->getName() << ' ' << dist << endl; + if (isTrue(phylip) != true) { outFile << align->get(i)->getName() << ' ' << align->get(j)->getName() << ' ' << dist << endl; } } - if (isTrue(phylip) == true) { philFile << dist << '\t'; } + if (isTrue(phylip) == true) { outFile << dist << '\t'; } } - if (isTrue(phylip) == true) { philFile << endl; } + if (isTrue(phylip) == true) { outFile << endl; } if(i % 100 == 0){ cout << i << '\t' << time(NULL) - startTime << endl; @@ -233,8 +218,6 @@ int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLi } cout << endLine-1 << '\t' << time(NULL) - startTime << endl; - if (isTrue(phylip) != true) { remove(pFilename.c_str()); } - //philFile.close(); //distFile.close(); diff --git a/distancecommand.h b/distancecommand.h index 8322eee..2cb653f 100644 --- a/distancecommand.h +++ b/distancecommand.h @@ -34,9 +34,8 @@ private: ValidCalculators* validCalculator; Dist* distCalculator; SequenceDB* seqDB; - ofstream out, phylipOut; + ofstream out, outFile; ifstream in; - string outputFileName; string countends, phylip; int processors; float cutoff; @@ -45,8 +44,8 @@ private: vector lines; void appendFiles(string, string); - void createProcesses(string, string); - int driver(Dist*, SequenceDB*, int, int, string, string, float); + void createProcesses(string); + int driver(Dist*, SequenceDB*, int, int, string, float); };