-
Notifications
You must be signed in to change notification settings - Fork 0
/
plastid-assembly.sh
442 lines (357 loc) · 15.9 KB
/
plastid-assembly.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env bash
#A script to assemble a chloroplast genome
#............................................................................
# How to use
# activate your conda environment with the tools needed
# bash plastid-assembly.sh -a adapters -b baits R1 R2 nano
#............................................................................
# Defaults
set -e #exit if a command exits with non zero status
script=$(basename $0) #script name less file path
adapters=""
baits=""
genome_size=160000
threads=16
#............................................................................
# Functions
function msg {
echo -e "$*"
}
# $* is args passed in, -e means interpret backslashes
function err {
echo "Error: $*" 1>&2
}
# redirects stdout to stderr
function banner {
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
}
function msg_banner {
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
msg "$*"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
}
function usage {
banner
msg "$script\n Assemble a chloroplast genome with long and short reads."
msg "Author:\n Anna Syme, [email protected]"
msg "Usage:\n $script [options] R1 R2 nano"
msg "Parameters:"
msg " Illumina R1 reads R1.fq.gz"
msg " Illumina R2 reads R2.fq.gz"
msg " Nanopore reads nano.fq.gz"
msg "Options:"
msg " -h Show this help"
msg " -t NUM Number of threads (default=16)"
msg " -g NUM genome size in bp (default=160000)"
msg " -a FILE illumina adapter sequences file name"
msg " -b FILE bait sequences file name"
msg "Example:"
msg " $script -t 8 R1.fq.gz R2.fq.gz minion.fq.gz"
banner
exit 1
#exits script
}
#...........................................................................
# Parse the command line options
#loops through, sets variable or runs function
#have to add in a colon after the flag, if it's looking for an arg
#e.g. t: means flag t is set, then look for the arg (eg 32) to set $threads to
# : at the start disables default error handling for this bit
#instead it will label an odd flag as a ?
#the extra : at the end means it will label missing args as :
while getopts ':ht:g:a:b::' opt ; do
case $opt in
h)
usage
;;
t)
threads=$OPTARG
;;
g)
genome_size=$OPTARG
;;
a)
adapters=$OPTARG
;;
b)
baits=$OPTARG
;;
\?)
echo "Invalid option '=$OPTARG'"
exit 1
;;
:)
echo "Option '-$OPTARG' requires an argument"
exit 1
;;
esac
done
shift $((OPTIND-1))
#remove all options that has been parsed by getopts
#so that $1 refers to next arg passed to script
#e.g. a positional arg
if [ $# -ne 3 ]; then
msg "\n **Please provide three input parameters** \n"
usage
fi
#if number of pos params is not 3, print usage msg
#...........................................................................
#start
msg "\n"
msg_banner "now running $script"
#...........................................................................
#check inputs
#give variable names to the inputs
R1_raw=$1
R2_raw=$2
nano_raw=$3
msg "This script will use:"
msg " Illumina reads R1: $R1_raw"
msg " Illumina reads R2: $R2_raw"
msg " Nanpore reads: $nano_raw"
msg " Bait sequences: $baits"
msg " Adapter sequences: $adapters"
msg " Genome size: $genome_size"
msg " Threads: $threads"
#...........................................................................
conda env export --name plastidenv > plastidenv.yml
#saves conda env with tools and versions
#...........................................................................
#these read files will be created during run
R1_fastp=R1_fastp.fq.gz
R2_fastp=R2_fastp.fq.gz
R1_cp=R1_cp.fq.gz
R2_cp=R2_cp.fq.gz
R1_cp_subset=R1_cp_subset.fq.gz
R2_cp_subset=R2_cp_subset.fq.gz
nano_cp=nano_cp.fq.gz
nano_cp_long=nano_cp_long.fq.gz
nano_cp2=nano_cp2.fq.gz
nano_cp_long2=nano_cp_long2.fq.gz
#...........................................................................
#these assemblies will be created during run
assembly_flye=assembly_flye.fasta
assembly_flye_racon1=assembly_flye_racon1.fasta
assembly_flye_racon2=assembly_flye_racon2.fasta
assembly_flye2=assembly_flye2.fasta
assembly_flye2_racon1=assembly_flye2_racon1.fasta
assembly_flye2_racon2=assembly_flye2_racon2.fasta
assembly_flye2_racon_pilon1=assembly_flye2_racon_pilon1.fasta
assembly_raven=assembly_raven.fasta
assembly_raven_pilon=assembly_raven_pilon.fasta
assembly_unicycler=assembly_unicycler.fasta
assembly_miniasm=assembly_miniasm.fasta
assembly_miniasm_minipolished=assembly_miniasm_minipolished.fasta
assembly_miniasm_minipolished_pilon1=assembly_miniasm_minipolished_pilon1.fasta
#...........................................................................
msg_banner "now extracting chloroplast nanopore reads from all reads - round 1"
minimap2 -a -x map-ont -t $threads $baits $nano_raw |
samtools fastq -0 $nano_cp -n -F 4 -
#map the reads to a reference set of chloroplast genes (baits)
#baits chosen are rbcL, matK and ndhF from Acacia ligulata
#this pipes the output to samtools fastq,
#which extracts the fastq reads from the alignment
#the flag -F 4 means exclude unmapped reads (i.e., non cp reads)
#...........................................................................
msg_banner "now keeping only the longest nanopore cp reads - round 1"
filtlong --length_weight 1 --mean_q_weight 0 --window_q_weight 0 \
--target_bases 40000000 $nano_cp | gzip > $nano_cp_long
##keeps set of longest reads to required cov (X200) based on genome size (160000)
##or cov (X250) = 40000000
##read score based on length only due to relative score weights
##alternative: filter by length and quality e.g.
##filtlong --target_bases 32000000 reads_nano_cp.fq.gz \
##| gzip > reads_nano_cp_filtered.fq.gz
#...........................................................................
msg_banner "now assembling nanopore cp reads - round 1"
flye --nano-raw $nano_cp_long --genome-size $genome_size \
--out-dir flye-out --threads $threads
#using uncorrected reads as read correction can lead to errors
#other flye options had little effect here - keep haplotpyes, meta, trestle
cp flye-out/assembly.fasta $assembly_flye
#...........................................................................
msg_banner "now polishing flye assembly with long reads - round 1"
#round 1. make overlaps file, then run racon
minimap2 -x map-ont -t $threads $assembly_flye $nano_cp_long \
| gzip > overlaps1.paf.gz
racon --threads $threads $nano_cp_long overlaps1.paf.gz \
$assembly_flye > $assembly_flye_racon1
#round 2
minimap2 -x map-ont -t $threads $assembly_flye_racon1 $nano_cp_long \
| gzip > overlaps2.paf.gz
racon --threads $threads $nano_cp_long overlaps2.paf.gz \
$assembly_flye_racon1 > $assembly_flye_racon2
#here, further rounds of polishing made little difference
#option to add in medaka polishing here
#...........................................................................
msg_banner "now extracting chloroplast nanopore reads from all reads - round 2"
#baits file is now the first polished assembly
#need to increase minimum match value
#otherwise too many reads are extracted and they don't assemble
minimap2 -m 5000 -a -x map-ont -t $threads $assembly_flye_racon2 $nano_raw |
samtools fastq -0 $nano_cp2 -n -F 4 -
#...........................................................................
msg_banner "now keeping only the longest nanopore cp reads - round 2"
filtlong --length_weight 1 --mean_q_weight 0 --window_q_weight 0 \
--target_bases 40000000 $nano_cp2 | gzip > $nano_cp_long2
#...........................................................................
msg_banner "now assembling nanopore cp reads - round 2"
flye --nano-raw $nano_cp_long2 --genome-size $genome_size \
--out-dir flye-out2 --threads $threads
cp flye-out2/assembly.fasta $assembly_flye2
#...........................................................................
msg_banner "now polishing flye assembly with long reads - round 2"
#round 1. make overlaps file, then run racon
minimap2 -x map-ont -t $threads $assembly_flye2 $nano_cp_long2 \
| gzip > overlaps1.paf.gz
racon --threads $threads $nano_cp_long2 overlaps1.paf.gz \
$assembly_flye2 > $assembly_flye2_racon1
#round 2
minimap2 -x map-ont -t $threads $assembly_flye2_racon1 $nano_cp_long2 \
| gzip > overlaps2.paf.gz
racon --threads $threads $nano_cp_long2 overlaps2.paf.gz \
$assembly_flye2_racon1 > $assembly_flye2_racon2
#...........................................................................
msg_banner "now trimming and filtering raw illumina reads"
fastp --in1 $R1_raw --out1 $R1_fastp --in2 $R2_raw --out2 $R2_fastp --verbose \
--adapter_fasta $adapters --n_base_limit 3 --length_required 130 \
--average_qual 35 --thread $threads
#filter illumina reads for quality and trim adapters
#--n_base_limit 3 - discard read/pair if > 3Ns
#--length_required 130
#--average_qual 35 - want very high qual for polishing
#...........................................................................
msg_banner "now extracting illumina chloroplast reads from all reads"
minimap2 -a -x sr $assembly_flye2_racon2 $R1_fastp $R2_fastp \
| samtools fastq -1 $R1_cp -2 $R2_cp -F 0x4 -f 0x2 -
#extract cp reads only by mapping to long-read assembly
#chose assembly made with longest nanopore reads
#needs end dash for stdin
#more info https://broadinstitute.github.io/picard/explain-flags.html
#samtools flags: -F4 exclude unmapped reads, -f2 include properly paired reads
#...........................................................................
msg_banner "now creating subset of illumina chloroplast reads"
rasusa -i $R1_cp -i $R2_cp --coverage 300 \
--genome-size $genome_size -o $R1_cp_subset -o $R2_cp_subset
#downsample to required coverage, x300
#...........................................................................
msg_banner "now polishing flye assembly with illumina"
#round 1pilon polish
bwa index $assembly_flye2_racon2
bwa mem -t $threads $assembly_flye2_racon2 $R1_cp_subset $R2_cp_subset \
| samtools sort > flye_aln1.bam
samtools index flye_aln1.bam
samtools faidx $assembly_flye2_racon2
pilon --genome $assembly_flye2_racon2 --frags flye_aln1.bam \
--output assembly_flye2_racon_pilon1 \
--fix bases --mindepth 0.5 --changes --threads $threads --verbose
#fix bases, not contig breaks in case that makes incorrect breaks
#round 2 pilon polish
#made no difference but left in as option
#bwa index $assembly_flye_racon_pilon1
#bwa mem -t $threads $assembly_flye_racon_pilon1 $R1_cp_subset $R2_cp_subset \
#| samtools sort > flye_aln2.bam
#samtools index flye_aln2.bam
#samtools faidx $assembly_flye_racon_pilon1
#pilon --genome $assembly_flye_racon_pilon1 --frags flye_aln2.bam \
#--output assembly_flye_racon_pilon2 \
#--fix bases --mindepth 0.5 --changes --threads $threads --verbose
#...........................................................................
msg_banner "now running raven assembler"
#note: this uses the same long and short reads as flye+racon+pilon
#long and short reads are not re-extracted.
#two rounds of racon polishing are included by default
raven --graphical-fragment-assembly raven.gfa -t $threads $nano_cp_long2 > $assembly_raven
#...........................................................................
msg_banner "now polishing raven assembler with pilon"
#round 1pilon polish
bwa index $assembly_raven
bwa mem -t $threads $assembly_raven $R1_cp_subset $R2_cp_subset \
| samtools sort > raven_aln1.bam
samtools index raven_aln1.bam
samtools faidx $assembly_raven
pilon --genome $assembly_raven --frags raven_aln1.bam \
--output assembly_raven_pilon \
--fix bases --mindepth 0.5 --changes --threads $threads --verbose
#...........................................................................
msg_banner "now running unicycler assembler"
unicycler -1 $R1_cp_subset -2 $R2_cp_subset -l $nano_cp_long2 -o unicycler \
--threads $threads --no_rotate --keep 2
#using fastp filtered illumina reads and longest nano cp reads
#--keep 2 will keep final files but also SAM
#--no_rotate means don't rotate replicons to certain start pos
cp unicycler/assembly.fasta $assembly_unicycler
#note: this uses the same reads as the flye but not raven assembly
#...........................................................................
msg_banner "now running miniasm assembler"
#map reads to themselves - miniasm needs this as input
minimap2 -x ava-ont $nano_cp_long2 $nano_cp_long2 | gzip -1 > reads_overlaps.paf.gz
#assemble with miniasm - needs reads and overlaps (the paf file)
miniasm -f $nano_cp_long2 reads_overlaps.paf.gz > miniasm.gfa
#convert gfa to fasta file of unitigs
awk '/^S/{print ">"$2"\n"$3}' miniasm.gfa > $assembly_miniasm
#...........................................................................
msg_banner "now polishing miniasm assembly with long reads"
#minipolish, uses racon and attempts to circularise
minipolish -t $threads $nano_cp_long2 miniasm.gfa > minipolished.gfa
awk '/^S/{print ">"$2"\n"$3}' minipolished.gfa > $assembly_miniasm_minipolished
#alternative: use racon polish only
#racon polish round 1
#minimap2 -x map-ont $assembly_miniasm $nano_cp_long | gzip > overlaps1.paf.gz
#racon --threads $threads $nano_cp_long overlaps1.paf.gz \
#$assembly_miniasm > $assembly_miniasm_racon1
#racon polish round 2
#minimap2 -x map-ont $assembly_miniasm_racon1 $nano_cp_long | gzip > overlaps2.paf.gz
#racon --threads $threads $nano_cp_long overlaps2.paf.gz \
#$assembly_miniasm_racon1 > $assembly_miniasm_racon2
#...........................................................................
msg_banner "now polishing miniasm assembly with short reads"
#pilon polish round 1
bwa index $assembly_miniasm_minipolished
bwa mem -t $threads $assembly_miniasm_minipolished $R1_cp_subset $R2_cp_subset \
| samtools sort > mini_pilon_aln1.bam
samtools index mini_pilon_aln1.bam
samtools faidx $assembly_miniasm_minipolished
pilon --genome $assembly_miniasm_minipolished --frags mini_pilon_aln1.bam \
--output assembly_miniasm_minipolished_pilon1 \
--fix bases --mindepth 0.5 --changes --threads $threads --verbose
#fix bases, not contig breaks in case that makes incorrect breaks
#pilon polish round 2
#option. made no difference here
#bwa index $assembly_miniasm_racon_pilon1
#bwa mem -t $threads $assembly_miniasm_racon_pilon1 $R1_cp_subset $R2_cp_subset \
#| samtools sort > mini_pilon_aln2.bam
#samtools index mini_pilon_aln2.bam
#samtools faidx $assembly_miniasm_racon_pilon1
#pilon --genome $assembly_miniasm_racon_pilon1 --frags mini_pilon_aln2.bam \
#--output assembly_miniasm_racon_pilon2 \
#--fix bases --mindepth 0.5 --changes --threads $threads --verbose
#...........................................................................
msg_banner "now mapping long reads to final flye assembly"
minimap2 -ax map-ont $assembly_flye2_racon_pilon1 $nano_cp_long2 \
| samtools sort -o longmapped.bam
#...........................................................................
msg_banner "now mapping short reads to final flye assembly"
bwa index $assembly_flye2_racon_pilon1
bwa mem -t $threads $assembly_flye2_racon_pilon1 $R1_cp_subset $R2_cp_subset \
| samtools sort -o shortmapped.bam
#...........................................................................
msg_banner "now calculating stats for reads and assemblies"
seqkit stats $nano_raw $nano_cp $nano_cp_long $nano_cp2 $nano_cp_long2 \
-Ta > nano_read_stats.tsv
seqkit stats $R1_raw $R2_raw $R1_fastp $R2_fastp $R1_cp $R2_cp \
$R1_cp_subset $R2_cp_subset \
-Ta > illumina_read_stats.tsv
seqkit stats \
$assembly_flye $assembly_flye_racon1 $assembly_flye_racon2 \
$assembly_flye2 $assembly_flye2_racon1 $assembly_flye2_racon2 \
$assembly_flye2_racon_pilon1 \
$assembly_raven $assembly_raven_pilon \
$assembly_unicycler \
$assembly_miniasm \
$assembly_miniasm_minipolished $assembly_miniasm_minipolished_pilon1 \
-Ta > assembly_stats.tsv
#option: canu assembler
#canu -p canu -d canu genomeSize=160000 -nanopore $reads
#...........................................................................
msg_banner "Script finished!"