## ----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) ## rowGraph (with node attribute `degree`) rg <- graph_from_adjacency_matrix( cor(t(mat)), mode = "undirected", weighted = TRUE ) rg <- set_vertex_attr(rg, "degree", value = strength(rg)) rg ## colGraph cg <- graph_from_adjacency_matrix( cor(mat), mode = "undirected", weighted = TRUE ) ## ----create_ge---------------------------------------------------------------- # Create a `GraphExperiment` object ge <- GraphExperiment( assays = list(counts = mat), rowData = rdata, colData = cdata, rowGraphs = list(gene_cor = rg), colGraphs = list(cell_cor = cg) ) ge ## ----error_missing_from_graph, error = TRUE----------------------------------- try({ # Remove 'gene1' to 'gene10' from the rowGraph and try to recreate object rg2 <- delete_vertices(rg, paste0("gene", 1:10)) GraphExperiment( assays = list(counts = mat), rowData = rdata, colData = cdata, rowGraphs = list(gene_cor = rg2) ) }) ## ----coerce_se---------------------------------------------------------------- # Coercing from `SummarizedExperiment` se <- SummarizedExperiment(list(counts = mat)) ge1 <- as(se, "GraphExperiment") ge1 ## ----graphNames--------------------------------------------------------------- # Get rowGraph names rowGraphNames(ge) # 'gene_cor' rowGraphNames(ge1) # empty (NULL) # Get colGraph names colGraphNames(ge) # 'cell_cor' colGraphNames(ge1) # empty (NULL) ## ----getters------------------------------------------------------------------ # Get rowGraphs rowGraphs(ge) # Get colGraphs colGraphs(ge) # Get first rowGraph by index rowGraph(ge, 1) # Get first rowGraph by index (alternative) rowGraphs(ge)[[1]] # Get graph by name rowGraph(ge, "gene_cor") ## ----rowdata_getter----------------------------------------------------------- # `rowGraphs` and `rowData` are always in sync! rowData(ge) # `colGraphs` and `colData` too - yay! colGraph(ge, 1) # note the `cell_type` attribute extracted from `colData` ## ----graph_setter------------------------------------------------------------- # Create a new rowGraph without correlations between -0.4 and 0.4 rg_filt <- rowGraph(ge, "gene_cor") |> delete_vertex_attr("pathway") |> delete_vertex_attr("degree") |> delete_vertex_attr("coding") todelete <- abs(E(rg_filt)$weight) <0.4 rg_filt <- delete_edges(rg_filt, which(todelete)) rg_filt # Add filtered graph a new graph named `fcor` rowGraph(ge, "filt_genecor") <- rg_filt ge ## ----graphs_setter------------------------------------------------------------ # Taking a quick look (note: nothing in `rowGraphs`/`colGraphs`) ge1 # Adding graphs from `ge` rowGraphs(ge1) <- rowGraphs(ge) colGraphs(ge1) <- colGraphs(ge) ge1 ## ----graphNames_setter-------------------------------------------------------- # Rename graphs rowGraphNames(ge1) <- c("correlations", "correlations_filtered_0.4") colGraphNames(ge1) <- c("cell_correlations") ge1 ## ----subset------------------------------------------------------------------- # Subsetting `GraphExperiment` object ge_subset <- ge[1:10, 1:10] ge_subset rowGraph(ge_subset, "gene_cor") ## ----session_info------------------------------------------------------------- sessioninfo::session_info()