Skip to content

Instantly share code, notes, and snippets.

@wenjie1991
Created March 30, 2020 19:21
Show Gist options
  • Save wenjie1991/2a6a09316ea5d07d7820c64a24a8ab96 to your computer and use it in GitHub Desktop.
Save wenjie1991/2a6a09316ea5d07d7820c64a24a8ab96 to your computer and use it in GitHub Desktop.
prototype RNAscope statistics
f1 = "../data/graph_pixel/ROI_170220-40x-tumorR2-405DAPI-488pLgr5-561mTomato-642pThbs1.20_MERGED.tif_Z#44C#1_Simple Segmentation.tif.txt"
f2 = "../data/graph_pixel/ROI_170220-40x-tumorR2-405DAPI-488pLgr5-561mTomato-642pThbs1.20_MERGED.tif_Z#44C#2_Simple Segmentation.tif.txt"
d1 = fread(f1)
d2 = fread(f2)
d1[, channel := "channel1"]
d2[, channel := "channel2"]
d = rbind(d1, d2)
ggplot(d, aes(x = V1, y = V2) ) + geom_point(aes(fill = channel, col = channel), alpha = 0.4) + theme_bw()
ggplot(d, aes(x = V1, y = V2) ) + geom_point(aes(fill = channel, col = channel), alpha = 0.1, size = 5) + theme_bw()
ggplot(d, aes(x = V1, y = V2) ) + geom_point(aes(fill = channel, col = channel), alpha = 0.1, size = 15) + theme_bw()
# calc the distance matrix and link the nodes
m1 = d1[, 1:2, with=F] %>% data.matrix
m2 = d2[, 1:2, with=F] %>% data.matrix
distance = data.frame(
x1 = rep(m1[, 1, drop=T], nrow(m2)),
y1 = rep(m1[, 2, drop=T], nrow(m2)),
x2 = rep(m2[, 1, drop=T], each = nrow(m1)),
y2 = rep(m2[, 2, drop=T], each = nrow(m1)),
distance = Inf
)
apply(distance, 1, function(l) {
sqrt((l[1] - l[3]) ^ 2 + (l[2] - l[4]) ^ 2)
}) -> distance_v
distance$distance = distance_v
hist(distance$distance)
distance_sub = distance[distance$distance < 20, ]
nrow(distance_sub)
plot(x = m1[, 1], y = m1[, 2], col = "red", pch = '.')
points(x = m2[, 1], y = m2[, 2], col = "blue", pch='.')
plot(x = m1[, 1], y = m1[, 2], col = "red", pch = '.')
points(x = m2[, 1], y = m2[, 2], col = "blue", pch='.')
for (i in 1:nrow(distance_sub)) {
lines(x = distance_sub[i, c(1, 3)], y = distance_sub[i, c(2, 4)], col = "green")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment