MarioniLab / miloR

R package implementation of Milo for testing for differential abundance in KNN graphs
https://bioconductor.org/packages/release/bioc/html/miloR.html
GNU General Public License v3.0
316 stars 20 forks source link

makeNhoods graph refinement (issue with isolated vertices) #341

Closed mdpatric closed 1 week ago

mdpatric commented 1 week ago

Describe the bug While trying to import an SNN graph from a seurat object An Invalid vertex name(s) error seems to occur when the iGraph object has isolated vertices. image This causes an issue with .graph_refined_sampling <- function(random_vertices, graph){ message("Running refined sampling with graph") random_vertices <- as.vector(random_vertices) X_graph <- set_vertex_attr(graph, "name", value = 1:length(V(graph))) refined_vertices <- lapply(seq_along(random_vertices), function(i){ target_vertices <- unlist(neighborhood(X_graph, order = 1, nodes = random_vertices[i])) #get neighborhood of random vertex target_vertices <- target_vertices[-1] #remove first entry which is the random vertex itself rv_induced_subgraph <- induced_subgraph(graph = X_graph, vids = target_vertices) triangles <- count_triangles(rv_induced_subgraph) max_triangles <- max(triangles) max_triangles_indices <- which(triangles == max_triangles)

note - take first max_ego_index in the next line of code

resulting_vertices <- V(rv_induced_subgraph)[max_triangles_indices]$name[1]
return(resulting_vertices)

}) %>% unlist() %>% as.integer() return(refined_vertices) } and outputs an NA in the refined_vertices that stops as_igraph_vs.

Removing all isolated vertices from the iGraph object allows it to run. I'm not sure what impact removing isolated vertices will have on the output of the data. Just thought I would report what I have found.

## your code sce <- as.SingleCellExperiment(seurat.combined) reducedDim(sce, "PCA", withDimnames=TRUE) <- seurat.combined[['integrated.cca']]@cell.embeddings reducedDim(sce, "UMAP", withDimnames=TRUE) <- seurat.combined[['UMAP.cca']]@cell.embeddings milo <- Milo(sce)

Define Neighbourhoods

miloR::graph(milo) <- miloR::graph(buildFromAdjacency(seurat.combined@graphs$SCT_snn,k=20)) milo <- makeNhoods(milo, prop = 0.1, k = 20, d = 30, refined = T, reduced_dims = "PCA", refinement_scheme = "graph")

##Full error traceback 8: h(simpleError(msg, call)) 7: .handleSimpleError(function (cond) .Internal(C_tryCatchHelper(addr, 1L, cond)), "Invalid vertex name(s)", base::quote(as_igraph_vs(graph, nodes))) 6: stop("Invalid vertex name(s)") 5: as_igraph_vs(graph, nodes) 4: neighborhood(x.graph, order = 1, nodes = sampled_vertices[X]) 3: unlist(neighborhood(x.graph, order = 1, nodes = sampled_vertices[X])) 2: [<-(*tmp*, unlist(neighborhood(x.graph, order = 1, nodes = sampled_vertices[X])), X, value = 1) 1: makeNhoods(milo, prop = 0.1, k = 20, d = 30, refined = T, reduced_dims = "PCA", refinement_scheme = "graph")

## the error Error in h(simpleError(msg, call)) : error in evaluating the argument 'i' in selecting a method for function '[<-': Invalid vertex name(s)

Session info R version 4.4.0 (2024-04-24 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 11 x64 (build 22631)

Matrix products: default

attached base packages: [1] stats4 stats graphics grDevices utils datasets methods base

other attached packages: [1] scran_1.32.0 scater_1.32.0 scuttle_1.14.0 miloR_2.0.0 org.Mm.eg.db_3.19.1
[6] AnnotationDbi_1.66.0 clusterProfiler_4.12.0 ggrepel_0.9.5 data.table_1.15.4 RColorBrewer_1.1-3
[11] DESeq2_1.44.0 png_0.1-8 apeglm_1.26.1 pheatmap_1.0.12 reshape2_1.4.4
[16] edgeR_4.2.0 limma_3.60.2 Matrix.utils_0.9.8 Matrix_1.7-0 cowplot_1.1.3
[21] SingleCellExperiment_1.26.0 SummarizedExperiment_1.34.0 GenomicRanges_1.56.0 GenomeInfoDb_1.40.1 IRanges_2.38.0
[26] S4Vectors_0.42.0 MatrixGenerics_1.16.0 matrixStats_1.3.0 SeuratWrappers_0.3.5 CellChat_2.1.2
[31] data.tree_1.1.0 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 purrr_1.0.2
[36] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 tidyverse_2.0.0 igraph_2.0.3
[41] ggraph_2.2.1 HGNChelper_0.8.14 patchwork_1.2.0 ggplot2_3.5.1 Seurat_5.1.0
[46] SeuratObject_5.0.2 sp_2.1-4 dplyr_1.1.4 Biobase_2.64.0 BiocGenerics_0.50.0

loaded via a namespace (and not attached): [1] R.methodsS3_1.8.2 goftest_1.2-3 Biostrings_2.72.0 vctrs_0.6.5 spatstat.random_3.2-3
[6] digest_0.6.35 shape_1.4.6.1 registry_0.5-1 deldir_2.0-4 parallelly_1.37.1
[11] MASS_7.3-60.2 httpuv_1.6.15 foreach_1.5.2 qvalue_2.36.0 withr_3.0.0
[16] ggfun_0.1.5 ggpubr_0.6.0 survival_3.5-8 memoise_2.0.1 ggbeeswarm_0.7.2
[21] gson_0.1.0 systemfonts_1.1.0 gtools_3.9.5 tidytree_0.4.6 zoo_1.8-12
[26] GlobalOptions_0.1.2 pbapply_1.7-2 R.oo_1.26.0 KEGGREST_1.44.1 promises_1.3.0
[31] httr_1.4.7 rstatix_0.7.2 globals_0.16.3 fitdistrplus_1.1-11 rstudioapi_0.16.0
[36] UCSC.utils_1.0.0 miniUI_0.1.1.1 generics_0.1.3 DOSE_3.30.1 ggalluvial_0.12.5
[41] zlibbioc_1.50.0 ScaledMatrix_1.12.0 polyclip_1.10-6 GenomeInfoDbData_1.2.12 SparseArray_1.4.1
[46] xtable_1.8-4 doParallel_1.0.17 S4Arrays_1.4.0 hms_1.1.3 irlba_2.3.5.1
[51] colorspace_2.1-0 ggnetwork_0.5.13 ROCR_1.0-11 reticulate_1.37.0 spatstat.data_3.1-2
[56] magrittr_2.0.3 lmtest_0.9-40 later_1.3.2 viridis_0.6.5 ggtree_3.12.0
[61] lattice_0.22-6 spatstat.geom_3.2-9 NMF_0.27 future.apply_1.11.2 shadowtext_0.1.3
[66] scattermore_1.2 RcppAnnoy_0.0.22 pillar_1.9.0 nlme_3.1-164 iterators_1.0.14
[71] sna_2.7-2 beachmat_2.20.0 gridBase_0.4-7 compiler_4.4.0 RSpectra_0.16-1
[76] stringi_1.8.4 tensor_1.5 plyr_1.8.9 crayon_1.5.3 abind_1.4-5
[81] gridGraphics_0.5-1 emdbook_1.3.13 locfit_1.5-9.9 graphlayouts_1.1.1 bit_4.0.5
[86] fastmatch_1.1-4 codetools_0.2-20 BiocSingular_1.20.0 bslib_0.7.0 GetoptLong_1.0.5
[91] plotly_4.10.4 mime_0.12 splines_4.4.0 circlize_0.4.16 Rcpp_1.0.12
[96] fastDummies_1.7.3 sparseMatrixStats_1.16.0 HDO.db_0.99.1 grr_0.9.5 blob_1.2.4
[101] utf8_1.2.4 clue_0.3-65 fs_1.6.4 listenv_0.9.1 DelayedMatrixStats_1.26.0 [106] ggsignif_0.6.4 ggplotify_0.1.2 statmod_1.5.0 tzdb_0.4.0 svglite_2.1.3
[111] tweenr_2.0.3 pkgconfig_2.0.3 network_1.18.2 tools_4.4.0 cachem_1.1.0
[116] RSQLite_2.3.7 viridisLite_0.4.2 DBI_1.2.3 numDeriv_2016.8-1.1 splitstackshape_1.4.8
[121] fastmap_1.2.0 scales_1.3.0 grid_4.4.0 ica_1.0-3 broom_1.0.6
[126] sass_0.4.9 coda_0.19-4.1 FNN_1.1.4 BiocManager_1.30.23 dotCall64_1.1-1
[131] carData_3.0-5 RANN_2.6.1 farver_2.1.2 scatterpie_0.2.3 tidygraph_1.3.1
[136] cli_3.6.2 leiden_0.4.3.1 lifecycle_1.0.4 uwot_0.2.2 mvtnorm_1.2-5
[141] bluster_1.14.0 backports_1.5.0 BiocParallel_1.38.0 timechange_0.3.0 gtable_0.3.5
[146] rjson_0.2.21 ggridges_0.5.6 progressr_0.14.0 parallel_4.4.0 ape_5.8
[151] jsonlite_1.8.8 RcppHNSW_0.6.0 bit64_4.0.5 Rtsne_0.17 yulab.utils_0.1.4
[156] spatstat.utils_3.0-4 BiocNeighbors_1.22.0 bdsmatrix_1.3-7 metapod_1.12.0 jquerylib_0.1.4
[161] dqrng_0.4.1 GOSemSim_2.30.0 R.utils_2.12.3 lazyeval_0.2.2 shiny_1.8.1.1
[166] htmltools_0.5.8.1 enrichplot_1.24.0 GO.db_3.19.1 sctransform_0.4.1 glue_1.7.0
[171] spam_2.10-0 XVector_0.44.0 treeio_1.28.0 gridExtra_2.3 R6_2.5.1
[176] labeling_0.4.3 cluster_2.1.6 rngtools_1.5.2 bbmle_1.0.25.1 aplot_0.2.3
[181] statnet.common_4.9.0 vipor_0.4.7 DelayedArray_0.30.0 tidyselect_1.2.1 ggforce_0.4.2
[186] car_3.1-2 future_1.33.2 rsvd_1.0.5 munsell_0.5.1 KernSmooth_2.23-22
[191] htmlwidgets_1.6.4 fgsea_1.30.0 ComplexHeatmap_2.20.0 rlang_1.1.3 spatstat.sparse_3.0-3
[196] spatstat.explore_3.2-7 remotes_2.5.0 fansi_1.0.6 Cairo_1.6-2 beeswarm_0.4.0

MikeDMorgan commented 1 week ago

Why do you have a graph with isolated vertices? In a true NN-graph, you should have every cell connected to at least one other cell.

mdpatric commented 1 week ago

I'm pretty sure it's because I'm integrating all my samples (5 time points) and then subsetting them by each timepoint for analysis. Your tool actually has good functionally to analyze them all together, which may be better. I'm still trying to figure out the best way to tackle all this data.

MikeDMorgan commented 1 week ago

If you're subsetting cells/samples then you need to recompute the reduced dimensions and graph for them to be valid. You can't just take a global graph and subset the cells from it.

mdpatric commented 1 week ago

Yeah that makes sense. Thank you for the help!