Skip to content

Commit

Permalink
Allow testing of karyotype and synteny (#661)
Browse files Browse the repository at this point in the history
* remove set_usage()

* Allow testing of graphics.karyotype

* Add missing testdata files

* Test synteny as well
  • Loading branch information
tanghaibao authored May 8, 2024
1 parent 06ca1aa commit 0c55cf6
Show file tree
Hide file tree
Showing 14 changed files with 73,831 additions and 20 deletions.
10 changes: 5 additions & 5 deletions jcvi/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import time

from argparse import ArgumentParser, SUPPRESS
from collections.abc import Iterable
from configparser import (
ConfigParser,
Expand All @@ -20,14 +21,13 @@
NoSectionError,
ParsingError,
)

from argparse import ArgumentParser, SUPPRESS
from http.client import HTTPSConnection
from socket import gethostname
from subprocess import CalledProcessError, PIPE, call, check_output
from time import ctime
from typing import Any, Collection, List, Optional, Tuple, Union
from urllib.parse import urlencode

from natsort import natsorted
from rich.console import Console
from rich.logging import RichHandler
Expand Down Expand Up @@ -277,7 +277,7 @@ def set_outfile(self, outfile: Optional[str] = "stdout"):
"""
self.add_argument("-o", "--outfile", default=outfile, help="Outfile name")

def set_outdir(self, outdir="."):
def set_outdir(self, outdir: Optional[str] = "."):
self.add_argument("--outdir", default=outdir, help="Specify output directory")

def set_email(self):
Expand Down Expand Up @@ -735,7 +735,7 @@ def set_pairs(self):
part, e.g. /1, /2, or .f, .r, default behavior is to truncate until last
char.
"""
self.set_usage(self.set_pairs.__doc__)
self.usage = self.set_pairs.__doc__

self.add_argument(
"--pairsfile", default=None, help="Write valid pairs to pairsfile"
Expand Down Expand Up @@ -2225,7 +2225,7 @@ def inspect(object):
print("{}: {}".format(k, details), file=sys.stderr)


def sample_N(a: Collection[Any], N: int, seed: Optional[int] = None) -> List[Any]:
def sample_N(a: Collection, N: int, seed: Optional[int] = None) -> List:
"""
When size of N is > size of a, random.sample() will emit an error:
ValueError: sample larger than population
Expand Down
8 changes: 4 additions & 4 deletions jcvi/apps/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def ensembl(args):

valid_species = [x for x in ls_ftp(fasta_url) if "." not in x]
doc = "\n".join((ensembl.__doc__, tile(valid_species)))
p.set_usage(doc)
p.usage = doc

if len(args) != 1:
sys.exit(not p.print_help())
Expand Down Expand Up @@ -273,15 +273,15 @@ def phytozome(args):
downloader=downloader,
)
g = GlobusXMLParser(directory_listing)
except:
except Exception as _:
logger.error("Error downloading directory listing ... cleaning up")
cleanup(directory_listing, cookies)
sys.exit(1)

genomes = g.get_genomes()
valid_species = genomes.keys()
species_tile = tile(valid_species)
p.set_usage("\n".join((phytozome.__doc__, species_tile)))
p.usage = "\n".join((phytozome.__doc__, species_tile))

if len(args) != 1:
sys.exit(not p.print_help())
Expand Down Expand Up @@ -383,7 +383,7 @@ def phytozome9(args):
valid_species = [x for x in ls_ftp(url) if "." not in x]

doc = "\n".join((phytozome9.__doc__, tile(valid_species)))
p.set_usage(doc)
p.usage = doc

if len(args) != 1:
sys.exit(not p.print_help())
Expand Down
10 changes: 6 additions & 4 deletions jcvi/graphics/karyotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import sys

from typing import Optional
from typing import List, Optional

from ..apps.base import OptionParser, logger
from ..compara.synteny import SimpleFile
Expand Down Expand Up @@ -408,7 +408,7 @@ def __init__(
self.layout = layout


def main():
def main(args: List[str]):
p = OptionParser(__doc__)
p.add_argument(
"--basepair",
Expand Down Expand Up @@ -441,7 +441,7 @@ def main():
help="Style of chromosome labels",
)
p.set_outfile("karyotype.pdf")
opts, args, iopts = p.set_image_options(figsize="8x7")
opts, args, iopts = p.set_image_options(args, figsize="8x7")

if len(args) != 2:
sys.exit(not p.print_help())
Expand All @@ -467,6 +467,8 @@ def main():
image_name = update_figname(opts.outfile, iopts.format)
savefig(image_name, dpi=iopts.dpi, iopts=iopts)

return image_name


if __name__ == "__main__":
main()
main(sys.argv[1:])
12 changes: 8 additions & 4 deletions jcvi/graphics/synteny.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"""

import sys
from typing import Optional

from typing import List, Optional

import numpy as np

from matplotlib import transforms
from matplotlib.path import Path

Expand Down Expand Up @@ -626,7 +628,7 @@ def draw_gene_legend(
ax.text(xr, ytop + d / 2, "repeat", ha="center")


def main():
def main(args: List[str]):
p = OptionParser(__doc__)
p.add_argument("--switch", help="Rename the seqid with two-column file")
p.add_argument("--tree", help="Display trees on the bottom of the figure")
Expand Down Expand Up @@ -684,7 +686,7 @@ def main():
action="store_true",
help="If set, do not exclude small features from annotation track (<1%% of region)",
)
opts, args, iopts = p.set_image_options(figsize="8x7")
opts, args, iopts = p.set_image_options(args, figsize="8x7")

if len(args) != 3:
sys.exit(not p.print_help())
Expand Down Expand Up @@ -728,6 +730,8 @@ def main():
image_name = pf + "." + iopts.format
savefig(image_name, dpi=iopts.dpi, iopts=iopts)

return image_name


if __name__ == "__main__":
main()
main(sys.argv[1:])
1 change: 1 addition & 0 deletions tests/graphics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

50 changes: 50 additions & 0 deletions tests/graphics/data/blocks
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
GSVIVT01012261001 .
GSVIVT01012259001 .
GSVIVT01012258001 .
GSVIVT01012257001 .
GSVIVT01012255001 Prupe.1G290900.1
GSVIVT01012253001 Prupe.1G290800.2
GSVIVT01012252001 Prupe.1G290700.1
GSVIVT01012250001 Prupe.1G290600.1
GSVIVT01012249001 Prupe.1G290500.1
GSVIVT01012248001 Prupe.1G290400.1
GSVIVT01012247001 Prupe.1G290200.2
GSVIVT01012246001 Prupe.1G290100.3
GSVIVT01012245001 Prupe.1G290000.1
GSVIVT01012244001 Prupe.1G289800.1
GSVIVT01012243001 .
GSVIVT01012242001 .
GSVIVT01012241001 .
GSVIVT01012240001 .
GSVIVT01012239001 Prupe.1G289700.1
GSVIVT01012238001 .
GSVIVT01012237001 Prupe.1G289300.1
GSVIVT01012236001 Prupe.1G289200.2
GSVIVT01012235001 .
GSVIVT01012234001 Prupe.1G289100.1
GSVIVT01012233001 Prupe.1G289000.1
GSVIVT01012232001 .
GSVIVT01012231001 Prupe.1G288900.1
GSVIVT01012230001 Prupe.1G288700.1
GSVIVT01012229001 .
GSVIVT01012228001 Prupe.1G288300.1
GSVIVT01012227001 Prupe.1G288100.3
GSVIVT01012226001 .
GSVIVT01012225001 Prupe.1G288000.1
GSVIVT01012224001 Prupe.1G287800.1
GSVIVT01012223001 Prupe.1G287500.1
GSVIVT01012222001 Prupe.1G287300.2
GSVIVT01012221001 Prupe.1G287300.1
GSVIVT01012220001 Prupe.1G287200.1
GSVIVT01012219001 Prupe.1G286800.1
GSVIVT01012218001 Prupe.1G286700.2
GSVIVT01012217001 .
GSVIVT01012216001 Prupe.1G285700.1
GSVIVT01012215001 Prupe.1G286600.2
GSVIVT01012214001 Prupe.1G286500.1
GSVIVT01012213001 Prupe.1G286400.1
GSVIVT01012212001 Prupe.1G286200.1
GSVIVT01012211001 Prupe.1G286100.1
GSVIVT01012210001 Prupe.1G286000.1
GSVIVT01012209001 Prupe.1G286000.1
GSVIVT01012208001 Prupe.1G285900.1
5 changes: 5 additions & 0 deletions tests/graphics/data/blocks.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# x, y, rotation, ha, va, color, ratio, label
0.5, 0.6, 0, left, center, m, 1, grape Chr1
0.5, 0.4, 0, left, center, #fc8d62, 1, peach scaffold_1
# edges
e, 0, 1
Loading

0 comments on commit 0c55cf6

Please sign in to comment.