Exemplo n.º 1
0
 def handle(self, request):
     '''Loop over all families and handle each one.'''
     problem, params = request.problem, request.params
     families = problem.find_families_by_member(params.single_member, genotyped=False, min_children=self.min_children) \
     if params.single_member else pt.selected_families(problem, params, genotyped=self.genotyped_families, min_children=self.min_children)
     for i, family in enumerate(families):
         # if self.debug and np.mod(snp, problem.num_snps/10):
         if params.debug:
             print 'Processing %d/%d %s' % ((i + 1), len(families), repr(family))
         # Note: ignoring processor handled (return) code
         self.family_handle(self, request, family)
         
         # Exit after one family -- debugging
         if params.single_family:
             return True
     return False
Exemplo n.º 2
0
    def handle(self, request):
        '''Loop over all families and handle each one.'''
        problem, params = request.problem, request.params
        families = problem.find_families_by_member(params.single_member, genotyped=False, min_children=self.min_children) \
        if params.single_member else pt.selected_families(problem, params, genotyped=self.genotyped_families, min_children=self.min_children)
        for i, family in enumerate(families):
            # if self.debug and np.mod(snp, problem.num_snps/10):
            if params.debug:
                print 'Processing %d/%d %s' % (
                    (i + 1), len(families), repr(family))
            # Note: ignoring processor handled (return) code
            self.family_handle(self, request, family)

            # Exit after one family -- debugging
            if params.single_family:
                return True
        return False
Exemplo n.º 3
0
def __handle_child_comparison(self, request):
    '''In families with at least 3 children:
    if one parent is a founder (or more generally, not sufficiently phased in het snps)
    and the other is not, the children in this family will be phased well by ParentChildFounder,
    but the non-founder parent will not be.
    
    By comparing children's haplotypes against a template child (=the most-filled child)
    and translating that into comparison between children haps and the unphased parent's, we can infer
    their IBS segments and subsequently the parent's haplotypes. 
    
    Note that the parent will have random hap-gender-assignment: we can't know which one of his/her
    haplotypes is paternal and which one is maternal (we might at a later stage, if his/her parent
    genotypes are genotyped or imputed by ancestor imputation).'''
    problem, params = request.problem, request.params
    g, h = problem.components
    # Find families with at least min_consensus_samples genotyped children, or use single
    # family if debug mode (single_member) is on
    potential_families = problem.find_families_by_member(params.single_member, genotyped=False,
                                                         min_children=params.min_consensus_samples) \
    if params.single_member else pt.selected_families(problem, params, genotyped=False, min_children=params.min_consensus_samples)
    families = [
        f for f in potential_families if len(
            problem.find_samples_with_fill_ge(
                params.surrogate_parent_fill_threshold,
                sample=f.children_array)) >= params.min_consensus_samples
    ]
    if params.debug:
        print '__handle_child_comparison(), families to process', list(
            families)
    for family in families:
        genotyped_parent_dict = [
            (k, v) for (k, v) in family.parents_dict().iteritems()
            if problem.is_genotyped(v)
        ]
        num_genotyped_parents = len(genotyped_parent_dict)
        # If both parents are genotyped, use all children - it is probably safe enough to generate
        # enough SNPs to work with (het in parent + filled in all children), since it has worked in the past.
        # If not both parents are genotyped, use filled children only to generate enough relevant SNPs.
        genotyped_children = np.array(
            [x for x in family.children_array if problem.is_genotyped(x)])
        filled_children = genotyped_children if num_genotyped_parents == 2 else \
            problem.find_samples_with_fill_ge(params.surrogate_parent_fill_threshold, sample=genotyped_children)[:, 0].astype(np.int)
        comparator = ic.ChildComparator(request, family, filled_children)
        # for parent_type, parent in reversed(family.parents_dict().items()):
        for parent_type, parent in genotyped_parent_dict:
            # het_snps = gt.where_heterozygous(h.data, parent)
            het_snps = gt.where_heterozygous(g.data, parent)
            if h.fill_fraction(sample=parent,
                               snps=het_snps) < params.het_fill_threshold:
                # if is_founder[parent]:
                # Choose template = most-filled child
                fill = problem.fill_fraction(sample=filled_children)
                if params.debug:
                    print '=' * 105
                    print 'Children comparison in', family, 'parent_type', parent_type
                    print '=' * 105
                    print[problem.is_genotyped(x) for x in family.children]
                    print '# genotyped children', sum(
                        problem.is_genotyped(x) for x in family.children),
                    print '# parent het snps', len(het_snps)
                    print 'Filled children', filled_children
                    print 'Family' 's fill:\n', problem.fill_fraction(
                        sample=family.member_set)
                template_child = int(fill[np.argmax(fill[:, 1]), 0])
                if params.debug:
                    'template_child', template_child
                # Choose a template child at random (first index in the children list)
                # template_child = list(family.children)[0]
                (_, _, info) = comparator.child_recombinations(
                    parent_type, template_child)
                # Save selected entries from the family info class in problem
                problem.set_family_info(family, info)
                # Impute parent from the template child
                if params.debug:
                    print family, parent_type
                    print 'Child recombinations'
                    print info.recombination_snp
                comparator.phase_parent_by_template(info)
                # Now phase children (and possibly some more of the parent) using IBD segments
                # found among them and the parent
                ibd.phase_by_ibd(request, info.ibs_segments(), 'majority')
    return False
Exemplo n.º 4
0
def __handle_child_comparison(self, request):
    '''In families with at least 3 children:
    if one parent is a founder (or more generally, not sufficiently phased in het snps)
    and the other is not, the children in this family will be phased well by ParentChildFounder,
    but the non-founder parent will not be.
    
    By comparing children's haplotypes against a template child (=the most-filled child)
    and translating that into comparison between children haps and the unphased parent's, we can infer
    their IBS segments and subsequently the parent's haplotypes. 
    
    Note that the parent will have random hap-gender-assignment: we can't know which one of his/her
    haplotypes is paternal and which one is maternal (we might at a later stage, if his/her parent
    genotypes are genotyped or imputed by ancestor imputation).'''
    problem, params = request.problem, request.params 
    g, h = problem.components
    # Find families with at least min_consensus_samples genotyped children, or use single
    # family if debug mode (single_member) is on
    potential_families = problem.find_families_by_member(params.single_member, genotyped=False,
                                                         min_children=params.min_consensus_samples) \
    if params.single_member else pt.selected_families(problem, params, genotyped=False, min_children=params.min_consensus_samples)
    families = [f for f in potential_families
                if len(problem.find_samples_with_fill_ge(params.surrogate_parent_fill_threshold, sample=f.children_array))
                >= params.min_consensus_samples]
    if params.debug: print '__handle_child_comparison(), families to process', list(families)
    for family in families:
        genotyped_parent_dict = [(k, v) for (k, v) in family.parents_dict().iteritems() if problem.is_genotyped(v)]
        num_genotyped_parents = len(genotyped_parent_dict)
        # If both parents are genotyped, use all children - it is probably safe enough to generate
        # enough SNPs to work with (het in parent + filled in all children), since it has worked in the past.
        # If not both parents are genotyped, use filled children only to generate enough relevant SNPs.
        genotyped_children = np.array([x for x in family.children_array if problem.is_genotyped(x)])
        filled_children = genotyped_children if num_genotyped_parents == 2 else \
            problem.find_samples_with_fill_ge(params.surrogate_parent_fill_threshold, sample=genotyped_children)[:, 0].astype(np.int)
        comparator = ic.ChildComparator(request, family, filled_children)
        # for parent_type, parent in reversed(family.parents_dict().items()):
        for parent_type, parent in genotyped_parent_dict:
            # het_snps = gt.where_heterozygous(h.data, parent)
            het_snps = gt.where_heterozygous(g.data, parent)
            if h.fill_fraction(sample=parent, snps=het_snps) < params.het_fill_threshold:
            # if is_founder[parent]:
                # Choose template = most-filled child
                fill = problem.fill_fraction(sample=filled_children)
                if params.debug:
                    print '=' * 105
                    print 'Children comparison in', family, 'parent_type', parent_type
                    print '=' * 105
                    print [problem.is_genotyped(x) for x in family.children]
                    print '# genotyped children', sum(problem.is_genotyped(x) for x in family.children),
                    print '# parent het snps', len(het_snps)
                    print 'Filled children', filled_children
                    print 'Family''s fill:\n', problem.fill_fraction(sample=family.member_set)
                template_child = int(fill[np.argmax(fill[:, 1]), 0])
                if params.debug:
                    'template_child', template_child
                # Choose a template child at random (first index in the children list)
                # template_child = list(family.children)[0]
                (_, _, info) = comparator.child_recombinations(parent_type, template_child)
                # Save selected entries from the family info class in problem
                problem.set_family_info(family, info)
                # Impute parent from the template child
                if params.debug:
                    print family, parent_type
                    print 'Child recombinations'
                    print info.recombination_snp
                comparator.phase_parent_by_template(info)
                # Now phase children (and possibly some more of the parent) using IBD segments
                # found among them and the parent
                ibd.phase_by_ibd(request, info.ibs_segments(), 'majority')