1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| library(Azimuth) AzimuthReference <- function (object, refUMAP = "umap", refDR = "spca", refAssay = "SCT", dims = 1:50, k.param = 31, plotref = "umap", plot.metadata = NULL, ori.index = NULL, colormap = NULL, assays = NULL, metadata = NULL, reference.version = "0.0.0", verbose = FALSE) { if (!refUMAP %in% Reductions(object = object)) { stop("refUMAP (", refUMAP, ") not found in Seurat object provided") } if (is.null(x = Misc(object = object[[refUMAP]], slot = "model"))) { stop("refUMAP (", refUMAP, ") does not have the umap model info stored. ", "Please rerun RunUMAP with return.model = TRUE.") } if (!refDR %in% Reductions(object = object)) { stop("refDR (", refDR, ") not found in Seurat object provided") } if (is.null(x = metadata)) { stop("Please specify at least one metadata field (for transfer and plotting).") } for (i in metadata) { if (!i %in% colnames(x = object[[]])) { warning(i, " not found in Seurat object metadata") next } if (!is.factor(x = object[[i, drop = TRUE]])) { warning(i, " is not a factor. Converting to factor with alphabetical ", "levels.", call. = FALSE) object[[i, drop = TRUE]] <- factor(x = object[[i, drop = TRUE]], levels = sort(x = unique(object[[i, drop = TRUE]]))) } } if (!refAssay %in% Assays(object = object)) { stop("Seurat object provided must have the SCT Assay stored.") } if (!inherits(x = object[[refAssay]], what = "SCTAssay")) { stop("refAssay (", refAssay, ") is not an SCTAssay.") } if (length(x = levels(x = object[[refAssay]])) != 1) { stop("refAssay (", refAssay, ") should contain a single SCT model.") } suppressWarnings(expr = object[["refUMAP"]] <- object[[refUMAP]]) suppressWarnings(expr = object[["refDR"]] <- object[[refDR]]) object <- FindNeighbors(object = object, reduction = "refDR", dims = dims, graph.name = "refdr.annoy.neighbors", k.param = k.param, nn.method = "annoy", annoy.metric = "cosine", cache.index = TRUE, return.neighbor = TRUE, l2.norm = FALSE, verbose = verbose) if (verbose) { message("Computing pseudobulk averages") } features <- rownames(x = Loadings(object = object[["refDR"]])) plot.metadata <- plot.metadata %||% object[[metadata]] if (inherits(x = plotref, what = "DimReduc")) { plot.metadata <- plot.metadata[Cells(x = plotref), ] } ad <- CreateAzimuthData(object = object, plotref = plotref, plot.metadata = plot.metadata, colormap = colormap, reference.version = reference.version) ori.index <- ori.index %||% match(Cells(x = object), Cells(x = object[["refUMAP"]])) object$ori.index <- ori.index DefaultAssay(object = object) <- refAssay object[[refAssay]] <- subset(x = object[[refAssay]], features = features) DefaultAssay(object = object[["refDR"]]) <- refAssay object <- DietSeurat(object = object, counts = FALSE, assays = c(refAssay, assays), dimreducs = c("refDR", "refUMAP")) metadata <- c(metadata, "ori.index") for (i in colnames(x = object[[]])) { if (!i %in% metadata) { object[[i]] <- NULL } } sct.model <- slot(object = object[[refAssay]], name = "SCTModel.list")[[1]] object[["refAssay"]] <- as(object = suppressWarnings(Seurat:::CreateDummyAssay(assay = object[[refAssay]])), Class = "SCTAssay") slot(object = object[["refAssay"]], name = "SCTModel.list") <- list(refmodel = sct.model) DefaultAssay(object = object) <- "refAssay" DefaultAssay(object = object[["refDR"]]) <- "refAssay" Tool(object = object) <- ad call_list <- sys.calls() tool.name <- as.character(x = call_list[[length(call_list)]]) tool.name <- lapply(X = strsplit(x = tool.name, split = "(", fixed = TRUE), FUN = "[", 1)[[1]] if (tool.name != "AzimuthReference") { slot(object, name = "tools")["AzimuthReference"] <- slot(object, name = "tools")[tool.name] slot(object, name = "tools")[tool.name] <- NULL } object <- DietSeurat(object = object, counts = FALSE, assays = c("refAssay", assays), dimreducs = c("refDR", "refUMAP")) ValidateAzimuthReference(object = object) return(object) }
|