Changing text color on gviz

I'm using Gviz to display transcripts. I wanna highlight some exons with different color. My input is a tab-separated plain text file like the following:

new=read.table("myfile",header=T) head(new,1) chromosome start end width strand feature gene exon transcript color 1 chrX 586072 586344 273 + protein_coding Gene1 Exon1 Trans1 red gen="hg19" chr="chrX" gtrack 

However, I found exons were not colored in the order of "color" column. My input file has already been sorted by coordinates. Did I miss something? How can I match an exon to its color?

Any help is appreciated. Thanks a lot.

Entering edit mode @florianhahnenovartiscom-3784 Last seen 6.0 years ago Switzerland

Did you check the package vignette or the class' help page?

On page 33 of the vignette:

Unless we tell the Gviz package how to deal with the respective feature types they will all be treated in a similar fashion, i.e., they will be plotted using the default color as defined by the fill display parameter. To define colors for individual feature types we simply have to add them as additional display parameters, where the parameter name matches the feature type and its value is supposed to be a valid R color qualifier. Of course this implies that we can only use feature names that are not already occupied by other display parameters defined in the package

From the class documentation:

feature: Factor (or other vector that can be coerced into one), giving

the feature types for the individual track items. When

plotting the track to the device, if a display parameter with

the same name as the value of 'feature' is set, this will be

used as the track item's fill color. See 'grouping' for

details. Needs to be of equal length as the provided genomic

coordinates, or of length 1.

So essentially what you want to do here is provide the values from your color column as the class' feature argument, and define display parameters for the colors accordingly:

gtrack 
ADD COMMENT • link 8.0 years ago florian.hahne@novartis.com ★ 1.6k
Entering edit mode

Thanks heap, Florian. It works perfectly, however it turned out a new question. I still have some exons not colored as expected. They were colored by the default yellow color which is not in my list. I found those exons are too close to their neighbors to be distinguished on the plot. Is that why they were not colored? Is there a way to achieve it without changing the plotted genomic range? Thanks a lot.

ADD REPLY • link 8.0 years ago dustar1986 ▴ 10 Entering edit mode

I have a similar problem. I want to use TxDb as input and to highlight one whole transcripts of a gene. But the result confuses me.

I created the TxDb from a GRanges Object, which is a subset of GencodeV14 mm10 (filter: gene_id==" ENSMUSG00000060126.14").

> print(txdb) TxDb object: # Db type: TxDb # Supporting package: GenomicFeatures # Genome: NA # transcript_nrow: 8 # exon_nrow: 24 # cds_nrow: 6 # Db created by: GenomicFeatures package from Bioconductor # Creation time: 2017-12-01 20:21:53 +0100 (Fri, 01 Dec 2017) # GenomicFeatures version at creation time: 1.24.5 # RSQLite version at creation time: 1.1-2 # DBSCHEMAVERSION: 1.1 > transcripts(txdb) GRanges object with 8 ranges and 2 metadata columns: seqnames ranges strand | tx_id tx_name   |  [1] chr14 [75845093, 75848415] + | 1 ENSMUST00000110894.8 [2] chr14 [75845302, 75848525] + | 2 ENSMUST00000142061.2 [3] chr14 [75845331, 75846892] + | 3 ENSMUST00000150047.7 [4] chr14 [75845332, 75845969] + | 4 ENSMUST00000128889.1 [5] chr14 [75845355, 75847088] + | 5 ENSMUST00000154533.7 [6] chr14 [75845900, 75848297] + | 6 ENSMUST00000134040.1 [7] chr14 [75845919, 75846777] + | 7 ENSMUST00000151751.1 [8] chr14 [75846185, 75846987] + | 8 ENSMUST00000125146.1 ------- seqinfo: 22 sequences from an unspecified genome; no seqlengths

Creating a standard GeneRegionTrack is easy then:

> grTrack 

I inspected some slots of the GeneRegionTrack to understand the structure:

> feature(grTrack) [1] "utr5" "utr5" "ncRNA" "ncRNA" "ncRNA" "CDS" "CDS" "ncRNA" "ncRNA" [10] "ncRNA" "ncRNA" "ncRNA" "ncRNA" "CDS" "CDS" "ncRNA" "ncRNA" "ncRNA" . > group(grTrack) [1] "ENSMUST00000110894.8" "ENSMUST00000142061.2" "ENSMUST00000150047.7" [4] "ENSMUST00000128889.1" "ENSMUST00000154533.7" "ENSMUST00000110894.8" [7] "ENSMUST00000110894.8" "ENSMUST00000150047.7" "ENSMUST00000154533.7" [10] "ENSMUST00000128889.1" "ENSMUST00000134040.1" "ENSMUST00000151751.1" [13] "ENSMUST00000125146.1" "ENSMUST00000110894.8" "ENSMUST00000142061.2" .

I would have expected that feature(grTrack)[1] belongs to group(grTrack)[1] . The slot start(grTrack) seems to match, too. If I am right, the paramter " fill " has to have the same order. Thus, one shoud be able to fill transcripts parts by using slot " group ".

> displayPars(grTrack) getPar(grTrack,"fill") [1] "green" "red" "green" "green" "green" "green" "green" "green" "green" [10] "green" "green" "green" "green" "green" "red" "green" "green" "green" .

However, the plot is strange. Exons of different transcripts are filled red instead of the choosen transcript.

> plotTracks( grTrack, transcriptAnnotation="transcript", just.group="above" )

Is that a bug, an example of somewhat "erratic" you mentioned or do I miss something? Using parameter " fill " within plotTracks is no option, because I want to use several other tracks.