Skip to content

Commit

Permalink
diversity complete
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Apr 29, 2024
1 parent 9490bcc commit cc3f3bf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
11 changes: 9 additions & 2 deletions jcvi/graphics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import os.path as op
from os import remove

import sys
import logging

Expand All @@ -12,6 +13,7 @@
logging.getLogger("PIL").setLevel(logging.INFO)

from functools import partial
from typing import Optional, List, Tuple, Union

import numpy as np
import matplotlib as mpl
Expand All @@ -35,7 +37,6 @@
FancyArrowPatch,
FancyBboxPatch,
)
from typing import Optional, List, Tuple, Union

from ..apps.base import datadir, glob, listify, logger, sample_N, which
from ..formats.base import LineFile
Expand Down Expand Up @@ -275,14 +276,20 @@ def prettyplot():


def normalize_axes(axes):
"""
Normalize the axes to have the same scale.
"""
axes = listify(axes)
for ax in axes:
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_axis_off()


def panel_labels(ax, labels, size=16):
def panel_labels(ax, labels, size: int = 16):
"""
Add panel labels (A, B, ...) to a figure.
"""
for xx, yy, panel_label in labels:
if rcParams["text.usetex"]:
panel_label = r"$\textbf{{{0}}}$".format(panel_label)
Expand Down
6 changes: 3 additions & 3 deletions jcvi/graphics/landscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,11 @@ def draw_depth(
alpha=0.5,
)

# vertical lines for all the breaks
# Vertical lines for all the breaks
for pos in starts.values():
ax.plot((pos, pos), (0, maxdepth), "-", lw=1, color=sepcolor)

# beautify the numeric axis
# Beautify the numeric axis
for tick in ax.get_xticklines() + ax.get_yticklines():
tick.set_visible(False)

Expand Down Expand Up @@ -444,7 +444,7 @@ def draw_multi_depth(
bed = Bed(bedfile)

if ypos > 0.001:
root.plot((0, 1), (ypos, ypos), "-", lw=2, color="lightslategray")
root.plot((0.02, 0.98), (ypos, ypos), "-", lw=2, color="lightslategray")

title = titleinfo.get(bedfile, pf.split("_", 1)[0])
subtitle = None
Expand Down
26 changes: 23 additions & 3 deletions jcvi/projects/jcvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

import sys

import networkx as nx

from ..apps.base import ActionDispatcher, OptionParser, logger
from ..assembly.geneticmap import draw_geneticmap_heatmap
from ..assembly.hic import draw_hic_heatmap
from ..assembly.kmer import draw_ks_histogram
from ..compara.pedigree import Pedigree, calculate_inbreeding
from ..graphics.base import normalize_axes, panel_labels, plt, savefig
from ..graphics.landscape import draw_multi_depth

Expand Down Expand Up @@ -111,6 +114,23 @@ def diversity(args):

# Panel A
logger.info("Plotting pedigree")
ped = Pedigree(pedfile)
pngfile = f"{pedfile}.png"
inb = calculate_inbreeding(ped, ploidy=4, N=10000)

G = ped.to_graph(inb, title="Pedigree of Variety1")
A = nx.nx_agraph.to_agraph(G)
dpi = 300
A.draw(pngfile, prog="dot", args=f"-Gdpi={dpi}")
logger.info("Pedigree graph written to `%s`", pngfile)

M = plt.imread(pngfile)
width, height = M.shape[1] / dpi, M.shape[0] / dpi
logger.info("Image size: %.2f x %.2f (dpi=%d)", width, height, dpi)

# Force aspect ratio to be equal
ax1_root.imshow(M)
ax1_root.set_axis_off()

# Panel B
logger.info("Plotting depth distribution across genomes")
Expand All @@ -119,7 +139,7 @@ def diversity(args):
ypos = 1 - yinterval
panel_roots, panel_axes = [], []
for _ in range(npanels):
panel_root = root if npanels == 1 else fig.add_axes((0.25, ypos, 1, yinterval))
panel_root = fig.add_axes((0.25, ypos, 0.75, yinterval))
panel_ax = fig.add_axes(
(0.25 + 0.1 * 0.75, ypos + 0.2 * yinterval, 0.8 * 0.75, 0.65 * yinterval)
)
Expand All @@ -139,11 +159,11 @@ def diversity(args):
)

labels = (
(0.25 * 0.1, 0.95, "A"),
(0.02, 0.95, "A"),
(0.25 + 0.25 * 0.1, 0.95, "B"),
)
panel_labels(root, labels)
normalize_axes([root, ax1_root, ax2_root])
normalize_axes([root, ax2_root])

image_name = "diversity.pdf"
savefig(image_name, dpi=iopts.dpi, iopts=iopts)
Expand Down
2 changes: 1 addition & 1 deletion jcvi/projects/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def waterlilyGOM(args):
pf = datafile.rsplit(".", 1)[0]

fig = plt.figure(1, (iopts.w, iopts.h))
root = fig.add_axes([0, 0, 1, 1])
root = fig.add_axes((0, 0, 1, 1))

margin, rmargin = 0.15, 0.19 # Left and right margin
leafinfo = LeafInfoFile("leafinfo.csv").cache
Expand Down

0 comments on commit cc3f3bf

Please sign in to comment.