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)
|
2
|
18 dat = read.csv(inFile, sep="\t")
|
|
19 #dat = data.frame(fread(inFile)) #faster but with a dep
|
0
|
20 setwd(outDir)
|
3
|
21 cat("<tr><td>Selecting first V/J Genes</td></tr>", file=logfile, append=T)
|
2
|
22 dat$V_Segment_Major_Gene = as.factor(as.character(lapply(strsplit(as.character(dat$V_Segment_Major_Gene), "; "), "[[", 1)))
|
|
23 dat$J_Segment_Major_Gene = as.factor(as.character(lapply(strsplit(as.character(dat$J_Segment_Major_Gene), "; "), "[[", 1)))
|
|
24
|
3
|
25 cat("<tr><td>Calculating Frequency</td></tr>", file=logfile, append=T)
|
0
|
26 dat$Frequency = ((10^dat$Log10_Frequency)*100)
|
2
|
27
|
3
|
28 dat = dat[dat$Frequency >= min_freq,]
|
|
29
|
|
30 cat("<tr><td>Normalizing cell count to 1.000.000</td></tr>", file=logfile, append=T)
|
2
|
31 dat$normalized_read_count = round(dat$Clone_Molecule_Count_From_Spikes / dat$Cell_Count * 1000000 / 2)
|
3
|
32 dat = dat[dat$normalized_read_count >= min_cells,]
|
2
|
33 dat$paste = paste(dat$Sample, dat$V_Segment_Major_Gene, dat$J_Segment_Major_Gene, dat$CDR3_Sense_Sequence)
|
3
|
34 cat("<tr><td>Removing duplicates</td></tr>", file=logfile, append=T)
|
2
|
35 dat = dat[!duplicated(dat$paste),]
|
0
|
36 patients = split(dat, dat$Patient, drop=T)
|
4
|
37 rm(dat)
|
|
38 intervalReads = rev(c(0,10,25,50,100,1000,10000))
|
0
|
39 intervalFreq = rev(c(0,0.01,0.1,0.5,1,5))
|
|
40 V_Segments = c(".*", "IGHV", "IGHD", "IGKV", "IGKV", "IgKINTR", "TRGV", "TRDV", "TRDD" , "TRBV")
|
|
41 J_Segments = c(".*", ".*", ".*", "IGKJ", "KDE", ".*", ".*", ".*", ".*", ".*")
|
|
42 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")
|
|
43 Titles = factor(Titles, levels=Titles)
|
|
44 TitlesOrder = data.frame("Title"=Titles, "TitlesOrder"=1:length(Titles))
|
|
45
|
|
46 patientCountOnColumn <- function(x, product, interval, on, appendtxt=F){
|
|
47 x$Sample = factor(x$Sample, levels=unique(x$Sample))
|
|
48 onShort = "reads"
|
|
49 if(on == "Frequency"){
|
|
50 onShort = "freq"
|
|
51 }
|
|
52 splt = split(x, x$Sample, drop=T)
|
4
|
53 type="pair"
|
2
|
54 if(length(splt) == 1){
|
3
|
55 print(paste(paste(x[1,which(colnames(x) == "Patient")]), "has one sample"))
|
4
|
56 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))
|
|
57 type="single"
|
2
|
58 }
|
0
|
59 patient1 = splt[[1]]
|
|
60 patient2 = splt[[2]]
|
|
61
|
|
62 threshholdIndex = which(colnames(product) == "interval")
|
|
63 V_SegmentIndex = which(colnames(product) == "V_Segments")
|
|
64 J_SegmentIndex = which(colnames(product) == "J_Segments")
|
|
65 titleIndex = which(colnames(product) == "Titles")
|
|
66 sampleIndex = which(colnames(x) == "Sample")
|
|
67 patientIndex = which(colnames(x) == "Patient")
|
|
68 oneSample = paste(patient1[1,sampleIndex], sep="")
|
|
69 twoSample = paste(patient2[1,sampleIndex], sep="")
|
|
70 patient = paste(x[1,patientIndex])
|
3
|
71
|
0
|
72 switched = F
|
|
73 if(length(grep(".*_Right$", twoSample)) == 1 || length(grep(".*_Dx_BM$", twoSample)) == 1 || length(grep(".*_Dx$", twoSample)) == 1 ){
|
|
74 tmp = twoSample
|
|
75 twoSample = oneSample
|
|
76 oneSample = tmp
|
|
77 tmp = patient1
|
|
78 patient1 = patient2
|
|
79 patient2 = tmp
|
|
80 switched = T
|
|
81 }
|
|
82 if(appendtxt){
|
4
|
83 cat(paste(patient, oneSample, twoSample, type, sep="\t"), file="patients.txt", append=T, sep="", fill=3)
|
0
|
84 }
|
3
|
85 cat(paste("<tr><td>", patient, "</td></tr>", sep=""), file=logfile, append=T)
|
0
|
86 patientMerge = merge(patient1, patient2, by="Clone_Sequence")
|
|
87 res1 = vector()
|
|
88 res2 = vector()
|
|
89 resBoth = vector()
|
|
90 read1Count = vector()
|
|
91 read2Count = vector()
|
2
|
92 locussum1 = vector()
|
|
93 locussum2 = vector()
|
0
|
94
|
2
|
95 print(patient)
|
0
|
96 #for(iter in 1){
|
|
97 for(iter in 1:length(product[,1])){
|
|
98 threshhold = product[iter,threshholdIndex]
|
|
99 V_Segment = paste(".*", as.character(product[iter,V_SegmentIndex]), ".*", sep="")
|
|
100 J_Segment = paste(".*", as.character(product[iter,J_SegmentIndex]), ".*", sep="")
|
|
101 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)
|
|
102 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))
|
|
103 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))
|
2
|
104 read1Count = append(read1Count, sum(patient1[one,]$normalized_read_count) + sum(patientMerge[both,]$normalized_read_count.x))
|
|
105 read2Count = append(read2Count, sum(patient2[two,]$normalized_read_count) + sum(patientMerge[both,]$normalized_read_count.y))
|
0
|
106 res1 = append(res1, sum(one))
|
2
|
107 res2 = append(res2, sum(two))
|
0
|
108 resBoth = append(resBoth, sum(both))
|
2
|
109 locussum1 = append(locussum1, sum(patient1[(grepl(V_Segment, patient1$V_Segment_Major_Gene) & grepl(J_Segment, patient1$J_Segment_Major_Gene)),]$normalized_read_count))
|
|
110 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
|
111 #threshhold = 0
|
|
112 if(threshhold != 0){
|
|
113 if(sum(one) > 0){
|
4
|
114 dfOne = patient1[one,c("V_Segment_Major_Gene", "J_Segment_Major_Gene", "normalized_read_count", "Frequency", "Clone_Sequence", "Related_to_leukemia_clone")]
|
|
115 colnames(dfOne) = c("Proximal segment", "Distal segment", "normalized_read_count", "Frequency", "Sequence", "Related_to_leukemia_clone")
|
0
|
116 filenameOne = paste(oneSample, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
117 write.table(dfOne, file=paste(filenameOne, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
118 }
|
|
119 if(sum(two) > 0){
|
4
|
120 dfTwo = patient2[two,c("V_Segment_Major_Gene", "J_Segment_Major_Gene", "normalized_read_count", "Frequency", "Clone_Sequence", "Related_to_leukemia_clone")]
|
|
121 colnames(dfTwo) = c("Proximal segment", "Distal segment", "normalized_read_count", "Frequency", "Sequence", "Related_to_leukemia_clone")
|
0
|
122 filenameTwo = paste(twoSample, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
123 write.table(dfTwo, file=paste(filenameTwo, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
124 }
|
|
125 }
|
|
126 if(sum(both) > 0){
|
4
|
127 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", "V_Segment_Major_Gene.y", "J_Segment_Major_Gene.y", "normalized_read_count.y", "Frequency.y", "Related_to_leukemia_clone.y")]
|
|
128 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),"Sequence", paste("Proximal segment", twoSample), paste("Distal segment", twoSample), paste("Normalized_Read_Count", twoSample), paste("Frequency", twoSample), paste("Related_to_leukemia_clone", twoSample))
|
0
|
129 filenameBoth = paste(oneSample, "_", twoSample, "_", product[iter, titleIndex], "_", threshhold, sep="")
|
|
130 write.table(dfBoth, file=paste(filenameBoth, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
131 }
|
|
132 }
|
2
|
133 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
|
134 if(sum(is.na(patientResult$percentage)) > 0){
|
|
135 patientResult[is.na(patientResult$percentage),]$percentage = 0
|
|
136 }
|
|
137 colnames(patientResult)[6] = oneSample
|
|
138 colnames(patientResult)[8] = twoSample
|
|
139 colnamesBak = colnames(patientResult)
|
2
|
140 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
|
141 write.table(patientResult, file=paste(patient, "_", onShort, ".txt", sep=""), quote=F, sep="\t", dec=",", row.names=F, col.names=T)
|
|
142 colnames(patientResult) = colnamesBak
|
|
143
|
|
144 patientResult$Locus = factor(patientResult$Locus, Titles)
|
|
145 patientResult$cut_off_value = factor(patientResult$cut_off_value, paste(">", interval, sep=""))
|
|
146
|
|
147 plt = ggplot(patientResult[,c("Locus", "cut_off_value", "Both")])
|
|
148 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=Both), stat='identity', position="dodge", fill="#79c36a")
|
|
149 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
150 plt = plt + geom_text(aes(ymax=max(Both), x=cut_off_value,y=Both,label=Both), angle=90, hjust=0)
|
|
151 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle("Number of clones in both")
|
|
152 plt = plt + theme(plot.margin = unit(c(1,8.8,0.5,1.5), "lines"))
|
|
153 png(paste(patient, "_", onShort, ".png", sep=""), width=1920, height=1080)
|
|
154 print(plt)
|
|
155 dev.off()
|
|
156 #(t,r,b,l)
|
|
157 plt = ggplot(patientResult[,c("Locus", "cut_off_value", "percentage")])
|
|
158 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=percentage), stat='identity', position="dodge", fill="#79c36a")
|
|
159 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
160 plt = plt + geom_text(aes(ymax=max(percentage), x=cut_off_value,y=percentage,label=percentage), angle=90, hjust=0)
|
|
161 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle("% clones in both left and right")
|
|
162 plt = plt + theme(plot.margin = unit(c(1,8.8,0.5,1.5), "lines"))
|
|
163 png(paste(patient, "_percent_", onShort, ".png", sep=""), width=1920, height=1080)
|
|
164 print(plt)
|
|
165 dev.off()
|
|
166
|
|
167 patientResult = melt(patientResult[,c('Locus','cut_off_value', oneSample, twoSample)] ,id.vars=1:2)
|
|
168 patientResult$relativeValue = patientResult$value * 10
|
|
169 patientResult[patientResult$relativeValue == 0,]$relativeValue = 1
|
|
170 plt = ggplot(patientResult)
|
|
171 plt = plt + geom_bar( aes( x=factor(cut_off_value), y=relativeValue, fill=variable), stat='identity', position="dodge")
|
|
172 plt = plt + facet_grid(.~Locus) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
|
|
173 plt = plt + scale_y_continuous(trans="log", breaks=10^c(0:10), labels=c(0, 10^c(0:9)))
|
|
174 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)
|
|
175 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)
|
|
176 plt = plt + xlab("Reads per locus") + ylab("Count") + ggtitle(paste("Number of clones in only ", oneSample, " and only ", twoSample, sep=""))
|
|
177 png(paste(patient, "_", onShort, "_both.png", sep=""), width=1920, height=1080)
|
|
178 print(plt)
|
|
179 dev.off()
|
|
180 }
|
|
181
|
3
|
182 cat("<tr><td>Starting Frequency analysis</td></tr>", file=logfile, append=T)
|
|
183
|
0
|
184 interval = intervalFreq
|
|
185 intervalOrder = data.frame("interval"=paste(">", interval, sep=""), "intervalOrder"=1:length(interval))
|
4
|
186 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)))
|
0
|
187 #patientFrequencyCount(patient1)
|
|
188 #lapply(patients[c(5,6,10)], FUN=patientFrequencyCount)
|
|
189 #lapply(patients[c(5,6,7,8,13)], FUN=patientCountOnColumn, product = product, interval=interval, on="Frequency", appendtxt=T)
|
|
190 #lapply(patients[c(6,7,8)], FUN=patientCountOnColumn, product = product, interval=interval, on="Frequency", appendtxt=T)
|
|
191 #lapply(patients[c(6)], FUN=patientCountOnColumn, product = product, interval=interval, on="Frequency", appendtxt=T)
|
4
|
192 mclapply(patients, FUN=patientCountOnColumn, product = product, interval=interval, on="Frequency", appendtxt=T)
|
0
|
193
|
3
|
194 cat("<tr><td>Starting Cell Count analysis</td></tr>", file=logfile, append=T)
|
|
195
|
0
|
196 interval = intervalReads
|
|
197 intervalOrder = data.frame("interval"=paste(">", interval, sep=""), "intervalOrder"=1:length(interval))
|
4
|
198 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)))
|
0
|
199 #patientResult = patientReadCount(patient1)
|
|
200 #lapply(patients[c(5,6,10)], FUN=patientReadCount)
|
|
201 #lapply(patients[c(5,6,7,8,13)], FUN=patientCountOnColumn, product = product, interval=interval, on="Clone_Molecule_Count_From_Spikes")
|
|
202 #lapply(patients[c(6)], FUN=patientCountOnColumn, product = product, interval=interval, on="Clone_Molecule_Count_From_Spikes")
|
4
|
203 mclapply(patients, FUN=patientCountOnColumn, product = product, interval=interval, on="Clone_Molecule_Count_From_Spikes")
|
0
|
204
|
3
|
205 cat("</table></html>", file=logfile, append=T)
|
|
206
|