Skip to content

Commit

Permalink
Fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Apr 28, 2024
1 parent 71c0563 commit e693671
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions jcvi/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from socket import gethostname
from subprocess import PIPE, call, check_output
from optparse import OptionParser as OptionP, OptionGroup, SUPPRESS_HELP
from typing import Any, Collection, List, Optional, Union
from typing import Any, Collection, List, Optional, Tuple, Union

from natsort import natsorted
from rich.console import Console
Expand All @@ -39,7 +39,7 @@
os.environ["LC_ALL"] = "C"
JCVIHELP = "JCVI utility libraries {} [{}]\n".format(__version__, __copyright__)

TextCollection = Union[str, Iterable[str]]
TextCollection = Union[str, List[str], Tuple[str]]


def debug(level=logging.DEBUG):
Expand Down
8 changes: 6 additions & 2 deletions jcvi/assembly/geneticmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from functools import lru_cache
from itertools import combinations, groupby
from random import sample
from typing import List

import numpy as np
import seaborn as sns
Expand All @@ -20,7 +21,7 @@
from ..algorithms.matrix import symmetrize
from ..formats.base import BaseFile, LineFile, must_open, read_block
from ..formats.bed import Bed, fastaFromBed
from ..graphics.base import plt, savefig, Rectangle, draw_cmap
from ..graphics.base import Rectangle, draw_cmap, plt, plot_heatmap, savefig


MSTheader = """population_type {0}
Expand Down Expand Up @@ -332,7 +333,10 @@ def dotplot(args):


@lru_cache(maxsize=None)
def calc_ldscore(a, b):
def calc_ldscore(a: List[str], b: List[str]) -> float:
"""
Calculate Linkage disequilibrium (r2) between two genotypes.
"""
assert len(a) == len(b), "{0}\n{1}".format(a, b)
# Assumes markers as A/B
c = Counter(zip(a, b))
Expand Down
8 changes: 3 additions & 5 deletions jcvi/assembly/hic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
savefig,
)
from ..graphics.dotplot import dotplot
from ..utils.cbook import gene_name, human_size
from ..utils.cbook import gene_name

# Map orientations to ints
FF = {"+": 1, "-": -1, "?": 1}
Expand Down Expand Up @@ -782,9 +782,7 @@ def heatmap(args):
breaks = sorted(breaks)[1:]
if contig or opts.nobreaks:
breaks = []
binsize = human_size(resolution, precision=0)
title = f"Resolution = {binsize} per bin"
plot_heatmap(ax, B, breaks, iopts, groups=new_groups, title=title)
plot_heatmap(ax, B, breaks, groups=new_groups, binsize=resolution)

# Title
pf = npyfile.rsplit(".", 1)[0]
Expand Down Expand Up @@ -1639,7 +1637,7 @@ def movieframe(args):
ax2_root = fig.add_axes([0.5, 0, 0.5, 1]) # dot plot canvas

# Left axis: heatmap
plot_heatmap(ax1, M, breaks, iopts)
plot_heatmap(ax1, M, breaks, binsize=BINSIZE)

# Right axis: synteny
qbed, sbed, qorder, sorder, is_self = check_beds(anchorsfile, p, opts, sorted=False)
Expand Down
24 changes: 12 additions & 12 deletions jcvi/graphics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
logging.getLogger("numexpr").setLevel(logging.WARNING)
logging.getLogger("PIL").setLevel(logging.INFO)


from functools import partial

import numpy as np
Expand Down Expand Up @@ -39,6 +38,7 @@

from ..apps.base import datadir, glob, listify, logger, sample_N, which
from ..formats.base import LineFile
from ..utils.cbook import human_size


CHARS = {
Expand Down Expand Up @@ -333,7 +333,7 @@ def savefig(figname, dpi=150, iopts=None, cleanup=True):


# human readable size (Kb, Mb, Gb)
def human_readable(x, pos, base=False):
def human_readable(x: Union[str, int], _, base=False):
x = str(int(x))
if x.endswith("000000000"):
x = x[:-9] + "G"
Expand Down Expand Up @@ -504,10 +504,9 @@ def plot_heatmap(
ax,
M: np.array,
breaks: List[int],
iopts: ImageOptions,
groups: List[Tuple[int, int, List[Tuple[int, str]], str]] = [],
plot_breaks: bool = False,
title: str = "",
binsize: Optional[int] = None,
):
"""Plot heatmap illustrating the contact probabilities in Hi-C data.
Expand All @@ -518,7 +517,7 @@ def plot_heatmap(
iopts (OptionParser options): Graphical options passed in from commandline
groups (List, optional): [(start, end, [(position, seqid)], color)]. Defaults to [].
plot_breaks (bool): Whether to plot white breaks. Defaults to False.
title (str): Title of the heatmap at bottom
binsize (int, optional): Resolution of the heatmap.
"""
cmap = sns.cubehelix_palette(rot=0.5, as_cmap=True)
ax.imshow(M, cmap=cmap, interpolation="none")
Expand Down Expand Up @@ -550,13 +549,14 @@ def simplify_seqid(seqid):
ax.set_xticklabels(ax.get_xticks(), family="Helvetica", color="gray")
ax.set_yticklabels(ax.get_yticks(), family="Helvetica", color="gray", rotation=90)
ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)
formatter = ticker.FuncFormatter(
lambda x, pos: human_readable(int(x) * binsize, pos, base=True)
)
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)
binlabel = "Resolution = {} per bin".format(human_size(binsize, precision=0))
ax.set_xlabel(binlabel)
if binsize is not None:
formatter = ticker.FuncFormatter(
lambda x, pos: human_readable(int(x) * binsize, pos, base=True)
)
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)
title = f"Resolution = {human_size(binsize, precision=0)} per bin"
ax.set_xlabel(title)


def discrete_rainbow(N=7, cmap=cm.Set1, usepreset=True, shuffle=False, plot=False):
Expand Down

0 comments on commit e693671

Please sign in to comment.