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

## ----installation, eval=FALSE-------------------------------------------------
# if(!requireNamespace('BiocManager', quietly = TRUE))
#   install.packages('BiocManager')
# 
# BiocManager::install("GraphExperiment")

## ----load_package, message=FALSE----------------------------------------------
# Load package after installation
library(GraphExperiment)

set.seed(777) # for reproducibility

## ----fig, echo=FALSE, out.width = "100%", fig.cap="The GraphExperiment class."----
knitr::include_graphics("GraphExperiment.png")

## ----simulate_slots, message=FALSE--------------------------------------------
# Simulate parts of a `GraphExperiment` object
## Assays
gene_ids <- paste0("gene", seq_len(200))
cell_ids <- paste0("cell", seq_len(100))
mat <- matrix(rpois(20000, 5), ncol = 100, dimnames = list(gene_ids, cell_ids))
mat[1:5, 1:5]

## rowData
rdata <- data.frame(
    row.names = gene_ids,
    pathway = sample(c("P1", "P2"), size = length(gene_ids), replace = TRUE),
    coding = sample(c(TRUE, FALSE), size = length(gene_ids), replace = TRUE)
)
head(rdata)

## colData
cdata <- data.frame(
    row.names = cell_ids, 
    cell_type = sample(c("ct1", "ct2"), size = length(cell_ids), replace = TRUE)
)
head(cdata)

## Graph (with node attribute `degree`)
g <- graph_from_adjacency_matrix(
    cor(t(mat)), mode = "undirected", weighted = TRUE
)
g <- set_vertex_attr(g, "degree", value = strength(g))
g

## ----create_ge----------------------------------------------------------------
# Create a `GraphExperiment` object
ge <- GraphExperiment(
    assays = list(counts = mat),
    rowData = rdata,
    colData = cdata,
    graphs = list(cor = g)
)
ge

## ----error_missing_from_graph, error = TRUE-----------------------------------
try({
# Remove 'gene1' to 'gene10' from the graph and try to recreate object
g2 <- delete_vertices(g, paste0("gene", 1:10))
GraphExperiment(
    assays = list(counts = mat),
    rowData = rdata,
    colData = cdata,
    graphs = list(cor = g2)
)
})

## ----coerce_se----------------------------------------------------------------
# Coercing from `SummarizedExperiment`
se <- SummarizedExperiment(list(counts = mat))
ge1 <- as(se, "GraphExperiment")
ge1

## ----graphNames---------------------------------------------------------------
# Get graph names
graphNames(ge)      # 'cor'
graphNames(ge1)     # empty (NULL)

## ----getters------------------------------------------------------------------
# Get graphs
graphs(ge)

# Get first graph by index
graph(ge, 1)

# Get first graph by index (alternative)
graphs(ge)[[1]]

# Get graph by name
graph(ge, "cor")

## ----rowdata_getter-----------------------------------------------------------
# `graphs` and `rowData` are always in sync!
rowData(ge)

## ----graph_setter-------------------------------------------------------------
# Create a new graph without correlations between -0.4 and 0.4
fg <- graph(ge, "cor") |> 
    delete_vertex_attr("pathway") |>
    delete_vertex_attr("degree") |>
    delete_vertex_attr("coding")

todelete <- abs(E(fg)$weight) <0.4
fg <- delete_edges(fg, which(todelete))
fg

# Add filtered graph a new graph named `fcor`
graph(ge, "fcor") <- fg
ge

## ----graphs_setter------------------------------------------------------------
# Taking a quick look (note: nothing in `graphs`)
ge1

# Adding graphs from `ge`
graphs(ge1) <- graphs(ge)
ge1

## ----graphNames_setter--------------------------------------------------------
# Rename graphs
graphNames(ge1) <- c("correlations", "correlations_filtered_0.4")
ge1

## ----subset-------------------------------------------------------------------
# Subsetting `GraphExperiment` object
ge_subset <- ge[1:10, ]

ge_subset
graph(ge_subset, "cor")

## ----session_info-------------------------------------------------------------
sessioninfo::session_info()

