Repository navigation
Description
The current samples argument to find_ibd requires us to provide an explicit list of pairs of nodes we want to record ibd segments for. This is a bad idea for a number of reasons I think:
- It's tedious - you have to do
samples=list(itertools.combinations(A, 2))to get all the pairs within a set A - It's expensive - if we have to accept or reject segments for recording in the
ibd_result_tobject based on whether or a pair exists in the input mapping, then we have no choice but to store this mapping (even if we're only accumulating statistics). - It leads to odd semantics in the ibd_result_t object, where an input pair (a, b) will be "in" the mapping, even if no segments were found
I think we should change the signature of find_ibd to this:
def find_ibd(self, *, among=None, between=None, max_time=None, min_length=None):
...Where among is a 1D array of node IDs (a "sample set" basically) and between is a list of sample sets. I guess initially between would be required to have two elements, but we could imagine this being allowed to be three or more.
Then, rather than building a list of sample pairs as input to the lower-level methods, we pass these among and between arrays down to the lower level code.
The core of the find_ibd algorithm is this chunk:
seg0 = self.A[current_parent].head
while seg0 is not None:
seg1 = list_to_add.head
while seg1 is not None:
left = max(seg0.left, seg1.left)
right = min(seg0.right, seg1.right)
# If there are any overlapping segments, record as a new IBD relationship.
if seg0.node != seg1.node and right - left > self.min_length:
self.result.add_segment(
seg0.node, seg1.node, Segment(left, right, current_parent)
)
seg1 = seg1.next
seg0 = seg0.nextCurrently (well after some PRs have dropped, see #1660 for the latest) we're letting the IBD result object decide whether or not to store the segment. It would be more efficient if we did it here, based on membership of the among and between sets.
Implementing among is trivial - we just use this to populate the initial ancestry and we're done.
For between we just need to check that seg0.node and seg1.node are in one or other of the input sample sets, which we can do with something like
def keep_pair(self, a, b, length):
if a == b:
return False
if length < min_length:
return False
if self.between is None:
return True
return (self.between_set[0][a] and self.between_set[1][b]) or (self.between_set[0][b] and self.between_set[1][a])where between_set is a (2, num_nodes) array of booleans.
This will be both more convenient, and also more efficient as it will allow us to discover segment pairs in large populations without having to list out all the pairs explicitly. Looking up two 1D arrays is also much more efficient than looking up map of pairs.
Another side effect is that is simplifies the IBDResult object semantics, in that a node pair is only ever "in" the output if it has a non-empty list of IBD segments.
Any thoughts @gtsambos, @petrelharp?
Activity
gtsambos commentedon Sep 6, 2021
Morning Jerome! This sounds good to me in general -- I completely agree with your view on the downsides of the current input/result semantics. I have a few questions:
Does this mean that if
among=[5, 6, 7]andbetween=[(0, 1), (0, 2)], we'd be looking for sample sets[(5, 6), (5, 7)]? (More generally, I don't totally understand howbetweendiffers from the presentsamplesargument.)I don't understand what about this proposed change causes this 'side effect'. Just because a sample pair is represented in the
amongandbetweeninputs doesn't mean it has any IBD segments, so either you're passing these arguments upfront to the result struct (in which case they are held in the output in some way, regardless of whether they have IBD or not), or you're using them to filter what gets passed into that result struct and updating the struct 'on the fly' as new pairs with IBD segments are found -- but my understanding was that the latter isn't possible because of C memory stuff.jeromekelleher commentedon Sep 6, 2021
Sorry, I should have been clearer here @gtsambos -- the
amongandbetweenarguments are mutually exclusive. You can only find IBD segments either within a specific list of samples (among) or between a two lists of samples (between). So, if we haveamong=[5, 6, 7]we could find IBD segments for pairs(5, 6),(5, 7)or(6, 7)in the output.If we have
between=[[0, 1], [2, 3]], then we could have (0,2), (0, 3), (1, 2), (1, 3) in the output (but nothing else). To do this with the current samples argument, you'd have to explicitly saysamples=[(0,2), (0, 3), (1, 2), (1, 3)].The current approach is more general, but not usefully so I think.
gtsambos commentedon Sep 6, 2021
Ah, okay, that makes more sense!
Yeah, I agree -- at least, I've never used
find_ibdwith a set of pairs that isn't in one of these two categories.petrelharp commentedon Sep 7, 2021
Change to "within" semantics for find_ibd