|
| 1 | +# Alternative enrichment analyses to GSEA |
| 2 | +# These are methods were implemented, expecting |
| 3 | +# RNA-seq input. These will need to be adjusted if |
| 4 | +# using microarray data |
| 5 | + |
| 6 | +# Load libraries |
| 7 | +library("fgsea") |
| 8 | +library("limma") |
| 9 | +library("GSVA") |
| 10 | +library("DEFormats") |
| 11 | +library("edgeR") |
| 12 | +library("DESeq2") |
| 13 | +library("clusterProfiler") |
| 14 | + |
| 15 | +find_enriched_pathways_ROAST <- function(expression_filename, |
| 16 | + metadata_filename, |
| 17 | + pathway_DB_filename) { |
| 18 | + |
| 19 | + # --------------------------------------------------------- |
| 20 | + # ROAST (rotation gene set tests) performs a focused gene set |
| 21 | + # testing, in which interest focuses on a few gene sets as opposed |
| 22 | + # to a large dataset. (available in limma). |
| 23 | + # * Self contained gene set test |
| 24 | + # * Instead of permutations they use rotations |
| 25 | + # (i.e. fractional permutation) in order to allow for more |
| 26 | + # complex experimental designs than binary experiments |
| 27 | + # (i.e. time-course, more than 2 groups) |
| 28 | + # * Also esimates correlation between genes |
| 29 | + # * Best power to detect modest fold changes with minority of genes changed |
| 30 | + # (https://pubmed.ncbi.nlm.nih.gov/20610611/) |
| 31 | + # --------------------------------------------------------- |
| 32 | + |
| 33 | + # Read data |
| 34 | + expression_data <- t(as.matrix(read.csv(expression_filename, sep="\t", header=TRUE, row.names=1))) |
| 35 | + metadata <- as.matrix(read.csv(metadata_filename, sep="\t", header=TRUE, row.names=1)) |
| 36 | + pathway_DB_data <- gmtPathways(pathway_DB_filename) |
| 37 | + |
| 38 | + group <- interaction(metadata[,1]) |
| 39 | + |
| 40 | + # Create DEGList based on counts |
| 41 | + #dge = DGEList(expression_data, group=group) |
| 42 | + |
| 43 | + # Design matrix |
| 44 | + design <- model.matrix(~0 + group) |
| 45 | + |
| 46 | + expression_data_voom <- voom(expression_data, design) |
| 47 | + #print(expression_data_voom) |
| 48 | + |
| 49 | + # Estimate dispersions |
| 50 | + #y <- estimateDisp(dge, design) |
| 51 | + |
| 52 | + # Format index |
| 53 | + gene_ids <- row.names(expression_data_voom) |
| 54 | + pathway_ind <- ids2indices(pathway_DB_data, gene_ids) |
| 55 | + |
| 56 | + print("Checking sample ordering...") |
| 57 | + print(all.equal(colnames(expression_data_voom), rownames(metadata))) |
| 58 | + |
| 59 | + # Call ROAST |
| 60 | + enrich_pathways <- mroast(expression_data_voom, index=pathway_ind, design, contrast=ncol(design), nrot=10000, adjust.method="BH") |
| 61 | + |
| 62 | + return(as.data.frame(enrich_pathways)) |
| 63 | +} |
| 64 | + |
| 65 | +find_enriched_pathways_CAMERA <- function(expression_filename, |
| 66 | + metadata_filename, |
| 67 | + pathway_DB_filename) { |
| 68 | + |
| 69 | + # --------------------------------------------------------- |
| 70 | + # CAMERA (Correlation Adjusted MEan RAnk gene set test) |
| 71 | + # is based on the idea of estimating the variance inflation |
| 72 | + # factor associated with inter-gene correlation, and |
| 73 | + # incorporating this into parametric or rank-based |
| 74 | + # test procedures. (available in limma) |
| 75 | + # * Competitive gene set test |
| 76 | + # * Performs the same rank-based test procedure as GSEA, |
| 77 | + # but also estimates the correlation between genes, |
| 78 | + # instead of treating genes as independent |
| 79 | + # * Recall GSEA: 1) Rank all genes using DE association statistics. |
| 80 | + # 2) An enrichment score (ES) is defined as the maximum distance |
| 81 | + # from the middle of the ranked list. Thus, the enrichment score |
| 82 | + # indicates whether the genes contained in a gene set are clustered |
| 83 | + # towards the beginning or the end of the ranked list |
| 84 | + # (indicating a correlation with change in expression). |
| 85 | + # 3) Estimate the statistical significance of the ES by a |
| 86 | + # phenotypic-based permutation (permute samples assigned to label) |
| 87 | + # test in order to produce a null distribution for the ES |
| 88 | + # (i.e. scores based on permuted phenotype) |
| 89 | + # * Appropriate for small and large fold changes |
| 90 | + # (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3458527/) |
| 91 | + # --------------------------------------------------------- |
| 92 | + |
| 93 | + # Read data |
| 94 | + expression_data <- t(as.matrix(read.csv(expression_filename, sep="\t", header=TRUE, row.names=1))) |
| 95 | + metadata <- as.matrix(read.csv(metadata_filename, sep="\t", header=TRUE, row.names=1)) |
| 96 | + pathway_DB_data <- gmtPathways(pathway_DB_filename) |
| 97 | + |
| 98 | + print("Checking sample ordering...") |
| 99 | + print(all.equal(colnames(expression_data), rownames(metadata))) |
| 100 | + |
| 101 | + group <- interaction(metadata[,1]) |
| 102 | + |
| 103 | + # Create DEGList based on counts |
| 104 | + dge = DGEList(expression_data, group=group) |
| 105 | + |
| 106 | + # Design matrix |
| 107 | + design <- model.matrix(~0 + group) |
| 108 | + |
| 109 | + # Estimate dispersions |
| 110 | + y <- estimateDisp(dge, design) |
| 111 | + |
| 112 | + # Call CAMERA |
| 113 | + enrich_pathways <- camera(y, pathway_DB_data, design, contrast=ncol(design), nrot=10000) |
| 114 | + |
| 115 | + return(as.data.frame(enrich_pathways)) |
| 116 | +} |
| 117 | + |
| 118 | +find_enriched_pathways_GSVA <- function(expression_filename, |
| 119 | + pathway_DB_filename) { |
| 120 | + |
| 121 | + # --------------------------------------------------------- |
| 122 | + # GSVA(Gene Set Variation Analysis) calculates sample-wise |
| 123 | + # gene set enrichment scores as a function of genes inside |
| 124 | + # and outside the gene set. This method is well-suited for |
| 125 | + # assessing gene set variation across a dichotomous phenotype. |
| 126 | + # (biocontuctor package GSVA) |
| 127 | + # * Competitive gene set test |
| 128 | + # * Estimates variation of gene set enrichment over the samples |
| 129 | + # independently of any class label |
| 130 | + # (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3618321/) |
| 131 | + # --------------------------------------------------------- |
| 132 | + |
| 133 | + # Read in expression data |
| 134 | + # Transpose to gene x sample |
| 135 | + expression_data <- t(read.table(expression_filename, |
| 136 | + sep = "\t", |
| 137 | + header = TRUE, |
| 138 | + row.names = NULL)) |
| 139 | + pathway_DB_data <- gmtPathways(pathway_DB_filename) |
| 140 | + |
| 141 | + enrich_pathways <- gsva(expression_data, |
| 142 | + pathway_DB_data, |
| 143 | + kcdf="Poisson", |
| 144 | + parallel.sz=1, |
| 145 | + verbose=TRUE |
| 146 | + ) |
| 147 | + return(as.data.frame(enrich_pathways)) |
| 148 | +} |
| 149 | + |
| 150 | +find_enriched_pathways_ORA <- function(expression_filename, |
| 151 | + metadata_filename, |
| 152 | + pathway_DB_filename |
| 153 | + ) { |
| 154 | + # --------------------------------------------------------- |
| 155 | + # ORA (over-representation analysis) uses the hypergeometric |
| 156 | + # test to determine if there a significant over-representation |
| 157 | + # of pathway in the selected set of DEGs. Here we're using |
| 158 | + # clusterProfiler library but there are multiple options for this analysis. |
| 159 | + # (https://www.rdocumentation.org/packages/clusterProfiler/versions/3.0.4/topics/enricher) |
| 160 | + # --------------------------------------------------------- |
| 161 | + |
| 162 | + # Read data |
| 163 | + expression_data <- t(as.matrix(read.csv(expression_filename, sep="\t", header=TRUE, row.names=1))) |
| 164 | + metadata <- as.matrix(read.csv(metadata_filename, sep="\t", header=TRUE, row.names=1)) |
| 165 | + |
| 166 | + print("Checking sample ordering...") |
| 167 | + print(all.equal(colnames(expression_data), rownames(metadata))) |
| 168 | + |
| 169 | + group <- interaction(metadata[,1]) |
| 170 | + |
| 171 | + mm <- model.matrix(~0 + group) |
| 172 | + |
| 173 | + ddset <- DESeqDataSetFromMatrix(expression_data, colData=metadata, design = ~group) |
| 174 | + |
| 175 | + deseq_object <- DESeq(ddset) |
| 176 | + |
| 177 | + # Note parameter settings: |
| 178 | + # `independentFilter=False`: We have turned off the automatic filtering, which |
| 179 | + # filter filter out those tests from the procedure that have no, or little |
| 180 | + # chance of showing significant evidence, without even looking at their test statistic. |
| 181 | + # Typically, this results in increased detection power at the same experiment-wide |
| 182 | + # type I error, as measured in terms of the false discovery rate. |
| 183 | + # cooksCutoff=True (default): Cook's distance as a diagnostic to tell if a single sample |
| 184 | + # has a count which has a disproportionate impact on the log fold change and p-values. |
| 185 | + # These genes are flagged with an NA in the pvalue and padj columns |
| 186 | + deseq_results <- results(deseq_object, independentFiltering=FALSE) |
| 187 | + |
| 188 | + deseq_results_df <- as.data.frame(deseq_results) |
| 189 | + |
| 190 | + # Get DEGs |
| 191 | + threshold=0.05 |
| 192 | + backgrd_genes <- row.names(deseq_results_df) |
| 193 | + degs <- deseq_results_df[deseq_results_df[,'padj']<threshold & abs(deseq_results_df[,'log2FoldChange'])>1,] |
| 194 | + degs_name <- row.names(degs) |
| 195 | + |
| 196 | + # Get over-represented pathways |
| 197 | + pathway_DB_data <- read.gmt(pathway_DB_filename) |
| 198 | + |
| 199 | + enrich_pathways <- enricher(degs_name, |
| 200 | + universe=backgrd_genes, |
| 201 | + pvalueCutoff=0.05, |
| 202 | + pAdjustMethod="BH", |
| 203 | + TERM2GENE=pathway_DB_data[, c("ont", "gene")] |
| 204 | + ) |
| 205 | + return(as.data.frame(summary(enrich_pathways))) |
| 206 | +} |
0 commit comments