###Download R scripts
Download the following R script which contains necessary functions: https://gist.github.com/ipurusho/64d6dbf1a32aa7c83f665785155e494b
Then, load the covariate_contribution.R
script into your R environment.
###Performing Covariate Analysis
Once you have obtained a variance stabilized count matrix and organized your meta data (meta data rows should correspond to VST columns), you can use the top.var
function (now in your R environment) to filter for the top 500 (or more) variable genes for input into PCA.
top500 <- top.var(vsd)
Next, we need to reduce VSTs to principal compenents and compute the percent contribution of the top PCs (we usually select the top 10):
PCs <- get.pc(top500)
PC.percents <- get.pc.percents
Now we can then use the evaluate.covariates
function to compute the contribution of each covariate relative to the principal components, for catagerical and continuous covariates disntinctly. For example:
heatmap.data <- evaluate.covariates(PCs[,1:10],
PC.percents[1:10],
continuous = data.frame(RIN = meta[,"RIN"], Age = meta[,"Age"], PMI = meta[,"PMI"])
categorical = data.frame(condition = meta[,"condition"], Region =meta[,"Region"])
)
Note: Please make sure continuous covariates are of type numeric
and categorical covariates are of type factor
###Generating Covariate Heatmap
A covariate heatmap can be visualized using ggplot2. The following is an example:
ggplot(heatmap.data, aes(Covariate, dim, group=dim)) +
geom_tile(aes(fill = R2)) +
geom_text(aes(fill = heatmap.data$R2, label = round(heatmap.data$R2, 2))) +
scale_fill_gradient(low = "white", high = "red") + labs(x = "covariates and Weighted Sum of R^2 Value", y = "dim and percent variance")+ggtitle("R^2 Value for dim-Covariate Regression \n (Using DESeq VST data)")+
theme(axis.text.x = element_text(face="bold", colour="#990000", vjust=0.5, size=10),axis.text.y = element_text(face="bold", colour="#990000", vjust=0.5, size=10),
axis.title.x = element_text(face="bold",size=12),axis.title.y = element_text(face="bold",size=12))