pipeline_3.sh 9.72 KB
Newer Older
Gonzalo S Nido's avatar
Gonzalo S Nido committed
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash

# exit when any command fails
set -e
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo "\"${last_command}\" command filed with exit code $?"' EXIT

if [ "$1" == "" ]
then
    echo "Provide ID!!"
    exit 1
fi

wd="/data/bs"
id=$1
ref_dir="/data/ref"
fastq_dir="/data/bs/trimmed_fq"

# Programs
bismark="/usr/local/bin/bismark-0.22.3"
deduplicate_bismark="/usr/local/bin/deduplicate_bismark-0.22.3"
bismark_meth_extract="/usr/local/bin/bismark_methylation_extractor-0.22.3"
bismark_bedGraph="/usr/local/bin/bismark2bedGraph-0.22.3"
bowtie2="/usr/local/bin/bowtie2-2.4.41"
bowtie_path="/opt/bowtie2-2.4.41/"
samtools="/usr/local/bin/samtools-1.10"
samtools_path="/opt/samtools-1.10"
fetch_mt_reads="/data/bin/fetch_mt_and_mate/fetch_mt_and_mate.py"
ext_mt_srt="/data/bs/ext_mt_srt.py"
#samviewwithmate="/data/bin/jvarkit/dist/samviewwithmate.jar"
#methylKit_extract="~/scripts/extractMethylation.R"

# References
b37_ref=${ref_dir}/b37_decoy/
nuc_ref=${ref_dir}/b37_decoy_no_mt/
mt_ref=${ref_dir}/b37_decoy_only_mt/

# Output folders
bam_dir=${wd}/bamfiles_3
mkdir -p ${bam_dir}
meth_dir=${wd}/meth_3
mkdir -p ${meth_dir}
tmp_dir=${wd}/tmp_3
mkdir -p ${tmp_dir}
log_dir=${wd}/logs_3
mkdir -p ${log_dir}


# Get fastq files
r1_files=$( find ${fastq_dir} -name *${id}*R1.fastq.gz | paste -sd "," - )
r2_files=$( find ${fastq_dir} -name *${id}*R2*fastq.gz | paste -sd "," - )

echo "R1 FILES: ${r1_files}"
echo "R2 FILES: ${r2_files}"


echo "#### STRATEGY 3: Align vs nuclear DNA, collect unmapped and align them to mt DNA \
methylation"
 
echo "     3.1 Aligning vs nuclear DNA..."
${bismark} ${nuc_ref} -1 ${r1_files} -2 ${r2_files} --un --ambiguous --multicore 12 \
    -o ${bam_dir} --temp_dir ${tmp_dir} --path_to_bowtie ${bowtie_path} \
    --samtools_path ${samtools_path} > ${log_dir}/3_1_aln.stdout 2> ${log_dir}/3_1_aln.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.1"
    exit 1
fi

echo "     3.2 Merging unaligned and ambiguos reads..."
unaligned_list_r1=""
ambig_list_r1=""
unaligned_list_r2=""
ambig_list_r2=""
IFS=',' read -r -a array <<< "${r1_files}"
for element in "${array[@]}"; do
    uns=$( basename ${element} )
    uns="${bam_dir}/${uns}_unmapped_reads_1.fq.gz"
    unaligned_list_r1="${unaligned_list_r1} ${uns}"
    amb=$( basename ${element} )
    amb="${bam_dir}/${amb}_ambiguous_reads_1.fq.gz"
    ambig_list_r1="${ambig_list_r1} ${amb}"
done
IFS=',' read -r -a array <<< "${r2_files}"
for element in "${array[@]}"; do
    uns=$( basename ${element} )
    uns="${bam_dir}/${uns}_unmapped_reads_2.fq.gz"
    unaligned_list_r2="${unaligned_list_r2} ${uns}"
    amb=$( basename ${element} )
    amb="${bam_dir}/${amb}_ambiguous_reads_2.fq.gz"
    ambig_list_r2="${ambig_list_r2} ${amb}"
done

cat ${unaligned_list_r1} > ${bam_dir}/${id}_unmapped.R1.fastq.gz
cat ${ambig_list_r1} > ${bam_dir}/${id}_ambiguous.R1.fastq.gz
cat ${unaligned_list_r2} > ${bam_dir}/${id}_unmapped.R2.fastq.gz
cat ${ambig_list_r2} > ${bam_dir}/${id}_ambiguous.R2.fastq.gz

if [ -s "${bam_dir}/${id}_unmapped.R1.fastq.gz" ]; then
    rm ${unaligned_list_r1}
fi
if [ -s "${bam_dir}/${id}_unmapped.R2.fastq.gz" ]; then
    rm ${unaligned_list_r2}
fi
if [ -s "${bam_dir}/${id}_ambiguous.R1.fastq.gz" ]; then
    rm ${ambig_list_r1}
fi
if [ -s "${bam_dir}/${id}_ambiguous.R2.fastq.gz" ]; then
    rm ${ambig_list_r2}
fi


echo "     3.3 Merging bamfiles..."
bamfile_list=""
IFS=',' read -r -a array <<< "${r1_files}"
for element in "${array[@]}"; do
    bamfile=$( basename ${element} )
    bamfile="${bam_dir}/${bamfile%.fastq.gz}_bismark_bt2_pe.bam"
    bamfile_list="${bamfile_list} ${bamfile}"
done

${samtools} merge -n -r -@ 64 ${bam_dir}/${id}_nuc.bam ${bamfile_list} > \
    ${log_dir}/3_3_merge.stdout 2> \
    ${log_dir}/3_3_merge.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.3"
    exit 1
fi

if [ -s "${bam_dir}/${id}_nuc.bam" ]; then
    echo "         Removing lane bams..."
    rm ${bamfile_list}
fi


## Deduplication not really needed
## echo "     3.4 Dedupping alignment..."
## ${deduplicate_bismark} --paired --samtools_path ${samtools_path} \
##     --output_dir ${bam_dir} ${bam_dir}/${id}_nuc.bam > \
##     ${log_dir}/3_4_dedup.stdout 2> \
##     ${log_dir}/3_4_dedup.stderr
## if [ "$?" != "0" ]; then
##     echo "ERROR IN STEP 3.4"
##     exit 1
## fi
## 
## if [ -s "${bam_dir}/${id}_nuc.deduplicated.bam" ]; then
##     mv ${bam_dir}/${id}_nuc.deduplicated.bam ${bam_dir}/${id}_nuc.dedup.bam
## fi

echo "     3.4 Re-aligning unmapped reads to mtDNA..."
${bismark} ${mt_ref} -1 ${bam_dir}/${id}_unmapped.R1.fastq.gz \
    -2 ${bam_dir}/${id}_unmapped.R2.fastq.gz --multicore 12 \
    -o ${bam_dir} --temp_dir ${tmp_dir} --path_to_bowtie ${bowtie_path} \
    --samtools_path ${samtools_path} > \
    ${log_dir}/3_4_mt_map.stdout 2> \
    ${log_dir}/3_4_mt_map.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.4"
    exit 1
fi

mv ${bam_dir}/${id}_unmapped.R1_bismark_bt2_pe.bam ${bam_dir}/${id}_unmapped_to_mt.bam

echo "     3.5 Re-aligning unmapped reads to nuc+mt..."
${bismark} ${b37_ref} -1 ${bam_dir}/${id}_unmapped.R1.fastq.gz \
    -2 ${bam_dir}/${id}_unmapped.R2.fastq.gz --multicore 12 \
    -o ${bam_dir} --temp_dir ${tmp_dir} --path_to_bowtie ${bowtie_path} \
    --samtools_path ${samtools_path} > \
    ${log_dir}/3_5_b37_map.stdout 2> \
    ${log_dir}/3_5_b37_map.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.4"
    exit 1
fi

mv ${bam_dir}/${id}_unmapped.R1_bismark_bt2_pe.bam ${bam_dir}/${id}_unmapped_to_b37.bam

echo "     3.6 Dedupping alignments..."
${deduplicate_bismark} --paired --samtools_path ${samtools_path} \
    --output_dir ${bam_dir} ${bam_dir}/${id}_unmapped_to_mt.bam > \
    ${log_dir}/3_6_a_dedup.stdout 2> \
    ${log_dir}/3_6_a_dedup.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.6.a"
    exit 1
fi

if [ -s "${bam_dir}/${id}_unmapped_to_mt.deduplicated.bam" ]; then
    mv ${bam_dir}/${id}_unmapped_to_mt.deduplicated.bam ${bam_dir}/${id}_unmapped_to_mt.dedup.bam
fi

${deduplicate_bismark} --paired --samtools_path ${samtools_path} \
    --output_dir ${bam_dir} ${bam_dir}/${id}_unmapped_to_b37.bam > \
    ${log_dir}/3_6_b_dedup.stdout 2> \
    ${log_dir}/3_6_b_dedup.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.6.b"
    exit 1
fi

if [ -s "${bam_dir}/${id}_unmapped_to_b37.deduplicated.bam" ]; then
    mv ${bam_dir}/${id}_unmapped_to_b37.deduplicated.bam ${bam_dir}/${id}_unmapped_to_b37.dedup.bam
fi




echo "     3.7 Extracting methylation in MT..."
${bismark_meth_extract} -p --comprehensive --output ${meth_dir} \
    --gzip --multicore 16 \
    --samtools_path ${samtools_path} \
    ${bam_dir}/${id}_unmapped_to_mt.dedup.bam > \
    ${log_dir}/3_7_a_meth.stdout 2> \
    ${log_dir}/3_7_a_meth.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.7.a"
    exit 1
fi
${bismark_meth_extract} -p --comprehensive --output ${meth_dir} \
    --gzip --multicore 16 \
    --samtools_path ${samtools_path} \
    ${bam_dir}/${id}_unmapped_to_b37.dedup.bam > \
    ${log_dir}/3_7_a_meth.stdout 2> \
    ${log_dir}/3_7_b_meth.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.7.b"
    exit 1
fi

echo "     3.8 Subsetting MT reads from methylation table..."
pigz -dc ${meth_dir}/CpG_context_${id}_unmapped_to_b37.dedup.txt.gz | \
    python3 ${ext_mt_srt} | pigz -nc > \
    ${meth_dir}/CpG_context_${id}_unmapped_to_b37_MT.dedup.txt.gz
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.8"
    exit 1
fi
pigz -dc ${meth_dir}/CHH_context_${id}_unmapped_to_b37.dedup.txt.gz | \
    python3 ${ext_mt_srt} | pigz -nc > \
    ${meth_dir}/CHH_context_${id}_unmapped_to_b37_MT.dedup.txt.gz
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.8"
    exit 1
fi
pigz -dc ${meth_dir}/CHG_context_${id}_unmapped_to_b37.dedup.txt.gz | \
    python3 ${ext_mt_srt} | pigz -nc > \
    ${meth_dir}/CHG_context_${id}_unmapped_to_b37_MT.dedup.txt.gz
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.8"
    exit 1
fi

## PROBABLY WORTH REMOVING NON-MT FILES
#if [ -s "${meth_dir}/CpG_context_${id}_unmapped_to_b37_MT.dedup.bam" ]; then
#    rm ${meth_dir}/CpG_context_${id}_unmapped_to_b37.dedup.bam
#fi
#if [ -s "${meth_dir}/CHH_context_${id}_unmapped_to_b37_MT.dedup.bam" ]; then
#    rm ${meth_dir}/CHH_context_${id}_unmapped_to_b37.dedup.bam
#fi
#if [ -s "${meth_dir}/CHG_context_${id}_unmapped_to_b37_MT.dedup.bam" ]; then
#    rm ${meth_dir}/CHG_context_${id}_unmapped_to_b37.dedup.bam
#fi
#
#
#
echo "     3.9 Generating bedGraph for MT..."
${bismark_bedGraph} --output ${id}_MT.bedGraph \
    --dir ${meth_dir} --CX --buffer_size 100G \
    ${meth_dir}/CpG_context_${id}_unmapped_to_mt.dedup.txt.gz \
    ${meth_dir}/CHH_context_${id}_unmapped_to_mt.dedup.txt.gz \
    ${meth_dir}/CHG_context_${id}_unmapped_to_mt.dedup.txt.gz > \
    ${log_dir}/3_9_a_bedgraph.stdout 2> \
    ${log_dir}/3_9_a_bedgraph.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.9a"
    exit 1
fi

${bismark_bedGraph} --output ${id}_b37_then_MT.bedGraph \
    --dir ${meth_dir} --CX --buffer_size 100G \
    ${meth_dir}/CpG_context_${id}_unmapped_to_b37_MT.dedup.txt.gz \
    ${meth_dir}/CHH_context_${id}_unmapped_to_b37_MT.dedup.txt.gz \
    ${meth_dir}/CHG_context_${id}_unmapped_to_b37_MT.dedup.txt.gz > \
    ${log_dir}/3_9_b_bedgraph.stdout 2> \
    ${log_dir}/3_9_b_bedgraph.stderr
if [ "$?" != "0" ]; then
    echo "ERROR IN STEP 3.9b"
    exit 1
fi





#echo "     3.8 Fetching MT reads (and their pairs)..."
#python3 ${fetch_mt_reads} ${bam_dir}/${id}_unmapped_to_mt.dedup.bam | \
#    ${samtools} view -@ 16 -b -o ${bam_dir}/${id}_unmapped_to_mt_reads_and_mates.bam -
#if [ "$?" != "0" ]; then
#    echo "ERROR IN STEP 3.8.a"
#    exit 1
#fi
#python3 ${fetch_mt_reads} ${bam_dir}/${id}_unmapped_to_b37.dedup.bam | \
#    ${samtools} view -@ 16 -b -o ${bam_dir}/${id}_unmapped_to_b37_reads_and_mates.bam -
#if [ "$?" != "0" ]; then
#    echo "ERROR IN STEP 3.8.b"
#    exit 1
#fi