Beispiel #1
0
    def test_calc_r2_windows(self):
        variations = VariationsArrays()
        chrom = numpy.array([b'chr1'] * 4)
        pos = numpy.array([1, 4, 6, 20])
        gts = numpy.array([[[0, 0], [1, 1], [0, 0]],
                           [[0, 0], [1, 1], [0, 0]],
                           [[1, 1], [0, 0], [1, 1]],
                           [[0, 0], [0, 1], [-1, -1]]])
        variations['/variations/chrom'] = chrom
        variations['/variations/pos'] = pos
        variations['/calls/GT'] = gts
        expected = [1.0, 1.0000002, 1.0, 1.0000002, 1.0, 1.0]
        assert numpy.allclose(_calc_r2(gts), expected)

        chrom, pos, r2 = calc_r2_windows(variations, 10)
        assert numpy.allclose(r2, [1.0000002384185933, numpy.nan],
                              equal_nan=True)
        assert numpy.all(chrom == b'chr1')
    def test_calc_r2_windows(self):
        variations = VariationsArrays()
        chrom = numpy.array([b'chr1'] * 4)
        pos = numpy.array([1, 4, 6, 20])
        gts = numpy.array([[[0, 0], [1, 1], [0, 0]],
                           [[0, 0], [1, 1], [0, 0]],
                           [[1, 1], [0, 0], [1, 1]],
                           [[0, 0], [0, 1], [-1, -1]]])
        variations['/variations/chrom'] = chrom
        variations['/variations/pos'] = pos
        variations['/calls/GT'] = gts
        expected = [1.0, 1.0000002, 1.0, 1.0000002, 1.0, 1.0]
        assert numpy.allclose(_calc_r2(gts), expected)

        chrom, pos, r2 = calc_r2_windows(variations, 10)
        assert numpy.allclose(r2, [1.0000002384185933, numpy.nan],
                              equal_nan=True)
        assert numpy.all(chrom == b'chr1')
def plot_r2(variations, window_size, data_dir, write_bg=False):
    
    # Calculate LD r2 parameter in windows
    chrom, pos, r2 = calc_r2_windows(variations, window_size=window_size)
    
    # Plot r2 distribution
    fpath = os.path.join(data_dir, 'r2_distrib.png')
    distrib, bins = histogram(r2, n_bins=50, range_=(0, 1))
    title = 'r2 distribution in windows of {} bp'.format(window_size)
    mpl_params={'set_xlabel': {'args': ['r2'], 'kwargs': {}},
                'set_ylabel': {'args': ['Number of windows'], 'kwargs': {}},
                'set_title': {'args': [title], 'kwargs': {}}}
    plot_distrib(distrib, bins, fhand=open(fpath, 'w'), figsize=(7, 7),
                 mpl_params=mpl_params)
    
    # Manhattan plot
    mask = numpy.logical_not(numpy.isnan(r2))
    chrom = chrom[mask]
    pos = pos[mask]
    r2 = r2[mask]
    fpath = os.path.join(data_dir, 'r2_manhattan.png')
    title = 'r2 along genome in windows of {} bp'.format(window_size)
    mpl_params={'set_xlabel': {'args': ['Chromosome'], 'kwargs': {}},
                'set_ylabel': {'args': ['r2'], 'kwargs': {}},
                'set_title': {'args': [title], 'kwargs': {}}}
    manhattan_plot(chrom, pos, r2, fhand=open(fpath, 'w'), figsize=(15, 7),
                   marker='k', mpl_params=mpl_params)
    
    # Write bg
    if write_bg:
        fpath = os.path.join(data_dir, 'r2_windows_{}.png'.format(window_size))
        bg_fhand = open(fpath, 'w')
        pos_r2 = PositionalStatsCalculator(chrom, pos, r2,
                                           window_size=window_size,
                                           step=window_size,
                                           take_windows=False)
        description = 'mean r2 in windows of {} bp'.format(window_size)
        pos_r2.write(bg_fhand, 'r2', description, track_type='bedgraph')
Beispiel #4
0
def plot_r2(variations, window_size, data_dir, write_bg=False):

    # Calculate LD r2 parameter in windows
    chrom, pos, r2 = calc_r2_windows(variations, window_size=window_size)

    # Plot r2 distribution
    fpath = os.path.join(data_dir, 'r2_distrib.png')
    distrib, bins = histogram(r2, n_bins=50, range_=(0, 1))
    title = 'r2 distribution in windows of {} bp'.format(window_size)
    mpl_params = {
        'set_xlabel': {
            'args': ['r2'],
            'kwargs': {}
        },
        'set_ylabel': {
            'args': ['Number of windows'],
            'kwargs': {}
        },
        'set_title': {
            'args': [title],
            'kwargs': {}
        }
    }
    plot_distrib(distrib,
                 bins,
                 fhand=open(fpath, 'w'),
                 figsize=(7, 7),
                 mpl_params=mpl_params)

    # Manhattan plot
    mask = numpy.logical_not(numpy.isnan(r2))
    chrom = chrom[mask]
    pos = pos[mask]
    r2 = r2[mask]
    fpath = os.path.join(data_dir, 'r2_manhattan.png')
    title = 'r2 along genome in windows of {} bp'.format(window_size)
    mpl_params = {
        'set_xlabel': {
            'args': ['Chromosome'],
            'kwargs': {}
        },
        'set_ylabel': {
            'args': ['r2'],
            'kwargs': {}
        },
        'set_title': {
            'args': [title],
            'kwargs': {}
        }
    }
    manhattan_plot(chrom,
                   pos,
                   r2,
                   fhand=open(fpath, 'w'),
                   figsize=(15, 7),
                   marker='k',
                   mpl_params=mpl_params)

    # Write bg
    if write_bg:
        fpath = os.path.join(data_dir, 'r2_windows_{}.png'.format(window_size))
        bg_fhand = open(fpath, 'w')
        pos_r2 = PositionalStatsCalculator(chrom,
                                           pos,
                                           r2,
                                           window_size=window_size,
                                           step=window_size,
                                           take_windows=False)
        description = 'mean r2 in windows of {} bp'.format(window_size)
        pos_r2.write(bg_fhand, 'r2', description, track_type='bedgraph')