TITLE: Plasmid Sequence Assembly from Long Reads
AUTHORS: David Eccles

[DESCRIPTION]
<div class = "text-blocks"><div class = "text-block">This protocol demonstrates how to assemble reads from plasmid DNA, and generate a circularised and non-repetitive consensus sequence</div><div class = "text-block">At the moment, this protocol uses Canu to de-novo assemble high-quality single-cut reads.</div><div class = "text-block"><span style = "font-weight:bold;">Input(s)</span><span>: </span></div><div class = "text-block"><ul style = "list-style-type:disc;"><li style = "counter-reset:ol0;list-style-type:disc;"><span>demultiplexed fastq files (see protocol </span><a href="https://dx.doi.org/10.17504/protocols.io.7vmhn46" style = "text-decoration:underline;color:blue;cursor:pointer;"><span style = ":;">Demultiplexing Nanopore reads with LAST</span></a><span>). I've noticed that the default demultiplexing carried out by Guppy (at least up to v4.2.2, as used in the first version of this protocol) has issues with </span><a href="https://doi.org/10.12688/f1000research.11547.2" style = "text-decoration:underline;color:blue;cursor:pointer;"><span style = ":;">chimeric reads</span></a><span>, which can affect assembly.</span></li><li style = "counter-reset:ol0;list-style-type:disc;"></li><li style = "counter-reset:ol0;list-style-type:disc;"><span style = "font-weight:bold;">Output(s):</span></li><li style = "counter-reset:ol0;list-style-type:disc;">Consensus sequence per barcode as a fasta file</li></ul></div><div class = "text-block"><span style = "font-weight:bold;">Output(s):</span></div><div class = "text-block"><ul style = "list-style-type:disc;"></ul></div></div>

[STEPS]
?. [Read file preparation]
Demultiplex reads as per protocol Demultiplexing Nanopore reads with LAST.If this has been done, then the following command should produce output without errors:for bc in $(awk '{print $2}' barcode_counts.txt);  do ls demultiplexed/reads_${bc}.fq.gz;doneExample output:demultiplexed/reads_BC02.fq.gzdemultiplexed/reads_BC03.fq.gzdemultiplexed/reads_BC04.fq.gzdemultiplexed/reads_BC05.fq.gzdemultiplexed/reads_BC07.fq.gzdemultiplexed/reads_BC09.fq.gzIf the barcode_counts.txt file is missing, the output will look like this:awk: fatal: cannot open file `barcode_counts.txt' for reading (No such file or directory)If one or more of the barcode-demultiplexed files are missing, the output will look something like this:demultiplexed/reads_BC02.fq.gzdemultiplexed/reads_BC03.fq.gzdemultiplexed/reads_BC04.fq.gzls: cannot access 'demultiplexed/reads_BC05.fq.gz': No such file or directoryls: cannot access 'demultiplexed/reads_BC07.fq.gz': No such file or directorydemultiplexed/reads_BC09.fq.gz
?. [Read file preparation]
Create a directory to store results filesmkdir results
?. [Read file preparation]
Determine the N50/L50 read length for each barcode. This will be used as the initial guess at the assembly size.(for bc in $(awk '{print $2}' barcode_counts.txt);    do echo -n ${bc};    fastx-length.pl demultiplexed/reads_${bc}.fq.gz 2>&1 > /dev/null | \      grep L50 | awk '{print "\t"$5$6}' | perl -pe 's/b$//'; done) > results/read_L50.txtThis file can be viewed to confirm the assembly lengths:cat results/read_L50.txtExample output:BC02    347BC03    8.904kBC04    8.888kBC05    10.262kBC07    11.076kBC09    11.093kNote: this file will be used for subsequent downstream processing. If any of these barcodes shouldn't be processed further, feel free to remove the corresponding lines from this file
?. [Read filitering]
Filter out any reads that are less than half of the target read length, and determine the average quality of the remainder, keeping information on (at most) 200 of the highest-quality reads:cat results/read_L50.txt | while read bc len;  do echo ${bc} ${len};  ~/scripts/fastx-compstats.pl demultiplexed/reads_${bc}.fq.gz | \    sort -t ',' -k 16rn,16 | \    awk -F ',' -v "len=${len}" \      'BEGIN{if(match(len, "k") > 0){sub("k","",len); len=len*1000}}       {if($18 > (len / 2)){print}}' | \    head -n 200 | grep -v '^name' > results/bestLong_200X_${bc}.csv;doneCheck how successful the sequencer was at getting 200X coverage by counting lines:wc -l results/bestLong_200X_*.csvExample output. In this case BC02 has fewer than 200 reads, so the assembly is less likely to be high-quality:  90 results/bestLong_200X_BC02.csv 200 results/bestLong_200X_BC03.csv 200 results/bestLong_200X_BC04.csv 200 results/bestLong_200X_BC05.csv 200 results/bestLong_200X_BC07.csv 200 results/bestLong_200X_BC09.csv1090 total
?. [Read filitering]
Subset the original read sets to only include the high-quality long reads:cat results/read_L50.txt | while read bc len;  do echo ${bc} ${len};  ~/scripts/fastx-fetch.pl -i results/bestLong_200X_${bc}.csv \    demultiplexed/reads_${bc}.fq.gz > results/bestLong_200X_${bc}.fastq; done
?. [Canu Assembly]
Run a Canu assembly on each read set, using default options:cat results/read_L50.txt | while read bc len;  do canu -nanopore results/bestLong_200X_${bc}.fastq \    -p ${bc} -d results/canu_${bc} genomeSize=${len};done
?. [Canu Assembly]
Trim the assembled contigs based on Canu's trim recommendations:cat results/read_L50.txt | while read bc len;  do echo ${bc} ${len};  samtools faidx results/canu_${bc}/${bc}.contigs.fasta \    $(grep '^>' results/canu_${bc}/${bc}.contigs.fasta | \      perl -pe 's/^>(.*?) .*trim=(.*)/$1:$2/') > results/circTrimmed_${bc}.fasta;done