Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

populate symbols for samtools and bcftools. #1143

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions pysam/bcftools.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
from typing import Final

from pysam.utils import PysamDispatcher

BCFTOOLS_DISPATCH = [
"index",
"annotate",
"concat",
"convert",
"isec",
"merge",
"norm",
"plugin",
"query",
"reheader",
"sort",
"view",
"head",
"call",
"consensus",
"cnv",
"csq",
"filter",
"gtcheck",
"mpileup",
"roh",
"stats"]

# instantiate bcftools commands as python functions
for cmd in BCFTOOLS_DISPATCH:
globals()[cmd] = PysamDispatcher("bcftools", cmd, None)
def _wrap_command(dispatch: str) -> PysamDispatcher:
return PysamDispatcher("bcftools", dispatch, ())


index: Final[PysamDispatcher] = _wrap_command("index")
annotate: Final[PysamDispatcher] = _wrap_command("annotate")
concat: Final[PysamDispatcher] = _wrap_command("concat")
convert: Final[PysamDispatcher] = _wrap_command("convert")
isec: Final[PysamDispatcher] = _wrap_command("isec")
merge: Final[PysamDispatcher] = _wrap_command("merge")
norm: Final[PysamDispatcher] = _wrap_command("norm")
plugin: Final[PysamDispatcher] = _wrap_command("plugin")
query: Final[PysamDispatcher] = _wrap_command("query")
reheader: Final[PysamDispatcher] = _wrap_command("reheader")
sort: Final[PysamDispatcher] = _wrap_command("sort")
view: Final[PysamDispatcher] = _wrap_command("view")
head: Final[PysamDispatcher] = _wrap_command("head")
call: Final[PysamDispatcher] = _wrap_command("call")
consensus: Final[PysamDispatcher] = _wrap_command("consensus")
cnv: Final[PysamDispatcher] = _wrap_command("cnv")
csq: Final[PysamDispatcher] = _wrap_command("csq")
filter: Final[PysamDispatcher] = _wrap_command("filter")
gtcheck: Final[PysamDispatcher] = _wrap_command("gtcheck")
mpileup: Final[PysamDispatcher] = _wrap_command("mpileup")
roh: Final[PysamDispatcher] = _wrap_command("roh")
stats: Final[PysamDispatcher] = _wrap_command("stats")
217 changes: 169 additions & 48 deletions pysam/samtools.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,176 @@
from typing import (
Final,
Callable,
List,
Tuple,
Iterable,
Union,
)

from pysam.utils import PysamDispatcher

# samtools command line options to export in python
SAMTOOLS_DISPATCH = {
_SAMTOOLS_DISPATCH = {
# samtools 'documented' commands
"view": ("view", None),
"head": ("head", None),
"sort": ("sort", None),
"mpileup": ("mpileup", None),
"consensus": ("consensus", None),
"depth": ("depth", None),
"faidx": ("faidx", None),
"fqidx": ("fqidx", None),
"tview": ("tview", None),
"index": ("index", None),
"idxstats": ("idxstats", None),
"fixmate": ("fixmate", None),
"flagstat": ("flagstat", None),
"calmd": ("calmd", None),
"merge": ("merge", None),
"markdup": ("markdup", None),
"rmdup": ("rmdup", None),
"reference": ("reference", None),
"reheader": ("reheader", None),
"cat": ("cat", None),
"targetcut": ("targetcut", None),
"phase": ("phase", None),
"bam2fq": ("bam2fq", None),
"dict": ("dict", None),
"addreplacerg": ("addreplacerg", None),
"pad2unpad": ("pad2unpad", None),
"depad": ("pad2unpad", None),
"bedcov": ("bedcov", None),
"coverage": ("coverage", None),
"bamshuf": ("bamshuf", None),
"collate": ("collate", None),
"stats": ("stats", None),
"fasta": ("fasta", None),
"fastq": ("fastq", None),
"quickcheck": ("quickcheck", None),
"split": ("split", None),
"flags": ("flags", None),
"ampliconclip": ("ampliconclip", None),
"ampliconstats": ("ampliconstats", None),
"version": ("version", None),
"fqimport": ("import", None),
"samples": ("samples", None),
"view": ("view", ()),
"head": ("head", ()),
"sort": ("sort", ()),
"mpileup": ("mpileup", ()),
"consensus": ("consensus", ()),
"depth": ("depth", ()),
"faidx": ("faidx", ()),
"fqidx": ("fqidx", ()),
"tview": ("tview", ()),
"index": ("index", ()),
"idxstats": ("idxstats", ()),
"fixmate": ("fixmate", ()),
"flagstat": ("flagstat", ()),
"calmd": ("calmd", ()),
"merge": ("merge", ()),
"markdup": ("markdup", ()),
"rmdup": ("rmdup", ()),
"reference": ("reference", ()),
"reheader": ("reheader", ()),
"cat": ("cat", ()),
"targetcut": ("targetcut", ()),
"phase": ("phase", ()),
"bam2fq": ("bam2fq", ()),
"dict": ("dict", ()),
"addreplacerg": ("addreplacerg", ()),
"pad2unpad": ("pad2unpad", ()),
"depad": ("pad2unpad", ()),
"bedcov": ("bedcov", ()),
"coverage": ("coverage", ()),
"bamshuf": ("bamshuf", ()),
"collate": ("collate", ()),
"stats": ("stats", ()),
"fasta": ("fasta", ()),
"fastq": ("fastq", ()),
"quickcheck": ("quickcheck", ()),
"split": ("split", ()),
"flags": ("flags", ()),
"ampliconclip": ("ampliconclip", ()),
"ampliconstats": ("ampliconstats", ()),
"version": ("version", ()),
"fqimport": ("import", ()),
"import_": ("import", ()),
"samples": ("samples", ()),
}

# instantiate samtools commands as python functions
for key, options in SAMTOOLS_DISPATCH.items():
cmd, parser = options
globals()[key] = PysamDispatcher("samtools", cmd, parser)

__all__ = list(SAMTOOLS_DISPATCH)
def _wrap_command(
dispatch: str,
parsers: Iterable[Tuple[str, Callable[[Union[str, List[str]]], Union[str, List[str]]]]],
) -> PysamDispatcher:
return PysamDispatcher("samtools", dispatch, parsers)


view: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["view"][0], _SAMTOOLS_DISPATCH["view"][1])

head: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["head"][0], _SAMTOOLS_DISPATCH["head"][1])

sort: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["sort"][0], _SAMTOOLS_DISPATCH["sort"][1])

mpileup: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["mpileup"][0], _SAMTOOLS_DISPATCH["mpileup"][1])

consensus: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["consensus"][0],
_SAMTOOLS_DISPATCH["consensus"][1],
)

depth: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["depth"][0], _SAMTOOLS_DISPATCH["depth"][1])

faidx: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["faidx"][0], _SAMTOOLS_DISPATCH["faidx"][1])

fqidx: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["fqidx"][0], _SAMTOOLS_DISPATCH["fqidx"][1])

tview: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["tview"][0], _SAMTOOLS_DISPATCH["tview"][1])

index: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["index"][0], _SAMTOOLS_DISPATCH["index"][1])

idxstats: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["idxstats"][0], _SAMTOOLS_DISPATCH["idxstats"][1])

fixmate: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["fixmate"][0], _SAMTOOLS_DISPATCH["fixmate"][1])

flagstat: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["flagstat"][0], _SAMTOOLS_DISPATCH["flagstat"][1])

calmd: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["calmd"][0], _SAMTOOLS_DISPATCH["calmd"][1])

merge: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["merge"][0], _SAMTOOLS_DISPATCH["merge"][1])

markdup: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["markdup"][0], _SAMTOOLS_DISPATCH["markdup"][1])

rmdup: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["rmdup"][0], _SAMTOOLS_DISPATCH["rmdup"][1])

reference: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["reference"][0],
_SAMTOOLS_DISPATCH["reference"][1],
)

reheader: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["reheader"][0], _SAMTOOLS_DISPATCH["reheader"][1])

cat: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["cat"][0], _SAMTOOLS_DISPATCH["cat"][1])

targetcut: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["targetcut"][0],
_SAMTOOLS_DISPATCH["targetcut"][1],
)

phase: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["phase"][0], _SAMTOOLS_DISPATCH["phase"][1])

bam2fq: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["bam2fq"][0], _SAMTOOLS_DISPATCH["bam2fq"][1])

dict: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["dict"][0], _SAMTOOLS_DISPATCH["dict"][1])

addreplacerg: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["addreplacerg"][0],
_SAMTOOLS_DISPATCH["addreplacerg"][1],
)

pad2unpad: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["pad2unpad"][0],
_SAMTOOLS_DISPATCH["pad2unpad"][1],
)

depad: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["depad"][0], _SAMTOOLS_DISPATCH["depad"][1])

bedcov: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["bedcov"][0], _SAMTOOLS_DISPATCH["bedcov"][1])

coverage: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["coverage"][0], _SAMTOOLS_DISPATCH["coverage"][1])

bamshuf: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["bamshuf"][0], _SAMTOOLS_DISPATCH["bamshuf"][1])

collate: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["collate"][0], _SAMTOOLS_DISPATCH["collate"][1])

stats: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["stats"][0], _SAMTOOLS_DISPATCH["stats"][1])

fasta: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["fasta"][0], _SAMTOOLS_DISPATCH["fasta"][1])

fastq: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["fastq"][0], _SAMTOOLS_DISPATCH["fastq"][1])

quickcheck: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["quickcheck"][0],
_SAMTOOLS_DISPATCH["quickcheck"][1],
)

split: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["split"][0], _SAMTOOLS_DISPATCH["split"][1])

flags: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["flags"][0], _SAMTOOLS_DISPATCH["flags"][1])

ampliconclip: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["ampliconclip"][0],
_SAMTOOLS_DISPATCH["ampliconclip"][1],
)

ampliconstats: Final[PysamDispatcher] = _wrap_command(
_SAMTOOLS_DISPATCH["ampliconstats"][0],
_SAMTOOLS_DISPATCH["ampliconstats"][1],
)

version: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["version"][0], _SAMTOOLS_DISPATCH["version"][1])

fqimport: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["fqimport"][0], _SAMTOOLS_DISPATCH["fqimport"][1])

import_: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["import_"][0], _SAMTOOLS_DISPATCH["import_"][1])

samples: Final[PysamDispatcher] = _wrap_command(_SAMTOOLS_DISPATCH["samples"][0], _SAMTOOLS_DISPATCH["samples"][1])
20 changes: 17 additions & 3 deletions pysam/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
from typing import (
Callable,
List,
Tuple,
Iterable,
Union,
)

from pysam.libcutils import _pysam_dispatch


Expand Down Expand Up @@ -36,14 +44,20 @@ class PysamDispatcher(object):
parsers = None
collection = None

def __init__(self, collection, dispatch, parsers):
def __init__(
self,
collection: str,
dispatch: str,
parsers: Iterable[Tuple[str, Callable[[Union[str, List[str]]], Union[str, List[str]]]]],
):
self.collection = collection
self.dispatch = dispatch
self.parsers = parsers
self.stderr = []

def __call__(self, *args, **kwargs):
'''execute a samtools command.
def __call__(self, *args: str, **kwargs) -> Union[str, List[str]]:
'''
execute a samtools command.

Keyword arguments:
catch_stdout -- redirect stdout from the samtools command and
Expand Down