Commit 096ab901 authored by Mikaël Salson's avatar Mikaël Salson

Fasta: add() method to add other sequences to a Fasta object

The constructor now uses the add() method which, itself, relies on the >> operator which has been overloaded.
Unit tests added for the add() method.
parent 29e32d65
......@@ -38,23 +38,27 @@ Fasta::Fasta(const string &input,
// oout = out;
this -> extract_field = extract_field ;
this -> extract_separator = extract_separator ;
total_size = 0;
ifstream is(input.c_str());
add(input);
out << " <== " << input
<< "\t" << setw(6) << total_size << " bp in " << setw(3) << size() << " sequences" << endl ;
}
void Fasta::add(istream &in) {
in >> *this;
}
void Fasta::add(const string &filename) {
ifstream is(filename.c_str());
if (is.fail())
{
out << " !! Error in opening file: " << input << endl ;
cerr << " !! Error in opening file: " << filename << endl ;
exit(1);
}
out << " <== " << input ;
while (is.good()) {
is >> *this;
}
add(is);
is.close();
out << "\t" << setw(6) << total_size << " bp in " << setw(3) << size() << " sequences" << endl ;
}
int Fasta::size() const{ return (int)reads.size(); }
......@@ -234,7 +238,6 @@ istream& operator>>(istream& in, Fasta& fasta){
string line;
Sequence read;
OnlineFasta of(in, fasta.extract_field, fasta.extract_separator);
fasta.total_size = 0 ;
while (of.hasNext()) {
of.next();
......
......@@ -56,6 +56,9 @@ public:
const string& label_full(int index) const;
const Sequence &read(int index) const;
const string& sequence(int index) const;
void add(istream &in);
void add(const string &filename);
friend istream& operator>>(istream&, Fasta&);
};
......
......@@ -14,6 +14,24 @@ void testFasta1() {
}
}
void testFastaAdd() {
Fasta fa1("../../data/test1.fa");
Fasta fa2("../../data/test1.fa");
fa2.add("../../data/test1.fa");
TAP_TEST(fa1.size() * 2 == fa2.size(), TEST_FASTA_ADD, "");
for (int i=0; i < fa1.size(); i++) {
TAP_TEST(fa1.label(i) == fa2.label(i)
&& fa1.label(i) == fa2.label(i+fa1.size()), TEST_FASTA_ADD, "");
TAP_TEST(fa1.label_full(i) == fa2.label_full(i)
&& fa1.label_full(i) == fa2.label_full(i+fa1.size()),
TEST_FASTA_ADD, "");
TAP_TEST(fa1.sequence(i) == fa2.sequence(i)
&& fa1.sequence(i) == fa2.sequence(i+fa1.size()),
TEST_FASTA_ADD, "");
}
}
void testRevcomp() {
TAP_TEST(complement("AATCAGactgactagATCGAn") == "TTAGTCTGACTGATCTAGCTN", TEST_REVCOMP, "");
TAP_TEST(revcomp("AATCAGactgactagATCGAn") == "NTCGATCTAGTCAGTCTGATT", TEST_REVCOMP, "");
......
......@@ -8,6 +8,7 @@ enum {
TEST_FASTA_LABEL,
TEST_FASTA_LABEL_FULL,
TEST_FASTA_SEQUENCE,
TEST_FASTA_ADD,
TEST_CREATE_SEQUENCE_LABEL_FULL,
TEST_CREATE_SEQUENCE_LABEL,
TEST_CREATE_SEQUENCE_SEQUENCE,
......@@ -104,6 +105,7 @@ inline void declare_tests() {
RECORD_TAP_TEST(TEST_FASTA_LABEL, "Fasta/Fastq label");
RECORD_TAP_TEST(TEST_FASTA_LABEL_FULL, "Fasta/Fastq full label");
RECORD_TAP_TEST(TEST_FASTA_SEQUENCE, "Fasta/Fastq sequence");
RECORD_TAP_TEST(TEST_FASTA_ADD, "Fasta add() method");
RECORD_TAP_TEST(TEST_CREATE_SEQUENCE_LABEL_FULL, "create_sequence: label_full field");
RECORD_TAP_TEST(TEST_CREATE_SEQUENCE_LABEL, "create_sequence: label field");
RECORD_TAP_TEST(TEST_CREATE_SEQUENCE_SEQUENCE, "create_sequence: sequence field");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment