annotate report_clonality/RScript.r @ 0:afe85eb6572e draft

Uploaded
author davidvanzessen
date Mon, 29 Aug 2016 05:41:20 -0400
parents
children 90a05ff900db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
1 # ---------------------- load/install packages ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
2
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
3 if (!("gridExtra" %in% rownames(installed.packages()))) {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
4 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
5 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
6 library(gridExtra)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
7 if (!("ggplot2" %in% rownames(installed.packages()))) {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
8 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
9 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
10 library(ggplot2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
11 if (!("plyr" %in% rownames(installed.packages()))) {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
12 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
13 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
14 library(plyr)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
15
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
16 if (!("data.table" %in% rownames(installed.packages()))) {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
17 install.packages("data.table", repos="http://cran.xl-mirror.nl/")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
18 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
19 library(data.table)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
20
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
21 if (!("reshape2" %in% rownames(installed.packages()))) {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
22 install.packages("reshape2", repos="http://cran.xl-mirror.nl/")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
23 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
24 library(reshape2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
25
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
26 if (!("lymphclon" %in% rownames(installed.packages()))) {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
27 install.packages("lymphclon", repos="http://cran.xl-mirror.nl/")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
28 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
29 library(lymphclon)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
30
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
31 # ---------------------- parameters ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
32
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
33 args <- commandArgs(trailingOnly = TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
34
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
35 infile = args[1] #path to input file
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
36 outfile = args[2] #path to output file
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
37 outdir = args[3] #path to output folder (html/images/data)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
38 clonaltype = args[4] #clonaltype definition, or 'none' for no unique filtering
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
39 ct = unlist(strsplit(clonaltype, ","))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
40 species = args[5] #human or mouse
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
41 locus = args[6] # IGH, IGK, IGL, TRB, TRA, TRG or TRD
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
42 filterproductive = ifelse(args[7] == "yes", T, F) #should unproductive sequences be filtered out? (yes/no)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
43 clonality_method = args[8]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
44
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
45
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
46 # ---------------------- Data preperation ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
47
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
48 print("Report Clonality - Data preperation")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
49
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
50 inputdata = read.table(infile, sep="\t", header=TRUE, fill=T, comment.char="")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
51
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
52 print(paste("nrows: ", nrow(inputdata)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
53
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
54 setwd(outdir)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
55
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
56 # remove weird rows
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
57 inputdata = inputdata[inputdata$Sample != "",]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
58
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
59 print(paste("nrows: ", nrow(inputdata)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
60
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
61 #remove the allele from the V,D and J genes
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
62 inputdata$Top.V.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.V.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
63 inputdata$Top.D.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.D.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
64 inputdata$Top.J.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.J.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
65
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
66 print(paste("nrows: ", nrow(inputdata)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
67
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
68 #filter uniques
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
69 inputdata.removed = inputdata[NULL,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
70
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
71 print(paste("nrows: ", nrow(inputdata)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
72
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
73 inputdata$clonaltype = 1:nrow(inputdata)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
74
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
75 #keep track of the count of sequences in samples or samples/replicates for the front page overview
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
76 input.sample.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
77 input.rep.count = data.frame(data.table(inputdata)[, list(All=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
78
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
79 PRODF = inputdata
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
80 UNPROD = inputdata
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
81 if(filterproductive){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
82 if("Functionality" %in% colnames(inputdata)) { # "Functionality" is an IMGT column
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
83 #PRODF = inputdata[inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)", ]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
84 PRODF = inputdata[inputdata$Functionality %in% c("productive (see comment)","productive"),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
85
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
86 PRODF.count = data.frame(data.table(PRODF)[, list(count=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
87
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
88 UNPROD = inputdata[inputdata$Functionality %in% c("unproductive (see comment)","unproductive"), ]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
89 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
90 PRODF = inputdata[inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" , ]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
91 UNPROD = inputdata[!(inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" ), ]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
92 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
93 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
94
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
95 prod.sample.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
96 prod.rep.count = data.frame(data.table(PRODF)[, list(Productive=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
97
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
98 unprod.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
99 unprod.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
100
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
101 clonalityFrame = PRODF
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
102
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
103 #remove duplicates based on the clonaltype
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
104 if(clonaltype != "none"){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
105 clonaltype = paste(clonaltype, ",Sample", sep="") #add sample column to clonaltype, unique within samples
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
106 PRODF$clonaltype = do.call(paste, c(PRODF[unlist(strsplit(clonaltype, ","))], sep = ":"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
107 PRODF = PRODF[!duplicated(PRODF$clonaltype), ]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
108
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
109 UNPROD$clonaltype = do.call(paste, c(UNPROD[unlist(strsplit(clonaltype, ","))], sep = ":"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
110 UNPROD = UNPROD[!duplicated(UNPROD$clonaltype), ]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
111
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
112 #again for clonalityFrame but with sample+replicate
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
113 clonalityFrame$clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(clonaltype, ","))], sep = ":"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
114 clonalityFrame$clonality_clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(paste(clonaltype, ",Replicate", sep=""), ","))], sep = ":"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
115 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$clonality_clonaltype), ]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
116 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
117
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
118 prod.unique.sample.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
119 prod.unique.rep.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
120
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
121 unprod.unique.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
122 unprod.unique.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
123
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
124 PRODF$freq = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
125
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
126 if(any(grepl(pattern="_", x=PRODF$ID))){ #the frequency can be stored in the ID with the pattern ".*_freq_.*"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
127 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
128 PRODF$freq = gsub("_.*", "", PRODF$freq)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
129 PRODF$freq = as.numeric(PRODF$freq)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
130 if(any(is.na(PRODF$freq))){ #if there was an "_" in the ID, but not the frequency, go back to frequency of 1 for every sequence
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
131 PRODF$freq = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
132 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
133 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
134
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
135
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
136
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
137 #write the complete dataset that is left over, will be the input if 'none' for clonaltype and 'no' for filterproductive
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
138 write.table(PRODF, "allUnique.txt", sep="\t",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
139 write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
140 write.table(UNPROD, "allUnproductive.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
141
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
142 #write the samples to a file
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
143 sampleFile <- file("samples.txt")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
144 un = unique(inputdata$Sample)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
145 un = paste(un, sep="\n")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
146 writeLines(un, sampleFile)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
147 close(sampleFile)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
148
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
149 # ---------------------- Counting the productive/unproductive and unique sequences ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
150
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
151 print("Report Clonality - counting productive/unproductive/unique")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
152
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
153 #create the table on the overview page with the productive/unique counts per sample/replicate
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
154 #first for sample
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
155 sample.count = merge(input.sample.count, prod.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
156 sample.count$perc_prod = round(sample.count$Productive / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
157 sample.count = merge(sample.count, prod.unique.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
158 sample.count$perc_prod_un = round(sample.count$Productive_unique / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
159
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
160 sample.count = merge(sample.count , unprod.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
161 sample.count$perc_unprod = round(sample.count$Unproductive / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
162 sample.count = merge(sample.count, unprod.unique.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
163 sample.count$perc_unprod_un = round(sample.count$Unproductive_unique / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
164
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
165 #then sample/replicate
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
166 rep.count = merge(input.rep.count, prod.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
167 rep.count$perc_prod = round(rep.count$Productive / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
168 rep.count = merge(rep.count, prod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
169 rep.count$perc_prod_un = round(rep.count$Productive_unique / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
170
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
171 rep.count = merge(rep.count, unprod.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
172 rep.count$perc_unprod = round(rep.count$Unproductive / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
173 rep.count = merge(rep.count, unprod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
174 rep.count$perc_unprod_un = round(rep.count$Unproductive_unique / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
175
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
176 rep.count$Sample = paste(rep.count$Sample, rep.count$Replicate, sep="_")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
177 rep.count = rep.count[,names(rep.count) != "Replicate"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
178
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
179 count = rbind(sample.count, rep.count)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
180
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
181
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
182
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
183 write.table(x=count, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
184
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
185 # ---------------------- V+J+CDR3 sequence count ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
186
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
187 VJCDR3.count = data.frame(table(clonalityFrame$Top.V.Gene, clonalityFrame$Top.J.Gene, clonalityFrame$CDR3.Seq.DNA))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
188 names(VJCDR3.count) = c("Top.V.Gene", "Top.J.Gene", "CDR3.Seq.DNA", "Count")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
189
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
190 VJCDR3.count = VJCDR3.count[VJCDR3.count$Count > 0,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
191 VJCDR3.count = VJCDR3.count[order(-VJCDR3.count$Count),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
192
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
193 write.table(x=VJCDR3.count, file="VJCDR3_count.txt", sep="\t",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
194
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
195 # ---------------------- Frequency calculation for V, D and J ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
196
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
197 print("Report Clonality - frequency calculation V, D and J")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
198
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
199 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
200 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
201 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
202 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
203
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
204 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
205 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
206 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
207 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
208
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
209 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
210 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
211 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
212 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
213
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
214 # ---------------------- Setting up the gene names for the different species/loci ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
215
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
216 print("Report Clonality - getting genes for species/loci")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
217
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
218 Vchain = ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
219 Dchain = ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
220 Jchain = ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
221
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
222 if(species == "custom"){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
223 print("Custom genes: ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
224 splt = unlist(strsplit(locus, ";"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
225 print(paste("V:", splt[1]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
226 print(paste("D:", splt[2]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
227 print(paste("J:", splt[3]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
228
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
229 Vchain = unlist(strsplit(splt[1], ","))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
230 Vchain = data.frame(v.name = Vchain, chr.orderV = 1:length(Vchain))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
231
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
232 Dchain = unlist(strsplit(splt[2], ","))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
233 if(length(Dchain) > 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
234 Dchain = data.frame(v.name = Dchain, chr.orderD = 1:length(Dchain))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
235 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
236 Dchain = data.frame(v.name = character(0), chr.orderD = numeric(0))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
237 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
238
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
239 Jchain = unlist(strsplit(splt[3], ","))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
240 Jchain = data.frame(v.name = Jchain, chr.orderJ = 1:length(Jchain))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
241
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
242 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
243 genes = read.table("genes.txt", sep="\t", header=TRUE, fill=T, comment.char="")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
244
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
245 Vchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "V",c("IMGT.GENE.DB", "chr.order")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
246 colnames(Vchain) = c("v.name", "chr.orderV")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
247 Dchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "D",c("IMGT.GENE.DB", "chr.order")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
248 colnames(Dchain) = c("v.name", "chr.orderD")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
249 Jchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "J",c("IMGT.GENE.DB", "chr.order")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
250 colnames(Jchain) = c("v.name", "chr.orderJ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
251 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
252 useD = TRUE
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
253 if(nrow(Dchain) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
254 useD = FALSE
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
255 cat("No D Genes in this species/locus")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
256 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
257 print(paste(nrow(Vchain), "genes in V"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
258 print(paste(nrow(Dchain), "genes in D"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
259 print(paste(nrow(Jchain), "genes in J"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
260
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
261 # ---------------------- merge with the frequency count ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
262
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
263 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
264
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
265 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
266
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
267 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
268
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
269 # ---------------------- Create the V, D and J frequency plots and write the data.frame for every plot to a file ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
270
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
271 print("Report Clonality - V, D and J frequency plots")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
272
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
273 pV = ggplot(PRODFV)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
274 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))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
275 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
276 write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
277
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
278 png("VPlot.png",width = 1280, height = 720)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
279 pV
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
280 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
281
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
282 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
283 pD = ggplot(PRODFD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
284 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))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
285 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
286 write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
287
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
288 png("DPlot.png",width = 800, height = 600)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
289 print(pD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
290 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
291 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
292
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
293 pJ = ggplot(PRODFJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
294 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))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
295 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
296 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
297
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
298 png("JPlot.png",width = 800, height = 600)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
299 pJ
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
300 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
301
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
302 pJ = ggplot(PRODFJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
303 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))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
304 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
305 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
306
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
307 png("JPlot.png",width = 800, height = 600)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
308 pJ
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
309 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
310
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
311 # ---------------------- Now the frequency plots of the V, D and J families ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
312
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
313 print("Report Clonality - V, D and J family plots")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
314
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
315 VGenes = PRODF[,c("Sample", "Top.V.Gene")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
316 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
317 VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
318 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
319 VGenes = merge(VGenes, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
320 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
321 VPlot = ggplot(VGenes)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
322 VPlot = VPlot + geom_bar(aes( x = Top.V.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
323 ggtitle("Distribution of V gene families") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
324 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
325 png("VFPlot.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
326 VPlot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
327 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
328 write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
329
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
330 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
331 DGenes = PRODF[,c("Sample", "Top.D.Gene")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
332 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
333 DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
334 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
335 DGenes = merge(DGenes, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
336 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
337 DPlot = ggplot(DGenes)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
338 DPlot = DPlot + geom_bar(aes( x = Top.D.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
339 ggtitle("Distribution of D gene families") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
340 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
341 png("DFPlot.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
342 print(DPlot)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
343 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
344 write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
345 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
346
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
347 JGenes = PRODF[,c("Sample", "Top.J.Gene")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
348 JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
349 JGenes = data.frame(data.table(JGenes)[, list(Count=.N), by=c("Sample", "Top.J.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
350 TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
351 JGenes = merge(JGenes, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
352 JGenes$Frequency = JGenes$Count * 100 / JGenes$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
353 JPlot = ggplot(JGenes)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
354 JPlot = JPlot + geom_bar(aes( x = Top.J.Gene, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
355 ggtitle("Distribution of J gene families") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
356 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
357 png("JFPlot.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
358 JPlot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
359 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
360 write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
361
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
362 # ---------------------- Plotting the cdr3 length ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
363
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
364 print("Report Clonality - CDR3 length plot")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
365
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
366 CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length.DNA")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
367 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
368 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
369 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
370 CDR3LengthPlot = ggplot(CDR3Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
371 CDR3LengthPlot = CDR3LengthPlot + geom_bar(aes( x = CDR3.Length.DNA, y = Frequency, fill = Sample), stat='identity', position='dodge' ) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
372 ggtitle("Length distribution of CDR3") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
373 xlab("CDR3 Length") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
374 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
375 png("CDR3LengthPlot.png",width = 1280, height = 720)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
376 CDR3LengthPlot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
377 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
378 write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
379
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
380 # ---------------------- Plot the heatmaps ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
381
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
382 #get the reverse order for the V and D genes
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
383 revVchain = Vchain
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
384 revDchain = Dchain
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
385 revVchain$chr.orderV = rev(revVchain$chr.orderV)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
386 revDchain$chr.orderD = rev(revDchain$chr.orderD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
387
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
388 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
389 print("Report Clonality - Heatmaps VD")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
390 plotVD <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
391 if(length(dat[,1]) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
392 return()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
393 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
394
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
395 img = ggplot() +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
396 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
397 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
398 scale_fill_gradient(low="gold", high="blue", na.value="white") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
399 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
400 xlab("D genes") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
401 ylab("V Genes")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
402
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
403 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
404 print(img)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
405 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
406 write.table(x=acast(dat, Top.V.Gene~Top.D.Gene, value.var="Length"), file=paste("HeatmapVD_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
407 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
408
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
409 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
410
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
411 VandDCount$l = log(VandDCount$Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
412 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
413 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
414 VandDCount$relLength = VandDCount$l / VandDCount$max
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
415
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
416 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
417
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
418 completeVD = merge(VandDCount, cartegianProductVD, by.x=c("Top.V.Gene", "Top.D.Gene"), by.y=c("Top.V.Gene", "Top.D.Gene"), all=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
419
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
420 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
421
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
422 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
423
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
424 fltr = is.nan(completeVD$relLength)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
425 if(all(fltr)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
426 completeVD[fltr,"relLength"] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
427 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
428
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
429 VDList = split(completeVD, f=completeVD[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
430 lapply(VDList, FUN=plotVD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
431 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
432
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
433 print("Report Clonality - Heatmaps VJ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
434
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
435 plotVJ <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
436 if(length(dat[,1]) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
437 return()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
438 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
439 cat(paste(unique(dat[3])[1,1]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
440 img = ggplot() +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
441 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
442 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
443 scale_fill_gradient(low="gold", high="blue", na.value="white") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
444 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
445 xlab("J genes") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
446 ylab("V Genes")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
447
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
448 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
449 print(img)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
450 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
451 write.table(x=acast(dat, Top.V.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapVJ_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
452 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
453
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
454 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
455
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
456 VandJCount$l = log(VandJCount$Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
457 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
458 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
459 VandJCount$relLength = VandJCount$l / VandJCount$max
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
460
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
461 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
462
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
463 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
464 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
465 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
466
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
467 fltr = is.nan(completeVJ$relLength)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
468 if(any(fltr)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
469 completeVJ[fltr,"relLength"] = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
470 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
471
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
472 VJList = split(completeVJ, f=completeVJ[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
473 lapply(VJList, FUN=plotVJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
474
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
475
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
476
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
477 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
478 print("Report Clonality - Heatmaps DJ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
479 plotDJ <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
480 if(length(dat[,1]) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
481 return()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
482 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
483 img = ggplot() +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
484 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
485 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
486 scale_fill_gradient(low="gold", high="blue", na.value="white") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
487 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
488 xlab("J genes") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
489 ylab("D Genes")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
490
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
491 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
492 print(img)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
493 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
494 write.table(x=acast(dat, Top.D.Gene~Top.J.Gene, value.var="Length"), file=paste("HeatmapDJ_", unique(dat[3])[1,1], ".csv", sep=""), sep=",",quote=F,row.names=T,col.names=NA)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
495 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
496
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
497
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
498 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
499
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
500 DandJCount$l = log(DandJCount$Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
501 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
502 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
503 DandJCount$relLength = DandJCount$l / DandJCount$max
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
504
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
505 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
506
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
507 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
508 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
509 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
510
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
511 fltr = is.nan(completeDJ$relLength)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
512 if(any(fltr)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
513 completeDJ[fltr, "relLength"] = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
514 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
515
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
516 DJList = split(completeDJ, f=completeDJ[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
517 lapply(DJList, FUN=plotDJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
518 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
519
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
520
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
521 # ---------------------- output tables for the circos plots ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
522
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
523 print("Report Clonality - Circos data")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
524
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
525 for(smpl in unique(PRODF$Sample)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
526 PRODF.sample = PRODF[PRODF$Sample == smpl,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
527
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
528 fltr = PRODF.sample$Top.V.Gene == ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
529 if(any(fltr, na.rm=T)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
530 PRODF.sample[fltr, "Top.V.Gene"] = "NA"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
531 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
532
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
533 fltr = PRODF.sample$Top.D.Gene == ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
534 if(any(fltr, na.rm=T)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
535 PRODF.sample[fltr, "Top.D.Gene"] = "NA"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
536 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
537
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
538 fltr = PRODF.sample$Top.J.Gene == ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
539 if(any(fltr, na.rm=T)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
540 PRODF.sample[fltr, "Top.J.Gene"] = "NA"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
541 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
542
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
543 v.d = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.D.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
544 v.j = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.J.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
545 d.j = table(PRODF.sample$Top.D.Gene, PRODF.sample$Top.J.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
546
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
547 write.table(v.d, file=paste(smpl, "_VD_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
548 write.table(v.j, file=paste(smpl, "_VJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
549 write.table(d.j, file=paste(smpl, "_DJ_circos.txt", sep=""), sep="\t", quote=F, row.names=T, col.names=NA)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
550 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
551
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
552 # ---------------------- calculating the clonality score ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
553
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
554 if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
555 {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
556 print("Report Clonality - Clonality")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
557 write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
558 if(clonality_method == "boyd"){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
559 samples = split(clonalityFrame, clonalityFrame$Sample, drop=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
560
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
561 for (sample in samples){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
562 res = data.frame(paste=character(0))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
563 sample_id = unique(sample$Sample)[[1]]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
564 for(replicate in unique(sample$Replicate)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
565 tmp = sample[sample$Replicate == replicate,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
566 clone_table = data.frame(table(tmp$clonaltype))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
567 clone_col_name = paste("V", replicate, sep="")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
568 colnames(clone_table) = c("paste", clone_col_name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
569 res = merge(res, clone_table, by="paste", all=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
570 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
571
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
572 res[is.na(res)] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
573 infer.result = infer.clonality(as.matrix(res[,2:ncol(res)]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
574
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
575 print(infer.result)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
576
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
577 write.table(data.table(infer.result[[12]]), file=paste("lymphclon_clonality_", sample_id, ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
578
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
579 res$type = rowSums(res[,2:ncol(res)])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
580
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
581 coincidence.table = data.frame(table(res$type))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
582 colnames(coincidence.table) = c("Coincidence Type", "Raw Coincidence Freq")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
583 write.table(coincidence.table, file=paste("lymphclon_coincidences_", sample_id, ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
584 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
585 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
586 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
587
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
588 #write files for every coincidence group of >1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
589 samples = unique(clonalFreq$Sample)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
590 for(sample in samples){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
591 clonalFreqSample = clonalFreq[clonalFreq$Sample == sample,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
592 if(max(clonalFreqSample$Type) > 1){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
593 for(i in 2:max(clonalFreqSample$Type)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
594 clonalFreqSampleType = clonalFreqSample[clonalFreqSample$Type == i,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
595 clonalityFrame.sub = clonalityFrame[clonalityFrame$clonaltype %in% clonalFreqSampleType$clonaltype,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
596 clonalityFrame.sub = clonalityFrame.sub[order(clonalityFrame.sub$clonaltype),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
597 write.table(clonalityFrame.sub, file=paste("coincidences_", sample, "_", i, ".txt", sep=""), sep="\t",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
598 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
599 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
600 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
601
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
602 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
603 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
604 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
605 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
606
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
607 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
608 tcct = textConnection(ct)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
609 CT = read.table(tcct, sep="\t", header=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
610 close(tcct)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
611 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
612 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
613
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
614 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
615 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
616 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
617 ReplicateReads$Reads = as.numeric(ReplicateReads$Reads)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
618 ReplicateReads$squared = as.numeric(ReplicateReads$Reads * ReplicateReads$Reads)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
619
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
620 ReplicatePrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
621 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
622 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
623
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
624 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
625 lapply(ReplicateSplit, FUN=ReplicatePrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
626
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
627 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(as.numeric(Reads)), ReadsSquaredSum=sum(as.numeric(squared))), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
628 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
629
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
630 ReplicateSumPrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
631 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
632 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
633
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
634 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
635 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
636
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
637 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
638 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
639 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
640 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
641 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
642
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
643 ClonalityScorePrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
644 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
645 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
646
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
647 clonalityScore = clonalFreqCount[c("Sample", "Result")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
648 clonalityScore = unique(clonalityScore)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
649
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
650 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
651 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
652
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
653 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
654
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
655
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
656
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
657 ClonalityOverviewPrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
658 dat = dat[order(dat[,2]),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
659 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
660 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
661
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
662 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
663 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
664 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
665 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
666
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
667 bak = PRODF
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
668
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
669 imgtcolumns = c("X3V.REGION.trimmed.nt.nb","P3V.nt.nb", "N1.REGION.nt.nb", "P5D.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "P3D.nt.nb", "N2.REGION.nt.nb", "P5J.nt.nb", "X5J.REGION.trimmed.nt.nb", "X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
670 if(all(imgtcolumns %in% colnames(inputdata)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
671 {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
672 print("found IMGT columns, running junction analysis")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
673
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
674 if(locus %in% c("IGK","IGL", "TRA", "TRG")){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
675 print("VJ recombination, no filtering on absent D")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
676 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
677 print("VDJ recombination, using N column for junction analysis")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
678 fltr = nchar(PRODF$Top.D.Gene) < 4
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
679 print(paste("Removing", sum(fltr), "sequences without a identified D"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
680 PRODF = PRODF[!fltr,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
681 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
682
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
683
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
684 #ensure certain columns are in the data (files generated with older versions of IMGT Loader)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
685 col.checks = c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
686 for(col.check in col.checks){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
687 if(!(col.check %in% names(PRODF))){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
688 print(paste(col.check, "not found adding new column"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
689 if(nrow(PRODF) > 0){ #because R is anoying...
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
690 PRODF[,col.check] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
691 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
692 PRODF = cbind(PRODF, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
693 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
694 if(nrow(UNPROD) > 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
695 UNPROD[,col.check] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
696 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
697 UNPROD = cbind(UNPROD, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
698 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
699 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
700 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
701
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
702 num_median = function(x, na.rm=T) { as.numeric(median(x, na.rm=na.rm)) }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
703
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
704 newData = data.frame(data.table(PRODF)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
705 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
706 P1=mean(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
707 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
708 P2=mean(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
709 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
710 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
711 P3=mean(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
712 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
713 P4=mean(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
714 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
715 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
716 Total.N=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
717 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T))),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
718 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
719 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
720 write.table(newData, "junctionAnalysisProd_mean.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
721
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
722 newData = data.frame(data.table(PRODF)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
723 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
724 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
725 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
726 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
727 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
728 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
729 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
730 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
731 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
732 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
733 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
734 Total.N=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
735 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T))),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
736 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
737 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
738 write.table(newData, "junctionAnalysisProd_median.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
739
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
740 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
741 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
742 P1=mean(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
743 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
744 P2=mean(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
745 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
746 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
747 P3=mean(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
748 N2=mean(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
749 P4=mean(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
750 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
751 Total.Del=mean(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
752 Total.N=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
753 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T))),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
754 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
755 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
756 write.table(newData, "junctionAnalysisUnProd_mean.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
757
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
758 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
759 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
760 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
761 N1=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
762 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
763 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
764 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
765 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
766 N2=num_median(rowSums(.SD[,c("N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
767 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
768 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
769 Total.Del=num_median(rowSums(.SD[,c("X3V.REGION.trimmed.nt.nb", "X5D.REGION.trimmed.nt.nb", "X3D.REGION.trimmed.nt.nb", "X5J.REGION.trimmed.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
770 Total.N=num_median(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb", "N2.REGION.nt.nb", "N3.REGION.nt.nb", "N4.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
771 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T))),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
772 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
773
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
774 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
775 write.table(newData, "junctionAnalysisUnProd_median.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
776 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
777
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
778 PRODF = bak
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
779
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
780
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
781 # ---------------------- D reading frame ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
782
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
783 D.REGION.reading.frame = PRODF$D.REGION.reading.frame
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
784
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
785 D.REGION.reading.frame[is.na(D.REGION.reading.frame)] = "No D"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
786
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
787 D.REGION.reading.frame = data.frame(table(D.REGION.reading.frame))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
788
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
789 write.table(D.REGION.reading.frame, "DReadingFrame.csv" , sep="\t",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
790
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
791 D.REGION.reading.frame = ggplot(D.REGION.reading.frame)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
792 D.REGION.reading.frame = D.REGION.reading.frame + geom_bar(aes( x = D.REGION.reading.frame, y = Freq), stat='identity', position='dodge' ) + ggtitle("D reading frame") + xlab("Frequency") + ylab("Frame")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
793
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
794 png("DReadingFrame.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
795 D.REGION.reading.frame
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
796 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
797
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
798
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
799
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
800
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
801 # ---------------------- AA composition in CDR3 ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
802
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
803 AACDR3 = PRODF[,c("Sample", "CDR3.Seq")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
804
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
805 TotalPerSample = data.frame(data.table(AACDR3)[, list(total=sum(nchar(as.character(.SD$CDR3.Seq)))), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
806
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
807 AAfreq = list()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
808
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
809 for(i in 1:nrow(TotalPerSample)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
810 sample = TotalPerSample$Sample[i]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
811 AAfreq[[i]] = data.frame(table(unlist(strsplit(as.character(AACDR3[AACDR3$Sample == sample,c("CDR3.Seq")]), ""))))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
812 AAfreq[[i]]$Sample = sample
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
813 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
814
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
815 AAfreq = ldply(AAfreq, data.frame)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
816 AAfreq = merge(AAfreq, TotalPerSample, by="Sample", all.x = T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
817 AAfreq$freq_perc = as.numeric(AAfreq$Freq / AAfreq$total * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
818
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
819
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
820 AAorder = read.table(sep="\t", header=TRUE, text="order.aa\tAA\n1\tR\n2\tK\n3\tN\n4\tD\n5\tQ\n6\tE\n7\tH\n8\tP\n9\tY\n10\tW\n11\tS\n12\tT\n13\tG\n14\tA\n15\tM\n16\tC\n17\tF\n18\tL\n19\tV\n20\tI")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
821 AAfreq = merge(AAfreq, AAorder, by.x='Var1', by.y='AA', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
822
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
823 AAfreq = AAfreq[!is.na(AAfreq$order.aa),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
824
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
825 AAfreqplot = ggplot(AAfreq)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
826 AAfreqplot = AAfreqplot + geom_bar(aes( x=factor(reorder(Var1, order.aa)), y = freq_perc, fill = Sample), stat='identity', position='dodge' )
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
827 AAfreqplot = AAfreqplot + annotate("rect", xmin = 0.5, xmax = 2.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
828 AAfreqplot = AAfreqplot + annotate("rect", xmin = 3.5, xmax = 4.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
829 AAfreqplot = AAfreqplot + annotate("rect", xmin = 5.5, xmax = 6.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
830 AAfreqplot = AAfreqplot + annotate("rect", xmin = 6.5, xmax = 7.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
831 AAfreqplot = AAfreqplot + ggtitle("Amino Acid Composition in the CDR3") + xlab("Amino Acid, from Hydrophilic (left) to Hydrophobic (right)") + ylab("Percentage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
832
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
833 png("AAComposition.png",width = 1280, height = 720)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
834 AAfreqplot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
835 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
836 write.table(AAfreq, "AAComposition.csv" , sep=",",quote=F,na="-",row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
837
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
838