Skip to content

Commit

Permalink
feat: cnvkit diagram wrapper (#1881)
Browse files Browse the repository at this point in the history
<!-- Ensure that the PR title follows conventional commit style (<type>:
<description>)-->
<!-- Possible types are here:
https://github.com/commitizen/conventional-commit-types/blob/master/index.json
-->

### Description

<!-- Add a description of your PR here-->

### QC
<!-- Make sure that you can tick the boxes below. -->

* [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 Oct 20, 2023
1 parent 7b5dc11 commit d3cfc11
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bio/cnvkit/diagram/environment.yaml
@@ -0,0 +1,6 @@
channels:
- conda-forge
- bioconda
- nodefaults
dependencies:
- cnvkit =0.9.10
13 changes: 13 additions & 0 deletions bio/cnvkit/diagram/meta.yaml
@@ -0,0 +1,13 @@
name: "cnvkit diagram"
description: >
Draw copy number (either individual bins (.cnn, .cnr) or segments (.cns)) on chromosomes as an ideogram. If both the bin-level log2 ratios and segmentation calls are given, show them side-by-side on each chromosome (segments on the left side, bins on the right side).
url: https://cnvkit.readthedocs.io/en/stable/plots.html?highlight=diagram#diagram
input:
- cns file (optional, if cnr is provided)
- cnr/cnn file (optional, if cns is provided)
output:
- pdf report
params:
- extra: additional parameters that will be forwarded to cnvkit diagram
authors:
- Patrik Smeds
47 changes: 47 additions & 0 deletions bio/cnvkit/diagram/test/Snakefile
@@ -0,0 +1,47 @@
rule cnvkit_diagram_cns:
input:
"test.cns",
output:
"test.cns.pdf",
log:
"logs/test.cns.log",
params:
extra = "" # optional
wrapper:
"master/bio/cnvkit/diagram"

rule cnvkit_diagram_cnr:
input:
"test.cnr",
output:
"test.cnr.pdf",
log:
"logs/test.cnr.log",
params:
extra = "" # optional
wrapper:
"master/bio/cnvkit/diagram"

rule cnvkit_diagram_cnn:
input:
"test.cnn",
output:
"test.cnn.pdf",
log:
"logs/test.cnn.log",
params:
extra = "" # optional
wrapper:
"master/bio/cnvkit/diagram"

rule cnvkit_diagram_cnscnr:
input:
["test.cns", "test.cnr"]
output:
"test.cnscnr.pdf",
log:
"logs/test.cnscnr.log",
params:
extra = "" # optional
wrapper:
"master/bio/cnvkit/diagram"
2 changes: 2 additions & 0 deletions bio/cnvkit/diagram/test/test.cnn
@@ -0,0 +1,2 @@
chromosome start end gene depth log2
chr1 160786623 160786747 LY9 0.27178 -1.87949
2 changes: 2 additions & 0 deletions bio/cnvkit/diagram/test/test.cnr
@@ -0,0 +1,2 @@
chromosome start end gene log2 depth weight
chr1 160786623 160786747 LY9 -1.87949 0.27178 0.310777
2 changes: 2 additions & 0 deletions bio/cnvkit/diagram/test/test.cns
@@ -0,0 +1,2 @@
chromosome start end gene log2 probes
chr1 160786623 160786747 LY9 -1.87949 1
32 changes: 32 additions & 0 deletions bio/cnvkit/diagram/wrapper.py
@@ -0,0 +1,32 @@
__author__ = "Patrik Smeds"
__copyright__ = "Copyright 2023, Patrik Smeds"
__email__ = "patrik.smeds@gmail.com"
__license__ = "MIT"

from snakemake.shell import shell

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

cns_file = [f for f in snakemake.input if f.endswith(".cns")]
cnr_file = [f for f in snakemake.input if f.endswith(".cnr") or f.endswith(".cnn")]

if cns_file:
if len(cns_file) > 1:
raise Exception(f"Expecting only one input cns file {cns_file}")
cns_file = f"-s {cns_file[0]}"

if cnr_file:
if len(cnr_file) > 1:
raise Exception(f"Expecting only one input cnr/cnn file {cnr_file}")
cnr_file = f"{cnr_file[0]}"

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

shell(
"(cnvkit.py diagram "
"{cns_file} "
"{cnr_file} "
"-o {snakemake.output} "
"{extra}) "
"{log}"
)
43 changes: 43 additions & 0 deletions test.py
Expand Up @@ -1180,6 +1180,49 @@ def test_bwa_mapping_meta():
],
)

@skip_if_not_modified
def test_cnvkit_diagram():
run(
"bio/cnvkit/diagram",
[
"snakemake",
"--cores",
"1",
"--use-conda",
"test.cns.pdf",
],
)
run(
"bio/cnvkit/diagram",
[
"snakemake",
"--cores",
"1",
"--use-conda",
"test.cnr.pdf",
],
)
run(
"bio/cnvkit/diagram",
[
"snakemake",
"--cores",
"1",
"--use-conda",
"test.cnn.pdf",
],
)
run(
"bio/cnvkit/diagram",
[
"snakemake",
"--cores",
"1",
"--use-conda",
"test.cnscnr.pdf",
],
)

@skip_if_not_modified
def test_cnvkit_antitarget():
run(
Expand Down

0 comments on commit d3cfc11

Please sign in to comment.