-
Notifications
You must be signed in to change notification settings - Fork 2
/
champ.Overlap.R
46 lines (36 loc) · 1.64 KB
/
champ.Overlap.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
# A script to overlap two Genome-Based segmenets. For example, peaks (CpGs) with genome annotation (CpGIslands, Histone Position)
# Author: Tian
library("GenomicRanges")
library("data.table")
ConcatCharacterArray <- function(x) {
paste(x, collapse=";")
}
champ.Overlap <- function(x,
y,
yAttributes=c("feature", "transcriptClass", "geneSymbol"),
collapseFunctions=list(ConcatCharacterArray, ConcatCharacterArray, ConcatCharacterArray))
{
if(!all(yAttributes %in% colnames(y))) stop("At least one yAttributes does not exist in y data frame.")
x.gr <- makeGRangesFromDataFrame(x)
y.gr <- makeGRangesFromDataFrame(y)
message("Finding overlap between x and y.")
ov <- GenomicRanges::findOverlaps(x.gr, y.gr)
ovY.dt <- setDT(y[subjectHits(ov), ])
ovY.dt[, xIndex := queryHits(ov)]
ovY.dt[, yIndex := subjectHits(ov)]
message("Collapse y for each x.")
ovXResult.dt <- ovY.dt[, .(.N, yIndexList = paste(yIndex, collapse=";")), by = .(xIndex)]
if(length(yAttributes) > 0) {
for(i in 1:length(yAttributes)) {
message("Collapsing ", yAttributes[i], " ...")
ovY.dt[, "tmpColumn" := ovY.dt[, get(yAttributes[i])]]
tmp.dt <- ovY.dt[, .(collapseFunctions[[i]](tmpColumn)), by = .(xIndex)]
ovXResult.dt <- merge(ovXResult.dt, tmp.dt)
}
}
message("Prepare data frame for return.")
ovXResult <- as.data.frame(ovXResult.dt)
colnames(ovXResult) <- c("xIndex", "yOvNumber", "yIndexList", yAttributes)
ovXResult <- cbind(x[ovXResult$xIndex, ], ovXResult)
return(ovXResult)
}