X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=sequencedb.cpp;h=e798ee14ac70837e795a7be3191a14f68788c234;hp=e8aade76284c73749af5799233a1ded3ef90f0a1;hb=a8e2df1b96a57f5f29576b08361b86a96a8eff4f;hpb=c196b6b4768ccb84955d773ff0f22e4994d1ba7b diff --git a/sequencedb.cpp b/sequencedb.cpp index e8aade7..e798ee1 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. * */ @@ -15,17 +15,100 @@ /***********************************************************************/ -SequenceDB::SequenceDB() {} +SequenceDB::SequenceDB() { m = MothurOut::getInstance(); length = 0; samelength = true; } +/***********************************************************************/ +//the clear function free's the memory +SequenceDB::~SequenceDB() { clear(); } /***********************************************************************/ SequenceDB::SequenceDB(int newSize) { - data.resize(newSize); + data.resize(newSize, Sequence()); + length = 0; samelength = true; } /***********************************************************************/ -SequenceDB::SequenceDB(ifstream&) {} +SequenceDB::SequenceDB(ifstream& filehandle) { + try{ + length = 0; samelength = true; + + //read through file + while (!filehandle.eof()) { + //input sequence info into sequencedb + Sequence newSequence(filehandle); + + if (newSequence.getName() != "") { + if (length == 0) { length = newSequence.getAligned().length(); } + if (length != newSequence.getAligned().length()) { samelength = false; } + data.push_back(newSequence); + } + + //takes care of white space + m->gobble(filehandle); + } + + filehandle.close(); + + } + catch(exception& e) { + m->errorOut(e, "SequenceDB", "SequenceDB"); + 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) { + m->errorOut(e, "SequenceDB", "readName"); + 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; } + } + + if (length == 0) { length = sequence.length(); } + if (length != sequence.length()) { samelength = false; } + + return sequence; + } + catch(exception& e) { + m->errorOut(e, "SequenceDB", "readSequence"); + exit(1); + } +} /***********************************************************************/ @@ -36,14 +119,31 @@ int SequenceDB::getNumSeqs() { /***********************************************************************/ void SequenceDB::set(int index, string newUnaligned) { - Sequence newSeq(data[index].getName(), newUnaligned); - data[index] = newSeq; + try { + if (length == 0) { length = newUnaligned.length(); } + if (length != newUnaligned.length()) { samelength = false; } + + data[index] = Sequence(data[index].getName(), newUnaligned); + } + catch(exception& e) { + m->errorOut(e, "SequenceDB", "set"); + exit(1); + } } /***********************************************************************/ void SequenceDB::set(int index, Sequence newSeq) { - data[index] = newSeq; + try { + if (length == 0) { length = newSeq.getAligned().length(); } + if (length != newSeq.getAligned().length()) { samelength = false; } + + data[index] = newSeq; + } + catch(exception& e) { + m->errorOut(e, "SequenceDB", "set"); + exit(1); + } } /***********************************************************************/ @@ -54,14 +154,26 @@ 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) { + m->errorOut(e, "SequenceDB", "resize"); + exit(1); + } } /***********************************************************************/ void SequenceDB::clear() { - data.clear(); + try { + data.clear(); + } + catch(exception& e) { + m->errorOut(e, "SequenceDB", "clear"); + exit(1); + } } /***********************************************************************/ @@ -73,22 +185,31 @@ 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) { + m->errorOut(e, "SequenceDB", "print"); + exit(1); + } } /***********************************************************************/ -void SequenceDB::add(Sequence newSequence) { +void SequenceDB::push_back(Sequence newSequence) { try { + if (length == 0) { length = newSequence.getAligned().length(); } + if (length != newSequence.getAligned().length()) { samelength = false; } + 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"; + m->errorOut(e, "SequenceDB", "push_back"); 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"; - exit(1); - } -} \ No newline at end of file +} + +/***********************************************************************/ +