libsequence  1.9.5
AlignStreamTest.cc
Go to the documentation of this file.
1 
3 #include <Sequence/Fasta.hpp>
4 #include <Sequence/Clustalw.hpp>
6 #include <boost/test/unit_test.hpp>
7 #include <fstream>
8 #include <iostream>
9 #include <unistd.h>
10 
11 struct infilenames
12 {
13  const char* input_data;
14  const char* phylip_input_data;
15  infilenames()
16  : input_data{ "data/CG15644-Z.aln" }, phylip_input_data{
17  "data/phylip_input.txt"
18  }
19  {
20  }
21 };
22 
25 
26 BOOST_FIXTURE_TEST_SUITE(AlignStreamTest, infilenames)
27 
28 //Test object construction
29 BOOST_AUTO_TEST_CASE(from_vector1)
30 {
31  std::vector<Sequence::Fasta> vf
32  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
33 
34  BOOST_REQUIRE_NO_THROW(clustal c(vf);
35  BOOST_CHECK_EQUAL(vf.size(), c.size()););
36 }
37 
38 BOOST_AUTO_TEST_CASE(from_vector2)
39 {
40  std::vector<Sequence::Fasta> vf
41  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
42 
43  BOOST_REQUIRE_NO_THROW(phylip c(vf);
44  BOOST_CHECK_EQUAL(vf.size(), c.size()););
45 }
46 
47 BOOST_AUTO_TEST_CASE(from_vector3)
48 {
49  std::vector<Sequence::Fasta> vf
50  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
51  BOOST_REQUIRE_NO_THROW(clustal c(std::move(vf)); BOOST_CHECK(vf.empty());
52  BOOST_REQUIRE_EQUAL(c.size(), 2););
53 }
54 
55 BOOST_AUTO_TEST_CASE(from_vector4)
56 {
57  std::vector<Sequence::Fasta> vf
58  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
59  BOOST_REQUIRE_NO_THROW(phylip c(std::move(vf)); BOOST_CHECK(vf.empty());
60  BOOST_REQUIRE_EQUAL(c.size(), 2););
61 }
62 
63 BOOST_AUTO_TEST_CASE(copy_construct1)
64 {
65  std::vector<Sequence::Fasta> vf
66  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
67  clustal c(std::move(vf));
68  BOOST_REQUIRE_NO_THROW(clustal c2(c);
69  BOOST_REQUIRE_EQUAL(c.size(), c2.size());
70  for (decltype(c.size()) i = 0; i < c.size();
71  ++i) { BOOST_REQUIRE(c[i] == c2[i]); });
72 }
73 
74 BOOST_AUTO_TEST_CASE(copy_construct2)
75 {
76  std::vector<Sequence::Fasta> vf
77  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
78  phylip c(std::move(vf));
79  BOOST_REQUIRE_NO_THROW(phylip c2(c);
80  BOOST_REQUIRE_EQUAL(c.size(), c2.size());
81  for (decltype(c.size()) i = 0; i < c.size();
82  ++i) { BOOST_REQUIRE(c[i] == c2[i]); });
83 }
84 
85 //Check that forwarding is working.
86 BOOST_AUTO_TEST_CASE(move_construct1)
87 {
88  std::vector<Sequence::Fasta> vf
89  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
90  clustal c(std::move(vf));
91  BOOST_CHECK_EQUAL(vf.size(), 0);
92  clustal c2(std::move(c));
93  BOOST_REQUIRE_EQUAL(c2.size(), 2);
94  BOOST_CHECK_EQUAL(c.size(), 0);
95 }
96 
97 //Check that forwarding is working
98 BOOST_AUTO_TEST_CASE(move_construct2)
99 {
100  std::vector<Sequence::Fasta> vf
101  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
102  phylip c(std::move(vf));
103  BOOST_CHECK_EQUAL(vf.size(), 0);
104  phylip c2(std::move(c));
105  BOOST_REQUIRE_EQUAL(c2.size(), 2);
106  BOOST_CHECK_EQUAL(c.size(), 0);
107 }
108 
109 //Check that forwarding works b/w types
110 BOOST_AUTO_TEST_CASE(move_construct3)
111 {
112  std::vector<Sequence::Fasta> vf
113  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
114  clustal c(std::move(vf));
115  BOOST_CHECK_EQUAL(vf.size(), 0);
116  phylip c2(std::move(c));
117  BOOST_REQUIRE_EQUAL(c2.size(), 2);
118  BOOST_CHECK_EQUAL(c.size(), 0);
119 }
120 
121 //Opposite direction to previous test
122 BOOST_AUTO_TEST_CASE(move_construct4)
123 {
124  std::vector<Sequence::Fasta> vf
125  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
126  phylip c(std::move(vf));
127  BOOST_CHECK_EQUAL(vf.size(), 0);
128  clustal c2(std::move(c));
129  BOOST_REQUIRE_EQUAL(c2.size(), 2);
130  BOOST_CHECK_EQUAL(c.size(), 0);
131 }
132 
133 BOOST_AUTO_TEST_CASE(should_throw1)
134 {
135  //Sequences of unequal length = bad mojo
136  std::vector<Sequence::Fasta> vf
137  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATCAA") };
138 
139  BOOST_REQUIRE_THROW(clustal c(vf), std::runtime_error);
140  BOOST_REQUIRE_THROW(phylip c(vf), std::runtime_error);
141  BOOST_REQUIRE_THROW(clustal c(std::vector<Sequence::Fasta>(
142  { Sequence::Fasta("seq1", "ATG"),
143  Sequence::Fasta("seq2", "ATCAA") })),
144  std::runtime_error);
145  BOOST_REQUIRE_THROW(clustal c(std::move(std::vector<Sequence::Fasta>(
146  { Sequence::Fasta("seq1", "ATG"),
147  Sequence::Fasta("seq2", "ATCAA") }))),
148  std::runtime_error);
149 }
150 
151 BOOST_AUTO_TEST_CASE(should_throw2)
152 {
153  //The data are good
154  std::vector<Sequence::Fasta> vf
155  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATC") };
156  clustal c(std::move(vf));
157 
158  //The user does domething dumb...
159  c[1].seq += std::string("ATGC");
160 
161  //The data are not valid
162  BOOST_REQUIRE_EQUAL(c.IsAlignment(), false);
163 
164  //And the user did not check and then tries to copy
165  BOOST_REQUIRE_THROW(clustal c2(c), std::runtime_error);
166 }
167 
168 BOOST_AUTO_TEST_CASE(should_throw3)
169 {
170  //Sequences of unequal length = bad mojo
171  std::vector<Sequence::Fasta> vf
172  = { Sequence::Fasta("seq1", "ATG"), Sequence::Fasta("seq2", "ATCAA") };
173  clustal c;
174  BOOST_REQUIRE_THROW(c.assign(vf.begin(), vf.end()), std::runtime_error);
175  //And make sure it works with c++11-style iterator fxns
176  BOOST_REQUIRE_THROW(c.assign(vf.cbegin(), vf.cend()), std::runtime_error);
177 }
178 
179 //Test IO routines
180 
181 //Simple reading clustalw
182 BOOST_AUTO_TEST_CASE(clustalw_in)
183 {
184  std::ifstream in(input_data);
185  std::string check;
186  clustal c;
187  BOOST_REQUIRE_NO_THROW(in >> c >> std::ws;);
188 
189  BOOST_REQUIRE(!c.empty());
190  BOOST_REQUIRE_EQUAL(c.size(), 10);
191 }
192 
193 //simple reading phylip
194 BOOST_AUTO_TEST_CASE(phylip_in)
195 {
196  std::ifstream in(phylip_input_data);
197  phylip p;
198  BOOST_REQUIRE_NO_THROW(in >> p >> std::ws;);
199 }
200 
201 BOOST_AUTO_TEST_CASE(clustalw_convert)
202 {
203  std::ifstream in(input_data);
204  std::string check;
205  clustal c;
206  BOOST_REQUIRE_NO_THROW(in >> c >> std::ws;);
207 
208  BOOST_REQUIRE(!c.empty());
209  BOOST_REQUIRE_EQUAL(c.size(), 10);
210 
211  //Copy construction
212  phylip p(c);
213 
214  BOOST_REQUIRE(!p.empty());
215  BOOST_REQUIRE_EQUAL(c.size(), p.size());
216  for (decltype(c.size()) i = 0; i < c.size(); ++i)
217  {
218  BOOST_REQUIRE_EQUAL(c[i], p[i]);
219  }
220 }
221 
222 BOOST_AUTO_TEST_CASE(clustalw_convert2)
223 {
224  std::ifstream in(input_data);
225  std::string check;
226  clustal c;
227  BOOST_REQUIRE_NO_THROW(in >> c >> std::ws;);
228 
229  BOOST_REQUIRE(!c.empty());
230  BOOST_REQUIRE_EQUAL(c.size(), 10);
231 
232  //Use assignment operator this time
233  phylip p = c;
234 
235  BOOST_REQUIRE(!p.empty());
236  BOOST_REQUIRE_EQUAL(c.size(), p.size());
237  for (decltype(c.size()) i = 0; i < c.size(); ++i)
238  {
239  BOOST_REQUIRE(c[i] == p[i]);
240  }
241 }
242 
243 BOOST_AUTO_TEST_CASE(clustalw_reread)
244 {
245  std::ifstream in(input_data);
246  std::string check;
247  clustal c;
248  in >> c >> std::ws;
249  in.close();
250 
251  BOOST_REQUIRE(!c.empty());
252  BOOST_REQUIRE_EQUAL(c.size(), 10);
253 
254  const char* outfile = "clustalw_out_test.aln";
255  std::ofstream out(outfile);
256  out << c << '\n';
257  out.close();
258 
259  in.open(outfile);
260  clustal c2;
261  in >> c2 >> std::ws;
262 
263  BOOST_REQUIRE(!c2.empty());
264  BOOST_REQUIRE_EQUAL(c.size(), c2.size());
265 
266  for (decltype(c.size()) i = 0; i < c.size(); ++i)
267  {
268  BOOST_REQUIRE(c[i] == c2[i]);
269  }
270  unlink(outfile);
271 }
272 
273 BOOST_AUTO_TEST_CASE(convert_write_read)
274 {
275  std::ifstream in(input_data);
276  std::string check;
277  clustal c;
278  in >> c >> std::ws;
279  in.close();
280  phylip p = c;
281 
282  const char* outfile = "phylip_test.txt";
283  std::ofstream out(outfile);
284  BOOST_REQUIRE_NO_THROW(out << p << '\n';);
285  out.close();
286 
287  in.open(outfile);
288 
289  phylip p2;
290  BOOST_REQUIRE_NO_THROW(in >> p2 >> std::ws);
291  BOOST_REQUIRE_EQUAL(p.size(), p2.size());
292 
293  for (decltype(p.size()) i = 0; i < p.size(); ++i)
294  {
295  //Note that Sequence::Seq::operator==
296  //only compares the sequences, not the
297  //names, so this must still pass
298  BOOST_REQUIRE(p[i] == p2[i]);
299  }
300  unlink(outfile);
301 }
302 
303 BOOST_AUTO_TEST_SUITE_END()
304 //EOF
FASTA sequence stream.
Definition: Fasta.hpp:49
Sequence::phylipData – read in phylip alignments.
size_type size(void) const
Definition: AlignStream.hpp:93
Declaration of Sequence::Fasta streams.
ClustalW streams.
Definition: Clustalw.hpp:77
Declaration of Sequence::ClustalW streams.
void assign(const_iterator beg, const_iterator end)