X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=sequencedb.cpp;h=bc8523aa78b5d217fd5cfd12c8bade6bbb630721;hb=cd37904452dc95b183ff313ff05720c562902487;hp=e8aade76284c73749af5799233a1ded3ef90f0a1;hpb=c196b6b4768ccb84955d773ff0f22e4994d1ba7b;p=mothur.git diff --git a/sequencedb.cpp b/sequencedb.cpp index e8aade7..bc8523a 100644 --- a/sequencedb.cpp +++ b/sequencedb.cpp @@ -3,7 +3,7 @@ * Mothur * * Created by Thomas Ryabin on 4/13/09. - * Copyright 2009 __MyCompanyName__. All rights reserved. + * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved. * */ @@ -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, Sequence()); } /***********************************************************************/ -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(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,14 +133,33 @@ int SequenceDB::getNumSeqs() { /***********************************************************************/ void SequenceDB::set(int index, string newUnaligned) { - Sequence newSeq(data[index].getName(), newUnaligned); - data[index] = newSeq; + try { + data[index] = Sequence(data[index].getName(), newUnaligned); + } + 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; + try { + 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); + } } /***********************************************************************/ @@ -54,14 +170,34 @@ Sequence SequenceDB::get(int index) { /***********************************************************************/ -void SequenceDB::changeSize(int newSize) { - data.resize(newSize); +void SequenceDB::resize(int newSize) { + try { + 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 { + 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 +209,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 +} + +/***********************************************************************/ +