## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>"
)

## ----eval = FALSE-------------------------------------------------------------
# if (!require("BiocManager", quietly = TRUE)) {
#     install.packages("BiocManager")
# }
# 
# BiocManager::install("MSstatsBioNet")

## -----------------------------------------------------------------------------
input = data.table::fread(system.file(
    "extdata/panitchpakdi-2022.csv",
    package = "MSstatsBioNet"
))
knitr::kable(head(input[, .(Analyte, Label, log2FC, adj.pvalue, issue)]))

## -----------------------------------------------------------------------------
input[grepl("diphenhydramine|DIP_", Analyte, ignore.case = TRUE),
      .(Analyte, log2FC, adj.pvalue)]

## -----------------------------------------------------------------------------
table(input$issue, useNA = "ifany")

## -----------------------------------------------------------------------------
library(MSstatsBioNet)
input$entity_type <- ifelse(input$Analyte == "Diphenhydramine",
                            "drug", "metabolite")
entities <- prepare_entities(input,
                             id_column = "Analyte",
                             entity_type = "entity_type",
                             id_type = "chemical_name")
knitr::kable(head(entities[, c("id", "entity_type", "id_type", "logFC",
                               "adj.pvalue")]))

## -----------------------------------------------------------------------------
indra <- indra_backend()
backend_capabilities(indra)$id_conversions

## ----include = FALSE----------------------------------------------------------
saved <- readRDS(system.file(
    "extdata/panitchpakdi-2022-network.rds",
    package = "MSstatsBioNet"
))

## ----eval = FALSE-------------------------------------------------------------
# entities <- convert_ids(indra, entities)

## ----include = FALSE----------------------------------------------------------
entities <- saved$entities

## -----------------------------------------------------------------------------
knitr::kable(entities[, c("id", "namespace", "entity_id", "entity_name")])

## -----------------------------------------------------------------------------
table(grounded = !is.na(entities$entity_id))

## -----------------------------------------------------------------------------
selected <- select_entities(entities, pvalue_cutoff = 0.05)
table(selected$included_in_query)

## ----eval = FALSE-------------------------------------------------------------
# network <- get_network(indra, selected, subnetwork_query())
# #> Dropping 4 row(s) with no entity grounding (NA entity_id).
# #> INDRA subnetwork: how are 7 selected entities connected to each other, with no other nodes added?

## ----include = FALSE----------------------------------------------------------
network <- saved$network_compounds

## -----------------------------------------------------------------------------
knitr::kable(network$nodes[, c("id", "entity_name", "namespace",
                               "entity_id", "logFC")])
knitr::kable(network$edges[, c("source", "target", "interaction",
                               "evidence_count")])

## -----------------------------------------------------------------------------
proteins <- c("HGNC:5182", "HGNC:2625", "HGNC:7967")

## ----eval = FALSE-------------------------------------------------------------
# network <- get_network(indra, selected, subnetwork_query(),
#                        include_entities = proteins)
# #> Dropping 4 row(s) with no entity grounding (NA entity_id).
# #> INDRA subnetwork: how are 7 selected entities and 3 added entities connected to each other, with no other nodes added?

## ----include = FALSE----------------------------------------------------------
network <- saved$network

## -----------------------------------------------------------------------------
knitr::kable(network$nodes[, c("id", "entity_type", "measured", "logFC")])
knitr::kable(head(network$edges[, c("source", "target", "interaction",
                                    "evidence_count")], 10))

## -----------------------------------------------------------------------------
validate_network(network)

## -----------------------------------------------------------------------------
selected_inf <- select_entities(entities, pvalue_cutoff = 0.05,
                                include_infinite_fc = TRUE)
table(selected_inf$included_in_query)

## ----eval = FALSE-------------------------------------------------------------
# network_inf <- get_network(indra, selected_inf, subnetwork_query(),
#                            include_entities = proteins)
# #> Dropping 4 row(s) with no entity grounding (NA entity_id).
# #> INDRA subnetwork: how are 21 selected entities and 3 added entities connected to each other, with no other nodes added?

## ----include = FALSE----------------------------------------------------------
network_inf <- saved$network_inf

## -----------------------------------------------------------------------------
setdiff(network_inf$nodes$id, network$nodes$id)

## -----------------------------------------------------------------------------
cytoscapeNetwork(network$nodes, network$edges,
                 displayLabelType = "entity_name")

## ----eval = FALSE-------------------------------------------------------------
# previewNetworkInBrowser(network$nodes, network$edges,
#                         displayLabelType = "entity_name")

## -----------------------------------------------------------------------------
sessionInfo()

