diff --git a/NAMESPACE b/NAMESPACE index c73e7ec..397697f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,6 +36,7 @@ export(dataProcessPlots) export(designSampleSize) export(designSampleSizePlots) export(extractSDRF) +export(fetchIndraData) export(getProcessed) export(getSamplesInfo) export(getSelectedProteins) @@ -46,6 +47,7 @@ export(modelBasedQCPlots) export(quantification) export(savePlot) export(theme_msstats) +export(visualizeNetworks) import(data.table) import(ggplot2) import(limma) @@ -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) @@ -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) @@ -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) diff --git a/R/networkPlots.R b/R/networkPlots.R new file mode 100644 index 0000000..cdf61ff --- /dev/null +++ b/R/networkPlots.R @@ -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") + +} \ No newline at end of file