5 * Created by Thomas Ryabin on 4/13/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sequencedb.h"
11 #include "sequence.hpp"
13 #include "calculator.h"
16 /***********************************************************************/
18 SequenceDB::SequenceDB() { m = MothurOut::getInstance(); length = 0; samelength = true; }
19 /***********************************************************************/
20 //the clear function free's the memory
21 SequenceDB::~SequenceDB() { clear(); }
23 /***********************************************************************/
25 SequenceDB::SequenceDB(int newSize) {
26 data.resize(newSize, Sequence());
27 length = 0; samelength = true;
30 /***********************************************************************/
32 SequenceDB::SequenceDB(ifstream& filehandle) {
34 length = 0; samelength = true;
37 while (!filehandle.eof()) {
38 //input sequence info into sequencedb
39 Sequence newSequence(filehandle);
41 if (newSequence.getName() != "") {
42 if (length == 0) { length = newSequence.getAligned().length(); }
43 if (length != newSequence.getAligned().length()) { samelength = false; }
44 data.push_back(newSequence);
47 //takes care of white space
48 m->gobble(filehandle);
55 m->errorOut(e, "SequenceDB", "SequenceDB");
59 /*******************************************************************************/
60 string SequenceDB::readName(ifstream& in) {
66 while ((c = in.get()) != EOF) {
67 //if c is not a line return
76 m->errorOut(e, "SequenceDB", "readName");
81 /*******************************************************************************/
82 string SequenceDB::readSequence(ifstream& in) {
89 //save position in file in case next line is a new name.
93 //if you are at a new name
95 //put file pointer back since you are now at a new name
96 in.seekg(pos, ios::beg);
97 c = in.get(); //because you put it back to a newline char
99 }else { sequence += line; }
102 if (length == 0) { length = sequence.length(); }
103 if (length != sequence.length()) { samelength = false; }
107 catch(exception& e) {
108 m->errorOut(e, "SequenceDB", "readSequence");
113 /***********************************************************************/
115 int SequenceDB::getNumSeqs() {
119 /***********************************************************************/
121 void SequenceDB::set(int index, string newUnaligned) {
123 if (length == 0) { length = newUnaligned.length(); }
124 if (length != newUnaligned.length()) { samelength = false; }
126 data[index] = Sequence(data[index].getName(), newUnaligned);
128 catch(exception& e) {
129 m->errorOut(e, "SequenceDB", "set");
134 /***********************************************************************/
136 void SequenceDB::set(int index, Sequence newSeq) {
138 if (length == 0) { length = newSeq.getAligned().length(); }
139 if (length != newSeq.getAligned().length()) { samelength = false; }
141 data[index] = newSeq;
143 catch(exception& e) {
144 m->errorOut(e, "SequenceDB", "set");
149 /***********************************************************************/
151 Sequence SequenceDB::get(int index) {
155 /***********************************************************************/
157 void SequenceDB::resize(int newSize) {
159 data.resize(newSize);
161 catch(exception& e) {
162 m->errorOut(e, "SequenceDB", "resize");
167 /***********************************************************************/
169 void SequenceDB::clear() {
173 catch(exception& e) {
174 m->errorOut(e, "SequenceDB", "clear");
179 /***********************************************************************/
181 int SequenceDB::size() {
185 /***********************************************************************/
187 void SequenceDB::print(ostream& out) {
189 for(int i = 0; i < data.size(); i++) {
190 data[i].printSequence(out);
193 catch(exception& e) {
194 m->errorOut(e, "SequenceDB", "print");
199 /***********************************************************************/
201 void SequenceDB::push_back(Sequence newSequence) {
203 if (length == 0) { length = newSequence.getAligned().length(); }
204 if (length != newSequence.getAligned().length()) { samelength = false; }
206 data.push_back(newSequence);
208 catch(exception& e) {
209 m->errorOut(e, "SequenceDB", "push_back");
214 /***********************************************************************/