-
Notifications
You must be signed in to change notification settings - Fork 1
/
ATAC_functions.R
142 lines (123 loc) · 3.19 KB
/
ATAC_functions.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
#################################
#Functions
#################################
#################################
#Recursive peak finding, sorted
#################################
find_peaks <- function(y, peak.threshold, footprint.fraction)
{
local.max = max(y)
if (local.max<peak.threshold)
{
return(c())
}
max.index = which.max(y)
peaks = c(max.index)
w = length(y)
# Descend forward until reach footprint threshold
i=max.index
while(i < w && y[i]>local.max*footprint.fraction)
{
i = i + 1
}
# start Tracking local min
local.min = y[i]
while(i < w)
{
i = i + 1
local.min = min(c(local.min,y[i]))
# Continue until back above the footprint threshold
if(y[i]>local.min/footprint.fraction)
{
# recursively call on the remaining curve
peaks = c(peaks, i+find_peaks(y[i:w], peak.threshold, footprint.fraction))
break
}
}
# Descend backward until reach footprint threshold
i=max.index
while(i > 1 && y[i]>local.max*footprint.fraction)
{
i = i - 1
}
# start tracking local min
local.min = y[i]
while(i > 1)
{
i = i - 1
local.min = min(c(local.min, y[i]))
# Continue until back above the footprint threshold
if(y[i]>local.min/footprint.fraction)
{
# recursively call on the remaining curve
peaks = c(find_peaks(y[0:i],peak.threshold, footprint.fraction), peaks)
break
}
}
return(peaks)
}
#############################
#Current footprint finding
#############################
find_footprints <- function(y, peaks)
{
npeaks = length(peaks)
footprints = rep(0,npeaks-1)
if(npeaks>1)
{
for(i in 1:(npeaks-1))
{
footprints[i] = peaks[i]+which.min(y[peaks[i]:peaks[i+1]])
}
}
return(footprints)
}
###########################
#Current centres
###########################
find_centre <- function(peaks, footprints)
{
# Centre on centre peak if the number of peaks is odd
if(length(footprints)%%2==0)
{
return(peaks[length(footprints)/2+1])
}
# Centre on centre footprint if the number of footprints is odd
return(footprints[(length(footprints)+1)/2])
}
####################
# Wrap functions
####################
process_peak <- function(i, r, peaks, min.reads, rel.threshold, abs.threshold, footprint.frac, bandwidth)
{
no.reads = nrow(r)
peaks[id==i, nreads := no.reads]
if(no.reads >= min.reads)
{
d = density(r[id==i,pos], bw = bandwidth)
threshold = max(c(abs.threshold, max(d$y)*rel.threshold))
p = find_peaks(d$y, threshold, footprint.frac)
if(length(p)>0)
{
footprints = find_footprints(d$y, p)
no.footprints = length(footprints)
if (no.footprints>0)
{
footprint.pos = round(d$x[footprints])
cent = round(d$x[find_centre(p, footprints)])
peaks[id==i, centre := cent]
peaks[id==i, nfootprints := no.footprints]
return(data.frame(i, footprint.pos, round(d$x[p[1:length(p)-1]]),round(d$x[p[2:length(p)]])))
} else
{
peaks[id==i, centre := round(d$x[p[1]])]
peaks[id==i, nfootprints := 0]
}
}
}else
{
peaks[id==i, centre := NA]
peaks[id==i, nfootprints := NA]
}
return(data.frame())
}