-
Notifications
You must be signed in to change notification settings - Fork 0
/
down_and_perm_trans.R
159 lines (142 loc) · 6.04 KB
/
down_and_perm_trans.R
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
library("dplyr")
library("Matrix")
library("Seurat")
library("stringr")
geneCap <- function(gene, gene_names) {
# Gene the gene name in the right format
gene_lower <- tolower(gene)
gene_upper <- toupper(gene)
gene_title <- str_to_title(gene)
error <- FALSE
if (gene_lower %in% gene_names) {
gene <- gene_lower
} else if (gene_upper %in% gene_names) {
gene <- gene_upper
} else if (gene_title %in% gene_names) {
gene <- gene_title
} else {
error <- TRUE
}
return(c(gene, error))
}
validGenes <- function(genes, gene_names) {
valid_genes <- c()
for (gene in genes) {
result <- geneCap(gene, gene_names)
gene <- result[1]
error <- as.logical(result[2])
if (! error) {
valid_genes <- c(valid_genes, gene)
}
} # end gene for
return(valid_genes)
} # end validGenes function
downsample <- function(combined, marker_genes, run) {
set.seed(run)
min_trans <- min(combined$nCount_RNA)
gene_names <- rownames(combined@assays$RNA@counts)
# new_matrix <- matrix(, nrow = nrow(combined@assays$RNA@counts), ncol = ncol(combined@assays$RNA@counts), dimnames = list(gene_names, colnames(combined@assays$RNA@counts)))
# new_new_matrix <- matrix(, nrow=nrow(combined@assays$RNA@counts))
marker_matrix <- matrix(, nrow=length(marker_genes), ncol = ncol(combined@assays$RNA@counts), dimnames = list(marker_genes, colnames(combined@assays$RNA@counts)))
i <- 0
for (cell in colnames(combined@assays$RNA@counts)) {
# i <- i + 1
# if (i%%500 == 1) {
# print(cell)
# }
# start.time <- Sys.time()
trans_names <- rep(gene_names, combined@assays$RNA@counts[,cell])
ran_trans_names <- sample(trans_names, min_trans)
ran_trans_names <- ran_trans_names[which(ran_trans_names %in% marker_genes)]
ran_df <- as.data.frame(table(ran_trans_names))
zero_gene_names <- marker_genes[which(! marker_genes %in% ran_trans_names)]
zero_df <- setNames(data.frame(zero_gene_names <- zero_gene_names, Freq <- rep(0, length(zero_gene_names))), c("ran_trans_names", "Freq"))
ran_df <- rbind(ran_df, zero_df)
rownames(ran_df) <- ran_df$ran_trans_names
ran_df <- ran_df[marker_genes,2]
# new_matrix[,cell] <-as.matrix(ran_df)
marker_matrix[,cell] <- as.matrix(ran_df)
# new_new_matrix <- cbind(new_new_matrix, as.matrix(ran_df))
# end.time <- Sys.time()
# time.taken <- end.time - start.time
# print(time.taken)
}
return(marker_matrix)
}
## END FUNCTIONS ##
rna_path <- "~/scratch/brain/"
combined <- readRDS(paste(rna_path, "/brain_scripts/brain_shiny/data/combined.rds", sep = ""))
marker_path <- paste(rna_path, "data/markers/", sep="")
marker_files <- dir(marker_path, pattern =paste("*.txt", sep=""))
markers <- data.frame(gene <- c(), bio <- c())
for (i in 1:length(marker_files)) {
file <- read.table(paste(marker_path, marker_files[i], sep=""), header=FALSE, sep="\t", stringsAsFactors=FALSE)
file[,1] <- toupper(file[,1])
markers <- rbind(markers, file[,1:2])
}
colnames(markers) <- c("gene", "bio")
markers <- markers[which(markers$bio == "DISC_ASE"),]
print("Before gene_names")
gene_names <- rownames(combined@assays$RNA)
print("After gene_names")
marker_genes <- unique(validGenes(markers$gene, gene_names))
valid_genes <- marker_genes
num_clusters <- as.numeric(tail(levels([email protected]$seurat_clusters), n=1))
down_avg_avg_trans <- rep(0, num_clusters+1)
num_runs <- 50
# No Perm, Bootstrap
for (run in 1:num_runs) {
cat(paste("no_perm", run, "\n"))
mat <- downsample(combined, marker_genes, run)
cells_per_cluster <- c()
trans_per_cluster <- c()
for (i in 0:num_clusters) {
this_cells <- WhichCells(combined, idents = i)
# genes_per_cluster <- c(genes_per_cluster, length(which(as.vector(combined@assays$RNA@counts[ran_markers,this_cells]) != 0))) # genes
trans_per_cluster <- c(trans_per_cluster, sum(rowSums(as.matrix(mat[,this_cells])))) # all trans
cells_per_cluster <- c(cells_per_cluster, length(this_cells))
}
avg_trans_per_cell_per_cluster <- trans_per_cluster/cells_per_cluster
down_avg_avg_trans <- down_avg_avg_trans + avg_trans_per_cell_per_cluster
}
down_avg_avg_trans <- down_avg_avg_trans / num_runs
print(down_avg_avg_trans)
# Perm, Bootstrap
backup_ids <- [email protected]$seurat_clusters
perm_down_avg_trans <- lapply(0:num_clusters, function(x) c())
for (run in (num_runs+1):(num_runs+num_runs)) {
cat(paste("perm", run, "\n"))
set.seed(run)
shuffled <- sample(backup_ids)
mat <- downsample(combined, marker_genes, run)
Idents(object = combined) <- shuffled
num_clusters <- as.numeric(tail(levels([email protected]$seurat_clusters), n=1))
gene_names <- rownames(combined@assays$RNA)
cells_per_cluster <- c()
trans_per_cluster <- c()
for (i in 0:num_clusters) {
this_cells <- WhichCells(combined, idents = i)
this_trans <- sum(rowSums(as.matrix(mat[valid_genes,this_cells])))
trans_per_cluster <- c(trans_per_cluster, this_trans) # trans
cells_per_cluster <- c(cells_per_cluster, length(this_cells))
perm_down_avg_trans[[i+1]] <- c(perm_down_avg_trans[[i+1]], this_trans/length(this_cells))
}
avg_trans_per_cell_per_cluster <- trans_per_cluster/cells_per_cluster
# perm_down_avg_gene <- c(perm_down_avg_gene, avg_gene_per_cell_per_cluster)
}
# Compare empirical data to 97.5th percentile of the permutated data on a PER CLUSTER basis
sig_clusters <- c()
df <- data.frame()
for (i in 0:num_clusters) {
sig <- quantile(perm_down_avg_trans[[i+1]], c(0.975))
df <- rbind(df, t(c(sig, down_avg_avg_trans[i+1])))
if ( down_avg_avg_trans[i+1] > sig ) {
sig_clusters <- i
}
}
# sig <- quantile(perm_down_avg_gene, c(.975))
# print(sig)
# sig_clusters <- which(down_avg_avg_gene > sig)-1
print(sig_clusters)
write.csv(df, file = paste(rna_path, "/results/down_perm_trans_data.csv", sep=""), row.names = FALSE)
write.csv(sig_clusters, file = paste(rna_path, "/results/down_perm_sig_clusters_trans.csv", sep=""), row.names = FALSE)