This is a test version of Biostars. For the public version, visit https://www.biostars.org.
rgb to hex in GRanges Object in R

Hey,

I have the following problem: using the karyoploteR package in R, i want to plot chromatin configuration along the genome as a colored bar. The GRanges Object with the chromatin regions has a column for that ("V9") with rgb values associated with each region. However, the kpPlotRegions function requires the colors in hex-format. I tried to export the Object, change rgb to hex and re-import it, but doesn't recognize the input. Has anyone experienced this problem or could provide a solution? Thank you!

K562.hmm

GRanges object with 622257 ranges and 6 metadata columns:
         seqnames              ranges strand |                V4        V5
            <Rle>           <IRanges>  <Rle> |       <character> <integer>
       1     chr1         10000-10600      * | 15_Repetitive/CNV         0
       2     chr1         10600-10937      * | 13_Heterochrom/lo         0
       3     chr1         10937-11937      * |       8_Insulator         0
       4     chr1         11937-12337      * | 5_Strong_Enhancer         0
       5     chr1         12337-13137      * |   7_Weak_Enhancer         0
     ...      ...                 ...    ... .               ...       ...
  622253     chrX 155256806-155257806      * |       11_Weak_Txn         0
  622254     chrX 155257806-155259206      * |       8_Insulator         0
  622255     chrX 155259206-155259406      * |   6_Weak_Enhancer         0
  622256     chrX 155259406-155259606      * |   7_Weak_Enhancer         0
  622257     chrX 155259606-155260406      * | 15_Repetitive/CNV         0
                  V6        V7        V8          V9
         <character> <integer> <integer> <character>
       1           .     10000     10600 245,245,245
       2           .     10600     10937 245,245,245
       3           .     10937     11937  10,190,254
       4           .     11937     12337   250,202,0
       5           .     12337     13137   255,252,4
     ...         ...       ...       ...         ...
  622253           . 155256806 155257806 153,255,102
  622254           . 155257806 155259206  10,190,254
  622255           . 155259206 155259406   255,252,4
  622256           . 155259406 155259606   255,252,4
  622257           . 155259606 155260406 245,245,245
  -------
  seqinfo: 23 sequences from an unspecified genome; no seqlengths
> kpPlotRegions(kp, K562.hmm, col=K562.hmm$V9, r0=0.22, r1=0.3)
Error in (function (xleft, ybottom, xright, ytop, density = NULL, angle = 45,  : 
Unzulässige Farbspezifikation "245,245,245"
chip-seq r

Can you show the error you get with the hex-format?

Thank you for your answer! There is no distinct error: When i import a file with hex-values, it just doesn't recognizes them and prints NA only.

probably due to the #, you would have to tweak the import settings of the function you're using. But see if the function I linked below already does what you want directly on the RGB values.

1 answer

The rgb conversion shown here should help. I.e.

sapply(strsplit(K562.hmm$V9, ","), function(x)
    rgb(x[1], x[2], x[3], maxColorValue=255))

Hey, It worked with K562.hmm$V9 <- sapply(strsplit(K562.hmm$V9, ","), function(x) rgb(x[1], x[2], x[3], maxColorValue=255))

Thank you for your help! :)

Log in to answer this question.