dea.R 7.36 KB
Newer Older
Brandi Cantarel's avatar
Brandi Cantarel committed
1 2
# UI-elements for DEA

Danni Luo's avatar
Danni Luo committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
ctfile <-  paste(data.dir, 'countTable.logCPM.txt', sep = '/')
samfile <-  paste(data.dir, 'design.shiny.txt', sep = '/')
cts <- read.table(file = ctfile, header = TRUE, sep = '\t')
samtbl <- read.table(samfile, header = TRUE, sep = "\t")
samples <- colnames(cts[, 4:length(cts)])
mergetbl <-
  merge(
    as.data.frame(samples),
    samtbl,
    by.x = "samples",
    by.y = "SampleID",
    all.x = TRUE,
    sort = FALSE
  )
Brandi Cantarel's avatar
Brandi Cantarel committed
17 18
grps <- mergetbl$SampleGroup
grpnames <- levels(factor(grps))
Danni Luo's avatar
Danni Luo committed
19 20 21
col.blocks <- col.grp(grps, grpnames)
MSIG.geneSets <-
  read.gmt(paste(data.dir, 'geneset.shiny.gmt', sep = '/'))
Brandi Cantarel's avatar
Brandi Cantarel committed
22 23

output$pick.dea <- renderUI({
Danni Luo's avatar
Danni Luo committed
24 25
  flist <- list.files(data.dir, pattern = "*edgeR.txt$")
  selectInput("file", "Choose Pair", choices=flist, width = "100%")
Brandi Cantarel's avatar
Brandi Cantarel committed
26 27
})
output$pick.pathway <- renderUI({
Danni Luo's avatar
Danni Luo committed
28 29 30
  pathways <- names(MSIG.geneSets)
  pathchoices = setNames(1:length(pathways), pathways)
  selectInput("deapathname", "Choose Pair", choices = pathchoices)
Brandi Cantarel's avatar
Brandi Cantarel committed
31 32 33
})

get.data <- function(var) {
Danni Luo's avatar
Danni Luo committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  f <- paste(data.dir, var$file, sep = '/')
 # f <-paste(f, "edgeR.txt", sep = ".") #This line needs to comment out if input is fullname
  f1 <- paste(data.dir, input$file, sep = '/')
  hmcomp <-paste("Heatmap of all genes with an FDR < 0.05 using EdgeR Results", sep ='')
  top50 <- paste("Top 50 User Defined Genes")
  allgene <- paste("All Differentially Expressed Genes")
  comp <- read.table(f, header = TRUE, sep = '\t')
  comp.filt <-
    na.omit(comp[abs(comp$logFC) >= var$fc.thresh &
                   comp$rawP <= var$pval.thresh,])
  if (var$adjust == 'FDR') {
    comp.filt <-
      na.omit(comp[abs(comp$logFC) >= var$fc.thresh &
                     comp$fdr <= var$pval.thresh,])
  }
  if (var$adjust == 'BONF') {
    comp.filt <-
      na.omit(comp[abs(comp$logFC) >= var$fc.thresh &
                     comp$bonf <= var$pval.thresh,])
  }
  genelist <-
    as.character(head(comp.filt[order(comp.filt$fdr),]$symbol, n = var$numgenes))
  if (var$heatmap == 'hgeneset') {
    genelist <- unlist(MSIG.geneSets[as.numeric(var$deapathname)])
  }
  if (var$heatmap == 'cgeneset') {
    genelist <- unlist(strsplit(as.character(var$genes), "[;\n]+"))
  }
  return(
    list(
      filt = comp.filt,
      glist = genelist,
      f1 = f1,
      hmcomp = hmcomp,
      top50 = top50,
      allgene = allgene
    )
  )
Brandi Cantarel's avatar
Brandi Cantarel committed
72 73
}

Danni Luo's avatar
Danni Luo committed
74 75 76 77
tbls <-  eventReactive(input$deButton,
                       {
                         get.data(input)
                       })
Brandi Cantarel's avatar
Brandi Cantarel committed
78 79

output$selectgenes <- renderUI({
Danni Luo's avatar
Danni Luo committed
80 81 82 83 84 85 86 87
  symnames <- tbls()$glist
  textAreaInput(
    "genes",
    "Gene Symbols separated by ';'",
    value = paste(symnames, collapse = ";"),
    width = '95%',
    rows = 10
  )
Brandi Cantarel's avatar
Brandi Cantarel committed
88 89 90
})

output$dge.c <- DT::renderDataTable({
Danni Luo's avatar
Danni Luo committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  t1 <- tbls()$filt
  t1$symbol <-
    paste(
      "<a href=http://www.genecards.org/cgi-bin/carddisp.pl?gene=",
      t1$symbol,
      '>',
      t1$symbol,
      "</a>",
      sep = ''
    )
  t1$ensembl <-
    paste(
      "<a href=http://www.ensembl.org/Homo_sapiens/Gene/Summary?g=",
      t1$ensembl,
      '>',
      t1$ensembl,
      "</a>",
      sep = ''
    )
  t1
}, escape = FALSE, filter = 'top', options = list(
  lengthMenu = list(c(10, 25, 50, 100, -1), list('10', '25', '50', '100', 'All')),
  autoWidth = TRUE,
  columnDefs = list(list(width = '5%', targets = '0')),
  scrollX = TRUE
))
Brandi Cantarel's avatar
Brandi Cantarel committed
117

Danni Luo's avatar
Danni Luo committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
output$downloadC <- renderUI({
  if (!is.null(tbls()$filt)) {
    downloadButton('OutputFile', 'Download CSV')
  }
})
output$OutputFile <- downloadHandler(
  file <- paste(input$file, ".filt.txt", sep = ""),
  content = function(file) {
    write.table(
      tbls()$filt,
      file,
      quote = FALSE,
      row.names = FALSE,
      sep = '\t'
    )
  }
)
Brandi Cantarel's avatar
Brandi Cantarel committed
135
plotHeatmap <- reactive({
Danni Luo's avatar
Danni Luo committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  syms <- tbls()$glist
  ct2 <- cts[cts$SYMBOL %in% syms,]
  subset <- ct2[, 4:length(ct2)]
  row.names(subset) <- ct2$SYMBOL
  STREE <- hclust(dist(t(subset)))
  zscores <- scale(t(subset))
  ngenes <- length(colnames(zscores))
  textscale <- (1 / (ngenes/30))
  if (textscale > 1) {
    textscale <- 12
  }
  if (textscale < 0.1) {
    textscale <- 12
  }
  if (input$cluster == 2) {
    heatmap.2(
      zscores,
      col = bluered(100),
      #Rowv = as.dendrogram(STREE),
      Rowv = FALSE,
      Colv = FALSE,
      lwid = c(0.5,3),
      RowSideColors = col.blocks,
      dendrogram = 'row',
      cexCol = 1.1,
      srtRow = 45,
      srtCol = 45,
      trace = "none",
      margins = c(8, 7)
    )
  } else{
    heatmap.2(
      zscores,
      col = bluered(100),
      Rowv = as.dendrogram(STREE),
      RowSideColors = col.blocks,
      dendrogram = 'row',
      cexCol = 1.1,
      srtRow = 45,
      srtCol = 45,
      lwid = c(0.5,3),
      trace = "none",
      margins = c(8, 7)
    )
  }
  legend("top",
         legend = grpnames,
         col = rainbow(length(grpnames)),
         pch = 20)
Brandi Cantarel's avatar
Brandi Cantarel committed
185 186
})
output$plot.heatmap <- renderPlot({
Danni Luo's avatar
Danni Luo committed
187
  plotHeatmap()
Brandi Cantarel's avatar
Brandi Cantarel committed
188
})
Danni Luo's avatar
Danni Luo committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

output$dlheatmap <- renderUI({
  if (!is.null(tbls()$glist)) {
    downloadButton('Downloadhp', 'Download PNG')
  }
})

output$Downloadhp <- downloadHandler(
  file = function() {
    paste('mean', 'png', sep = ".")
  },
  content <- function(file) {
    png(
      file,
      width = 8 * 140,
      height = 4 * 100,
      res = 140,
      pointsize = 5
    )
    if (is.null(tbls()$glist)) {
      return()
    }
    syms <- tbls()$glist
    ct2 <- cts[cts$SYMBOL %in% syms,]
    subset <- ct2[, 4:length(ct2)]
    row.names(subset) <- ct2$SYMBOL
    STREE <- hclust(dist(t(subset)))
    zscores <- scale(t(subset))
    ngenes <- length(colnames(zscores))
    textscale <- (1 / (ngenes/30))
    if (textscale > 1) {
      textscale <- 12
    }
    if (textscale < 0.1) {
      textscale <- 12
    }
    if (input$cluster == 2) {
      plot <- heatmap.2(
        zscores,
        col = bluered(100),
        Rowv = FALSE,
        Colv = FALSE,
        lwid = c(0.5,3),
        RowSideColors = col.blocks,
        dendrogram = 'row',
        cexCol = 1.1,
        srtRow = 45,
        srtCol = 45,
        trace = "none",
        margins = c(8, 7)
      )
    } else{
     plot <- heatmap.2(
        zscores,
        col = bluered(100),
        Rowv = as.dendrogram(STREE),
        RowSideColors = col.blocks,
        dendrogram = 'row',
        cexCol = 1.1,
        srtRow = 45,
        srtCol = 45,
        lwid = c(0.5,3),
        trace = "none",
        margins = c(8, 7)
      )
    }
    legend("top",
           legend = grpnames,
           col = rainbow(length(grpnames)),
           pch = 20)
    print(plot)
    dev.off()
  },
  contentType = "image/png"
)

Brandi Cantarel's avatar
Brandi Cantarel committed
265
output$downloadpdf = downloadHandler(
Danni Luo's avatar
Danni Luo committed
266 267 268 269 270 271 272
  filename = "output.heatmap.pdf",
  content = function(file) {
    pdf(file = file, paper = "letter")
    plotHeatmap()
    dev.off()
  }
)
Brandi Cantarel's avatar
Brandi Cantarel committed
273 274

output$hm.comp <- renderImage({
Danni Luo's avatar
Danni Luo committed
275 276 277 278 279 280 281 282 283
  f1 <- paste(tbls()$f1)
  png <- gsub('edgeR.txt', 'heatmap.edgeR.png', f1)
  list(
    src = png,
    alt = paste("HeatMap Comparison"),
    width = "100%",
    height = "auto"
  )
}, deleteFile = FALSE)
Brandi Cantarel's avatar
Brandi Cantarel committed
284

Danni Luo's avatar
Danni Luo committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
output$dlhmcomp <- renderUI({
  if (!is.null(tbls()$f1)) {
    downloadButton('Downloadhm', 'Download PNG')
  }
})
output$Downloadhm <- downloadHandler(
  file = function() {
    paste('heatmap_comp', 'png', sep = ".")
  },
  content <- function(file) {
    f1 <- paste(tbls()$f1)
    png <- gsub('edgeR.txt', 'heatmap.edgeR.png', f1)
    file.copy(png, file)
  }
)

output$hmcomp.allgene <- renderText({
  paste(tbls()$allgene)
})
output$hmcomp.top50 <- renderText({
  paste(tbls()$top50)
})
output$hmcomp.desc <- renderText({
  paste(tbls()$hmcomp)
})