Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
db7fbd1
vg call: refuse --top-down and -Y with --read-likelihood
benedictpaten Oct 29, 2026
48e81d5
vg call: add --group-candidates, which lets groups of a site's walks …
benedictpaten Oct 29, 2026
80a25b0
vg call: turn --group-candidates on by default, and add --no-group-ca…
benedictpaten Oct 29, 2026
2ad02fa
vg call: keep two TAP tests of grouped walks, on reads where grouping…
benedictpaten Oct 29, 2026
ce5c000
vg call: test a child chain that one strand of a grouped parent crosses
benedictpaten Oct 29, 2026
ec9cb73
vg call: re-genotype each read-linked run in its own frame
benedictpaten Nov 5, 2026
c06c52e
vg call: weigh the panel's phase by its fitted error rates
benedictpaten Nov 8, 2026
0d3f87c
vg call: count each read once in a hung site's vote
benedictpaten Nov 8, 2026
f924614
Merge vgteam/vg master into group-candidates
benedictpaten Nov 8, 2026
60adde5
vg call: document mapping without the reference, for the mosaic and a…
benedictpaten Nov 16, 2026
298ecfe
vg call: read the grouped-walk test records with cut, and fix its MAP…
benedictpaten Nov 16, 2026
5cdc448
vg call: anchor file version 10, with a kind column and ranked pin so…
benedictpaten Nov 17, 2026
93cef84
vg call: fill anchors at evenly spaced points inside unbranched node …
benedictpaten Nov 17, 2026
d64ceeb
vg call: document fill anchors, and test them on the command line
benedictpaten Nov 17, 2026
808cf76
vg call: fill anchors take their slot from the innermost genotyped site
benedictpaten Nov 17, 2026
3b36c7f
vg call: tally the fill anchors' fetches apart, and test fill anchors…
benedictpaten Nov 17, 2026
bccdd1e
vg call: say why fill anchors in repeats hold more misplaced reads th…
benedictpaten Nov 17, 2026
1d9ac91
vg call: build each site's anchors over the alleles its line wrote
benedictpaten Nov 17, 2026
8d3cc33
vg call: place the fill anchors' reads from the windows the direct pa…
benedictpaten Nov 17, 2026
a04d028
vg call: remove a diagnostic table, a second fetch tally and other un…
benedictpaten Nov 20, 2026
1fd44dc
vg call: remove --off-panel-svs, three debugging outputs and two unus…
benedictpaten Nov 20, 2026
b7d0aa3
vg call: remove --regeno-shuffle, --anchors-strict-hets and two DiffB…
benedictpaten Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 47 additions & 36 deletions artifacts/allele_likelihood.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ double DepthContext::ratio(const vector<int>& genotype) const {

void AlleleReadLikelihoods::set_contents(size_t reads, size_t alleles, vector<double>&& rows,
vector<double>&& mismap, vector<double>&& best_ln,
vector<string>&& names, size_t dropped) {
size_t dropped) {
this->n_reads = reads;
this->n_alleles = alleles;
this->matrix = std::move(rows);
this->read_mismap_prob = std::move(mismap);
this->read_best_ln = std::move(best_ln);
this->read_names = std::move(names);
this->unplaceable = dropped;
this->effective_read_total = 0.0;
for (double e : this->read_mismap_prob) {
Expand Down Expand Up @@ -149,6 +148,12 @@ vector<double> AlleleReadLikelihoods::mixture_weights(const vector<int>& genotyp
}

double AlleleReadLikelihoods::genotype_likelihood(const vector<int>& genotype) const {
return genotype_likelihood(genotype, matrix);
}

double AlleleReadLikelihoods::genotype_likelihood(const vector<int>& genotype,
const vector<double>& rows,
bool with_depth) const {
if (genotype.empty()) {
return 0.0;
}
Expand All @@ -165,7 +170,7 @@ double AlleleReadLikelihoods::genotype_likelihood(const vector<int>& genotype) c
if (allele < 0 || (size_t)allele >= n_alleles) {
continue;
}
mixture += weights[i] * rel(r, (size_t)allele);
mixture += weights[i] * rows.at(r * n_alleles + (size_t)allele);
}

// Mix in the chance that the read is mismapped, in which case its likelihood is the row
Expand All @@ -174,16 +179,19 @@ double AlleleReadLikelihoods::genotype_likelihood(const vector<int>& genotype) c
total += log((1.0 - e_r) * mixture + e_r);
}

if (uses_depth_term()) {
// Add ln of the tempered density f^beta / Z_beta at the observed count, with beta the
// depth weight. Z_beta grows with lambda when beta < 1, so without it we would favour
// the genotypes that expect more reads.
double lambda = expected_reads(genotype);
total += depth_weight * ln_poisson_pmf(observed_reads(), lambda)
- ln_tempered_poisson_normaliser(lambda, depth_weight);
}
return with_depth ? total + depth_term(genotype) : total;
}

return total;
double AlleleReadLikelihoods::depth_term(const vector<int>& genotype) const {
if (!uses_depth_term()) {
return 0.0;
}
// Take ln of the tempered density f^beta / Z_beta at the observed count, with beta the depth
// weight. Z_beta grows with lambda when beta < 1, so without it we would favour the genotypes
// that expect more reads.
double lambda = expected_reads(genotype);
return depth_weight * ln_poisson_pmf(observed_reads(), lambda)
- ln_tempered_poisson_normaliser(lambda, depth_weight);
}

double AlleleReadLikelihoods::achievable_gap(const vector<int>& called,
Expand Down Expand Up @@ -278,30 +286,37 @@ vector<vector<int>> AlleleReadLikelihoods::enumerate_genotypes(size_t num_allele
}

vector<pair<vector<int>, double>> AlleleReadLikelihoods::score_genotypes(int ploidy) const {
return score_genotypes(enumerate_genotypes(n_alleles, ploidy));
}

vector<pair<vector<int>, double>> AlleleReadLikelihoods::score_genotypes(
const vector<vector<int>>& genotypes, const vector<int>* group_of, bool with_depth) const {
assert(group_of == nullptr || group_of->size() == n_alleles);
// With groups, give each read's entry for an allele the read's best entry in the allele's
// group.
vector<double> shared;
if (group_of != nullptr && n_alleles > 0) {
shared.resize(matrix.size());
vector<double> best(*std::ranges::max_element(*group_of) + 1);
for (size_t r = 0; r < n_reads; ++r) {
std::ranges::fill(best, 0.0);
for (size_t a = 0; a < n_alleles; ++a) {
best[(*group_of)[a]] = max(best[(*group_of)[a]], rel(r, a));
}
for (size_t a = 0; a < n_alleles; ++a) {
shared[r * n_alleles + a] = best[(*group_of)[a]];
}
}
}
const vector<double>& rows = group_of != nullptr ? shared : matrix;
vector<pair<vector<int>, double>> scored;
for (auto& genotype : enumerate_genotypes(n_alleles, ploidy)) {
double ll = genotype_likelihood(genotype);
for (auto& genotype : genotypes) {
double ll = genotype_likelihood(genotype, rows, with_depth);
scored.emplace_back(genotype, ll);
}
return scored;
}

void AlleleReadLikelihoods::dump(ostream& out, const string& site_name) const {
out << "#site\tread\tmismap_prob\tbest_ln";
for (size_t a = 0; a < n_alleles; ++a) {
out << "\tallele_" << a;
}
out << endl;
for (size_t r = 0; r < n_reads; ++r) {
out << site_name << "\t" << (read_names.empty() ? string(".") : read_names[r]) << "\t"
<< read_mismap_prob[r] << "\t" << read_best_ln[r];
for (size_t a = 0; a < n_alleles; ++a) {
out << "\t" << rel(r, a);
}
out << endl;
}
}

////////////////////////////////////////////////////////////////////////////////
// AlleleReadLikelihoodsBuilder
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -382,20 +397,17 @@ AlleleReadLikelihoods AlleleReadLikelihoodsBuilder::build() {
matrix.reserve(n_reads * n_alleles);
vector<double> sorted_mismap;
vector<double> sorted_best;
vector<string> sorted_names;
sorted_mismap.reserve(n_reads);
sorted_best.reserve(n_reads);
sorted_names.reserve(n_reads);
for (size_t i : order) {
matrix.insert(matrix.end(), rows[i].begin(), rows[i].end());
sorted_mismap.push_back(mismap_probs[i]);
sorted_best.push_back(best_lns[i]);
sorted_names.push_back(std::move(names[i]));
}

AlleleReadLikelihoods result;
result.set_contents(n_reads, n_alleles, std::move(matrix), std::move(sorted_mismap),
std::move(sorted_best), std::move(sorted_names), unplaceable);
std::move(sorted_best), unplaceable);
result.set_mismap_floor(min_mismap);
if (read_length_count > 0) {
// Set R whichever mixture is in use, since the depth term's lambda = rate * (L + R - 1)
Expand Down Expand Up @@ -2068,8 +2080,7 @@ void GraphAlignedAlleleLikelihoodCalculator::score_row(
for (size_t a = 0; a < alleles.steps.size(); ++a) {
const vector<AlleleStep>& steps = alleles.steps[a];
if (steps.empty()) {
// An allele with no visits, such as the empty traversal of a star allele, cannot place
// the read.
// An allele with no visits cannot place the read.
row[a] = -numeric_limits<double>::infinity();
continue;
}
Expand Down
23 changes: 16 additions & 7 deletions artifacts/allele_likelihood.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ class AlleleReadLikelihoods {
* Get the likelihood of read r under allele a, relative to the best allele of read r.
*
* It is 0 where allele a fits the read so much worse than the best that the ratio
* underflows. It is also 0 where allele a has no node visits, as the empty traversal of a
* star allele has, and cannot place the read.
* underflows. It is also 0 where allele a has no node visits, and so cannot place the read.
*/
double rel(size_t r, size_t a) const;

Expand Down Expand Up @@ -182,6 +181,9 @@ class AlleleReadLikelihoods {
return depth.expected_reads(genotype);
}

/// Return the depth term of `genotype` in `genotype_likelihood`, or 0 when the term is off.
double depth_term(const vector<int>& genotype) const;

/// Return whether `set_depth_context` has supplied traversal lengths.
bool has_depth_context() const { return !depth.lengths.empty(); }

Expand Down Expand Up @@ -274,8 +276,12 @@ class AlleleReadLikelihoods {
/// Return `genotype_likelihood` for every genotype of this ploidy, in VCF GL order.
vector<pair<vector<int>, double>> score_genotypes(int ploidy) const;

/// Write the matrix as TSV for debugging, with one line per read.
void dump(ostream& out, const string& site_name) const;
/// Return `genotype_likelihood` for each of `genotypes`, in their order. With `group_of`, the
/// relative likelihood of each read for an allele is its best over the allele's group. With
/// `with_depth` false, each score leaves out the depth term.
vector<pair<vector<int>, double>> score_genotypes(const vector<vector<int>>& genotypes,
const vector<int>* group_of = nullptr,
bool with_depth = true) const;

/**
* Keep the anchor evidence of the site, and fill its rows from this matrix.
Expand Down Expand Up @@ -304,8 +310,7 @@ class AlleleReadLikelihoods {

/// Fill the matrix, for `AlleleReadLikelihoodsBuilder`.
void set_contents(size_t reads, size_t alleles, vector<double>&& rows,
vector<double>&& mismap, vector<double>&& best_ln,
vector<string>&& names, size_t dropped);
vector<double>&& mismap, vector<double>&& best_ln, size_t dropped);

/// Set the floor on e_r, which `achievable_gap` gives its ideal reads.
void set_mismap_floor(double floor) { this->mismap_floor = floor; }
Expand All @@ -325,6 +330,11 @@ class AlleleReadLikelihoods {
/// Set N in `depth` from the reads and from whether they count as 1 - e_r.
void count_observed();

/// Return `genotype_likelihood` with `rows` in place of the relative likelihoods. `rows` is
/// row major, n_reads * n_alleles. Leave out the depth term when `with_depth` is false.
double genotype_likelihood(const vector<int>& genotype, const vector<double>& rows,
bool with_depth = true) const;

/// The anchor evidence of the site, or null.
unique_ptr<AnchorSiteEvidence> anchor_evidence;
/// The read-phasing evidence of the site, or null.
Expand All @@ -348,7 +358,6 @@ class AlleleReadLikelihoods {
bool depth_effective = true;
double depth_weight = 0.0;
vector<double> read_best_ln;
vector<string> read_names;
size_t n_reads = 0;
size_t n_alleles = 0;
size_t unplaceable = 0;
Expand Down
Loading
Loading