Skip to content

Commit

Permalink
copy fwdpp type into fwdpy11 headers
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Jul 4, 2024
1 parent bee37c2 commit 334c027
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <type_traits>
#include <functional>
#include "../DiploidGeneticValue.hpp"
#include <fwdpp/fitness_models.hpp>
#include <fwdpy11/genetic_values/site_dependent_fitness.hpp>
#include <fwdpy11/genetic_value_noise/GeneticValueNoise.hpp>

namespace fwdpy11
Expand All @@ -26,7 +26,7 @@ namespace fwdpy11
}

inline double
operator()(const fwdpp::site_dependent_genetic_value& gv,
operator()(const fwdpy11::site_dependent_genetic_value& gv,
const std::size_t diploid_index,
const DiploidMetadata& /*metadata*/,
const DiploidPopulation& pop) const
Expand All @@ -47,7 +47,7 @@ namespace fwdpy11
}

inline double
operator()(const fwdpp::site_dependent_genetic_value& gv,
operator()(const fwdpy11::site_dependent_genetic_value& gv,
const std::size_t diploid_index, const DiploidMetadata& metadata,
const DiploidPopulation& pop) const
{
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace fwdpy11
};

using callback_type = std::function<double(
const fwdpp::site_dependent_genetic_value&, const std::size_t,
const fwdpy11::site_dependent_genetic_value&, const std::size_t,
const DiploidMetadata&, const DiploidPopulation&)>;

using make_return_value_t = std::function<double(double)>;
Expand All @@ -90,7 +90,7 @@ namespace fwdpy11
return multi_deme_callback(aa_scaling);
}

fwdpp::site_dependent_genetic_value gv;
fwdpy11::site_dependent_genetic_value gv;
double aa_scaling;
make_return_value_t make_return_value;
callback_type callback;
Expand Down
218 changes: 218 additions & 0 deletions fwdpy11/headers/fwdpy11/genetic_values/site_dependent_fitness.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#pragma once

#include <fwdpp/type_traits.hpp>

namespace fwdpy11
{
struct site_dependent_genetic_value
{
//! The return value type
using result_type = double;

template <typename iterator_t, typename MutationContainerType,
typename updating_policy_hom, typename updating_policy_het,
typename make_return_value>
inline result_type
operator()(iterator_t first1, iterator_t last1, iterator_t first2,
iterator_t last2, const MutationContainerType &mutations,
const updating_policy_hom &fpol_hom,
const updating_policy_het &fpol_het,
const make_return_value &rv_function,
const double starting_value) const noexcept
/*!
Range-based call operator. Calculates genetic values over ranges of
mutation keys first1/last1
and first2/last2, which are iterators derived from the 'smutations'
of two haploid_genomes in a diploid.
\param first1 Iterator to first mutation derived from haploid_genome 1
\param last1 Iterator to one past the last mutation derived from
haploid_genome 1
\param first2 Iterator to first mutation derived from haploid_genome 2
\param last2 Iterator to one past the last mutation derived from
haploid_genome 2
\param mutations The container of mutations for the simulation
\param fpol_hom Policy that updates genetic value for a homozygous
mutation.
\param fpol_het Policy that updates genetic value for a heterozygous
mutation.
\param make_return_value Policy generated the final return value.
Must be equivalent to std::function<double(double)>
\param starting_value The initial genetic value.
\returns Fitness (double)
*/
{
static_assert(fwdpp::traits::is_mutation<
typename MutationContainerType::value_type>::value,
"MutationContainerType::value_type must be a mutation type");
static_assert(
std::is_convertible<
updating_policy_hom,
std::function<void(double &, const typename MutationContainerType::
value_type &)>>::value,
"decltype(fpol_hom) must be convertible to "
"std::function<void(double &,const typename "
"MutationContainerType::value_type");
static_assert(
std::is_convertible<
updating_policy_het,
std::function<void(double &, const typename MutationContainerType::
value_type &)>>::value,
"decltype(fpol_het) must be convertible to "
"std::function<void(double &,const typename "
"MutationContainerType::value_type");
result_type w = starting_value;
if (first1 == last1 && first2 == last2)
return rv_function(w);
else if (first1 == last1)
{
for (; first2 != last2; ++first2)
fpol_het(w, mutations[*first2]);
return rv_function(w);
}
else if (first2 == last2)
{
for (; first1 != last1; ++first1)
fpol_het(w, mutations[*first1]);
return rv_function(w);
}
for (; first1 != last1; ++first1)
{
for (; first2 != last2 && *first1 != *first2
&& mutations[*first2].pos < mutations[*first1].pos;
++first2)
// All mutations in this range are Aa
{
fpol_het(w, mutations[*first2]);
}
if (first2 < last2
&& (*first1 == *first2
|| mutations[*first1].pos == mutations[*first2].pos))
// mutation with index first1 is homozygous
{
fpol_hom(w, mutations[*first1]);
++first2; // increment so that we don't re-process
// this site as a het next time 'round
}
else // mutation first1 is heterozygous
{
fpol_het(w, mutations[*first1]);
}
}
for (; first2 != last2; ++first2)
{
fpol_het(w, mutations[*first2]);
}
return rv_function(w);
}

template <typename iterator_t, typename MutationContainerType,
typename updating_policy_hom, typename updating_policy_het>
inline result_type
operator()(iterator_t first1, iterator_t last1, iterator_t first2,
iterator_t last2, const MutationContainerType &mutations,
const updating_policy_hom &fpol_hom,
const updating_policy_het &fpol_het,
const double starting_value) const noexcept
{
return this->operator()(
first1, last1, first2, last2, mutations, fpol_hom, fpol_het,
[](double d) { return d; }, starting_value);
}

template <typename HaploidGenomeType, typename MutationContainerType,
typename updating_policy_hom, typename updating_policy_het,
typename make_return_value>
inline result_type
operator()(const HaploidGenomeType &g1, const HaploidGenomeType &g2,
const MutationContainerType &mutations,
const updating_policy_hom &fpol_hom,
const updating_policy_het &fpol_het,
const make_return_value &rv_function,
const double starting_value) const noexcept
/*!
Calculates genetic value for a diploid whose genotype
across sites is given by haploid_genomes g1 and g2.
\param g1 A haploid_genome
\param g2 A haploid_genome
\param mutations The container of mutations for the simulation
\param fpol_hom Policy that updates genetic value for a homozygous
mutation.
\param fpol_het Policy that updates genetic value for a heterozygous
mutation.
\param starting_value The initial genetic value.
\returns Fitness (double)
*/
{
static_assert(fwdpp::traits::is_haploid_genome<HaploidGenomeType>::value,
"HaploidGenomeType::value_type must be a haploid_genome "
"type");
return this->operator()(g1.smutations.cbegin(), g1.smutations.cend(),
g2.smutations.cbegin(), g2.smutations.cend(),
mutations, fpol_hom, fpol_het, rv_function,
starting_value);
}

template <typename HaploidGenomeType, typename MutationContainerType,
typename updating_policy_hom, typename updating_policy_het>
inline result_type
operator()(const HaploidGenomeType &g1, const HaploidGenomeType &g2,
const MutationContainerType &mutations,
const updating_policy_hom &fpol_hom,
const updating_policy_het &fpol_het,
const double starting_value) const noexcept
{
return this->operator()(
g1, g2, mutations, fpol_hom, fpol_het, [](double d) { return d; },
starting_value);
}

template <typename DiploidType, typename GenomeContainerType,
typename MutationContainerType, typename updating_policy_hom,
typename updating_policy_het, typename make_return_value>
inline result_type
operator()(const DiploidType &dip, const GenomeContainerType &haploid_genomes,
const MutationContainerType &mutations,
const updating_policy_hom &fpol_hom,
const updating_policy_het &fpol_het,
const make_return_value &rv_function,
const double starting_value) const noexcept
/*!
Calculates genetic value for a diploid type.
\param dip A diploid
\param haploid_genomes The container of haploid_genomes for the simulation.
\param mutations The container of mutations for the simulation
\param fpol_hom Policy that updates genetic value for a homozygous
mutation.
\param fpol_het Policy that updates genetic value for a heterozygous
mutation.
\param starting_value The initial genetic value.
\returns Fitness (double)
*/
{
return this->operator()(haploid_genomes[dip.first],
haploid_genomes[dip.second], mutations, fpol_hom,
fpol_het, rv_function, starting_value);
}

template <typename DiploidType, typename GenomeContainerType,
typename MutationContainerType, typename updating_policy_hom,
typename updating_policy_het>
inline result_type
operator()(const DiploidType &dip, const GenomeContainerType &haploid_genomes,
const MutationContainerType &mutations,
const updating_policy_hom &fpol_hom,
const updating_policy_het &fpol_het,
const double starting_value) const noexcept
{
return this->operator()(
dip, haploid_genomes, mutations, fpol_hom, fpol_het,
[](double d) { return d; }, starting_value);
}
};
}

0 comments on commit 334c027

Please sign in to comment.