Skip to content

Commit

Permalink
feat(indra): Fetch networks from indra cogex and visualize with cytos…
Browse files Browse the repository at this point in the history
…cape
  • Loading branch information
Tony Wu committed Jun 21, 2024
1 parent 3cb49a7 commit 55138f0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
14 changes: 14 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export(dataProcessPlots)
export(designSampleSize)
export(designSampleSizePlots)
export(extractSDRF)
export(fetchIndraData)
export(getProcessed)
export(getSamplesInfo)
export(getSelectedProteins)
Expand All @@ -46,6 +47,7 @@ export(modelBasedQCPlots)
export(quantification)
export(savePlot)
export(theme_msstats)
export(visualizeNetworks)
import(data.table)
import(ggplot2)
import(limma)
Expand All @@ -57,12 +59,17 @@ importFrom(MSstatsConvert,MSstatsImport)
importFrom(MSstatsConvert,MSstatsLogsSettings)
importFrom(MSstatsConvert,MSstatsMakeAnnotation)
importFrom(MSstatsConvert,MSstatsPreprocess)
importFrom(RCy3,createNetworkFromDataFrames)
importFrom(RCy3,createVisualStyle)
importFrom(RCy3,mapVisualProperty)
importFrom(RCy3,setVisualStyle)
importFrom(Rcpp,sourceCpp)
importFrom(data.table,as.data.table)
importFrom(data.table,melt)
importFrom(data.table,rbindlist)
importFrom(data.table,setDT)
importFrom(data.table,uniqueN)
importFrom(dplyr,filter)
importFrom(ggrepel,geom_text_repel)
importFrom(gplots,heatmap.2)
importFrom(grDevices,dev.off)
Expand All @@ -79,6 +86,10 @@ importFrom(graphics,title)
importFrom(htmltools,div)
importFrom(htmltools,save_html)
importFrom(htmltools,tagList)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(jsonlite,toJSON)
importFrom(limma,squeezeVar)
importFrom(lme4,lmer)
importFrom(marray,maPalette)
Expand All @@ -94,6 +105,9 @@ importFrom(plotly,plot_ly)
importFrom(plotly,style)
importFrom(plotly,subplot)
importFrom(preprocessCore,normalize.quantiles)
importFrom(r2r,hashmap)
importFrom(r2r,keys)
importFrom(r2r,query)
importFrom(stats,dist)
importFrom(stats,fitted)
importFrom(stats,formula)
Expand Down
98 changes: 98 additions & 0 deletions R/networkPlots.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

#' Fetch data on a set of proteins from INDRA
#'
#' @param input groupComparison comparisionResult table
#' @param pvalue_cutoff p-value cutoff for filtering
#' @param stmt_types types of statements to filter, default is "Complex"
#' @importFrom jsonlite toJSON
#' @importFrom httr POST add_headers content
#' @importFrom r2r hashmap query keys
#' @importFrom dplyr filter
#'
#' @export
#'
#' @noRd
#'
#'
fetchIndraData = function(input, pvalue_cutoff = 0.05, stmt_types = c("Complex")) {
input = filter(input, adj.pvalue < pvalue_cutoff)
input = filter(input, is.na(issue))
hgnc_ids = input$HgncId
uniprot_ids = input$Protein
gene_id_map = hashmap()
gene_id_map[input$HgncId] = input$HgncName
url = "https://discovery.indra.bio/api/indra_subnetwork_relations"
groundings = lapply(hgnc_ids, function(x) list("HGNC", x))
groundings = list(nodes = groundings)
json_body = jsonlite::toJSON(groundings, auto_unbox = TRUE)
res = POST(url, body = json_body, add_headers("Content-Type" = "application/json"), encode = "raw")
output = content(res)
output = Filter(function(x) x$data$stmt_type %in% stmt_types, output)

edge_data = hashmap()
for (edge in output) {
key = paste(edge$source_id, edge$target_id, edge$data$stmt_type, sep="_")
if (key %in% keys(edge_data)) {
edge_data[[key]]$data$evidence_count = edge_data[[key]]$data$evidence_count + edge$data$evidence_count
} else {
edge_data[[key]] = edge
}
}
evidenceList = sapply(keys(edge_data), function(x)
paste("https://db.indra.bio/statements/from_agents?subject=",
query(gene_id_map, query(edge_data, x)$source_id), "&object=",
query(gene_id_map, query(edge_data, x)$target_id), "&type=",
query(edge_data, x)$data$stmt_type, "&format=html", sep=""))


nodes = data.frame(id=hgnc_ids,
uniprot_id=uniprot_ids,
logFC=input$log2FC,
pvalue=input$adj.pvalue,
stringsAsFactors=FALSE)
edges = data.frame(source=sapply(keys(edge_data), function(x) query(edge_data, x)$source_id),
target=sapply(keys(edge_data), function(x) query(edge_data, x)$target_id),
interaction=sapply(keys(edge_data), function(x) query(edge_data, x)$data$stmt_type),
evidenceCount=sapply(keys(edge_data), function(x) query(edge_data, x)$data$evidence_count),
evidenceLink=evidenceList,
stringsAsFactors=FALSE)

return(list(nodes=nodes, edges=edges))
}

#' Create visualization of networks in cytoscape
#'
#' @param nodes dataframe of nodes
#' @param edges dataframe of edges
#' @importFrom RCy3 createNetworkFromDataFrames mapVisualProperty createVisualStyle setVisualStyle
#'
#' @export
#'
#' @noRd
#'
#'
visualizeNetworks = function(nodes, edges) {
network_id = createNetworkFromDataFrames(nodes, edges, title="my first network", collection="DataFrame Example")
arrowShapes = mapVisualProperty(
'Edge Target Arrow Shape','interaction',
'd',
c("Complex", "Activation", "Inhibition"),
c("Arrow","Arrow","Arrow")
)
nodeLabels = mapVisualProperty('Node Label','uniprot_id','p')
edgeWidth = mapVisualProperty('Edge Width','evidenceCount','p')
createVisualStyle("Y",
list(
NODE_FILL_COLOR="lightblue",
NODE_SHAPE="ROUNDRECT",
NODE_SIZE=50,
NODE_LABEL_FONT_SIZE=6,
NODE_LABEL_POSITION="center"),
list(
nodeLabels,
edgeWidth,
arrowShapes
))
setVisualStyle("Y")

}

0 comments on commit 55138f0

Please sign in to comment.