Repository navigation
Expand file tree
/
Copy pathprocess_tissue_objects.R
More file actions
81 lines (63 loc) · 2.98 KB
/
Copy pathprocess_tissue_objects.R
File metadata and controls
81 lines (63 loc) · 2.98 KB
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
library(Seurat)
library(SeuratDisk)
library(BPCells)
library(dplyr)
library(tibble)
library(ggplot2)
# Set up paths and tissue names
config <- read.csv("/brahms/lis/pan-human/neuralnet/training_obj/data/filter_config.csv")
tissue_name1 <- "" # changed to desired tissue name
tissue_row <- config[config$tissue == tissue_name1, ]
num_cells <- tissue_row$top
path_type <- tissue_row$path
path <- paste0("/brahms/mollag/meta_analysis/pan-human/outputs/",tissue_name1,"/",tissue_name1,"_final_object.rds")
slpath <- paste0("/brahms/lis/pan-human/outputs/",tissue_name1,"/",tissue_name1,"_final_object.rds")
tissue_name1 <- gsub(" ","", tissue_name1)
save_path <- paste0("/brahms/lis/pan-human/neuralnet/training_obj/unfiltered/",tissue_name1,".hdf5")
metadata_save_path <- paste0("/brahms/lis/pan-human/neuralnet/training_obj/unfiltered/",tissue_name1,"_metadata.csv")
if (path_type == "2") {
path <- slpath
}
obj <- readRDS(path)
# Filtering
## If filtering
## Filter for only cells with 0.75-1 score
save_path <- paste0("/brahms/lis/pan-human/neuralnet/training_obj/filtered/orignorm/",tissue_name1,".hdf5")
metadata_save_path <- paste0("/brahms/lis/pan-human/neuralnet/training_obj/filtered/orignorm/",tissue_name1,"_metadata.csv")
table(obj$ridge_score_bin, obj$updated_annotations_full)
Idents(obj) <- "ridge_score_bin"
obj_filtered <- subset(obj, ident = "[0.75-1)")
## Alternate option if too many cells (> 1m)
#OR
top_cells_all <- obj_filtered@meta.data %>%
rownames_to_column(var = "cell_names") %>%
group_by(updated_annotations_full) %>%
arrange(desc(one_vs_all_score)) %>%
slice_head(n = 25000)
obj_filtered <- subset(obj_filtered, cells = top_cells_all$cell_names)
# Plot filtered obj
counts_obj <- obj[[]] %>%
count(updated_annotations_full) %>%
mutate(Source = "Before filtering")
counts_obj_filtered <- obj_filtered[[]] %>%
count(updated_annotations_full) %>%
mutate(Source = "Filtered")
combined_counts <- bind_rows(counts_obj,counts_obj_filtered)
total_before <- sum(counts_obj$n)
total_after <- sum(counts_obj_filtered$n)
p <- ggplot(combined_counts, aes(x = updated_annotations_full, y = n, fill = Source)) +
geom_bar(stat = "identity", position = "identity") +
theme_minimal() +
labs(x = "Cell Type", y = "Number of Cells", fill = "Source") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
annotate("text", x = Inf, y = Inf, label = paste("Total Before Filtering:", total_before),
hjust = 1.1, vjust = 2, size = 4, color = "red") +
annotate("text", x = Inf, y = Inf, label = paste("Total After Filtering:", total_after),
hjust = 1.1, vjust = 4, size = 4, color = "blue")
p
# Write normalized matrix out
write_matrix_anndata_hdf5(obj_filtered[['RNA']]$data, save_path, group='X')
metadata <- as.data.frame(obj_filtered[[]])
write.csv(metadata, metadata_save_path)
barplot_save_path <- paste0("/brahms/lis/pan-human/neuralnet/training_obj/filtered/orignorm/plots/",tissue_name1,"_top_quartile_filtering.png")
ggsave(barplot_save_path, plot=p)