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