forked from algaebrown/oligoCLIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake_downbam_UMI.py
202 lines (180 loc) · 6.97 KB
/
Snake_downbam_UMI.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#configfile: "config.yaml"
# snakemake -j 9 -s Snake_downbam_UMI.py --configfile config/estimate_read_saturation/se_downsample.yaml --cluster "qsub -l walltime={params.run_time}:00:00 -l nodes=1:ppn={params.cores} -q home-yeo" -n
# snakemake -j 9 -s Snake_downbam_UMI.py --configfile config/estimate_read_saturation/se_downsample_SLBP.yaml --cluster "qsub -l walltime={params.run_time}:00:00 -l nodes=1:ppn={params.cores} -q home-yeo" -n
# downsample bams to match the # of mapped reads
import pandas as pd
import os
import sys
import glob
import numpy as np
manifest = pd.read_table(config['DOWNSAMPLE_MENIFEST'], index_col = 0, sep = ',')
config['CLIPper_pvalthes'] = None
workdir: config['WORKDIR']
def get_total_reads(sample_label):
with open(f"downsample_bam_UMI/{sample_label}.totalcount", 'r') as f:
nread = int(f.readlines())[0]
return int(nread.rstrip())
# downsample targets
targets = [int(i) for i in np.array([0.1, 0.2, 0.5, 0.8, 1, 2, 4]) * int(config['max_read'])]
seeds = []
sample_labels = manifest.Sample.tolist()
module Snake_CLIPper:
snakefile:
# here, plain paths, URLs and the special markers for code hosting providers (see below) are possible.
#"Snake_downbam_UMI",
"rules/Snake_CLIPper.py"
config:
config
module peak_anno:
snakefile:
"rules/Snake_peakanno.py"
config: config
rule all:
input:
expand("downsample_bam_UMI_counts/{sample_label}.{nread}.rmDup.count",
sample_label = [s for s in sample_labels if 'Dan' in s],
nread = targets),
# expand("downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.normed.compressed.annotate.bed",
# sample_label = ['Dan_singleplex_K562_rep1_RBFOX2', 'katieoligo_RBFOX2_rep1'],
# nread = np.array(targets)[[0,1,2]])
output:
"snakeUMI.txt"
params:
error_out_file = "error_files/all",
run_time = 1,
cores = "1",
memory = "20",
job_name = "all"
shell:
"""
echo $(date) > {output};
echo created by HLH and the Yeo lab >> {output}
"""
rule count_read_begin:
input:
bam=lambda wildcards: glob.glob(manifest.loc[manifest.Sample == wildcards.sample_label]["bam"].values[0])
output:
"downsample_bam_UMI/{sample_label}.totalcount"
params:
run_time=1,
error_out_file = "error_files/downsample",
cores = "1",
shell:
"""
module load samtools
samtools view -cF 4 {input.bam} > {output}
"""
rule downsample_bam:
input:
bam=lambda wildcards: glob.glob(manifest.loc[manifest.Sample == wildcards.sample_label]["bam"].values[0]),
count_csv = "downsample_bam_UMI/{sample_label}.totalcount"
output:
subsample_bam="downsample_bam_UMI/{sample_label}.{nread}.bam",
subsample_bai="downsample_bam_UMI/{sample_label}.{nread}.bam.bai"
params:
run_time=1,
error_out_file = "error_files/downsample",
cores = "1",
nread="{nread}"
shell:
"""
module load samtools
total=$(cat {input.count_csv})
frac=$(awk "BEGIN {{print ({params.nread})/$total}}")
samtools view -h -F 4 -s 5$frac {input.bam} | samtools sort - | samtools view -Sb - > {output.subsample_bam}
samtools index {output.subsample_bam}
"""
rule umi_dedup:
input:
bam="downsample_bam_UMI/{sample_label}.{nread}.bam",
bai="downsample_bam_UMI/{sample_label}.{nread}.bam.bai"
output:
bam_dedup="downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam"
params:
error_out_file="error_files/umidedup",
run_time = 4,
cores = "4",
memory = "10000",
job_name = "sortbam",
prefix='downsample_bam_UMI/{sample_label}.{nread}'
shell:
"""
module load eclip;
umi_tools dedup \
--random-seed 1 \
-I {input.bam} \
--method unique \
--output-stats {params.prefix} \
-S {output.bam_dedup}
"""
rule count_read:
input:
bam_dedup="downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam"
output:
"downsample_bam_UMI_counts/{sample_label}.{nread}.rmDup.count"
params:
error_out_file = "error_files/all",
run_time = 1,
cores = "1",
memory = "20",
job_name = "all"
shell:
"""
module load samtools
samtools view -cF 4 {input.bam_dedup} > {output}
"""
rule index_bam:
input:
bam_dedup="downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam"
output:
"downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam.bai"
params:
error_out_file = "error_files/all",
run_time = 1,
cores = "1",
memory = "20",
job_name = "all"
shell:
"""
module load samtools
samtools index {input.bam_dedup}
"""
use rule clipper from Snake_CLIPper as clipper with:
input:
subsample_bam="downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam",
subsample_bai="downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam.bai"
output:
peak="downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.bed"
params:
error_out_file = "error_files/all",
run_time = 16,
cores = "10",
memory = "20",
job_name = "all",
species=config['SPECIES'],
use rule count_read_num from Snake_CLIPper as count_read2 with:
input:
subsample_bam_ip="downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam",
subsample_bam_in=lambda wildcards: glob.glob(manifest.loc[manifest.Sample == wildcards.sample_label]["bam_control"].values[0])
output:
nread_ip="downsample_bam_UMI/output/{sample_label}.{nread}.ip.readnum.txt",
nread_in="downsample_bam_UMI/output/{sample_label}.{nread}.in.readnum.txt"
use rule norm_peaks from Snake_CLIPper as norm_peak with:
input:
subsample_bam_ip="downsample_bam_UMI/{sample_label}.{nread}.rmDup.bam",
subsample_bam_in=lambda wildcards: glob.glob(manifest.loc[manifest.Sample == wildcards.sample_label]["bam_control"].values[0]),
nread_ip="downsample_bam_UMI/output/{sample_label}.{nread}.ip.readnum.txt",
nread_in="downsample_bam_UMI/output/{sample_label}.{nread}.in.readnum.txt",
peak="downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.bed"
output:
norm_peak="downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.normed.bed"
use rule compress_peak from Snake_CLIPper as compress_peak with:
input:
norm_peak="downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.normed.bed"
output:
compress_peak="downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.normed.compressed.bed"
use rule annotate from peak_anno as annotate_peak with:
input:
peak="downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.normed.compressed.bed"
output:
"downsample_bam_UMI/CLIPper/{sample_label}.{nread}.peaks.normed.compressed.annotate.bed"