Read-mapping against the contig catalogue from metagenomics by illumina and nanopore

public public 1yr ago 0 bookmarks

Snakemake workflow: snakemap

A Snakemake workflow for snakemap

A snakemake workflow for read-mapping against the contig catalogue from metagenomics by illumina and nanopore

Usage

  1. Init
python init.py -i /path/to/illumina_fqs -n /path/to/nanopore_fqs -w /path/to/workdir
  1. Run
snakemake all -j6 --snakefile /path/to/Snakefile --configfile /path/to/config.yaml --directory /path/to/workdir --use-conda

Code Snippets

 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
__author__ = "Johannes Köster"
__copyright__ = "Copyright 2016, Johannes Köster"
__email__ = "koester@jimmy.harvard.edu"
__license__ = "MIT"


import os
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)


n = len(snakemake.input.sample)
assert (
    n == 1 or n == 2
), "input->sample must have 1 (single-end) or 2 (paired-end) elements."

if n == 1:
    reads = "-U {}".format(*snakemake.input.sample)
else:
    reads = "-1 {} -2 {}".format(*snakemake.input.sample)


index = os.path.commonprefix(snakemake.input.idx).rstrip(".")


shell(
    "(bowtie2"
    " --threads {snakemake.threads}"
    " {reads} "
    " -x {index}"
    " {extra}"
    "| samtools view"
    " {samtools_opts}"
    " -"
    ") {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__ = "Daniel Standage"
__copyright__ = "Copyright 2020, Daniel Standage"
__email__ = "daniel.standage@nbacc.dhs.gov"
__license__ = "MIT"


import os
from snakemake.shell import shell

extra = snakemake.params.get("extra", "")
log = snakemake.log_fmt_shell(stdout=True, stderr=True)


index = os.path.commonprefix(snakemake.output).rstrip(".")


shell(
    "bowtie2-build"
    " --threads {snakemake.threads}"
    " {extra}"
    " {snakemake.input.ref}"
    " {index}"
    " {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}"
)
 7
 8
 9
10
11
12
13
14
15
run:
    with open(output[0], "w") as out:
        with open (input[0], "r") as inp:
            i = 1
            for line in inp:
                if line.startswith(">"):
                    line = ">id_" + str(i) + "\n"
                    i += 1 
                out.write(line)
26
wrapper: "v1.12.2/bio/bowtie2/build"
38
wrapper: "v1.12.2/bio/bowtie2/align"
45
46
shell:
    "(samtools view -h {input} | {workflow.basedir}/scripts/read_count_bam.pl | samtools view -Su -q30 - | samtools sort -O BAM -o {output} -) 2>> {log}"
54
wrapper: "v1.12.2/bio/samtools/index"
63
64
65
66
67
shell:
    """
    echo '#OTU ID' > {output}
    samtools idxstats $(ls {input.bam} | head -n 1) | grep -v "*" | cut -f1 >> {output}
    """
76
77
78
79
80
shell:
    """
    echo '{wildcards.sample}' > {output}
    samtools idxstats {input.bam} | grep -v "*" | cut -f3 >> {output}
    """
88
89
90
91
92
93
94
95
96
97
shell:
    """
    cp {input.ids} {output}
    # split if exceed 1000 (max 1024 by default in most OS)
    while read fs; do
        paste {output} $fs > {output}.tmp
        # redirection first
        mv {output}.tmp {output}
    done < <(echo {input.counts} | xargs -n 1000)
    """
17
shell: "minimap2 -I {params.index_size} -d {output} {input} 2> {log}"
24
shell: "samtools dict {input} | cut -f1-3 > {output} 2> {log}"
37
38
39
40
41
shell:
    """
    minimap2 -t {threads} -ax {params.x} --secondary=no {input.mmi} {input.fq} 2> {log} | \
    grep -v "^@" | cat {input.dict} - > {output} 2>> {log}
    """
51
52
shell:
    "samtools view -F 3584 -b {input} | samtools sort - -T {params.prefix} -m {params.m} -o {output} 2>{log}"
ShowHide 12 more snippets with no or duplicated tags.

Login to post a comment if you would like to share your experience with this workflow.

Do you know this workflow well? If so, you can request seller status , and start supporting this workflow.

Free

Created: 1yr ago
Updated: 1yr ago
Maitainers: public
URL: https://github.com/yanhui09/snakemap
Name: snakemap
Version: 1
Badge:
workflow icon

Insert copied code into your website to add a link to this workflow.

Downloaded: 0
Copyright: Public Domain
License: GNU General Public License v3.0
  • Future updates

Related Workflows

cellranger-snakemake-gke
snakemake workflow to run cellranger on a given bucket using gke.
A Snakemake workflow for running cellranger on a given bucket using Google Kubernetes Engine. The usage of this workflow ...