]> git.donarmstrong.com Git - mothur.git/blobdiff - readblast.cpp
added checks for ^C to quit command instead of program
[mothur.git] / readblast.cpp
index a6d23e95bfddf71afb20a2d4c2b2d8d1377db71d..49e370836bb2c9e2edd59e99d9835de8acfc5582 100644 (file)
@@ -16,12 +16,13 @@ inline bool compareOverlap(seqDist left, seqDist right){
        return (left.dist < right.dist);        
 } 
 /*********************************************************************************************/
-ReadBlast::ReadBlast(string file, float c, float p, int l, bool m, bool h) : blastfile(file), cutoff(c), penalty(p), length(l), minWanted(m), hclusterWanted(h) {
+ReadBlast::ReadBlast(string file, float c, float p, int l, bool ms, bool h) : blastfile(file), cutoff(c), penalty(p), length(l), minWanted(ms), hclusterWanted(h) {
        try {
+               m = MothurOut::getInstance();
                matrix = NULL;
        }
        catch(exception& e) {
-               errorOut(e, "ReadBlast", "ReadBlast");
+               m->errorOut(e, "ReadBlast", "ReadBlast");
                exit(1);
        }
 } 
@@ -29,12 +30,14 @@ ReadBlast::ReadBlast(string file, float c, float p, int l, bool m, bool h) : bla
 //assumptions about the blast file: 
 //1. if duplicate lines occur the first line is always best and is chosen
 //2. blast scores are grouped together, ie. a a .... score, a b .... score, a c ....score...
-void ReadBlast::read(NameAssignment* nameMap) {
+int ReadBlast::read(NameAssignment* nameMap) {
        try {
        
                //if the user has not given a names file read names from blastfile
                if (nameMap->size() == 0) { readNames(nameMap);  }
                int nseqs = nameMap->size();
+               
+               if (m->control_pressed) { return 0; }
 
                ifstream fileHandle;
                openInputFile(blastfile, fileHandle);
@@ -59,7 +62,14 @@ void ReadBlast::read(NameAssignment* nameMap) {
                        openOutputFile(overlapFile, outOverlap);
                        openOutputFile(distFile, outDist);
                }
-       
+               
+               if (m->control_pressed) { 
+                       fileHandle.close();
+                       if (!hclusterWanted) {  delete matrix; }
+                       else { outOverlap.close(); remove(overlapFile.c_str()); outDist.close(); remove(distFile.c_str());  }
+                       return 0;
+               }
+               
                Progress* reading = new Progress("Reading blast:     ", nseqs * nseqs);
                
                //this is used to quickly find if we already have a distance for this combo
@@ -98,11 +108,19 @@ void ReadBlast::read(NameAssignment* nameMap) {
                                        }
                                }
                        }
-               }else { mothurOut("Error in your blast file, cannot read."); mothurOutEndLine(); exit(1); }
+               }else { m->mothurOut("Error in your blast file, cannot read."); m->mothurOutEndLine(); exit(1); }
 
                                
                //read file
                while(!fileHandle.eof()){  
+               
+                       if (m->control_pressed) { 
+                               fileHandle.close();
+                               if (!hclusterWanted) {  delete matrix; }
+                               else { outOverlap.close(); remove(overlapFile.c_str()); outDist.close(); remove(distFile.c_str());  }
+                               delete reading;
+                               return 0;
+                       }
                        
                        //read in line from file
                        fileHandle >> firstName >> secondName >> percentId >> numBases >> mismatch >> gap >> startQuery >> endQuery >> startRef >> endRef >> eScore >> score;
@@ -248,6 +266,14 @@ void ReadBlast::read(NameAssignment* nameMap) {
                thisRowsBlastScores.clear();
                dists.clear();
                
+               if (m->control_pressed) { 
+                               fileHandle.close();
+                               if (!hclusterWanted) {  delete matrix; }
+                               else { outOverlap.close(); remove(overlapFile.c_str()); outDist.close(); remove(distFile.c_str());  }
+                               delete reading;
+                               return 0;
+               }
+               
                if (!hclusterWanted) {
                        sort(overlap.begin(), overlap.end(), compareOverlap);
                }else {
@@ -255,19 +281,29 @@ void ReadBlast::read(NameAssignment* nameMap) {
                        outOverlap.close();
                }
                
+               if (m->control_pressed) { 
+                               fileHandle.close();
+                               if (!hclusterWanted) {  delete matrix; }
+                               else {  remove(overlapFile.c_str());  remove(distFile.c_str());  }
+                               delete reading;
+                               return 0;
+               }
+               
                reading->finish();
                delete reading;
                fileHandle.close();
+               
+               return 0;
        }
        catch(exception& e) {
-               errorOut(e, "ReadBlast", "read");
+               m->errorOut(e, "ReadBlast", "read");
                exit(1);
        }
 } 
 /*********************************************************************************************/
-void ReadBlast::readNames(NameAssignment* nameMap) {
+int ReadBlast::readNames(NameAssignment* nameMap) {
        try {
-               mothurOut("Reading names... "); cout.flush();
+               m->mothurOut("Reading names... "); cout.flush();
                
                string name, hold, prevName;
                int num = 1;
@@ -284,6 +320,7 @@ void ReadBlast::readNames(NameAssignment* nameMap) {
                nameMap->push_back(prevName);
                
                while (!in.eof()) {
+                       if (m->control_pressed) { in.close(); return 0; }
                        
                        //read line
                        in >> name;
@@ -306,12 +343,15 @@ void ReadBlast::readNames(NameAssignment* nameMap) {
                //openOutputFile(outNames, out);
                //nameMap->print(out);
                //out.close();
+               if (m->control_pressed) { return 0; }
+               
+               m->mothurOut(toString(num) + " names read."); m->mothurOutEndLine();
                
-               mothurOut(toString(num) + " names read."); mothurOutEndLine();
+               return 0;
                
        }
        catch(exception& e) {
-               errorOut(e, "ReadBlast", "readNames");
+               m->errorOut(e, "ReadBlast", "readNames");
                exit(1);
        }
 }