forked from jhammelman/gifford-align
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_accessible_regions-from-bam.py
176 lines (154 loc) · 7.73 KB
/
call_accessible_regions-from-bam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/env python
import argparse
import subprocess
import os
from collections import defaultdict
genome_size = {'mm10':1.87e9,
'hg38':2.7e9}
genome_blacklist = {"mm10":"/archive/gl/shared/user/jhammelm/old_cluster/jhammelm/genomes/atac_mm10/mm10/mm10.blacklist.bed.gz",
"hg38":"/archive/gl/shared/user/jhammelm/old_cluster/jhammelm/genomes/atac_human/hg38/hg38.blacklist.bed.gz"}
def ensure_dir(file_path):
if not os.path.exists(file_path):
os.makedirs(file_path)
def process_bam(bam_info,args):
cmds = []
bam = bam_info['bamfile']+'.bam'
bam_qname = bam_info['experiment']+'/bams/'+bam_info['samplename']+'-qnamesort.bam'
bam_filt = bam_info['experiment']+'/bams/'+bam_info['samplename']+'-filt1.bam'
bam_fixmate = bam_info['experiment']+'/bams/'+bam_info['samplename']+'-fixmate.bam'
final_bam=bam_info['experiment']+'/bams/'+bam_info['samplename']+'-filtered.bam'
bam_plusstrand = bam_info['experiment']+'/bams/'+bam_info['samplename']+'-plus.bam'
bam_minusstrand = bam_info['experiment']+'/bams/'+bam_info['samplename']+'-minus.bam'
bamstats = bam_info['experiment']+'/stats/'+bam_info['samplename']+'.stats'
#Filt Bam (mito, unmapped, mapq)
if bam_info['readtype'] == 'se':
cmds.append(["samtools sort -n","-@",str(args.nthreads),bam,"-o ",bam_qname])
cmds.append(["samtools view -F 1796 -u",bam_qname,"| samtools sort /dev/stdin -o",final_bam])
cmds.append(["rm -f ",bam_qname])
cmds.append(["samtools index",final_bam])
elif bam_info['assay'] == 'dnase':
cmds.append(["samtools sort -n",bam,"-o",bam_qname])
cmds.append(["samtools fixmate -r",bam_qname,bam_fixmate])
cmds.append(["rm -f",bam_qname])
cmds.append(["samtools view -F 1804 -f 2 -u",bam_fixmate,
"| samtools sort /dev/stdin -o",final_bam])
cmds.append(["rm -f",bam_fixmate])
cmds.append(["samtools index",final_bam])
cmds.append(["samtools flagstat",final_bam,">",bamstats])
else:
#technically, should shift bam reads for ATAC-seq - option to uncomment in future
#cmds.append(["samtools view -f 13 -u",bam," | awk '{$4+=4;print $0}' | samtools sort /dev/stdin -n -o",bam_plusstrand])
#cmds.append(["samtools view -f 33 -u",bam," | awk '{$4-=5;print $0}' | samtools sort /dev/stdin -n -o",bam_minusstrand])
#cmds.append(["samtools merge ",bam_qname,bam_plusstrand,bam_minusstrand])
#cmds.append(["rm -f",bam_plusstrand])
#cmds.append(["rm -f",bam_minusstrand])
cmds.append(["samtools sort -n ",bam,"-o ",bam_qname])
cmds.append(["samtools fixmate -r",bam_qname,bam_fixmate])
cmds.append(["rm -f",bam_qname])
cmds.append(["samtools view -F 1804 -f 2 -u",bam_fixmate,
"| samtools sort /dev/stdin -o",final_bam])
cmds.append(["rm -f",bam_fixmate])
cmds.append(["samtools index",final_bam])
cmds.append(["samtools flagstat",final_bam,">",bamstats])
return cmds
def tag2region(bam_info,args):
cmds=[]
NPEAKS=300000
bam=bam_info['experiment']+'/bams/'+bam_info['samplename']+'-filtered.bam'
prefix = bam_info['experiment']+'/regions/'+bam_info['samplename']+'-threshold'+str(args.pval)
#Macs2 peaks
if bam_info['assay'] == 'atac':
smooth_window = 73
else:
smooth_window = 150
shiftsize = round(-smooth_window/2)
if 'mm10' == bam_info['genome_build']:
genome='mm'
else:
genome='hs'
if bam_info['readtype'] == 'se':
cmds.append(["macs2 callpeak -t",bam,"-f BAM","-n",prefix,"-g",genome,
"-p",str(args.pval),"--shift",str(shiftsize),'--extsize',str(smooth_window),'--nomodel',
'--keep-dup all','--call-summits'])
elif bam_info['readtype'] == 'pe':
cmds.append(["macs2 callpeak -t",bam,"-f BAMPE","-n",prefix,"-g",genome,
"-p",str(args.pval),"--shift",str(shiftsize),'--extsize',str(smooth_window),'--nomodel',
'--keep-dup all','--call-summits'])
cmds.append(["sort -k 8gr,8gr",prefix+"_peaks.narrowPeak | awk 'BEGIN{OFS=\"\\t\"}{$4=\"Peak_\"NR ; print $0}' | gzip -nc >",prefix+".narrowPeak.gz"])
cmds.append(["rm -f",prefix+"_peaks.narrowPeak"])
cmds.append(["rm -f",prefix+"_peaks.xls"])
cmds.append(["rm -f",prefix+"_summits.bed"])
cmds.append(["bedtools intersect -v -a ",prefix+".narrowPeak.gz","-b",
genome_blacklist[bam_info['genome_build']],
"| awk 'BEGIN{OFS=\"\\t\"} {if ($5>1000) $5=1000; print $0}'",
"| grep -P 'chr[\dXY]+[ \\t]' | gzip -nc >",prefix+'.filt.narrowPeak.gz'])
return cmds
def process_csv(args):
hasheader=True
expts = defaultdict(list)
for line in open(args.experiment_template):
if hasheader:
hasheader=False
header=line.strip().split(',')
assert('pe/se' in header)
assert('assay' in header)
assert('samplename' in header)
assert('bamfile' in header)
assert('genome_build' in header)
continue
data = line.strip().split(',')
exptname = data[header.index('experiment')]
assay = data[header.index('assay')]
assert(assay in ['atac','dnase'])
readtype = data[header.index('pe/se')]
assert(readtype in ['pe','se'])
expts[exptname].append({'experiment':exptname,
'samplename':data[header.index('samplename')],
'bamfile':data[header.index('bamfile')],
'assay':data[header.index('assay')],
'genome_build':data[header.index('genome_build')],
'readtype':data[header.index('pe/se')]})
return expts
def pool_bam(expt,bams_info):
#returns list of samtools merge command
# pooled bam info dict
cmd = ['samtools merge '+expt+'/bams/'+expt+'_pooled_reps-filtered.bam']
for bam in bams_info:
cmd.append(expt+'/bams/'+bam['samplename']+'-filtered.bam')
return cmd,{'experiment':expt,
'samplename':expt+'_pooled_reps',
'assay':bam['assay'],
'readtype':bam['readtype'],
'genome_build':bam['genome_build']}
if __name__ == "__main__":
#Steps
parser = argparse.ArgumentParser()
parser.add_argument('experiment_template')
parser.add_argument('-t','--nthreads',type=int,default=2)
parser.add_argument('-p','--pval',default=0.01,type=float)
parser.add_argument('-nop','--nopeaks',default=False,action='store_true')
#parser.add_argument('-mapq','--mapq',default=255,type=int)
opts = parser.parse_args()
expt_data = process_csv(opts)
#pool bams, call on pooled
for expt in expt_data.keys():
cmds = []
ensure_dir(expt)
ensure_dir(expt+'/bams')
ensure_dir(expt+'/stats')
ensure_dir(expt+'/regions')
for bam_info in expt_data[expt]:
cmds.extend(process_bam(bam_info,opts))
pool_cmd,pool_data = pool_bam(expt,expt_data[expt])
cmds.append(pool_cmd)
expt_data[expt].append(pool_data)
if not opts.nopeaks:
for bam_info in expt_data[expt]:
cmds.extend(tag2region(bam_info,opts))
with open(expt+'/call_accessible.sh','w') as f:
for cmd in cmds:
f.write(' '.join(cmd)+'\n')
subprocess.run(["qsub -m e -M [email protected] -pe slots.pe "+str(opts.nthreads)+" -v PATH=$PATH -wd $PWD -N call_accessible-"+expt+" ./"+expt+"/call_accessible.sh"],shell=True)
#run IDR on replicates? Not really necessary since we
#never use these for anything
#IDR