# Make sure you run at least step 1 + step 2 before this
# the input for this optional step is one of the GO processes
# that you find in res.go.up.df or res.go.down.df
# the idea is that you find all genes that are associated
# with the process, make a PPI network, visualize the data 
# and find drug targets

# so instead of finding the module from the large network,
# you define the module based on a process of interest

# #############################################
# GET GO TERM
# #############################################

# Check if Cytoscape is running
cytoscapePing()
## You are connected to Cytoscape!
# example "DNA replication"
# look up GO ID in the enrichment result tables
genes <- res.go@geneSets[go.term]

query <- format_csv(as.data.frame(genes[1]), col_names=F, escape = "double", eol =",")
commandsRun(paste0('string protein query cutoff=0.9 newNetName="PPI network" query="',query,'" limit=0 species="Homo sapiens"'))
## [1] "Loaded network 'STRING network - PPI network - 2' with 324 nodes and 1024 edges"
analyzeNetwork()
##                                    networkTitle                                       nodeCount 
## "STRING network - PPI network - 2 (undirected)"                                           "324" 
##                                       edgeCount                                     avNeighbors 
##                                          "1024"                              "9.25925925925926" 
##                                        diameter                                          radius 
##                                             "8"                                             "4" 
##                                           avSpl                                              cc 
##                            "3.1602928509905253"                             "0.457825253422492" 
##                                         density                                   heterogeneity 
##                           "0.04306632213608958"                            "0.9927638188411179" 
##                                  centralization                                             ncc 
##                            "0.2053466637687459"                                            "94" 
##                                            time 
##                                          "0.01"
RCy3::loadTableData(data=data, data.key.column = "GeneName", table = "node", table.key.column = "query term")
## [1] "Success: Data loaded in defaultnode table"
RCy3::createVisualStyle("log2FC vis")
RCy3::setNodeLabelMapping("display name", style.name = "log2FC vis")
## NULL
control.points <- c (-3.0, 0.0, 3.0)
colors <-  c ('#5588DD', '#FFFFFF', '#DD8855')
setNodeColorMapping("log2FC", control.points, colors, style.name = "log2FC vis", default.color = "#C0C0C0")
## NULL
RCy3::setVisualStyle("log2FC vis")
##                 message 
## "Visual Style applied."
RCy3::lockNodeDimensions("TRUE", "log2FC vis")

# Select significant genes and change border color
x <- RCy3::createColumnFilter('P.Value', 'P.Value', 0.05, "LESS_THAN")
## No edges selected.
RCy3::setNodeBorderColorBypass(x$nodes, new.colors = "#009900")
RCy3::setNodeBorderWidthBypass(x$nodes, new.sizes = 7)
RCy3::clearSelection()

# ==================================================================
# Extend the process PPI with known drug-target interactions from DrugBank
# ==================================================================
unzip(system.file("extdata","drugbank-5.1.0.xgmml.zip", package="rWikiPathways"), exdir = getwd())
drugbank <- file.path(getwd(), "drugbank-5.1.0.xgmml")

commandsRun(paste0('cytargetlinker extend idAttribute="GeneID" linkSetFiles="', drugbank, '"') )
## [1] "Extension step: 1"                   "Linkset: DrugBank_v5.1.0 (approved)" "Added edges: 260"                   
## [4] "Added nodes: 136"
commandsRun('cytargetlinker applyLayout network="current"')

my.drugs <- selectNodes("drug", by.col = "CTL.Type", preserve = FALSE)$nodes #easy way to collect node SUIDs by column value
clearSelection()
setNodeColorBypass(my.drugs, "#DD99FF")
setNodeShapeBypass(my.drugs, "hexagon")

drug.labels <- getTableColumns(columns=c("SUID","CTL.label"))
drug.labels <- na.omit(drug.labels)
mapply(function(x,y) setNodeLabelBypass(x,y), drug.labels$SUID, drug.labels$CTL.label)
## [[1]]
## NULL
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## NULL
## 
## [[5]]
## NULL
## 
## [[6]]
## NULL
## 
## [[7]]
## NULL
## 
## [[8]]
## NULL
## 
## [[9]]
## NULL
## 
## [[10]]
## NULL
## 
## [[11]]
## NULL
## 
## [[12]]
## NULL
## 
## [[13]]
## NULL
## 
## [[14]]
## NULL
## 
## [[15]]
## NULL
## 
## [[16]]
## NULL
## 
## [[17]]
## NULL
## 
## [[18]]
## NULL
## 
## [[19]]
## NULL
## 
## [[20]]
## NULL
## 
## [[21]]
## NULL
## 
## [[22]]
## NULL
## 
## [[23]]
## NULL
## 
## [[24]]
## NULL
## 
## [[25]]
## NULL
## 
## [[26]]
## NULL
## 
## [[27]]
## NULL
## 
## [[28]]
## NULL
## 
## [[29]]
## NULL
## 
## [[30]]
## NULL
## 
## [[31]]
## NULL
## 
## [[32]]
## NULL
## 
## [[33]]
## NULL
## 
## [[34]]
## NULL
## 
## [[35]]
## NULL
## 
## [[36]]
## NULL
## 
## [[37]]
## NULL
## 
## [[38]]
## NULL
## 
## [[39]]
## NULL
## 
## [[40]]
## NULL
## 
## [[41]]
## NULL
## 
## [[42]]
## NULL
## 
## [[43]]
## NULL
## 
## [[44]]
## NULL
## 
## [[45]]
## NULL
## 
## [[46]]
## NULL
## 
## [[47]]
## NULL
## 
## [[48]]
## NULL
## 
## [[49]]
## NULL
## 
## [[50]]
## NULL
## 
## [[51]]
## NULL
## 
## [[52]]
## NULL
## 
## [[53]]
## NULL
## 
## [[54]]
## NULL
## 
## [[55]]
## NULL
## 
## [[56]]
## NULL
## 
## [[57]]
## NULL
## 
## [[58]]
## NULL
## 
## [[59]]
## NULL
## 
## [[60]]
## NULL
## 
## [[61]]
## NULL
## 
## [[62]]
## NULL
## 
## [[63]]
## NULL
## 
## [[64]]
## NULL
## 
## [[65]]
## NULL
## 
## [[66]]
## NULL
## 
## [[67]]
## NULL
## 
## [[68]]
## NULL
## 
## [[69]]
## NULL
## 
## [[70]]
## NULL
## 
## [[71]]
## NULL
## 
## [[72]]
## NULL
## 
## [[73]]
## NULL
## 
## [[74]]
## NULL
## 
## [[75]]
## NULL
## 
## [[76]]
## NULL
## 
## [[77]]
## NULL
## 
## [[78]]
## NULL
## 
## [[79]]
## NULL
## 
## [[80]]
## NULL
## 
## [[81]]
## NULL
## 
## [[82]]
## NULL
## 
## [[83]]
## NULL
## 
## [[84]]
## NULL
## 
## [[85]]
## NULL
## 
## [[86]]
## NULL
## 
## [[87]]
## NULL
## 
## [[88]]
## NULL
## 
## [[89]]
## NULL
## 
## [[90]]
## NULL
## 
## [[91]]
## NULL
## 
## [[92]]
## NULL
## 
## [[93]]
## NULL
## 
## [[94]]
## NULL
## 
## [[95]]
## NULL
## 
## [[96]]
## NULL
## 
## [[97]]
## NULL
## 
## [[98]]
## NULL
## 
## [[99]]
## NULL
## 
## [[100]]
## NULL
## 
## [[101]]
## NULL
## 
## [[102]]
## NULL
## 
## [[103]]
## NULL
## 
## [[104]]
## NULL
## 
## [[105]]
## NULL
## 
## [[106]]
## NULL
## 
## [[107]]
## NULL
## 
## [[108]]
## NULL
## 
## [[109]]
## NULL
## 
## [[110]]
## NULL
## 
## [[111]]
## NULL
## 
## [[112]]
## NULL
## 
## [[113]]
## NULL
## 
## [[114]]
## NULL
## 
## [[115]]
## NULL
## 
## [[116]]
## NULL
## 
## [[117]]
## NULL
## 
## [[118]]
## NULL
## 
## [[119]]
## NULL
## 
## [[120]]
## NULL
## 
## [[121]]
## NULL
## 
## [[122]]
## NULL
## 
## [[123]]
## NULL
## 
## [[124]]
## NULL
## 
## [[125]]
## NULL
## 
## [[126]]
## NULL
## 
## [[127]]
## NULL
## 
## [[128]]
## NULL
## 
## [[129]]
## NULL
## 
## [[130]]
## NULL
## 
## [[131]]
## NULL
## 
## [[132]]
## NULL
## 
## [[133]]
## NULL
## 
## [[134]]
## NULL
## 
## [[135]]
## NULL
## 
## [[136]]
## NULL
# Try different layouts (e.g. yFiles organic layout) if nodes are overlapping too much 
# Cytoscape > Layout menu!

exportImage(paste0(out.folder,'cluster-',cluster,'-with-drugs.png'), type='PNG', zoom=500) #.png; use zoom or width args to increase size/resolution
##                                                                                   file 
## "/home/peipr/gits/BBS2061-breast-cancer/scripts/../output/cluster-3283-with-drugs.png"
# ==================================================================
# SAVING CYTOSCAPE SESSION
# ==================================================================

saveSession(paste0(out.folder,'breast-cancer-GO-PPI.cys'))



## OPTIONAL - FILTER network to only include drugs + targets

selectNodes(my.drugs)
## $nodes
##   [1] 471797 472307 472049 471791 472061 471803 472313 472568 472325 472067 471809 471821 472331 472073 471815 472583
##  [17] 471830 472085 472079 472094 471581 471836 471575 472343 472358 472613 472100 471587 471842 472622 471854 472106
##  [33] 471593 471848 472118 471605 472112 471599 472124 471611 472379 472631 472646 471878 471620 472130 472385 471872
##  [49] 472142 472397 471884 471626 472136 472391 472403 471632 472412 471644 472154 471896 472166 471650 471905 472430
##  [65] 472172 471659 472682 472424 471911 472181 471668 471923 472700 472187 471674 472442 471686 472451 471938 472706
##  [81] 471680 471950 472202 472457 471944 472469 471701 472721 472208 472463 471695 472733 471707 472475 472217 472727
##  [97] 471959 471713 472223 471980 472235 472490 471722 472745 472502 471734 471986 472241 471728 472253 472508 471740
## [113] 471995 472262 471749 472514 472526 472013 471755 472520 472277 472019 471761 472271 471773 472283 472025 472535
## [129] 471767 472550 471782 472037 471521 472031 472556 472298
## 
## $edges
## list()
selectFirstNeighbors()
## $nodes
##   [1] 463349 472049 463344 472061 463354 472568 472067 463359 462859 472073 472583 472085 462869 462864 472079 472094
##  [17] 471581 463389 471575 472613 472100 471587 463394 462369 472622 472106 471593 462889 472118 463414 471605 472112
##  [33] 471599 472124 471611 463419 472631 472646 471620 472130 472142 462924 471626 463434 472136 462929 471632 462429
##  [49] 471644 472154 463449 472166 471650 463469 472172 462444 471659 472682 472181 471668 463474 462449 472700 472187
##  [65] 471674 461944 471686 463494 472706 471680 462464 472202 471701 462484 472721 472208 471695 472733 471707 461979
##  [81] 463514 472217 472727 471713 463009 472223 463519 472235 471722 472745 463529 471734 463029 472241 471728 461999
##  [97] 472253 463549 471740 463034 472262 471749 471755 463049 462024 472277 471761 472271 471773 472283 471767 471782
## [113] 472298 471797 472307 471791 462589 471803 462074 472313 462584 472325 462594 471809 471821 472331 471815 462599
## [129] 471830 463124 463134 471836 472343 472358 463139 471842 462624 471854 471848 472379 463159 471878 472385 471872
## [145] 472397 471884 463179 472391 472403 462674 462174 472412 462684 463194 471896 471905 472430 463209 472424 462184
## [161] 471911 462709 471923 462704 472442 462214 472451 471938 471950 462734 472457 471944 463254 472469 462229 472463
## [177] 472475 471959 463269 463264 471980 472490 472502 462774 463284 471986 463279 463294 472508 471995 462779 463289
## [193] 472514 472526 472013 472520 472019 472025 462809 472535 472550 472037 462309 471521 463329 472031 472556
## 
## $edges
## list()
selected <- getSelectedNodes()

createSubnetwork("selected", "all")
## network 
##  505460