modified: src1/worker.c
[GalaxyCodeBases.git] / makefiles / bwa.mk
blob2613f104749c47aa8055837be9cee593e3a65013
1 INPUTPATH := mda mlbac
2 OUTPUTPREFIX := out
4 ALNARG:=aln -l 17 -q 10
5 SAMPEARG:=sampe -a 800
6 SAMSORTARG:=sort -l 9
7 REF:=ref/hg19p10XYM
9 INF := $(patsubst %_1.fastq.gz,%,$(foreach ONEINPUTPATH,$(INPUTPATH),$(wildcard $(ONEINPUTPATH)/*_1.fastq.gz)))
11 OUTF := $(addprefix $(OUTPUTPREFIX)/,$(INF))
12 SAI1 := $(addsuffix _1.sai,$(OUTF))
13 SAI2 := $(addsuffix _2.sai,$(OUTF))
14 SAMS := $(addsuffix .sam.gz,$(OUTF))
15 BAMS := $(addsuffix .bam,$(OUTF))
16 BAMSORT := $(addsuffix .sort.bam,$(OUTF))
17 BAMRMDUP := $(addsuffix .rmdup.bam,$(OUTF))
19 PATHS := $(addprefix $(OUTPUTPREFIX)/,$(MDAPATH) $(MLBACPATH))
21 NUMPROC := $(shell grep -c ^processor /proc/cpuinfo)
22 SAMCOUNT := $(words $(SAI1))
23 SAITHREADS := $(shell echo 1.5*$(NUMPROC)/$(SAMCOUNT) |bc)
24 ifneq ($(SAITHREADS),)
25 ifneq ($(SAITHREADS),0)
26 ifneq ($(SAITHREADS),1)
27 ALNARG += -t $(SAITHREADS)
28 endif
29 endif
30 endif
31 FREEMEM := $(shell free -m|grep -e '-/+ buffers/cache'|awk '{print $$NF}')
32 SAMMEM := $(shell echo 0.8*$(FREEMEM)/$(SAMCOUNT)/$(SAITHREADS) |bc)
34 NEEDED_COMMANDS := bc bwa samtools grep mkdir free gzip
36 all: $(OUTF)
37 @echo "[$(OUTF)]" "[$(INF)]" "[$@]" "[$(SAMMEM) $(FREEMEM)]"
38 date > $(OUTPUTPREFIX)/_alldone.log
40 check:
41 @for thecmd in $(NEEDED_COMMANDS); do \
42 if ! command -v "$${thecmd%% *}" >/dev/null 2>&1; then \
43 checkok="0"; \
44 echo "[x]'$${thecmd%% *}' not found."; \
45 fi; \
46 done; \
47 if [ "$${checkok}" == "0" ]; then \
48 echo "[!]Please install missing cmd(s) above."; \
49 exit -1; \
50 fi;
52 $(PATHS): check
53 mkdir -p $@
55 $(OUTF): $(PATHS) $(BAMRMDUP)
56 $(eval IN := $(patsubst $(OUTPUTPREFIX)/%,%,$@))
57 echo "$@" "$<" "$(IN)"
58 touch $@
60 $(SAI1) $(SAI2): | $(PATHS)
61 $(eval IN := $(patsubst $(OUTPUTPREFIX)/%,%,$(@:%.sai=%)))
62 bwa $(ALNARG) $(REF) $(IN).fastq.gz >$@ 2>$@.log && \
63 date >>$@.log && \
64 echo done. >>$@.log
66 $(SAMS): $(SAI1) $(SAI2)
67 $(eval IN := $(@:%.sam.gz=%))
68 $(eval FQ := $(patsubst $(OUTPUTPREFIX)/%,%,$(IN)))
69 bwa $(SAMPEARG) $(REF) $(IN)_1.sai $(IN)_2.sai $(FQ)_1.fastq.gz $(FQ)_2.fastq.gz 2>$(IN).sam.log |gzip -9c >$@ && \
70 date >>$(IN).sam.log && \
71 echo done. >>$(IN).sam.log
73 $(BAMS): $(SAMS)
74 samtools view -bS $< >$@ 2>$@.log && \
75 date >>$@.log && \
76 echo "done (sam.gz to bam)">>$@.log
78 $(BAMSORT): $(BAMS)
79 if [ "$(SAMMEM)" -gt "768" ]; then \
80 SAMSORTMEM="-@ $(SAITHREADS) -m $(SAMMEM)M";\
81 else \
82 SAMSORTMEM=;\
83 fi;\
84 samtools $(SAMSORTARG) $$SAMSORTMEM $< $(@:%.bam=%) >>$<.log && \
85 date >>$<.log && \
86 echo "done (bam sort)">>$<.log
88 $(BAMRMDUP): $(BAMSORT)
89 $(eval LOG := $(@:%.rmdup.bam=%.bam.log))
90 samtools rmdup $< $@ >>$(LOG) && \
91 date >>$(LOG) && \
92 echo "done (bam rmdup)">>$(LOG)
94 help: check
95 @if [ "$(NUMPROC)" -gt "$(SAMCOUNT)" ]; then \
96 JOBCNT="$(SAMCOUNT)";\
97 fi;\
98 echo -e "Usage: make -j $(SAMCOUNT) |tee make.log\n\n$(NUMPROC) core(s) & $(FREEMEM) mb free memory found now.\nBWA will run sai with [$(ALNARG)] for your $(SAMCOUNT) bam file(s)."
99 @if [ "$(SAMMEM)" -gt "768" ]; then \
100 echo -e "SAMTOOLS will sort with [-@ $(SAITHREADS) -m $(SAMMEM)M], if free memory remains same when you type make."; \
103 clean:
104 rm -fr out/*
106 .PHONY: help clean check