forked from CMU-SAFARI/AirLift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_pipeline.sh
308 lines (267 loc) · 12.6 KB
/
run_pipeline.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
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
#!/bin/bash
SRCFOLDER=$1
#ref folders containing ref file:
OLDREF=$2
NEWREF=$3
SEQ_FILE_EXT=$4
READSIZE=$5
#make sure the bam files are indexed (i.e., samtools index)
READ_BAM=$6
FIRST_PAIR=$7
SECOND_PAIR=$8
OUTPUT=$9
THREAD=${10}
function generate_chain(){
bash "${SRCFOLDER}/1-generate_chain/chain_install.sh" "${OUTPUT}"
bash "${SRCFOLDER}/1-generate_chain/sample_run.sh" \
"${OLDREF}" \
"${NEWREF}" \
"${OUTPUT}/" \
"${SRCFOLDER}/1-generate_chain/chain_generate.sh" \
${SEQ_FILE_EXT} \
"${OUTPUT}/"
}
function generate_gaps(){
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`; \
/usr/bin/time -v -p -o "${OUTPUT}/${chr}_extracted_gaps.fa.time" \
python3 "${SRCFOLDER}/2-generate_gaps/extract_gaps.py" \
$i \
"${NEWREF}/$chr.${SEQ_FILE_EXT}" \
${READSIZE} \
"${OUTPUT}/${chr}_extracted_gaps.fa"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`; \
/usr/bin/time -v -p -o "${OUTPUT}/${chr}_kmer_gaps.fasta.time" \
python3 "${SRCFOLDER}/2-generate_gaps/gaps_to_fasta.py" \
"${OUTPUT}/${chr}_extracted_gaps.fa" \
${READSIZE} \
"${OUTPUT}/${chr}_kmer_gaps.fasta" \
10
done
}
function align_gaps(){
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`; \
bash "${SRCFOLDER}/3-align_gaps/align_gaps.sh" \
"${OLDREF}/${chr}.${SEQ_FILE_EXT}" \
"${OUTPUT}/${chr}_kmer_gaps.fasta" \
${THREAD} \
"${OUTPUT}/${chr}_aligned_gaps"
done
}
function extract_reads(){
mkdir "${OUTPUT}/bedfiles"
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`; mkdir -p "${OUTPUT}/bedfiles/${chr}/"; \
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/extract_regions.time" \
bash "${SRCFOLDER}/4-extract_reads/extract_regions.sh" \
"${OUTPUT}/${chr}_aligned_gaps.bam" \
"${OUTPUT}/bedfiles/${chr}/"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
local num_of_files=`ls -l ${OUTPUT}/bedfiles/${chr}/*.bed | wc -l`
local sed_bed_files=$(echo ${OUTPUT}\/bedfiles\/${chr}/ | sed 's/\//\\\//g')
local sed_read_scripts=$(echo ${SRCFOLDER}\/4-extract_reads\/extract_reads.bash | sed 's/\//\\\//g')
local sed_read_bam=$(echo $READ_BAM | sed 's/\//\\\//g')
cat "${SRCFOLDER}/4-extract_reads/slurm_extract_reads.sh" | \
sed "s/{NUM_FILES}/$num_of_files/g" | \
sed "s/{THREAD}/$THREAD/g" | \
sed "s/{BED_FILES_FOLDER}/$sed_bed_files/g" | \
sed "s/{EXTRACT_READS_SCRIPT}/$sed_read_scripts/g" | \
sed "s/{READ_BAM_FILE}/$sed_read_bam/g" | \
sed "s/{READSIZE}/$READSIZE/g" > "${OUTPUT}/${chr}_samplejob.sh"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
sbatch --wait "${OUTPUT}/${chr}_samplejob.sh"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/merged_${chr}.bed.time" \
cat "${OUTPUT}/bedfiles/${chr}/"*.bed | sortBed -i - | mergeBed -i - > "${OUTPUT}/bedfiles/${chr}/merged_${chr}.bed"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
mkdir -p "${OUTPUT}/bedfiles/${chr}/retired_bed/"
mkdir -p "${OUTPUT}/bedfiles/${chr}/constant_bed/"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_regions.bed.time" \
python3 "${SRCFOLDER}/4-extract_reads/get_retired_regions.py" \
${READSIZE} \
"${OUTPUT}/bedfiles/${chr}/merged_${chr}.bed" \
$i \
"${OUTPUT}/bedfiles/${chr}/retired_bed/retired_regions.bed"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_reads.bed.time" \
bash "${SRCFOLDER}/4-extract_reads/extract_reads_noprune.sh" \
${READ_BAM} \
"${OUTPUT}/bedfiles/${chr}/retired_bed/retired_regions.bed" > "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_reads.bed"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
mkdir -p "${OUTPUT}/bedfiles/${chr}/reads/"
cat "${OUTPUT}/bedfiles/${chr}/"*.reads "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_reads.bed" > "${OUTPUT}/bedfiles/${chr}/updated_and_retired_reads.bed"
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/extract_sequences_full.time" \
bash "${SRCFOLDER}/4-extract_reads/extract_sequence.sh" \
${FIRST_PAIR} \
${SECOND_PAIR} \
"${OUTPUT}/bedfiles/${chr}/updated_and_retired_reads.bed" \
"${OUTPUT}/bedfiles/${chr}/reads/"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/reads/paired_reads.time" \
bwa mem -M -t $THREAD "${NEWREF}/$chr.${SEQ_FILE_EXT}" "${OUTPUT}/bedfiles/${chr}/reads/reads_1.fastq" "${OUTPUT}/bedfiles/${chr}/reads/reads_2.fastq" | \
samtools view -h -F4 | \
samtools sort -m 16g -l0 > "${OUTPUT}/bedfiles/${chr}/reads/paired_reads.bam"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/reads/singletons_reads.time" \
bwa mem -M -t $THREAD "${NEWREF}/$chr.${SEQ_FILE_EXT}" "${OUTPUT}/bedfiles/${chr}/reads/singletons.fastq" | \
samtools view -h -F4 | \
samtools sort -m 16g -l0 > "${OUTPUT}/bedfiles/${chr}/reads/singletons_reads.bam"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
bash "${SRCFOLDER}/0-align_reads.sh" \
"${NEWREF}/$chr.${SEQ_FILE_EXT}" \
"${OUTPUT}/bedfiles/${chr}/reads/reads" \
"${OUTPUT}/bedfiles/${chr}/reads/paired" \
$THREAD
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
bash "${SRCFOLDER}/0-align_singletons.sh" \
"${NEWREF}/$chr.${SEQ_FILE_EXT}" \
"${OUTPUT}/bedfiles/${chr}/reads/singletons.fastq" \
"${OUTPUT}/bedfiles/${chr}/reads/singletons" \
$THREAD
done
}
function extract_reads_with_parallel(){
mkdir "${OUTPUT}/bedfiles"
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`; mkdir -p "${OUTPUT}/bedfiles/${chr}/"; \
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/extract_regions.time" \
bash "${SRCFOLDER}/4-extract_reads/extract_regions.sh" \
"${OUTPUT}/${chr}_aligned_gaps.bam" \
"${OUTPUT}/bedfiles/${chr}/"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
local num_of_files=`ls -l ${OUTPUT}/bedfiles/${chr}/*.bed | wc -l`
local sed_bed_files=$(echo ${OUTPUT}\/bedfiles\/${chr}/ | sed 's/\//\\\//g')
local sed_read_scripts=$(echo ${SRCFOLDER}\/4-extract_reads\/extract_reads.bash | sed 's/\//\\\//g')
local sed_read_bam=$(echo $READ_BAM | sed 's/\//\\\//g')
cat "${SRCFOLDER}/4-extract_reads/parallel_extract_reads.sh" | \
sed "s/{NUM_FILES}/$num_of_files/g" | \
sed "s/{THREAD}/$THREAD/g" | \
sed "s/{BED_FILES_FOLDER}/$sed_bed_files/g" | \
sed "s/{EXTRACT_READS_SCRIPT}/$sed_read_scripts/g" | \
sed "s/{READ_BAM_FILE}/$sed_read_bam/g" | \
sed "s/{READSIZE}/$READSIZE/g" > "${OUTPUT}/${chr}_samplejob.sh"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
bash "${OUTPUT}/${chr}_samplejob.sh"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/merged_${chr}.bed.time" \
cat "${OUTPUT}/bedfiles/${chr}/"*.bed | sortBed -i - | mergeBed -i - > "${OUTPUT}/bedfiles/${chr}/merged_${chr}.bed"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
mkdir -p "${OUTPUT}/bedfiles/${chr}/retired_bed/"
mkdir -p "${OUTPUT}/bedfiles/${chr}/constant_bed/"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_regions.bed.time" \
python3 "${SRCFOLDER}/4-extract_reads/get_retired_regions.py" \
${READSIZE} \
"${OUTPUT}/bedfiles/${chr}/merged_${chr}.bed" \
$i \
"${OUTPUT}/bedfiles/${chr}/retired_bed/retired_regions.bed"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_reads.bed.time" \
bash "${SRCFOLDER}/4-extract_reads/extract_reads_noprune.sh" \
${READ_BAM} \
"${OUTPUT}/bedfiles/${chr}/retired_bed/retired_regions.bed" > "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_reads.bed"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
mkdir -p "${OUTPUT}/bedfiles/${chr}/reads/"
cat "${OUTPUT}/bedfiles/${chr}/"*.reads "${OUTPUT}/bedfiles/${chr}/retired_bed/retired_reads.bed" > "${OUTPUT}/bedfiles/${chr}/updated_and_retired_reads.bed"
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/extract_sequences_full.time" \
bash "${SRCFOLDER}/4-extract_reads/extract_sequence.sh" \
${FIRST_PAIR} \
${SECOND_PAIR} \
"${OUTPUT}/bedfiles/${chr}/updated_and_retired_reads.bed" \
"${OUTPUT}/bedfiles/${chr}/reads/"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/reads/paired_reads.time" \
bwa mem -M -t $THREAD "${NEWREF}/$chr.${SEQ_FILE_EXT}" "${OUTPUT}/bedfiles/${chr}/reads/reads_1.fastq" "${OUTPUT}/bedfiles/${chr}/reads/reads_2.fastq" | \
samtools view -h -F4 | \
samtools sort -m 16g -l0 > "${OUTPUT}/bedfiles/${chr}/reads/paired_reads.bam"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
/usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/reads/singletons_reads.time" \
bwa mem -M -t $THREAD "${NEWREF}/$chr.${SEQ_FILE_EXT}" "${OUTPUT}/bedfiles/${chr}/reads/singletons.fastq" | \
samtools view -h -F4 | \
samtools sort -m 16g -l0 > "${OUTPUT}/bedfiles/${chr}/reads/singletons_reads.bam"
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
bash "${SRCFOLDER}/0-align_reads.sh" \
"${NEWREF}/$chr.${SEQ_FILE_EXT}" \
"${OUTPUT}/bedfiles/${chr}/reads/reads" \
"${OUTPUT}/bedfiles/${chr}/reads/paired" \
$THREAD
done
for i in `echo ${OUTPUT}/*.chain`; do
local chr=`basename $i | sed s/.chain//`
bash "${SRCFOLDER}/0-align_singletons.sh" \
"${NEWREF}/$chr.${SEQ_FILE_EXT}" \
"${OUTPUT}/bedfiles/${chr}/reads/singletons.fastq" \
"${OUTPUT}/bedfiles/${chr}/reads/singletons" \
$THREAD
done
}
function main(){
mkdir -p "${OUTPUT}/"
local chain_existed=$(ls ${OUTPUT}/*.chain 2> /dev/null)
if [[ ${#chain_existed} -gt 0 ]]; then
echo "Line "${LINENO}": In function "${FUNCNAME}": Use user offered chain "${chain_existed}
else
echo "Line "${LINENO}": In function "${FUNCNAME}": User does not offer chain file. Generating one."
generate_chain
fi
generate_gaps
align_gaps
if [[ -z ${11} ]]; then
extract_reads_with_parallel
else
extract_reads
fi
}
if [[ "${#BASH_SOURCE[@]}" -eq 1 ]]
then
main "$@"
fi
#constant regions:
#for i in `echo ${OUTPUT}/*.chain`; do local chr=`basename $i | sed s/.chain//`; /usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/constant_bed/constant_regions.bed.time" python3 "${SRCFOLDER}/4-extract_reads/get_constant_regions.py" ${READSIZE} $i "${OUTPUT}/bedfiles/${chr}/constant_bed/constant_regions.bed"; done
#sbatch --local wrap="for i in `echo ${OUTPUT}/*.chain`; do local chr=`basename $i | sed s/.chain//`; /usr/bin/time -v -p -o "${OUTPUT}/bedfiles/${chr}/constant_bed/constant_reads.bed.time" bash "${SRCFOLDER}/4-extract_reads/extract_reads_noprune.sh" ${READ_BAM} "${OUTPUT}/bedfiles/${chr}/constant_bed/constant_regions.bed" > "${OUTPUT}/bedfiles/${chr}/constant_bed/constant_reads.bed"; done"