# Need samtools and bowtie2 in path to work.
# Execute with command: snakemake -s samtools ../intermediate_data/[run_name]_sorted.bam

import re
import subprocess
import shlex

configfile: "config.json"

# path to index file without extension (removed .fa or .fasta)
folder = "intermediate_data/"
index_basename=folder+"index/"+config["reference_name"]
sample=config["name"]+"_on_ref_"+config["reference_name"]

def get_samtools_sort_command():
    p1 = subprocess.Popen(shlex.split(r'samtools --version'), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    p2 = subprocess.Popen(shlex.split(r'head -n 1'), stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    p3 = subprocess.Popen(shlex.split(r'cut -d" " -f2'), stdin=p2.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, error = p3.communicate()
    print ("out:"+ out.decode("utf-8"))
    if error:
        print('Error: '  + e.decode('ascii'))
    #print('code: ' + str(proc.returncode))

    version = float(out.decode("utf-8").split("\n")[0])
    print ("samtools version being used:"+ str(version) )

    if version >= 1.3:
        return ">"
    else:
        return "-f" 

rule samtools_sort_already_mapped:
    input:
        # Read files
        bam_file = rules.subsample_map.output
    output:
        folder+"from_mapped_"+config["name"]+str(config["subsample"])+"_sorted.bam"
    threads:
        config["threads"]
    log:
        "logs/samtools_sort_already_mapped_"+sample+".log"
    run:
        shell("samtools sort {input.bam_file} -f {output} \n \
        samtools index {output}")
    

rule samtools_sort_new_mapped:
    input:
        # Read files
        bam = rules.bowtie2_map.output
    output:
        folder+"from_raw_"+config["name"]+str(config["subsample"])+"_sorted.bam"
    threads:
        config["threads"]
    log:
        "logs/samtools_sort_new_mapped_"+sample+".log"
    run:
        command = get_samtools_sort_command()
        shell("samtools sort {input.bam} "+command+" {output} \n \
        samtools index {output} \n \
        rm {input.bam}")
