annotate aa_histogram.r @ 20:ba2511ebd36a draft

Uploaded
author davidvanzessen
date Mon, 28 Nov 2016 07:23:50 -0500
parents faae21ba5c63
children 0bea8c187a90
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
1 library(ggplot2)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
2
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
3 args <- commandArgs(trailingOnly = TRUE)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
4
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
5 mutations.by.id.file = args[1]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
6 absent.aa.by.id.file = args[2]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
7 genes = strsplit(args[3], ",")[[1]]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
8 genes = c(genes, "")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
9 outdir = args[4]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
10
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
11
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
12 print("---------------- read input ----------------")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
13
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
14 mutations.by.id = read.table(mutations.by.id.file, sep="\t", fill=T, header=T, quote="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
15 absent.aa.by.id = read.table(absent.aa.by.id.file, sep="\t", fill=T, header=T, quote="")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
16
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
17 for(gene in genes){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
18
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
19 if(gene == ""){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
20 mutations.by.id.gene = mutations.by.id[!grepl("unmatched", mutations.by.id$best_match),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
21 absent.aa.by.id.gene = absent.aa.by.id[!grepl("unmatched", absent.aa.by.id$best_match),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
22 } else {
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
23 mutations.by.id.gene = mutations.by.id[grepl(paste("^", gene, sep=""), mutations.by.id$best_match),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
24 absent.aa.by.id.gene = absent.aa.by.id[grepl(paste("^", gene, sep=""), absent.aa.by.id$best_match),]
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
25 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
26 print(paste("nrow", gene, nrow(absent.aa.by.id.gene)))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
27 if(nrow(mutations.by.id.gene) == 0){
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
28 next
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
29 }
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
30
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
31 mutations.at.position = colSums(mutations.by.id.gene[,-c(1,2)])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
32 aa.at.position = colSums(absent.aa.by.id.gene[,-c(1,2,3,4)])
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
33
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
34 dat_freq = mutations.at.position / aa.at.position
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
35 dat_freq[is.na(dat_freq)] = 0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
36 dat_dt = data.frame(i=1:length(dat_freq), freq=dat_freq)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
37
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
38 print("---------------- plot ----------------")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
39
1
faae21ba5c63 Uploaded
davidvanzessen
parents: 0
diff changeset
40 m = ggplot(dat_dt, aes(x=i, y=freq)) + theme(axis.text.x = element_text(angle = 90, hjust = 1), text = element_text(size=13, colour="black"))
0
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
41 m = m + geom_bar(stat="identity", colour = "black", fill = "darkgrey", alpha=0.8) + scale_x_continuous(breaks=dat_dt$i, labels=dat_dt$i)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
42 m = m + annotate("segment", x = 0.5, y = -0.05, xend=26.5, yend=-0.05, colour="darkgreen", size=1) + annotate("text", x = 13, y = -0.1, label="FR1")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
43 m = m + annotate("segment", x = 26.5, y = -0.07, xend=38.5, yend=-0.07, colour="darkblue", size=1) + annotate("text", x = 32.5, y = -0.15, label="CDR1")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
44 m = m + annotate("segment", x = 38.5, y = -0.05, xend=55.5, yend=-0.05, colour="darkgreen", size=1) + annotate("text", x = 47, y = -0.1, label="FR2")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
45 m = m + annotate("segment", x = 55.5, y = -0.07, xend=65.5, yend=-0.07, colour="darkblue", size=1) + annotate("text", x = 60.5, y = -0.15, label="CDR2")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
46 m = m + annotate("segment", x = 65.5, y = -0.05, xend=104.5, yend=-0.05, colour="darkgreen", size=1) + annotate("text", x = 85, y = -0.1, label="FR3")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
47 m = m + expand_limits(y=c(-0.1,1)) + xlab("AA position") + ylab("Frequency") + ggtitle(paste(gene, "AA mutation frequency"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
48 m = m + theme(panel.background = element_rect(fill = "white", colour="black"), panel.grid.major.y = element_line(colour = "black"), panel.grid.major.x = element_blank())
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
49 m = m + scale_colour_manual(values=c("black"))
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
50
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
51 print("---------------- write/print ----------------")
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
52
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
53 png(filename=paste(outdir, "/aa_histogram_", gene, ".png", sep=""), width=1280, height=720)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
54 print(m)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
55 dev.off()
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
56
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
57 dat.sums = data.frame(index=1:length(mutations.at.position), mutations.at.position=mutations.at.position, aa.at.position=aa.at.position)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
58
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
59 write.table(dat.sums, paste(outdir, "/aa_histogram_sum_", gene, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
60 write.table(mutations.by.id.gene, paste(outdir, "/aa_histogram_count_", gene, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
61 write.table(absent.aa.by.id.gene, paste(outdir, "/aa_histogram_absent_", gene, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
62 write.table(dat_dt, paste(outdir, "/aa_histogram_", gene, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
c33d93683a09 Uploaded
davidvanzessen
parents:
diff changeset
63 }