-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.r
246 lines (232 loc) · 13.3 KB
/
graphics.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#graphics.r
#generate dn/ds distro plots
layout(matrix(c(1,1,0,2),2,2,byrow=TRUE)) #4 plots, 2 rows, 2 columns
regionstable <- read.delim("~/work/data/pmodeldata/regionsmafsdnds.bed.gz", header=T, comment.char="", check.names=F)
colnames(regionstable)[colnames(regionstable)=='#chr']<-'chr'
colnames(regionstable)[colnames(regionstable)=='autoregs(uniqid for non-domain regions)']<-'autoreg'
colnames(regionstable)[colnames(regionstable)=='dn/ds']<-'dnds'
library(dplyr)
library(stringr)
r<-regionstable
r$mafs<-str_split(r$mafs,",")
r$type<-str_split(r$type,",")
r$maf_modifier<-0.001
for (i in 1:length(r$mafs)) {r$type[[i]]<-r$type[[i]][as.double(r$mafs[[i]])>0.001]; r$mafs[[i]]<-r$mafs[[i]][as.double(r$mafs[[i]])>0.001];}
r<-arrange(r,desc(dnds))
plot(r$dnds,ylim=c(0,6),ylab="dn/ds",main="dN/dS distribution across all protein regions")
abline(h=mean(r$dnds))
abline(h=median(r$dnds))
text(x=c(length(r$dnds)*3/4,length(r$dnds)*3/4),cex=0.7,y=c(mean(r$dnds),median(r$dnds)),pos=3,labels=c(paste('mean=',mean(r$dnds),sep=''),paste('median=',median(r$dnds),sep='')))
legend('topright','MAF>0.001%',bty='n') ## for common
hist(r$dnds,breaks=100,xlab="dN/dS",main="dN/dS distribution for all protein regions",xlim=c(0,5))
legend('topright','MAF>0.001%',bty='n') ## for common
plot(r$length,r$dnds,xlab="length",ylab="dn/ds",main="dN/dS vs length of region for all protein regions")
legend('topright','MAF>0.001%',bty='n') ## for common
plot(r$density,r$dnds,xlab="density",ylab="dn/ds",main="dN/dS vs density of region for all protein regions")
legend('topright','MAF>0.001%',bty='n') ## for common
plot(r$fvrv,r$dnds,xlab="FVRV",ylab="dn/ds",main="dN/dS vs FVRV of region for all protein regions")
legend('topright','MAF>0.001%',bty='n') ## for common
u<-regionstable[regionstable$domain!=".",]
u<-arrange(u,desc(dnds))
plot(u$dnds,ylim=c(0,6),ylab="dn/ds",main="dN/dS distribution across domain regions") # dnds dist
abline(h=mean(u$dnds))
abline(h=median(u$dnds))
text(x=c(length(u$dnds)*3/4,length(u$dnds)*3/4),cex=0.7,y=c(mean(u$dnds),median(u$dnds)),pos=3,labels=c(paste('mean=',mean(u$dnds),sep=''),paste('median=',median(u$dnds),sep='')))
text(x=c(length(n$dnds)*.3),y=5,labels=c("MAF>0.001%")) ## for common
hist(u$dnds,breaks=100,xlab="dN/dS",main="dN/dS distribution for domains",xlim=c(0,5)) # histogram
legend('topright','MAF>0.001%',bty='n') ## for common
plot(u$length,u$dnds,xlab="length",ylab="dn/ds",main="dN/dS vs length of region for domain regions") # length vs dnds
legend('topright','MAF>0.001%',bty='n') ## for common
plot(u$density,u$dnds,xlab="density",ylab="dn/ds",main="dN/dS vs density of region for domain regions")
legend('topright','MAF>0.001%',bty='n') ## for common
plot(u$fvrv,u$dnds,xlab="FVRV",ylab="dn/ds",main="dN/dS vs FVRV of region for all domain regions")
legend('topright','MAF>0.001%',bty='n') ## for common
n<-regionstable[regionstable$domain==".",]
n<-arrange(n,desc(dnds))
plot(n$dnds,ylim=c(0,6),ylab="dn/ds",main="dN/dS distribution across non-domain regions")
abline(h=mean(n$dnds))
abline(h=median(n$dnds))
text(x=c(length(n$dnds)*3/4,length(n$dnds)*3/4),cex=0.7,y=c(mean(n$dnds),median(n$dnds)),pos=3,labels=c(paste('mean=',mean(n$dnds),sep=''),paste('median=',median(n$dnds),sep='')))
text(x=c(length(n$dnds)*.3),y=5,labels=c("MAF>0.001%")) ## for common
hist(n$dnds,breaks=100,xlab="dN/dS",main="dN/dS distribution for non-domain regions",xlim=c(0,5))
legend('topright','MAF>0.001%',bty='n') ## for common
plot(n$length,n$dnds,xlab="length",ylab="dn/ds",main="dN/dS vs length of region for non-domain regions")
legend('topright','MAF>0.001%',bty='n') ## for common
plot(n$density,n$dnds,xlab="density",ylab="dn/ds",main="dN/dS vs density of region for non-domain regions")
legend('topright','MAF>0.001%',bty='n') ## for common
plot(n$fvrv,n$dnds,xlab="FVRV",ylab="dn/ds",main="dN/dS vs FVRV of region for non-domain regions")
legend('topright','MAF>0.001%',bty='n') ## for common
genetablefilter <- read.table("~/work/data/pmodeldata/genetablefilter.g.0.0001-.txt", quote="\"")
g<-genetablefilter
g<-arrange(g,desc(V6))
png("~/Documents/workimages/maf0.01/genednds.png",width=1603,height=800,units="px")
plot(g$V6,ylim=c(0,6),ylab="dn/ds",main="dN/dS distribution across genes")
abline(h=mean(g$V6))
abline(h=median(g$V6))
text(x=c(length(g$V6)*3/4,length(g$V6)*3/4),cex=0.7,y=c(mean(g$V6),median(g$V6)),pos=3,labels=c(paste('mean=',mean(g$V6),sep=''),paste('median=',median(g$V6),sep='')))
text(x=c(length(g$V6)*.3),y=5,labels=c("MAF>0.001%")) ## for common
dev.off()
png("~/Documents/workimages/maf0.01/genedndshist.png",width=1603,height=800,units="px")
hist(g$V6,breaks=100,xlab="dN/dS",main="dN/dS distribution for genes",xlim=c(0,5))
legend('topright','MAF>0.001%',bty='n') ## for common
dev.off()
png("~/Documents/workimages/maf0.01/genedndslength.png",width=1603,height=800,units="px")
plot(g$V2,g$V6,xlab="length",ylab="dn/ds",main="dN/dS vs length of region for genes")
legend('topright','MAF>0.001%',bty='n') ## for common
dev.off()
png("~/Documents/workimages/maf0.01/genedndsdensity.png",width=1603,height=800,units="px")
plot(g$V7,g$V6,xlab="density",ylab="dn/ds",main="dN/dS vs density of region for genes")
legend('topright','MAF>0.001%',bty='n') ## for common
dev.off()
uniqgenetablefilter <- read.table("~/work/data/pmodeldata/uniqgenetablefilter.g.0.0001-.txt", quote="\"")
gu<-uniqgenetablefilter
gu<-arrange(gu,desc(V6))
plot(gu$V6,ylim=c(0,6),ylab="dn/ds",main="dN/dS distribution across genes without nodoms")
abline(h=mean(gu$V6))
abline(h=median(gu$V6))
text(x=c(length(gu$V6)*3/4,length(gu$V6)*3/4),cex=0.7,y=c(mean(gu$V6),median(gu$V6)),pos=3,labels=c(paste('mean=',mean(gu$V6),sep=''),paste('median=',median(gu$V6),sep='')))
text(x=c(length(gu$V6)*.3),y=5,labels=c("MAF>0.001%")) ## for common
hist(gu$V6,breaks=100,xlab="dN/dS",main="dN/dS distribution for genes without nodoms",xlim=c(0,5))
legend('topright','MAF>0.001%',bty='n') ## for common
plot(gu$V2,gu$V6,xlab="length",ylab="dn/ds",main="dN/dS vs length of genes without nodoms")
legend('topright','MAF>0.001%',bty='n') ## for common
plot(gu$V7,gu$V6,xlab="density",ylab="dn/ds",main="dN/dS vs density of region for genes without nodoms")
legend('topright','MAF>0.001%',bty='n') ## for common
#for density plots and other plots
library(ggplot2)
library(plotrix)
library(RColorBrewer)
DATA<-"~/work/data/pmodeldata"
dtable <- read.delim("~/work/data/pmodeldata/dtable.txt")
clans <- read.delim("~/work/data/pmodeldata/count_human_pfam_clan.tab")
m<-merge(dtable,clans,by.x="domain",by.y="pfamA_id")
write.table(m,paste(DATA,"/clans.txt",sep=""),sep="\t",row.names=FALSE,quote=FALSE)
echo -e "clan_acc\tclan_id\tnonsynct\tsynct\ttotalvarct\tdomcount\ttotalbp\tmmaf\tdn.ds\tvar.bp.ratio" > $DATA/ctable.txt
sed '1d' $DATA/clans.txt | sort -k12,12 | grep -v -w "NA" | bedtools groupby -g 12,13 -c 3,4,5,6,7,8,9,10 -o sum,sum,sum,sum,sum,sum,sum,sum | awk '{printf $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7"\t"$8"\t"; if ($4!=0) printf $3/$4; else printf $3/(1+$4); printf "\t"$5/$7"\n"}' >> $DATA/ctable.txt
ctable <- read.delim("~/work/data/pmodeldata/ctable.txt")
shuffle <-function(list){
l<-c()
i=1
while (i < length(list)/2){
l[length(l)+1]<-list[i]
l[length(l)+1]<-list[length(list)-i+1]
i=i+1
}
return(l)
}
pointmod <- function(i=dtable,ll=0,ul=14000,w=sturges(t(dtable["mmaf"])),nbin=6,pchcolumn="totalvarct",colorcolumn="mmaf",pchbool=TRUE,colorbool=TRUE){
if (pchbool==FALSE){
pch=NULL
} else{
pch<-c()
plist<-seq(0,nbin-1)
pchleg<-cut_number(t(i[pchcolumn]),nbin)
for (k in 1:length(t(i[pchcolumn]))){
for (j in 1:length(levels(pchleg))){
if (toString(pchleg[k])==pchleg[j]){
pch[length(pch)+1] <- plist[j]
j_=j
break
}
}
}
}
if (colorbool==FALSE){
col<-1
} else{
col <- c()
pal <- shuffle(colorRampPalette(brewer.pal(11,"Spectral"))(length(w)+1))
for (k in 1:length(t(i[colorcolumn]))){
for (j in 1:length(w)){
if (t(i[colorcolumn])[k]<=w[j]){
col[length(col)+1] <- pal[j]
j_=j
break
}
}
if (t(i[colorcolumn])[k]>w[j_]){
col[length(col)+1]<-pal[j_+1]
}
}
}
pchleg<-levels(pchleg)
l<-list(pch,col,pchleg)
return(l)
}
sturges <- function(m=t(dtable["mmaf"]),column="mmaf"){
w <- (range(m)[2]-range(m)[1])/(1+3.322*log(length(m),base=10))
tot <- min(m)
widthlist <- c(tot)
while (tot < max(m)){
widthlist[length(widthlist)+1] <- tot+w
tot <- tot+w
}
if (tot >= max(m)){
return(widthlist)
}
}
subplot <- function(i=dtable,xll=0,xul=6,yll=0,yul=3,pchcolumn="totalvarct",colorcolumn="mmaf",nbin=6,x="dn.ds",y="domcount",xlab="dn/ds",ylab="domain occurrences in exome",xlimiter="dn.ds",ylimiter="domcount",label="domain",labelbool=TRUE,colorbool=TRUE,pchbool=TRUE,xlimitbool=TRUE,ylimitbool=TRUE){
if (xlimitbool==TRUE){
i=i[xll<i[xlimiter] & i[xlimiter]<=xul,]
}
if (ylimitbool==TRUE){
i=i[yll<i[ylimiter] & i[ylimiter]<=yul,]
}
if (colorbool==TRUE){
w<-round(sturges(m=t(i[colorcolumn]),column=colorcolumn),5)
c<-shuffle(colorRampPalette(brewer.pal(11,"Spectral"))(length(w)+1))
} else{
w=NULL
}
l<-pointmod(i=i,pchcolumn=pchcolumn,colorcolumn=colorcolumn,nbin=nbin,w=w,pchbool=pchbool,colorbool=colorbool)
pch<-l[1][[1]]
col<-l[2][[1]]
pchleg<-l[3][[1]]
# par(fig=c(0.1,0.9,0,0.9),new=TRUE)
plot(t(i[x]),t(i[y]),xlab=xlab,ylab=ylab,pch=pch,col=col)
if (labelbool==TRUE){
text(t(i[x]),t(i[y]),labels=t(i[label]),adj=c(.5,-.4),cex=0.5,offset=0.5)
}
# par(fig=c(0,.1,.3,.5),xpd=NA)
if (colorbool==TRUE){
color.legend((max(i[x])-min(i[x]))*.985+min(i[x]),(max(i[y])-min(i[y]))*.75+min(i[y]),max(i[x]),max(i[y]),legend=w,rect.col=c,gradient="y")
}
if (pchbool==TRUE){
legend(par()$usr[1],mean(par()$usr[3:4]),border=rep("white",nbin),legend=pchleg,pch=seq(0,nbin-1),bty="n",xpd=TRUE,xjust=0,yjust=0.5,cex=0.65) #inset=c(0,0) #min(i[x]),(max(i[y])-min(i[y]))*.75+min(i[y]),(max(i[x])-min(i[x]))*.05+min(i[x]),max(i[y])
}
}
plot(dtable$var.bp.ratio,log(dtable$totalbp,base=10),xlab="variants/total length of domain",ylab="total length of domain (log)",cex=cex,col=col)
color.legend(.10,3,.14,5,legend=w,rect.col=c,gradient="y")
f<-dtable[order(-dtable$dn.ds),]
l<-f[f$dn.ds<6]
plot(l$dn.ds,ylab="dn/ds")
text(l$dn.ds,labels=l$domain,cex=.5,pos=4,srt=45)
f<-ctable[order(-ctable$dn.ds),]
l<-f[f$dn.ds<6,]
plot(l$dn.ds,ylab="dn/ds")
text(l$dn.ds,labels=l$clan_id,cex=.5,pos=4,srt=45)
#limiting the maf and then testing distribution of domcount
dtable <- read.delim("~/work/data/pmodeldata/dtablemaf.g.0.001-.txt")
par(mfrow=c(2,4))
hist(t(dtable[dtable["domcount"]==1,]["dn.ds"]),main="domcount=1",xlab="dn.ds",breaks=30,xlim=c(0,ceiling(max(dtable["dn.ds"]))))
text(.85*par('usr')[2],.9*par('usr')[4],labels=paste("mean:",round(mean(t(dtable[dtable["domcount"]==1,]["dn.ds"])),2)))
text(.85*par('usr')[2],.86*par('usr')[4],labels=paste("median:",round(median(t(dtable[dtable["domcount"]==1,]["dn.ds"])),2)))
hist(t(dtable[dtable["domcount"]>1 & dtable["domcount"]<10,]["dn.ds"]),main="10>domcount>1",xlab="dn.ds",breaks=30,xlim=c(0,ceiling(max(dtable["dn.ds"]))))
text(.85*par('usr')[2],.9*par('usr')[4],labels=paste("mean:",round(mean(t(dtable[dtable["domcount"]>1 & dtable["domcount"]<10,]["dn.ds"])),2)))
text(.85*par('usr')[2],.86*par('usr')[4],labels=paste("median:",round(median(t(dtable[dtable["domcount"]>1 & dtable["domcount"]<10,]["dn.ds"])),2)))
hist(t(dtable[dtable["domcount"]>10 & dtable["domcount"]<25,]["dn.ds"]),main="25>domcount>10",xlab="dn.ds",breaks=30,xlim=c(0,ceiling(max(dtable["dn.ds"]))))
text(.85*par('usr')[2],.9*par('usr')[4],labels=paste("mean:",round(mean(t(dtable[dtable["domcount"]>10 & dtable["domcount"]<25,]["dn.ds"])),2)))
text(.85*par('usr')[2],.86*par('usr')[4],labels=paste("median:",round(median(t(dtable[dtable["domcount"]>10 & dtable["domcount"]<25,]["dn.ds"])),2)))
hist(t(dtable[dtable["domcount"]>25 & dtable["domcount"]<50,]["dn.ds"]),main="50>domcount>25",xlab="dn.ds",breaks=30,xlim=c(0,ceiling(max(dtable["dn.ds"]))))
text(.85*par('usr')[2],.9*par('usr')[4],labels=paste("mean:",round(mean(t(dtable[dtable["domcount"]>25 & dtable["domcount"]<50,]["dn.ds"])),2)))
text(.85*par('usr')[2],.86*par('usr')[4],labels=paste("median:",round(median(t(dtable[dtable["domcount"]>25 & dtable["domcount"]<50,]["dn.ds"])),2)))
hist(t(dtable[dtable["domcount"]>50 & dtable["domcount"]<75,]["dn.ds"]),main="75>domcount>50",xlab="dn.ds",breaks=30,xlim=c(0,ceiling(max(dtable["dn.ds"]))))
text(.85*par('usr')[2],.9*par('usr')[4],labels=paste("mean:",round(mean(t(dtable[dtable["domcount"]>50 & dtable["domcount"]<75,]["dn.ds"])),2)))
text(.85*par('usr')[2],.86*par('usr')[4],labels=paste("median:",round(median(t(dtable[dtable["domcount"]>50 & dtable["domcount"]<75,]["dn.ds"])),2)))
hist(t(dtable[dtable["domcount"]>75 & dtable["domcount"]<100,]["dn.ds"]),main="100>domcount>75",xlab="dn.ds",breaks=30,xlim=c(0,ceiling(max(dtable["dn.ds"]))))
text(.85*par('usr')[2],.9*par('usr')[4],labels=paste("mean:",round(mean(t(dtable[dtable["domcount"]>75 & dtable["domcount"]<100,]["dn.ds"])),2)))
text(.85*par('usr')[2],.86*par('usr')[4],labels=paste("median:",round(median(t(dtable[dtable["domcount"]>75 & dtable["domcount"]<100,]["dn.ds"])),2)))
hist(t(dtable[dtable["domcount"]>100,]["dn.ds"]),main="domcount>100",xlab="dn.ds",breaks=30,xlim=c(0,ceiling(max(dtable["dn.ds"]))))
text(.85*par('usr')[2],.9*par('usr')[4],labels=paste("mean:",round(mean(t(dtable[dtable["domcount"]>100,]["dn.ds"])),2)))
text(.85*par('usr')[2],.86*par('usr')[4],labels=paste("median:",round(median(t(dtable[dtable["domcount"]>100,]["dn.ds"])),2)))
mtext("maf>0.001", side = 3, line = -2, outer = TRUE, col=2)