Skip to content

Instantly share code, notes, and snippets.

@ipurusho
Last active September 28, 2016 17:05
Show Gist options
  • Save ipurusho/32c4eedaf86a21322e310ef21a0d6979 to your computer and use it in GitHub Desktop.
Save ipurusho/32c4eedaf86a21322e310ef21a0d6979 to your computer and use it in GitHub Desktop.

###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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment