|
| 1 | +#' DevKidCC |
| 2 | +#' |
| 3 | +#' @param seurat seurat object |
| 4 | +#' @param threshold minimum value for an identity to be assigned within the model call, default is 0.7 |
| 5 | +#' @param max.iter Can ask scPred to run this number of integrations, set to 0 be default |
| 6 | +#' |
| 7 | +#' @return |
| 8 | +#' @export |
| 9 | +#' |
| 10 | +#' @examples |
| 11 | +DevKidCC <- function(seurat, threshold = 0.7, max.iter = 0) { |
| 12 | + if (("DKCC" %in% colnames(seurat@meta.data)) == FALSE){ |
| 13 | + seurat@misc$old.meta <- seurat@meta.data |
| 14 | + } else { |
| 15 | + seurat@meta.data <- seurat@misc$old.meta |
| 16 | + } |
| 17 | + md <- seurat@meta.data |
| 18 | + #if ("cell" %in% colnames(md)) { |
| 19 | + # [email protected] %>% rownames_to_column("celltemp") %>% select(-cell) %>% column_to_rownames("celltemp") |
| 20 | + #} |
| 21 | + old.seurat <- seurat |
| 22 | + seurat <- CreateSeuratObject(old.seurat@assays$RNA@counts, meta.data = md) |
| 23 | + DefaultAssay(seurat) <- "RNA" |
| 24 | + seurat <- NormalizeData(seurat) |
| 25 | + kcc <- data.frame() |
| 26 | + |
| 27 | + seurat <- scPred::scPredict(seurat, reference = model1.all, threshold = threshold, max.iter.harmony=max.iter) |
| 28 | + seurat$LineageID <- seurat$scpred_prediction |
| 29 | + |
| 30 | + kcc <- seurat@meta.data %>% rownames_to_column("cell") %>% filter(LineageID %in% c("unassigned", "NPC", "Endothelial")) %>% transmute(cell = cell, kcc = LineageID) |
| 31 | + #kcc$kcc <- gsub("unassigned", "OffTarget", kcc$kcc) |
| 32 | + t1 <- seurat[, seurat$LineageID%in%c("unassigned", "NPC", "Endothelial")] |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + if (nrow(seurat@meta.data %>% filter(LineageID=="Nephron")) > 2){ |
| 38 | + nephronid <- scPred::scPredict(seurat[, seurat$LineageID=="Nephron"], reference = model2.nephron, |
| 39 | + threshold = 0.0, max.iter.harmony=max.iter) |
| 40 | + nephronid$NephronID <- nephronid$scpred_prediction |
| 41 | + nephronid$NephronID <- gsub("unassigned", "Nephron_NC", nephronid$NephronID) |
| 42 | + kcc <- bind_rows(kcc, |
| 43 | + nephronid@meta.data %>% rownames_to_column("cell") %>% filter(NephronID %in% c("CellCycle", "EarlyNephron", "unassigned")) %>% transmute(cell = cell, kcc = NephronID)) |
| 44 | + |
| 45 | + if (nrow(nephronid@meta.data %>% filter(NephronID %in% c("CellCycle", "EarlyNephron", "unassigned"))) > 0){ |
| 46 | + neph <- nephronid[, nephronid$NephronID %in% c("CellCycle", "EarlyNephron", "unassigned")] |
| 47 | + t1 <- merge(t1, neph) |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + if (nrow(nephronid@meta.data %>% filter(NephronID=="ProximalNephron")) > 2){ |
| 52 | + proximalid <- scPred::scPredict(nephronid[, nephronid$NephronID =="ProximalNephron"], |
| 53 | + reference = model3.pn, threshold = 0.0, max.iter.harmony=max.iter) |
| 54 | + proximalid$SegmentID <- proximalid$scpred_prediction |
| 55 | + |
| 56 | + kcc <- bind_rows(kcc, |
| 57 | + proximalid@meta.data %>% rownames_to_column("cell") %>% transmute(cell = cell, kcc = SegmentID)) |
| 58 | + t1 <- merge(t1, proximalid) |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + if (nrow(nephronid@meta.data %>% filter(NephronID=="DistalNephron")) > 2){ |
| 63 | + distalid <- scPred::scPredict(nephronid[, nephronid$NephronID =="DistalNephron"], |
| 64 | + reference = model3.dn, threshold = 0.0, max.iter.harmony=max.iter) |
| 65 | + distalid$SegmentID <- distalid$scpred_prediction |
| 66 | + |
| 67 | + kcc <- bind_rows(kcc, |
| 68 | + distalid@meta.data %>% rownames_to_column("cell") %>% transmute(cell = cell, kcc = SegmentID)) |
| 69 | + t1 <- merge(t1, distalid) |
| 70 | + } |
| 71 | + |
| 72 | + if (nrow(nephronid@meta.data %>% filter(NephronID=="RenalCorpuscle")) > 2){ |
| 73 | + rcid <- scPred::scPredict(nephronid[, nephronid$NephronID =="RenalCorpuscle"], reference = model3.rc, |
| 74 | + threshold = 0.0, max.iter.harmony=max.iter) |
| 75 | + rcid$SegmentID <- rcid$scpred_prediction |
| 76 | + |
| 77 | + kcc <- bind_rows(kcc, |
| 78 | + rcid@meta.data %>% rownames_to_column("cell") %>% transmute(cell = cell, kcc = SegmentID)) |
| 79 | + t1 <- merge(t1, rcid) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (nrow(seurat@meta.data %>% filter(LineageID=="Stroma")) > 2){ |
| 84 | + stromaid <- scPred::scPredict(seurat[, seurat$LineageID=="Stroma"], reference = model2.stroma, |
| 85 | + threshold = 0.0, max.iter.harmony=max.iter) |
| 86 | + stromaid$StromaID<- stromaid$scpred_prediction |
| 87 | + |
| 88 | + stromaid$StromaID <- gsub(pattern = "unassigned", replacement = "Stroma_NC", x = stromaid$StromaID) |
| 89 | + kcc <- bind_rows(kcc, |
| 90 | + stromaid@meta.data %>% rownames_to_column("cell") %>% transmute(cell = cell, kcc = StromaID)) |
| 91 | + t1 <- merge(t1, stromaid) |
| 92 | + } |
| 93 | + |
| 94 | + if (nrow(seurat@meta.data %>% filter(LineageID=="UrEp")) > 2){ |
| 95 | + urepid <- scPred::scPredict(seurat[, seurat$LineageID=="UrEp"], reference = model2.urep, |
| 96 | + threshold = 0.0, max.iter.harmony = max.iter) |
| 97 | + urepid$UrEpID <- urepid$scpred_prediction |
| 98 | + urepid$UrEpID <- gsub(pattern = "unassigned", replacement = "UrEp", x = urepid$UrEpID) |
| 99 | + kcc <- bind_rows(kcc, |
| 100 | + urepid@meta.data %>% rownames_to_column("cell") %>% transmute(cell = cell, kcc = UrEpID)) |
| 101 | + t1 <- merge(t1, urepid) |
| 102 | + } |
| 103 | + |
| 104 | + if ("NephronID" %in% colnames(t1@meta.data)){ |
| 105 | + t1$NephronID <- gsub("CellCycle", "CC", t1$NephronID) |
| 106 | + t1$NephronID <- gsub("EarlyNephron", "EN", t1$NephronID) |
| 107 | + t1$NephronID <- gsub("DistalNephron", "DN", t1$NephronID) |
| 108 | + t1$NephronID <- gsub("ProximalNephron", "PN", t1$NephronID) |
| 109 | + t1$NephronID <- gsub("RenalCorpuscle", "RC", t1$NephronID) |
| 110 | + t1$NephronID <- factor(t1$NephronID, levels = c("unassigned", "CC", "EN", "DN", "PN", "RC")) |
| 111 | + } |
| 112 | + |
| 113 | + kcc$kcc <- gsub("CellCycle", "CC", kcc$kcc) |
| 114 | + kcc$kcc <- gsub("EarlyNephron", "EN", kcc$kcc) |
| 115 | + levels <- c("unassigned", "NPC", "Endothelial", |
| 116 | + "CC", "EN", "DN", "PN", "RC", |
| 117 | + "SPC", "Cortex", "Medullary", "Mesangial", "Stroma_NC", |
| 118 | + "Tip", "OuterStalk", "InnerStalk", |
| 119 | + "EDT_EMT", "DT", "LOH", "EPT", "PT", "PEC", "EPod", "Pod", "Nephron_NC") |
| 120 | + |
| 121 | + t1$DKCC <- factor(kcc$kcc, levels = levels[levels %in% unique(kcc$kcc)]) |
| 122 | + t1$LineageID <- factor(t1$LineageID, levels = c("unassigned", "Endothelial", "Stroma", "NPC", "Nephron", "UrEp")) |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + # |
| 127 | + |
| 128 | + |
| 129 | + old.seurat@meta.data <- left_join(md %>% rownames_to_column("cell") %>% select(cell), |
| 130 | + t1@meta.data %>% rownames_to_column("cell"), by = "cell") %>% column_to_rownames("cell") |
| 131 | + |
| 132 | + return(old.seurat) |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
0 commit comments