SAMTOOLS BAM2FQ SEPARATE

Convert a bam file with paired end reads back to unaligned reads in a two separate fastq files with samtools. Reads that are not properly paired are discarded (READ_OTHER and singleton reads in samtools bam2fq documentation), as are secondary (0x100) and supplementary reads (0x800).

Software dependencies

  • samtools ==1.9

Example

This wrapper can be used in the following way:

rule samtools_bam2fq_separate:
    input:
        "mapped/{sample}.bam"
    output:
        "reads/{sample}.1.fq",
        "reads/{sample}.2.fq"
    params:
        sort = "-m 4G",
        bam2fq = "-n"
    threads: 3
    wrapper:
        "0.31.1/bio/samtools/bam2fq/separate"

Note that input, output and log file paths can be chosen freely. When running with

snakemake --use-conda

the software dependencies will be automatically deployed into an isolated environment before execution.

Authors

  • David Laehnemann
  • Victoria Sack

Code

__author__ = "David Laehnemann, Victoria Sack"
__copyright__ = "Copyright 2018, David Laehnemann, Victoria Sack"
__email__ = "david.laehnemann@hhu.de"
__license__ = "MIT"


import os
from snakemake.shell import shell

prefix = os.path.splitext(snakemake.output[0])[0]

shell(
    "samtools sort -n "
    " -@ {snakemake.threads} "
    " -T {prefix} "
    " {snakemake.params.sort} "
    " {snakemake.input[0]} | "
    "samtools bam2fq "
    " {snakemake.params.bam2fq} "
    " -1 {snakemake.output[0]} "
    " -2 {snakemake.output[1]} "
    " -0 /dev/null "
    " -s /dev/null "
    " -F 0x900 "
    " - "
    )