]> git.donarmstrong.com Git - mothur.git/blobdiff - readfasta.cpp
fixed some bugs
[mothur.git] / readfasta.cpp
index e1f22ce228bbb729ce24a75a10d507f243d2179c..fcac6bd276406119fd35415ba56f491b62958a3c 100644 (file)
 #include <fstream>
 
 /*******************************************************************************/
-ReadFasta::ReadFasta(string file) : ReadSeqs(file) {
-       try {
-       }
-       catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ReadTree class Function ReadTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the ReadTree class function ReadTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
-}
+ReadFasta::ReadFasta(string file) : ReadSeqs(file) {}
 /*******************************************************************************/
 ReadFasta::~ReadFasta(){
        //for(int i = 0; i < sequencedb.getNumSeqs(); i++)
@@ -31,7 +20,8 @@ ReadFasta::~ReadFasta(){
 }
 /*******************************************************************************/
 void ReadFasta::read() {
-       string name = "";
+       try {
+       /*string name = "";
        string sequence = "";
        string temp;
        int count = 0;
@@ -61,12 +51,104 @@ void ReadFasta::read() {
                        
        }
        Sequence newSequence(name, sequence);
-       sequencedb.add(newSequence);
+       sequencedb.add(newSequence); */
+
+               string name, sequence, line;
+               sequence = "";
+               int c;
+               string temp;
+               
+               
+               //read through file
+               while ((c = filehandle.get()) != EOF) {
+                       name = ""; sequence = ""; 
+                       //is this a name
+                       if (c == '>') { 
+                               name = readName(filehandle); 
+                               sequence = readSequence(filehandle); 
+                       }else {  cout << "Error fasta in your file. Please correct." << endl; }
 
-       filehandle.close();
+                       //input sequence info into sequencedb
+                       Sequence newSequence(name, sequence);
+                       sequencedb.add(newSequence);
+                       
+                       //takes care of white space
+                       gobble(filehandle);
+               }
+
+               filehandle.close();
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the ReadFasta class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the ReadFasta class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
 }
 
 /*********************************************************************************/
 SequenceDB* ReadFasta::getDB() {
        return &sequencedb;
 }
+/*******************************************************************************/
+string ReadFasta::readName(ifstream& in) {
+       try{
+               string name = "";
+               int c;
+               string temp;
+               
+               while ((c = in.get()) != EOF) {
+                       //if c is not a line return
+                       if (c != 10) {
+                               name += c;
+                       }else { break;  }
+               }
+                       
+               return name;
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the ReadFasta class Function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the ReadFasta class function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+}
+
+/*******************************************************************************/
+string ReadFasta::readSequence(ifstream& in) {
+       try{
+               string sequence = "";
+               string line;
+               int pos, c;
+               
+               while (!in.eof()) {
+                       //save position in file in case next line is a new name.
+                       pos = in.tellg();
+                       line = "";
+                       in >> line;                     
+                       //if you are at a new name
+                       if (line[0] == '>') {
+                               //put file pointer back since you are now at a new name
+                               in.seekg(pos, ios::beg);
+                               c = in.get();  //because you put it back to a newline char
+                               break;
+                       }else {  sequence += line;      }
+               }
+                       
+               return sequence;
+       }
+       catch(exception& e) {
+               cout << "Standard Error: " << e.what() << " has occurred in the ReadFasta class Function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+       catch(...) {
+               cout << "An unknown error has occurred in the ReadFasta class function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               exit(1);
+       }
+}
+/*******************************************************************************/
+