configfile: "config.yaml"
print ("config file:", config['samples'])
print ("genome:", config['genome'])
print (len(config['samples']))


import os, sys, re, glob, shutil
import pandas as pd
from datetime import datetime
import multiprocessing as mp
from itertools import chain



# rule all:
#     input:
#         expand("results/mapped_sorted_reads/{sample}.bam.bai", sample = config['samples'])

rule all:
    input:
        expand("results/mapped_sorted_reads/{sample}.bam.bai", sample = config['samples'])

rule filter_fastq:
    input:
        lambda wildcards: config["samples"][wildcards.sample]
        #"fastq_tiny_1000/{sample}.fastq"
    output:
        "results/filtered_fastq/{sample}.fastq"
    script:
        "scripts/filter_fastq.py"

rule align:
    input:
        fasta = config['fasta'],
        fastq = "results/filtered_fastq/{sample}.fastq"
        #fastq= lambda wildcards: config["samples"][wildcards.sample]
    output:
        #"results/{sample}.bam"
        "results/mapped_reads/{sample}.bam"
    #params:
    #    rg=r"@RG\tID:{sample}\tSM:{sample}"
    log:
        "results/logs/alignment/{sample}.log"
    threads: 4
    run:
        if config["aligner"] == "bwa-mem":
            if config["index"] == True:
                shell("bwa mem {input.fasta}")
            #"(bwa mem -R '{params.rg}' -t {threads} {input} | "
            shell("bwa mem -t {threads} {input} | samtools view -Sb - > {output} 2> {log}")
        if config["aligner"] == "bowtie2":
            if config["index"] == True:
                shell("bowtie2-build {input.fasta} {input.fasta}")
            #"(bwa mem -R '{params.rg}' -t {threads} {input} | "
            shell("bowtie2 -x {input.fasta} -U {input.fastq} -p {threads}| samtools view -Sb - > {output} 2> {log}")


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


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

dce16781f113f86838fb96602f835ad575b01885
configfile: "config.yaml"
print ("config file:", config['samples'])
print ("genome:", config['genome'])
print (len(config['samples']))


import os, sys, re, glob, shutil
import pandas as pd
from datetime import datetime
import multiprocessing as mp
from itertools import chain



# rule all:
#     input:
#         expand("results/mapped_sorted_reads/{sample}.bam.bai", sample = config['samples'])

rule all:
    input:
        expand("results/mapped_sorted_reads/{sample}.bam.bai", sample = config['samples'])

rule filter_fastq:
    input:
        lambda wildcards: config["samples"][wildcards.sample]
        #"fastq_tiny_1000/{sample}.fastq"
    output:
        "results/filtered_fastq/{sample}.fastq"
    script:
        "scripts/filter_fastq.py"

rule align:
    input:
        fasta = config['fasta'],
        fastq = "results/filtered_fastq/{sample}.fastq"
        #fastq= lambda wildcards: config["samples"][wildcards.sample]
    output:
        #"results/{sample}.bam"
        "results/mapped_reads/{sample}.bam"
    #params:
    #    rg=r"@RG\tID:{sample}\tSM:{sample}"
    log:
        "results/logs/alignment/{sample}.log"
    threads: 4
    run:
        if config["aligner"] == "bwa-mem":
            if config["index"] == True:
                shell("bwa mem {input.fasta}")
            #"(bwa mem -R '{params.rg}' -t {threads} {input} | "
            shell("bwa mem -t {threads} {input} | samtools view -Sb - > {output} 2> {log}")
        if config["aligner"] == "bowtie2":
            if config["index"] == True:
                shell("bowtie2-build {input.fasta} {input.fasta}")
            #"(bwa mem -R '{params.rg}' -t {threads} {input} | "
            shell("bowtie2 -x {input.fasta} -U {input.fastq} -p {threads}| samtools view -Sb - > {output} 2> {log}")


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


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