How to use the tsinfer.new_inference.Segment function in tsinfer

To help you get started, we’ve selected a few tsinfer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tskit-dev / tsinfer / tsinfer / new_inference.py View on Github external
break
            intersection = S[j].end - S[k].start
            if intersection > max_intersection:
                max_intersection = intersection
                max_intersection_segs = j, k
            j = k
            k += 1
        if max_intersection_segs is None:
            ret = segments
        else:
            j, k = max_intersection_segs
            ret = S[:j]
            # print("INTERSECT:", j, k)
            if S[j].start != S[k].start:
                # Trim off a new segment for the leading overhang.
                ret.append(Segment(S[j].start, S[k].start, S[j].value))
                S[j].start = S[k].start
            # Create a new segment for the coalesced region.
            parent = self.next_minor_node
            self.time_slice_minor_nodes.append(parent)
            self.next_minor_node += 1
            seg = Segment(S[j].start, min(S[j].end, S[k].end), parent)
            self.record_coalescence(seg.start, seg.end, parent, [S[j].value, S[k].value])
            ret.append(seg)
            if S[j].end > S[k].end:
                S[j].start = S[k].end
                ret.append(S[j])
            elif S[k].end > S[j].end:
                S[k].start = S[j].end
                ret.append(S[k])
            if j + 1 != k:
                ret.extend(S[j + 1: k])
github tskit-dev / tsinfer / tsinfer / new_inference.py View on Github external
def record_coalescence(self, start, end, parent, children):
        """
        Coalescences the specified set of segments into the specified parent.
        All segments must be have identical coordinates.
        """
        assert len(children) > 1
        assert start < end
        record = Segment(start, end, (parent, sorted(children)))
        # print("COALESCENCE:", record)
        self.edgesets.append(record)
github tskit-dev / tsinfer / tsinfer / new_inference.py View on Github external
assert ancestor_age.shape[0] == self.num_ancestors
        self.sites = [SiteState(pos, []) for pos in position]
        self.ancestor_focal_sites = [[]]
        self.ancestor_age = ancestor_age
        last_ancestor = 0
        assert focal_site_ancestor.shape == focal_site.shape
        for j in range(focal_site_ancestor.shape[0]):
            if focal_site_ancestor[j] != last_ancestor:
                last_ancestor = focal_site_ancestor[j]
                self.ancestor_focal_sites.append([])
            self.ancestor_focal_sites[-1].append(focal_site[j])

        j = 0
        for l in range(self.num_sites):
            while j < site.shape[0] and site[j] == l:
                self.sites[l].segments.append(Segment(start[j], end[j]))
                j += 1
        self.num_older_ancestors = np.zeros(self.num_sites, dtype=np.uint32)
        num_older_ancestors = 1
        last_frequency = ancestor_age[1] + 1
        for j in range(1, self.num_ancestors):
            if ancestor_age[j] < last_frequency:
                last_frequency = ancestor_age[j]
                num_older_ancestors = j
            self.num_older_ancestors[j] = num_older_ancestors
        self.num_epochs = np.unique(ancestor_age).shape[0] + 1
        self.epoch_ancestors = [[] for _ in range(self.num_epochs)]
        epoch = self.num_epochs - 2
        last_frequency = ancestor_age[1]
        for j in range(1, self.num_ancestors):
            if ancestor_age[j] < last_frequency:
                last_frequency = ancestor_age[j]
github tskit-dev / tsinfer / tsinfer / new_inference.py View on Github external
def record_nonoverlapping_segment(self, segment, parent):
        # print("NONOVERLAPPING", segment, parent)
        # child_mapping = Segment(segment.start, segment.end, parent)
        # self.parent[parent].append(segment)
        self.next_time_slice_segments[parent].append((segment.start, segment.end))
        record = Segment(segment.start, segment.end, (parent, [segment.value]))
        self.edgesets.append(record)
github tskit-dev / tsinfer / tsinfer / new_inference.py View on Github external
def add_child_mapping(self, start, end, parent, child):
        # print("ADD CHILD MAPPING", start, end, parent, child)
        self.parent[child].append(Segment(start, end, parent))
github tskit-dev / tsinfer / tsinfer / new_inference.py View on Github external
def resolve(self, age, parents):
        # print("RESOLVING TS @:", age, parents)
        # self.print_state()
        self.time_slice_minor_nodes = []
        parent_map = {parent:list() for parent in parents}
        for child in range(self.num_major_nodes):
            unused = []
            for segment in self.parent[child]:
                if segment.value in parent_map:
                    parent_map[segment.value].append(Segment(segment.start, segment.end, child))
                else:
                    unused.append(segment)
            # print("Setting unused ", child, "->", unused)
            self.parent[child] = unused

        # print("Parent mappings:")
        for parent, segments in parent_map.items():
            # print(parent, "->", segments)
            self.resolve_segments(parent, segments)
            # print("-" * self.num_sites)

        # print("nodes used in this time slice")
        # print(self.time_slice_minor_nodes)
        for parent in parents:
            self.node_time[parent] = age
        for j, u in enumerate(self.time_slice_minor_nodes):