Diff of /Examples/dtw_example.R [000000] .. [5d6472]

Switch to unified view

a b/Examples/dtw_example.R
1
# An example to run DTW time delay analysis across modalities.
2
3
library(tidyverse)
4
library(dtw)
5
library(zoo)
6
7
gene = 'EGR1'
8
bins = 20
9
10
# input file contains three columns: latent time, motif accessibility, and TF gene expression
11
res = read.delim(paste0(gene, '_res.txt'), sep=' ', header = F, stringsAsFactors = F)
12
colnames(res) = c('t', 'm', 'r')
13
res = res[order(res$t),]
14
15
res$t = res$t * bins
16
res$window = floor(res$t / 1)
17
res$window[res$window==bins] = bins-1
18
res2 = aggregate(res, by=list(res$window), FUN=mean)
19
20
rv = res2$r - min(res2$r)
21
rv = c(rv[1], rv, rv[length(rv)])
22
rv = rollmean(rv, 3)
23
rv[1] = 0
24
rv[length(rv)] = 0
25
rv = rv / max(rv)
26
mv = res2$m - min(res2$m)
27
mv = c(mv[1], mv, mv[length(mv)])
28
mv = rollmean(mv, 3)
29
mv[1] = 0
30
mv[length(mv)] = 0
31
mv = mv / max(mv)
32
33
# compute dtw
34
dtw.res = dtw(rv, mv, keep.internals = T)
35
dtwPlotTwoWay(dtw.res, col=c('magenta', '#f7ba41'), lwd=3, xlab='latent time', ylab='accessibility and expression')