inst/app/code/server/s_OverlayOmics.R

output$test = eventReactive(
    eventExpr = input$label.spliting.string
    ,{
    print("testing")
    label.spliting.string = input$label.spliting.string
    print(label.spliting.string)
    return("testing")
})

# Combine the selected variables into a new data frame
# selectedData <- reactive({
#     iris[, c(input$xcol, input$ycol)]
# })

# clusters <- reactive({
#     kmeans(selectedData(), input$clusters)
# })
outfileEachString = "initial"
# outfileEach = observeEvent(
outfileEach = eventReactive(
    input$run.OmicsSBGN
    ,{
        # pathway.id = "P00002"
        outfile <- tempfile()
        tempDir = tempdir()
        data("gse16873.d")
        df <- read.table(input$file1$datapath,
                         header = input$header,
                         sep = input$sep,
                         as.is = TRUE,
                         quote = input$quote)
        
        gene.data = omicsGlyphIdTable()
        if(input$usePathways == 'UsePrePathways'){
            pathway.id = isolate(input$pathway.id)
            cat("\n\n\n using pre pathways \n\n\n")
            print("pathway.id is")
            print(pathway.id)
            # gene.id.type = NA
            gene.id.type = "pathwayCommons"
            sbgn.gene.id.type = NULL
            sbgn.dir = tempDir
        }else if(input$usePathways == 'userSBGN'){
            pathway.id = sbgn.info()$server.sbgn
            sbgn.dir = sbgn.info()$sbgn.dir
            gene.id.type = SBGN.glyph.id.type()
            # gene.id.type = input$OmicsIdType
            # sbgn.gene.id.type = SBGN.glyph.id.type()
            sbgn.gene.id.type = input$uploadedSbgnGlyphIdType
            if(!identical(gene.id.type,sbgn.gene.id.type)){
                stop("Omics ID type and SBGN-ML glyph ID types are different!\n
                     Please check the uploaded ID mapping file,
                     your input of 'SBGN-ML glyph ID type' and your selection of 'Omics molecule ID type'\n
                     'SBGN-ML glyph ID type' and 'Omics molecule ID type' 
                        must be in the column names of the uploaded ID mapping table.\n
                        Your inputs are: \n
                        SBGN-ML glyph ID type: ",sbgn.gene.id.type
                        ,"\n Omics molecule ID type: ", gene.id.type
                     )
            }
        }else if(input$usePathways == 'enrichAnalysis'){
            # pathway.id = isolate(selectedPathFromEnrich())
            pathway.id = isolate(input$selectedPathwaysSelector$right)
            # pathway.id = pathway.id[,1]
            cat("\n\n\n using enriched pathways \n\n\n")
            print("pathway.id is")
            print(pathway.id)
            # gene.id.type = NA
            gene.id.type = "pathwayCommons"
            sbgn.gene.id.type = NULL
            sbgn.dir = tempDir
        }
        
        cat("\n\n\n input data for sbgnview \n\n")
        print(pathway.id)
        print(sbgn.dir)
        cat("\n\n gene id type is: ",gene.id.type)
        cat("\n\n sbgn gene id type is: ",sbgn.gene.id.type)
        print(head(gene.data))
        cat("\n\n\n\n done printing data \n\n\n")
        all.inputs = names(input)
        
        SBGNview.obj <- SBGNview(
        # SBGNview(
              # gene.data = gse16873.d[,1:3],
                      
              gene.data = gene.data
              ,gene.id.type = gene.id.type
              # gene.id.type = input$OmicsIdType,
              ,org = input$org
              ,input.sbgn = pathway.id
              ,sbgn.dir = sbgn.dir
              ,SBGNview.data.folder = tempDir
              ,sbgn.gene.id.type = sbgn.gene.id.type
              # output.file = "shiny",
              ,output.file = outfile,
              output.formats = c("png"),
              ################
              show.pathway.name = FALSE,
                  color.panel.scale = input$color.panel.scale, 
                  space.between.color.panel.and.entity = input$space.between.color.panel.and.entity,
              col.gene.high = input$col.gene.high,
              col.gene.mid = input$col.gene.mid,
              col.gene.low = input$col.gene.low,
              max.gene.value = input$max.gene.value,
              min.gene.value = input$min.gene.value,
              mid.gene.value = input$mid.gene.value,
              
              node.sum = input$node.sum,
              label.spliting.string = gsub("no split", "", input$label.spliting.string),
              
                    edge.tip.size = input$edge.tip.size,
              inhibition.edge.end.shift = input$inhibition.edge.end.shift,
              
              font.size = input$font.size,
              logic.node.font.scale = input$logic.node.font.scale,
              
              text.length.factor.macromolecule = input$text.length.factor.macromolecule,
                    font.size.scale.gene = input$font.size.scale.gene,
              
              text.length.factor.complex = input$text.length.factor.complex,
                    font.size.scale.complex = input$font.size.scale.complex,
                    node.width.adjust.factor.complex = input$node.width.adjust.factor.complex,
                    if.scale.complex.font.size = input$ifScaleComplexFontSize,
              font.size.scale.compartment = input$font.size.scale.compartment,
              text.length.factor.compartment = input$text.length.factor.compartment,
                      complex.compartment.label.margin = input$complex.compartment.label.margin,
              
              if.scale.compartment.font.size = as.logical(input$ifScaleCompartmentFontSize),
              node.width.adjust.factor.compartment = input$node.width.adjust.factor.compartment,
              
                      compartment.opacity = input$compartment.opacity,
                    auxiliary.opacity = input$auxiliary.opacity  # opacity of auxiliary nodes
              )
        print(SBGNview.obj)
        out.image.svg <- paste0(outfile,"_",pathway.id,".svg")
        out.image.png <- paste0(outfile,"_",pathway.id,".png")
        print("file name is")
        print(out.image.svg)
        print("done printing file name")
        # Sys.chmod(outfileEachString,mode="0444")
        print("done setting read only")
        out.image.files = list(
            svg = out.image.svg
            ,png = out.image.png
            ,pathways = pathway.id
            ,SBGNview.obj = SBGNview.obj
        )
        cat("\n\n\n returned list is\n")
        # print(out.image.files)
        return(out.image.files)
    }
)

down.img.file = reactiveVal("")
current.view.pathway = reactiveVal("")
currentPathwayNumber = reactiveVal("")
output$currentViewPathway = renderText({
    currentPathwayNumber()
})
output$image <- renderImage({
    # Get width and height of image output
    out.file = outfileEach()
    if(input$usePathways == 'enrichAnalysis'){
        # i = input$nextImg - input$previousImg +1
        i = (input$nextImg - input$previousImg) %% length(out.file$svg) +1
        # if(i > length(out.file$svg)){
        #     i = length(out.file$svg)
        # }
        # if(i < 1){
        #     i = 1
        # }
        out.file.svg = out.file$svg[i]
        out.file.png = out.file$png[i]
        current.pathway = out.file$pathways[i]
        pathway.data = out.file$SBGNview.obj$data[[current.pathway]]
    }else {
        i = 1
        out.file.svg = out.file$svg
        out.file.png = out.file$png
        current.pathway = out.file$pathways
        pathway.data = out.file$SBGNview.obj$data[[1]]
    }
    width = pathway.data$svg.dim.x
    height = pathway.data$svg.dim.y 
    cat("svg file is")
    print(out.file.svg)
    down.img.file(out.file.svg)
    current.view.pathway(current.pathway)
    currentPathwayNumber(as.character(i))
    client.width = session$clientData$output_image_width 
    client.height = session$clientData$output_image_height
    width.out = client.width
    height.out = client.width / width * height
    if(height.out > client.height){
        height.out = client.height
        width.out = client.height /height * width
    }
            
    list(
        src = out.file.png,
        # src = outfileEachString,
        contentType = "image/png",
        width = width.out * input$zoom/800,
        height = height.out * input$zoom/800,
        alt = "This is alternate text"
    )
},deleteFile = FALSE)

output$downloadImg <- downloadHandler(
    filename = function() {
        # downloadNameVector[input$preData]
        "out.svg"
    },
    content = function(file) {
        # file.copy(input$preData, file)
        # out.file = outfileEach()
        # print(out.file$svg)
        file.copy(down.img.file(), file)
    }
)

output$downloadSBGN <- downloadHandler(
    filename = function() {
        # downloadNameVector[input$preData]
        "out.sbgn"
    },
    content = function(file) {
        # file.copy(input$preData, file)
        # out.file = outfileEach()
        cat("trying to download pathway SBGN")
        # pathway.id = isolate(input$pathway.id)
        # print(pathway.id)
        pathway.id =  current.view.pathway()
        
        out.file = download.sbgn.file(pathway.id)
        print(out.file)
        file.copy(out.file, file)
    }
)


# selectedPathForSBGNview = reactiveVal(c())
output$selectedPathwaysOverlayPage = renderUI({
    req(selectedPathFromEnrich())
    tagList(
        chooserInput(inputId = "selectedPathwaysSelector"
                     ,leftLabel = "Selected"
                     ,rightLabel = "Run SBGNview"
                     ,leftChoices = c()
                     ,rightChoices = selectedPathFromEnrich()[,1]
                     ,size = 10, multiple = TRUE
                     
        )
    )
})
chemokine/OmicsSBGN documentation built on June 27, 2019, 7:52 p.m.