X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=sequencedb.cpp;h=e0bd10033459896f10c2d42fb9b3b4f6921add7f;hb=faf4e99552d6fb4595ff348b1f909ddc74536da2;hp=f948bf355c84f18f7e67573ab77952d0f1e0cbf3;hpb=d5d2761f88b41f1006d0b700e0ab51e2ce48b875;p=mothur.git diff --git a/sequencedb.cpp b/sequencedb.cpp index f948bf3..e0bd100 100644 --- a/sequencedb.cpp +++ b/sequencedb.cpp @@ -16,16 +16,113 @@ /***********************************************************************/ SequenceDB::SequenceDB() {} +/***********************************************************************/ +//the clear function free's the memory +SequenceDB::~SequenceDB() { clear(); } /***********************************************************************/ SequenceDB::SequenceDB(int newSize) { - data.resize(newSize); + data.resize(newSize, NULL); } /***********************************************************************/ -SequenceDB::SequenceDB(ifstream&) {} +SequenceDB::SequenceDB(ifstream& filehandle) { + try{ + 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; } + + //input sequence info into sequencedb + Sequence* newSequence = new Sequence(name, sequence); + data.push_back(newSequence); + + //takes care of white space + gobble(filehandle); + } + + filehandle.close(); + + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function SequenceDB. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function SequenceDB. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } +} +/*******************************************************************************/ +string SequenceDB::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 SequenceDB class Function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function readName. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } +} + +/*******************************************************************************/ +string SequenceDB::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 SequenceDB class Function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function readSequence. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } +} /***********************************************************************/ @@ -36,32 +133,79 @@ int SequenceDB::getNumSeqs() { /***********************************************************************/ void SequenceDB::set(int index, string newUnaligned) { - Sequence newSeq(data[index].getName(), newUnaligned); - data[index] = newSeq; + try { + if (data[index] != NULL) { delete data[index]; } //free memory + + Sequence* newSeq = new Sequence(data[index]->getName(), newUnaligned); + data[index] = newSeq; + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } } /***********************************************************************/ -void SequenceDB::set(int index, Sequence newSeq) { - data[index] = newSeq; +void SequenceDB::set(int index, Sequence* newSeq) { + try { + if (data[index] != NULL) { delete data[index]; } //free memory + data[index] = newSeq; + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function set. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } } /***********************************************************************/ -Sequence SequenceDB::get(int index) { +Sequence* SequenceDB::get(int index) { return data[index]; } /***********************************************************************/ -void SequenceDB::changeSize(int newSize) { - data.resize(newSize); +void SequenceDB::resize(int newSize) { + try { + int size = data.size(); + + for (int i = size; i > newSize; i--) { delete data[i]; } + data.resize(newSize); + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function resize. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function resize. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } } /***********************************************************************/ void SequenceDB::clear() { - data.clear(); + try { + for (int i = 0; i < data.size(); i++) { delete data[i]; } + data.clear(); + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function clear. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } } /***********************************************************************/ @@ -73,22 +217,36 @@ int SequenceDB::size() { /***********************************************************************/ void SequenceDB::print(ostream& out) { - for(int i = 0; i < data.size(); i++) - data[i].printSequence(out); + try { + for(int i = 0; i < data.size(); i++) { + data[i]->printSequence(out); + } + } + catch(exception& e) { + cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } + catch(...) { + cout << "An unknown error has occurred in the SequenceDB class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + exit(1); + } } /***********************************************************************/ -void SequenceDB::add(Sequence newSequence) { +void SequenceDB::push_back(Sequence* newSequence) { try { data.push_back(newSequence); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the RAbundVector class Function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + cout << "Standard Error: " << e.what() << " has occurred in the SequenceDB class Function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; exit(1); } catch(...) { - cout << "An unknown error has occurred in the RAbundVector class function push_back. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + cout << "An unknown error has occurred in the SequenceDB class function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; exit(1); } -} \ No newline at end of file +} + +/***********************************************************************/ +