libsequence  1.9.5
FastaIO.cc
1 #include <Sequence/Fasta.hpp>
2 #include <fstream>
3 #include <iostream>
4 #include <boost/test/unit_test.hpp>
5 #include <unistd.h>
6 #include <iterator>
7 
8 struct fasta_io_fixture
9 {
10  std::string name, seq, seq_left, seq_right;
11  fasta_io_fixture()
12  : name{ "seqname is a seq" }, seq{ "AGCGTAGACAGTAGAGTGAT" },
13  seq_left{ "AGCGTAGAC" }, seq_right{ "AGTAGAGTGAT" }
14  {
15  }
16 };
17 
18 BOOST_FIXTURE_TEST_SUITE(FastaIOTest,fasta_io_fixture)
19 
20 BOOST_AUTO_TEST_CASE(ostream_test)
21 {
22  const char* filename = "fasta_ostream_test_out.fasta";
23  Sequence::Fasta f(name, seq), f2;
24  std::ofstream o(filename);
25  o << f << std::endl;
26  o.close();
27  std::ifstream in(filename);
28  in >> f2 >> std::ws;
29  BOOST_REQUIRE_EQUAL(f, f2);
30  unlink(filename);
31 }
32 
33 BOOST_AUTO_TEST_CASE(ostream_test_3recs)
34 {
35  const char* filename = "fasta_ostream_test_out.fasta";
36  Sequence::Fasta f(name, seq), f2;
37  std::ofstream o(filename);
38  o << f << '\n' << f << '\n' << f << '\n';
39  o.close();
40  std::ifstream in(filename);
41  unsigned count = 0;
42  while (!in.eof())
43  {
44  in >> f2 >> std::ws;
45  BOOST_REQUIRE_EQUAL(f.name, f2.name);
46  BOOST_REQUIRE_EQUAL(f.seq, f2.seq);
47  ++count;
48  }
49  BOOST_REQUIRE_EQUAL(count, 3);
50  //in >> f2 >> std::ws;
51 
52  unlink(filename);
53 }
54 
55 //No newline @ end of data
56 BOOST_AUTO_TEST_CASE(ostream_test_no_newline_end_of_file)
57 {
58  const char* filename = "fasta_ostream_test_out.fasta";
59  Sequence::Fasta f(name, seq), f2;
60  std::ofstream o(filename);
61  o << f << '\n' << f << '\n' << f;
62  o.close();
63  std::ifstream in(filename);
64  unsigned count = 0;
65  while (!in.eof())
66  {
67  in >> f2 >> std::ws;
68  BOOST_REQUIRE_EQUAL(f.name, f2.name);
69  BOOST_REQUIRE_EQUAL(f.seq, f2.seq);
70  ++count;
71  }
72  BOOST_REQUIRE_EQUAL(count, 3);
73  unlink(filename);
74 }
75 
76 BOOST_AUTO_TEST_CASE(ostream_test_newline_within_seq)
77 {
78  const char* filename = "fasta_ostream_test_out.fasta";
79  Sequence::Fasta f(name, seq), f2;
80  std::ofstream o(filename);
81  for (unsigned i = 0; i < 3; ++i)
82  {
83  o << '>' << name << '\n' << seq_left << '\n' << seq_right << '\n';
84  }
85  o.close();
86  std::ifstream in(filename);
87  unsigned count = 0;
88  while (!in.eof())
89  {
90  in >> f2 >> std::ws;
91  BOOST_REQUIRE_EQUAL(f.name, f2.name);
92  BOOST_REQUIRE_EQUAL(f.seq, f2.seq);
93  ++count;
94  }
95  BOOST_REQUIRE_EQUAL(count, 3);
96  unlink(filename);
97 }
98 
99 BOOST_AUTO_TEST_CASE(ostream_test_really_bad_input)
100 {
101  const char* filename = "fasta_ostream_test_out.fasta";
102  Sequence::Fasta f(name, seq), f2;
103  std::ofstream o(filename);
104  for (unsigned i = 0; i < 3; ++i)
105  {
106  o << '>' << name << '\n'
107  << seq_left << "\n\n\n\n\n" //Bad bad bad
108  << seq_right << '\n';
109  }
110  o.close();
111  std::ifstream in(filename);
112  unsigned count = 0;
113  while (!in.eof())
114  {
115  in >> f2 >> std::ws;
116  BOOST_REQUIRE_EQUAL(f.name, f2.name);
117  BOOST_REQUIRE_EQUAL(f.seq, f2.seq);
118  ++count;
119  }
120  BOOST_REQUIRE_EQUAL(count, 3);
121  unlink(filename);
122 }
123 
124 BOOST_AUTO_TEST_CASE(ostream_test_really_bad_input_istream_iterator)
125 {
126  const char* filename = "fasta_ostream_test_out.fasta";
127  Sequence::Fasta f(name, seq), f2;
128  std::ofstream o(filename);
129  for (unsigned i = 0; i < 3; ++i)
130  {
131  o << '>' << name << '\n'
132  << seq_left << "\n\n\n\n\n" //Bad bad bad
133  << seq_right << '\n';
134  }
135  o.close();
136  std::ifstream in(filename);
137  unsigned count = 0;
138  std::istream_iterator<Sequence::Fasta> i(in);
139  for (; i != std::istream_iterator<Sequence::Fasta>(); ++i)
140  {
141  ++count;
142  }
143  BOOST_REQUIRE_EQUAL(count, 3);
144  unlink(filename);
145 }
146 
147 BOOST_AUTO_TEST_CASE(exception_test)
148 {
149  const char* filename = "fasta_ostream_test_out.fasta";
150  std::ofstream out(filename);
151  //Write a badly-formatted FASTA record (we forgot the >)
152  out << name << '\n' << seq;
153  out.close();
154 
155  std::ifstream in(filename);
156  Sequence::Fasta f;
157  BOOST_CHECK_THROW(in >> f >> std::ws, std::runtime_error);
158  unlink(filename);
159 }
160 
161 BOOST_AUTO_TEST_SUITE_END()
162 //EOF
FASTA sequence stream.
Definition: Fasta.hpp:49
Declaration of Sequence::Fasta streams.