-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_preprocessing.sh
361 lines (280 loc) · 15.2 KB
/
01_preprocessing.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
indir=$1
# 1>&2 (redireciona STDOUT para STDERR)
# 2>&1 (redireciona STDERR para STDOUT)
# SE ${indir} NÃO EXISTE, SE NÃO FOI PASSADO ARGUMENTO NA LINHA DE COMANDO
if [ ! ${indir} ]; then
echo "[ERROR] Missing input directory." 1>&2
exit
fi
# SE ${indir} NÃO É DIRETÓRIO
if [ ! -d ${indir} ]; then
echo "[ERROR] Wrong input directory (${indir})." 1>&2
exit
fi
outdir=$2
# SE ${outdir} NÃO EXISTE, SE NÃO FOI PASSADO ARGUMENTO NA LINHA DE COMANDO
if [ ! ${outdir} ]; then
echo "[ERROR] Missing output directory." 1>&2
exit
fi
# SE ${outdir} NÃO É DIRETÓRIO
if [ ! -d ${outdir} ]; then
echo "[ERROR] Wrong output directory (${outdir})." 1>&2
exit
fi
# Número de CORES para o processamento
# ATENÇÃO: Não exceder o limite da máquina
THREADS=$3
if [ ! ${THREADS} ]; then
echo "[ERROR] Missing number of threads." 1>&2
exit
fi
# Contaminantes
contaminants=$4
mkdir -p ${outdir}/processed/cleaned
if [ ${contaminants} ]; then
if [ ! -e ${contaminants} ]; then
echo "[ERROR] Wrong contaminants file (${contaminants})." 1>&2
exit
fi
mkdir -p ${outdir}/processed/cleaned/refs
curdir=`pwd`
contaminants_abs_path=`readlink -f ${contaminants}`
cd ${outdir}/processed/cleaned/refs
if [ ! -e "./contaminants.fa" ]; then
ln -s ${contaminants_abs_path} contaminants.fa
if [ ! -e "./contaminants.1.bt2" ] ||
[ ! -s "./contaminants.1.bt2" ]; then
echo -e "Creating bowtie2 index for contaminants (${contaminants}) ...\n"
bowtie2-build --threads ${THREADS} \
contaminants.fa contaminants > bowtie2-build.log.out.txt 2> bowtie2-build.log.err.txt
fi
fi
cd ${curdir}
fi
# Criar estrutura de diretórios
mkdir -p ${outdir}/processed/fastqc/pre
mkdir -p ${outdir}/processed/atropos
mkdir -p ${outdir}/processed/prinseq
mkdir -p ${outdir}/processed/fastqc/pos
# É IMPORTANTE R1 E R2 TENHAM EXATAMENTE O MESMO NOME EXCETO ESTA PARTE: "1" ou "2" depois do "_R"
for r1 in `ls ${indir}/*_R1.fastq.gz`; do
# R2 (caso sequenciamento paired-end)
r2=`echo ${r1} | sed 's/_R1.fastq.gz/_R2.fastq.gz/'`
# Este teste irá verificar a existência de R2
if [ ! -e ${r2} ]; then
echo "[ERROR] Not found mate for R1 (${r2})." 1>&2
fi
# OBTÉM O NOME DA BIBLIOTECA EXCETO _R1.fastq
name=`basename ${r1} | sed 's/_R1.fastq.gz//'`
echo -e "Processing library ${name} ..."
if [ ! -e "${outdir}/processed/fastqc/pre/${name}_R1_fastqc.html" ] ||
[ ! -s "${outdir}/processed/fastqc/pre/${name}_R1_fastqc.html" ]; then
echo -e "\tFastQC evaluation using sample ${name}: ${r1} ...\n"
fastqc -t ${THREADS} \
${r1} \
-o ${outdir}/processed/fastqc/pre/ \
1> ${outdir}/processed/fastqc/pre/${name}_R1.log.out.txt \
2> ${outdir}/processed/fastqc/pre/${name}_R1.log.err.txt
fi
if [ ! -e "${outdir}/processed/fastqc/pre/${name}_R2_fastqc.html" ] ||
[ ! -s "${outdir}/processed/fastqc/pre/${name}_R2_fastqc.html" ]; then
echo -e "\tFastQC evaluation using sample ${name}: ${r2} ...\n"
fastqc -t ${THREADS} \
${r2} \
-o ${outdir}/processed/fastqc/pre/ \
1> ${outdir}/processed/fastqc/pre/${name}_R2.log.out.txt \
2> ${outdir}/processed/fastqc/pre/${name}_R2.log.err.txt
fi
if [ ! -e "${outdir}/processed/atropos/${name}_R1.atropos_final.fastq" ] ||
[ ! -s "${outdir}/processed/atropos/${name}_R1.atropos_final.fastq" ]; then
echo -e "\tRunning atropos (insert) for adapter trimming using sample ${name}: ${r1} & ${r2} ...\n"
# este trecho de ativação do ambiente "atropos" usando o pyenv pode ser modificado
# para adaptar ao sistema instalado no servidor
# no servidor hammer utilizamos o "pyenv"
# necessário inicializar o "pyenv" dentro desta sessão
#eval "$(pyenv init -)"
# ativando ambiente Python para execução do atropos
#pyenv activate atropos
# i7: CTGTCTCTTATACACATCTCCGAGCCCACGAGACNNNNNNNNNNATCTCGTATGCCGTCTTCTGCTTG
# i5: CTGTCTCTTATACACATCTGACGCTGCCGACGANNNNNNNNNNGTGTAGATCTCGGTGGTCGCCGTATCATT
# *: barcode bases (index)
# A execução no modo insert só faz sentido usando sequenciamento paired-end
atropos trim --aligner insert \
--error-rate 0.1 \
--times 2 \
--minimum-length 1 \
--op-order GAWCQ \
--match-read-wildcards \
--overlap 3 \
--quality-cutoff 10 \
--threads ${THREADS} \
--correct-mismatches conservative \
--pair-filter any \
-a CTGTCTCTTATACACATCTCCGAGCCCACGAGACNNNNNNNNNNATCTCGTATGCCGTCTTCTGCTTG \
-A CTGTCTCTTATACACATCTGACGCTGCCGACGANNNNNNNNNNGTGTAGATCTCGGTGGTCGCCGTATCATT \
-o ${outdir}/processed/atropos/${name}_R1.atropos_insert.fastq \
-p ${outdir}/processed/atropos/${name}_R2.atropos_insert.fastq \
-pe1 ${r1} \
-pe2 ${r2} \
--untrimmed-output ${outdir}/processed/atropos/${name}_R1.atropos_untrimmed.fastq \
--untrimmed-paired-output ${outdir}/processed/atropos/${name}_R2.atropos_untrimmed.fastq \
> ${outdir}/processed/atropos/${name}.atropos.log.out.txt \
2> ${outdir}/processed/atropos/${name}.atropos.log.err.txt
echo -e "\tRunning atropos (adapter) for adapter trimming using sample ${name}: ${outdir}/processed/atropos/${name}_R1.atropos_untrimmed.fastq & ${outdir}/processed/atropos/${name}_R2.atropos_untrimmed.fastq ...\n"
atropos trim --aligner adapter \
-e 0.1 \
-n 2 \
-m 1 \
--match-read-wildcards \
-O 10 \
-q 5 \
--pair-filter any \
-pe1 ${outdir}/processed/atropos/${name}_R1.atropos_untrimmed.fastq \
-pe2 ${outdir}/processed/atropos/${name}_R2.atropos_untrimmed.fastq \
-a CTGTCTCTTATACACATCTCCGAGCCCACGAGACNNNNNNNNNNATCTCGTATGCCGTCTTCTGCTTG \
-g AATGATACGGCGACCACCGAGATCTACACNNNNNNNNNNTCGTCGGCAGCGTCAGATGTGTATAAGAGACAG \
-A CTGTCTCTTATACACATCTGACGCTGCCGACGANNNNNNNNNNGTGTAGATCTCGGTGGTCGCCGTATCATT \
-G CAAGCAGAAGACGGCATACGAGATNNNNNNNNNNGTCTCGTGGGCTCGGAGATGTGTATAAGAGACAG \
-T ${THREADS} \
-o ${outdir}/processed/atropos/${name}_R1.atropos_adapter.fastq \
-p ${outdir}/processed/atropos/${name}_R2.atropos_adapter.fastq \
> ${outdir}/processed/atropos/${name}.atropos_adapter.log.out.txt \
2> ${outdir}/processed/atropos/${name}.atropos_adapter.log.err.txt
echo -e "\tMerging atropos adapter trimming results using sample ${name}: ${outdir}/processed/atropos/${name}_R1.atropos_insert.fastq and ${outdir}/processed/atropos/${name}_R2.atropos_insert.fastq + ${outdir}/processed/atropos/${name}_R1.atropos_adapter.fastq and ${outdir}/processed/atropos/${name}_R2.atropos_adapter.fastq ...\n"
cat ${outdir}/processed/atropos/${name}_R1.atropos_insert.fastq \
${outdir}/processed/atropos/${name}_R1.atropos_adapter.fastq \
> ${outdir}/processed/atropos/${name}_R1.atropos_final.fastq
cat ${outdir}/processed/atropos/${name}_R2.atropos_insert.fastq \
${outdir}/processed/atropos/${name}_R2.atropos_adapter.fastq \
> ${outdir}/processed/atropos/${name}_R2.atropos_final.fastq
echo -e "\tRemoving useless atropos results ...\n"
rm -f ${outdir}/processed/atropos/${name}_R1.atropos_insert.fastq \
${outdir}/processed/atropos/${name}_R1.atropos_adapter.fastq \
${outdir}/processed/atropos/${name}_R1.atropos_untrimmed.fastq \
${outdir}/processed/atropos/${name}_R2.atropos_insert.fastq \
${outdir}/processed/atropos/${name}_R2.atropos_adapter.fastq \
${outdir}/processed/atropos/${name}_R2.atropos_untrimmed.fastq
fi
if [ ! -e "${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1.fastq" ] ||
[ ! -s "${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1.fastq" ] ; then
echo -e "\tPrinSeq processing: ${outdir}/processed/atropos/${name}_R1.atropos_final.fastq & ${outdir}/processed/atropos/${name}_R2.atropos_final.fastq ...\n"
prinseq-lite.pl -fastq ${outdir}/processed/atropos/${name}_R1.atropos_final.fastq \
-fastq2 ${outdir}/processed/atropos/${name}_R2.atropos_final.fastq \
-out_format 3 \
-trim_qual_window 3 \
-trim_qual_step 1 \
-trim_qual_type mean \
-trim_qual_rule lt \
-trim_qual_right 20 \
-out_good ${outdir}/processed/prinseq/${name}.atropos_final.prinseq \
-out_bad null \
-lc_method dust \
-lc_threshold 50 \
-min_len 15 \
-ns_max_p 80 \
> ${outdir}/processed/prinseq/${name}.atropos_final.prinseq.out.log \
2> ${outdir}/processed/prinseq/${name}.atropos_final.prinseq.err.log
fi
if [ ! -e "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_fastqc.zip" ] ||
[ ! -s "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_fastqc.zip" ] ; then
echo -e "\tFastQC pos-evaluation using sample ${name}: ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1.fastq\n"
fastqc -t ${THREADS} \
${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1.fastq \
-o ${outdir}/processed/fastqc/pos/ \
> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1.log.out.txt \
2> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1.log.err.txt
fi
if [ ! -e "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_2_fastqc.zip" ] ||
[ ! -s "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_2_fastqc.zip" ] ; then
echo -e "\tFastQC pos-evaluation using sample ${name}: ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2.fastq\n"
fastqc -t ${THREADS} \
${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2.fastq \
-o ${outdir}/processed/fastqc/pos/ \
> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_2.log.out.txt \
2> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_2.log.err.txt
fi
# Remover (sempre) para garantir que não serão acrescentadas reads
rm -f ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_singletons.fastq
# SE EXISTIR <SAMPLE_NAME>.atropos_final.prinseq_1_singletons.fastq
if [ -e "${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1_singletons.fastq" ]; then
if [ ! -e "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_singletons_fastqc.zip" ] ||
[ ! -s "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_singletons_fastqc.zip" ]; then
fastqc -t ${THREADS} \
${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1_singletons.fastq \
-o ${outdir}/processed/fastqc/pos/ \
> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_singletons.log.out.txt \
2> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_singletons.log.err.txt
fi
# concatena se existe "*_1_singletons.fastq"
cat ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1_singletons.fastq > ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_singletons.fastq
fi
# SE EXISTIR <SAMPLE_NAME>.atropos_final.prinseq_2_singletons.fastq
if [ -e "${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2_singletons.fastq" ]; then
if [ ! -e "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_singletons_fastqc.zip" ] ||
[ ! -s "${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_1_singletons_fastqc.zip" ]; then
fastqc -t ${THREADS} \
${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2_singletons.fastq \
-o ${outdir}/processed/fastqc/pos/ \
> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_2_singletons.log.out.txt \
2> ${outdir}/processed/fastqc/pos/${name}.atropos_final.prinseq_2_singletons.log.err.txt
fi
# concatena se existe "*_2_singletons.fastq"
cat ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2_singletons.fastq >> ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_singletons.fastq
fi
if [ ! -e "${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1_singletons.fastq" ]; then
touch ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1_singletons.fastq
fi
if [ ! -e "${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2_singletons.fastq" ]; then
touch ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2_singletons.fastq
fi
if [ ${contaminants} ]; then
if [ ! -e "${outdir}/processed/cleaned/${name}.bowtie2.log.out.txt" ]; then
bowtie2 -x ${outdir}/processed/cleaned/refs/contaminants \
-1 ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1.fastq \
-2 ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2.fastq \
-U ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_singletons.fastq \
1> ${outdir}/processed/cleaned/${name}.bowtie2.log.out.txt \
2> ${outdir}/processed/cleaned/${name}.bowtie2.log.err.txt
cat ${outdir}/processed/cleaned/${name}.bowtie2.log.out.txt | samtools view -F 4 - | cut -f 1 | sed 's/\/[12]$//' | sort | uniq | perl -lane 'print "$_/1"; print "$_/2"; print "$_";' > ${outdir}/processed/cleaned/${name}_exclude.txt
rm -f ${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_1.fastq \
${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_2.fastq \
${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_1_singletons.fastq \
${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_2_singletons.fastq
pullseq -i ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1.fastq \
-n ${outdir}/processed/cleaned/${name}_exclude.txt -e \
> ${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_1.fastq
pullseq -i ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2.fastq \
-n ${outdir}/processed/cleaned/${name}_exclude.txt -e \
> ${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_2.fastq
pullseq -i ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1_singletons.fastq \
-n ${outdir}/processed/cleaned/${name}_exclude.txt -e \
> ${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_1_singletons.fastq
pullseq -i ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2_singletons.fastq \
-n ${outdir}/processed/cleaned/${name}_exclude.txt -e \
> ${outdir}/processed/cleaned/${name}.atropos_final.prinseq.cleaned_2_singletons.fastq
fi
else
curdir=`pwd`
r1_cleaned=`readlink -f ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1.fastq`
r2_cleaned=`readlink -f ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2.fastq`
s1_cleaned=`readlink -f ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_1_singletons.fastq`
s2_cleaned=`readlink -f ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_2_singletons.fastq`
cd ${outdir}/processed/cleaned
rm -f ./${name}_exclude.txt \
./${name}.bowtie2.log.out.txt \
./${name}.bowtie2.log.err.txt \
./${name}.atropos_final.prinseq.cleaned_1.fastq \
./${name}.atropos_final.prinseq.cleaned_2.fastq \
./${name}.atropos_final.prinseq.cleaned_1_singletons.fastq \
./${name}.atropos_final.prinseq.cleaned_2_singletons.fastq
ln -s ${r1_cleaned} ${name}.atropos_final.prinseq.cleaned_1.fastq
ln -s ${r2_cleaned} ${name}.atropos_final.prinseq.cleaned_2.fastq
ln -s ${s1_cleaned} ${name}.atropos_final.prinseq.cleaned_1_singletons.fastq
ln -s ${s2_cleaned} ${name}.atropos_final.prinseq.cleaned_2_singletons.fastq
cd ${curdir}
# Remover singletons concatenados (não utilizados fora do bowtie2)
rm -f ${outdir}/processed/prinseq/${name}.atropos_final.prinseq_singletons.fastq
fi
done