Source

utils / bwa_align.makefile

Full commit
SHELL = /bin/bash
BWA = /usr/local/bin/bwa
SAMTOOLS = /usr/local/bin/samtools
BWAOPT_ALN =
BWAOPT_PE =
BWAOPT_SE =
PICMERGE = /usr/local/bin/MergeSamFiles.jar
PICOPTS = VALIDATION_STRINGENCY=SILENT CREATE_INDEX=true MSD=true ASSUME_SORTED=true

fcid =  $(shell tail -1 SampleSheet.csv | cut -d',' -f1)
reference = $(shell tail -1 SampleSheet.csv | cut -d',' -f4)
ref_genome = /usr/local/scratch/genomes/$(reference)/bwa/$(reference)

experiment_name=$(subst Sample_,,$(notdir $(abspath $(dir .))))

r1_fastq := $(wildcard *R1*fastq.gz)
r2_fastq := $(wildcard *R2*fastq.gz)

chunk_bam := $(patsubst %.fastq.gz,%.bam,$(r1_fastq))
chunk_bam := $(subst _R1,,$(chunk_bam))


all: $(chunk_bam)
	java -jar $(PICMERGE) $(foreach bamfile,$^,I=$(bamfile)) \
	O=$(experiment_name).bam \
	CREATE_INDEX=true \
	MSD=true \
	VALIDATION_STRINGENCY=SILENT \
	ASSUME_SORTED=true \
	USE_THREADING=true


$(chunk_bam): $(r1_fastq) $(r2_fastq)
	$(eval lane := $(subst .bam,,$(shell echo $@ | rev | cut -d'_' -f 2 | rev)))
	$(eval rindex := $(subst .bam,,$(shell echo $@ | rev| cut -d'_' -f 3 | rev)))
	$(eval R1 := $(subst .bam,.fastq.gz,$(subst $(experiment_name)_$(rindex)_$(lane),$(experiment_name)_$(rindex)_$(lane)_R1,$@)))
	$(eval R2 := $(subst .bam,.fastq.gz,$(subst $(experiment_name)_$(rindex)_$(lane),$(experiment_name)_$(rindex)_$(lane)_R2,$@)))
ifeq ($(r2_fastq), )
	$(BWA) samse $(BWAOPT_SE) \
	-r "@RG\tID:$(experiment_name)_$(lane)\tPL:illumina\tPU:$(fcid)\tLB:$(experiment_name)\tSM:$(experiment_name)\tCN:CTGB" \
	$(ref_genome) \
	<( $(BWA) aln $(BWAOPT_ALN) $(ref_genome) $(R1)) \
	$(R1) | $(SAMTOOLS) view -Su - | $(SAMTOOLS) sort - $(subst .bam,,$@)
else
	$(BWA) sampe $(BWAOPT_PE) \
	-r "@RG\tID:$(experiment_name)_$(lane)\tPL:illumina\tPU:$(fcid)\tLB:$(experiment_name)\tSM:$(experiment_name)\tCN:CTGB" \
	$(ref_genome) \
	<( $(BWA) aln $(BWAOPT_ALN) $(ref_genome) $(R1)) \
	<( $(BWA) aln $(BWAOPT_ALN) $(ref_genome) $(R2)) \
	$(R1) \
	$(R2) | $(SAMTOOLS) view -Su - | $(SAMTOOLS) sort - $(subst .bam,,$@)
endif