def plot_all_diffs(h, id1, id2, hap1_type=None, hap2_type=None, snps=None, xaxis='snp', fig=None): '''Generate a plot of all pairs of hap differences between sample indices id1, id2 in the Haplotype object h. xaxis = ''snp'|''bp''.''' d = diff.all_diffs(h.data, id1, id2, hap1_type=hap1_type, hap2_type=hap2_type) if snps is not None: d = d[:, snps] else: snps = h.snp_range plot_sequences(d, title='Hap Differences Between Family Samples %d,%d' % (id1, id2,), x=_xaxis(h, xaxis, snps), # x=haplotype.snp['name'], xlabel='SNP position' if xaxis == 'snp' else 'Mbp', ylabel=['(%d,%d)' % (j, k,) for j in ((0, 1) if hap1_type is None else (hap1_type,)) for k in ((0, 1) if hap2_type is None else (hap2_type,))], fig=fig) return d
def plot_all_diffs(h, id1, id2, hap1_type=None, hap2_type=None, snps=None, xaxis='snp', fig=None): '''Generate a plot of all pairs of hap differences between sample indices id1, id2 in the Haplotype object h. xaxis = ''snp'|''bp''.''' d = diff.all_diffs(h.data, id1, id2, hap1_type=hap1_type, hap2_type=hap2_type) if snps is not None: d = d[:, snps] else: snps = h.snp_range plot_sequences( d, title='Hap Differences Between Family Samples %d,%d' % ( id1, id2, ), x=_xaxis(h, xaxis, snps), # x=haplotype.snp['name'], xlabel='SNP position' if xaxis == 'snp' else 'Mbp', ylabel=[ '(%d,%d)' % ( j, k, ) for j in ((0, 1) if hap1_type is None else (hap1_type, )) for k in ((0, 1) if hap2_type is None else (hap2_type, )) ], fig=fig) return d
def ibs_segments(haplotype, id1, id2, hap1_type, hap2_type, snps=None, include_alt_phase=False, error_filter='median', error_filter_length=5, length_bound=None, min_segment_length=INDETERMINATE, margin=0.0, debug=False): '''Return 1) Identical-by-State (IBS) segments separated by recombination events between two sample haplotypes (id1, hap1_type) and (id2, hap2_type). The 2-D output array's ith row format is (segment_start, segment_stop), (id1, hap1), (id2, hap2), (segment_start_bp, segment_stop_bp, segment_length_in_bp, num_errors_in_segment) The SNP range is [segment_start, segment_stop) where start=inclusive and stop is exclusive. 2) List of het_snp indices at which there are likely genotype errors. Options: snps - list of SNPs to base the comparison on. For parent-child comparisons, these should be heterozygous SNPs in the parent's genotype, distinguishing its haplotypes and used to locate segments. For unphased-phased individuals, these should be the list of homozygous SNPs at the unphased individual (those that have data). If not specified, all SNPs are used. length_bound - minimum segment length bound type: None: no lower bound enforced 'base_pair': output segments of at least min_segment_length [base pair] 'snp': output segments of at least min_segment_length consecutive SNPs out of the snps list. This is useful only if snps includes all SNPs (or is None) *NOTE*: min_segment_length''s units are interpreted differently depending on length_bound. margin = fraction of segment to discard near the endpoints (margin/2 is removed from each side).''' if debug: print 'Computing IBD segments between haplotypes (%d,%d), (%d,%d); filter %s length %d' % \ (id1, hap1_type, id2, hap2_type, error_filter, error_filter_length) d = diff.all_diffs(haplotype.data, id1, id2, hap1_type=hap1_type, hap2_type=hap2_type)[0] # Segment length, as defined by the input parameters segment_length = lambda f: np.inf if not length_bound else ( f.length if length_bound == 'base_pair' else f.num_snps) # @UnusedVariable # Consider informative or the specified SNPs only snps = snps if snps is not None else haplotype.snp_range snps = np.intersect1d(snps, np.where(d != INDETERMINATE)[0]) d_snps = d[snps] filtered_diff = filter_diff(d_snps, error_filter, error_filter_length) error_snps = snps[np.nonzero(d_snps - filtered_diff)[0]] # Detect edges as non-zero gradient points; output sufficiently long segments bp = haplotype.snp['base_pair'] num_snps = haplotype.num_snps if np.size(filtered_diff) == 0: # No data to consider ==> no IBD intervals can be identified segments = [] else: deriv = ndimage.convolve(filtered_diff, [1, -1]) edge = np.where(deriv != 0)[0] initial_phase = hap1_type if filtered_diff[0] == 0 else 1 - hap1_type if debug: print 'initial_phase', initial_phase # , 'edge', edge # Convert recombination locations to segments of no recombination; filter short segments segments = [ f for f in ( Segment(((x[0], x[1])), set(((id1, x[2]), (id2, hap2_type))), ( bp[x[0]], segment.stop_bp(bp, x[1], num_snps)), error_snps=segment.in_segment(error_snps, x)) for x in segment.edges_to_segments( snps, edge, initial_phase, haplotype.num_snps, hap1_type)) if segment_length(f) >= min_segment_length ] # Cut segment margins if margin >= constants.SMALL_FLOAT: segments = [ s for s in (s.middle_part(haplotype.nearest_snp, bp, margin) for s in segments) if s ] # Restrict errors to those inside segments segment_set = SegmentSet(segments, np.array(util.flattened_meshgrid(reduce(list.__add__, (s.error_snps.tolist() for s in segments)), np.array([id1, id2])), dtype=int) \ if segments else gt.empty_errors_array()) if debug: print 'ibs_segments()', segment_set print 'errors', segment_set.errors return segment_set
def ibs_segments(haplotype, id1, id2, hap1_type, hap2_type, snps=None, include_alt_phase=False, error_filter='median', error_filter_length=5, length_bound=None, min_segment_length=INDETERMINATE, margin=0.0, debug=False): '''Return 1) Identical-by-State (IBS) segments separated by recombination events between two sample haplotypes (id1, hap1_type) and (id2, hap2_type). The 2-D output array's ith row format is (segment_start, segment_stop), (id1, hap1), (id2, hap2), (segment_start_bp, segment_stop_bp, segment_length_in_bp, num_errors_in_segment) The SNP range is [segment_start, segment_stop) where start=inclusive and stop is exclusive. 2) List of het_snp indices at which there are likely genotype errors. Options: snps - list of SNPs to base the comparison on. For parent-child comparisons, these should be heterozygous SNPs in the parent's genotype, distinguishing its haplotypes and used to locate segments. For unphased-phased individuals, these should be the list of homozygous SNPs at the unphased individual (those that have data). If not specified, all SNPs are used. length_bound - minimum segment length bound type: None: no lower bound enforced 'base_pair': output segments of at least min_segment_length [base pair] 'snp': output segments of at least min_segment_length consecutive SNPs out of the snps list. This is useful only if snps includes all SNPs (or is None) *NOTE*: min_segment_length''s units are interpreted differently depending on length_bound. margin = fraction of segment to discard near the endpoints (margin/2 is removed from each side).''' if debug: print 'Computing IBD segments between haplotypes (%d,%d), (%d,%d); filter %s length %d' % \ (id1, hap1_type, id2, hap2_type, error_filter, error_filter_length) d = diff.all_diffs(haplotype.data, id1, id2, hap1_type=hap1_type, hap2_type=hap2_type)[0] # Segment length, as defined by the input parameters segment_length = lambda f: np.inf if not length_bound else (f.length if length_bound == 'base_pair' else f.num_snps) # @UnusedVariable # Consider informative or the specified SNPs only snps = snps if snps is not None else haplotype.snp_range snps = np.intersect1d(snps, np.where(d != INDETERMINATE)[0]) d_snps = d[snps] filtered_diff = filter_diff(d_snps, error_filter, error_filter_length) error_snps = snps[np.nonzero(d_snps - filtered_diff)[0]] # Detect edges as non-zero gradient points; output sufficiently long segments bp = haplotype.snp['base_pair'] num_snps = haplotype.num_snps if np.size(filtered_diff) == 0: # No data to consider ==> no IBD intervals can be identified segments = [] else: deriv = ndimage.convolve(filtered_diff, [1, -1]) edge = np.where(deriv != 0)[0] initial_phase = hap1_type if filtered_diff[0] == 0 else 1 - hap1_type if debug: print 'initial_phase', initial_phase # , 'edge', edge # Convert recombination locations to segments of no recombination; filter short segments segments = [f for f in (Segment(((x[0], x[1])), set(((id1, x[2]), (id2, hap2_type))), (bp[x[0]], segment.stop_bp(bp, x[1], num_snps)), error_snps=segment.in_segment(error_snps, x)) for x in segment.edges_to_segments(snps, edge, initial_phase, haplotype.num_snps, hap1_type)) if segment_length(f) >= min_segment_length] # Cut segment margins if margin >= constants.SMALL_FLOAT: segments = [s for s in (s.middle_part(haplotype.nearest_snp, bp, margin) for s in segments) if s] # Restrict errors to those inside segments segment_set = SegmentSet(segments, np.array(util.flattened_meshgrid(reduce(list.__add__, (s.error_snps.tolist() for s in segments)), np.array([id1, id2])), dtype=int) \ if segments else gt.empty_errors_array()) if debug: print 'ibs_segments()', segment_set print 'errors', segment_set.errors return segment_set