0
|
1 args <- commandArgs(trailingOnly = TRUE)
|
|
2
|
|
3 inFile = args[1]
|
|
4 outDir = args[2]
|
3
|
5 logfile = args[3]
|
|
6 min_freq = as.numeric(args[4])
|
|
7 min_cells = as.numeric(args[5])
|
|
8
|
|
9 cat("<html><table><tr><td>Starting analysis</td></tr>", file=logfile, append=F)
|
0
|
10
|
4
|
11 library(ggplot2)
|
|
12 library(reshape2)
|
|
13 library(data.table)
|
|
14 library(grid)
|
|
15 library(parallel)
|
0
|
16 #require(xtable)
|
3
|
17 cat("<tr><td>Reading input</td></tr>", file=logfile, append=T)
|
13
|
18 dat = read.table(inFile, header=T, sep="\t", dec=".", fill=T, stringsAsFactors=F)
|
9
|
19 dat = dat[!is.na(dat$Patient),]
|
13
|
20 dat$Related_to_leukemia_clone = F
|
9
|
21
|
0
|
22 setwd(outDir)
|
3
|
23 cat("<tr><td>Selecting first V/J Genes</td></tr>", file=logfile, append=T)
|
2
|
24 dat$V_Segment_Major_Gene = as.factor(as.character(lapply(strsplit(as.character(dat$V_Segment_Major_Gene), "; "), "[[", 1)))
|
|
25 dat$J_Segment_Major_Gene = as.factor(as.character(lapply(strsplit(as.character(dat$J_Segment_Major_Gene), "; "), "[[", 1)))
|
|
26
|
3
|
27 cat("<tr><td>Calculating Frequency</td></tr>", file=logfile, append=T)
|
12
|
28
|
13
|
29 dat$Frequency = ((10^dat$Log10_Frequency)*100)
|
2
|
30
|
3
|
31 dat = dat[dat$Frequency >= min_freq,]
|
|
32
|
13
|
33 triplets = dat[grepl("VanDongen_cALL_14696", dat$Patient) | grepl("(16278)|(26402)|(26759)", dat$Sample),]
|
|
34
|
|
35 cat("<tr><td>Normalizing to lowest cell count within locus</td></tr>", file=logfile, append=T)
|
|
36
|
|
37 dat$locus_V = substring(dat$V_Segment_Major_Gene, 0, 4)
|
|
38 dat$locus_J = substring(dat$J_Segment_Major_Gene, 0, 4)
|
|
39 min_cell_count = data.frame(data.table(dat)[, list(min_cell_count=min(.SD$Cell_Count)), by=c("Patient", "locus_V", "locus_J")])
|
|
40
|
|
41 dat$min_cell_paste = paste(dat$Patient, dat$locus_V, dat$locus_J)
|
|
42 min_cell_count$min_cell_paste = paste(min_cell_count$Patient, min_cell_count$locus_V, min_cell_count$locus_J)
|
|
43
|
|
44 min_cell_count = min_cell_count[,c("min_cell_paste", "min_cell_count")]
|
|
45
|
|
46 dat = merge(dat, min_cell_count, by="min_cell_paste")
|
|
47
|
|
48 dat$normalized_read_count = round(dat$Clone_Molecule_Count_From_Spikes / dat$Cell_Count * dat$min_cell_count / 2, digits=2) #??????????????????????????????????? wel of geen / 2
|
|
49
|
3
|
50 dat = dat[dat$normalized_read_count >= min_cells,]
|
13
|
51
|
|
52 dat$paste = paste(dat$Sample, dat$Clone_Sequence)
|
9
|
53
|
0
|
54 patients = split(dat, dat$Patient, drop=T)
|
9
|
55 intervalReads = rev(c(0,10,25,50,100,250,500,750,1000,10000))
|
6
|
56 intervalFreq = rev(c(0,0.01,0.05,0.1,0.5,1,5))
|
0
|
57 V_Segments = c(".*", "IGHV", "IGHD", "IGKV", "IGKV", "IgKINTR", "TRGV", "TRDV", "TRDD" , "TRBV")
|
|
58 J_Segments = c(".*", ".*", ".*", "IGKJ", "KDE", ".*", ".*", ".*", ".*", ".*")
|
|
59 Titles = c("Total", "IGH-Vh-Jh", "IGH-Dh-Jh", "Vk-Jk", "Vk-Kde" , "Intron-Kde", "TCRG", "TCRD-Vd-Dd", "TCRD-Dd-Dd", "TCRB-Vb-Jb")
|
|
60 Titles = factor(Titles, levels=Titles)
|
|
61 TitlesOrder = data.frame("Title"=Titles, "TitlesOrder"=1:length(Titles))
|
|
62
|
|
63 patientCountOnColumn <- function(x, product, interval, on, appendtxt=F){
|
|
64 x$Sample = factor(x$Sample, levels=unique(x$Sample))
|
|
65 onShort = "reads"
|
|
66 if(on == "Frequency"){
|
|
67 onShort = "freq"
|
|
68 }
|
|
69 splt = split(x, x$Sample, drop=T)
|
4
|
70 type="pair"
|
2
|
71 if(length(splt) == 1){
|
3
|
72 print(paste(paste(x[1,which(colnames(x) == "Patient")]), "has one sample"))
|
4
|
73 splt[[2]] = data.frame("Patient" = character(0), "Receptor" = character(0), "Sample" = character(0), "Cell_Count" = numeric(0), "Clone_Molecule_Count_From_Spikes" = numeric(0), "Log10_Frequency" = numeric(0), "Total_Read_Count" = numeric(0), "dsMol_per_1e6_cells" = numeric(0), "J_Segment_Major_Gene" = character(0), "V_Segment_Major_Gene" = character(0), "Clone_Sequence" = character(0), "CDR3_Sense_Sequence" = character(0), "Related_to_leukemia_clone" = logical(0), "Frequency"= numeric(0), "normalized_read_count" = numeric(0), "paste" = character(0))
|
|
74 type="single"
|
2
|
75 }
|
0
|
76 patient1 = splt[[1]]
|
|
77 patient2 = splt[[2]]
|
|
78
|
|
79 threshholdIndex = which(colnames(product) == "interval")
|
|
80 V_SegmentIndex = which(colnames(product) == "V_Segments")
|
|
81 J_SegmentIndex = which(colnames(product) == "J_Segments")
|
|
82 titleIndex = which(colnames(product) == "Titles")
|
|
83 sampleIndex = which(colnames(x) == "Sample")
|
|
84 patientIndex = which(colnames(x) == "Patient")
|
|
85 oneSample = paste(patient1[1,sampleIndex], sep="")
|
|
86 twoSample = paste(patient2[1,sampleIndex], sep="")
|
|
87 patient = paste(x[1,patientIndex])
|
3
|
88
|
0
|
89 switched = F
|
|
90 if(length(grep(".*_Right$", twoSample)) == 1 || length(grep(".*_Dx_BM$", twoSample)) == 1 || length(grep(".*_Dx$", twoSample)) == 1 ){
|
|
91 tmp = twoSample
|
|
92 twoSample = oneSample
|
|
93 oneSample = tmp
|
|
94 tmp = patient1
|
|
95 patient1 = patient2
|
|
96 patient2 = tmp
|
|
97 switched = T
|
|
98 }
|
|
99 if(appendtxt){
|
4
|
100 cat(paste(patient, oneSample, twoSample, type, sep="\t"), file="patients.txt", append=T, sep="", fill=3)
|
0
|
101 }
|
3
|
102 cat(paste("<tr><td>", patient, "</td></tr>", sep=""), file=logfile, append=T)
|
9
|
103
|
12
|
104 #patient1$merge = paste(patient1$V_Segment_Major_Gene, patient1$J_Segment_Major_Gene, patient1$CDR3_Sense_Sequence)
|
|
105 #patient2$merge = paste(patient2$V_Segment_Major_Gene, patient2$J_Segment_Major_Gene, patient2$CDR3_Sense_Sequence)
|
13
|
106 patient1$merge = paste(patient1$Clone_Sequence)
|
|
107 patient2$merge = paste(patient2$Clone_Sequence)
|
9
|
108
|
12
|
109 #patientMerge = merge(patient1, patient2, by.x="merge", by.y="merge")
|
9
|
110 patientMerge = merge(patient1, patient2, by.x="merge", by.y="merge")
|
0
|
111 res1 = vector()
|
|
112 res2 = vector()
|
|
113 resBoth = vector()
|
|
114 read1Count = vector()
|
|
115 read2Count = vector()
|
2
|
116 locussum1 = vector()
|
|
117 locussum2 = vector()
|
9
|
118
|
|
119 print(patient)
|
0
|
120 #for(iter in 1){
|
|
121 for(iter in 1:length(product[,1])){
|
|
122 threshhold = product[iter,threshholdIndex]
|
|
123 V_Segment = paste(".*", as.character(product[iter,V_SegmentIndex]), ".*", sep="")
|
|
124 J_Segment = paste(".*", as.character(product[iter,J_SegmentIndex]), ".*", sep="")
|
|
125 both = (grepl(V_Segment, patientMerge$V_Segment_Major_Gene.x) & grepl(J_Segment, patientMerge$J_Segment_Major_Gene.x) & patientMerge[,paste(on, ".x", sep="")] > threshhold & patientMerge[,paste(on, ".y", sep="")] > threshhold)
|
10
|
126 one = (grepl(V_Segment, patient1$V_Segment_Major_Gene) & grepl(J_Segment, patient1$J_Segment_Major_Gene) & patient1[,on] > threshhold & !(patient1$CDR3_Sense_Sequence %in% patientMerge[both,]$CDR3_Sense_Sequence.x))
|
|
127 two = (grepl(V_Segment, patient2$V_Segment_Major_Gene) & grepl(J_Segment, patient2$J_Segment_Major_Gene) & patient2[,on] > threshhold & !(patient2$CDR3_Sense_Sequence %in% patientMerge[both,]$CDR3_Sense_Sequence.x))
|
14
|
128 read1Count = append(read1Count, sum(patient1[one,]$normalized_read_count))
|
|
129 read2Count = append(read2Count, sum(patient2[two,]$normalized_read_count))
|
0
|
130 res1 = append(res1, sum(one))
|
2
|
131 res2 = append(res2, sum(two))
|
0
|
132 resBoth = append(resBoth, sum(both))
|
2
|
133 locussum1 = append(locussum1, sum(patient1[(grepl(V_Segment, patient1$V_Segment_Major_Gene) & grepl(J_Segment, patient1$J_Segment_Major_Gene)),]$normalized_read_count))
|
|
134 locussum2 = append(locussum2, sum(patient2[(grepl(V_Segment, patient2$V_Segment_Major_Gene) & grepl(J_Segment, patient2$J_Segment_Major_Gene)),]$normalized_read_count))
|
0
|
135 #threshhold = 0
|
|
136 if(threshhold != 0){
|
|
137 if(sum(one) > 0){
|
9
|
138 dfOne = patient1[one,c("V_Segment_Major_Gene", "J_Segment_Major_Gene", "normalized_read_count", "Frequency", "CDR3_Sense_Sequence", "Related_to_leukemia_clone")]
|
|
139 colnames(dfOne) = c("Proximal segment", "Distal segment", "normalized_read_count", "Frequency", "CDR3 Sequence", "Related_to_leukemia_clone")
|
0
|
140 filenameOne = paste(oneSample, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
141 write.table(dfOne, file=paste(filenameOne, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
142 }
|
|
143 if(sum(two) > 0){
|
9
|
144 dfTwo = patient2[two,c("V_Segment_Major_Gene", "J_Segment_Major_Gene", "normalized_read_count", "Frequency", "CDR3_Sense_Sequence", "Related_to_leukemia_clone")]
|
|
145 colnames(dfTwo) = c("Proximal segment", "Distal segment", "normalized_read_count", "Frequency", "CDR3 Sequence", "Related_to_leukemia_clone")
|
0
|
146 filenameTwo = paste(twoSample, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
147 write.table(dfTwo, file=paste(filenameTwo, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
148 }
|
|
149 }
|
|
150 if(sum(both) > 0){
|
9
|
151 dfBoth = patientMerge[both,c("V_Segment_Major_Gene.x", "J_Segment_Major_Gene.x", "normalized_read_count.x", "Frequency.x", "Related_to_leukemia_clone.x", "CDR3_Sense_Sequence.x", "V_Segment_Major_Gene.y", "J_Segment_Major_Gene.y", "normalized_read_count.y", "Frequency.y", "Related_to_leukemia_clone.y")]
|
|
152 colnames(dfBoth) = c(paste("Proximal segment", oneSample), paste("Distal segment", oneSample), paste("Normalized_Read_Count", oneSample), paste("Frequency", oneSample), paste("Related_to_leukemia_clone", oneSample),"CDR3 Sequence", paste("Proximal segment", twoSample), paste("Distal segment", twoSample), paste("Normalized_Read_Count", twoSample), paste("Frequency", twoSample), paste("Related_to_leukemia_clone", twoSample))
|
0
|
153 filenameBoth = paste(oneSample, "_", twoSample, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
154 write.table(dfBoth, file=paste(filenameBoth, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
155 }
|
|
156 }
|
2
|
157 patientResult = data.frame("Locus"=product$Titles, "J_Segment"=product$J_Segments, "V_Segment"=product$V_Segments, "cut_off_value"=paste(">", product$interval, sep=""), "Both"=resBoth, "tmp1"=res1, "read_count1" = round(read1Count), "tmp2"=res2, "read_count2"= round(read2Count), "Sum"=res1 + res2 + resBoth, "percentage" = round((resBoth/(res1 + res2 + resBoth)) * 100, digits=2), "Locus_sum1"=locussum1, "Locus_sum2"=locussum2)
|
0
|
158 if(sum(is.na(patientResult$percentage)) > 0){
|
|
159 patientResult[is.na(patientResult$percentage),]$percentage = 0
|
|
160 }
|
|
161 colnames(patientResult)[6] = oneSample
|
|
162 colnames(patientResult)[8] = twoSample
|
|
163 colnamesBak = colnames(patientResult)
|
2
|
164 colnames(patientResult) = c("Ig/TCR gene rearrangement type", "Distal Gene segment", "Proximal gene segment", "cut_off_value", paste("Number of sequences ", patient, "_Both", sep=""), paste("Number of sequences", oneSample, sep=""), paste("Normalized Read Count", oneSample), paste("Number of sequences", twoSample, sep=""), paste("Normalized Read Count", twoSample), paste("Sum number of sequences", patient), paste("Percentage of sequences ", patient, "_Both", sep=""), paste("Locus Sum", oneSample), paste("Locus Sum", twoSample))
|
0
|
165 write.table(patientResult, file=paste(patient, "_", onShort, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
166 colnames(patientResult) = colnamesBak
|
|
167
|
|
168 patientResult$Locus = factor(patientResult$Locus, Titles)
|
|
169 patientResult$cut_off_value = factor(patientResult$cut_off_value, paste(">", interval, sep=""))
|
|
170
|
|
171 plt = ggplot(patientResult[,c("Locus", "cut_off_value", "Both")])
|
|
172 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=Both), stat='identity', position="dodge", fill="#79c36a")
|
|
173 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
174 plt = plt + geom_text(aes(ymax=max(Both), x=cut_off_value,y=Both,label=Both), angle=90, hjust=0)
|
|
175 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle("Number of clones in both")
|
|
176 plt = plt + theme(plot.margin = unit(c(1,8.8,0.5,1.5), "lines"))
|
|
177 png(paste(patient, "_", onShort, ".png", sep=""), width=1920, height=1080)
|
|
178 print(plt)
|
|
179 dev.off()
|
|
180 #(t,r,b,l)
|
|
181 plt = ggplot(patientResult[,c("Locus", "cut_off_value", "percentage")])
|
|
182 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=percentage), stat='identity', position="dodge", fill="#79c36a")
|
|
183 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
184 plt = plt + geom_text(aes(ymax=max(percentage), x=cut_off_value,y=percentage,label=percentage), angle=90, hjust=0)
|
|
185 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle("% clones in both left and right")
|
|
186 plt = plt + theme(plot.margin = unit(c(1,8.8,0.5,1.5), "lines"))
|
|
187 png(paste(patient, "_percent_", onShort, ".png", sep=""), width=1920, height=1080)
|
|
188 print(plt)
|
|
189 dev.off()
|
|
190
|
|
191 patientResult = melt(patientResult[,c('Locus','cut_off_value', oneSample, twoSample)] ,id.vars=1:2)
|
|
192 patientResult$relativeValue = patientResult$value * 10
|
|
193 patientResult[patientResult$relativeValue == 0,]$relativeValue = 1
|
|
194 plt = ggplot(patientResult)
|
|
195 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=relativeValue, fill=variable), stat='identity', position="dodge")
|
|
196 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
197 plt = plt + scale_y_continuous(trans="log", breaks=10^c(0:10), labels=c(0, 10^c(0:9)))
|
|
198 plt = plt + geom_text(data=patientResult[patientResult$variable == oneSample,], aes(ymax=max(value), x=cut_off_value,y=relativeValue,label=value), angle=90, position=position_dodge(width=0.9), hjust=0, vjust=-0.2)
|
|
199 plt = plt + geom_text(data=patientResult[patientResult$variable == twoSample,], aes(ymax=max(value), x=cut_off_value,y=relativeValue,label=value), angle=90, position=position_dodge(width=0.9), hjust=0, vjust=0.8)
|
|
200 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle(paste("Number of clones in only ", oneSample, " and only ", twoSample, sep=""))
|
|
201 png(paste(patient, "_", onShort, "_both.png", sep=""), width=1920, height=1080)
|
|
202 print(plt)
|
|
203 dev.off()
|
|
204 }
|
|
205
|
3
|
206 cat("<tr><td>Starting Frequency analysis</td></tr>", file=logfile, append=T)
|
|
207
|
0
|
208 interval = intervalFreq
|
|
209 intervalOrder = data.frame("interval"=paste(">", interval, sep=""), "intervalOrder"=1:length(interval))
|
4
|
210 product = data.frame("Titles"=rep(Titles, each=length(interval)), "interval"=rep(interval, times=10), "V_Segments"=rep(V_Segments, each=length(interval)), "J_Segments"=rep(J_Segments, each=length(interval)))
|
|
211 mclapply(patients, FUN=patientCountOnColumn, product = product, interval=interval, on="Frequency", appendtxt=T)
|
0
|
212
|
3
|
213 cat("<tr><td>Starting Cell Count analysis</td></tr>", file=logfile, append=T)
|
|
214
|
0
|
215 interval = intervalReads
|
|
216 intervalOrder = data.frame("interval"=paste(">", interval, sep=""), "intervalOrder"=1:length(interval))
|
4
|
217 product = data.frame("Titles"=rep(Titles, each=length(interval)), "interval"=rep(interval, times=10), "V_Segments"=rep(V_Segments, each=length(interval)), "J_Segments"=rep(J_Segments, each=length(interval)))
|
9
|
218 mclapply(patients, FUN=patientCountOnColumn, product = product, interval=interval, on="normalized_read_count")
|
0
|
219
|
3
|
220 cat("</table></html>", file=logfile, append=T)
|
|
221
|
7
|
222
|
13
|
223
|
7
|
224 tripletAnalysis <- function(patient1, label1, patient2, label2, patient3, label3, product, interval, on, appendTriplets= FALSE){
|
|
225 onShort = "reads"
|
|
226 if(on == "Frequency"){
|
|
227 onShort = "freq"
|
|
228 }
|
|
229 type="triplet"
|
|
230
|
|
231 threshholdIndex = which(colnames(product) == "interval")
|
|
232 V_SegmentIndex = which(colnames(product) == "V_Segments")
|
|
233 J_SegmentIndex = which(colnames(product) == "J_Segments")
|
|
234 titleIndex = which(colnames(product) == "Titles")
|
|
235 sampleIndex = which(colnames(patient1) == "Sample")
|
|
236 patientIndex = which(colnames(patient1) == "Patient")
|
|
237 oneSample = paste(patient1[1,sampleIndex], sep="")
|
|
238 twoSample = paste(patient2[1,sampleIndex], sep="")
|
|
239 threeSample = paste(patient3[1,sampleIndex], sep="")
|
|
240
|
12
|
241 #patient1$merge = paste(patient1$V_Segment_Major_Gene, patient1$J_Segment_Major_Gene, patient1$CDR3_Sense_Sequence)
|
|
242 #patient2$merge = paste(patient2$V_Segment_Major_Gene, patient2$J_Segment_Major_Gene, patient2$CDR3_Sense_Sequence)
|
|
243 #patient3$merge = paste(patient3$V_Segment_Major_Gene, patient3$J_Segment_Major_Gene, patient3$CDR3_Sense_Sequence)
|
|
244
|
|
245 patient1$merge = paste(patient1$CDR3_Sense_Sequence)
|
|
246 patient2$merge = paste(patient2$CDR3_Sense_Sequence)
|
|
247 patient3$merge = paste(patient3$CDR3_Sense_Sequence)
|
9
|
248
|
|
249 patientMerge = merge(patient1, patient2, by="merge")
|
|
250 patientMerge = merge(patientMerge, patient3, by="merge")
|
13
|
251 colnames(patientMerge)[30:length(colnames(patientMerge))] = paste(colnames(patientMerge)[30:length(colnames(patientMerge))], ".z", sep="")
|
7
|
252 res1 = vector()
|
|
253 res2 = vector()
|
|
254 res3 = vector()
|
|
255 resAll = vector()
|
|
256 read1Count = vector()
|
|
257 read2Count = vector()
|
|
258 read3Count = vector()
|
|
259
|
|
260 if(appendTriplets){
|
9
|
261 cat(paste(label1, label2, label3, sep="\t"), file="triplets.txt", append=T, sep="", fill=3)
|
7
|
262 }
|
|
263 for(iter in 1:length(product[,1])){
|
|
264 threshhold = product[iter,threshholdIndex]
|
|
265 V_Segment = paste(".*", as.character(product[iter,V_SegmentIndex]), ".*", sep="")
|
|
266 J_Segment = paste(".*", as.character(product[iter,J_SegmentIndex]), ".*", sep="")
|
|
267 all = (grepl(V_Segment, patientMerge$V_Segment_Major_Gene.x) & grepl(J_Segment, patientMerge$J_Segment_Major_Gene.x) & patientMerge[,paste(on, ".x", sep="")] > threshhold & patientMerge[,paste(on, ".y", sep="")] > threshhold & patientMerge[,paste(on, ".z", sep="")] > threshhold)
|
10
|
268 one = (grepl(V_Segment, patient1$V_Segment_Major_Gene) & grepl(J_Segment, patient1$J_Segment_Major_Gene) & patient1[,on] > threshhold & !(patient1$CDR3_Sense_Sequence %in% patientMerge[all,]$CDR3_Sense_Sequence.x))
|
|
269 two = (grepl(V_Segment, patient2$V_Segment_Major_Gene) & grepl(J_Segment, patient2$J_Segment_Major_Gene) & patient2[,on] > threshhold & !(patient2$CDR3_Sense_Sequence %in% patientMerge[all,]$CDR3_Sense_Sequence.x))
|
|
270 three = (grepl(V_Segment, patient3$V_Segment_Major_Gene) & grepl(J_Segment, patient3$J_Segment_Major_Gene) & patient3[,on] > threshhold & !(patient3$CDR3_Sense_Sequence %in% patientMerge[all,]$CDR3_Sense_Sequence.x))
|
7
|
271
|
14
|
272 read1Count = append(read1Count, sum(patient1[one,]$normalized_read_count))
|
|
273 read2Count = append(read2Count, sum(patient2[two,]$normalized_read_count))
|
|
274 read3Count = append(read3Count, sum(patient3[three,]$normalized_read_count))
|
7
|
275 res1 = append(res1, sum(one))
|
|
276 res2 = append(res2, sum(two))
|
|
277 res3 = append(res3, sum(three))
|
|
278 resAll = append(resAll, sum(all))
|
|
279 #threshhold = 0
|
|
280 if(threshhold != 0){
|
|
281 if(sum(one) > 0){
|
9
|
282 dfOne = patient1[one,c("V_Segment_Major_Gene", "J_Segment_Major_Gene", "normalized_read_count", "Frequency", "CDR3_Sense_Sequence", "Related_to_leukemia_clone")]
|
13
|
283 colnames(dfOne) = c("Proximal segment", "Distal segment", "normalized_read_count", "Frequency", "Sequence", "Related_to_leukemia_clone")
|
7
|
284 filenameOne = paste(label1, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
285 write.table(dfOne, file=paste(filenameOne, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
286 }
|
|
287 if(sum(two) > 0){
|
9
|
288 dfTwo = patient2[two,c("V_Segment_Major_Gene", "J_Segment_Major_Gene", "normalized_read_count", "Frequency", "CDR3_Sense_Sequence", "Related_to_leukemia_clone")]
|
13
|
289 colnames(dfTwo) = c("Proximal segment", "Distal segment", "normalized_read_count", "Frequency", "Sequence", "Related_to_leukemia_clone")
|
7
|
290 filenameTwo = paste(label2, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
291 write.table(dfTwo, file=paste(filenameTwo, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
292 }
|
|
293 if(sum(three) > 0){
|
9
|
294 dfThree = patient3[three,c("V_Segment_Major_Gene", "J_Segment_Major_Gene", "normalized_read_count", "Frequency", "CDR3_Sense_Sequence", "Related_to_leukemia_clone")]
|
13
|
295 colnames(dfThree) = c("Proximal segment", "Distal segment", "normalized_read_count", "Frequency", "Sequence", "Related_to_leukemia_clone")
|
7
|
296 filenameThree = paste(label3, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
297 write.table(dfThree, file=paste(filenameThree, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
298 }
|
|
299 }
|
|
300 if(sum(all) > 0){
|
9
|
301 dfAll = patientMerge[all,c("V_Segment_Major_Gene.x", "J_Segment_Major_Gene.x", "normalized_read_count.x", "Frequency.x", "Related_to_leukemia_clone.x", "CDR3_Sense_Sequence.x", "V_Segment_Major_Gene.y", "J_Segment_Major_Gene.y", "normalized_read_count.y", "Frequency.y", "Related_to_leukemia_clone.y", "V_Segment_Major_Gene.z", "J_Segment_Major_Gene.z", "normalized_read_count.z", "Frequency.z", "Related_to_leukemia_clone.z")]
|
|
302 colnames(dfAll) = c(paste("Proximal segment", oneSample), paste("Distal segment", oneSample), paste("Normalized_Read_Count", oneSample), paste("Frequency", oneSample), paste("Related_to_leukemia_clone", oneSample),"CDR3_Sense_Sequence", paste("Proximal segment", twoSample), paste("Distal segment", twoSample), paste("Normalized_Read_Count", twoSample), paste("Frequency", twoSample), paste("Related_to_leukemia_clone", twoSample), paste("Proximal segment", threeSample), paste("Distal segment", threeSample), paste("Normalized_Read_Count", threeSample), paste("Frequency", threeSample), paste("Related_to_leukemia_clone", threeSample))
|
7
|
303 filenameAll = paste(label1, "_", label2, "_", label3, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
304 write.table(dfAll, file=paste(filenameAll, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
305 }
|
|
306 }
|
|
307 patientResult = data.frame("Locus"=product$Titles, "J_Segment"=product$J_Segments, "V_Segment"=product$V_Segments, "cut_off_value"=paste(">", product$interval, sep=""), "All"=resAll, "tmp1"=res1, "read_count1" = round(read1Count), "tmp2"=res2, "read_count2"= round(read2Count), "tmp3"=res3, "read_count3"=round(read3Count))
|
|
308 colnames(patientResult)[6] = oneSample
|
|
309 colnames(patientResult)[8] = twoSample
|
|
310 colnames(patientResult)[10] = threeSample
|
|
311
|
|
312 colnamesBak = colnames(patientResult)
|
|
313 colnames(patientResult) = c("Ig/TCR gene rearrangement type", "Distal Gene segment", "Proximal gene segment", "cut_off_value", "Number of sequences All", paste("Number of sequences", oneSample), paste("Normalized Read Count", oneSample), paste("Number of sequences", twoSample), paste("Normalized Read Count", twoSample), paste("Number of sequences", threeSample), paste("Normalized Read Count", threeSample))
|
|
314 write.table(patientResult, file=paste(label1, "_", label2, "_", label3, "_", onShort, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
315 colnames(patientResult) = colnamesBak
|
|
316
|
|
317 patientResult$Locus = factor(patientResult$Locus, Titles)
|
|
318 patientResult$cut_off_value = factor(patientResult$cut_off_value, paste(">", interval, sep=""))
|
|
319
|
|
320 plt = ggplot(patientResult[,c("Locus", "cut_off_value", "All")])
|
|
321 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=All), stat='identity', position="dodge", fill="#79c36a")
|
|
322 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
323 plt = plt + geom_text(aes(ymax=max(All), x=cut_off_value,y=All,label=All), angle=90, hjust=0)
|
|
324 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle("Number of clones in All")
|
|
325 plt = plt + theme(plot.margin = unit(c(1,8.8,0.5,1.5), "lines"))
|
|
326 png(paste(label1, "_", label2, "_", label3, "_", onShort, "_total_all.png", sep=""), width=1920, height=1080)
|
|
327 print(plt)
|
|
328 dev.off()
|
|
329
|
|
330 fontSize = 4
|
|
331
|
|
332 bak = patientResult
|
|
333 patientResult = melt(patientResult[,c('Locus','cut_off_value', oneSample, twoSample, threeSample)] ,id.vars=1:2)
|
|
334 patientResult$relativeValue = patientResult$value * 10
|
|
335 patientResult[patientResult$relativeValue == 0,]$relativeValue = 1
|
|
336 plt = ggplot(patientResult)
|
|
337 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=relativeValue, fill=variable), stat='identity', position="dodge")
|
|
338 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
339 plt = plt + scale_y_continuous(trans="log", breaks=10^c(0:10), labels=c(0, 10^c(0:9)))
|
|
340 plt = plt + geom_text(data=patientResult[patientResult$variable == oneSample,], aes(ymax=max(value), x=cut_off_value,y=relativeValue,label=value), angle=90, position=position_dodge(width=0.9), hjust=0, vjust=-0.7, size=fontSize)
|
|
341 plt = plt + geom_text(data=patientResult[patientResult$variable == twoSample,], aes(ymax=max(value), x=cut_off_value,y=relativeValue,label=value), angle=90, position=position_dodge(width=0.9), hjust=0, vjust=0.4, size=fontSize)
|
|
342 plt = plt + geom_text(data=patientResult[patientResult$variable == threeSample,], aes(ymax=max(value), x=cut_off_value,y=relativeValue,label=value), angle=90, position=position_dodge(width=0.9), hjust=0, vjust=1.5, size=fontSize)
|
|
343 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle("Number of clones in only one sample")
|
|
344 png(paste(label1, "_", label2, "_", label3, "_", onShort, "_indiv_all.png", sep=""), width=1920, height=1080)
|
|
345 print(plt)
|
|
346 dev.off()
|
|
347 }
|
|
348
|
9
|
349 triplets$uniqueID = "ID"
|
|
350
|
|
351 triplets[grepl("16278_Left", triplets$Sample),]$uniqueID = "16278_26402_26759_Left"
|
|
352 triplets[grepl("26402_Left", triplets$Sample),]$uniqueID = "16278_26402_26759_Left"
|
|
353 triplets[grepl("26759_Left", triplets$Sample),]$uniqueID = "16278_26402_26759_Left"
|
|
354
|
|
355 triplets[grepl("16278_Right", triplets$Sample),]$uniqueID = "16278_26402_26759_Right"
|
|
356 triplets[grepl("26402_Right", triplets$Sample),]$uniqueID = "16278_26402_26759_Right"
|
|
357 triplets[grepl("26759_Right", triplets$Sample),]$uniqueID = "16278_26402_26759_Right"
|
|
358
|
|
359 triplets[grepl("14696", triplets$Patient),]$uniqueID = "14696"
|
|
360
|
13
|
361 triplets$locus_V = substring(triplets$V_Segment_Major_Gene, 0, 4)
|
|
362 triplets$locus_J = substring(triplets$J_Segment_Major_Gene, 0, 4)
|
|
363 min_cell_count = data.frame(data.table(triplets)[, list(min_cell_count=min(.SD$Cell_Count)), by=c("uniqueID", "locus_V", "locus_J")])
|
|
364
|
|
365 triplets$min_cell_paste = paste(triplets$uniqueID, triplets$locus_V, triplets$locus_J)
|
|
366 min_cell_count$min_cell_paste = paste(min_cell_count$uniqueID, min_cell_count$locus_V, min_cell_count$locus_J)
|
|
367
|
|
368 min_cell_count = min_cell_count[,c("min_cell_paste", "min_cell_count")]
|
9
|
369
|
13
|
370 triplets = merge(triplets, min_cell_count, by="min_cell_paste")
|
|
371
|
|
372 triplets$normalized_read_count = round(triplets$Clone_Molecule_Count_From_Spikes / triplets$Cell_Count * triplets$min_cell_count / 2, digits=2) #??????????????????????????????????? wel of geen / 2
|
|
373
|
|
374 triplets = triplets[triplets$normalized_read_count >= min_cells,]
|
|
375
|
|
376 column_drops = c("locus_V", "locus_J", "min_cell_count", "min_cell_paste")
|
|
377
|
|
378 triplets = triplets[,!(colnames(triplets) %in% column_drops)]
|
9
|
379
|
7
|
380 interval = intervalReads
|
|
381 intervalOrder = data.frame("interval"=paste(">", interval, sep=""), "intervalOrder"=1:length(interval))
|
|
382 product = data.frame("Titles"=rep(Titles, each=length(interval)), "interval"=rep(interval, times=10), "V_Segments"=rep(V_Segments, each=length(interval)), "J_Segments"=rep(J_Segments, each=length(interval)))
|
|
383
|
9
|
384 one = triplets[triplets$Sample == "14696_reg_BM",]
|
|
385 two = triplets[triplets$Sample == "24536_reg_BM",]
|
|
386 three = triplets[triplets$Sample == "24062_reg_BM",]
|
8
|
387 tripletAnalysis(one, "14696_1", two, "14696_2", three, "14696_3", product=product, interval=interval, on="normalized_read_count", T)
|
7
|
388
|
9
|
389 one = triplets[triplets$Sample == "16278_Left",]
|
|
390 two = triplets[triplets$Sample == "26402_Left",]
|
|
391 three = triplets[triplets$Sample == "26759_Left",]
|
8
|
392 tripletAnalysis(one, "16278_Left", two, "26402_Left", three, "26759_Left", product=product, interval=interval, on="normalized_read_count", T)
|
7
|
393
|
9
|
394 one = triplets[triplets$Sample == "16278_Right",]
|
|
395 two = triplets[triplets$Sample == "26402_Right",]
|
|
396 three = triplets[triplets$Sample == "26759_Right",]
|
8
|
397 tripletAnalysis(one, "16278_Right", two, "26402_Right", three, "26759_Right", product=product, interval=interval, on="normalized_read_count", T)
|
7
|
398
|
|
399
|
|
400 interval = intervalFreq
|
|
401 intervalOrder = data.frame("interval"=paste(">", interval, sep=""), "intervalOrder"=1:length(interval))
|
|
402 product = data.frame("Titles"=rep(Titles, each=length(interval)), "interval"=rep(interval, times=10), "V_Segments"=rep(V_Segments, each=length(interval)), "J_Segments"=rep(J_Segments, each=length(interval)))
|
|
403
|
9
|
404 one = triplets[triplets$Sample == "14696_reg_BM",]
|
|
405 two = triplets[triplets$Sample == "24536_reg_BM",]
|
|
406 three = triplets[triplets$Sample == "24062_reg_BM",]
|
8
|
407 tripletAnalysis(one, "14696_1", two, "14696_2", three, "14696_3", product=product, interval=interval, on="Frequency", F)
|
7
|
408
|
9
|
409 one = triplets[triplets$Sample == "16278_Left",]
|
|
410 two = triplets[triplets$Sample == "26402_Left",]
|
|
411 three = triplets[triplets$Sample == "26759_Left",]
|
8
|
412 tripletAnalysis(one, "16278_Left", two, "26402_Left", three, "26759_Left", product=product, interval=interval, on="Frequency", F)
|
7
|
413
|
9
|
414 one = triplets[triplets$Sample == "16278_Right",]
|
|
415 two = triplets[triplets$Sample == "26402_Right",]
|
|
416 three = triplets[triplets$Sample == "26759_Right",]
|
8
|
417 tripletAnalysis(one, "16278_Right", two, "26402_Right", three, "26759_Right", product=product, interval=interval, on="Frequency", F)
|