Skip to content

Commit

Permalink
feat: enable output of unmapped reads (#1549)
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

Ability to save unmapped reads in fastq or fastq.gz formats using the
`--outReadsUnmapped Fastx` option in STAR

### 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
kashyapchhatbar and fgvieira authored Jul 27, 2023
1 parent ad5d062 commit bff21bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions bio/star/align/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors:
- Johannes Köster
- Tomás Di Domenico
- Filipe G. Vieira
- Kashyap Chhatbar
notes: |
* The `extra` param allows for additional program arguments.
* It is advisable to consider updating the limits setting before running STAR,
Expand Down
2 changes: 2 additions & 0 deletions bio/star/align/test/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rule star_pe_multi:
aln="star/pe/{sample}/pe_aligned.sam",
log="logs/pe/{sample}/Log.out",
sj="star/pe/{sample}/SJ.out.tab",
unmapped=["star/pe/{sample}/unmapped.1.fastq.gz","star/pe/{sample}/unmapped.2.fastq.gz"],
log:
"logs/pe/{sample}.log",
params:
Expand All @@ -32,6 +33,7 @@ rule star_se:
aln="star/se/{sample}/se_aligned.bam",
log="logs/se/{sample}/Log.out",
log_final="logs/se/{sample}/Log.final.out",
unmapped="star/se/{sample}/unmapped.fastq",
log:
"logs/se/{sample}.log",
params:
Expand Down
13 changes: 13 additions & 0 deletions bio/star/align/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
else:
readcmd = ""

out_unmapped = snakemake.output.get("unmapped", "")
if out_unmapped:
out_unmapped = "--outReadsUnmapped Fastx"

index = snakemake.input.get("idx")
if not index:
Expand All @@ -64,6 +67,7 @@
" --readFilesIn {input_str}"
" {readcmd}"
" {extra}"
" {out_unmapped}"
" --outTmpDir {tmpdir}/STARtmp"
" --outFileNamePrefix {tmpdir}/"
" --outStd {stdout}"
Expand All @@ -83,3 +87,12 @@
shell("cat {tmpdir}/Log.progress.out > {snakemake.output.log_progress:q}")
if snakemake.output.get("log_final"):
shell("cat {tmpdir}/Log.final.out > {snakemake.output.log_final:q}")
unmapped = snakemake.output.get("unmapped")
if unmapped:
# SE
if not fq2:
unmapped = [unmapped]

for i, out_unmapped in enumerate(unmapped, 1):
cmd = "gzip -c" if out_unmapped.endswith("gz") else "cat"
shell("{cmd} {tmpdir}/Unmapped.out.mate{i} > {out_unmapped}")

0 comments on commit bff21bc

Please sign in to comment.