-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECDF_Utillity_Functions.R
185 lines (126 loc) · 5.2 KB
/
ECDF_Utillity_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# This function takes 2 arguments:
# 1. peakposn: The position on the genome where the maximum value of any association profile has occured.
#
# 2. reegion: A numeric vector of size two indicating the lower and upper bound of the causal region.
#
# This function returns the distance from the risk region (passed as region to function).
# In the scenario where there are multiple peaks, it returns the average of the distances from the region.
#
avgPeakDist = function(peakposn, region){
LowerBound = region[1]
UpperBound = region[2]
if(any(peakposn < LowerBound)){
dist1 = LowerBound - peakposn[peakposn < LowerBound]
}else{dist1 = NA}
if(any(peakposn > UpperBound)){
dist2 = peakposn[peakposn > UpperBound] - UpperBound
}else{dist2 = NA}
if(any( peakposn >= LowerBound & peakposn <= UpperBound )){
dist3 = rep(0,length(peakposn >= LowerBound & peakposn <= UpperBound))
}else{dist3 = NA}
dist = c(dist1, dist2, dist3)
if(any(is.na(dist))){
avgDist = mean(dist[!is.na(dist)])
}else{
avgDist = mean(dist)
}
return(peakAvgDist = avgDist)
}
#
# This function takes the path to the folder of any simulated dataset
# and calculates the distance from the risk causal region for every method.
# The output is a numeric vector. Each element of this vector, denotes the
# distance from the risk causal region.
#
# The output vector reports the distances in the following order:
# "FET", "SKATO", "dCorN", "dCorIT", "dCorIG
#
get_localization_results = function(path){
# Create an empty numeric vector to record the average distance
# from the risk region for each method
average_distance = numeric( length = 4)
# Loading the localization results for non IBD methods and IBD based methods
# A numeric vector of positions on the genome.
# We can use this for all methods except SKATO.
load( paste0(path, "FET.RData") )
pos = FET$pos
if( file.exists(paste0(path, "FET.RData") ) ){
load( paste0(path, "FET.RData") )
max.indx = which.max( -log( FET$pvalue, base = 10 ) )
average_distance[1] = avgPeakDist( peakposn = pos[max.indx], region = c(900000,1100000) )
}else{
average_distance[1] = NA
}
if( file.exists( paste0(path, "SKATO.RData" )) ){
load( paste0(path, "SKATO.RData") )
pos.skato = SKATO$pos
max.indx = which.max( -log( SKATO$pvalue, base = 10 ) )
average_distance[2] = avgPeakDist( peakposn = pos.skato[max.indx], region = c(900000,1100000) )
}else{
average_distance[2] = NA
}
if( file.exists(paste0(path, "dCorN.RData")) ){
load( paste0(path, "dCorN.RData") )
max.indx = which.max( dCorN )
average_distance[3] = avgPeakDist( peakposn = pos[max.indx], region = c(900000,1100000) )
}else{
average_distance[3] = NA
}
if( file.exists(paste0(path, "Mantel.RData")) ){
load( paste0(path, "Mantel.RData") )
max.indx = which.max( Mantel^2 )
average_distance[4] = avgPeakDist( peakposn = pos[max.indx], region = c(900000,1100000) )
}else{
average_distance[4] = NA
}
names(average_distance) = c("FET", "SKATO", "dCorN", "Mantel")
return(average_distance)
}
#
# This function takes the path to the folder of any simulated dataset
# and calculates the results of detection for each method.
#
# The output is a numeric vector where each element is the calculated pvalue
# based on the permutation of case/control status of individuals.
#
# The output vector reports the pvalues in the following order:
# "FET", "SKATO", "dCorN", "Mantel"
#
get_detection_results = function(path){
# Create an empty numeric vector to record the pvalue obtained by permutation.
pvalue = numeric( length = 4)
if( file.exists( paste0(path, "FET_perm.RData") ) ){
load( paste0(path, "FET_perm.RData") )
n_perm = nrow(FET_permutations) - 1
perms = apply(X = -log(FET_permutations,base=10), MARGIN = 1, FUN = max)
pvalue[1] = mean( perms >= perms[ n_perm + 1 ] )
}else{
pvalue[1] = NA
}
if( file.exists(paste0(path, "SKATO_perm.RData")) ){
load( paste0(path, "SKATO_perm.RData") )
n_perm = nrow(SKATO_permutations) - 1
perms = apply(X = -log(SKATO_permutations,base=10), MARGIN = 1, FUN = max)
pvalue[2] = mean( perms >= perms[ n_perm + 1 ] )
}else{
pvalue[2] = NA
}
if( file.exists(paste0(path, "dCorN_perm.RData")) ){
load( paste0(path, "dCorN_perm.RData") )
n_perm = nrow(dCorN_permutation) - 1
perms = apply(X = dCorN_permutation , MARGIN = 1, FUN = max)
pvalue[3] = mean( perms >= perms[ n_perm + 1 ] )
}else{
pvalue[3] = NA
}
if( file.exists(paste0(path, "Mantel_perm.RData")) ){
load( paste0(path, "Mantel_perm.RData") )
n_perm = nrow(Mantel_permutation) - 1
perms = apply(X = Mantel_permutation^2 , MARGIN = 1, FUN = max)
pvalue[4] = mean( perms >= perms[ n_perm + 1 ] )
}else{
pvalue[4] = NA
}
names(pvalue) = c("FET", "SKATO", "dCorN", "Mantel")
return(pvalue)
}