libsequence  1.9.5
PolyTableBadBehavior.cc
Go to the documentation of this file.
1 
10 #include <Sequence/PolySites.hpp>
11 #include <Sequence/Fasta.hpp>
12 #include <Sequence/Alignment.hpp>
15 #include <boost/test/unit_test.hpp>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <cctype>
19 #include <iostream>
20 #include <functional>
21 
22 BOOST_AUTO_TEST_SUITE(PolyTableBadBehaviorTest)
23 
24 BOOST_AUTO_TEST_CASE( exception1 )
25 {
26  std::vector<double> pos = {1,2,3,4,5};
27  std::vector<std::string> data = {"AAAAA",
28  "AAGAA",
29  "CTGAA",
30  "NAAC"}; //Sequence not same length as pos.size()
31  BOOST_REQUIRE_THROW(Sequence::PolySites ps(std::move(pos),std::move(data)), std::runtime_error );
32 }
33 
34 // This test became moot in 1.8.9
35 // BOOST_AUTO_TEST_CASE( badness1 )
36 // {
37 // std::vector<double> pos = {1,2,3,4,5};
38 // std::vector<std::string> data = {"AAAAA",
39 // "AAGAA",
40 // "CTGAA",
41 // "NAACT"};
42 
43 // Sequence::PolySites ps(std::move(pos),std::move(data)),
44 // ps2(Sequence::copyPolyTable(ps));
45 
46 // //force the calculation of const_site_iterator's underlying data
47 // std::for_each( ps.sbegin(),ps.send(),[]( const Sequence::PolySites::const_site_iterator::value_type & __p ) {} );
48 
49 // //Now, remove that haplo with missing data
50 // //by skipping PolyTable's iterator functions (this is BAD)
51 // ps.second.erase( std::remove_if(ps.second.begin(),
52 // ps.second.end(),
53 // [](const std::string & __s) {
54 // return __s.find('N') != std::string::npos;
55 // }),
56 // ps.second.end() );
57 
58 // /*
59 // Crap!
60 // The underlying data did not get reset...
61 // */
62 // BOOST_CHECK_EQUAL( ps.sbegin()->second,"AACN" );
63 
64 // //This will reset the data, as it calls PolyTable::begin in a non-const context
65 // auto i = ps.begin();
66 // BOOST_CHECK_EQUAL( ps.sbegin()->second,"AAC" );
67 // }
68 
69 //Moot in 1.8.9
70 //Do the above, but correctly this time
71 // BOOST_AUTO_TEST_CASE( badness1_fix1 )
72 // {
73 // std::vector<double> pos = {1,2,3,4,5};
74 // std::vector<std::string> data = {"AAAAA",
75 // "AAGAA",
76 // "CTGAA",
77 // "NAACT"};
78 
79 // Sequence::PolySites ps(std::move(pos),std::move(data)),
80 // ps2(Sequence::copyPolyTable(ps));
81 
82 // //force the calculation of const_site_iterator's underlying data
83 // std::for_each( ps.sbegin(),ps.send(),[]( const Sequence::PolySites::const_site_iterator::value_type & __p ) {} );
84 
85 // //call PolyTable::begin and end
86 // ps.second.erase( std::remove_if(ps.begin(),
87 // ps.end(),
88 // [](const std::string & __s) {
89 // return __s.find('N') != std::string::npos;
90 // }),
91 // ps.end() );
92 // BOOST_CHECK_EQUAL( ps.sbegin()->second,"AAC" );
93 // }
94 
95 //Moot in 1.8.9
96 // BOOST_AUTO_TEST_CASE( badness1_fix2 )
97 // {
98 // std::vector<double> pos = {1,2,3,4,5};
99 // std::vector<std::string> data = {"AAAAA",
100 // "AAGAA",
101 // "CTGAA",
102 // "NAACT"};
103 
104 // Sequence::PolySites ps(std::move(pos),std::move(data)),
105 // ps2(Sequence::copyPolyTable(ps));
106 
107 // //force the calculation of const_site_iterator's underlying data
108 // std::for_each( ps.sbegin(),ps.send(),[]( const Sequence::PolySites::const_site_iterator::value_type & __p ) {} );
109 
110 // //uh-oh, remove_if is not using PolyTable's functions!
111 // ps.second.erase( std::remove_if(ps.second.begin(),
112 // ps.second.end(),
113 // [](const std::string & __s) {
114 // return __s.find('N') != std::string::npos;
115 // }),
116 // ps.end() ); //but, we call PolyTable::end here, and the context is non-const, so the next call to sbegin will be fine.
117 // BOOST_CHECK_EQUAL( ps.sbegin()->second,"AAC" );
118 // }
119 
120 BOOST_AUTO_TEST_CASE( badness2 )
121 {
122  //std::vector<double> pos = {1,2,3,4,5};
123  /*
124  std::vector<std::string> data = {"AAAAA",
125  "AAGAA",
126  "CTGAA",
127  "NAACT"};
128  */
129  std::vector<double> pos(1000,1.);
130  std::vector<std::string> data(1000,std::string(1000,'A'));
131  Sequence::PolySites ps(std::move(pos),std::move(data)),ps2;
132 
133  //You now have a table with unequal haplotype lengths
134  //This is bad, and is user error.
135  ps[0] = std::string("A");
136 
137  //Now, your internal data fail the check that all elements
138  //are the same length, which may have unintended consequences
139  //for later actions.
140  BOOST_CHECK_EQUAL(Sequence::Alignment::IsAlignment(std::vector<std::string>(ps.begin(),ps.end())), false);
141 }
142 BOOST_AUTO_TEST_SUITE_END()
Polymorphism tables for sequence data.
Definition: PolySites.hpp:33
Declaration of namespace Sequence::Alignment.
Site-major variation tables in ASCII format.
Sequence::PolySites, generates polymorphism tables from data.
Declaration of Sequence::Fasta streams.
Operations on non-const Sequence::PolyTable objects.