-
Notifications
You must be signed in to change notification settings - Fork 2
/
sim.sh
281 lines (243 loc) · 7.31 KB
/
sim.sh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/sh
# Simulate datasets to compare PP5mC with HBS-tools.
export odir=simout
mkdir -p $odir
export PATH=$HOME/src/pp5mC:$HOME/src/pp5mC/HBS-tools/hbs_tools.v1.1:$HOME/src/bowtie-1.2.2-linux-x86_64:$PATH
export ref=/localscratch/Refs/H_sapiens/chr1-100mb/chr1-100mb.fa
nseqs=100000
readlen=150
profile1=profile.INS.r1.txt
profile2=profile.INS.r2.txt
fraglens="4.0|0.25 5.0|0.4"
moltypes="hp bs pal"
for fraglen in $fraglens; do
# ancient DNA fraglen dist (median ~55)
# R> x=20:100; y=dlnorm(x,4.0,0.25); plot(x,y)
# modern DNA fraglen dist (median ~150)
# R> x=20:350; y=dlnorm(x,5.0,0.4); plot(x,y)
mu=$(echo $fraglen | cut -f1 -d\|)
sigma=$(echo $fraglen | cut -f2 -d\|)
for moltype in $moltypes; do
pfx="sim-r${readlen}-u${mu}-s${sigma}-${moltype}"
echo simhbs \
-m $moltype \
-u $mu \
-s $sigma \
-E $profile1,$profile2 \
-n $nseqs \
-o $odir/$pfx \
$ref
done
done | parallel
# Concatenate $pfx's into $sims. Can't do this above, as the for loop runs
# in a separate process when piping, so $sims loses its value.
sims=""
for fraglen in $fraglens; do
mu=$(echo $fraglen | cut -f1 -d\|)
sigma=$(echo $fraglen | cut -f2 -d\|)
for moltype in $moltypes; do
pfx="sim-r${readlen}-u${mu}-s${sigma}-${moltype}"
sims="$sims $pfx"
done
done
do_hbs() {
sim=$1
/usr/bin/time \
hbs_process \
--hpmeth \
${odir}/${sim}.r1.fq \
${odir}/${sim}.r2.fq \
adapters.fa \
hairpins.fa \
> ${odir}/${sim}.hbs_process.txt 2> ${odir}/${sim}.hbs_process-stderr.txt
/usr/bin/time \
hbs_mapper \
--keep-temp 1 \
-q ${ref%.fa} \
${odir}/${sim}.r1.fq.processed \
${odir}/${sim}.r2.fq.processed \
> ${odir}/${sim}.hbs_mapper.txt 2> ${odir}/${sim}.hbs_mapper-stderr.txt
# Don't really need this; can get stats from hbs_mapper's *.report.txt
#/usr/bin/time \
#hbs_methylation_extractor \
# --merge_non_CpG \
# ${odir}/${sim}.r1.fq.processed.recovered.sam \
# > ${odir}/${sim}.hbs_methylation_extractor.txt 2> ${odir}/${sim}.hbs_methylation_extractor-stderr.txt
#mv CpG_context_${sim}.r1.fq.processed.recovered.sam.txt ${odir}/
#mv Non_CpG_context_${sim}.r1.fq.processed.recovered.sam.txt ${odir}/
# check for empty file
[ -s ${odir}/${sim}.r1.fq.processed.ori ] || return
if /bin/false; then
fq2html.py \
--latex \
--ori ${odir}/${sim}.r1.fq.processed.ori \
${odir}/${sim}.r1.fq \
${odir}/${sim}.r2.fq \
> ${odir}/${sim}.HBS.tex
pdflatex \
--output-directory ${odir} \
${odir}/${sim}.HBS.tex
fi
# map with bwa-mem, to match pp5mC
bwa mem \
-t 2 \
$ref \
${odir}/${sim}.r1.fq.processed.ori \
> ${odir}/${sim}.hbs.bwa.sam
}
export -f do_hbs
do_pp5mC() {
sim=$1
/usr/bin/time \
foldreads \
-1 ${odir}/${sim}.r1.fq \
-2 ${odir}/${sim}.r2.fq \
-m ${odir}/${sim}.metrics \
-u ${odir}/${sim}.unfolded \
> ${odir}/${sim}.folded.fq 2> ${odir}/${sim}.foldreads-stderr.txt
# check for empty file
[ -s ${odir}/${sim}.folded.fq ] || return
if /bin/false; then
fq2html.py \
--latex \
${odir}/${sim}.folded.fq \
> ${odir}/${sim}.folded.tex
pdflatex \
--output-directory ${odir} \
${odir}/${sim}.folded.tex
fq2html.py \
--latex \
${odir}/${sim}.unfolded_r1.fq \
${odir}/${sim}.unfolded_r2.fq \
> ${odir}/${sim}.unfolded.tex
pdflatex \
--output-directory ${odir} \
${odir}/${sim}.unfolded.tex
fi
bwa mem \
-C \
-t 2 \
$ref \
${odir}/${sim}.folded.fq \
| samtools view \
-Sbu - \
| samtools sort \
-O bam \
-@ 2 \
-m 1G \
-T tmp.sort.$sim \
- \
> ${odir}/${sim}.folded.bwa.bam
samtools index ${odir}/${sim}.folded.bwa.bam
samtools view ${odir}/${sim}.folded.bwa.bam > ${odir}/${sim}.folded.bwa.sam
# Map using bowtie1, with the same parameters used by HBStools.
# Actually, hbs_mapper uses `-k 2', and then excludes reads for
# which two equally good alignments exist. In bwa, MAPQ filtering
# achieves this goal, but bowtie does not set MAPQ appropriately.
# So we use `-k 1' here (single best alignment reported only), and
# avoid MAPQ filtering in bwa for comparison purposes.
bowtie \
-q -n 2 -l 28 -k 1 --best --chunkmbs 512 \
--sam --no-unal \
-p 2 \
${ref%.fa} \
${odir}/${sim}.folded.fq \
> ${odir}/${sim}.folded.bt1.sam
# check for empty file
[ -s ${odir}/${sim}.folded.bwa.sam ] || return
/usr/bin/time \
mark5mC \
${odir}/${sim}.folded.bwa.bam \
$ref \
> ${odir}/${sim}.folded.bwa.methlist.txt 2> ${odir}/${sim}.mark5mC-stderr.txt
scanbp \
${odir}/${sim}.folded.bwa.bam \
> ${odir}/${sim}.folded.pairs.txt
plot_nt_pairing.py \
--title $sim \
${odir}/${sim}.folded.pairs.txt \
${odir}/${sim}.pairs.pdf
}
export -f do_pp5mC
for sim in $sims; do
echo do_hbs $sim
echo do_pp5mC $sim
done | parallel -j 8
# Print stats for comparison.
for sim in $sims; do
# molecules recovered
mrhbs=$(awk '/Recovered .* original sequences/ {print 100*$2/'$nseqs'}' ${odir}/${sim}.hbs_mapper-stderr.txt)
mrfold=$(awk '/^NF/ {print 100*$2/'$nseqs'}' ${odir}/${sim}.metrics)
# correctly mapped (BWA)
if [ -s ${odir}/${sim}.hbs.bwa.sam ]; then
cm1hbs=$(checkseq2.py ${odir}/${sim}.r1.fq ${odir}/${sim}.hbs.bwa.sam | awk '{print 100*$3/'$nseqs'}')
else
cm1hbs=0
fi
if [ -s ${odir}/${sim}.folded.bwa.sam ]; then
cm1fold=$(checkseq2.py ${odir}/${sim}.r1.fq ${odir}/${sim}.folded.bwa.sam | awk '{print 100*$3/'$nseqs'}')
else
cm1fold=0
fi
# correctly mapped (Bowtie1)
if [ -s ${odir}/${sim}.r1.fq.processed.recovered.sam ]; then
cm2hbs=$(checkseq2.py ${odir}/${sim}.r1.fq ${odir}/${sim}.r1.fq.processed.recovered.sam | awk '{print 100*$3/'$nseqs'}')
else
cm2hbs=0
fi
if [ -s ${odir}/${sim}.folded.bt1.sam ]; then
cm2fold=$(checkseq2.py ${odir}/${sim}.r1.fq ${odir}/${sim}.folded.bt1.sam | awk '{print 100*$3/'$nseqs'}')
else
cm2fold=0
fi
printf "%s\t& %.3f & %.3f & %.3f & %.3f & %.3f & %.3f\\\\\\\\ \n" \
$sim $mrhbs $mrfold $cm1hbs $cm1fold $cm2hbs $cm2fold
done
echo
for sim in $sims; do
# correct bases
if [ -s ${odir}/${sim}.r1.fq.processed.ori ]; then
cbhbs=$(checkseq.py ${odir}/${sim}.r1.fq ${odir}/${sim}.r1.fq.processed.ori | awk '{print 100*$3}')
else
cbhbs=0
fi
if [ -s ${odir}/${sim}.folded.fq ]; then
cbfold=$(checkseq.py ${odir}/${sim}.r1.fq ${odir}/${sim}.folded.fq | awk '{print 100*$3}')
else
cbfold=0
fi
# error rates for mC vs C
# (all CpG's were methylated to start with, all other C's were unmethylated)
if [ -s ${odir}/${sim}.folded.bwa.methlist.txt ]; then
fpfold=$(awk ' $8~/^CG/ {cg_C+=$6; cg_mC+=$7}
$8~/^C[CAT]/ {ch_C+=$6; ch_mC+=$7}
END {
if (cg_mC) a=cg_C/cg_mC ; else a=0;
if (ch_C) b=ch_mC/ch_C ; else b=0;
print a, b
}' ${odir}/${sim}.folded.bwa.methlist.txt)
fpfold1=$(echo $fpfold | awk '{print 100*$1}')
fpfold2=$(echo $fpfold | awk '{print 100*$2}')
else
fpfold1=0
fpfold2=0
fi
if [ -s ${odir}/${sim}.r1.fq.processed.recovered.sam.report.txt ]; then
fphbs=$(awk ' / methylated CpG/ {cg_mC=$6}
/ methylated CH/ {ch_mC+=$6}
/ unmethylated CpG/ {cg_C=$6}
/ unmethylated CH/ {ch_C+=$6}
END {
if (cg_mC) a=cg_C/cg_mC ; else a=0;
if (ch_C) b=ch_mC/ch_C ; else b=0;
print a, b
}' ${odir}/${sim}.r1.fq.processed.recovered.sam.report.txt)
fphbs1=$(echo $fphbs | awk '{print 100*$1}')
fphbs2=$(echo $fphbs | awk '{print 100*$2}')
else
fphbs1=0
fphbs2=0
fi
printf "%s\t& %.3f & %.3f & %.3f & %.3f & %.3f & %.3f\\\\\\\\ \n" \
$sim $cbhbs $cbfold $fphbs1 $fpfold1 $fphbs2 $fpfold2
done