-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacepai.sh
executable file
·316 lines (238 loc) · 8.82 KB
/
facepai.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
#!/usr/bin/env bash
## FACEPAI 1.0
## by Emma Wahlberg, 2019
## FACEPAI will take eDNA sequences in demultiplexed FASTQ-format and pool several
## replicates, filter, merge reads, dereplicate, cluster and finally perform
## taxonomic identification. FACEPAI is constructed to be fast and to be
## used with minimum effort and hands on time. Results in table format can
## thereafter be manually controlled, checked and validated. For detailed
## information and instructions, consult the readme file.
##
## Usage: FACEPAI SAMPLE_NAME FORWARD_IDENTIFIER REVERSE_IDENTIFIER
## SAMPLE_NAME = the name of the sample.
## FORWARD_IDENTIFIER = string that identifies the file or
## files containing the forward reads in FASTQ format,
## this may be one or multiple files, as long as the identifier
## string is the same.
## REVERSE_IDENTIFIER = same as above but for reverse reads.
## -h Print this text.
##
## Preferences for primer sequences, minminum length, reference database,
## and header variables are set in the file "options.config".
## Software prerequisites: fastp, vsearch, cutadapt, swarm, blastn,
## and makdeblastdb (see readme for versions and details).
# Function for spinner to keep user from becoming bored.
show_spinner()
{
local -r pid="${1}"
local -r delay='0.75'
local spinstr='\|/-'
local temp
while ps a | awk '{print $1}' | grep -q "${pid}"; do
temp="${spinstr#?}"
printf " [%c] " "${spinstr}"
spinstr=${temp}${spinstr%"${temp}"}
sleep "${delay}"
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
# Display help if requested.
if [ "$1" == '-h' ]
then
abspath=$(cd ${0%/*} && echo $PWD/${0##*/})
grep '^\##.*$' $abspath
exit 1;
fi
# Read options file
ABSPATH=$(readlink -f $0)
ABSDIR=$(dirname $ABSPATH)
. "$ABSDIR/options.config"
# Define variables
VSEARCH=$(which vsearch)
THREADS=4
ENCODING=33
INPUT=$1
INPUTENDINGF=$2
INPUTENDINGR=$3
INPUTS=1
OUTPUT=${INPUT}
REPFILEENDINGF="_pooled_F.fastq"
REPFILEENDINGR="_pooled_R.fastq"
POOLEDF="$INPUT$REPFILEENDINGF"
POOLEDR="$INPUT$REPFILEENDINGR"
FILTEREDENDINGF="_pooled_F_filtered.fastq"
FILTEREDENDINGR="_pooled_R_filtered.fastq"
FILTEREDNAMEF="$INPUT$FILTEREDENDINGF"
FILTEREDNAMER="$INPUT$FILTEREDENDINGR"
BLASTENDING="_BLAST_results.tab"
BLASTRESULTS="$INPUT$BLASTENDING"
# Setup log file
LOGFILENAME="${INPUT}_analysis.log"
exec &> >(tee -a "$LOGFILENAME")
# Log time
echo -en "\nAnalysis started at:\n"
date +"Date: %d/%m/%Y Time: %H:%M:%S"
# Check for required dependencies
echo -en "\nChecking dependencies... "
for name in fastp vsearch cutadapt swarm blastn
do
[[ $(which $name 2>/dev/null) ]] || { echo -en "\n$name needs to be installed.";deps=1; }
done
[[ $deps -ne 1 ]] && echo -en "OK\n" || { echo -en "\nInstall the above and rerun this script.\n";exit 1; }
# Check input
echo -en "\nChecking input... "
if [ -z "$INPUT" ]; then
echo -en "\nA sample name needs to be provided.";INPUTS=$((INPUTS+1));
fi
if [ -z "$INPUTENDINGF" ]; then
echo -en "\nFile ending of forward reads needs to be provided.";INPUTS=$((INPUTS+1));
fi
if [ -z "$INPUTENDINGR" ]; then
echo -en "\nFile ending of reverse reads needs to be provided.";INPUTS=$((INPUTS+1));
fi
if [ $INPUTS -ne 1 ]; then
echo -en "\nCheck above inputs and rerun this script.\nFor help, run script with argument -h or consult the documentation.\n";exit 1;
else
echo -en "OK\n";
fi
# Merge, filter and pool samples
read -p $'\n'$"Merging reads, filtering and pooling samples..."$'\n' -t 1
RAW_FILES=*${INPUTENDINGF}
for f in $RAW_FILES
do
FORWARDREAD=$f
REVERSEREAD=${f/$INPUTENDINGF/$INPUTENDINGR}
REPMERGED=${f/$INPUTENDINGF/_merged.fastq}
read -p $'\n'$"Processing ${FORWARDREAD} and ${REVERSEREAD}."$'\n' -t 0.5
read -p $'\n'$""$'\n' -t 0.5
fastp -m -u 100 -q 15 -c -L --overlap_len_require 10 --overlap_diff_percent_limit 40 -i "${FORWARDREAD}" -I "${REVERSEREAD}" --merged_out "${REPMERGED}"
done
cat *_merged.fastq > "${INPUT}"
# Remove primers
read -p $'\n'$"Primers and minimum length defined:"$'\n' -t 0.5
echo Forward primer: ${PRIMER_F}
echo Reverse complemented primer: ${PRIMER_R}
echo Minimum length: ${MIN_LENGTH}
read -p $'\n'$"Removing primers..."$'\n' -t 1
MIN_F=$(( ${#PRIMER_F} * 2 / 3 ))
MIN_R=$(( ${#PRIMER_R} * 2 / 3 ))
# Define binaries, temporary files and output files
CUTADAPT="$(which cutadapt) --discard-untrimmed --minimum-length ${MIN_LENGTH}"
VSEARCH=$(which vsearch)
INPUT_REVCOMP=$(mktemp)
TMP_FASTQ=$(mktemp)
TMP_FASTQ2=$(mktemp)
TMP_FASTA=$(mktemp)
OUTPUT=$(mktemp)
QUALITY_FILE="${INPUT/.fastq/.qual}"
# Reverse complement fastq file
"${VSEARCH}" --quiet \
--fastx_revcomp "${INPUT}" \
--fastqout "${INPUT_REVCOMP}"
LOG="${INPUT}.log"
FINAL_FASTA="${INPUT}.fas"
# Trim forward & reverse primers (search normal and antisens)
cat "${INPUT}" "${INPUT_REVCOMP}" | \
${CUTADAPT} -g "${PRIMER_F}" -O "${MIN_F}" - 2>> "${LOG}" | \
${CUTADAPT} -a "${PRIMER_R}" -O "${MIN_R}" - 2>> "${LOG}" > "${TMP_FASTQ}"
read -p $'\n'$"Additional filtering using VSEARCH..."$'\n' -t 0.5
read -p $'\n'$"Discarding sequences with N:s, converting to FASTA and dereplicate."$'\n' -t 1
# Discard sequences containing Ns, add expected error rates
"${VSEARCH}" \
--quiet \
--fastq_filter "${TMP_FASTQ}" \
--fastq_maxns 0 \
--relabel_sha1 \
--eeout \
--fastqout "${TMP_FASTQ2}" 2>> "${LOG}"
# Discard sequences containing Ns, convert to fasta
"${VSEARCH}" \
--quiet \
--fastq_filter "${TMP_FASTQ}" \
--fastq_maxns 0 \
--fastaout "${TMP_FASTA}" 2>> "${LOG}"
# Dereplicate at the study level
"${VSEARCH}" \
--quiet \
--derep_fulllength "${TMP_FASTA}" \
--minuniquesize 10 \
--sizeout \
--fasta_width 0 \
--relabel_sha1 \
--output "${FINAL_FASTA}" 2>> "${LOG}"
# 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}"
# Produce the final quality file
read -p $'\n'$"Cleaning up..."$'\n' -t 1
sort -k3,3n -k1,1d -k2,2n "${OUTPUT}" | \
uniq --check-chars=40 > "${QUALITY_FILE}"
# Clean
rm -f "${INPUT_REVCOMP}" "${TMP_FASTQ}" "${TMP_FASTA}" "${TMP_FASTQ2}" "${OUTPUT}"
read -p $'\n'$"Finished preparing files."$'\n' -t 0.5
read -p $'\n'$"Moving on to swarming and BLASTing..."$'\n' -t 1
SWARM=$(which swarm)
TMP_FASTA=$(mktemp --tmpdir=".")
FE=".fasta"
FINAL_FASTA="$INPUT$FE"
# Read sequences
cat *.fas > "${TMP_FASTA}"
# Dereplicate (vsearch)
read -p $'\n'$"Dereplicating..."$'\n' -t 1
"${VSEARCH}" --derep_fulllength "${TMP_FASTA}" \
--sizein \
--sizeout \
--fasta_width 0 \
--output "${FINAL_FASTA}" > /dev/null
rm -f "${TMP_FASTA}"
# Clustering
read -p $'\n'$"Clustering..."$'\n' -t 1
THREADS=8
TMP_REPRESENTATIVES=$(mktemp --tmpdir=".")
"${SWARM}" \
-d 1 -f -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
read -p $'\n'$"Sorting representatives..."$'\n' -t 1
"${VSEARCH}" --fasta_width 0 \
--sortbysize ${TMP_REPRESENTATIVES} \
--output ${FINAL_FASTA/.fas/_1f_representatives.fas}
rm ${TMP_REPRESENTATIVES}
# Chimera checking
read -p $'\n'$"Checking for chimeras..."$'\n' -t 1
REPRESENTATIVES=${FINAL_FASTA/.fas/_1f_representatives.fas}
UCHIME=${REPRESENTATIVES/.fas/.uchime}
"${VSEARCH}" --uchime_denovo "${REPRESENTATIVES}" \
--uchimeout "${UCHIME}"
# Perform local BLAST
read -p $'\n'$"Completed swarming and chimera checking. Prepare for BLAST!"$'\n' -t 0.5
echo -en "\nBLAST started at:\n"
date +"Date: %d/%m/%Y Time: %H:%M:%S"
read -p $'\n'$"Be aware, this may take some time..."$'\n' -t 0.5
TMP_BLAST=$(mktemp)
TMP_BLAST2=$(mktemp)
TMP_BLAST3=$(mktemp)
TMP_BLAST4=$(mktemp)
TMP_BLAST5=$(mktemp)
sed -i 's/^\([^_]*\(_[^_]*\)\{2\}\).*/\1/' *_1f_representatives.fasta
while true;do show_spinner;sleep 1;done &
LC_CTYPE=C && LANG=C ionice -c2 -n7 blastn -query *_1f_representatives.fasta -db "${BLASTDB}" -max_target_seqs 10 -outfmt '6 qseqid pident evalue qcovs sseqid' > "${TMP_BLAST}"
kill $!; trap 'kill $!' SIGTERM
read -p $'\n'$"BLAST complete, preparing results..."$'\n' -t 1
tr '|' \\t < "${TMP_BLAST}" > "${TMP_BLAST2}"
tr '_' ' ' < "${TMP_BLAST2}" > "${TMP_BLAST3}"
awk -vRS=";size=" -vORS="\t" '1' "${TMP_BLAST3}" > "${TMP_BLAST4}"
tr -d ';' < "${TMP_BLAST4}" > "${TMP_BLAST5}"
echo -e "${TABHEADER//,/\\t}" | cat - "${TMP_BLAST5}" > "${BLASTRESULTS}"
rm -f "${TMP_BLAST}" "${TMP_BLAST2}" "${TMP_BLAST3}" "${TMP_BLAST4}" "${TMP_BLAST5}" "${TMP_DB}"
# Log time
echo -en "\nAnalysis finished at:\n"
date +"Date: %d/%m/%Y Time: %H:%M:%S"
read -p $'\n'$"Results complete. Carry on, captain!"$'\n' -t 1