TITLE: Stranded Mapping from Oriented Long Reads
AUTHORS: David Eccles

[DESCRIPTION]
<div class = "text-blocks"><div class = "text-block">This protocol demonstrates how to map strand-oriented long reads to a genome, and visualise them in a genome browser.</div><div class = "text-block">The general idea is to use minimap2 to create stranded BAM files, which are split for forward/reverse orientation then converted into BigWig format for display in a genome browser.</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>stranded fastq files (see protocol </span><a href="https://dx.doi.org/10.17504/protocols.io.57hg9j6" style = "text-decoration:underline;color:blue;cursor:pointer;"><span style = ":;">Preparing Reads for Stranded Mapping</span></a><span style = ":;">)</span></li><li style = "counter-reset:ol0;list-style-type:disc;">a FASTA file containing the genome / sequence of interest.
 </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;">Genome-mapped stranded BAM files</li><li style = "counter-reset:ol0;list-style-type:disc;">Genome-mapped stranded BigWig files</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;"><li style = "counter-reset:ol0;list-style-type:disc;">Genome-mapped stranded BAM files</li><li style = "counter-reset:ol0;list-style-type:disc;">Genome-mapped stranded BigWig files</li></ul></div></div>

[STEPS]
?. [Orient Reads]
Orient reads as per protocol Preparing Reads for Stranded Mapping.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 oriented/${bc}_reads_dirAdjusted.fq.gz;doneExample output:oriented/BC03_reads_dirAdjusted.fq.gzoriented/BC04_reads_dirAdjusted.fq.gzoriented/BC05_reads_dirAdjusted.fq.gzoriented/BC06_reads_dirAdjusted.fq.gzoriented/BC07_reads_dirAdjusted.fq.gzoriented/BC08_reads_dirAdjusted.fq.gz
?. [Index Preparation]
Prepare genome index for spliced alignment
 minimap2 -d mmus_ucsc_all-splice.idx -Q -t 10 -x splice mmus_ucsc_all.fa
?. [Read Mapping]
Map the long reads to the genome using minimap2, using samtools to covert to a sorted BAM format. This is where the reverse complementing done during demultiplexing gives a big saving of effort. As this BAM file is one of the main outputs, the run name is added to the file name.runName="CHANGE_THIS";mkdir -p mapped;for bc in $(awk '{print $2}' barcode_counts.txt);  do echo ${bc};  minimap2 -t 10 -a -x splice mmus_ucsc_all-splice.idx oriented/${bc}_reads_dirAdjusted.fq.gz | \    samtools view -b | samtools sort > mapped/mm2_${runName}_called_${bc}_vs_MmusG.bam;done
?. [Creating BigWig Coverage Files]
A bedGraph of coverage is created using samtools mpileup and mpileupDC.pl, excluding any skipped intronic sequence. When 'mpileupDC.pl' is provided with a single file, it will output a bedGraph file with a header line starting with '##'; this header line is removed. To simplify output naming in later steps, the run name is added to the file name.The particular JBrowse plugin that I use for stranded display requires that the reverse strand have negative coverage values, so that file needs to be changed:runName="CHANGE_THIS";for bc in $(awk '{print $2}' barcode_counts.txt);  do echo ${bc};  samtools view -b -F 0x10 mapped/mm2_called_${bc}_vs_MmusG.bam | \    samtools mpileup -A -B -Q 0 -q 0 -I -q 0 -Q 0 - | \    mpileupDC.pl | tail -n +2 > mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.plus  samtools view -b -f 0x10 mapped/mm2_called_${bc}_vs_MmusG.bam | \    samtools mpileup -A -B -Q 0 -q 0 -I -q 0 -Q 0 - | \    mpileupDC.pl | tail -n +2 > mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.minus  perl -i -pe 's/([0-9]+)$/-$1/' mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.minusdone;As an alternative, BEDTools can be used to generate coverage. The default options for BEDTools treat sequence deletions (which happen frequently in nanopore reads) as a drop in coverage, which can make exon hunting and coverage calculation more difficult. I have proposed a fix to this via a command-line option "-ignoreD", which is available from my BEDTools fork:runName="CHANGE_THIS";for bc in $(awk '{print $2}' barcode_counts.txt);  do echo ${bc};  samtools view -b mapped/mm2_${runName}_called_${bc}_vs_MmusG.bam | \    bedtools genomecov -bga -strand '+' -split -ignoreD -ibam - > mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.plus  samtools view -b mapped/mm2_${runName}_called_${bc}_vs_MmusG.bam | \    bedtools genomecov -bga -strand '-' -split -ignoreD -ibam - > mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.minus  perl -i -pe 's/([0-9]+)$/-$1/' mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.minusdone;With the standard version of BEDTools, the "-ignoreD" parameter can be excluded in order to generate a similar BedGraph output, but with drops in coverage at deletion points:runName="CHANGE_THIS";for bc in $(awk '{print $2}' barcode_counts.txt);  do echo ${bc};  samtools view -b mapped/mm2_${runName}_called_${bc}_vs_MmusG.bam | \    bedtools genomecov -bga -strand '+' -split -ibam - > mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.plus  samtools view -b mapped/mm2_${runName}_called_${bc}_vs_MmusG.bam | \    bedtools genomecov -bga -strand '-' -split -ibam - > mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.minus  perl -i -pe 's/([0-9]+)$/-$1/' mapped/mm2_${runName}_called_${bc}_vs_MmusG.bg.minusdone;
?. [Creating BigWig Coverage Files]
Stranded bedgraph files are converted to bigwig. This requires BEDTools and a genome information file containing chromosome lengths (one for Mmus/mm10 is attached to this step). As this bigwig file is one of the main outputs, the run name is added to the file name.runName="CHANGE_THIS";for bc in $(awk '{print $2}' barcode_counts.txt);  do echo ${bc};  basename="mapped/mm2_${runName}_called_${bc}_vs_MmusG"  bedGraphToBigWig ${basename}.bg.plus Mmus_genome.chrInfo.txt ${basename}.bw.plus  bedGraphToBigWig ${basename}.bg.minus Mmus_genome.chrInfo.txt ${basename}.bw.minus done
?. [JBrowse Configuration]
Each track should have its own JBrowse configuration section using the StrangedBigWig class and StrandedXYPlot type. BAM tracks can also be added. An example is shown here:[tracks.bam-CG005_Nov18_BC03-track]# settings for what data is shown in the trackstoreClass     = JBrowse/Store/SeqFeature/BAMurlTemplate    = raw/mm2_called_CG005AB_BC03_vs_MmusG.bambaiUrlTemplate = raw/mm2_called_CG005AB_BC03_vs_MmusG.bam.baichunkSizeLimit = 10000000maxHeight      = 3000# settings for how the track lookscategory = MinION / Alignmentstype = JBrowse/View/Track/Alignments2key  = Minimap2 alignments from 4T1.ρ0#C [CG005][tracks.bw-CG005_Nov18_BC03-both-track]storeClass     = StrandedPlotPlugin/Store/SeqFeature/StrandedBigWigurlTemplate    = bw/mm2_called_CG005AB_BC03_vs_MmusG.bwcategory       = MinION / Coveragetype           = StrandedPlotPlugin/View/Track/Wiggle/StrandedXYPlotkey            = Minimap2 coverage from 4T1.ρ0#C [CG005]scale          = logscoreType      = maxScoreautoscale      = globalstyle.pos_color = #228B22style.neg_color = lightskyblue[tracks.BWCG004-4T1-BC04-both-track ] storeClass     = StrandedPlotPlugin/Store/SeqFeature/StrandedBigWig urlTemplate    = bw/mm2_called_CG004_BC04_vs_MmusG.bw category       = MinION - Coverage type           = StrandedPlotPlugin/View/Track/Wiggle/StrandedXYPlot key            = MinION minimap2 coverage from CG004-4T1-WT (combined strands) scale          = log scoreType      = maxScore autoscale      = global style.pos_color = darkred style.neg_color = darkgreenI have written my own helper scripts to reduce the effort required to generate these track sections. This is specific to my use case, but may help others for adapting to their own use. It takes three command-line parameters: the base file name, the internal ID for use in the track definition, and the label that appears within JBrowse. I use it as follows:./makeMinIONTemplate.sh mm2_called_CG005AB_BC03_vs_MmusG CG005_Nov18_BC03 '4T1.ρ0#C [CG005]' >> tracks.conf
?. [Sanity Check]
If this has worked properly, then mapping human or mouse to the mitochondrial genome should show most expression appearing on the positive strand, with a small scattering of negative-strand expression, a bit like the Expected Results shown here.If not, check for the following issues:Tracks not displaying at all in JBrowse -- make sure track IDs inside square brackets are of the form [ tracks.-track ]JBrowse track is reflected in the X axis -- make sure that the reverse bedgraph file is orientated the correct way; it should be created with the '-f 0x10' flag (no capitalisation).JBrowse track only shows one direction -- make sure that the reverse bedgraph file has negative values, and re-generate the bigwig file