Skip to content

Instantly share code, notes, and snippets.

@slavailn
Created August 12, 2019 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slavailn/67a992f239ffd5cf4fdd95590b294432 to your computer and use it in GitHub Desktop.
Save slavailn/67a992f239ffd5cf4fdd95590b294432 to your computer and use it in GitHub Desktop.
Function to calculate SPIA scores for DESeq2 results table
resFile <- "DESeq2_result_name" # has to have entrez ids as annotations
compName <- "comparison" # Name of the comparison
calcSPIA <- function(resFile, compName)
{
res <- read.table(resFile, header = T, sep = "\t", fill=T, quote = "\"", stringsAsFactors = F)
# Select differentially expressed genes (adjusted p-value < 0.05)
# This is a named vector, where names are entrez ids, and values are log2 fold changes
ids <- res[res$padj < 0.05,]$entrezgene_id
logFC <- res[res$padj < 0.05,]$log2FoldChange
DE_genes <- logFC
names(DE_genes) <- ids
DE_genes <- DE_genes[!is.na(names(DE_genes))]
# Get all expressed genes entrez ids
ALL_genes <- res$entrezgene_id
ALL_genes <- ALL_genes[!is.na(ALL_genes)]
res <- spia(de=DE_genes, all=ALL_genes, organism="hsa", nB=2000, plots=FALSE, beta=NULL, combine="fisher", verbose=FALSE)
write.table(res, file=paste(compName, "_SPIA_result.txt", sep = ""), sep = "\t", col.names = T, row.names = F, quote = F)
tiff(file=paste(compName, "_SPIA_result.tiff", sep = ""), width=500, height=500)
plotP(res, threshold = 0.05)
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment