Skip to content

Commit

Permalink
fix(visualizeNetwork): Update visualizeNetwork to only execute when i…
Browse files Browse the repository at this point in the history
…nterative() is true (#14)

* fix(visualizeNetwork): Update visualizeNetwork to only execute when interative() is true

* update unit tests for non-interactive code block
  • Loading branch information
tonywu1999 authored Sep 4, 2024
1 parent 82f9b29 commit 2cce44c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 64 deletions.
14 changes: 7 additions & 7 deletions R/getSubnetworkFromIndra.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
#' "extdata/groupComparisonModel.csv",
#' package = "MSstatsBioNet"
#' ))
#' # subnetwork = getSubnetworkFromIndra(input, pvalue_cutoff = 0.05)
#' # head(subnetwork$nodes)
#' # head(subnetwork$edges)
#' subnetwork <- getSubnetworkFromIndra(input, pvalue_cutoff = 0.05)
#' head(subnetwork$nodes)
#' head(subnetwork$edges)
#'
getSubnetworkFromIndra <- function(input, pvalue_cutoff = NULL) {
input <- .filterGetSubnetworkFromIndraInput(input, pvalue_cutoff)
res <- .callIndraCogexApi(input$HgncId)
nodes <- .constructNodesDataFrame(input)
edges <- .constructEdgesDataFrame(res, input)
warning(
"NOTICE: This function includes third-party software components
that are licensed under the BSD 2-Clause License. Please ensure to
include the third-party licensing agreements if redistributing this
package or utilizing the results based on this package.
"NOTICE: This function includes third-party software components
that are licensed under the BSD 2-Clause License. Please ensure to
include the third-party licensing agreements if redistributing this
package or utilizing the results based on this package.
See the LICENSE file for more details."
)
return(list(nodes = nodes, edges = edges))
Expand Down
59 changes: 32 additions & 27 deletions R/visualizeNetworks.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#' Create visualization of subnetwork in cytoscape
#' Create visualization of network in Cytoscape Desktop. Note that the
#' Cytoscape Desktop app must be open for this function to work.
#'
#' @param nodes dataframe of nodes
#' @param edges dataframe of edges
Expand All @@ -16,44 +17,48 @@
#' "extdata/groupComparisonModel.csv",
#' package = "MSstatsBioNet"
#' ))
#' # subnetwork = getSubnetworkFromIndra(input)
#' # visualizeNetworks(subnetwork$nodes, subnetwork$edges)
#' subnetwork <- getSubnetworkFromIndra(input)
#' visualizeNetworks(subnetwork$nodes, subnetwork$edges)
#'
#' @return cytoscape visualization of subnetwork
#'
#'
visualizeNetworks <- function(nodes, edges,
pvalue_cutoff = 0.05, logfc_cutoff = 0.5) {
pvalue_cutoff = 0.05, logfc_cutoff = 0.5) {
# Add additional columns for visualization
nodes$logFC_color <- nodes$logFC
nodes$logFC_color[nodes$pvalue > pvalue_cutoff |
abs(nodes$logFC) < logfc_cutoff] <- 0

# Create network
createNetworkFromDataFrames(nodes, edges)
if (interactive()) {
createNetworkFromDataFrames(nodes, edges)

# Apply visual style
DEFAULT_VISUAL_STYLE <- list(
NODE_SHAPE = "ROUNDRECT",
NODE_SIZE = 50,
NODE_LABEL_FONT_SIZE = 6,
NODE_LABEL_POSITION = "center",
EDGE_TARGET_ARROW_SHAPE = "Arrow"
)
VISUAL_STYLE_NAME <- "MSstats-Indra Visual Style"
# Apply visual style
DEFAULT_VISUAL_STYLE <- list(
NODE_SHAPE = "ROUNDRECT",
NODE_SIZE = 50,
NODE_LABEL_FONT_SIZE = 6,
NODE_LABEL_POSITION = "center",
EDGE_TARGET_ARROW_SHAPE = "Arrow"
)
VISUAL_STYLE_NAME <- "MSstats-Indra Visual Style"

VISUAL_STYLE_MAPPINGS <- list(
mapVisualProperty("Node Label", "id", "p"),
mapVisualProperty(
"Node Fill Color", "logFC_color", "c",
c(-logfc_cutoff, 0.0, logfc_cutoff),
c("#5588DD", "#5588DD", "#D3D3D3", "#DD8855", "#DD8855")
VISUAL_STYLE_MAPPINGS <- list(
mapVisualProperty("Node Label", "id", "p"),
mapVisualProperty(
"Node Fill Color", "logFC_color", "c",
c(-logfc_cutoff, 0.0, logfc_cutoff),
c("#5588DD", "#5588DD", "#D3D3D3", "#DD8855", "#DD8855")
)
)
createVisualStyle(
VISUAL_STYLE_NAME,
DEFAULT_VISUAL_STYLE,
VISUAL_STYLE_MAPPINGS
)
)
createVisualStyle(
VISUAL_STYLE_NAME,
DEFAULT_VISUAL_STYLE,
VISUAL_STYLE_MAPPINGS
)
setVisualStyle(VISUAL_STYLE_NAME)
setVisualStyle(VISUAL_STYLE_NAME)
} else {
warning("Visualization is not available in non-interactive mode.")
}
}
6 changes: 3 additions & 3 deletions man/getSubnetworkFromIndra.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions man/visualizeNetworks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tests/testthat/test-getSubnetworkFromIndra.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("getSubnetworkFromIndra works correctly", {
local_mocked_bindings(.callIndraCogexApi = function(x) {
return(readRDS(system.file("extdata/indraResponse.rds", package = "MSstatsBioNet")))
})
subnetwork <- getSubnetworkFromIndra(input)
suppressWarnings(subnetwork <- getSubnetworkFromIndra(input))
expect_equal(nrow(subnetwork$nodes), 7)
expect_equal(nrow(subnetwork$edges), 2)
})
Expand All @@ -17,7 +17,8 @@ test_that("getSubnetworkFromIndra with pvalue filter works correctly", {
local_mocked_bindings(.callIndraCogexApi = function(x) {
return(readRDS(system.file("extdata/indraResponse.rds", package = "MSstatsBioNet")))
})
subnetwork <- getSubnetworkFromIndra(input, pvalue_cutoff = 0.45)
suppressWarnings(
subnetwork <- getSubnetworkFromIndra(input, pvalue_cutoff = 0.45))
expect_equal(nrow(subnetwork$nodes), 6)
expect_equal(nrow(subnetwork$edges), 2)
})
26 changes: 26 additions & 0 deletions tests/testthat/test-visualizeNetworks.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ test_that("visualizeNetworks works correctly", {
package = "MSstatsBioNet"
))

mock_interactive <- mock(TRUE)
stub(
visualizeNetworks, "interactive",
mock_interactive
)
mock_createNetworkFromDataFrames <- mock()
stub(
visualizeNetworks, "createNetworkFromDataFrames",
Expand Down Expand Up @@ -37,6 +42,11 @@ test_that("visualizeNetworks with p-value and logFC constraints works", {
package = "MSstatsBioNet"
))

mock_interactive <- mock(TRUE)
stub(
visualizeNetworks, "interactive",
mock_interactive
)
mock_createNetworkFromDataFrames <- mock()
stub(
visualizeNetworks, "createNetworkFromDataFrames",
Expand Down Expand Up @@ -72,3 +82,19 @@ test_that("visualizeNetworks with p-value and logFC constraints works", {
)
expect_equal(nodes[input$nodes$id == "BRD4_HUMAN", ]$logFC_color, 0)
})

test_that("visualizeNetworks returns warning for non-interactive calls", {
input <- readRDS(system.file("extdata/subnetwork.rds",
package = "MSstatsBioNet"
))

mock_interactive <- mock(FALSE)
stub(
visualizeNetworks, "interactive",
mock_interactive
)

expect_warning(visualizeNetworks(input$nodes, input$edges,
pvalue_cutoff = 0.01, logfc_cutoff = 2.5
))
})
50 changes: 29 additions & 21 deletions vignettes/MSstatsBioNet.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,31 @@ networks. The package is designed to be used in conjunction with the

```{r}
library(MSstatsConvert)
input = system.file("tinytest/raw_data/Metamorpheus/AllQuantifiedPeaks.tsv",
package = "MSstatsConvert")
input = data.table::fread(input)
annot = system.file("tinytest/raw_data/Metamorpheus/Annotation.tsv",
package = "MSstatsConvert")
annot = data.table::fread(annot)
msstats_imported = MetamorpheusToMSstatsFormat(input, annotation = annot,
use_log_file = FALSE)
input <- system.file("tinytest/raw_data/Metamorpheus/AllQuantifiedPeaks.tsv",
package = "MSstatsConvert"
)
input <- data.table::fread(input)
annot <- system.file("tinytest/raw_data/Metamorpheus/Annotation.tsv",
package = "MSstatsConvert"
)
annot <- data.table::fread(annot)
msstats_imported <- MetamorpheusToMSstatsFormat(input,
annotation = annot,
use_log_file = FALSE
)
head(msstats_imported)
```

# MSstats Process and GroupComparison

```{r}
library(MSstats)
QuantData = dataProcess(msstats_imported, use_log_file = FALSE)
groupComparisonResult = groupComparison(contrast.matrix="pairwise",
data=QuantData,
use_log_file = FALSE)
QuantData <- dataProcess(msstats_imported, use_log_file = FALSE)
groupComparisonResult <- groupComparison(
contrast.matrix = "pairwise",
data = QuantData,
use_log_file = FALSE
)
```

# MSstatsBioNet Analysis
Expand All @@ -65,7 +71,7 @@ In the future, this uniprot client will be added as a separate function to allow
users to perform this conversion in R in a streamlined way.

```{r}
uniprot_to_hgnc_mapping = c(
uniprot_to_hgnc_mapping <- c(
"B9A064" = "38476",
"O00391" = "9756",
"O14818" = "9536",
Expand All @@ -74,7 +80,7 @@ uniprot_to_hgnc_mapping = c(
"P16050" = "11390",
"P84243" = "4765"
)
groupComparisonResult$ComparisonResult$HgncId = uniprot_to_hgnc_mapping[
groupComparisonResult$ComparisonResult$HgncId <- uniprot_to_hgnc_mapping[
groupComparisonResult$ComparisonResult$Protein
]
```
Expand All @@ -83,19 +89,21 @@ groupComparisonResult$ComparisonResult$HgncId = uniprot_to_hgnc_mapping[

The package provides a function `getSubnetworkFromIndra` that retrieves a
subnetwork of proteins from the INDRA database based on differential abundance
analysis results. The function `visualizeNetworks` then takes the output
of `getSubnetworkFromIndra` and visualizes the subnetwork.

```{r proteinNetworkDiscovery, eval=FALSE}
analysis results.

subnetwork = getSubnetworkFromIndra(groupComparisonResult$ComparisonResult)
```{r}
subnetwork <- getSubnetworkFromIndra(groupComparisonResult$ComparisonResult)
```

This package is distributed under the [Artistic-2.0](https://opensource.org/licenses/Artistic-2.0) license. However, its dependencies may have different licenses. In this example, getSubnetworkFromIndra depends on INDRA, which is distributed under the [BSD 2-Clause](https://opensource.org/license/bsd-2-clause) license. Furthermore, INDRA's knowledge sources may have different licenses for commercial applications. Please refer to the [INDRA README](https://github.com/sorgerlab/indra?tab=readme-ov-file#indra-modules) for more information on its knowledge sources and their associated licenses.


## Visualize Networks

```{r visualizeNetwork, eval=FALSE}
The function `visualizeNetworks` then takes the output of
`getSubnetworkFromIndra` and visualizes the subnetwork.

```{r}
visualizeNetworks(subnetwork$nodes, subnetwork$edges)
```

Expand All @@ -105,4 +113,4 @@ visualizeNetworks(subnetwork$nodes, subnetwork$edges)

```{r}
sessionInfo()
```
```

0 comments on commit 2cce44c

Please # to comment.