annotate tools/mytools/genomeview-old2.r @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 caloffset = function(genome){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 total_len = sum(as.numeric(genome[,2]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 offset = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 for (i in 1:nrow(genome)) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 offset = c(offset,offset[i]+genome[i,2])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 offset
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 coverage = function(intervals,genome,offset,resolution) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 nChr = length(offset) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 total_len = offset[nChr+1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 nbin = as.integer(total_len / resolution)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 #cat('nbin=',nbin,'genomelen=',total_len,'\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 cov = numeric(nbin)#coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 col = numeric(nbin)#color
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 for (i in 1:nChr) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 d = x[x[,1]==as.character(genome[i,1]),2:3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 if (nrow(d) > 0){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 #cat('dim(d)=',dim(d),'\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 d = ceiling((d+offset[i])*nbin/total_len)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 for (j in 1:nrow(d)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 cov[d[j,1]:d[j,2]] = cov[d[j,1]:d[j,2]] + 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 col[ceiling(offset[i]*nbin/total_len):ceiling(offset[i]*nbin/total_len)] = i
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 list(nbin=nbin,cov=cov)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 # plot coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 # res = genomeView(x,genome,100000)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 plotcov = function(res,genome,offset,title,uselog) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if (uselog == 'log'){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 res$cov = log10(res$cov+1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 ymax = max(res$cov)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 par(mar=c(5,5,5,1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 plot(seq(length(res$cov)),res$cov,type='h',cex=0.1,cex.axis=2,cex.lab=2,cex.main=3,col=res$col,xaxt='n',main=title,xlab='chromosome',ylab='coverage',frame.plot=F,ylim=c(0,ymax))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 xticks = numeric(nrow(genome))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 for (i in 1:nrow(genome)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 xticks[i] = (offset[i]+offset[i+1])/2*res$nbin/offset[length(offset)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 mtext(genome[,1],side=1,at=xticks,adj=1,las=2,col=seq(nrow(genome)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 union_correlation = function(x,y){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 z = x>0 | y>0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 cor(x[z],y[z])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 }