1
1
library(' RANN' )
2
- # main function that generates the paths
3
- generate_tad_random = function (hops = 1000 , origin = rep(0 ,3 ), limit = 1 , abs_limit = limit * 4 ,scale = 0.1 , min_delta = scale , cis = 50 , warp = NULL ){
2
+ # main function that generates the paths. A path is a list of connected x,y,x coords and it's length is set by _hops_.
3
+ # origin = the triplet coordinate where a path begins
4
+ # limit = linear distance away from the origin after the path will be penalized and forced to return towards the origin
5
+ # it determines the extent of a domain.
6
+ # abs_limit = as the path grows (see warp) one might one to bound the scope of the full path. This determines the global limits of the path
7
+ # scale = scaling factor
8
+ # min_delta = minimim step required by the walker to call a hop valid
9
+ # cis =
10
+ # warp = a path can suddenly do an abrupt, long-range hop in order to start populating a different volume. This setting determines how ofter an warp will happen. Once the path warps, the origin is reset.
11
+ generate_tad_random = function (hops = 1000 , origin = rep(0 ,3 ), limit = 1 , abs_limit = limit * 4 ,scale = 0.1 , min_delta = scale , cis = 50 , warp = NULL ){
4
12
5
13
central_origin = origin
6
14
tad_loc = data.frame (x = origin [1 ], y = origin [2 ], z = origin [3 ])
7
15
# tad_loc = origin
8
16
current = origin
9
- pos = data.frame (x = current [1 ], y = current [2 ], z = current [3 ])
17
+ pos = data.frame (x = current [1 ], y = current [2 ], z = current [3 ])
10
18
11
19
delta = c(0 ,0 ,0 )
12
20
mem_trail = delta
@@ -49,17 +57,17 @@ generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_lim
49
57
}
50
58
}
51
59
iter = iter + 1
52
- fhop = tensor(origin , current , limit = limit , scale = scale )
53
- if (hop > = cis ){
60
+ fhop = tensor(origin , current , limit = limit , scale = scale )
61
+ if (hop > = cis ){
54
62
memory = memory_centroid(pos [(nrow(pos )- cis ): nrow(pos ), ])
55
63
} else {
56
64
memory = colMeans(pos ) # current + rnorm(3, 0, min_delta)
57
65
}
58
66
59
67
# cat(sprintf("last jump was of %.2f", euclidean_distance(fhop$pos, current)))
60
68
# message(sprintf("from the origin %.2f", euclidean_distance(fhop$pos, origin)))
61
- if (nrow(pos )> 0 ){
62
- knn = nn2(as.matrix(pos ), matrix (fhop $ pos , ncol = 3 , nrow = 1 ), k = nrow(pos ))
69
+ if (nrow(pos ) > 0 ){
70
+ knn = nn2(as.matrix(pos ), matrix (fhop $ pos , ncol = 3 , nrow = 1 ), k = nrow(pos ))
63
71
knn = knn $ nn.dist [1 ,1 ] > min_delta * 1.52
64
72
}else {
65
73
knn = TRUE
@@ -75,7 +83,7 @@ generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_lim
75
83
delta = c(delta , fhop $ delta )
76
84
current = fhop $ pos
77
85
hop = hop + 1
78
- cat(sprintf(" \r %.2f%%\t %s" , 100 * hop / hops , iter ))
86
+ cat(sprintf(" \r %.2f%%\t %s" , 100 * hop / hops , iter ))
79
87
}
80
88
81
89
@@ -85,16 +93,16 @@ generate_tad_random = function(hops = 1000, origin = rep(0,3), limit =1, abs_lim
85
93
# path = matrix(pos, ncol=3, byrow=T)
86
94
path = pos
87
95
colnames(path ) = c(" x" ," y" ," z" )
88
- deltas = matrix (delta , ncol = 3 , byrow = T )
96
+ deltas = matrix (delta , ncol = 3 , byrow = T )
89
97
colnames(deltas ) = c(" dx" ," dy" ," dz" )
90
98
91
99
# as.data.frame(cbind(path, deltas))
92
100
# as.data.frame(path)
93
- list (path = as.data.frame(path ), deltas = deltas , mem_trail = mem_trail , cis = cis )
101
+ list (path = as.data.frame(path ), deltas = deltas , mem_trail = mem_trail , cis = cis )
94
102
}
95
103
96
104
# main engine that suggests the next step on a path
97
- tensor = function (origin = c(0 ,0 ,0 ), current = c(0 ,0 ,0 ), limit = 1 , penalty_factor = 0.25 , scale = 0.1 ){
105
+ tensor = function (origin = c(0 ,0 ,0 ), current = c(0 ,0 ,0 ), limit = 1 , penalty_factor = 0.25 , scale = 0.1 ){
98
106
pf = penalty_factor
99
107
x = current [1 ]
100
108
y = current [2 ]
@@ -106,15 +114,15 @@ tensor= function(origin=c(0,0,0), current=c(0,0,0), limit = 1, penalty_factor =
106
114
nx = rnorm(1 , 0 , scale )
107
115
ny = rnorm(1 , 0 , scale )
108
116
nz = rnorm(1 , 0 , scale )
109
- delta = c(nx , ny , nz ) + (c(dx ,dy ,dz ) * sapply(c(0 ,0 ,0 ), function (x ){ifelse(sign(x )== 0 , 1 , sign(x ))}))
117
+ delta = c(nx , ny , nz ) + (c(dx ,dy ,dz ) * sapply(c(0 ,0 ,0 ), function (x ){ifelse(sign(x ) == 0 , 1 , sign(x ))}))
110
118
111
119
} else {
112
120
nx = rnorm(1 , 0 , scale )
113
121
ny = rnorm(1 , 0 , scale )
114
122
nz = rnorm(1 , 0 , scale )
115
123
delta = - c(nx , ny , nz )
116
124
}
117
- return (list (pos = current + delta , delta = delta ))
125
+ return (list (pos = current + delta , delta = delta ))
118
126
}
119
127
# basic functions
120
128
euclidean_distance = function (a , b ){
@@ -131,12 +139,12 @@ memory_centroid = function(a){
131
139
colMeans(a )
132
140
}
133
141
134
- knn_path = function (tad_list , k = 100 , len = 20 , region = 20 : 30 ){
142
+ knn_path = function (tad_list , k = 100 , len = 20 , region = 20 : 30 ){
135
143
136
144
end = nrow(tad_list $ path )
137
- start = tad_list $ cis + (end * 0.1 )
145
+ start = tad_list $ cis + (end * 0.1 )
138
146
139
- knn = nn2(tad_list $ path , tad_list $ path , k = 100 )
147
+ knn = nn2(tad_list $ path , tad_list $ path , k = 100 )
140
148
k = c()
141
149
142
150
x = sample(start : end , 1 )
@@ -149,7 +157,7 @@ knn_path = function(tad_list, k=100, len=20, region=20:30){
149
157
}
150
158
}
151
159
dev.new()
152
- plot(tad_list $ path [k ,], t = ' l' )
160
+ plot(tad_list $ path [k ,], t = ' l' )
153
161
return (tad_list $ path [k ,])
154
162
}
155
163
@@ -167,19 +175,18 @@ plot_progress = function(pos, mem_trail, warp = NULL){
167
175
n_mem = nrow(mem_trail )
168
176
f_warp = 1 : n_mem %% warp == 0
169
177
170
- plot(pos [, 1 ], pos [, 2 ], t = ' l' , xlab = " X" , ylab = " Y" )
171
- points(mem_trail [f_warp , 1 ], mem_trail [f_warp , 2 ], pch = 19 , col = ' red' )
172
- abline(v = mem_trail [n_mem , 1 ], h = mem_trail [n_mem , 2 ], col = ' lightblue' , lty = 2 , lwd = 2 )
178
+ plot(pos [, 1 ], pos [, 2 ], t = ' l' , xlab = " X" , ylab = " Y" )
179
+ points(mem_trail [f_warp , 1 ], mem_trail [f_warp , 2 ], pch = 19 , col = ' red' )
180
+ abline(v = mem_trail [n_mem , 1 ], h = mem_trail [n_mem , 2 ], col = ' lightblue' , lty = 2 , lwd = 2 )
173
181
174
- plot(pos [, 1 ], pos [, 3 ], t = ' l' , xlab = " X" , ylab = " Z" )
175
- points(mem_trail [f_warp , 1 ], mem_trail [f_warp , 3 ],pch = 19 , col = ' red' )
176
- abline(v = mem_trail [n_mem , 1 ], h = mem_trail [n_mem , 3 ], col = ' lightblue' , lty = 2 , lwd = 2 )
182
+ plot(pos [, 1 ], pos [, 3 ], t = ' l' , xlab = " X" , ylab = " Z" )
183
+ points(mem_trail [f_warp , 1 ], mem_trail [f_warp , 3 ], pch = 19 , col = ' red' )
184
+ abline(v = mem_trail [n_mem , 1 ], h = mem_trail [n_mem , 3 ], col = ' lightblue' , lty = 2 , lwd = 2 )
177
185
178
- plot(pos [, 2 ], pos [, 3 ], t = ' l' , xlab = " Y" , ylab = " Z" )
179
- points(mem_trail [f_warp , 2 ], mem_trail [f_warp , 3 ],pch = 19 , col = ' red' )
180
- abline(v = mem_trail [n_mem , 2 ], h = mem_trail [n_mem , 3 ], col = ' lightblue' , lty = 2 , lwd = 2 )
186
+ plot(pos [, 2 ], pos [, 3 ], t = ' l' , xlab = " Y" , ylab = " Z" )
187
+ points(mem_trail [f_warp , 2 ], mem_trail [f_warp , 3 ], pch = 19 , col = ' red' )
188
+ abline(v = mem_trail [n_mem , 2 ], h = mem_trail [n_mem , 3 ], col = ' lightblue' , lty = 2 , lwd = 2 )
181
189
182
-
183
190
# plot(mem_trail[,1], mem_trail[,2], t='l', col='red')
184
191
# plot(mem_trail[,2], mem_trail[,3], t='l', col='red')
185
192
# plot(mem_trail[,1], mem_trail[,3], t='l', col='red')
0 commit comments