-
Notifications
You must be signed in to change notification settings - Fork 2
/
seekG4hunt.r
154 lines (137 loc) · 4.03 KB
/
seekG4hunt.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
##########################################################
G4translate <- function(y,v1=1,v2=2,v3=3,v4=4)
# x a DNAString or a DNAStringSet (or just a string of char)
{
require(S4Vectors)
x= Rle(strsplit(as.character(y),NULL)[[1]])
xres=x
runValue(xres)[runValue(x)=='C' & runLength(x)>3] <- -v4
runValue(xres)[runValue(x)=='C' & runLength(x)==3] <- -v3
runValue(xres)[runValue(x)=='C' & runLength(x)==2] <- -v2
runValue(xres)[runValue(x)=='C' & runLength(x)==1] <- -v1
runValue(xres)[runValue(x)=='G' & runLength(x)>3] <- v4
runValue(xres)[runValue(x)=='G' & runLength(x)==3] <- v3
runValue(xres)[runValue(x)=='G' & runLength(x)==2] <- v2
runValue(xres)[runValue(x)=='G' & runLength(x)==1] <- v1
runValue(xres)[runValue(x)!='C' & runValue(x)!='G'] <- 0 # N or U are not a problem
Rle(as.numeric(xres))
}
################################################################################
################################################################################
##### return the G4Hscore. y is a string of char (DNA sequence)
G4Hscore <- function(y)
{
y3=G4translate(y)
mean(y3)
}
################################################################################
################################################################################
##### function to refine G4hunt results
G4startrun=function(y,chrom=chr,letter='C') #y is a START
{
if (letter(chrom,y)==letter)
{
while (letter(chrom,y)==letter & y!=1)
{
if (letter(chrom,y-1)==letter)
{
y <- y-1
}else{
break
}
}
}else{
y=y+1
while (letter(chrom,y)!=letter) {y=y+1}
}
return(y)
}
G4endrun=function(y,chrom=chr,letter='C') #y is a END
{
if (letter(chrom,y)==letter)
{
while (letter(chrom,y)==letter & y!=length(chrom))
{
if (letter(chrom,y+1)==letter)
{
y <- y+1
}else{
break
}
}
}else{
y=y-1
while (letter(chrom,y)!=letter) {y=y-1}
}
return(y)
}
################################################################################
################################################################################
## modified mG4hunt to add the refining procedure
modG4huntref <- function(k=25,hl=1.5,chr,seqname='target',with.seq=T,Gseq.only=F)
{
require(GenomicRanges,Biostrings)
#### k=RUNMEAN WINDOW SIZE, hl=threshold
tchr <- G4translate(chr)
chr_G4hk <- runmean(tchr,k)
j <- hl
chrCh <- Views(chr_G4hk, chr_G4hk<=(-j))
chrGh <- Views(chr_G4hk, chr_G4hk>=j)
IRC <- reduce(IRanges(start=start(chrCh),end=(end(chrCh)+k-1)))
if (length(IRC)==0)
{
nxC <- GRanges()
}else{
nnIRC=IRC
start(nnIRC)=sapply(start(IRC),G4startrun,letter='C',chrom=chr)
end(nnIRC)=sapply(end(IRC),G4endrun,letter='C',chrom=chr)
seqC=as.character(Views(chr,nnIRC))
if (Gseq.only)
{
nnseqC=as.character(reverseComplement(Views(chr,nnIRC)))
}else{
nnseqC=as.character(Views(chr,nnIRC))
}
nG4scoreC=sapply(seqC,function(x) signif(G4Hscore(x),3))
mscoreC <- signif(min(Views(chr_G4hk,IRC)),3)
straC <- Rle(rep('-',length(IRC)))
hlC <- Rle(rep(j,length(IRC)))
kC <- Rle(rep(k,length(IRC)))
nxC <- GRanges(seqnames=Rle(seqname),
ranges=nnIRC,
strand=straC,
score=nG4scoreC,
max_score=mscoreC,
hl=hlC,
k=kC,
sequence=nnseqC)
}
IRG <- reduce(IRanges(start=start(chrGh),end=(end(chrGh)+k-1)))
if (length(IRG)==0)
{
nxG <- GRanges()
}else{
nnIRG=IRG
start(nnIRG)=sapply(start(IRG),G4startrun,letter='G',chrom=chr)
end(nnIRG)=sapply(end(IRG),G4endrun,letter='G',chrom=chr)
nnseqG=as.character(Views(chr,nnIRG))
nG4scoreG=sapply(nnseqG,function(x) signif(G4Hscore(x),3))
mscoreG <- signif(max(Views(chr_G4hk,IRG)),3)
straG <- Rle(rep('+',length(IRG)))
hlG <- Rle(rep(j,length(IRG)))
kG <- Rle(rep(k,length(IRG)))
nxG <- GRanges(seqnames=Rle(seqname),
ranges=nnIRG,
strand=straG,
score=nG4scoreG,
max_score=mscoreG,
hl=hlG,
k=kG,
sequence=nnseqG)
}
nx <- sort(c(nxC,nxG),ignore.strand=T)
names(nx) <- NULL
if (with.seq==F) {nx$sequence=NULL}
return(nx)
}
############