 |
ʻOhana
Population structure, admixture history, and selection using learning methods.
|
7 #ifndef JADE_NEOSCAN_HPP__
8 #define JADE_NEOSCAN_HPP__
10 #include "jade.args.hpp"
11 #include "jade.genotype_matrix_factory.hpp"
20 template <
typename TValue>
73 std::ostringstream out;
97 , _y (_init_y(years, q))
98 , _f_j (f.get_height(), 1)
111 template <
typename TOutputAction>
112 void execute(
const TOutputAction & output_action)
const
116 for (
size_t j = 0; j < J; j++)
122 const auto range_low = -col_max;
123 const auto range_high =
value_type(1.0) - col_min;
131 static const auto phi =
value_type(0.5 * (sqrt(5.0) + 1.0));
133 const auto dr_phi = (range_high - range_low) / phi;
137 auto c = range_high - dr_phi;
138 auto d = range_low + dr_phi;
140 while (std::abs(c - d) > tol)
142 if (_compute_lle_j(j, c) > _compute_lle_j(j, d))
147 c = b - (b - a) / phi;
148 d = a + (b - a) / phi;
151 const auto gss_delta =
value_type(0.5) * (a + b);
152 const auto gss_lle = _compute_lle_j(j, gss_delta);
156 out.
delta = gss_delta;
170 const std::unique_ptr<genotype_matrix_type> g_ptr (
178 std::cout <<
"d\tglobal_lle\tlocal_lle\tlle_ratio\n";
184 std::cout << out.
to_string() << std::endl;
205 const auto f_j_end = _f_j.
get_data() + K;
210 while (f_j_src != f_j_end)
212 const auto q_ik = *q_src++;
213 const auto f_kj = *f_j_src++;
227 static const auto epsilon =
value_type(1.0e-6);
228 static const auto lower_epsilon =
value_type(0.0) + epsilon;
229 static const auto upper_epsilon =
value_type(1.0) - epsilon;
239 const auto dst_end = _f_j.
get_data() + K;
240 const auto dy = d * _y[i];
242 while (dst_ptr != dst_end)
244 *dst_ptr++ = std::min(std::max(
262 const auto & g = _g.to_dgm().get_matrix();
266 for (
size_t i = 0; i < I; i++)
269 if (!_try_convert(g(i, j), g_ij))
272 _compute_f_j(j, i, d);
275 _compute_ab_ij(i, a_ij, b_ij);
277 lle_all += std::log(a_ij) * g_ij;
278 lle_all += std::log(b_ij) * (
value_type(2.0) - g_ij);
286 const auto & g = _g.to_lgm();
287 const auto & g_AA = g.get_major_major_matrix();
288 const auto & g_Aa = g.get_major_minor_matrix();
289 const auto & g_aa = g.get_minor_minor_matrix();
293 for (
size_t i = 0; i < I; i++)
298 const auto g_AA_ij = g_AA(i, j);
299 const auto g_Aa_ij = g_Aa(i, j);
300 const auto g_aa_ij = g_aa(i, j);
302 _compute_f_j(j, i, d);
305 _compute_ab_ij(i, a_ij, b_ij);
308 (g_AA_ij * a_ij * a_ij) +
309 (g_aa_ij * b_ij * b_ij) +
324 if (!years.is_column_vector())
326 <<
"invalid years matrix has "
328 <<
" columns; expected column vector";
330 const auto I = q.get_height();
333 if (years.get_height() != I)
335 <<
"inconsistent number of years specified ("
336 << years.get_height()
337 <<
"); expected height of Q matrix (" << I <<
")";
341 const auto max_value = years.get_max_value();
342 const auto min_value = years.get_min_value();
343 const auto avg_value = years.get_sum() /
value_type(I);
344 const auto years_end = years.get_data() + I;
345 auto years_ptr = years.get_data();
346 auto out_ptr = out.get_data();
348 while (years_ptr != years_end)
350 const auto y = *years_ptr++;
353 : (avg_value - y) / std::max(max_value - y, y - min_value);
360 static bool _try_convert(
const genotype g,
value_type & out)
364 case jade::genotype_major_major:
368 case jade::genotype_major_minor:
372 case jade::genotype_minor_minor:
391 #endif // JADE_NEOSCAN_HPP__
A template for a class implementing the neoscan algorithm. This algorithm scans for positive selectio...
A template for a class that performs validation on various types of matrices.
void validate_empty() const
Throws an exception if there are more arguments to be processed.
value_type compute_lle_ratio() const
value_type global_lle
The global lle.
const value_type * get_data() const
static genotype_matrix_type * create(const std::string &path)
Creates a genotype matrix based on values from a file. This function determines what kind of genotype...
bool get_min_max_column(const size_t column, value_type &min, value_type &max) const
Returns the minimum and maximum elements in a column. If the matrix is empty, this method returns fal...
size_t get_length() const
void execute(const TOutputAction &output_action) const
Executes the algorithm using the specified action for the output of each column.
A template class implementing operations for a likelihood genotype matrix type.
basic_neoscan(const genotype_matrix_type &g, const matrix_type &q, const matrix_type &f, const matrix_type &years)
Initializes a new instance of the class. Memory for the specified G, Q, and Fa matrices must remain v...
basic_genotype_matrix< value_type > genotype_matrix_type
The genotype matrix type.
std::string to_string() const
Returns the delta, global_lle, local_lle, and computed log-likelihood ratio converted to high-precisi...
A template for an abstract class implementing operations for a genotype matrix.
basic_verification< value_type > verification_type
The verification type.
static void set_high_precision(std::ostream &out)
Sets the specified stream to scientific notation with a high precision.
virtual size_t get_width() const =0
TValue value_type
The value type.
static void run(args &a)
Runs the program based on command-line arguments.
size_t get_height() const
static bool validate_gqf_sizes(const genotype_matrix_type &g, const matrix_type &q, const matrix_type &f)
Validates the sizes of the G, Q, and F matrices and throws an exception if validation fails.
A template for a class implementing operations for a discrete genotype matrix.
A template for a class that helps process command-line arguments.
basic_genotype_matrix_factory< value_type > genotype_matrix_factory_type
The genotype matrix factory type.
A template for a class that creates genotype matrices based on files and their file extensions.
A template for a class representing an exception thrown from this namespace.
value_type local_lle
The local lle.
value_type delta
The delta.
basic_matrix< value_type > matrix_type
The matrix type.