GATK Best-Practices Pipeline for Small Genomic Variant Calling
Help improve this workflow!
This workflow has been published but could be further improved with some additional meta data:- Keyword(s) in categories input, output
You can help improve this workflow by suggesting the addition or removal of keywords, suggest changes and report issues, or request to become a maintainer of the Workflow .
This Snakemake pipeline implements the GATK best-practices workflow for calling small genomic variants.
It's based on this workflow by Johannes Köster.
Authors
-
Elena Piñeiro
-
Tomás Di Domenico
Usage
Simple
Step 1: Install workflow
If you simply want to use this workflow, clone the repository or download its source code. If you intend to modify and further extend this workflow or want to work under version control, fork this repository as outlined in Advanced . The latter way is recommended.
In any case, if you use this workflow in a paper, don't forget to give credits to the authors by citing the URL of this repository and, if available, its DOI (see above).
Step 2: Configure workflow
Configure the workflow according to your needs via editing the files
config.yaml
,
contigs.tsv
,
samples.tsv
and
units.tsv
.
config.yaml
This file contains the paths to files required in the analysis, the enabling/disabling of optional steps, as well as additional parameters for some of the programs used in the process.
The file is structured in several sections to be customized according to the characteristics of the analysis. The indications for its filling are provided inside the file.
contigs.tsv
It contains the contigs of the reference genome to include in the analysis (one contig per line). If empty, the analysis is performed using all contigs in the fasta index.
samples.tsv
It lists all samples to be included in the run, the group to which the samples belong, the use of MuTect2 and its execution mode.
HaplotypeCaller will be executed for each sample in the
sample
column. The results of the samples from each
group
will be merged, and consequently, at the end, there will be one HaplotypeCaller result for each
group
defined in this file.
Mutect will always be run independently for each tumor sample or tumor/control pair, and its results will remain separated. To activate the execution of MuTect2 and set its execution mode, the
control
column is used. If control contains:
-
-
MuTect2 is not executed for that sample. -
The same sample name as in the
sample
column: MuTect2 is executed for that sample in tumor-only mode. -
A different sample name than in the
sample
column: MuTect2 is executed in tumor-normal mode; being the tumor sample the one indicated in thesample
column, and the normal sample the one indicated in thecontrol
column.
Examples:
- No MuTect2 execution:
group | sample | control |
---|---|---|
1 | A | - |
- MuTect2 execution in tumor-only mode:
group | sample | control |
---|---|---|
1 | A | A |
- MuTect2 execution in tumor-normal mode:
group | sample | control |
---|---|---|
1 | A | B |
1 | B | - |
being A:tumor, B:normal
units.tsv
It contains the specifications of the samples (sequencing units, sequencing platform and fastq files) listed in
samples.tsv
, as described in the original workflow (
Step3: configure workflow
).
Step 3: Execute workflow
Test your configuration by performing a dry-run via
snakemake --use-conda -n
Execute the workflow locally via
snakemake --use-conda --cores $N
using
$N
cores or run it in a cluster environment via
snakemake --use-conda --cluster qsub --jobs 100
or
snakemake --use-conda --drmaa --jobs 100
If you not only want to fix the software stack but also the underlying OS, use
snakemake --use-conda --use-singularity
in combination with any of the modes above. See the Snakemake documentation for further details.
After successful execution, you can create a self-contained interactive HTML report with all results via:
snakemake --report report.html
This report can, e.g., be forwarded to your collaborators. An example, using some trivial test data, can be seen here .
Advanced
The following recipe provides established best practices for running and extending this workflow in a reproducible way.
-
Fork the repo to a personal or lab account.
-
Clone the fork to the desired working directory for the concrete project/run on your machine.
-
Create a new branch (the project-branch) within the clone and switch to it. The branch will contain any project-specific modifications (e.g. to configuration, but also to code).
-
Modify the config, and any necessary sheets (and probably the workflow) as needed.
-
Commit any changes and push the project-branch to your fork on github.
-
Run the analysis.
-
Optional: Merge back any valuable and generalizable changes to the upstream repo via a pull request . This would be greatly appreciated .
-
Optional: Push results (plots/tables) to the remote branch on your fork.
-
Optional: Create a self-contained workflow archive for publication along with the paper (snakemake --archive).
-
Optional: Delete the local clone/workdir to free space.
Testing
Tests cases are in the subfolder
.test
. They are automtically executed via continuous integration.
Code Snippets
11 12 | wrapper: "v2.0.0/bio/snpeff/download" |
31 32 | wrapper: "v2.0.0/bio/snpeff/annotate" |
48 49 50 | shell: """ vep -i {input} -o {output} --vcf --compress_output gzip --force_overwrite {params} --fork {threads} """ |
66 67 68 | shell: """ vep -i {input} -o {output} --format 'vcf' --vcf --compress_output gzip --force_overwrite {params} --fork {threads} """ |
9 10 | shell: "sort-bed {input} > {output}" |
21 22 | shell: "bedextract {params.contig} {input} > {output}" |
40 41 | wrapper: "v2.0.0/bio/gatk/haplotypecaller" |
56 57 | wrapper: "v2.0.0/bio/gatk/combinegvcfs" |
74 75 | wrapper: "v2.0.0/bio/gatk/genotypegvcfs" |
91 92 | wrapper: "v2.0.0/bio/picard/mergevcfs" |
102 103 | wrapper: "v2.0.0/bio/samtools/merge" |
116 117 | wrapper: "v2.0.0/bio/samtools/index" |
139 140 141 | shell:""" gatk Mutect2 --callable-depth 1 --native-pair-hmm-threads 16 -R {input.ref} {params.regions} -I {input.bam} {params.normal} -O {output.out} --f1r2-tar-gz {output.f1r2} """ |
20 21 | wrapper: "v2.0.0/bio/gatk/selectvariants" |
44 45 | wrapper: "v2.0.0/bio/gatk/variantfiltration" |
73 74 | wrapper: "v2.0.0/bio/gatk/variantrecalibrator" |
93 94 | wrapper: "v2.0.0/bio/picard/mergevcfs" |
107 108 | wrapper: "v2.0.0/bio/gatk/learnreadorientationmodel" |
125 126 | wrapper: "v2.0.0/bio/gatk/filtermutectcalls" |
142 143 | wrapper: "v2.0.0/bio/gatk/variantfiltration" |
15 16 | wrapper: "v2.0.0/bio/trimmomatic/se" |
36 37 | wrapper: "v2.0.0/bio/trimmomatic/pe" |
60 61 | wrapper: "v2.0.0/bio/bwa-mem2/index" |
81 82 | wrapper: "v2.0.0/bio/bwa-mem2/mem" |
98 99 | wrapper: "v2.0.0/bio/picard/markduplicates" |
112 113 | wrapper: "v2.0.0/bio/samtools/faidx" |
134 135 | wrapper: "v2.0.0/bio/gatk/baserecalibrator" |
154 155 | wrapper: "v2.0.0/bio/gatk/applybqsr" |
168 169 | wrapper: "v2.0.0/bio/samtools/index" |
182 183 | wrapper: "v2.0.0/bio/samtools/index" |
197 198 199 | shell:""" gatk IndexFeatureFile -I {input.file} -O {output.index} """ |
11 12 | wrapper: "v2.0.0/bio/fastqc" |
25 26 | wrapper: "v2.0.0/bio/samtools/stats" |
40 41 | wrapper: "v2.0.0/bio/picard/createsequencedictionary" |
57 58 | wrapper: "v2.0.0/bio/picard/bedtointervallist" |
76 77 | wrapper: "v2.0.0/bio/picard/collecthsmetrics" |
95 96 | wrapper: "v2.0.0/bio/multiqc" |
12 13 14 15 | shell: "bcftools view --apply-filters PASS --output-type u {input} | " "rbt vcf-to-txt -g --fmt DP AD --info ANN | " "gzip > {output}" |
30 31 | script: "../scripts/plot-depths.py" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import common import matplotlib.pyplot as plt import pandas as pd import numpy as np import seaborn as sns calls = pd.read_table(snakemake.input[0], header=[0, 1]) samples = [name for name in calls.columns.levels[0] if name != "VARIANT"] sample_info = calls.loc[:, samples].stack([0, 1]).unstack().reset_index(1, drop=False) sample_info = sample_info.rename({"level_1": "sample"}, axis=1) if not (sample_info.empty): sample_info = sample_info[sample_info["DP"] > 0] sample_info["freq"] = sample_info["AD"] / sample_info["DP"] sample_info.index = np.arange(sample_info.shape[0]) plt.figure() sns.stripplot(x="sample", y="freq", data=sample_info, jitter=True) plt.ylabel("allele frequency") plt.xticks(rotation="vertical") plt.savefig(snakemake.output.freqs) plt.figure() sns.stripplot(x="sample", y="DP", data=sample_info, jitter=True) plt.ylabel("read depth") plt.xticks(rotation="vertical") plt.savefig(snakemake.output.depths) else: plt.figure() plt.savefig(snakemake.output.freqs) plt.figure() plt.savefig(snakemake.output.depths) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | __author__ = "Christopher Schröder, Patrik Smeds" __copyright__ = "Copyright 2020, Christopher Schröder, Patrik Smeds" __email__ = "christopher.schroeder@tu-dortmund.de, patrik.smeds@gmail.com" __license__ = "MIT" from os import path from snakemake.shell import shell log = snakemake.log_fmt_shell(stdout=True, stderr=True) # Check inputs/arguments. if len(snakemake.input) == 0: raise ValueError("A reference genome has to be provided.") elif len(snakemake.input) > 1: raise ValueError("Please provide exactly one reference genome as input.") valid_suffixes = {".0123", ".amb", ".ann", ".bwt.2bit.64", ".pac"} def get_valid_suffix(path): for suffix in valid_suffixes: if path.endswith(suffix): return suffix prefixes = set() for s in snakemake.output: suffix = get_valid_suffix(s) if suffix is None: raise ValueError(f"{s} cannot be generated by bwa-mem2 index (invalid suffix).") prefixes.add(s[: -len(suffix)]) if len(prefixes) != 1: raise ValueError("Output files must share common prefix up to their file endings.") (prefix,) = prefixes shell("bwa-mem2 index -p {prefix} {snakemake.input[0]} {log}") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | __author__ = "Christopher Schröder, Johannes Köster, Julian de Ruiter" __copyright__ = ( "Copyright 2020, Christopher Schröder, Johannes Köster and Julian de Ruiter" ) __email__ = "christopher.schroeder@tu-dortmund.de koester@jimmy.harvard.edu, julianderuiter@gmail.com" __license__ = "MIT" import tempfile from os import path from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts from snakemake_wrapper_utils.samtools import get_samtools_opts # Extract arguments. extra = snakemake.params.get("extra", "") log = snakemake.log_fmt_shell(stdout=False, stderr=True) sort = snakemake.params.get("sort", "none") sort_order = snakemake.params.get("sort_order", "coordinate") sort_extra = snakemake.params.get("sort_extra", "") samtools_opts = get_samtools_opts(snakemake) java_opts = get_java_opts(snakemake) index = snakemake.input.get("index", "") if isinstance(index, str): index = path.splitext(snakemake.input.idx)[0] else: index = path.splitext(snakemake.input.idx[0])[0] # Check inputs/arguments. if not isinstance(snakemake.input.reads, str) and len(snakemake.input.reads) not in { 1, 2, }: raise ValueError("input must have 1 (single-end) or 2 (paired-end) elements") if sort_order not in {"coordinate", "queryname"}: raise ValueError(f"Unexpected value for sort_order ({sort_order})") # Determine which pipe command to use for converting to bam or sorting. if sort == "none": # Simply convert to bam using samtools view. pipe_cmd = "samtools view {samtools_opts}" elif sort == "samtools": # Sort alignments using samtools sort. pipe_cmd = "samtools sort {samtools_opts} {sort_extra} -T {tmpdir}" # Add name flag if needed. if sort_order == "queryname": sort_extra += " -n" elif sort == "picard": # Sort alignments using picard SortSam. pipe_cmd = "picard SortSam {java_opts} {sort_extra} --INPUT /dev/stdin --TMP_DIR {tmpdir} --SORT_ORDER {sort_order} --OUTPUT {snakemake.output[0]}" else: raise ValueError(f"Unexpected value for params.sort ({sort})") with tempfile.TemporaryDirectory() as tmpdir: shell( "(bwa-mem2 mem" " -t {snakemake.threads}" " {extra}" " {index}" " {snakemake.input.reads}" " | " + pipe_cmd + ") {log}" ) |
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | __author__ = "Julian de Ruiter" __copyright__ = "Copyright 2017, Julian de Ruiter" __email__ = "julianderuiter@gmail.com" __license__ = "MIT" from os import path import re from tempfile import TemporaryDirectory from snakemake.shell import shell from snakemake_wrapper_utils.snakemake import get_mem extra = snakemake.params.get("extra", "") log = snakemake.log_fmt_shell(stdout=True, stderr=True) # Define memory per thread (https://github.com/s-andrews/FastQC/blob/master/fastqc#L201-L222) mem_mb = int(get_mem(snakemake, "MiB") / snakemake.threads) def basename_without_ext(file_path): """Returns basename of file path, without the file extension.""" base = path.basename(file_path) # Remove file extension(s) (similar to the internal fastqc approach) base = re.sub("\\.gz$", "", base) base = re.sub("\\.bz2$", "", base) base = re.sub("\\.txt$", "", base) base = re.sub("\\.fastq$", "", base) base = re.sub("\\.fq$", "", base) base = re.sub("\\.sam$", "", base) base = re.sub("\\.bam$", "", base) return base # If you have multiple input files fastqc doesn't know what to do. Taking silently only first gives unapreciated results if len(snakemake.input) > 1: raise IOError("Got multiple input files, I don't know how to process them!") # Run fastqc, since there can be race conditions if multiple jobs # use the same fastqc dir, we create a temp dir. with TemporaryDirectory() as tempdir: shell( "fastqc" " --threads {snakemake.threads}" " --memory {mem_mb}" " {extra}" " --outdir {tempdir:q}" " {snakemake.input[0]:q}" " {log}" ) # Move outputs into proper position. output_base = basename_without_ext(snakemake.input[0]) html_path = path.join(tempdir, output_base + "_fastqc.html") zip_path = path.join(tempdir, output_base + "_fastqc.zip") if snakemake.output.html != html_path: shell("mv {html_path:q} {snakemake.output.html:q}") if snakemake.output.zip != zip_path: shell("mv {zip_path:q} {snakemake.output.zip:q}") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | __author__ = "Christopher Schröder" __copyright__ = "Copyright 2020, Christopher Schröder" __email__ = "christopher.schroeder@tu-dortmund.de" __license__ = "MIT" import tempfile from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts # Extract arguments extra = snakemake.params.get("extra", "") reference = snakemake.input.get("ref") embed_ref = snakemake.params.get("embed_ref", False) java_opts = get_java_opts(snakemake) log = snakemake.log_fmt_shell(stdout=True, stderr=True, append=True) if snakemake.output.bam.endswith(".cram") and embed_ref: output = "/dev/stdout" pipe_cmd = " | samtools view -h -O cram,embed_ref -T {reference} -o {snakemake.output.bam} -" else: output = snakemake.output.bam pipe_cmd = "" with tempfile.TemporaryDirectory() as tmpdir: shell( "(gatk --java-options '{java_opts}' ApplyBQSR" " --input {snakemake.input.bam}" " --bqsr-recal-file {snakemake.input.recal_table}" " --reference {reference}" " {extra}" " --tmp-dir {tmpdir}" " --output {output}" + pipe_cmd + ") {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | __author__ = "Christopher Schröder" __copyright__ = "Copyright 2020, Christopher Schröder" __email__ = "christopher.schroeder@tu-dortmund.de" __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) log = snakemake.log_fmt_shell(stdout=True, stderr=True) known = snakemake.input.get("known", "") if known: if isinstance(known, str): known = [known] known = list(map("--known-sites {}".format, known)) with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' BaseRecalibrator" " --input {snakemake.input.bam}" " --reference {snakemake.input.ref}" " {known}" " {extra}" " --tmp-dir {tmpdir}" " --output {snakemake.output.recal_table}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.com" __license__ = "MIT" import os 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) gvcfs = list(map("--variant {}".format, snakemake.input.gvcfs)) log = snakemake.log_fmt_shell(stdout=True, stderr=True) with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' CombineGVCFs" " {gvcfs}" " --reference {snakemake.input.ref}" " {extra}" " --tmp-dir {tmpdir}" " --output {snakemake.output.gvcf}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | __author__ = "Patrik Smeds" __copyright__ = "Copyright 2021, Patrik Smeds" __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) log = snakemake.log_fmt_shell(stdout=True, stderr=True) aln = snakemake.input.get("aln", "") if aln: aln = f"--input {aln}" contamination = snakemake.input.get("contemination_table", "") if contamination: contamination = f"--contamination-table {contamination}" segmentation = snakemake.input.get("segmentation", "") if segmentation: segmentation = f"--tumor-segmentation {segmentation}" f1r2 = snakemake.input.get("f1r2", "") if f1r2: f1r2 = f"--orientation-bias-artifact-priors {f1r2}" intervals = snakemake.input.get("bed", "") if intervals: intervals = f"--intervals {intervals}" with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' FilterMutectCalls" " --variant {snakemake.input.vcf}" " --reference {snakemake.input.ref}" " {aln}" # BAM/SAM/CRAM file containing reads " {contamination}" # Tables containing contamination information " {segmentation}" # Tumor segments' minor allele fractions " {f1r2}" # .tar.gz files containing tables of prior artifact " {intervals}" # Genomic intervals over which to operate " {extra}" " --tmp-dir {tmpdir}" " --output {snakemake.output.vcf}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.com" __license__ = "MIT" import os 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) intervals = snakemake.input.get("intervals", "") if not intervals: intervals = snakemake.params.get("intervals", "") if intervals: intervals = "--intervals {}".format(intervals) dbsnp = snakemake.input.get("known", "") if dbsnp: dbsnp = "--dbsnp {}".format(dbsnp) # Allow for either an input gvcf or GenomicsDB gvcf = snakemake.input.get("gvcf", "") genomicsdb = snakemake.input.get("genomicsdb", "") if gvcf: if genomicsdb: raise Exception("Only input.gvcf or input.genomicsdb expected, got both.") input_string = gvcf else: if genomicsdb: input_string = "gendb://{}".format(genomicsdb) else: raise Exception("Expected input.gvcf or input.genomicsdb.") log = snakemake.log_fmt_shell(stdout=True, stderr=True) with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' GenotypeGVCFs" " --variant {input_string}" " --reference {snakemake.input.ref}" " {dbsnp}" " {intervals}" " {extra}" " --tmp-dir {tmpdir}" " --output {snakemake.output.vcf}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.com" __license__ = "MIT" import os 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) bams = snakemake.input.bam if isinstance(bams, str): bams = [bams] bams = list(map("--input {}".format, bams)) intervals = snakemake.input.get("intervals", "") if not intervals: intervals = snakemake.params.get("intervals", "") if intervals: intervals = "--intervals {}".format(intervals) known = snakemake.input.get("known", "") if known: known = "--dbsnp " + str(known) vcf_output = snakemake.output.get("vcf", "") if vcf_output: output = " --output " + str(vcf_output) gvcf_output = snakemake.output.get("gvcf", "") if gvcf_output: output = " --emit-ref-confidence GVCF " + " --output " + str(gvcf_output) if (vcf_output and gvcf_output) or (not gvcf_output and not vcf_output): if vcf_output and gvcf_output: raise ValueError( "please set vcf or gvcf as output, not both! It's not supported by gatk" ) else: raise ValueError("please set one of vcf or gvcf as output (not both)!") bam_output = snakemake.output.get("bam", "") if bam_output: bam_output = " --bam-output " + str(bam_output) log = snakemake.log_fmt_shell(stdout=True, stderr=True) with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' HaplotypeCaller" " --native-pair-hmm-threads {snakemake.threads}" " {bams}" " --reference {snakemake.input.ref}" " {intervals}" " {known}" " {extra}" " --tmp-dir {tmpdir}" " {output}" " {bam_output}" " {log}" ) |
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | __author__ = "Thibault Dayris" __copyright__ = "Copyright 2022, Dayris Thibault" __email__ = "thibault.dayris@gustaveroussy.fr" __license__ = "MIT" import tempfile from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts log = snakemake.log_fmt_shell(stdout=True, stderr=True) extra = snakemake.params.get("extra", "") java_opts = get_java_opts(snakemake) f1r2 = "--input " if isinstance(snakemake.input["f1r2"], list): # Case user provided a list of archives f1r2 += "--input ".join(snakemake.input["f1r2"]) else: # Case user provided a single archive as a string f1r2 += snakemake.input["f1r2"] with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' LearnReadOrientationModel" # Tool and its subprocess " {f1r2}" # Path to input mapping file " {extra}" # Extra parameters " --tmp-dir {tmpdir}" " --output {snakemake.output[0]}" # Path to output vcf file " {log}" # Logging behaviour ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.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) log = snakemake.log_fmt_shell(stdout=True, stderr=True) with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' SelectVariants" " --variant {snakemake.input.vcf}" " --reference {snakemake.input.ref}" " {extra}" " --tmp-dir {tmpdir}" " --output {snakemake.output.vcf}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.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) log = snakemake.log_fmt_shell(stdout=True, stderr=True) filters = [ "--filter-name {} --filter-expression '{}'".format(name, expr.replace("'", "\\'")) for name, expr in snakemake.params.filters.items() ] intervals = snakemake.input.get("intervals", "") if not intervals: intervals = snakemake.params.get("intervals", "") if intervals: intervals = "--intervals {}".format(intervals) with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' VariantFiltration" " --variant {snakemake.input.vcf}" " --reference {snakemake.input.ref}" " {filters}" " {intervals}" " {extra}" " --tmp-dir {tmpdir}" " --output {snakemake.output.vcf}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.com" __license__ = "MIT" import os 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) def fmt_res(resname, resparams): fmt_bool = lambda b: str(b).lower() try: f = snakemake.input.get(resname) except KeyError: raise RuntimeError( f"There must be a named input file for every resource (missing: {resname})" ) return "{},known={},training={},truth={},prior={} {}".format( resname, fmt_bool(resparams["known"]), fmt_bool(resparams["training"]), fmt_bool(resparams["truth"]), resparams["prior"], f, ) annotation_resources = [ "--resource:{}".format(fmt_res(resname, resparams)) for resname, resparams in snakemake.params["resources"].items() ] annotation = list(map("-an {}".format, snakemake.params.annotation)) tranches = snakemake.output.get("tranches", "") if tranches: tranches = f"--tranches-file {tranches}" log = snakemake.log_fmt_shell(stdout=True, stderr=True) with tempfile.TemporaryDirectory() as tmpdir: shell( "gatk --java-options '{java_opts}' VariantRecalibrator" " --variant {snakemake.input.vcf}" " --reference {snakemake.input.ref}" " --mode {snakemake.params.mode}" " {annotation_resources}" " {tranches}" " {annotation}" " {extra}" " --tmp-dir {tmpdir}" " --output {snakemake.output.vcf}" " {log}" ) |
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | __author__ = "Julian de Ruiter" __copyright__ = "Copyright 2017, Julian de Ruiter" __email__ = "julianderuiter@gmail.com" __license__ = "MIT" from os import path from snakemake.shell import shell extra = snakemake.params.get("extra", "") # Set this to False if multiqc should use the actual input directly # instead of parsing the folders where the provided files are located use_input_files_only = snakemake.params.get("use_input_files_only", False) if not use_input_files_only: input_data = set(path.dirname(fp) for fp in snakemake.input) else: input_data = set(snakemake.input) output_dir = path.dirname(snakemake.output[0]) output_name = path.basename(snakemake.output[0]) log = snakemake.log_fmt_shell(stdout=True, stderr=True) shell( "multiqc" " {extra}" " --force" " -o {output_dir}" " -n {output_name}" " {input_data}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | __author__ = "Fabian Kilpert" __copyright__ = "Copyright 2020, Fabian Kilpert" __email__ = "fkilpert@gmail.com" __license__ = "MIT" import tempfile from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts log = snakemake.log_fmt_shell() extra = snakemake.params.get("extra", "") java_opts = get_java_opts(snakemake) with tempfile.TemporaryDirectory() as tmpdir: shell( "picard BedToIntervalList" " {java_opts} {extra}" " --INPUT {snakemake.input.bed}" " --SEQUENCE_DICTIONARY {snakemake.input.dict}" " --TMP_DIR {tmpdir}" " --OUTPUT {snakemake.output}" " {log}" ) |
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | __author__ = "Julian de Ruiter" __copyright__ = "Copyright 2017, Julian de Ruiter" __email__ = "julianderuiter@gmail.com" __license__ = "MIT" import tempfile from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts log = snakemake.log_fmt_shell(stdout=False, stderr=True) extra = snakemake.params.get("extra", "") java_opts = get_java_opts(snakemake) with tempfile.TemporaryDirectory() as tmpdir: shell( "picard CollectHsMetrics" " {java_opts} {extra}" " --INPUT {snakemake.input.bam}" " --TMP_DIR {tmpdir}" " --OUTPUT {snakemake.output[0]}" " --REFERENCE_SEQUENCE {snakemake.input.reference}" " --BAIT_INTERVALS {snakemake.input.bait_intervals}" " --TARGET_INTERVALS {snakemake.input.target_intervals}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.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) log = snakemake.log_fmt_shell(stdout=False, stderr=True) with tempfile.TemporaryDirectory() as tmpdir: shell( "picard CreateSequenceDictionary" " {java_opts} {extra}" " --REFERENCE {snakemake.input[0]}" " --TMP_DIR {tmpdir}" " --OUTPUT {snakemake.output[0]}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | __author__ = "Johannes Köster, Christopher Schröder" __copyright__ = "Copyright 2016, Johannes Köster" __email__ = "koester@jimmy.harvard.edu" __license__ = "MIT" import tempfile from pathlib import Path from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts from snakemake_wrapper_utils.samtools import get_samtools_opts, infer_out_format log = snakemake.log_fmt_shell() extra = snakemake.params.get("extra", "") # the --SORTING_COLLECTION_SIZE_RATIO default of 0.25 might # indicate a JVM memory overhead of that proportion java_opts = get_java_opts(snakemake, java_mem_overhead_factor=0.3) samtools_opts = get_samtools_opts(snakemake) tool = "MarkDuplicates" if snakemake.params.get("withmatecigar", False): tool = "MarkDuplicatesWithMateCigar" bams = snakemake.input.bams if isinstance(bams, str): bams = [bams] bams = list(map("--INPUT {}".format, bams)) output = snakemake.output.bam output_fmt = infer_out_format(output) convert = "" if output_fmt == "CRAM": output = "/dev/stdout" # NOTE: output format inference should be done by snakemake-wrapper-utils. Keeping it here for backwards compatibility. if snakemake.params.get("embed_ref", False): samtools_opts += ",embed_ref" convert = f" | samtools view {samtools_opts}" elif output_fmt == "BAM" and snakemake.output.get("idx"): extra += " --CREATE_INDEX" with tempfile.TemporaryDirectory() as tmpdir: shell( "(picard {tool}" # Tool and its subcommand " {java_opts}" # Automatic java option " {extra}" # User defined parmeters " {bams}" # Input bam(s) " --TMP_DIR {tmpdir}" " --OUTPUT {output}" # Output bam " --METRICS_FILE {snakemake.output.metrics}" # Output metrics " {convert}) {log}" # Logging ) output_prefix = Path(snakemake.output.bam).with_suffix("") if snakemake.output.get("idx"): if output_fmt == "BAM" and snakemake.output.idx != str(output_prefix) + ".bai": shell("mv {output_prefix}.bai {snakemake.output.idx}") |
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2018, Johannes Köster" __email__ = "johannes.koester@protonmail.com" __license__ = "MIT" import tempfile from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts inputs = " ".join("--INPUT {}".format(f) for f in snakemake.input.vcfs) log = snakemake.log_fmt_shell(stdout=False, stderr=True) extra = snakemake.params.get("extra", "") java_opts = get_java_opts(snakemake) with tempfile.TemporaryDirectory() as tmpdir: shell( "picard MergeVcfs" " {java_opts} {extra}" " {inputs}" " --TMP_DIR {tmpdir}" " --OUTPUT {snakemake.output[0]}" " {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | __author__ = "Michael Chambers" __copyright__ = "Copyright 2019, Michael Chambers" __email__ = "greenkidneybean@gmail.com" __license__ = "MIT" from snakemake.shell import shell from snakemake_wrapper_utils.samtools import get_samtools_opts samtools_opts = get_samtools_opts( snakemake, parse_threads=False, parse_write_index=False, parse_output_format=False ) extra = snakemake.params.get("extra", "") log = snakemake.log_fmt_shell(stdout=True, stderr=True) shell("samtools faidx {samtools_opts} {extra} {snakemake.input[0]} {log}") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2016, Johannes Köster" __email__ = "koester@jimmy.harvard.edu" __license__ = "MIT" from snakemake.shell import shell extra = snakemake.params.get("extra", "") log = snakemake.log_fmt_shell(stdout=True, stderr=True) # Samtools takes additional threads through its option -@ # One thread for samtools merge # Other threads are *additional* threads passed to the '-@' argument threads = "" if snakemake.threads <= 1 else " -@ {} ".format(snakemake.threads - 1) shell( "samtools index {threads} {extra} {snakemake.input[0]} {snakemake.output[0]} {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2016, Johannes Köster" __email__ = "koester@jimmy.harvard.edu" __license__ = "MIT" from snakemake.shell import shell from snakemake_wrapper_utils.samtools import get_samtools_opts samtools_opts = get_samtools_opts(snakemake) extra = snakemake.params.get("extra", "") log = snakemake.log_fmt_shell(stdout=True, stderr=True) shell("samtools merge {samtools_opts} {extra} {snakemake.input} {log}") |
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | __author__ = "Julian de Ruiter" __copyright__ = "Copyright 2017, Julian de Ruiter" __email__ = "julianderuiter@gmail.com" __license__ = "MIT" from snakemake.shell import shell from snakemake_wrapper_utils.samtools import get_samtools_opts bed = snakemake.input.get("bed", "") if bed: bed = f"-t {bed}" samtools_opts = get_samtools_opts( snakemake, parse_write_index=False, parse_output=False, parse_output_format=False ) extra = snakemake.params.get("extra", "") region = snakemake.params.get("region", "") log = snakemake.log_fmt_shell(stdout=False, stderr=True) shell( "samtools stats {samtools_opts} {extra} {snakemake.input[0]} {bed} {region} > {snakemake.output[0]} {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | __author__ = "Bradford Powell" __copyright__ = "Copyright 2018, Bradford Powell" __email__ = "bpow@unc.edu" __license__ = "BSD" from snakemake.shell import shell from os import path import shutil import tempfile from pathlib import Path from snakemake_wrapper_utils.java import get_java_opts extra = snakemake.params.get("extra", "") java_opts = get_java_opts(snakemake) outcalls = snakemake.output.calls if outcalls.endswith(".vcf.gz"): outprefix = "| bcftools view -Oz" elif outcalls.endswith(".bcf"): outprefix = "| bcftools view -Ob" else: outprefix = "" incalls = snakemake.input[0] if incalls.endswith(".bcf"): incalls = "< <(bcftools view {})".format(incalls) log = snakemake.log_fmt_shell(stdout=False, stderr=True) data_dir = Path(snakemake.input.db).parent.resolve() stats = snakemake.output.get("stats", "") csvstats = snakemake.output.get("csvstats", "") csvstats_opt = "" if not csvstats else "-csvStats {}".format(csvstats) stats_opt = "-noStats" if not stats else "-stats {}".format(stats) reference = path.basename(snakemake.input.db) shell( "snpEff {java_opts} -dataDir {data_dir} " "{stats_opt} {csvstats_opt} {extra} " "{reference} {incalls} " "{outprefix} > {outcalls} {log}" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | __author__ = "Johannes Köster" __copyright__ = "Copyright 2020, Johannes Köster" __email__ = "johannes.koester@uni-due.de" __license__ = "MIT" from snakemake.shell import shell from pathlib import Path from snakemake_wrapper_utils.java import get_java_opts java_opts = get_java_opts(snakemake) reference = snakemake.params.reference outdir = Path(snakemake.output[0]).parent.resolve() log = snakemake.log_fmt_shell(stdout=False, stderr=True) shell("snpEff download {java_opts} -dataDir {outdir} {reference} {log}") |
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | __author__ = "Johannes Köster, Jorge Langa" __copyright__ = "Copyright 2016, Johannes Köster" __email__ = "koester@jimmy.harvard.edu" __license__ = "MIT" from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts # Distribute available threads between trimmomatic itself and any potential pigz instances def distribute_threads(input_files, output_files, available_threads): gzipped_input_files = sum(1 for file in input_files if file.endswith(".gz")) gzipped_output_files = sum(1 for file in output_files if file.endswith(".gz")) potential_threads_per_process = available_threads // ( 1 + gzipped_input_files + gzipped_output_files ) if potential_threads_per_process > 0: # decompressing pigz creates at most 4 threads pigz_input_threads = ( min(4, potential_threads_per_process) if gzipped_input_files != 0 else 0 ) pigz_output_threads = ( (available_threads - pigz_input_threads * gzipped_input_files) // (1 + gzipped_output_files) if gzipped_output_files != 0 else 0 ) trimmomatic_threads = ( available_threads - pigz_input_threads * gzipped_input_files - pigz_output_threads * gzipped_output_files ) else: # not enough threads for pigz pigz_input_threads = 0 pigz_output_threads = 0 trimmomatic_threads = available_threads return trimmomatic_threads, pigz_input_threads, pigz_output_threads def compose_input_gz(filename, threads): if filename.endswith(".gz") and threads > 0: return "<(pigz -p {threads} --decompress --stdout {filename})".format( threads=threads, filename=filename ) return filename def compose_output_gz(filename, threads, compression_level): if filename.endswith(".gz") and threads > 0: return ">(pigz -p {threads} {compression_level} > {filename})".format( threads=threads, compression_level=compression_level, filename=filename ) return filename extra = snakemake.params.get("extra", "") java_opts = get_java_opts(snakemake) log = snakemake.log_fmt_shell(stdout=True, stderr=True) compression_level = snakemake.params.get("compression_level", "-5") trimmer = " ".join(snakemake.params.trimmer) # Distribute threads input_files = [snakemake.input.r1, snakemake.input.r2] output_files = [ snakemake.output.r1, snakemake.output.r1_unpaired, snakemake.output.r2, snakemake.output.r2_unpaired, ] trimmomatic_threads, input_threads, output_threads = distribute_threads( input_files, output_files, snakemake.threads ) input_r1, input_r2 = [ compose_input_gz(filename, input_threads) for filename in input_files ] output_r1, output_r1_unp, output_r2, output_r2_unp = [ compose_output_gz(filename, output_threads, compression_level) for filename in output_files ] shell( "trimmomatic PE -threads {trimmomatic_threads} {java_opts} {extra} " "{input_r1} {input_r2} " "{output_r1} {output_r1_unp} " "{output_r2} {output_r2_unp} " "{trimmer} " "{log}" ) |
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | __author__ = "Johannes Köster, Jorge Langa" __copyright__ = "Copyright 2016, Johannes Köster" __email__ = "koester@jimmy.harvard.edu" __license__ = "MIT" from snakemake.shell import shell from snakemake_wrapper_utils.java import get_java_opts # Distribute available threads between trimmomatic itself and any potential pigz instances def distribute_threads(input_file, output_file, available_threads): gzipped_input_files = 1 if input_file.endswith(".gz") else 0 gzipped_output_files = 1 if output_file.endswith(".gz") else 0 potential_threads_per_process = available_threads // ( 1 + gzipped_input_files + gzipped_output_files ) if potential_threads_per_process > 0: # decompressing pigz creates at most 4 threads pigz_input_threads = ( min(4, potential_threads_per_process) if gzipped_input_files != 0 else 0 ) pigz_output_threads = ( (available_threads - pigz_input_threads * gzipped_input_files) // (1 + gzipped_output_files) if gzipped_output_files != 0 else 0 ) trimmomatic_threads = ( available_threads - pigz_input_threads * gzipped_input_files - pigz_output_threads * gzipped_output_files ) else: # not enough threads for pigz pigz_input_threads = 0 pigz_output_threads = 0 trimmomatic_threads = available_threads return trimmomatic_threads, pigz_input_threads, pigz_output_threads def compose_input_gz(filename, threads): if filename.endswith(".gz") and threads > 0: return "<(pigz -p {threads} --decompress --stdout {filename})".format( threads=threads, filename=filename ) return filename def compose_output_gz(filename, threads, compression_level): if filename.endswith(".gz") and threads > 0: return ">(pigz -p {threads} {compression_level} > {filename})".format( threads=threads, compression_level=compression_level, filename=filename ) return filename extra = snakemake.params.get("extra", "") java_opts = get_java_opts(snakemake) log = snakemake.log_fmt_shell(stdout=True, stderr=True) compression_level = snakemake.params.get("compression_level", "-5") trimmer = " ".join(snakemake.params.trimmer) # Distribute threads trimmomatic_threads, input_threads, output_threads = distribute_threads( snakemake.input[0], snakemake.output[0], snakemake.threads ) # Collect files input = compose_input_gz(snakemake.input[0], input_threads) output = compose_output_gz(snakemake.output[0], output_threads, compression_level) shell( "trimmomatic SE -threads {trimmomatic_threads} " "{java_opts} {extra} {input} {output} {trimmer} {log}" ) |
Support
- Future updates
Related Workflows





