-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.OracleMetabarcoding.sh
More file actions
290 lines (245 loc) · 8.29 KB
/
Copy path5.OracleMetabarcoding.sh
File metadata and controls
290 lines (245 loc) · 8.29 KB
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
#!/bin/bash -
### We want to use Fred's metabarcoding pipeline to obtain OTU table from FASTQ files (with swarm)
#cd $HOME/Documents/Marilyne/PhD_Thesis/SAMA_12_first_10k_reads/Metabarcoding
## Check quality encoding (33 or 64?) : Our data are ARN 16S metadata
cd $HOME/Documents/marilyne/Metabarcoding/
# Test if vsearch is available on your computer
VSEARCH=$(which vsearch) && \
echo "vsearch is installed!" || \
{ echo "Error: vsearch is not installed" ; exit 1 ; }
# Check quality encoding (33 or 64?)
for f in *fastq.gz ; do
"${VSEARCH}" \
--fastq_chars ${f} 2> ${f/fastq.gz/log}
done
## Now we know how quality is encoded (offset of 33), we can merge paired-reads (R1 and R2)
# Merge read pairs
VSEARCH=$(which vsearch)
THREADS=4
ENCODING=33
for f in *R1_001.fastq.gz ; do
FORWARD=$f
REVERSE=${f/R1/R2}
OUTPUT=${f/_*/_assembled.fastq}
"${VSEARCH}" \
--threads ${THREADS} \
--fastq_mergepairs ${FORWARD} \
--reverse ${REVERSE} \
--fastq_ascii ${ENCODING} \
--fastqout ${OUTPUT} \
--fastq_allowmergestagger \
--quiet 2> ${OUTPUT/.fastq/.log}
done
#cd $HOME/Documents/Marilyne/PhD_Thesis/SAMA_12_first_10k_reads/Metabarcoding
cd $HOME/Documents/marilyne/Metabarcoding/
## primer clipping, sample dereplication and quality extraction
#set -x
# Define binaries, temporary files and output files
MIN_LENGTH=32
CUTADAPT="$(which cutadapt) --discard-untrimmed --minimum-length ${MIN_LENGTH}"
VSEARCH=$(which vsearch)
INPUT_REVCOMP=$(mktemp)
TMP_FASTQ=$(mktemp)
TMP_FASTQ1=$(mktemp)
TMP_FASTQ2=$(mktemp)
TMP_FASTA=$(mktemp)
OUTPUT=$(mktemp)
PRIMER_F="CCTACGGGNGGCWGCAG"
ANTI_PRIMER_R="GGATTAGATACCCBDGTAGTC"
#PRIMER_R="GACTACHVGGGTATCTAATCC"
for INPUT in *_assembled.fastq ; do
MIN_F=$(( ${#PRIMER_F} * 2 / 3 )) # primer match is >= 2/3 of primer length
MIN_R=$(( ${#ANTI_PRIMER_R} * 2 / 3 ))
QUALITY_FILE="${INPUT/.fastq/.qual}"
FINAL_FASTA="${INPUT/_assembled.fastq/.fas}"
LOG="${INPUT/_*/.log}"
LOG1="${INPUT/_*/_final.log}"
# # Reverse complement fastq file
"${VSEARCH}" --quiet \
--fastx_revcomp "${INPUT}" \
--fastqout "${INPUT_REVCOMP}"
# Trim forward & reverse primers (search normal and antisens)
cat "$INPUT" "${INPUT_REVCOMP}" | \
${CUTADAPT} -g "${PRIMER_F}" -O "${MIN_F}" -o "${TMP_FASTQ1}" - > "${LOG}"
cat "${TMP_FASTQ1}" | \
${CUTADAPT} -a "${ANTI_PRIMER_R}" -O "${MIN_R}" -o "${TMP_FASTQ}" - >> "${LOG}"
#cat "${LOG}"
# Discard sequences containing Ns, add expected error rates
"${VSEARCH}" \
--quiet \
--fastq_filter "${TMP_FASTQ}" \
--fastq_maxns 0 \
--relabel_sha1 \
--eeout \
--log /dev/stdout \
--fastqout "${TMP_FASTQ2}" > "${LOG1}"
#cat "${LOG1}"
# Discard sequences containing Ns, convert to fasta
"${VSEARCH}" \
--quiet \
--fastq_filter "${TMP_FASTQ}" \
--fastq_maxns 0 \
--fastaout "${TMP_FASTA}" >> "${LOG1}"
# Dereplicate at the study level
"${VSEARCH}" \
--quiet \
--derep_fulllength "${TMP_FASTA}" \
--sizeout \
--fasta_width 0 \
--relabel_sha1 \
--output "${FINAL_FASTA}" >> "${LOG1}"
# cat "${LOG1}"
#Discard quality lines, extract hash, expected error rates and read length
sed 'n;n;N;d' "${TMP_FASTQ2}" | \
awk 'BEGIN {FS = "[;=]"}
{if (/^@/) {printf "%s\t%s\t", $1, $3} else {print length($1)}}' | \
tr -d "@" > "${OUTPUT}"
cat "${OUTPUT}"
# Produce the final quality file
sort -k3,3n -k1,1d -k2,2n "${OUTPUT}" | \
uniq --check-chars=40 > "${QUALITY_FILE}"
done
# Clean
rm -f "${INPUT_REVCOMP}" "${TMP_FASTQ}" "${TMP_FASTA}" "${TMP_FASTQ2}" "${OUTPUT}"
#cd $HOME/Documents/Marilyne/PhD_Thesis/SAMA_12_first_10k_reads/Metabarcoding
## Global dereplication, clustering and chimera detection
cd $HOME/Documents/marilyne/Metabarcoding/
VSEARCH=$(which vsearch)
SWARM=$(which swarm)
TMP_FASTA=$(mktemp --tmpdir=".")
FINAL_FASTA="combined_samples.fas"
# Pool sequences
#cat *.fas > "${TMP_FASTA}"
# Dereplicate (vsearch)
cat *.fas | \
"${VSEARCH}" \
--derep_fulllength - \
--sizein \
--sizeout \
--fasta_width 0 \
--output "${FINAL_FASTA}" > /dev/null
# "${VSEARCH}" --derep_fulllength "${TMP_FASTA}" \
# --sizein \
# --sizeout \
# --fasta_width 0 \
# --output "${FINAL_FASTA}" > /dev/null
#rm -f "${TMP_FASTA}"
#cd $HOME/Documents/Marilyne/PhD_Thesis/SAMA_12_first_10k_reads/Metabarcoding
cd $HOME/Documents/marilyne/Metabarcoding/
# Clustering
SWARM=$(which swarm) #
VSEARCH=$(which vsearch) #
THREADS=16
TMP_REPRESENTATIVES=$(mktemp --tmpdir=".")
FINAL_FASTA="combined_samples.fas"
"${SWARM}" \
-d 1 --fastidious -y 16 -b 3 -t ${THREADS} -z \
-i ${FINAL_FASTA/.fas/_1f.struct} \
-s ${FINAL_FASTA/.fas/_1f.stats} \
-w ${TMP_REPRESENTATIVES} \
-o ${FINAL_FASTA/.fas/_1f.swarms} < ${FINAL_FASTA}
# Sort representatives
"${VSEARCH}" --fasta_width 0 \
--sortbysize ${TMP_REPRESENTATIVES} \
--output ${FINAL_FASTA/.fas/_1f_representatives.fas}
rm ${TMP_REPRESENTATIVES}
# Chimera checking
REPRESENTATIVES=${FINAL_FASTA/.fas/_1f_representatives.fas}
UCHIME=${REPRESENTATIVES/.fas/.uchime}
"${VSEARCH}" --uchime_denovo "${REPRESENTATIVES}" \
--uchimeout "${UCHIME}"
# Define variables, temporary files and output files
cd $HOME/Documents/marilyne/Metabarcoding/
RELEASE=132
URL="https://www.arb-silva.de/fileadmin/silva_databases/release_${RELEASE}/Exports"
INPUT="SILVA_${RELEASE}_SSURef_Nr99_tax_silva.fasta.gz"
PRIMER_F_NAME="341F_785R"
OUTPUT="${INPUT/.fasta.gz/}_${PRIMER_F_NAME}.fas"
LOG="${OUTPUT/.fas/.log}"
PRIMER_F="CCTACGGGNGGCWGCAG"
PRIMER_R="GGATTAGATACCCBDGTAGTC"
MIN_LENGTH=32
MIN_F=$(( ${#PRIMER_F} * 2 / 3 ))
MIN_R=$(( ${#PRIMER_R} * 2 / 3 ))
CUTADAPT="cutadapt --discard-untrimmed --minimum-length ${MIN_LENGTH}"
# Download
[[ -f "${INPUT}" ]] || wget \
--no-clobber \
--continue \
"${URL}/${INPUT}" "${URL}/${INPUT}.md5"
# Check
md5sum -c "${INPUT}.md5"
# Trim forward & reverse primers
zcat "${INPUT}" | sed '/^>/ ! s/U/T/g' | \
${CUTADAPT} -g "${PRIMER_F}" -O "${MIN_F}" - 2> "${LOG}" | \
${CUTADAPT} -a "${PRIMER_R}" -O "${MIN_F}" - 2>> "${LOG}" | \
sed '/^>/ s/;/|/g ; /^>/ s/ /_/g ; /^>/ s/_/ /1' > "${OUTPUT}"
# Stats
grep -c "^>" "${OUTPUT}"
set -x
cd $HOME/Documents/marilyne/Metabarcoding/
FINAL_FASTA="combined_samples_1f_representatives.fas"
DATABASE="SILVA_132_SSURef_Nr99_tax_silva_341F_785R.fas"
FILTER=2
THREADS=2
VSEARCH=$(which vsearch)
set -x "${VSEARCH}" \
--fastx_filter "${FINAL_FASTA}" \
--minsize "${FILTER}" \
--quiet \
--fastaout - | \
set -x "${VSEARCH}" \
--usearch_global - \
--threads ${THREADS} \
--dbmask none \
--qmask none \
--rowlen 0 \
--notrunclabels \
--userfields query+id1+target \
--maxaccepts 0 \
--maxrejects 32 \
--top_hits_only \
--output_no_hits \
--db ${DATABASE} \
--id 0.5 \
--iddef 1 \
--userout - > hits.representatives
set -x
# in case of multi-best hit, find the last-common ancestor
cd $HOME/Documents/marilyne/Metabarcoding/
python3 ./stampa_merge.py $(pwd)
# sort by decreasing abundance
cd $HOME/Documents/marilyne/Metabarcoding/
RESULTS="tailhitsrepresentatives.txt"
sort -k2,2nr -k1,1d results.representatives > "${RESULTS}"
# clean
rm {hits,results}.representatives
# Build the OTU table
set -x
cd $HOME/Documents/marilyne/Metabarcoding/
FASTA="combined_samples.fas"
SCRIPT="OTU_contingency_table.py"
STATS="combined_samples_1f.stats"
SWARMS="combined_samples_1f.swarms"
REPRESENTATIVES="combined_samples_1f_representatives.fas"
UCHIME="combined_samples_1f_representatives.uchime"
ASSIGNMENTS="results.representatives"
QUALITY="combined_samples.qual"
OTU_TABLE="${FASTA/.fas/.OTU.table}"
python \
"${SCRIPT}" \
"${REPRESENTATIVES}" \
"${STATS}" \
"${SWARMS}" \
"${UCHIME}" \
"${QUALITY}" \
"${ASSIGNMENTS}" \
OR-*.fas [T]-*.fas > "${OTU_TABLE}"
# Filter the OTU table
cd $HOME/Documents/marilyne/Metabarcoding/
TABLE="combined_samples.OTU.table"
FILTERED="${TABLE/.table/.filtered.table}"
head -n 1 "${TABLE}" > "${FILTERED}"
awk '$7 == "N" && $9 <= 0.0002 && ($2 >= 3 || $8 >= 2)' "${TABLE}" >> "${FILTERED}"
#cat "${TABLE}"| awk '$7 == "N" && $9 <= 0.0002 && ($2 >= 3 || $8 >= 2)' >> "${FILTERED}"
exit 0