Program Listing for File data_matrix.hpp

Return to documentation for file (fwdpp/data_matrix.hpp)

#ifndef FWDPP_DATA_MATRIX_HPP_
#define FWDPP_DATA_MATRIX_HPP_

#include <numeric>
#include <utility>
#include <vector>
#include <type_traits>
#include <cstdint>
#include <algorithm>
#include <stdexcept>
#include <set>
#include <unordered_map>
#include <fwdpp/forward_types.hpp>
#include <fwdpp/type_traits.hpp>
#include <fwdpp/poptypes/tags.hpp>
#include <gsl/gsl_matrix_char.h>

namespace fwdpp
{
    struct state_matrix
    {
        std::vector<std::int8_t> data;
        std::vector<double> positions;
        state_matrix() : data(), positions()
        {
        }
        template <typename T, typename P>
        state_matrix(T &&t, P &&p)
            : data(std::forward<T>(t)), positions(std::forward<P>(p))
        {
        }
    };

    struct data_matrix
    {
        state_matrix neutral;
        state_matrix selected;
        std::vector<std::size_t> neutral_keys;
        std::vector<std::size_t> selected_keys;
        std::size_t ncol;
        explicit data_matrix(const std::size_t ncol_)
            : neutral{}, selected{}, neutral_keys{}, selected_keys{}, ncol{ncol_}
        {
        }
    };
} // namespace fwdpp

// This header contains code re-used for
// implementing functions defined below.
#include "internal/data_matrix_details.hpp"

namespace fwdpp
{
    template <typename poptype>
    std::pair<std::vector<std::pair<std::size_t, uint_t>>,
              std::vector<std::pair<std::size_t, uint_t>>>
    mutation_keys(const poptype &pop, const std::vector<std::size_t> &individuals,
                  const bool include_neutral, const bool include_selected)
    {
        return data_matrix_details::mutation_keys(
            pop.diploids, individuals, pop.haploid_genomes, pop.mcounts, include_neutral,
            include_selected, typename poptype::popmodel_t());
    }

    template <typename MutationContainerType>
    inline void
    sort_keys(const MutationContainerType &mutations,
              std::vector<std::pair<std::size_t, uint_t>> &keys)
    {
        const auto comp = [&mutations](const std::pair<std::size_t, uint_t> &a,
                                       const std::pair<std::size_t, uint_t> &b) {
            return mutations[a.first].pos < mutations[b.first].pos;
        };
        std::sort(keys.begin(), keys.end(), comp);
    }

    template <typename F>
    inline void
    filter_keys(std::vector<std::pair<std::size_t, uint_t>> &keys, F f)
    {
        keys.erase(std::remove_if(keys.begin(), keys.end(), f), keys.end());
    }

    template <typename poptype>
    data_matrix
    genotype_matrix(const poptype &pop, const std::vector<std::size_t> &individuals,
                    const std::vector<std::pair<std::size_t, uint_t>> &neutral_keys,
                    const std::vector<std::pair<std::size_t, uint_t>> &selected_keys)
    {
        return data_matrix_details::fill_matrix(
            pop, individuals, neutral_keys, selected_keys,
            data_matrix_details::matrix_type::genotype);
    }

    template <typename poptype>
    data_matrix
    haplotype_matrix(const poptype &pop, const std::vector<std::size_t> &individuals,
                     const std::vector<std::pair<std::size_t, uint_t>> &neutral_keys,
                     const std::vector<std::pair<std::size_t, uint_t>> &selected_keys)
    {
        return data_matrix_details::fill_matrix(
            pop, individuals, neutral_keys, selected_keys,
            data_matrix_details::matrix_type::haplotype);
    }

    inline std::pair<std::vector<std::uint32_t>, std::vector<std::uint32_t>>
    row_sums(const data_matrix &m)
    {
        return std::make_pair(
            data_matrix_details::row_col_sums_details(
                m.neutral.data, m.neutral.positions.size(), m.ncol, true),
            data_matrix_details::row_col_sums_details(
                m.selected.data, m.selected.positions.size(), m.ncol, true));
    }

    inline std::pair<std::vector<std::uint32_t>, std::vector<std::uint32_t>>
    col_sums(const data_matrix &m)
    {
        return std::make_pair(
            data_matrix_details::row_col_sums_details(
                m.neutral.data, m.neutral.positions.size(), m.ncol, false),
            data_matrix_details::row_col_sums_details(
                m.selected.data, m.selected.positions.size(), m.ncol, false));
    }
} // namespace fwdpp

#endif