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