]> git.donarmstrong.com Git - mothur.git/blobdiff - fileoutput.cpp
bugs for 1.5
[mothur.git] / fileoutput.cpp
index c8df770c8054b42d1bdcf7c654700a284ee2aa8f..597a05a6ad9a892caff5f0b6f69269ae6af4013d 100644 (file)
@@ -16,7 +16,7 @@ ThreeColumnFile::~ThreeColumnFile(){
        inFile.close();
        outFile.close();
        remove(outName.c_str());
-};
+}
 
 /***********************************************************************/
 
@@ -27,7 +27,7 @@ void ThreeColumnFile::initFile(string label){
                        openInputFile(inName, inFile);
 
                        string inputBuffer;
-                       getline(inFile, inputBuffer);
+                       inputBuffer = getline(inFile);
                
                        outFile <<  inputBuffer << '\t' << label << "\tlci\thci" << endl;
                }
@@ -40,11 +40,7 @@ void ThreeColumnFile::initFile(string label){
                outFile.setf(ios::showpoint);
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the ThreeColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "ThreeColumnFile", "initFile");
                exit(1);
        }
 }
@@ -55,7 +51,7 @@ void ThreeColumnFile::output(int nSeqs, vector<double> data){
        try {
                if(counter != 0){               
                        string inputBuffer;
-                       getline(inFile, inputBuffer);
+                       inputBuffer = getline(inFile);
                
                        outFile <<  inputBuffer << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
                }
@@ -64,14 +60,10 @@ void ThreeColumnFile::output(int nSeqs, vector<double> data){
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "ThreeColumnFile", "output");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the ThreeColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-};
+}
 
 /***********************************************************************/
 
@@ -85,17 +77,118 @@ void ThreeColumnFile::resetFile(){
                        outFile.close();
                }
                counter = 1;
+               
                remove(inName.c_str());
-               rename(outName.c_str(), inName.c_str());
+               renameOk = rename(outName.c_str(), inName.c_str());
+               
+               //checks to make sure user was able to rename and remove successfully
+               if ((renameOk != 0)) {  mothurOut("Unable to rename necessary files."); mothurOutEndLine(); }
+
+       }
+       catch(exception& e) {
+               errorOut(e, "ThreeColumnFile", "resetFile");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+/***********************************************************************/
+
+ColumnFile::~ColumnFile(){
+       
+       inFile.close();
+       outFile.close();
+       remove(outName.c_str());
+}
+
+/***********************************************************************/
+
+void ColumnFile::initFile(string label, vector<string> tags){
+       try {
+               if(counter != 0){
+                       openOutputFile(outName, outFile);
+                       openInputFile(inName, inFile);
+
+                       string inputBuffer;
+                       inputBuffer = getline(inFile);
+               
+                       outFile <<  inputBuffer << '\t'; 
+                       for(int i = 0; i < tags.size(); i++) {
+                               outFile << label + tags[i] << '\t';
+                       }
+                       outFile << endl;
+               }
+               else{
+                       openOutputFile(outName, outFile);
+                       for(int i = 0; i < tags.size(); i++) {
+                               outFile << label + tags[i] << '\t';
+                       }
+                       outFile << endl;
+               }
+
+               outFile.setf(ios::fixed, ios::floatfield);
+               outFile.setf(ios::showpoint);
+       }
+       catch(exception& e) {
+               errorOut(e, "ColumnFile", "initFile");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+
+void ColumnFile::output(vector<double> data){
+       try {
+       
+               if(counter != 0){               
+                       string inputBuffer;
+                       inputBuffer = getline(inFile);
+
+                       outFile << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(iters.length());
+                       for (int i = 1; i< data.size(); i++) {
+                               outFile << '\t' << data[i]; 
+                       }
+                       outFile << endl;
+               }
+               else{
+                       outFile << setprecision(6) << data[0] << setprecision(iters.length());
+                       for (int i = 1; i< data.size(); i++) {
+                               outFile << '\t' << data[i]; 
+                       }
+                       outFile << endl;
+               }
+
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "ColumnFile", "output");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the ThreeColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+}
+
+/***********************************************************************/
+
+void ColumnFile::resetFile(){
+       try {
+               if(counter != 0){
+                       outFile.close();
+                       inFile.close();
+               }
+               else{
+                       outFile.close();
+               }
+               counter = 1;
+               
+               remove(inName.c_str());
+               renameOk = rename(outName.c_str(), inName.c_str());
+               
+               //checks to make sure user was able to rename and remove successfully
+               if ((renameOk != 0)) { mothurOut("Unable to rename necessary files."); mothurOutEndLine(); }
+
+       }
+       catch(exception& e) {
+               errorOut(e, "ColumnFile", "resetFile");
                exit(1);
-       }       
+       }
 }
 
 /***********************************************************************/
@@ -106,7 +199,7 @@ SharedThreeColumnFile::~SharedThreeColumnFile(){
        inFile.close();
        outFile.close();
        remove(outName.c_str());
-};
+}
 
 /***********************************************************************/
 
@@ -117,7 +210,7 @@ void SharedThreeColumnFile::initFile(string label){
                        openInputFile(inName, inFile);
 
                        string inputBuffer;
-                       getline(inFile, inputBuffer);
+                       inputBuffer = getline(inFile);
                
                        outFile <<  inputBuffer << '\t' << label << "\tlci\thci" << endl;
                }
@@ -130,11 +223,7 @@ void SharedThreeColumnFile::initFile(string label){
                outFile.setf(ios::showpoint);
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedThreeColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "SharedThreeColumnFile", "initFile");
                exit(1);
        }
 }
@@ -145,7 +234,7 @@ void SharedThreeColumnFile::output(int nSeqs, vector<double> data){
        try {
                if(counter != 0){               
                        string inputBuffer;
-                       getline(inFile, inputBuffer);
+                       inputBuffer = getline(inFile);
                
                        outFile <<  inputBuffer << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
                }
@@ -155,14 +244,10 @@ void SharedThreeColumnFile::output(int nSeqs, vector<double> data){
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "SharedThreeColumnFile", "output");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedThreeColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-};
+}
 
 /***********************************************************************/
 
@@ -177,16 +262,16 @@ void SharedThreeColumnFile::resetFile(){
                }
                counter = 1;
                remove(inName.c_str());
-               rename(outName.c_str(), inName.c_str());
+               renameOk = rename(outName.c_str(), inName.c_str());
+               
+               //checks to make sure user was able to rename and remove successfully
+               if ((renameOk != 0)) { mothurOut("Unable to rename necessary files."); mothurOutEndLine(); }
+
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "SharedThreeColumnFile", "resetFile");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedThreeColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -198,7 +283,7 @@ OneColumnFile::~OneColumnFile(){
        inFile.close();
        outFile.close();
        remove(outName.c_str());        
-};
+}
 
 /***********************************************************************/
 
@@ -209,7 +294,7 @@ void OneColumnFile::initFile(string label){
                        openInputFile(inName, inFile);
                
                        string inputBuffer;
-                       getline(inFile, inputBuffer);
+                       inputBuffer = getline(inFile);
                
                        outFile <<  inputBuffer << '\t' << label << endl;
                }
@@ -222,13 +307,9 @@ void OneColumnFile::initFile(string label){
                outFile.setf(ios::showpoint);
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "OneColumnFile", "initFile");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the OneColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -237,7 +318,7 @@ void OneColumnFile::output(int nSeqs, vector<double> data){
        try {   
                if(counter != 0){               
                        string inputBuffer;
-                       getline(inFile, inputBuffer);
+                       inputBuffer = getline(inFile);
                
                        outFile <<  inputBuffer << setprecision(4) << '\t'  << data[0] << endl;
                }
@@ -246,14 +327,10 @@ void OneColumnFile::output(int nSeqs, vector<double> data){
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "OneColumnFile", "output");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the OneColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
-};
+}
 
 /***********************************************************************/
 
@@ -268,27 +345,27 @@ void OneColumnFile::resetFile(){
                }       
                counter = 1;
                remove(inName.c_str());
-               rename(outName.c_str(), inName.c_str());
+               renameOk = rename(outName.c_str(), inName.c_str());
+               
+               //checks to make sure user was able to rename and remove successfully
+               if ((renameOk != 0)) { mothurOut("Unable to rename necessary files."); mothurOutEndLine(); }
+
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "OneColumnFile", "resetFile");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the OneColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
 /***********************************************************************/
 
-SharedOneColumnFile::~OneColumnFile(){
+SharedOneColumnFile::~SharedOneColumnFile(){
        
        inFile.close();
        outFile.close();
        remove(outName.c_str());        
-};
+}
 
 /***********************************************************************/
 
@@ -299,7 +376,7 @@ void SharedOneColumnFile::initFile(string label){
                        openInputFile(inName, inFile);
                
                        string inputBuffer;
-                       getline(inFile, inputBuffer);
+                       inputBuffer = getline(inFile);
                
                        outFile <<  inputBuffer << '\t' << label  << endl;
 
@@ -314,13 +391,9 @@ void SharedOneColumnFile::initFile(string label){
                outFile.setf(ios::showpoint);
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "SharedOneColumnFile", "initFile");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the OneColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -336,7 +409,7 @@ void SharedOneColumnFile::output(int nSeqs, vector<double> data){
                        }
                        if(counter != 0){               
                                string inputBuffer;
-                               getline(inFile, inputBuffer);
+                               inputBuffer = getline(inFile);
 
                                outFile <<  inputBuffer << setprecision(2) << '\t' << dataOutput << endl;
                        }
@@ -345,14 +418,10 @@ void SharedOneColumnFile::output(int nSeqs, vector<double> data){
                        }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "SharedOneColumnFile", "output");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the OneColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
-};
+}
 
 /***********************************************************************/
 
@@ -366,17 +435,19 @@ void SharedOneColumnFile::resetFile(){
                        outFile.close();
                }       
                counter = 1;
+               
                remove(inName.c_str());
-               rename(outName.c_str(), inName.c_str());
+               renameOk = rename(outName.c_str(), inName.c_str());
+               
+               //checks to make sure user was able to rename and remove successfully
+               if ((renameOk != 0)) { mothurOut("Unable to rename necessary files."); mothurOutEndLine(); }
+
+               
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               errorOut(e, "SharedOneColumnFile", "resetFile");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the OneColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/