-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtutorial_fit_conditions_to_data.R
79 lines (48 loc) · 2.1 KB
/
tutorial_fit_conditions_to_data.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
# Jake Yeung
# Date of Creation: 2020-08-03
# File: ~/projects/CircadianRNASeq/test_nconds_fits.R
# Test nconds fits
library(CircadianRNASeq)
# library(Matrix)
# library(hash)
# library(numbers) # easy bell number calculation
# library(ggplot2)
#' ## Load data
data(dat_long_WTKO, verbose=TRUE)
#' # Run fits
#' Subset data to just two genes for the tutorial
dat.long <- subset(dat_long_WTKO, gene %in% c("Nr1d1", "Insig2"))
dat.long.forplot <- dat.long %>%
rowwise() %>%
mutate(geno = strsplit(as.character(tissue), "_")[[1]][[2]],
tissue = strsplit(as.character(tissue), "_")[[1]][[1]])
#' Expression of Insig2, a gene that is rhythmic in only one of the four conditions (Liver WT):
PlotGeneTissuesWTKO(subset(dat.long.forplot, gene == "Insig2"))
tissues.uniq <- unique(as.character(dat.long$tissue))
dat.env <- DatLongToEnvironment(dat.long)
method <- "g=1000" # Method for penalizing model complexity: other possibilities: BIC, or g={integer}
jstart <- Sys.time()
print("Running fits...")
fits.all <- lapply(ls(dat.env), function(gene){
MakeDesMatRunFitEnv(dat.env, gene, tissues.uniq,
n.rhyth.max = length(tissues.uniq), w = 2 * pi / 24,
criterion = method, normalize.weights = TRUE,
cutoff = 1e-5, top.n = NULL, sparse = FALSE)
})
print("Running fits... done")
print(Sys.time() - jstart)
n.combos <- numbers::bell(length(tissues.uniq) + 1)
fits.all.long <- lapply(fits.all, function(x){
gene <- x$gene
x$gene <- NULL
fits.temp.long <- ListToLong(x, gene, top.n = n.combos)
})
fits.all.long <- do.call(rbind, fits.all.long)
fits.long.filt.subset <- fits.all.long %>%
group_by(gene) %>%
filter(weight.raw == min(weight.raw))
print(fits.long.filt.subset$param.list[[1]])
#' The model allows automatic separation between oscillatory conditions (here Insig2 oscillates in only Liver WT) versus "flat" conditions (all other conditions):
jgene <- "Insig2"
#' Model separates between oscillating conditions versus flat conditions
PlotGeneByRhythmicParameters(fits.long.filt.subset, dat_long_WTKO, jgene) + xlab("Time (ZT)")