forked from esherm/intSiteCaller
-
Notifications
You must be signed in to change notification settings - Fork 5
/
debugCluster.R
75 lines (73 loc) · 3.33 KB
/
debugCluster.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
# This source code file is a component of the larger INSPIIRED genomic analysis software package.
# Copyright (C) 2016 Frederic Bushman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
library(dplyr)
cluster_multihits <- function(unclusteredMultihits, method, unique = TRUE){
if(method == "gt.reduce.cluster"){
flank.sites <- flank(unclusteredMultihits, -1, start = TRUE)
red.sites <- reduce(flank.sites, min.gapwidth = 5L, with.revmap = TRUE)
revmap <- red.sites$revmap
edgelist <- unique(matrix(
c(Rle(unclusteredMultihits$readPairKey[sapply(revmap, "[", 1)],
sapply(revmap, length)),
unclusteredMultihits$readPairKey[unlist(revmap)]),
ncol = 2))
graph <- graph.edgelist(edgelist, directed = FALSE)
clusters <- clusters(graph)
unclusteredMultihits$cid <- clusters$membership[unclusteredMultihits$readPairKey]
split(unclusteredMultihits, unclusteredMultihits$cid)
}else if(method == "iter.reduce.cluster"){
flank.sites <- flank(unclusteredMultihits, -1, start = TRUE)
red.sites <- reduce(flank.sites, min.gapwidth = 5L, with.revmap=TRUE)
pair.revmap <- lapply(red.sites$revmap, function(idx) unclusteredMultihits$readPairKey[idx])
cid <- rep(NA, max(as.numeric(factor(unclusteredMultihits$readPairKey))))
for(i in 1:length(pair.revmap)) {
posidx <- pair.revmap[[i]]
if( all(is.na(cid[posidx])) ) {
cid[posidx] <- i
} else {
precid <- unique(cid[posidx])
precid <- precid[!is.na(precid)]
mincid <- min(precid)
cid[cid %in% precid] <- mincid
cid[posidx] <- mincid
}
}
sites$cid <- as.numeric(factor(cid[unclusteredMultihits$readPairKey]))
split(unclusteredMultihits, unclusteredMultihits$cid)
}else if(method == "gt.findOverlaps.cluster"){
if(unique){
multihits.split <- unique(split(unclusteredMultihits, unclusteredMultihits$ID))
}else{
multihits.split <- split(unclusteredMultihits, unclusteredMultihits$ID)
}
multihits.split <- flank(multihits.split, -1, start=T)
overlaps <- findOverlaps(multihits.split, multihits.split, maxgap=5)
edgelist <- matrix(c(queryHits(overlaps), subjectHits(overlaps)), ncol=2)
clusteredMultihitData <- clusters(graph.edgelist(edgelist, directed=F))
clusteredMultihitNames <- split(names(multihits.split), clusteredMultihitData$membership)
if(unique){
clusteredMultihitPositions <- GRangesList(lapply(clusteredMultihitNames, function(x){
unname(granges(unique(unlist(multihits.split[x]))))
}))
}else{
clusteredMultihitPositions <- GRangesList(lapply(clusteredMultihitNames, function(x){
unname(granges(unlist(multihits.split[x])))
}))
}
clusteredMultihitPositions
}
}