annotate report_clonality/RScript.r @ 3:beaa487ecf43 draft

Uploaded
author davidvanzessen
date Wed, 07 Dec 2016 08:36:23 -0500
parents 90a05ff900db
children
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/")
3
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
13 }
0
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
1
90a05ff900db Uploaded
davidvanzessen
parents: 0
diff changeset
118 print("SAMPLE TABLE:")
90a05ff900db Uploaded
davidvanzessen
parents: 0
diff changeset
119 print(table(PRODF$Sample))
90a05ff900db Uploaded
davidvanzessen
parents: 0
diff changeset
120
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
121 prod.unique.sample.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
122 prod.unique.rep.count = data.frame(data.table(PRODF)[, list(Productive_unique=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
123
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
124 unprod.unique.sample.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
125 unprod.unique.rep.count = data.frame(data.table(UNPROD)[, list(Unproductive_unique=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
126
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
127 PRODF$freq = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
128
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
129 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
130 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
131 PRODF$freq = gsub("_.*", "", PRODF$freq)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
132 PRODF$freq = as.numeric(PRODF$freq)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
133 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
134 PRODF$freq = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
135 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
136 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
137
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
138
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
139
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
140 #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
141 write.table(PRODF, "allUnique.txt", sep="\t",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
142 write.table(PRODF, "allUnique.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
143 write.table(UNPROD, "allUnproductive.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
144
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
145 #write the samples to a file
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
146 sampleFile <- file("samples.txt")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
147 un = unique(inputdata$Sample)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
148 un = paste(un, sep="\n")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
149 writeLines(un, sampleFile)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
150 close(sampleFile)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
151
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
152 # ---------------------- Counting the productive/unproductive and unique sequences ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
153
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
154 print("Report Clonality - counting productive/unproductive/unique")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
155
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
156 #create the table on the overview page with the productive/unique counts per sample/replicate
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
157 #first for sample
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
158 sample.count = merge(input.sample.count, prod.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
159 sample.count$perc_prod = round(sample.count$Productive / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
160 sample.count = merge(sample.count, prod.unique.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
161 sample.count$perc_prod_un = round(sample.count$Productive_unique / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
162
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
163 sample.count = merge(sample.count , unprod.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
164 sample.count$perc_unprod = round(sample.count$Unproductive / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
165 sample.count = merge(sample.count, unprod.unique.sample.count, by="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
166 sample.count$perc_unprod_un = round(sample.count$Unproductive_unique / sample.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
167
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
168 #then sample/replicate
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
169 rep.count = merge(input.rep.count, prod.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
170 rep.count$perc_prod = round(rep.count$Productive / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
171 rep.count = merge(rep.count, prod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
172 rep.count$perc_prod_un = round(rep.count$Productive_unique / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
173
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
174 rep.count = merge(rep.count, unprod.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
175 rep.count$perc_unprod = round(rep.count$Unproductive / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
176 rep.count = merge(rep.count, unprod.unique.rep.count, by=c("Sample", "Replicate"), all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
177 rep.count$perc_unprod_un = round(rep.count$Unproductive_unique / rep.count$All * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
178
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
179 rep.count$Sample = paste(rep.count$Sample, rep.count$Replicate, sep="_")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
180 rep.count = rep.count[,names(rep.count) != "Replicate"]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
181
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
182 count = rbind(sample.count, rep.count)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
183
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
184
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
185
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
186 write.table(x=count, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
187
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
188 # ---------------------- V+J+CDR3 sequence count ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
189
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
190 VJCDR3.count = data.frame(table(clonalityFrame$Top.V.Gene, clonalityFrame$Top.J.Gene, clonalityFrame$CDR3.Seq.DNA))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
191 names(VJCDR3.count) = c("Top.V.Gene", "Top.J.Gene", "CDR3.Seq.DNA", "Count")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
192
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
193 VJCDR3.count = VJCDR3.count[VJCDR3.count$Count > 0,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
194 VJCDR3.count = VJCDR3.count[order(-VJCDR3.count$Count),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
195
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
196 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
197
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
198 # ---------------------- Frequency calculation for V, D and J ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
199
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
200 print("Report Clonality - frequency calculation V, D and J")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
201
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
202 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
203 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
204 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
205 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
206
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
207 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
208 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
209 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
210 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
211
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
212 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
213 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
214 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
215 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
216
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
217 # ---------------------- Setting up the gene names for the different species/loci ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
218
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
219 print("Report Clonality - getting genes for species/loci")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
220
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
221 Vchain = ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
222 Dchain = ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
223 Jchain = ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
224
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
225 if(species == "custom"){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
226 print("Custom genes: ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
227 splt = unlist(strsplit(locus, ";"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
228 print(paste("V:", splt[1]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
229 print(paste("D:", splt[2]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
230 print(paste("J:", splt[3]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
231
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
232 Vchain = unlist(strsplit(splt[1], ","))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
233 Vchain = data.frame(v.name = Vchain, chr.orderV = 1:length(Vchain))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
234
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
235 Dchain = unlist(strsplit(splt[2], ","))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
236 if(length(Dchain) > 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
237 Dchain = data.frame(v.name = Dchain, chr.orderD = 1:length(Dchain))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
238 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
239 Dchain = data.frame(v.name = character(0), chr.orderD = numeric(0))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
240 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
241
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
242 Jchain = unlist(strsplit(splt[3], ","))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
243 Jchain = data.frame(v.name = Jchain, chr.orderJ = 1:length(Jchain))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
244
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
245 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
246 genes = read.table("genes.txt", sep="\t", header=TRUE, fill=T, comment.char="")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
247
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
248 Vchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "V",c("IMGT.GENE.DB", "chr.order")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
249 colnames(Vchain) = c("v.name", "chr.orderV")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
250 Dchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "D",c("IMGT.GENE.DB", "chr.order")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
251 colnames(Dchain) = c("v.name", "chr.orderD")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
252 Jchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "J",c("IMGT.GENE.DB", "chr.order")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
253 colnames(Jchain) = c("v.name", "chr.orderJ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
254 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
255 useD = TRUE
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
256 if(nrow(Dchain) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
257 useD = FALSE
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
258 cat("No D Genes in this species/locus")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
259 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
260 print(paste(nrow(Vchain), "genes in V"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
261 print(paste(nrow(Dchain), "genes in D"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
262 print(paste(nrow(Jchain), "genes in J"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
263
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
264 # ---------------------- merge with the frequency count ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
265
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
266 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
267
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
268 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
269
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
270 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
271
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
272 # ---------------------- 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
273
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
274 print("Report Clonality - V, D and J frequency plots")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
275
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
276 pV = ggplot(PRODFV)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
277 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
278 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
279 write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
280
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
281 png("VPlot.png",width = 1280, height = 720)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
282 pV
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
283 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
284
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
285 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
286 pD = ggplot(PRODFD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
287 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
288 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
289 write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
290
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
291 png("DPlot.png",width = 800, height = 600)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
292 print(pD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
293 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
294 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
295
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
296 pJ = ggplot(PRODFJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
297 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
298 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
299 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
300
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
301 png("JPlot.png",width = 800, height = 600)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
302 pJ
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
303 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
304
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
305 pJ = ggplot(PRODFJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
306 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
307 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
308 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
309
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
310 png("JPlot.png",width = 800, height = 600)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
311 pJ
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
312 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
313
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
314 # ---------------------- Now the frequency plots of the V, D and J families ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
315
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
316 print("Report Clonality - V, D and J family plots")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
317
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
318 VGenes = PRODF[,c("Sample", "Top.V.Gene")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
319 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
320 VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
321 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
322 VGenes = merge(VGenes, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
323 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
324 VPlot = ggplot(VGenes)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
325 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
326 ggtitle("Distribution of V gene families") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
327 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
328 png("VFPlot.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
329 VPlot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
330 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
331 write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
332
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
333 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
334 DGenes = PRODF[,c("Sample", "Top.D.Gene")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
335 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
336 DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
337 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
338 DGenes = merge(DGenes, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
339 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
340 DPlot = ggplot(DGenes)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
341 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
342 ggtitle("Distribution of D gene families") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
343 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
344 png("DFPlot.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
345 print(DPlot)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
346 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
347 write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
348 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
349
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
350 JGenes = PRODF[,c("Sample", "Top.J.Gene")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
351 JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
352 JGenes = data.frame(data.table(JGenes)[, list(Count=.N), by=c("Sample", "Top.J.Gene")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
353 TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
354 JGenes = merge(JGenes, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
355 JGenes$Frequency = JGenes$Count * 100 / JGenes$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
356 JPlot = ggplot(JGenes)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
357 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
358 ggtitle("Distribution of J gene families") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
359 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
360 png("JFPlot.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
361 JPlot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
362 dev.off();
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
363 write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
364
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
365 # ---------------------- Plotting the cdr3 length ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
366
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
367 print("Report Clonality - CDR3 length plot")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
368
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
369 CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length.DNA")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
370 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
371 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
372 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
373 CDR3LengthPlot = ggplot(CDR3Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
374 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
375 ggtitle("Length distribution of CDR3") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
376 xlab("CDR3 Length") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
377 ylab("Percentage of sequences")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
378 png("CDR3LengthPlot.png",width = 1280, height = 720)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
379 CDR3LengthPlot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
380 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
381 write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
382
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
383 # ---------------------- Plot the heatmaps ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
384
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
385 #get the reverse order for the V and D genes
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
386 revVchain = Vchain
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
387 revDchain = Dchain
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
388 revVchain$chr.orderV = rev(revVchain$chr.orderV)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
389 revDchain$chr.orderD = rev(revDchain$chr.orderD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
390
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
391 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
392 print("Report Clonality - Heatmaps VD")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
393 plotVD <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
394 if(length(dat[,1]) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
395 return()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
396 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
397
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
398 img = ggplot() +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
399 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
400 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
401 scale_fill_gradient(low="gold", high="blue", na.value="white") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
402 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
403 xlab("D genes") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
404 ylab("V Genes")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
405
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
406 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
407 print(img)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
408 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
409 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
410 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
411
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
412 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
413
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
414 VandDCount$l = log(VandDCount$Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
415 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
416 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
417 VandDCount$relLength = VandDCount$l / VandDCount$max
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
418
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
419 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
420
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
421 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
422
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
423 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
424
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
425 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
426
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
427 fltr = is.nan(completeVD$relLength)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
428 if(all(fltr)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
429 completeVD[fltr,"relLength"] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
430 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
431
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
432 VDList = split(completeVD, f=completeVD[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
433 lapply(VDList, FUN=plotVD)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
434 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
435
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
436 print("Report Clonality - Heatmaps VJ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
437
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
438 plotVJ <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
439 if(length(dat[,1]) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
440 return()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
441 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
442 cat(paste(unique(dat[3])[1,1]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
443 img = ggplot() +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
444 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
445 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
446 scale_fill_gradient(low="gold", high="blue", na.value="white") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
447 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
448 xlab("J genes") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
449 ylab("V Genes")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
450
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
451 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
452 print(img)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
453 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
454 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
455 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
456
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
457 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
458
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
459 VandJCount$l = log(VandJCount$Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
460 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
461 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
462 VandJCount$relLength = VandJCount$l / VandJCount$max
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
463
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
464 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
465
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
466 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
467 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
468 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
469
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
470 fltr = is.nan(completeVJ$relLength)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
471 if(any(fltr)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
472 completeVJ[fltr,"relLength"] = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
473 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
474
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
475 VJList = split(completeVJ, f=completeVJ[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
476 lapply(VJList, FUN=plotVJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
477
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
478
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
479
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
480 if(useD){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
481 print("Report Clonality - Heatmaps DJ")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
482 plotDJ <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
483 if(length(dat[,1]) == 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
484 return()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
485 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
486 img = ggplot() +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
487 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
488 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
489 scale_fill_gradient(low="gold", high="blue", na.value="white") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
490 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
491 xlab("J genes") +
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
492 ylab("D Genes")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
493
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
494 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
495 print(img)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
496 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
497 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
498 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
499
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
500
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
501 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
502
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
503 DandJCount$l = log(DandJCount$Length)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
504 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
505 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
506 DandJCount$relLength = DandJCount$l / DandJCount$max
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
507
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
508 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
509
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
510 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
511 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
512 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
513
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
514 fltr = is.nan(completeDJ$relLength)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
515 if(any(fltr)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
516 completeDJ[fltr, "relLength"] = 1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
517 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
518
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
519 DJList = split(completeDJ, f=completeDJ[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
520 lapply(DJList, FUN=plotDJ)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
521 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
522
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
523
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
524 # ---------------------- output tables for the circos plots ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
525
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
526 print("Report Clonality - Circos data")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
527
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
528 for(smpl in unique(PRODF$Sample)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
529 PRODF.sample = PRODF[PRODF$Sample == smpl,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
530
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
531 fltr = PRODF.sample$Top.V.Gene == ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
532 if(any(fltr, na.rm=T)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
533 PRODF.sample[fltr, "Top.V.Gene"] = "NA"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
534 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
535
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
536 fltr = PRODF.sample$Top.D.Gene == ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
537 if(any(fltr, na.rm=T)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
538 PRODF.sample[fltr, "Top.D.Gene"] = "NA"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
539 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
540
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
541 fltr = PRODF.sample$Top.J.Gene == ""
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
542 if(any(fltr, na.rm=T)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
543 PRODF.sample[fltr, "Top.J.Gene"] = "NA"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
544 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
545
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
546 v.d = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.D.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
547 v.j = table(PRODF.sample$Top.V.Gene, PRODF.sample$Top.J.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
548 d.j = table(PRODF.sample$Top.D.Gene, PRODF.sample$Top.J.Gene)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
549
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
550 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
551 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
552 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
553 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
554
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
555 # ---------------------- calculating the clonality score ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
556
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
557 if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
558 {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
559 print("Report Clonality - Clonality")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
560 write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
561 if(clonality_method == "boyd"){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
562 samples = split(clonalityFrame, clonalityFrame$Sample, drop=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
563
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
564 for (sample in samples){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
565 res = data.frame(paste=character(0))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
566 sample_id = unique(sample$Sample)[[1]]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
567 for(replicate in unique(sample$Replicate)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
568 tmp = sample[sample$Replicate == replicate,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
569 clone_table = data.frame(table(tmp$clonaltype))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
570 clone_col_name = paste("V", replicate, sep="")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
571 colnames(clone_table) = c("paste", clone_col_name)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
572 res = merge(res, clone_table, by="paste", all=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
573 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
574
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
575 res[is.na(res)] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
576 infer.result = infer.clonality(as.matrix(res[,2:ncol(res)]))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
577
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
578 print(infer.result)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
579
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
580 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
581
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
582 res$type = rowSums(res[,2:ncol(res)])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
583
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
584 coincidence.table = data.frame(table(res$type))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
585 colnames(coincidence.table) = c("Coincidence Type", "Raw Coincidence Freq")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
586 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
587 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
588 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
589 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
590
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
591 #write files for every coincidence group of >1
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
592 samples = unique(clonalFreq$Sample)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
593 for(sample in samples){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
594 clonalFreqSample = clonalFreq[clonalFreq$Sample == sample,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
595 if(max(clonalFreqSample$Type) > 1){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
596 for(i in 2:max(clonalFreqSample$Type)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
597 clonalFreqSampleType = clonalFreqSample[clonalFreqSample$Type == i,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
598 clonalityFrame.sub = clonalityFrame[clonalityFrame$clonaltype %in% clonalFreqSampleType$clonaltype,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
599 clonalityFrame.sub = clonalityFrame.sub[order(clonalityFrame.sub$clonaltype),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
600 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
601 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
602 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
603 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
604
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
605 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
606 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
607 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
608 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
609
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
610 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
611 tcct = textConnection(ct)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
612 CT = read.table(tcct, sep="\t", header=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
613 close(tcct)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
614 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
615 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
616
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
617 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
618 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
619 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
620 ReplicateReads$Reads = as.numeric(ReplicateReads$Reads)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
621 ReplicateReads$squared = as.numeric(ReplicateReads$Reads * ReplicateReads$Reads)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
622
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
623 ReplicatePrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
624 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
625 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
626
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
627 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
628 lapply(ReplicateSplit, FUN=ReplicatePrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
629
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
630 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
631 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
632
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
633 ReplicateSumPrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
634 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
635 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
636
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
637 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
638 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
639
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
640 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
641 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
642 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
643 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
644 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
645
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
646 ClonalityScorePrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
647 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
648 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
649
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
650 clonalityScore = clonalFreqCount[c("Sample", "Result")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
651 clonalityScore = unique(clonalityScore)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
652
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
653 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
654 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
655
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
656 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
657
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
658
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
659
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
660 ClonalityOverviewPrint <- function(dat){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
661 dat = dat[order(dat[,2]),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
662 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
663 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
664
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
665 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
666 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
667 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
668 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
669
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
670 bak = PRODF
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
671
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
672 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
673 if(all(imgtcolumns %in% colnames(inputdata)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
674 {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
675 print("found IMGT columns, running junction analysis")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
676
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
677 if(locus %in% c("IGK","IGL", "TRA", "TRG")){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
678 print("VJ recombination, no filtering on absent D")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
679 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
680 print("VDJ recombination, using N column for junction analysis")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
681 fltr = nchar(PRODF$Top.D.Gene) < 4
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
682 print(paste("Removing", sum(fltr), "sequences without a identified D"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
683 PRODF = PRODF[!fltr,]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
684 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
685
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
686
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
687 #ensure certain columns are in the data (files generated with older versions of IMGT Loader)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
688 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
689 for(col.check in col.checks){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
690 if(!(col.check %in% names(PRODF))){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
691 print(paste(col.check, "not found adding new column"))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
692 if(nrow(PRODF) > 0){ #because R is anoying...
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
693 PRODF[,col.check] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
694 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
695 PRODF = cbind(PRODF, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
696 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
697 if(nrow(UNPROD) > 0){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
698 UNPROD[,col.check] = 0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
699 } else {
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
700 UNPROD = cbind(UNPROD, data.frame(N3.REGION.nt.nb=numeric(0), N4.REGION.nt.nb=numeric(0)))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
701 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
702 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
703 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
704
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
705 num_median = function(x, na.rm=T) { as.numeric(median(x, na.rm=na.rm)) }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
706
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
707 newData = data.frame(data.table(PRODF)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
708 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
709 P1=mean(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
710 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
711 P2=mean(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
712 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
713 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
714 P3=mean(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
715 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
716 P4=mean(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
717 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
718 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
719 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)),
3
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
720 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
721 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
722 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
723 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
724 write.table(newData, "junctionAnalysisProd_mean.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
725
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
726 newData = data.frame(data.table(PRODF)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
727 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
728 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
729 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
730 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
731 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
732 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
733 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
734 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
735 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
736 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
737 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
738 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)),
3
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
739 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
740 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
741 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
742 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
743 write.table(newData, "junctionAnalysisProd_median.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
744
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
745 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
746 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
747 P1=mean(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
748 N1=mean(rowSums(.SD[,c("N.REGION.nt.nb", "N1.REGION.nt.nb"), with=F], na.rm=T)),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
749 P2=mean(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
750 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
751 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
752 P3=mean(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
753 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
754 P4=mean(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
755 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
756 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
757 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)),
3
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
758 Total.P=mean(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
759 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
760 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
761 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
762 write.table(newData, "junctionAnalysisUnProd_mean.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
763
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
764 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
765 VH.DEL=num_median(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
766 P1=num_median(.SD$P3V.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
767 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
768 P2=num_median(.SD$P5D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
769 DEL.DH=num_median(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
770 DH.DEL=num_median(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
771 P3=num_median(.SD$P3D.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
772 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
773 P4=num_median(.SD$P5J.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
774 DEL.JH=num_median(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
775 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
776 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)),
3
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
777 Total.P=num_median(rowSums(.SD[,c("P3V.nt.nb", "P5D.nt.nb", "P3D.nt.nb", "P5J.nt.nb"), with=F], na.rm=T)),
beaa487ecf43 Uploaded
davidvanzessen
parents: 1
diff changeset
778 Median.CDR3.l=median(.SD$CDR3.Length.DNA)),
0
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
779 by=c("Sample")])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
780
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
781 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
782 write.table(newData, "junctionAnalysisUnProd_median.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
783 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
784
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
785 PRODF = bak
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
786
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
787
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
788 # ---------------------- D reading frame ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
789
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
790 D.REGION.reading.frame = PRODF$D.REGION.reading.frame
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
791
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
792 D.REGION.reading.frame[is.na(D.REGION.reading.frame)] = "No D"
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
793
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
794 D.REGION.reading.frame = data.frame(table(D.REGION.reading.frame))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
795
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
796 write.table(D.REGION.reading.frame, "DReadingFrame.csv" , sep="\t",quote=F,row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
797
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
798 D.REGION.reading.frame = ggplot(D.REGION.reading.frame)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
799 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
800
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
801 png("DReadingFrame.png")
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
802 D.REGION.reading.frame
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
803 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
804
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
805
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
806
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
807
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
808 # ---------------------- AA composition in CDR3 ----------------------
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
809
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
810 AACDR3 = PRODF[,c("Sample", "CDR3.Seq")]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
811
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
812 TotalPerSample = data.frame(data.table(AACDR3)[, list(total=sum(nchar(as.character(.SD$CDR3.Seq)))), by=Sample])
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
813
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
814 AAfreq = list()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
815
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
816 for(i in 1:nrow(TotalPerSample)){
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
817 sample = TotalPerSample$Sample[i]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
818 AAfreq[[i]] = data.frame(table(unlist(strsplit(as.character(AACDR3[AACDR3$Sample == sample,c("CDR3.Seq")]), ""))))
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
819 AAfreq[[i]]$Sample = sample
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
820 }
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
821
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
822 AAfreq = ldply(AAfreq, data.frame)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
823 AAfreq = merge(AAfreq, TotalPerSample, by="Sample", all.x = T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
824 AAfreq$freq_perc = as.numeric(AAfreq$Freq / AAfreq$total * 100)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
825
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
826
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
827 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
828 AAfreq = merge(AAfreq, AAorder, by.x='Var1', by.y='AA', all.x=TRUE)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
829
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
830 AAfreq = AAfreq[!is.na(AAfreq$order.aa),]
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
831
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
832 AAfreqplot = ggplot(AAfreq)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
833 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
834 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
835 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
836 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
837 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
838 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
839
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
840 png("AAComposition.png",width = 1280, height = 720)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
841 AAfreqplot
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
842 dev.off()
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
843 write.table(AAfreq, "AAComposition.csv" , sep=",",quote=F,na="-",row.names=F,col.names=T)
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
844
afe85eb6572e Uploaded
davidvanzessen
parents:
diff changeset
845