rule bam_to_sam:
	input: "04_bam_files/{sample}.sorted.dedup.bam"
	output: "03_sam_files/{sample}.sorted.dedup.sam"
	log: "00_logs/{sample}_bam_to_sam.log"
	resources:
		mem_mb = 1024 * 16, # Last number is memory in GB
		time = 60 * 24 * 2, # Last number is days
		#account = "publicgrp", # Placeholder for SLURM submission
		#partition = "high", # Placeholder for SLURM submission
	shell: "samtools view -@ THREADS '{input}' > '{output}' 2> '{log}'"
		
rule sam_to_aln:
	input: "03_sam_files/{sample}.sorted.dedup.sam"
	output: "06_cisgenome_files/{sample}.aln"
	log: "00_logs/{sample}_sam_to_aln.log"
	resources:
		mem_mb = 1024 * 16, # Last number is memory in GB
		time = 60 * 24 * 2, # Last number is days
		#account = "publicgrp", # Placeholder for SLURM submission
		#partition = "high", # Placeholder for SLURM submission
	shell: "'{src}'/tools/sam2aln -i '{input}' > '{output}' 2> '{log}'"

def sample_group(wildcards):
	group = Samples[wildcards.sample]
	return expand("06_cisgenome_files/{sample}.aln", sample = group)

def control_group(wildcards):
	group = Controls[wildcards.control]
	return expand("06_cisgenome_files/{sample}.aln", sample = group)
		
rule write_filelist:
	input:
		s = sample_group,
		c = control_group
	output: "06_cisgenome_files/{sample}_{control}_filelist.txt"
	run:
		for sample in input.s: shell("echo \'{sample}\t1\' >> '{output}'")
		for control in input.c: shell("echo \'{control}\t0\' >> '{output}'")

rule call_peaks:	
	input: "06_cisgenome_files/{sample}_{control}_filelist.txt"
	output:
		multiext("06_cisgenome_peaks/{sample}_{control}", "_peak.cod", "_log2fc.bar",
			"_t.bar")
	log: "00_logs/{sample}_{control}_cisgenome_peaks.log"
	resources:
		mem_mb = 1024 * 25, # Last number is memory in GB
		time = 60 * 24 * 2, # Last number is days
		#account = "publicgrp", # Placeholder for SLURM submission
		#partition = "high", # Placeholder for SLURM submission
	shell:
		"seqpeak -i '{input}' -d 06_cisgenome_peaks -o '{wildcards.sample}_{wildcards.control}' "
		"2> '{log}'"
