annotate report_clonality/RScript.r @ 5:bcec7bb4e089 draft

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