-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclic_genes_FFT.R
149 lines (103 loc) · 5.02 KB
/
cyclic_genes_FFT.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
library(openxlsx)
library(tidyverse)
library(splines)
library(parallel)
library(ggplot2)
library(tidytext)
library(ggrepel)
library(geomtextpath)
library(ggVennDiagram)
source('./util_funcs.R')
num.cores <- detectCores(all.tests = FALSE, logical = TRUE)
getCurvePeakLoc <- function(t, y, prob = 0.8){
## Fitting the estimated kernel with smooth splines
spline.fit <- smooth.spline(x = t, y = y)
## Compute the derivatives of the fitted splines
s.0 <- predict(spline.fit, spline.fit$x, deriv=0)
s.1 <- predict(spline.fit, spline.fit$x, deriv=1)
s.derv <- data.frame(s0=round(s.0$y, digits = 3), s1=round(s.1$y, digits = 3))
## Get the location of the extrema
locs <- rle(den.sign <- sign(s.derv$s1))
## Maxima
inc.ind <- which(locs$values == 1)
if(length(inc.ind) >= 1 & any(locs$values== -1)){
maxima.ind = {}
for(i in inc.ind){
maxima.ind = c(maxima.ind, sum(locs$lengths[1:i]))
}
## Interpolate a point between the location where derivative changes sign
maxima = (spline.fit$x[maxima.ind] + spline.fit$x[(maxima.ind + 1)]) / 2
maxima = maxima[!is.na(maxima)]
## Get the maximum values
maxval = predict(spline.fit, maxima)
## Get the outliers
maxima.outliers = which(maxval$y >= quantile(maxval$y, prob = prob))
## Peaks for entities of interest
entity.x = maxval$x[maxima.outliers]
entity.y = maxval$y[maxima.outliers]
}else{
entity.x <- spline.fit$x[which.max(spline.fit$y)]
entity.y <- spline.fit$y[which.max(spline.fit$y)]
}
if(length(entity.x) == 0){
entity.x <- spline.fit$x[which.max(spline.fit$y)]
entity.y <- spline.fit$y[which.max(spline.fit$y)]
}
return(entity.x)
}
prod.disc <- read.xlsx('../../Input/toxo_genomics/genes/ProductDescription_ME49_65.xlsx')
sc.rna.genes.expr.pt <- readRDS('../../Input/DeepToxo/rds/sc_rna_spline_fit_1.0.rds')
sc.atac.genes.expr.pt <- readRDS('../../Input/DeepToxo/rds/sc_atac_spline_fit_1.0.rds')
genes <- intersect(unique(sc.rna.genes.expr.pt$GeneID), unique(sc.atac.genes.expr.pt$GeneID))
fft.stas <- mclapply(1:length(genes), function(i){
rna.sp <- sc.rna.genes.expr.pt %>% dplyr::filter(GeneID == genes[i]) %>%
transmute(GeneID = GeneID, x = x, y = y) %>% arrange(x)
atac.sp <- sc.atac.genes.expr.pt %>% dplyr::filter(GeneID == genes[i]) %>%
transmute(GeneID = GeneID, x = x, y = y) %>% arrange(x)
rna.upper.expr <- mean(rna.sp$y[rna.sp$y > quantile(rna.sp$y, p = 0.75)])
rna.transform = fft(rna.sp$y)/length(rna.sp$y)
rna.magTransform = abs(rna.transform)
rna.amp <- max(rna.magTransform[-1]) * 2
rna.freq <- which.max(rna.magTransform[-1])
rna.peak.time <- getCurvePeakLoc(rna.sp$x, rna.sp$y, prob = 0.8)
atac.upper.expr <- mean(atac.sp$y[atac.sp$y > quantile(atac.sp$y, p = 0.75)])
atac.transform = fft(atac.sp$y)/length(atac.sp$y)
atac.magTransform = abs(atac.transform)
atac.amp <- max(atac.magTransform[-1]) * 2
atac.freq <- which.max(atac.magTransform[-1])
atac.peak.time <- getCurvePeakLoc(atac.sp$x, atac.sp$y, prob = 0.8)
L <- list(rna.upper.expr, rna.amp, rna.freq, rna.peak.time,
atac.upper.expr, atac.amp, atac.freq, atac.peak.time)
}, mc.cores = num.cores)
stats <- data.frame(GeneID = genes,
rna.upper.expr = unlist(lapply(fft.stas, `[[`, 1)),
rna.amp = unlist(lapply(fft.stas, `[[`, 2)),
rna.freq = unlist(lapply(fft.stas, `[[`, 3)),
rna.peak.time = unlist(lapply(fft.stas, `[[`, 4)),
atac.upper.expr = unlist(lapply(fft.stas, `[[`, 5)),
atac.amp = unlist(lapply(fft.stas, `[[`, 6)),
atac.freq = unlist(lapply(fft.stas, `[[`, 7)),
atac.peak.time = unlist(lapply(fft.stas, `[[`, 8)))
stats$GeneID <- gsub('-', '_', stats$GeneID)
stats <- left_join(stats, prod.disc, by = 'GeneID')
rna.expr.cutoff <- quantile(stats$rna.upper.expr, prob = 0.01, na.rm = T)
stats$rna.expressed <- ifelse(stats$rna.upper.expr >= rna.expr.cutoff, 1, 0)
atac.expr.cutoff <- quantile(stats$atac.upper.expr, prob = 0.01, na.rm = T)
stats$atac.expressed <- ifelse(stats$atac.upper.expr >= atac.expr.cutoff, 1, 0)
rna.amp.cutoff <- quantile(stats$rna.amp, prob = 0.5)
stats$rna.cyclic<- ifelse(stats$rna.amp >= rna.amp.cutoff , 1, 0)
atac.amp.cutoff <- quantile(stats$atac.amp, prob = 0.5)
stats$atac.cyclic<- ifelse(stats$atac.amp >= atac.amp.cutoff , 1, 0)
stats.expressed <- stats %>% dplyr::filter(rna.expressed == 1)
nrow(stats.expressed) / nrow(stats)
nrow(stats.expressed)
stats.cyclic.rna <- stats %>% dplyr::filter(rna.expressed == 1, rna.cyclic == 1)
nrow(stats.cyclic.rna) / nrow(stats)
nrow(stats.cyclic.rna)
stats.cyclic.atac <- stats %>% dplyr::filter(rna.expressed == 1, atac.cyclic == 1)
nrow(stats.cyclic.atac)
stats.cyclic.both <- stats %>% dplyr::filter(rna.expressed == 1, rna.cyclic == 1 & atac.cyclic == 1)
nrow(stats.cyclic.both) / nrow(stats)
nrow(stats.cyclic.both)
sum(stats$rna.cyclic) / nrow(stats)
write.xlsx(stats, '../../Output/DeepToxo/tables/cyclic_genes.xlsx')