|
| 1 | +--- |
| 2 | +title: "edgeR" |
| 3 | +author: "Katarzyna Sikora" |
| 4 | +output: html_document |
| 5 | +--- |
| 6 | + |
| 7 | +```{r setup, include=FALSE} |
| 8 | +wd<-file.path(snakemake@params[["basedir"]],snakemake@params[["outdir"]]) |
| 9 | +system(paste0('mkdir -p ',wd)) |
| 10 | +knitr::opts_chunk$set(echo = TRUE) |
| 11 | +knitr::opts_knit$set(root.dir = wd) |
| 12 | +``` |
| 13 | + |
| 14 | +## Libs |
| 15 | + |
| 16 | +```{r libs} |
| 17 | +library(tximport) |
| 18 | +library(edgeR) |
| 19 | +library(GenomicRanges) |
| 20 | +``` |
| 21 | + |
| 22 | +## Import counts and summarize gene-wise |
| 23 | + |
| 24 | +Original oarfish output will be used. |
| 25 | + |
| 26 | +The output files are read using tximport function, which imports the transcript-level counts and summarizes them per gene using a transcript to gene mapping file. |
| 27 | + |
| 28 | +```{r tximport} |
| 29 | +input_files<-unlist(snakemake@params[["input_files"]]) |
| 30 | +names(input_files)<-gsub(".quant","",basename(input_files)) |
| 31 | +tx2gene_file<-snakemake@input[["t2g"]] |
| 32 | +tx2gene<-read.delim(tx2gene_file,header=FALSE)[,c(1,2)] |
| 33 | +txi<-tximport(input_files, type = "oarfish", tx2gene = tx2gene) |
| 34 | +
|
| 35 | +``` |
| 36 | + |
| 37 | +## Prep the DGEList object |
| 38 | + |
| 39 | +```{r sampleinfo} |
| 40 | +data.counts<-txi$counts |
| 41 | +write.table(data.counts,"data.counts.tsv",sep="\t",quote=FALSE) |
| 42 | +
|
| 43 | +sampleSheet<-snakemake@input[["sampleSheet"]] |
| 44 | +sampleInfo<-read.table(sampleSheet,sep="\t",header=TRUE) |
| 45 | +sampleInfo_h1<-sampleInfo |
| 46 | +sampleInfo_h2<-sampleInfo |
| 47 | +sampleInfo_h1$allele<-"h1" |
| 48 | +sampleInfo_h2$allele<-"h2" |
| 49 | +allelic_sampleInfo<-as.data.frame(rbind(sampleInfo_h1,sampleInfo_h2)) |
| 50 | +#rownames(sampleInfo)<-sampleInfo$name |
| 51 | +allelic_sampleInfo$unique_name<-with(allelic_sampleInfo,paste0(name,"_",allele)) |
| 52 | +
|
| 53 | +write.table(allelic_sampleInfo,"allelic_sampleInfo.tsv",sep="\t",quote=FALSE) |
| 54 | +data.counts<-data.counts[,allelic_sampleInfo$unique_name] |
| 55 | +
|
| 56 | +
|
| 57 | +y <- DGEList(counts = data.counts, samples = allelic_sampleInfo, group = allelic_sampleInfo$allele) |
| 58 | +
|
| 59 | +head(y$genes) |
| 60 | +
|
| 61 | +#gtf_file<-snakemake@input[["gtf_file"]] |
| 62 | +
|
| 63 | +
|
| 64 | +#gtf<-rtracklayer::import(gtf_file) |
| 65 | +
|
| 66 | +#ginfo <- mcols(gtf)[match(rownames(y),mcols(gtf)$transcript_id),c("transcript_type","gene_id","gene_name")] |
| 67 | +#y$genes <- cbind(y$genes,ginfo) |
| 68 | +#head(y$genes) |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +## Filter and normalize |
| 73 | + |
| 74 | +Lowly expressed genes are filtered out prior to the downstream analysis. |
| 75 | +Scaling factors can computed using the TMM method to convert the resulting library sizes to effective library sizes. |
| 76 | + |
| 77 | +```{r filt norm} |
| 78 | +keep <- filterByExpr(y) |
| 79 | +table(keep) |
| 80 | +y <- y[keep, , keep.lib.sizes=FALSE] |
| 81 | +
|
| 82 | +
|
| 83 | +y <- normLibSizes(y) |
| 84 | +y$samples |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +## Calculate MDS |
| 89 | + |
| 90 | +MDS plots can also be used to visualize differences between the expression profiles of different samples with gene-level counts. |
| 91 | + |
| 92 | +```{r MDS} |
| 93 | +plotMDS(y,col = c(1:2)[y$samples$allele],labels = y$samples$unique_name,xlim = c(-4,4)) |
| 94 | +``` |
| 95 | + |
| 96 | +## Design matrix |
| 97 | + |
| 98 | +We create the design matrix to compare HEK293 cells against HAP1 cells. |
| 99 | + |
| 100 | +```{r design} |
| 101 | +design <- model.matrix(~ allele, data = y$samples) |
| 102 | +design |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +## Dispersion estimation |
| 107 | + |
| 108 | +Estimate and visualize NB dispersions. |
| 109 | + |
| 110 | +```{r disp} |
| 111 | +y <- estimateDisp(y, design, robust=TRUE) |
| 112 | +saveRDS(y,"y.RDS") |
| 113 | +y$common.dispersion |
| 114 | +plotBCV(y) |
| 115 | +
|
| 116 | +``` |
| 117 | + |
| 118 | +The NB dispersion estimates will not be used further under the latest quasi-likelihood (QL) pipeline. |
| 119 | +For DGE analyses, we're going to use the quasi-likelihood (QL) pipeline for stricter error rate control by accounting for the uncertainty associated with the dispersion estimation. |
| 120 | + |
| 121 | +```{r quasil} |
| 122 | +fit <- glmQLFit(y, design, robust=TRUE) |
| 123 | +plotQLDisp(fit) |
| 124 | +``` |
| 125 | + |
| 126 | +## Differential expression |
| 127 | + |
| 128 | +Differentially expressed genes are tested between cell lines using the QL F-test. |
| 129 | + |
| 130 | +```{r dte} |
| 131 | +qlf <- glmQLFTest(fit) |
| 132 | +is.de <- decideTests(qlf, p.value=0.05) |
| 133 | +summary(is.de) |
| 134 | +
|
| 135 | +
|
| 136 | +tt <- as.data.frame(topTags(qlf, n = Inf,p.value=0.05)) |
| 137 | +head(tt) |
| 138 | +table(tt$transcript_type) |
| 139 | +length(unique(tt$gene_id)) |
| 140 | +``` |
| 141 | +## MA plot |
| 142 | + |
| 143 | +```{r ma} |
| 144 | +plotMD(qlf) |
| 145 | +``` |
| 146 | + |
| 147 | +## Save and export results |
| 148 | + |
| 149 | +```{r save} |
| 150 | +saveRDS(qlf,"qlf.RDS") |
| 151 | +write.table(tt,"topTags_pval0.05.tsv",sep="\t",quote=FALSE) |
| 152 | +
|
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | +## Session Info |
| 157 | + |
| 158 | +```{r session info} |
| 159 | +sessionInfo() |
| 160 | +``` |
| 161 | + |
| 162 | + |
0 commit comments