Skip to content

Make tree internal to variant and seek to site - #2160

Merged
benjeffery merged 2 commits into
tskit-dev:mainfrom
benjeffery:variant-internal-tree
Mar 17, 2022
Merged

benjeffery merged 2 commits into
tskit-dev:mainfrom
benjeffery:variant-internal-tree

Conversation

@benjeffery

Copy link
Copy Markdown
Member

Part of #605

@benjeffery
benjeffery force-pushed the variant-internal-tree branch 2 times, most recently from 4daccab to b2d1bde Compare March 16, 2022 16:25
@benjeffery
benjeffery force-pushed the variant-internal-tree branch from b2d1bde to 76cd7b7 Compare March 16, 2022 16:39
@benjeffery

benjeffery commented Mar 16, 2022 •

Copy link
Copy Markdown
Member Author

There's an issue here with the lifetime of the site object as the variant stores a reference to it. This wasn't a problem before as the site was owned by the tree, using the fact that tree_sites is filled by the treeseq_init. It seems a shame to have to call treeseq_get_site for every site when it was already done by treeseq_init, but that seems to be the simplest route forward - making the site owned by variant and changing the argument to variant_decode to an id.

@jeromekelleher

jeromekelleher commented Mar 16, 2022 •

Copy link
Copy Markdown
Member

I agree we should make the argument to tsk_variant_decode a site ID, if only because it makes the client code simpler:

tsk_variant_t var;
tsk_id_t site_id;

for (site_id = 0; site_id < (tsk_id_t) tsk_treeseq_get_num_sites(ts); site_id++) {
     ret = tsk_variant_decode(&var, site_id, 0);
     if (ret != 0) {
          goto out;
     }
} 

vs

tsk_variant_t var;
tsk_site_t site;
tsk_id_t site_id;

for (site_id = 0; site_id < (tsk_id_t) tsk_treeseq_get_num_sites(ts); site_id++) {
     ret = tsk_treeseq_get_site(ts, site_id, &site);
     if (ret != 0) {
          goto out;
     }
     ret = tsk_variant_decode(&var, &site, 0);
     if (ret != 0) {
          goto out;
     }
} 

It's not like we save anything by having to call tsk_treeseq_get_site externally, and we'd actually have to work harder to validate the tsk_site_t object passed in as well (what tree sequence is it for? Is it some kind of random garbage the user has passed in?).

The first way is much neater I think.

@jeromekelleher

Copy link
Copy Markdown
Member

Why not keep storing the site on the variant object @benjeffery? We're keeping a reference to the underlying tree sequence, so there's basically no overhead in storing a pointer to a site, is there?

@codecov

codecov Bot commented Mar 17, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #2160 (0542d8d) into main (48a501a) will increase coverage by 0.00%.
The diff coverage is 83.72%.

❗ Current head 0542d8d differs from pull request most recent head e1eefdb. Consider uploading reports for the commit e1eefdb to get more accurate results

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2160   +/-   ##
=======================================
  Coverage   93.27%   93.28%           
=======================================
  Files          27       27           
  Lines       25919    25896   -23     
  Branches     1163     1163           
=======================================
- Hits        24177    24156   -21     
+ Misses       1712     1710    -2     
  Partials       30       30           
Flag Coverage Δ
c-tests 92.18% <89.28%> (-0.02%) ⬇️
lwt-tests 89.05% <ø> (ø)
python-c-tests 72.09% <73.33%> (+<0.01%) ⬆️
python-tests 98.89% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
python/_tskitmodule.c 90.92% <73.33%> (+0.02%) ⬆️
c/tskit/genotypes.c 89.26% <89.28%> (-0.58%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 48a501a...e1eefdb. Read the comment docs.

@benjeffery

Copy link
Copy Markdown
Member Author

Why not keep storing the site on the variant object @benjeffery? We're keeping a reference to the underlying tree sequence, so there's basically no overhead in storing a pointer to a site, is there?

I started the work to make variant lookup and store the site when I realised we didn't actually need to store it. I'm in two minds about it though as the site lookup is cheaper than I realised, basically copying a few pointers. Thought I would upload this version for feedback. If you feel strongly to store the site then we can switch to that, there was more breakage here than I expected so probably that is the right thing to do.

@jeromekelleher

Copy link
Copy Markdown
Member

Since it's cheap I don't think it's worth the breakage. From a client perspective, it's nice to be able to do things like:

tsk_variant_t var;
tsk_id_t site_id;

for (site_id = 0; site_id < (tsk_id_t) tsk_treeseq_get_num_sites(ts); site_id++) {
     ret = tsk_variant_decode(&var, site_id, 0);
     if (ret != 0) {
          goto out;
     }
     printf("Variant at site %d (pos=%f)\n", var.site.id, var.site.position);
} 

Otherwise, you have to do the site lookup again in the client code.

Comment thread c/tskit/genotypes.c
tsk_size_t num_samples_alloc;

tsk_memset(self, 0, sizeof(tsk_variant_t));
self->site = malloc(sizeof(*self->site));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could probably avoid this malloc, but let's not change it yet to keep the diff size managable. I've made #2161 to track.

@benjeffery
benjeffery force-pushed the variant-internal-tree branch from 5985dcb to e1eefdb Compare March 17, 2022 13:56
@benjeffery
benjeffery marked this pull request as ready for review March 17, 2022 13:56
@benjeffery
benjeffery marked this pull request as draft March 17, 2022 13:57
@benjeffery
benjeffery marked this pull request as ready for review March 17, 2022 14:12

@jeromekelleher jeromekelleher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment thread c/tskit/genotypes.c Resolved
@benjeffery

Copy link
Copy Markdown
Member Author

@Mergifyio rebase

@mergify

mergify Bot commented Mar 17, 2022

Copy link
Copy Markdown
Contributor

rebase

✅ Branch has been successfully rebased

@benjeffery
benjeffery merged commit 2983da8 into tskit-dev:main Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants