Skip to content

Commit

Permalink
feat: gatk collectreadcount wrapper (#1315)
Browse files Browse the repository at this point in the history
### Description

Add a wrapper for gatk collect read count 

### QC

* [x] I confirm that:

For all wrappers added by this PR, 

* there is a test case which covers any introduced changes,
* `input:` and `output:` file paths in the resulting rule can be changed
arbitrarily,
* either the wrapper can only use a single core, or the example rule
contains a `threads: x` statement with `x` being a reasonable default,
* rule names in the test case are in
[snake_case](https://en.wikipedia.org/wiki/Snake_case) and somehow tell
what the rule is about or match the tools purpose or name (e.g.,
`map_reads` for a step that maps reads),
* all `environment.yaml` specifications follow [the respective best
practices](https://stackoverflow.com/a/64594513/2352071),
* wherever possible, command line arguments are inferred and set
automatically (e.g. based on file extensions in `input:` or `output:`),
* all fields of the example rules in the `Snakefile`s and their entries
are explained via comments (`input:`/`output:`/`params:` etc.),
* `stderr` and/or `stdout` are logged correctly (`log:`), depending on
the wrapped tool,
* temporary files are either written to a unique hidden folder in the
working directory, or (better) stored where the Python function
`tempfile.gettempdir()` points to (see
[here](https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir);
this also means that using any Python `tempfile` default behavior
works),
* the `meta.yaml` contains a link to the documentation of the respective
tool or command,
* `Snakefile`s pass the linting (`snakemake --lint`),
* `Snakefile`s are formatted with
[snakefmt](https://github.com/snakemake/snakefmt),
* Python wrapper scripts are formatted with
[black](https://black.readthedocs.io).
* Conda environments use a minimal amount of channels, in recommended
ordering. E.g. for bioconda, use (conda-forge, bioconda, nodefaults, as
conda-forge should have highest priority and defaults channels are
usually not needed because most packages are in conda-forge nowadays).

---------

Co-authored-by: Filipe G. Vieira <1151762+fgvieira@users.noreply.github.com>
  • Loading branch information
Smeds and fgvieira committed May 3, 2023
1 parent 158c328 commit ee55de4
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bio/gatk/collectreadcounts/environment.yaml
@@ -0,0 +1,7 @@
channels:
- conda-forge
- bioconda
- nodefaults
dependencies:
- gatk4 =4.4.0.0
- snakemake-wrapper-utils =0.5.3
15 changes: 15 additions & 0 deletions bio/gatk/collectreadcounts/meta.yaml
@@ -0,0 +1,15 @@
name: gatk CollectReadCounts
url: https://gatk.broadinstitute.org/hc/en-us/articles/360037592671-CollectReadCounts
description: |
Collects read counts at specified intervals. The count for each interval is calculated by counting the number of read starts that lie in the interval.
authors:
- Patrik Smeds
input:
- bam: BAM/SAM/CRAM file containing reads
- intervals: one or more genomic intervals over which to operate
params:
- mergingRule: interval merging rule for abutting intervals (default, OVERLAPPING_ONLY)
- java_opts: additional arguments to be passed to the java compiler, e.g. "-XX:ParallelGCThreads=10" (not for `-XmX` or `-Djava.io.tmpdir`, since they are handled automatically).
- extra: additional program arguments.
output:
- counts: output file for read counts, `tsv` or `hdf5`
15 changes: 15 additions & 0 deletions bio/gatk/collectreadcounts/test/Snakefile
@@ -0,0 +1,15 @@
rule collectreadcounts:
input:
bam=["mapped/a.bam"],
intervals=["a.interval_list"],
output:
counts="a.counts.hdf5",
log:
"logs/gatk/collectreadcounts.log",
params:
extra="", # optional
java_opts="", # optional
resources:
mem_mb=1024,
wrapper:
"master/bio/gatk/collectreadcounts"
4 changes: 4 additions & 0 deletions bio/gatk/collectreadcounts/test/a.interval_list
@@ -0,0 +1,4 @@
@HD VN:1.6 SO:coordinate
@SQ SN:ref LN:45
@SQ SN:ref2 LN:40
ref2 1 30 + TEST
Binary file added bio/gatk/collectreadcounts/test/mapped/a.bam
Binary file not shown.
Binary file added bio/gatk/collectreadcounts/test/mapped/a.bam.bai
Binary file not shown.
29 changes: 29 additions & 0 deletions bio/gatk/collectreadcounts/wrapper.py
@@ -0,0 +1,29 @@
__author__ = "Patrik Smeds"
__copyright__ = "Copyright 2023, Patrik Smed"
__email__ = "patrik.smeds@gmail.com"
__license__ = "MIT"


import tempfile
from snakemake.shell import shell
from snakemake_wrapper_utils.java import get_java_opts


extra = snakemake.params.get("extra", "")
java_opts = get_java_opts(snakemake)

mergingRule = snakemake.params.get("mergingRule", "OVERLAPPING_ONLY")

log = snakemake.log_fmt_shell(stdout=True, stderr=True)

with tempfile.TemporaryDirectory() as tmpdir:
shell(
"gatk --java-options '{java_opts}' CollectReadCounts"
" -I {snakemake.input.bam}"
" -L {snakemake.input.intervals}"
" --interval-merging-rule {mergingRule}"
" {extra}"
" --tmp-dir {tmpdir}"
" --output {snakemake.output.counts}"
" {log}"
)
6 changes: 6 additions & 0 deletions test.py
Expand Up @@ -3966,6 +3966,12 @@ def test_gatk_baserecalibratorspark():
["snakemake", "--cores", "1", "recal/a.grp", "--use-conda", "-F"],
)

@skip_if_not_modified
def test_gatk_collectreadcounts():
run(
"bio/gatk/collectreadcounts",
["snakemake", "--cores", "1", "a.counts.hdf5", "--use-conda", "-F"],
)

@skip_if_not_modified
def test_gatk_collectalleliccounts():
Expand Down

0 comments on commit ee55de4

Please sign in to comment.