10 months ago by
The way how
Gviz handles colouring for
AlignmentsTrack is a bit special. It recycles colours specified in the
fill.reads along the length of unique read ids (
uid column). On top of it, if you plot reads as arrows, it also sorts the reads before plotting according to the strand.
Maybe this piece of code does what you want. It is similar to what you suggested but still retain all plotting functionalities for the
AlignmentsTrack class. I tested it on few regions (even with gapped reads) and it seems to work.
bf <- system.file(package="Gviz", "extdata", "gapped.bam")
chrom <- "chr12"
from <- 2997500
to <- 2998200
alTrack <- Gviz:::.import.bam.alignments(bf, GRanges(chrom, IRanges(from, to)))
alTrack <- AlignmentsTrack(sort(alTrack))
displayPars(alTrack)$fill.reads <- c(plus="#ECAFAE", minus="#AFAEED")[as.numeric(sort(ranges(alTrack)$readStrand))]
plotTracks(alTrack, from=from, to=to, chromosome=chrom)
Let me know if that works for you.