0
|
1 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
|
|
2
|
|
3 args <- commandArgs(trailingOnly = TRUE)
|
|
4
|
|
5 inFile = args[1]
|
|
6 outFile = args[2]
|
32
|
7 outDir = args[3]
|
0
|
8
|
32
|
9 if (!require("gridExtra")) {
|
35
|
10 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
|
32
|
11 }
|
|
12 library(gridExtra)
|
|
13 if (!require("ggplot2")) {
|
35
|
14 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
|
32
|
15 }
|
23
|
16 require(ggplot2)
|
32
|
17 if (!require("plyr")) {
|
35
|
18 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
|
32
|
19 }
|
23
|
20 require(plyr)
|
0
|
21
|
|
22 test = read.table(inFile, sep="\t", header=TRUE)
|
|
23
|
|
24 test$Top.V.Gene = gsub("[*]([0-9]+)", "", test$Top.V.Gene)
|
|
25 test$Top.D.Gene = gsub("[*]([0-9]+)", "", test$Top.D.Gene)
|
|
26 test$Top.J.Gene = gsub("[*]([0-9]+)", "", test$Top.J.Gene)
|
|
27
|
|
28 test$VDJCDR3 = do.call(paste, c(test[c("Top.V.Gene", "Top.D.Gene", "Top.J.Gene","CDR3.Seq.DNA")], sep = ":"))
|
|
29
|
|
30 PROD = test[test$VDJ.Frame != "In-frame with stop codon" & test$VDJ.Frame != "Out-of-frame" & test$CDR3.Found.How != "NOT_FOUND" , ]
|
|
31
|
|
32 NONPROD = test[test$VDJ.Frame == "In-frame with stop codon" | test$VDJ.Frame == "Out-of-frame" | test$CDR3.Found.How == "NOT_FOUND" , ]
|
|
33
|
35
|
34 PRODF = PROD[ -1]
|
0
|
35
|
32
|
36 #PRODF = unique(PRODF)
|
|
37 PRODF = PRODF[!duplicated(PRODF$VDJCDR3), ]
|
|
38
|
35
|
39
|
|
40 uniqueCount = split(PRODF, f=PRODF[,"Sample"])
|
|
41
|
|
42 for(i in 1:length(uniqueCount)) {
|
|
43 dat = data.frame(uniqueCount[i])
|
|
44 sample = paste(unique(dat[,15]))
|
|
45 uniqueCount[sample] = length(dat[,1])
|
|
46 }
|
|
47
|
|
48 PRODFV = ddply(PRODF, c("Sample", "Top.V.Gene"), function(x) summary(x$VDJCDR3))
|
0
|
49 PRODFV$Length = as.numeric(PRODFV$Length)
|
|
50 Total = 0
|
|
51 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
52 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
53 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
54
|
35
|
55 PRODFD = ddply(PRODF, c("Sample", "Top.D.Gene"), function(x) summary(x$VDJCDR3))
|
0
|
56 PRODFD$Length = as.numeric(PRODFD$Length)
|
|
57 Total = 0
|
|
58 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
59 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
60 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
61
|
35
|
62 PRODFJ = ddply(PRODF, c("Sample", "Top.J.Gene"), function(x) summary(x$VDJCDR3))
|
0
|
63 PRODFJ$Length = as.numeric(PRODFJ$Length)
|
|
64 Total = 0
|
|
65 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
|
|
66 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
|
|
67 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
|
|
68
|
|
69 V = c("v.name\tchr.orderV\nIGHV7-81\t1\nIGHV3-74\t2\nIGHV3-73\t3\nIGHV3-72\t4\nIGHV3-71\t5\nIGHV2-70\t6\nIGHV1-69\t7\nIGHV3-66\t8\nIGHV3-64\t9\nIGHV4-61\t10\nIGHV4-59\t11\nIGHV1-58\t12\nIGHV3-53\t13\nIGHV3-52\t14\nIGHV5-a\t15\nIGHV5-51\t16\nIGHV3-49\t17\nIGHV3-48\t18\nIGHV3-47\t19\nIGHV1-46\t20\nIGHV1-45\t21\nIGHV3-43\t22\nIGHV4-39\t23\nIGHV3-35\t24\nIGHV4-34\t25\nIGHV3-33\t26\nIGHV4-31\t27\nIGHV4-30-4\t28\nIGHV4-30-2\t29\nIGHV3-30-3\t30\nIGHV3-30\t31\nIGHV4-28\t32\nIGHV2-26\t33\nIGHV1-24\t34\nIGHV3-23\t35\nIGHV3-22\t36\nIGHV3-21\t37\nIGHV3-20\t38\nIGHV3-19\t39\nIGHV1-18\t40\nIGHV3-15\t41\nIGHV3-13\t42\nIGHV3-11\t43\nIGHV3-9\t44\nIGHV1-8\t45\nIGHV3-7\t46\nIGHV2-5\t47\nIGHV7-4-1\t48\nIGHV4-4\t49\nIGHV4-b\t50\nIGHV1-3\t51\nIGHV1-2\t52\nIGHV6-1\t53")
|
|
70 tcV = textConnection(V)
|
|
71 Vchain = read.table(tcV, sep="\t", header=TRUE)
|
|
72 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
|
|
73 close(tcV)
|
|
74
|
|
75 D = c("v.name\tchr.orderD\nIGHD1-1\t1\nIGHD2-2\t2\nIGHD3-3\t3\nIGHD6-6\t4\nIGHD1-7\t5\nIGHD2-8\t6\nIGHD3-9\t7\nIGHD3-10\t8\nIGHD4-11\t9\nIGHD5-12\t10\nIGHD6-13\t11\nIGHD1-14\t12\nIGHD2-15\t13\nIGHD3-16\t14\nIGHD4-17\t15\nIGHD5-18\t16\nIGHD6-19\t17\nIGHD1-20\t18\nIGHD2-21\t19\nIGHD3-22\t20\nIGHD4-23\t21\nIGHD5-24\t22\nIGHD6-25\t23\nIGHD1-26\t24\nIGHD7-27\t25")
|
|
76 tcD = textConnection(D)
|
|
77 Dchain = read.table(tcD, sep="\t", header=TRUE)
|
|
78 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
|
|
79 close(tcD)
|
|
80
|
|
81
|
|
82 J = c("v.name\tchr.orderJ\nIGHJ1\t1\nIGHJ2\t2\nIGHJ3\t3\nIGHJ4\t4\nIGHJ5\t5\nIGHJ6\t6")
|
|
83 tcJ = textConnection(J)
|
|
84 Jchain = read.table(tcJ, sep="\t", header=TRUE)
|
|
85 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
|
|
86 close(tcJ)
|
|
87
|
32
|
88 setwd(outDir)
|
|
89
|
0
|
90 pV = ggplot(PRODFV)
|
|
91 pV = pV + geom_bar( aes( x=factor(reorder(Top.V.Gene, chr.orderV)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
|
29
|
92 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
|
32
|
93
|
|
94 png("VPlot.png",width = 1280, height = 720)
|
|
95 pV
|
|
96 dev.off();
|
0
|
97
|
|
98 pD = ggplot(PRODFD)
|
|
99 pD = pD + geom_bar( aes( x=factor(reorder(Top.D.Gene, chr.orderD)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
|
29
|
100 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
|
32
|
101
|
|
102 png("DPlot.png",width = 800, height = 600)
|
|
103 pD
|
|
104 dev.off();
|
0
|
105
|
|
106 pJ = ggplot(PRODFJ)
|
|
107 pJ = pJ + geom_bar( aes( x=factor(reorder(Top.J.Gene, chr.orderJ)), y=relFreq, fill=Sample), stat='identity', position="dodge") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
|
32
|
108 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
|
|
109
|
|
110 png("JPlot.png",width = 800, height = 600)
|
|
111 pJ
|
|
112 dev.off();
|
|
113
|
0
|
114
|
32
|
115 plotVD <- function(dat){
|
|
116 img = ggplot() +
|
|
117 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=log)) +
|
|
118 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
35
|
119 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
|
120 ggtitle(paste(unique(dat$Sample), " (N=" , uniqueCount[paste(unique(dat$Sample))] ,")", sep="")) +
|
32
|
121 xlab("D genes") +
|
|
122 ylab("V Genes")
|
|
123
|
|
124 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
125 print(img)
|
|
126 dev.off()
|
|
127 }
|
|
128
|
|
129
|
35
|
130 VandDCount = ddply(PRODF, c("Top.V.Gene", "Top.D.Gene", "Sample"), function(x) summary(x$VDJCDR3))
|
32
|
131 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(test$Sample))
|
|
132
|
|
133 completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
|
|
134 completeVD$Length = as.numeric(completeVD$Length)
|
|
135 completeVD$log = log(completeVD$Length)
|
|
136 completeVD = merge(completeVD, Vchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
137 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
138 #completeVD$log[is.na(completeVD$log)] = 0
|
|
139 l = split(completeVD, f=completeVD[,"Sample"])
|
|
140
|
|
141 lapply(l, FUN=plotVD)
|
|
142
|
0
|
143
|
29
|
144
|
32
|
145 plotVJ <- function(dat){
|
|
146 img = ggplot() +
|
|
147 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=log)) +
|
|
148 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
149 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
35
|
150 ggtitle(paste(unique(dat$Sample), " (N=" , uniqueCount[paste(unique(dat$Sample))] ,")", sep="")) +
|
32
|
151 xlab("J genes") +
|
|
152 ylab("V Genes")
|
|
153
|
|
154 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
|
|
155 print(img)
|
|
156 dev.off()
|
|
157 }
|
|
158
|
35
|
159 VandJCount = ddply(PRODF, c("Top.V.Gene", "Top.J.Gene", "Sample"), function(x) summary(x$VDJCDR3))
|
32
|
160 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
161
|
|
162 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
|
|
163 completeVJ$Length = as.numeric(completeVJ$Length)
|
|
164 completeVJ$log = log(completeVJ$Length)
|
|
165 completeVJ = merge(completeVJ, Vchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
|
|
166 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
167 #completeVJ$log[is.na(completeVJ$log)] = 0
|
|
168 l = split(completeVJ, f=completeVJ[,"Sample"])
|
|
169 lapply(l, FUN=plotVJ)
|
|
170
|
|
171 plotDJ <- function(dat){
|
|
172 img = ggplot() +
|
|
173 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=log)) +
|
|
174 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
|
|
175 scale_fill_gradient(low="gold", high="blue", na.value="white") +
|
35
|
176 ggtitle(paste(unique(dat$Sample), " (N=" , uniqueCount[paste(unique(dat$Sample))] ,")", sep="")) +
|
32
|
177 xlab("J genes") +
|
|
178 ylab("D Genes")
|
|
179
|
|
180 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
|
|
181 print(img)
|
|
182 dev.off()
|
|
183 }
|
|
184
|
35
|
185 DandJCount = ddply(PRODF, c("Top.D.Gene", "Top.J.Gene", "Sample"), function(x) summary(x$VDJCDR3))
|
32
|
186 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(test$Sample))
|
|
187
|
|
188 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
|
|
189 completeDJ$Length = as.numeric(completeDJ$Length)
|
|
190 completeDJ$log = log(completeDJ$Length)
|
|
191 completeDJ = merge(completeDJ, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
|
|
192 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
|
|
193 #completeDJ$log[is.na(completeDJ$log)] = 0
|
|
194 l = split(completeDJ, f=completeDJ[,"Sample"])
|
|
195 lapply(l, FUN=plotDJ)
|
0
|
196
|
|
197
|
32
|
198 sampleFile <- file("samples.txt")
|
|
199 un = unique(test$Sample)
|
|
200 un = paste(un, sep="\n")
|
|
201 writeLines(un, sampleFile)
|
|
202 close(sampleFile)
|