]> git.donarmstrong.com Git - mothur.git/commitdiff
got rid of extra temp files in dist.seqs
authorwestcott <westcott>
Mon, 8 Jun 2009 16:40:33 +0000 (16:40 +0000)
committerwestcott <westcott>
Mon, 8 Jun 2009 16:40:33 +0000 (16:40 +0000)
distancecommand.cpp
distancecommand.h

index 9297909cab4056625299ff713328860a0cc656bb..67b87f10e21ab5524a090241e442b388c03306aa 100644 (file)
@@ -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;i<endLine;i++){
                        
@@ -218,13 +203,13 @@ int DistanceCommand::driver(Dist* distCalculator, SequenceDB* align, int startLi
                                double dist = distCalculator->getDist();
                                
                                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();
                
index 8322eeedee6721ae333fdcbd6bc0447c9d654c53..2cb653f790aeb970f2a8f9ba7e1122d2f8ef4c57 100644 (file)
@@ -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<linePair*> 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);
 
 };