]> git.donarmstrong.com Git - mothur.git/blobdiff - formatphylip.cpp
cleaned up code
[mothur.git] / formatphylip.cpp
index d7211cf8c0a2b1b0ef08c8696e348358404ae369..60591178836ee793a83cbd3ec75776b463af4ead 100644 (file)
 
 /***********************************************************************/
 FormatPhylipMatrix::FormatPhylipMatrix(string df) : filename(df) {
-        openInputFile(filename, fileHandle);
+        m->openInputFile(filename, fileHandle);
 }
 /***********************************************************************/
 //not using nameMap
-void FormatPhylipMatrix::read(NameAssignment* nameMap){
+int FormatPhylipMatrix::read(NameAssignment* nameMap){
        try {
         
                        float distance;
@@ -24,7 +24,12 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                        string name;
                        ofstream out;
                        
-                       fileHandle >> nseqs >> name;
+                       string numTest;
+                       fileHandle >> numTest >> name;
+                       
+                       if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
+                       else { convert(numTest, nseqs); }
+               
                                                
                        list = new ListVector(nseqs);
                        list->set(0, name);
@@ -37,11 +42,11 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                        fileHandle.close();  //reset file
                                        
                                        //open and get through numSeqs, code below formats rest of file
-                                       openInputFile(filename, fileHandle);
-                                       fileHandle >> nseqs; gobble(fileHandle);
+                                       m->openInputFile(filename, fileHandle);
+                                       fileHandle >> nseqs; m->gobble(fileHandle);
                                        
                                        distFile = filename + ".rowFormatted";
-                                       openOutputFile(distFile, out);
+                                       m->openOutputFile(distFile, out);
                                        break;
                                }
                                if(d == '\n'){
@@ -59,15 +64,19 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                
                                ofstream outTemp;
                                string tempFile = filename + ".temp";
-                               openOutputFile(tempFile, outTemp);
+                               m->openOutputFile(tempFile, outTemp);
                 
                                //convert to square column matrix
                                for(int i=1;i<nseqs;i++){
+                               
                                        fileHandle >> name;
                                        
                                        list->set(i, name);
                                        
                                        for(int j=0;j<i;j++){
+                                       
+                                               if (m->control_pressed) { outTemp.close(); m->mothurRemove(tempFile); fileHandle.close();  delete reading; return 0; }
+                                                                                       
                                                fileHandle >> distance;
                                                
                                                if (distance == -1) { distance = 1000000; }
@@ -84,10 +93,10 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                
                                //format from square column to rowFormatted
                                //sort file by first column so the distances for each row are together
-                               string outfile = getRootName(tempFile) + "sorted.dist.temp";
+                               string outfile = m->getRootName(tempFile) + "sorted.dist.temp";
                                
                                //use the unix sort 
-                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                                        string command = "sort -n " + tempFile + " -o " + outfile;
                                        system(command.c_str());
                                #else //sort using windows sort
@@ -95,13 +104,14 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                        system(command.c_str());
                                #endif
                                
+                               if (m->control_pressed) { m->mothurRemove(tempFile); m->mothurRemove(outfile);  delete reading; return 0; }
 
                                //output to new file distance for each row and save positions in file where new row begins
                                ifstream in;
-                               openInputFile(outfile, in);
+                               m->openInputFile(outfile, in);
                                
                                distFile = outfile + ".rowFormatted";
-                               openOutputFile(distFile, out);
+                               m->openOutputFile(distFile, out);
                                
                                rowPos.resize(nseqs, -1);
                                int currentRow;
@@ -118,7 +128,9 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                for(int k = 0; k < firstString.length(); k++)  {   in.putback(firstString[k]);  }
                                
                                while(!in.eof()) {
-                                       in >> first >> second >> dist; gobble(in);
+                                       if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(tempFile); m->mothurRemove(distFile); m->mothurRemove(outfile);  delete reading; return 0; }
+
+                                       in >> first >> second >> dist; m->gobble(in);
                                        
                                        if (first != currentRow) {
                                                //save position in file of each new row
@@ -158,8 +170,11 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                in.close();
                                out.close();
                                
-                               remove(tempFile.c_str());
-                               remove(outfile.c_str());
+                               m->mothurRemove(tempFile);
+                               m->mothurRemove(outfile);
+                               
+                               if (m->control_pressed) {  m->mothurRemove(distFile);   delete reading; return 0; }
+
                        }
                        else{ //square matrix convert directly to formatted row file
                                int index = nseqs;
@@ -173,6 +188,8 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                        list->set(i, name);
                                        
                                        for(int j=0;j<nseqs;j++){
+                                               if (m->control_pressed) {  fileHandle.close(); out.close(); m->mothurRemove(distFile);   delete reading; return 0; }
+                                               
                                                fileHandle >> distance;
                                        
                                                if (distance == -1) { distance = 1000000; }
@@ -184,7 +201,7 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                                                reading->update(index);
                                        }
                                        
-                                       gobble(fileHandle);
+                                       m->gobble(fileHandle);
                        
                                        //save position in file of each new row
                                        rowPos[i] = out.tellp();
@@ -202,14 +219,19 @@ void FormatPhylipMatrix::read(NameAssignment* nameMap){
                        }
                        reading->finish();
                        delete reading;
-                       
-                       list->setLabel("0");
                        fileHandle.close();
                        out.close();
                        
+                       if (m->control_pressed) { m->mothurRemove(distFile);  return 0; }
+                       
+                       list->setLabel("0");
+                       
+                       return 1;
+                       
+                       
        }
        catch(exception& e) {
-               errorOut(e, "FormatPhylipMatrix", "read");
+               m->errorOut(e, "FormatPhylipMatrix", "read");
                 exit(1);
        }
 }