highchartconverter.R 2.25 KB
Newer Older
fsoubes's avatar
fsoubes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
### Author: Franck Soubès
### Bioinformatics Master Degree - University of Bordeaux, France
### Link: https://github.com/fsoubes/MA_Trix_App
### Where: GET-TRiX's facility
### Application: MATRiX is a shiny application for Microarray Analysis on Transcriptomic impact of Xenobiotics
### Licence: GPL-3.0


DftoHighjson <- function(data, param) {
  #View(data)
  foldEnrichment <- param$search 
  tempData <- (data[grep(param$search,colnames(data))])
  colnames(tempData) <- paste0("y",1)
  tempData$id <- data$Category 
  tempData$Term <- data$Term
  tempData$pvalue <- data$Benjamini
  tempData$FE <- data$Fold.Enrichment 
  tempData$Topgenes <-  data$Top
  tempData$percent <- data$percent

fsoubes's avatar
fsoubes committed
21
  
fsoubes's avatar
fsoubes committed
22 23 24 25 26 27 28 29 30 31 32 33 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 72 73 74
  unifiedData <- reshape(tempData, varying=paste0("y",1), 
                      direction="long", idvar="Top",sep="",timevar="x")
  
  
  unifiedData <- unifiedData[order(unifiedData$id),]
  
  
  unifiedData$x <- as.numeric(as.character(unifiedData$FE))
  unifiedData$y <- as.numeric(unifiedData$Topgenes)
  unifiedData$z <-  as.numeric(as.character(unifiedData$percent))
  
  
  unifiedData$GO  = sapply(unifiedData$Term, FUN= function(x) if(grepl("^mmu", x)) return(strsplit(as.character(x), ":")%>% unlist() %>% .[1]) 
                        else return(strsplit(as.character(x), "~")%>% unlist() %>% .[1])) 
  
  unifiedData$Term  = sapply(unifiedData$Term, FUN= function(x) if(grepl("^mmu", x)) return(strsplit(as.character(x), ":")%>% unlist() %>% .[2]) 
                        else return(strsplit(as.character(x), "~")%>% unlist() %>% .[2])) 
  
  unifiedData$Pvalue =  format(tempData$pvalue, digits = 3)


  
  return(highchartsConvert(unifiedData))
}


formatPoint <- function(index) {
  return( list( x=index$x, y=index$y,z=index$z, GO=index$GO, term=index$Term, pvalue = index$Pvalue))
}


formatCategory <- function(dframeCategory) {
  
  categoryTemplate <- list(name="",data={})
  categoryTemplate$name <- dframeCategory$id[[1]]
  categoryTemplate$visible <- F

  dataPoints <- dlply(dframeCategory,.(1:nrow(dframeCategory)),formatPoint)
  names(dataPoints) <- NULL 
  categoryTemplate$data <- dataPoints
  
  return(categoryTemplate)
}



highchartsConvert <- function(DftoHighjson) {
  
  json <- dlply (DftoHighjson, .(id), formatCategory)
  names(json) <- NULL   
  return(json)
  
}