shell.prefix("source config.sh; set -eo pipefail ; ")

configfile: "config.json"

import os
import glob
import csv

TMP = config["local_path"]
REF = config["reference"]
REFI = config["reference"] + ".bwt"

BWA_COMMAND = config["bwa_prefix"] + config["bwa_special"]

SAMPLES=config["samples"].keys()

def _get_reads_by_sample(wildcards):
    return config["samples"][wildcards.samples]["reads"].values()

def _get_read_group_by_sample(wildcards):
    return config["samples"][wildcards.samples]["read_group"]

rule beer    :
     input   : expand("sorted_bam/{samples}.bwa_aln.sort.bam.bai", samples=SAMPLES)

rule burrito :
     input   : "sorted_bam/{samples}.bwa_aln.sort.bam"
     output  : "sorted_bam/{samples}.bwa_aln.sort.bam.bai"
     params  : sge_opts=config["cluster_settings"]["lite"]
     shell   : "samtools index {input}"

rule salsa   :
     input   : "raw_bam/{samples}.bwa_aln.bam"
     output  : "sorted_bam/{samples}.bwa_aln.sort.bam"
     params  : sge_opts=config["cluster_settings"]["heavy"]
     shell   : "samtools sort --threads 10 -o {output} {input}"

rule chips   :
     input   : R={REF}, RI={REFI}, READS=_get_reads_by_sample,
     output  : "raw_bam/{samples}.bwa_aln.bam"
     params  : sge_opts=config["cluster_settings"]["heavy"], RG=_get_read_group_by_sample
     shell   : "{BWA_COMMAND} {params.RG} {input.R} {input.READS} | samtools view -Sb > {TMP}/{wildcards.samples}.bam ; rsync  --ignore-existing --bwlimit=50000  {TMP}/{wildcards.samples}.bam {output}"

rule index   :
     input   : {REF}
     output  : {REFI}
     params  : sge_opts=config["cluster_settings"]["lite"]
     shell   : "bwa index {REF}"