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

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

## -----------------------------------------------------------------------------
ptm = data.table::fread(system.file(
    "extdata/cptac-ccrcc-ptm-adjusted.csv",
    package = "MSstatsBioNet"
))
global = data.table::fread(system.file(
    "extdata/cptac-ccrcc-protein.csv",
    package = "MSstatsBioNet"
))
knitr::kable(head(ptm, 3))
knitr::kable(head(global, 3))

## -----------------------------------------------------------------------------
table(Adjusted = ptm$Adjusted)

## -----------------------------------------------------------------------------
library(MSstatsBioNet)
sites <- prepare_entities(ptm, entity_type = "ptm_site", id_type = "uniprot")
proteins <- prepare_entities(global, entity_type = "protein",
                             id_type = "uniprot")
knitr::kable(head(sites[, c("id", "parent_id", "site", "logFC",
                            "adj.pvalue")], 3))

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

## ----eval = FALSE-------------------------------------------------------------
# indra <- indra_backend()
# sites <- convert_ids(indra, sites)
# proteins <- convert_ids(indra, proteins)

## ----include = FALSE----------------------------------------------------------
sites <- saved$sites
proteins <- saved$proteins

## -----------------------------------------------------------------------------
knitr::kable(head(sites[, c("id", "namespace", "entity_id",
                            "entity_name")], 3))
c(sites = sum(!is.na(sites$entity_id)), of = nrow(sites))
c(proteins = sum(!is.na(proteins$entity_id)), of = nrow(proteins))

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

## ----eval = FALSE-------------------------------------------------------------
# network_sites <- get_network(indra, selected, subnetwork_query(),
#                              interaction_types = "Phosphorylation")
# #> INDRA subnetwork: how are 236 selected PTM sites connected to each other, with no other nodes added?

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

## -----------------------------------------------------------------------------
nrow(network_sites$edges)

## -----------------------------------------------------------------------------
filtered_sites <- filter_by_ptm_site(network_sites)
knitr::kable(filtered_sites$edges[, c("source", "target", "interaction",
                                      "site", "evidence_count")])

## -----------------------------------------------------------------------------
kinases <- c(MTOR = "HGNC:3942", AKT1 = "HGNC:391", MAPK1 = "HGNC:6871",
             MAPK3 = "HGNC:6877", CDK1 = "HGNC:1722", CSNK2A1 = "HGNC:2457",
             PRKACA = "HGNC:9380", GSK3B = "HGNC:4617", SRC = "HGNC:11283",
             PRKCA = "HGNC:9393")

## ----eval = FALSE-------------------------------------------------------------
# network <- get_network(indra, selected, subnetwork_query(),
#                        interaction_types = "Phosphorylation",
#                        include_entities = unname(kinases))
# #> INDRA subnetwork: how are 236 selected PTM sites and 10 added entities connected to each other, with no other nodes added?

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

## -----------------------------------------------------------------------------
nrow(network$edges)
network <- filter_by_ptm_site(network)
knitr::kable(network$edges[, c("source", "target", "site",
                               "evidence_count")])

## -----------------------------------------------------------------------------
proteins <- select_entities(proteins, pvalue_cutoff = 0.05)
network <- add_entity_values(network, proteins)
protein_rows <- network$nodes[is.na(network$nodes$site), ]
knitr::kable(protein_rows[, c("entity_name", "node_role", "measured",
                              "logFC", "adj.pvalue")])

## -----------------------------------------------------------------------------
site_rows <- network$nodes[!is.na(network$nodes$site), ]
knitr::kable(site_rows[, c("entity_name", "site", "logFC", "adj.pvalue")])

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

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

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

