3 #include <Sequence/VariantMatrix.hpp>     4 #include <Sequence/AlleleCountMatrix.hpp>     5 #include <Sequence/VariantMatrixViews.hpp>     6 #include <Sequence/variant_matrix/windows.hpp>     7 #include <boost/test/unit_test.hpp>    11 #include "VariantMatrixFixture.hpp"    13 struct vmatrix_fixture
    15     std::vector<std::int8_t> input_data;
    16     std::vector<double> input_pos;
    20         : input_data(make_input_data()), input_pos(make_intput_pos()),
    21           m(input_data, input_pos), m2(input_data, input_pos), c{ m }, c2{ m2 }
    31     std::vector<std::int8_t>
    36         std::vector<std::int8_t> rv;
    37         for (
int i = 0; i < nsites; ++i)
    39                 for (
int j = 0; j < nsam; ++j)
    41                         std::int8_t state = (j % 2 == 0.) ? 1 : 0;
    51         std::vector<double> rv;
    53         std::iota(rv.begin(), rv.end(), 0.);
    58 BOOST_AUTO_TEST_SUITE(BasicVariantMatrixTests)
    60 BOOST_AUTO_TEST_CASE(construct_empty_VariantMatrix_from_move)
    62     std::vector<std::int8_t> d{};
    63     std::vector<double> p{};
    65     BOOST_REQUIRE_EQUAL(m.
nsam, 0);
    66     BOOST_REQUIRE_EQUAL(m.
nsites, 0);
    69 BOOST_AUTO_TEST_CASE(construct_empty_VariantMatrix_from_init_lists)
    72                               std::vector<double>{});
    73     BOOST_REQUIRE_EQUAL(m.
nsam, 0);
    74     BOOST_REQUIRE_EQUAL(m.
nsites, 0);
    77 BOOST_AUTO_TEST_SUITE_END()
    79 BOOST_FIXTURE_TEST_SUITE(VariantMatrixTest, vmatrix_fixture)
    81 BOOST_AUTO_TEST_CASE(test_construction)
    84                               std::vector<double>(5, 0.0));
    85     BOOST_REQUIRE_EQUAL(m.
nsites, 5);
    86     BOOST_REQUIRE_EQUAL(m.
nsam, 20);
    90 BOOST_AUTO_TEST_CASE(test_max_allele)
    94     BOOST_CHECK_EQUAL(vm.max_allele, 5);
    96     BOOST_REQUIRE_EQUAL(vmc.ncol, 6);
    99 BOOST_AUTO_TEST_CASE(test_range_exceptions)
   101     BOOST_REQUIRE_THROW(m.
at(m.
nsites + 1, 0), std::out_of_range);
   102     BOOST_REQUIRE_THROW(m.
at(0, m.
nsam + 1), std::out_of_range);
   105 BOOST_AUTO_TEST_CASE(test_iteration)
   107     for (std::size_t i = 0; i < m.
nsam; ++i)
   109             for (std::size_t j = 0; j < m.
nsites; ++j)
   111                     auto x = m.
get(j, i);
   112                     std::int8_t ex = (i % 2 == 0.) ? 1 : 0;
   113                     BOOST_REQUIRE_EQUAL(static_cast<int>(x),
   114                                         static_cast<int>(ex));
   119 BOOST_AUTO_TEST_CASE(test_bad_row_swap)
   123     BOOST_REQUIRE_THROW(swap(a, b), std::invalid_argument);
   126 BOOST_AUTO_TEST_CASE(test_bad_column_swap)
   130     BOOST_REQUIRE_THROW(swap(a, b), std::invalid_argument);
   133 BOOST_AUTO_TEST_CASE(test_row_views)
   135     for (std::size_t i = 0; i < m.
nsites; ++i)
   138             for (
auto j = x.begin(); j != x.end(); ++j)
   141                         = (std::distance(x.begin(), j) % 2 == 0.0) ? 1 : 0;
   142                     BOOST_REQUIRE_EQUAL(static_cast<int>(*j),
   143                                         static_cast<int>(ex));
   145             for (
auto j = x.cbegin(); j != x.cend(); ++j)
   148                         = (std::distance(x.cbegin(), j) % 2 == 0.0) ? 1 : 0;
   149                     BOOST_REQUIRE_EQUAL(static_cast<int>(*j),
   150                                         static_cast<int>(ex));
   152             for (
auto j = std::begin(x); j != std::end(x); ++j)
   155                         = (std::distance(x.begin(), j) % 2 == 0.0) ? 1 : 0;
   156                     BOOST_REQUIRE_EQUAL(static_cast<int>(*j),
   157                                         static_cast<int>(ex));
   159             for (std::size_t j = 0; j < x.size(); ++j)
   161                     std::int8_t ex = (j % 2 == 0.) ? 1 : 0;
   162                     BOOST_REQUIRE_EQUAL(static_cast<int>(x[j]),
   163                                         static_cast<int>(ex));
   168                     std::int8_t ex = (j % 2 == 0.) ? 1 : 0;
   169                     BOOST_REQUIRE_EQUAL(static_cast<int>(xj),
   170                                         static_cast<int>(ex));
   176 BOOST_AUTO_TEST_CASE(test_const_row_views)
   178     for (std::size_t i = 0; i < m.
nsites; ++i)
   182             for (
auto j = x.begin(); j != x.end(); ++j)
   185                         = (std::distance(x.begin(), j) % 2 == 0.0) ? 1 : 0;
   186                     BOOST_REQUIRE_EQUAL(static_cast<int>(*j),
   187                                         static_cast<int>(ex));
   189             for (
auto j = x.cbegin(); j != x.cend(); ++j)
   192                         = (std::distance(x.cbegin(), j) % 2 == 0.0) ? 1 : 0;
   193                     BOOST_REQUIRE_EQUAL(static_cast<int>(*j),
   194                                         static_cast<int>(ex));
   196             for (
auto j = std::begin(x); j != std::end(x); ++j)
   199                         = (std::distance(x.begin(), j) % 2 == 0.0) ? 1 : 0;
   200                     BOOST_REQUIRE_EQUAL(static_cast<int>(*j),
   201                                         static_cast<int>(ex));
   203             for (std::size_t j = 0; j < x.size(); ++j)
   205                     std::int8_t ex = (j % 2 == 0.) ? 1 : 0;
   206                     BOOST_REQUIRE_EQUAL(static_cast<int>(x[j]),
   207                                         static_cast<int>(ex));
   212                     std::int8_t ex = (j % 2 == 0.) ? 1 : 0;
   213                     BOOST_REQUIRE_EQUAL(static_cast<int>(xj),
   214                                         static_cast<int>(ex));
   220 BOOST_AUTO_TEST_CASE(test_row_view_exceptions)
   228     BOOST_REQUIRE_THROW(r.at(m.
nsam + 1), std::exception);
   229     BOOST_REQUIRE_THROW(r.at(m.
nsam + 1), std::out_of_range);
   232 BOOST_AUTO_TEST_CASE(test_const_row_view_exceptions)
   240     BOOST_REQUIRE_THROW(r.at(m.
nsam + 1), std::exception);
   241     BOOST_REQUIRE_THROW(r.at(m.
nsam + 1), std::out_of_range);
   244 BOOST_AUTO_TEST_CASE(test_row_view_iterators)
   246     for (std::size_t i = 0; i < m.
nsites; ++i)
   249             BOOST_REQUIRE_EQUAL(std::distance(row.begin(), row.end()), m.
nsam);
   250             BOOST_REQUIRE_EQUAL(std::distance(row.cbegin(), row.cend()),
   255 BOOST_AUTO_TEST_CASE(test_column_views)
   257     for (std::size_t i = 0; i < m.
nsam; ++i)
   260             std::int8_t state = (i % 2 == 0) ? 1 : 0;
   262                 std::count(std::begin(col), std::end(col), !state), 0);
   263             BOOST_REQUIRE_EQUAL(std::count(col.rbegin(), col.rend(), !state),
   265             BOOST_REQUIRE_EQUAL(std::count(col.cbegin(), col.cend(), !state),
   267             BOOST_REQUIRE_EQUAL(std::count(col.crbegin(), col.crend(), !state),
   270             BOOST_REQUIRE_EQUAL(std::distance(std::begin(col), std::end(col)),
   272             BOOST_REQUIRE_EQUAL(std::distance(col.rbegin(), col.rend()),
   277             auto fwd = col.begin();
   278             auto rev = col.rbegin();
   279             for (; rev < col.rend(); ++rev)
   281                     auto rf = std::distance(fwd, rev.base());
   282                     auto rb = std::distance(rev, col.rend());
   283                     BOOST_REQUIRE_EQUAL(rf, rb);
   286             auto cfwd = col.cbegin();
   287             auto crev = col.crbegin();
   288             for (; crev < col.crend(); ++crev)
   290                     auto rf = std::distance(cfwd, crev.base());
   291                     auto rb = std::distance(crev, col.crend());
   292                     BOOST_REQUIRE_EQUAL(rf, rb);
   297 BOOST_AUTO_TEST_CASE(tesl_col_view_iterator_increment)
   301     unsigned num_increments = 0;
   307     BOOST_REQUIRE_EQUAL(num_increments, 3);
   310 BOOST_AUTO_TEST_CASE(test_column_view_invalid_compare)
   314     BOOST_REQUIRE_NO_THROW(std::distance(c0.begin(), c0.end()));
   315     BOOST_REQUIRE_THROW(std::distance(c0.begin(), c1.begin()),
   316                         std::invalid_argument);
   322 BOOST_AUTO_TEST_CASE(test_accumulate)
   325     int sum = 
static_cast<int>(std::accumulate(c.cbegin(), c.cend(), 0));
   326     BOOST_REQUIRE_EQUAL(sum, static_cast<int>(m.
nsites));
   328     sum = 
static_cast<int>(std::accumulate(c.cbegin(), c.cend(), 0));
   329     BOOST_REQUIRE_EQUAL(sum, 0);
   331 BOOST_AUTO_TEST_SUITE_END()
   333 BOOST_FIXTURE_TEST_SUITE(VariantMatrixWindowTest, dataset)
   335 BOOST_AUTO_TEST_CASE(tests_windows_size_0)
   338     BOOST_REQUIRE_EQUAL(w.nsam, 0);
   339     BOOST_REQUIRE_EQUAL(w.nsites, 0);
   342 BOOST_AUTO_TEST_CASE(tests_windows_size_1)
   344     for (std::size_t i = 0; i < m.
positions.size(); ++i)
   347             BOOST_REQUIRE_EQUAL(w.positions[0], m.
positions[i]);
   350                 std::mismatch(w.data.begin(), w.data.end(), r.begin()).first
   356 BOOST_AUTO_TEST_CASE(test_windows_multi_site)
   359     BOOST_REQUIRE_EQUAL(w.nsites, 1);
   360     BOOST_REQUIRE_EQUAL(w.positions[0], 0.2);
   363 BOOST_AUTO_TEST_SUITE_END()
   365 BOOST_FIXTURE_TEST_SUITE(test_AlleleCountMatrixOperations, dataset)
   367 BOOST_AUTO_TEST_CASE(test_slice)
   373     for (std::size_t i = 0; i < 2; ++i)
   376             c.insert(c.end(), r.first, r.second);
   379     BOOST_REQUIRE_EQUAL(amw.counts == amslice.counts, 
true);
   380     BOOST_REQUIRE_EQUAL(amw.ncol, amslice.ncol);
   381     BOOST_REQUIRE_EQUAL(amw.nrow, amslice.nrow);
   382     BOOST_REQUIRE_EQUAL(amw.nsam, amslice.nsam);
   385 BOOST_AUTO_TEST_SUITE_END()
 const std::int8_t max_allele
Max allelic value stored in matrix. 
 
VariantMatrix make_window(const VariantMatrix &m, const double beg, const double end)
Return a window from a VariantMatrix. 
 
std::int8_t & at(const std::size_t site, const std::size_t haplotype)
Get data from marker site and haplotype haplotype. std::out_of_range is thrown if indexes are invalid...
 
ConstRowView get_RowView(const VariantMatrix &m, const std::size_t row)
Return a ConstRowView from VariantMatrix m at row row. std::out_of_range is thrown if row is out of r...
 
std::size_t nsites
Number of sites in data set. 
 
std::vector< double > positions
Position of sites. 
 
ConstRowView get_ConstRowView(const VariantMatrix &m, const std::size_t row)
Return a ConstRowView from VariantMatrix m at row row. std::out_of_range is thrown if row is out of r...
 
Matrix representation of variation data. 
 
ColView get_ColView(VariantMatrix &m, const std::size_t col)
Return a ColView from VariantMatrix m at col col. std::out_of_range is thcoln if col is out of range...
 
std::size_t nsam
Sample size of data set. 
 
std::int8_t & get(const std::size_t site, const std::size_t haplotype)
Get data from marker site and haplotype haplotype. No range-checking is done. 
 
ConstColView get_ConstColView(VariantMatrix &m, const std::size_t col)
Return a ConstColView from VariantMatrix m at col col. std::out_of_range is thcoln if col is out of r...
 
std::vector< std::int8_t > data
Data stored in matrix form with rows as sites. 
 
Matrix representation of allele counts in a VariantMatrix To be constructed.