Skip to content

Commit

Permalink
Merge pull request #8 from rse-ops/add/snakemake
Browse files Browse the repository at this point in the history
add snakemake tutorial example workflow
  • Loading branch information
vsoch committed Nov 18, 2022
2 parents 5992c56 + e8dec65 commit 7996646
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 0 deletions.
30 changes: 30 additions & 0 deletions snakemake/atacseq/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ARG flux_restful_tag=latest
FROM ghcr.io/flux-framework/flux-restful-api:${flux_restful_tag}

# docker build -t snakemake .
# command: snakemake --cores 1 --flux
# workdir: /workflow

USER root
RUN apt-get update
RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install \
python3 \
python3-pip \
curl \
&& apt-get clean \
&& apt-get autoremove \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Wrappers to ensure we source the mamba environment!
RUN git clone --depth 1 https://github.com/snakemake/snakemake-tutorial-data /workflow
WORKDIR /workflow

# This wraps the existing entrypoint
COPY ./setup.sh ./Snakefile ./
RUN mkdir ./scripts && /bin/bash setup.sh
COPY ./scripts/plot-quals.py ./scripts/plot-quals.py

# This is probably cheating a bit :)
# instead of altering the entrypoint to active the environment!
ENV PATH=/root/micromamba/envs/snakemake/bin:$PATH
WORKDIR /code
78 changes: 78 additions & 0 deletions snakemake/atacseq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Flux Snakemake Example

This is an example container where you can build (optional) and run
the [Snakemake tutorial workflow](https://snakemake.readthedocs.io/en/stable/tutorial/tutorial.html):

```bash
$ docker build -t snakemake .
```

or with a tag for the restful API:

```bash
$ docker build --no-cache --build-arg app="latest" -t snakemake .
```

## Running the Workflow

### RESTFul API and Interface

To use the Flux RESTFul API and interface:

```
$ docker run -it -p 5000:5000 snakemake
```

Requiring a Flux user/token (fluxuser and 12345)

```bash
$ docker run -it --env require_auth=true -p 5000:5000 snakemake
```

And then enter the fluxuser and 123456 as the user and token, and try submitting a job to
the examples like:

```console
# Potential command and workdir
# command: snakemake --cores 1 --flux
# workdir: /workflow
```

![img/submit.png](img/submit.png)

And then browse to the table and click on the ID to see the log.

![img/log.png](img/log.png)

You can also try using the [RESTFul API](https://flux-framework.org/flux-restful-api/getting_started/user-guide.html#getting-started-user-guide--page-root). Have fun!

### Manual

Or just shell into the container:

```bash
$ docker run -it --entrypoint bash snakemake
```

Activate the environment:

```bash
$ micromamba shell init --shell=bash --prefix=~/micromamba
$ source /root/.bashrc
$ eval $(micromamba shell hook --shell=bash)
$ micromamba activate snakemake
```

And then start the flux instance:

```bash
$ flux start --test-size=4
```

And go to the snakemake workflow, and run it with Flux!

```bash
$ cd /workflow
$ snakemake --cores 1 --flux
```
That's it!
56 changes: 56 additions & 0 deletions snakemake/atacseq/Snakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
SAMPLES = ["A", "B"]


rule all:
input:
"plots/quals.svg"


rule bwa_map:
input:
"data/genome.fa",
"data/samples/{sample}.fastq"
output:
"mapped_reads/{sample}.bam"
shell:
"bwa mem {input} | samtools view -Sb - > {output}"


rule samtools_sort:
input:
"mapped_reads/{sample}.bam"
output:
"sorted_reads/{sample}.bam"
shell:
"samtools sort -T sorted_reads/{wildcards.sample} "
"-O bam {input} > {output}"


rule samtools_index:
input:
"sorted_reads/{sample}.bam"
output:
"sorted_reads/{sample}.bam.bai"
shell:
"samtools index {input}"


rule bcftools_call:
input:
fa="data/genome.fa",
bam=expand("sorted_reads/{sample}.bam", sample=SAMPLES),
bai=expand("sorted_reads/{sample}.bam.bai", sample=SAMPLES)
output:
"calls/all.vcf"
shell:
"bcftools mpileup -f {input.fa} {input.bam} | "
"bcftools call -mv - > {output}"


rule plot_quals:
input:
"calls/all.vcf"
output:
"plots/quals.svg"
script:
"scripts/plot-quals.py"
Binary file added snakemake/atacseq/img/log.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snakemake/atacseq/img/submit.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions snakemake/atacseq/scripts/plot-quals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from pysam import VariantFile

quals = [record.qual for record in VariantFile(snakemake.input[0])]
plt.hist(quals)

plt.savefig(snakemake.output[0])
10 changes: 10 additions & 0 deletions snakemake/atacseq/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -eEu -o pipefail

# Setup the mamba environment with snakedeploy
curl -Ls https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj bin/micromamba
mv bin/micromamba /usr/local/bin && rmdir bin
which micromamba
micromamba create --prefix snakemake -f environment.yaml
micromamba shell init --shell=bash --prefix=~/micromamba
7 changes: 7 additions & 0 deletions snakemake/atacseq/uptodate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dockerbuild:

build_args:
flux_restful_tag:
key: app
versions:
- latest

0 comments on commit 7996646

Please sign in to comment.