NewUsr_stat
NewUsr_stat

Reputation: 2571

igraph node colors according to gene expression values attributes

I have a range of values from -0.10100 to 0.28400 (totally 120 continues values). This values represent gene expression of 120 genes. In my network they represent the interacting genes of TGFB1. I'm trying plot my network by using igraph library. I would like to have two effects on my node colors: the first one is to: color my nodes according to the gene expression value in greenred (in green the negative values and in red the positive ones) according to the range of values representing my attributes. Then, I would like to make transparent the redgreen nodes in the range from -0.10100 to 0.04720. Since I'm not an R expert I'm encountering troubles to have so many effects on my network. Can anyone help me please?

My attempts:

 tmp1= read.delim("mynet.txt", header= T)
 g <- graph.data.frame(tmp1, directed=FALSE)
 V(g)$name
 [1] "COL6A3"    "PDGFRB"    "COL3A1"    "COL5A1"    "LOXL1"   ....

 g
 GRAPH UN-- 120 120 -- 
 + attr: name (v/c), GEX (v/c), color (v/c)

 tmp2= read.delim("myattributes.txt", header= T)
 GENE       S2N      
 COL6A3     0.28400
 PDGFRB     0.28100
 COL3A1     0.26300
 ......     .......    
 V(g)$GEX=as.numeric(tmp2$S2N[match(V(g)$name,tmp2$GENE)])
 V(g)$color=V(g)$GEX        

Then unfortunately I stopped and I'm not able to continue. Can anyone help me please?

Best

Upvotes: 1

Views: 1894

Answers (2)

koalabear
koalabear

Reputation: 31

You can create a mapping for your colors to a color palette. Assuming you want your negative values green, zeros white and positive values red, this should work:

my_palette <- colorRampPalette(c("green", "white", "red"))(n = 10000)

sf <- max(abs(tmp2$S2N))
node.colors <- (tmp2$S2N+sf) / (2*sf) * 10000

plot(g, vertex.color=my_palette[node.colors])

Upvotes: 3

ddiez
ddiez

Reputation: 1127

Probably this is not an optimal solution but lets see if it is useful to you. Basically, you have to assign to V(g)$color a color, not a number as you are doing now. My solution is to define intervals over your continuous data and assign a color to each of the intervals. To map your continuous data (i.e. df$S2N) to categorical data you can use cut. Here is an example:

library(igraph)
library(org.Hs.eg.db) # Bioconductor annotation package for human.

set.seed(123)

# create toy network:
g <- barabasi.game(10, directed = FALSE)

# assign random gene ids.
V(g)$name <- sample(keys(org.Hs.eg.db), 10)

# assign the gene symbol to the label attribute:
V(g)$label <- select(org.Hs.eg.db, keys = V(g)$name, columns="SYMBOL")$SYMBOL
plot(g)

# generate toy dataset:
df <- data.frame(GENE=V(g)$label, S2N=sample(seq(-2,2,.1),10,replace=TRUE))

# if you are plotting e.g. fold changes, you may want dark blue for 
# FC between -2 and -1, blue for FC between -1 and -.5, grey for -.5 and .5, and so on.
# define colors:
col <- c("darkblue", "blue", "grey", "orange", "red")

# map your continuous data to the intervals you want:
(colc <- cut(df$S2N, breaks = c(-2, -1, -.5, .5, 1, 2), include.lowest = TRUE))

[1] (-1,-0.5]  (0.5,1]    (-0.5,0.5] (1,2]      (0.5,1]    (-0.5,0.5]
[7] (-0.5,0.5] (1,2]      (1,2]      [-2,-1]   
Levels: [-2,-1] (-1,-0.5] (-0.5,0.5] (0.5,1] (1,2]

# assign the colors to the network
V(g)$color=col[colc]
plot(g)

Note that keys and select are from the AnnotationDbi package that handles the annotations in org.Hs.eg.db (it's a dependency), but otherwise are not necessary. Here are only used for the example's sake.

Upvotes: 1

Related Questions