Tutorial: Network plot from expression data in R using igraph
23
gravatar for Kevin Blighe
20 days ago by
Kevin Blighe9.0k
Europe/Americas
Kevin Blighe9.0k wrote:

This tutorial will allow you to:

  1. create graph and tree objects from a data-frame or -matrix of numerical data
  2. identify community structure in your graph and tree objects
  3. Perform some basic downstream analyses on the tree objects

For simple testing, we'll use the estrogen microarray data-set in R and follow the tutorial from Rob Gentleman and Wolfgang Huber. This dataset was looking at genes that respond to estrogen stimulation.

The method can be applied to any type of numerical data, be it microarray, RNA-seq, metabolomics, qPCR, etc.

Step 1, Read in, normalise, and identify genes with significant effects

NB - IF YOU ALREADY HAVE YOUR OWN DATA, PROCEED TO STEP 2

[Si ya tiene sus propios datos, se va a Step 2]

[Se você já tem seus próprios dados, vá para a Step 2]

library(affy)
library(estrogen)
library(vsn)
library(genefilter)

datadir <- system.file("extdata", package="estrogen")
dir(datadir)
setwd(datadir)

#Read in the phenotype data and the raw expression 'CEL' files
pd <- read.AnnotatedDataFrame("estrogen.txt", header=TRUE, sep="", row.names=1)
a <- ReadAffy(filenames=rownames(pData(pd)), phenoData=pd, verbose=TRUE)

#Normalise the data
x <- expresso(
  a,
  bg.correct=FALSE,
  normalize.method="vsn",
  normalize.param=list(subsample=1000),
  pmcorrect.method="pmonly",
  summary.method="medianpolish"
)

#NB - if the vsn normalisation does not function, use:
#x <- expresso(a, bgcorrect.method="rma", normalize.method="constant", pmcorrect.method="pmonly", summary.method="avgdiff")

#Remove control probes
controlProbeIdx <- grep("^AFFX", rownames(x))
x <- x[-controlProbeIdx,]

#Identify genes of significant effect
lm.coef <- function(y) lm(y ~ estrogen * time.h)$coefficients
eff <- esApply(x, 1, lm.coef)
effectUp <- names(sort(eff[2,], decreasing=TRUE)[1:25])
effectDown <- names(sort(eff[2,], decreasing=FALSE)[1:25])
main.effects <- c(effectUp, effectDown)

#Filter the expression set object to include only genes of significant effect
estrogenMainEffects <- exprs(x)[main.effects,]

We now have the top 25 genes showing statistically significant effects in both directions and have filtered our expression object to include only these genes. The object that we'll use for further analysis is estrogenMainEffects

head(estrogenMainEffects)
          low10-1.cel low10-2.cel high10-1.cel high10-2.cel low48-1.cel
910_at       8.869798    8.863362     11.23440     11.12650    8.399458
31798_at    10.217214   10.121984     12.79881     12.33325   10.372707
40117_at     8.916664    8.809936     10.82381     10.72865    8.887119
1884_s_at    8.591428    8.406488     10.16884     10.33448    8.433163
947_at       9.777758    9.813295     11.72156     11.40366   10.106400
38116_at     9.126792    8.957475     11.15331     10.80998    8.152348
          low48-2.cel high48-1.cel high48-2.cel
910_at       8.400309     10.65822    10.597463
31798_at    10.112305     13.61006    13.234540
40117_at     8.669938     10.56590    10.000753
1884_s_at    8.446880     10.17888     9.533156
947_at       9.730117     11.86636    11.073517
38116_at     7.870085     10.72301    10.082670

Step 2, Create the graph and tree objects

In this example, we now have our data-frame/-matrix stored as estrogenMainEffects and will construct a graph adjacency object from this. estrogenMainEffects can be any numerical data-frame/-matrix from you own experiment(s).

library(igraph)

#Create a graph adjacency based on correlation distances between genes in  pairwise fashion.
g <- graph.adjacency(
  as.matrix(as.dist(cor(t(estrogenMainEffects), method="pearson"))),

  mode="undirected",
  weighted=TRUE,
  diag=FALSE
)

#NB - Euclidean distances can also be used instead of correlation, but this changes the tutorial slightly:
#g <- graph.adjacency(
#  as.matrix(dist(estrogenMainEffects, method="euclidean")),
#  mode="undirected",
#  weighted=TRUE,
#  diag=FALSE
#)

#Simplfy the adjacency object
g <- simplify(g, remove.multiple=TRUE, remove.loops=TRUE)

#Colour negative correlation edges as blue
E(g)[which(E(g)$weight<0)]$color <- "darkblue"

#Colour positive correlation edges as red
E(g)[which(E(g)$weight>0)]$color <- "darkred"

#Convert edge weights to absolute values
E(g)$weight <- abs(E(g)$weight)

#Change arrow size
#For directed graphs only
#E(g)$arrow.size <- 1.0

#Remove edges below absolute Pearson correlation 0.8
g <- delete_edges(g, E(g)[which(E(g)$weight<0.8)])

#Assign names to the graph vertices (optional)
V(g)$name <- V(g)$name

#Change shape of graph vertices
V(g)$shape <- "sphere"

#Change colour of graph vertices
V(g)$color <- "skyblue"

#Change colour of vertex frames
V(g)$vertex.frame.color <- "white"

#Scale the size of the vertices to be proportional to the level of expression of each gene represented by each vertex
#Multiple scaled vales by a factor of 10
scale01 <- function(x){(x-min(x))/(max(x)-min(x))}
vSizes <- (scale01(apply(estrogenMainEffects, 1, mean)) + 1.0) * 10

#Amplify or decrease the width of the edges
edgeweights <- E(g)$weight * 2.0

#Convert the graph adjacency object into a minimum spanning tree based on Prim's algorithm
mst <- mst(g, algorithm="prim")

#Plot the tree object
plot(
  mst,
  layout=layout.fruchterman.reingold,
  edge.curved=TRUE,
  vertex.size=vSizes,
  vertex.label.dist=-0.5,
  vertex.label.color="black",
  asp=FALSE,
  vertex.label.cex=0.6,
  edge.width=edgeweights,
  edge.arrow.mode=0,
  main="My first graph"
)

first

Note that layout can be modified to various other values, including:

  • layout.fruchterman.reingold
  • layout.kamada.kawai
  • layout.lgl
  • layout.circle
  • layout.graphopt

For further information, see Graph layouts


Step 3, Identify communities in the tree object based on 'edge betweenness'

mst.communities <- edge.betweenness.community(mst, directed=FALSE)
mst.clustering <- make_clusters(mst, membership=mst.communities$membership)
V(mst)$color <- mst.communities$membership + 1

par(mfrow=c(1,2))
plot(
  mst.clustering, mst,
  layout=layout.fruchterman.reingold,
  edge.curved=TRUE,
  vertex.size=vSizes,
  vertex.label.dist=-0.5,
  vertex.label.color="black",
  asp=FALSE,
  vertex.label.cex=0.6,
  edge.width=edgeweights,
  edge.arrow.mode=0,
  main="My first graph"
)

plot(
  mst,
  layout=layout.fruchterman.reingold,
  edge.curved=TRUE,
  vertex.size=vSizes,
  vertex.label.dist=-0.5,
  vertex.label.color="black",
  asp=FALSE,
  vertex.label.cex=0.6,
  edge.width=edgeweights,
  edge.arrow.mode=0,
  main="My first graph"
)

comm

Other community identification methods, including fastgreedy.community, can be found at Detecting Community Structure.


Step 4, Further analyses

#Check the vertex degree, i.e., number of connections to each vertex
degree(mst)

#Output information for each community, including vertex-to-community assignments and modularity
commSummary <- data.frame(
  mst.communities$names,
  mst.communities$membership,
  mst.communities$modularity
)
colnames(commSummary) <- c("Gene", "Community", "Modularity")
options(scipen=999)
commSummary
         Gene Community                Modularity
1      910_at         1 -0.0281615237929416124818
2    31798_at         2 -0.0086268060222194423159
3    40117_at         3  0.0117105668413069411576
4   1884_s_at         4  0.0311655447994580621363
5    38116_at         5  0.0518542468936595488116
6      947_at         6  0.0715756886160198307900
7    41400_at         4  0.0905925646858431743436
....

#Compare community structures using the variance of information (vi) metric (not relevant here)
#Community structures that are identical will have a vi=0
compare(mst.communities, mst.communities, method="vi")
[1] 0

Step 5, Session info (basic)

R version 3.2.5 (2016-04-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS
ADD COMMENTlink modified 19 days ago • written 20 days ago by Kevin Blighe9.0k
1

Suggestion: add microarray tag? Also include that in the title? Or are you using the microarray data only becuase it is easily available?

ADD REPLYlink modified 20 days ago • written 20 days ago by genomax39k
1

Thanks genomax. I've added microarray and rna-seq. Technically, the method can be applied to any type of data though. I originally pieced together the pipeline on metabolomics data.

ADD REPLYlink written 20 days ago by Kevin Blighe9.0k
1

Thanks @ Kevin.

ADD REPLYlink written 20 days ago by cpad01123.6k

Dear @Kevin Blighe, Hi and thank you. Can I use igraph for my Trinity de novo RNA-seq assembly? I have assembled my reads then performed DEG analysis using Trinity. which file I can use as input of igraph? Thanks again.

ADD REPLYlink written 20 days ago by Farbod3.0k
1

Dear Farbod,

All that you require is a data-frame or data-matrix of numerical data, preferably binomial distributed, as the method is based on Pearson correlation.

If your data-frame/-matrix is MyData, then the starting point is:

library(igraph)

#Create a graph adjacency based on correlation distances between genes in  pairwise fashion.
g <- graph.adjacency(as.matrix(as.dist(cor(t(MyData), method="pearson"))), mode="undirected", weighted=TRUE, diag=FALSE)

You can also use Euclidean distances instead of correlation, with:

g <- graph.adjacency(as.matrix(dist(MyData, method="euclidean")), mode="undirected", weighted=TRUE, diag=FALSE)
ADD REPLYlink modified 20 days ago • written 20 days ago by Kevin Blighe9.0k

Thank you

Trinity produces several kind of matrix. for example the first line of "Trans_counts.TMM.EXPR.matrix" is as:

                             J1     J2        J3      M1        M2     M3

TRINITY_DN190458_c7_g4_i9    0.188   0.020   0.098   0.223   0.121   0.092

can I use that ?

ADD REPLYlink modified 20 days ago • written 20 days ago by Farbod3.0k
1

It looks fine.

The code in the tutorial above can be used in very diverse ways and the meaning/significance of the graph will change depending on the data that you provide. If you are supplying it data from Trinity, then I presume that it is de novo transcriptome assembly transcripts? The graph of these would then show transcripts whose expression is highly positively or inversely correlated.

ADD REPLYlink written 20 days ago by Kevin Blighe9.0k

Yes it is de novo but

Please tell me what do you mean by "ranscripts whose expression is highly positively or inversely correlated"? are you telling me that the result graph would have some sort of skew or error? or it would be just a great candidate for my data?

ADD REPLYlink written 20 days ago by Farbod3.0k
1

Hey Farbod, the tutorial first generates a square correlation matrix of your data. It answers the question: 'Which genes are highly correlated with each other?' The graph object is then created from this correlation matrix. Thus, the edges in your graph will connect genes that are highly positively or inversely correlated. Closely related genes will also be grouped together in the same community.

You should probably ask yourself this question: why do you want to perform network analysis on your data and what do you believe it will add to your project?

In the example that I provide in the tutorial, I first filter the example dataset to include only genes that are statistically significantly related to treatment group and time (n=50). The resulting plot, then, shows the potential relationships between just these significant genes.

ADD REPLYlink modified 20 days ago • written 20 days ago by Kevin Blighe9.0k

Thank you Kevin, I got some errors in step1 data normalization

x <- expresso(
+   a,
+   bg.correct=FALSE,
+   normalize.method="vsn",
+   normalize.param=list(subsample=1000),
+   pmcorrect.method="pmonly",
+   summary.method="medianpolish"
+ )

normalization: vsn

PM/MM correction : pmonly

expression values: medianpolish

normalizing...Error in do.call(method, alist(object, ...)) : could not find function "normalize.AffyBatch.vsn"

ADD REPLYlink written 19 days ago by Mike880
1

try loading library "vsn" (library(vsn))

ADD REPLYlink written 19 days ago by cpad01123.6k

Hi Mike, did you install the vsn package? - Does library(vsn) return an error?

If you already have your own data, then you can start from the second step 'Create the graph and tree objects'

ADD REPLYlink written 19 days ago by Kevin Blighe9.0k

yes I installed vsn and loaded library(vsn)

then I check normalize.AffyBatch.vsn, but I couldnt get it...

?normalize.AffyBatch.vsn

normalize.AffyBatch.vsn package:vsn R Documentation

Wrapper for vsn to be used as a normalization method with expresso

Description:

 Wrapper for ‘vsn2’ to be used as a normalization method with the
 expresso function of the package affy. **The expresso function is
 deprecated, consider using ‘justvsn’ instead**. The
 normalize.AffyBatch.vsn can still be useful on its own, as it
 provides some additional control of the normalization process
 (fitting on subsets, alternate transform parameters).
ADD REPLYlink written 19 days ago by Mike880
1

The normalisation method is not too important just for the tutorial. Could you try:

x <- expresso(a, bgcorrect.method="rma", normalize.method="constant", pmcorrect.method="pmonly", summary.method="avgdiff")
ADD REPLYlink written 19 days ago by Kevin Blighe9.0k

Thank you very much, Kevin

ADD REPLYlink written 19 days ago by Mike880

Then watch out for the control probes step!

Use this instead of the original line:

main.effects <- main.effects[-which(main.effects %in% c("AFFX-CreX-3_at","AFFX-CreX-5_at"))]
ADD REPLYlink written 19 days ago by Kevin Blighe9.0k

Hi again Mike, I now remove those control probes just after normalisation. So, they shouldn't be a problem. I also added a note about VSN possibly not working.

Thanks for your feedback!

ADD REPLYlink written 19 days ago by Kevin Blighe9.0k

Hi, I have used it on my whole Trinity matrix file just for test and I received the following error:

Error in cor(t("/home/trans_counts.TMM.EXPR.matrix"),  : 
  'x' must be numeric

is it some separator issues?

ADD REPLYlink written 18 days ago by Farbod3.0k
1

You may have to read the file 'home/trans_counts.TMM.EXPR.matrix' into a data-frame? Something like

MyData <- read.table("/home/trans_counts.TMM.EXPR.matrix", sep="\t") #assuming tab-delimited

g <- graph.adjacency(
  as.matrix(as.dist(cor(t(MyData), method="pearson"))),
  mode="undirected",
  weighted=TRUE,
  diag=FALSE
)
ADD REPLYlink modified 18 days ago • written 18 days ago by Kevin Blighe9.0k

Thank you, there is another "x" and "numeric" issue:

Error in cor(t(MyData), method = "pearson") : 'x' must be numeric
ADD REPLYlink written 18 days ago by Farbod3.0k

Try as.matrix(as.dist(cor(t(data.matrix(MyData)), method="pearson")))

ADD REPLYlink written 18 days ago by Kevin Blighe9.0k

Hi, it seems that it is a problem with huge matrix file so I did it simple and just put 100 lines in the matrix. I want to know by which command I can create the plot? is it just plot() ?(sorry for simple questions)


library(igraph)
MyData <- read.table('/home/emami/Desktop/A1.matrix', sep="\t") #assuming tab-delimited
g <- graph.adjacency(
  as.matrix(as.dist(cor(t(data.matrix(MyData)), method="pearson"))),
  mode="undirected",
  weighted=TRUE,
  diag=FALSE
)
plot (g)
ADD REPLYlink written 18 days ago by Farbod3.0k

Yes, it will be difficult to produce a network of a large data-matrix because it has to compute a squared distance/correlation matrix. For example, if you tried it on a transcriptome of 20,000 protein-coding genes, you would have to generate a distance matrix of 400,000,000 data points.

You can generate a quick plot with just the base R plot() function, which will interpret the graph object. plot.igraph should also work. However, between the graph adjacency object and actually plotting the graph, there are many other important steps (as you can see above in the tutorial)

ADD REPLYlink written 18 days ago by Kevin Blighe9.0k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1431 users visited in the last hour