-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbed2pwm.r
82 lines (62 loc) · 1.96 KB
/
bed2pwm.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
#!/usr/bin/Rscript
#Rscript script common-path jaspar-dir id output
#example:
#Rscript pwmmatch.r /cluster/thashim/basepiq/common.r /cluster/thashim/basepiq/pwms/jaspar.txt 141 /cluster/thashim/basepiq/tmp/pwmout.RData
options(echo=TRUE)
args <- commandArgs(trailingOnly = TRUE)
print(args)
commonfile = args[1]
bedin = args[2]
bedname = args[3]
bedid = as.double(args[4])
outdir = args[5]
pwmname = bedname
pwmid = bedid
outdir=paste0(outdir,'/')
source(commonfile)
if(!overwrite & file.exists(paste0(outdir,pwmid,'.pwmout.RData'))){
stop("pwm file already exists")
}
####
# load PWMs
####
require('GenomicRanges')
rt = read.table(bedin)
gr = GRanges(rt[,1],ranges=IRanges(start=rt[,2],end=rt[,3]),strand=rt[,6],score=rt[,5])
chrstr = unique(format(rt[,1],justify='none'))
gstr = gr[strand(gr)=='+']
coords=lapply(chrstr,function(i){
gsub=gstr[seqnames(gstr)==i]
ranges(gsub)
})
coords.pwm=lapply(chrstr,function(i){
gsub=gstr[seqnames(gstr)==i]
score(gsub)
})
clengths=sapply(coords,length)
print(sum(clengths))
coords.short=coords[clengths>0]
names(coords.short)=chrstr[clengths>0]
ncoords=chrstr[clengths>0]#names(coords)
coords2=sapply(coords.short,flank,width=wsize,both=T)
pwmin=matrix(rep(1,16),4,4)
ipr=matrix(rep(0,16),4,4)
save(coords,coords.pwm,ipr,pwmin,pwmname,chrstr,clengths,coords.short,ncoords,coords2,file=paste0(outdir,pwmid,'.pwmout.RData'))
gstr = gr[strand(gr)=='-']
coords=lapply(chrstr,function(i){
gsub=gstr[seqnames(gstr)==i]
ranges(gsub)
})
coords.pwm=lapply(chrstr,function(i){
gsub=gstr[seqnames(gstr)==i]
score(gsub)
})
clengths=sapply(coords,length)
print(sum(clengths))
coords.short=coords[clengths>0]
names(coords.short)=chrstr[clengths>0]
ncoords=chrstr[clengths>0]#names(coords)
coords2=sapply(coords.short,flank,width=wsize,both=T)
pwmin=matrix(rep(1,16),4,4)
ipr=matrix(rep(0,16),4,4)
save(coords,coords.pwm,ipr,pwmin,pwmname,chrstr,clengths,coords.short,ncoords,coords2,file=paste0(outdir,pwmid,'.pwmout.rc.RData'))