Esempio n. 1
0
def plot_pairwise_ld(m, colorbar=True, ax=None, imshow_kwargs=None):
    """Plot a matrix of genotype linkage disequilibrium values between
    all pairs of variants.

    Parameters
    ----------
    m : array_like
        Array of linkage disequilibrium values in condensed form.
    colorbar : bool, optional
        If True, add a colorbar to the current figure.
    ax : axes, optional
        The axes on which to draw. If not provided, a new figure will be
        created.
    imshow_kwargs : dict-like, optional
        Additional keyword arguments passed through to
        :func:`matplotlib.pyplot.imshow`.

    Returns
    -------
    ax : axes
        The axes on which the plot was drawn.

    """

    import matplotlib.pyplot as plt

    # check inputs
    m_square = ensure_square(m)

    # blank out lower triangle and flip up/down
    m_square = np.tril(m_square)[::-1, :]

    # set up axes
    if ax is None:
        # make a square figure with enough pixels to represent each variant
        x = m_square.shape[0] / plt.rcParams['figure.dpi']
        x = max(x, plt.rcParams['figure.figsize'][0])
        fig, ax = plt.subplots(figsize=(x, x))
        fig.tight_layout(pad=0)

    # setup imshow arguments
    if imshow_kwargs is None:
        imshow_kwargs = dict()
    imshow_kwargs.setdefault('interpolation', 'none')
    imshow_kwargs.setdefault('cmap', 'Greys')
    imshow_kwargs.setdefault('vmin', 0)
    imshow_kwargs.setdefault('vmax', 1)

    # plot as image
    im = ax.imshow(m_square, **imshow_kwargs)

    # tidy up
    ax.set_xticks([])
    ax.set_yticks([])
    for s in 'bottom', 'right':
        ax.spines[s].set_visible(False)
    if colorbar:
        plt.gcf().colorbar(im, shrink=.5, pad=0)

    return ax
Esempio n. 2
0
def plot_pairwise_ld(m, colorbar=True, ax=None, imshow_kwargs=None):
    """Plot a matrix of genotype linkage disequilibrium values between
    all pairs of variants.

    Parameters
    ----------
    m : array_like
        Array of linkage disequilibrium values in condensed form.
    colorbar : bool, optional
        If True, add a colorbar to the current figure.
    ax : axes, optional
        The axes on which to draw. If not provided, a new figure will be
        created.
    imshow_kwargs : dict-like, optional
        Additional keyword arguments passed through to
        :func:`matplotlib.pyplot.imshow`.

    Returns
    -------
    ax : axes
        The axes on which the plot was drawn.

    """

    import matplotlib.pyplot as plt

    # check inputs
    m_square = ensure_square(m)

    # blank out lower triangle and flip up/down
    m_square = np.tril(m_square)[::-1, :]

    # set up axes
    if ax is None:
        # make a square figure with enough pixels to represent each variant
        x = m_square.shape[0] / plt.rcParams['savefig.dpi']
        x = max(x, plt.rcParams['figure.figsize'][0])
        fig, ax = plt.subplots(figsize=(x, x))
        fig.tight_layout(pad=0)

    # setup imshow arguments
    if imshow_kwargs is None:
        imshow_kwargs = dict()
    imshow_kwargs.setdefault('interpolation', 'none')
    imshow_kwargs.setdefault('cmap', 'Greys')
    imshow_kwargs.setdefault('vmin', 0)
    imshow_kwargs.setdefault('vmax', 1)

    # plot as image
    im = ax.imshow(m_square, **imshow_kwargs)

    # tidy up
    ax.set_xticks([])
    ax.set_yticks([])
    for s in 'bottom', 'right':
        ax.spines[s].set_visible(False)
    if colorbar:
        plt.gcf().colorbar(im, shrink=.5, pad=0)

    return ax
Esempio n. 3
0
def pcoa(dist):
    """Perform principal coordinate analysis of a distance matrix, a.k.a.
    classical multi-dimensional scaling.

    Parameters
    ----------
    dist : array_like
        Distance matrix in condensed form.

    Returns
    -------
    coords : ndarray, shape (n_samples, n_dimensions)
        Transformed coordinates for the samples.
    explained_ratio : ndarray, shape (n_dimensions)
        Variance explained by each dimension.

    """
    import scipy.linalg

    # This implementation is based on the skbio.math.stats.ordination.PCoA
    # implementation, with some minor adjustments.

    # check inputs
    dist = ensure_square(dist)

    # perform scaling
    e_matrix = (dist ** 2) / -2
    row_means = e_matrix.mean(axis=1, keepdims=True)
    col_means = e_matrix.mean(axis=0, keepdims=True)
    matrix_mean = e_matrix.mean()
    f_matrix = e_matrix - row_means - col_means + matrix_mean
    eigvals, eigvecs = scipy.linalg.eigh(f_matrix)

    # deal with eigvals close to zero
    close_to_zero = np.isclose(eigvals, 0)
    eigvals[close_to_zero] = 0

    # sort descending
    idxs = eigvals.argsort()[::-1]
    eigvals = eigvals[idxs]
    eigvecs = eigvecs[:, idxs]

    # keep only positive eigenvalues
    keep = eigvals >= 0
    eigvecs = eigvecs[:, keep]
    eigvals = eigvals[keep]

    # compute coordinates
    coords = eigvecs * np.sqrt(eigvals)

    # compute ratio explained
    explained_ratio = eigvals / eigvals.sum()

    return coords, explained_ratio
Esempio n. 4
0
def pcoa(dist):
    """Perform principal coordinate analysis of a distance matrix, a.k.a.
    classical multi-dimensional scaling.

    Parameters
    ----------
    dist : array_like
        Distance matrix in condensed form.

    Returns
    -------
    coords : ndarray, shape (n_samples, n_dimensions)
        Transformed coordinates for the samples.
    explained_ratio : ndarray, shape (n_dimensions)
        Variance explained by each dimension.

    """
    import scipy.linalg

    # This implementation is based on the skbio.math.stats.ordination.PCoA
    # implementation, with some minor adjustments.

    # check inputs
    dist = ensure_square(dist)

    # perform scaling
    e_matrix = (dist**2) / -2
    row_means = np.mean(e_matrix, axis=1, keepdims=True)
    col_means = np.mean(e_matrix, axis=0, keepdims=True)
    matrix_mean = np.mean(e_matrix)
    f_matrix = e_matrix - row_means - col_means + matrix_mean
    eigvals, eigvecs = scipy.linalg.eigh(f_matrix)

    # deal with eigvals close to zero
    close_to_zero = np.isclose(eigvals, 0)
    eigvals[close_to_zero] = 0

    # sort descending
    idxs = eigvals.argsort()[::-1]
    eigvals = eigvals[idxs]
    eigvecs = eigvecs[:, idxs]

    # keep only positive eigenvalues
    keep = eigvals >= 0
    eigvecs = eigvecs[:, keep]
    eigvals = eigvals[keep]

    # compute coordinates
    coords = eigvecs * np.sqrt(eigvals)

    # compute ratio explained
    explained_ratio = eigvals / eigvals.sum()

    return coords, explained_ratio
Esempio n. 5
0
def plot_pairwise_distance(dist, labels=None, colorbar=True, ax=None, imshow_kwargs=None):
    """Plot a pairwise distance matrix.

    Parameters
    ----------
    dist : array_like
        The distance matrix in condensed form.
    labels : sequence of strings, optional
        Sample labels for the axes.
    colorbar : bool, optional
        If True, add a colorbar to the current figure.
    ax : axes, optional
        The axes on which to draw. If not provided, a new figure will be
        created.
    imshow_kwargs : dict-like, optional
        Additional keyword arguments passed through to
        :func:`matplotlib.pyplot.imshow`.

    Returns
    -------
    ax : axes
        The axes on which the plot was drawn

    """

    import matplotlib.pyplot as plt

    # check inputs
    dist_square = ensure_square(dist)

    # set up axes
    if ax is None:
        # make a square figure
        x = plt.rcParams["figure.figsize"][0]
        fig, ax = plt.subplots(figsize=(x, x))
        fig.tight_layout()

    # setup imshow arguments
    if imshow_kwargs is None:
        imshow_kwargs = dict()
    imshow_kwargs.setdefault("interpolation", "none")
    imshow_kwargs.setdefault("cmap", "jet")
    imshow_kwargs.setdefault("vmin", np.min(dist))
    imshow_kwargs.setdefault("vmax", np.max(dist))

    # plot as image
    im = ax.imshow(dist_square, **imshow_kwargs)

    # tidy up
    if labels:
        ax.set_xticks(range(len(labels)))
        ax.set_yticks(range(len(labels)))
        ax.set_xticklabels(labels, rotation=90)
        ax.set_yticklabels(labels, rotation=0)
    else:
        ax.set_xticks([])
        ax.set_yticks([])
    if colorbar:
        plt.gcf().colorbar(im, shrink=0.5)

    return ax
Esempio n. 6
0
def plot_pairwise_distance(dist,
                           labels=None,
                           colorbar=True,
                           ax=None,
                           imshow_kwargs=None):
    """Plot a pairwise distance matrix.

    Parameters
    ----------
    dist : array_like
        The distance matrix in condensed form.
    labels : sequence of strings, optional
        Sample labels for the axes.
    colorbar : bool, optional
        If True, add a colorbar to the current figure.
    ax : axes, optional
        The axes on which to draw. If not provided, a new figure will be
        created.
    imshow_kwargs : dict-like, optional
        Additional keyword arguments passed through to
        :func:`matplotlib.pyplot.imshow`.

    Returns
    -------
    ax : axes
        The axes on which the plot was drawn

    """

    import matplotlib.pyplot as plt

    # check inputs
    dist_square = ensure_square(dist)

    # set up axes
    if ax is None:
        # make a square figure
        x = plt.rcParams['figure.figsize'][0]
        fig, ax = plt.subplots(figsize=(x, x))
        fig.tight_layout()

    # setup imshow arguments
    if imshow_kwargs is None:
        imshow_kwargs = dict()
    imshow_kwargs.setdefault('interpolation', 'none')
    imshow_kwargs.setdefault('cmap', 'jet')
    imshow_kwargs.setdefault('vmin', np.min(dist))
    imshow_kwargs.setdefault('vmax', np.max(dist))

    # plot as image
    im = ax.imshow(dist_square, **imshow_kwargs)

    # tidy up
    if labels:
        ax.set_xticks(range(len(labels)))
        ax.set_yticks(range(len(labels)))
        ax.set_xticklabels(labels, rotation=90)
        ax.set_yticklabels(labels, rotation=0)
    else:
        ax.set_xticks([])
        ax.set_yticks([])
    if colorbar:
        plt.gcf().colorbar(im, shrink=.5)

    return ax