annotate report_clonality/RScript.r~ @ 26:28fbbdfd7a87 draft

Uploaded
author davidvanzessen
date Mon, 13 Feb 2017 09:08:46 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
1 # ---------------------- load/install packages ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
2
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
3 if (!("gridExtra" %in% rownames(installed.packages()))) {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
4 install.packages("gridExtra", repos="http://cran.xl-mirror.nl/")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
5 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
6 library(gridExtra)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
7 if (!("ggplot2" %in% rownames(installed.packages()))) {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
8 install.packages("ggplot2", repos="http://cran.xl-mirror.nl/")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
9 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
10 library(ggplot2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
11 if (!("plyr" %in% rownames(installed.packages()))) {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
12 install.packages("plyr", repos="http://cran.xl-mirror.nl/")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
13 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
14 library(plyr)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
15
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
16 if (!("data.table" %in% rownames(installed.packages()))) {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
17 install.packages("data.table", repos="http://cran.xl-mirror.nl/")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
18 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
19 library(data.table)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
20
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
21 if (!("reshape2" %in% rownames(installed.packages()))) {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
22 install.packages("reshape2", repos="http://cran.xl-mirror.nl/")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
23 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
24 library(reshape2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
25
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
26 if (!("lymphclon" %in% rownames(installed.packages()))) {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
27 install.packages("lymphclon", repos="http://cran.xl-mirror.nl/")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
28 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
29 library(lymphclon)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
30
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
31 # ---------------------- parameters ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
32
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
33 args <- commandArgs(trailingOnly = TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
34
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
35 infile = args[1] #path to input file
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
36 outfile = args[2] #path to output file
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
37 outdir = args[3] #path to output folder (html/images/data)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
38 clonaltype = args[4] #clonaltype definition, or 'none' for no unique filtering
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
39 ct = unlist(strsplit(clonaltype, ","))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
40 species = args[5] #human or mouse
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
41 locus = args[6] # IGH, IGK, IGL, TRB, TRA, TRG or TRD
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
42 filterproductive = ifelse(args[7] == "yes", T, F) #should unproductive sequences be filtered out? (yes/no)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
43 clonality_method = args[8]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
44
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
45 # ---------------------- Data preperation ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
46
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
47 inputdata = read.table(infile, sep="\t", header=TRUE, fill=T, comment.char="")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
48
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
49 setwd(outdir)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
50
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
51 # remove weird rows
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
52 inputdata = inputdata[inputdata$Sample != "",]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
53
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
54 #remove the allele from the V,D and J genes
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
55 inputdata$Top.V.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.V.Gene)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
56 inputdata$Top.D.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.D.Gene)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
57 inputdata$Top.J.Gene = gsub("[*]([0-9]+)", "", inputdata$Top.J.Gene)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
58
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
59 inputdata$clonaltype = 1:nrow(inputdata)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
60
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
61 PRODF = inputdata
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
62 UNPROD = inputdata
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
63 if(filterproductive){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
64 if("Functionality" %in% colnames(inputdata)) { # "Functionality" is an IMGT column
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
65 PRODF = inputdata[inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)", ]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
66 UNPROD = inputdata[!(inputdata$Functionality == "productive" | inputdata$Functionality == "productive (see comment)"), ]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
67 } else {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
68 PRODF = inputdata[inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" , ]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
69 UNPROD = inputdata[!(inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND" ), ]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
70 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
71 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
72
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
73 clonalityFrame = PRODF
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
74
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
75 #remove duplicates based on the clonaltype
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
76 if(clonaltype != "none"){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
77 clonaltype = paste(clonaltype, ",Sample", sep="") #add sample column to clonaltype, unique within samples
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
78 PRODF$clonaltype = do.call(paste, c(PRODF[unlist(strsplit(clonaltype, ","))], sep = ":"))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
79 PRODF = PRODF[!duplicated(PRODF$clonaltype), ]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
80
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
81 UNPROD$clonaltype = do.call(paste, c(UNPROD[unlist(strsplit(clonaltype, ","))], sep = ":"))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
82 UNPROD = UNPROD[!duplicated(UNPROD$clonaltype), ]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
83
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
84 #again for clonalityFrame but with sample+replicate
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
85 clonalityFrame$clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(clonaltype, ","))], sep = ":"))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
86 clonalityFrame$clonality_clonaltype = do.call(paste, c(clonalityFrame[unlist(strsplit(paste(clonaltype, ",Replicate", sep=""), ","))], sep = ":"))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
87 clonalityFrame = clonalityFrame[!duplicated(clonalityFrame$clonality_clonaltype), ]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
88 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
89
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
90 PRODF$freq = 1
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
91
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
92 if(any(grepl(pattern="_", x=PRODF$ID))){ #the frequency can be stored in the ID with the pattern ".*_freq_.*"
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
93 PRODF$freq = gsub("^[0-9]+_", "", PRODF$ID)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
94 PRODF$freq = gsub("_.*", "", PRODF$freq)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
95 PRODF$freq = as.numeric(PRODF$freq)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
96 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
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
97 PRODF$freq = 1
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
98 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
99 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
100
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
101
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
102
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
103 #write the complete dataset that is left over, will be the input if 'none' for clonaltype and 'no' for filterproductive
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
104 write.table(PRODF, "allUnique.txt", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
105 write.table(PRODF, "allUnique.csv", sep="\t",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
106 write.table(UNPROD, "allUnproductive.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
107
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
108 #write the samples to a file
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
109 sampleFile <- file("samples.txt")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
110 un = unique(inputdata$Sample)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
111 un = paste(un, sep="\n")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
112 writeLines(un, sampleFile)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
113 close(sampleFile)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
114
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
115 # ---------------------- Counting the productive/unproductive and unique sequences ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
116
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
117 if(!("Functionality" %in% inputdata)){ #add a functionality column to the igblast data
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
118 inputdata$Functionality = "unproductive"
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
119 search = (inputdata$VDJ.Frame != "In-frame with stop codon" & inputdata$VDJ.Frame != "Out-of-frame" & inputdata$CDR3.Found.How != "NOT_FOUND")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
120 if(sum(search) > 0){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
121 inputdata[search,]$Functionality = "productive"
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
122 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
123 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
124
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
125 inputdata.dt = data.table(inputdata) #for speed
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
126
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
127 if(clonaltype == "none"){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
128 ct = c("clonaltype")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
129 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
130
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
131 inputdata.dt$samples_replicates = paste(inputdata.dt$Sample, inputdata.dt$Replicate, sep="_")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
132 samples_replicates = c(unique(inputdata.dt$samples_replicates), unique(as.character(inputdata.dt$Sample)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
133 frequency_table = data.frame(ID = samples_replicates[order(samples_replicates)])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
134
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
135
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
136 sample_productive_count = inputdata.dt[, list(All=.N,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
137 Productive = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
138 perc_prod = 1,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
139 Productive_unique = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",list(count=.N),by=ct]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
140 perc_prod_un = 1,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
141 Unproductive= nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
142 perc_unprod = 1,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
143 Unproductive_unique =nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",list(count=.N),by=ct]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
144 perc_unprod_un = 1),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
145 by=c("Sample")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
146
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
147 sample_productive_count$perc_prod = round(sample_productive_count$Productive / sample_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
148 sample_productive_count$perc_prod_un = round(sample_productive_count$Productive_unique / sample_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
149
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
150 sample_productive_count$perc_unprod = round(sample_productive_count$Unproductive / sample_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
151 sample_productive_count$perc_unprod_un = round(sample_productive_count$Unproductive_unique / sample_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
152
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
153
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
154 sample_replicate_productive_count = inputdata.dt[, list(All=.N,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
155 Productive = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
156 perc_prod = 1,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
157 Productive_unique = nrow(.SD[.SD$Functionality == "productive" | .SD$Functionality == "productive (see comment)",list(count=.N),by=ct]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
158 perc_prod_un = 1,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
159 Unproductive= nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
160 perc_unprod = 1,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
161 Unproductive_unique =nrow(.SD[.SD$Functionality != "productive" & .SD$Functionality != "productive (see comment)",list(count=.N),by=ct]),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
162 perc_unprod_un = 1),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
163 by=c("samples_replicates")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
164
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
165 sample_replicate_productive_count$perc_prod = round(sample_replicate_productive_count$Productive / sample_replicate_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
166 sample_replicate_productive_count$perc_prod_un = round(sample_replicate_productive_count$Productive_unique / sample_replicate_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
167
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
168 sample_replicate_productive_count$perc_unprod = round(sample_replicate_productive_count$Unproductive / sample_replicate_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
169 sample_replicate_productive_count$perc_unprod_un = round(sample_replicate_productive_count$Unproductive_unique / sample_replicate_productive_count$All * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
170
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
171 setnames(sample_replicate_productive_count, colnames(sample_productive_count))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
172
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
173 counts = rbind(sample_replicate_productive_count, sample_productive_count)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
174 counts = counts[order(counts$Sample),]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
175
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
176 write.table(x=counts, file="productive_counting.txt", sep=",",quote=F,row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
177
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
178 # ---------------------- Frequency calculation for V, D and J ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
179
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
180 PRODFV = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.V.Gene")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
181 Total = ddply(PRODFV, .(Sample), function(x) data.frame(Total = sum(x$Length)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
182 PRODFV = merge(PRODFV, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
183 PRODFV = ddply(PRODFV, c("Sample", "Top.V.Gene"), summarise, relFreq= (Length*100 / Total))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
184
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
185 PRODFD = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.D.Gene")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
186 Total = ddply(PRODFD, .(Sample), function(x) data.frame(Total = sum(x$Length)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
187 PRODFD = merge(PRODFD, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
188 PRODFD = ddply(PRODFD, c("Sample", "Top.D.Gene"), summarise, relFreq= (Length*100 / Total))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
189
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
190 PRODFJ = data.frame(data.table(PRODF)[, list(Length=sum(freq)), by=c("Sample", "Top.J.Gene")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
191 Total = ddply(PRODFJ, .(Sample), function(x) data.frame(Total = sum(x$Length)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
192 PRODFJ = merge(PRODFJ, Total, by.x='Sample', by.y='Sample', all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
193 PRODFJ = ddply(PRODFJ, c("Sample", "Top.J.Gene"), summarise, relFreq= (Length*100 / Total))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
194
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
195 # ---------------------- Setting up the gene names for the different species/loci ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
196
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
197 Vchain = ""
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
198 Dchain = ""
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
199 Jchain = ""
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
200
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
201 if(species == "custom"){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
202 print("Custom genes: ")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
203 splt = unlist(strsplit(locus, ";"))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
204 print(paste("V:", splt[1]))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
205 print(paste("D:", splt[2]))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
206 print(paste("J:", splt[3]))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
207
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
208 Vchain = unlist(strsplit(splt[1], ","))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
209 Vchain = data.frame(v.name = Vchain, chr.orderV = 1:length(Vchain))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
210
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
211 Dchain = unlist(strsplit(splt[2], ","))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
212 if(length(Dchain) > 0){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
213 Dchain = data.frame(v.name = Dchain, chr.orderD = 1:length(Dchain))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
214 } else {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
215 Dchain = data.frame(v.name = character(0), chr.orderD = numeric(0))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
216 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
217
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
218 Jchain = unlist(strsplit(splt[3], ","))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
219 Jchain = data.frame(v.name = Jchain, chr.orderJ = 1:length(Jchain))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
220
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
221 } else {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
222 genes = read.table("genes.txt", sep="\t", header=TRUE, fill=T, comment.char="")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
223
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
224 Vchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "V",c("IMGT.GENE.DB", "chr.order")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
225 colnames(Vchain) = c("v.name", "chr.orderV")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
226 Dchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "D",c("IMGT.GENE.DB", "chr.order")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
227 colnames(Dchain) = c("v.name", "chr.orderD")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
228 Jchain = genes[grepl(species, genes$Species) & genes$locus == locus & genes$region == "J",c("IMGT.GENE.DB", "chr.order")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
229 colnames(Jchain) = c("v.name", "chr.orderJ")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
230 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
231 useD = TRUE
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
232 if(nrow(Dchain) == 0){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
233 useD = FALSE
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
234 cat("No D Genes in this species/locus")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
235 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
236 print(paste("useD:", useD))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
237
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
238 # ---------------------- merge with the frequency count ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
239
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
240 PRODFV = merge(PRODFV, Vchain, by.x='Top.V.Gene', by.y='v.name', all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
241
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
242 PRODFD = merge(PRODFD, Dchain, by.x='Top.D.Gene', by.y='v.name', all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
243
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
244 PRODFJ = merge(PRODFJ, Jchain, by.x='Top.J.Gene', by.y='v.name', all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
245
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
246 # ---------------------- Create the V, D and J frequency plots and write the data.frame for every plot to a file ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
247
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
248 pV = ggplot(PRODFV)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
249 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))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
250 pV = pV + xlab("Summary of V gene") + ylab("Frequency") + ggtitle("Relative frequency of V gene usage")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
251 write.table(x=PRODFV, file="VFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
252
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
253 png("VPlot.png",width = 1280, height = 720)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
254 pV
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
255 dev.off();
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
256
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
257 if(useD){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
258 pD = ggplot(PRODFD)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
259 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))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
260 pD = pD + xlab("Summary of D gene") + ylab("Frequency") + ggtitle("Relative frequency of D gene usage")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
261 write.table(x=PRODFD, file="DFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
262
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
263 png("DPlot.png",width = 800, height = 600)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
264 print(pD)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
265 dev.off();
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
266 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
267
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
268 pJ = ggplot(PRODFJ)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
269 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))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
270 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
271 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
272
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
273 png("JPlot.png",width = 800, height = 600)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
274 pJ
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
275 dev.off();
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
276
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
277 pJ = ggplot(PRODFJ)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
278 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))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
279 pJ = pJ + xlab("Summary of J gene") + ylab("Frequency") + ggtitle("Relative frequency of J gene usage")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
280 write.table(x=PRODFJ, file="JFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
281
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
282 png("JPlot.png",width = 800, height = 600)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
283 pJ
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
284 dev.off();
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
285
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
286 # ---------------------- Now the frequency plots of the V, D and J families ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
287
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
288 VGenes = PRODF[,c("Sample", "Top.V.Gene")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
289 VGenes$Top.V.Gene = gsub("-.*", "", VGenes$Top.V.Gene)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
290 VGenes = data.frame(data.table(VGenes)[, list(Count=.N), by=c("Sample", "Top.V.Gene")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
291 TotalPerSample = data.frame(data.table(VGenes)[, list(total=sum(.SD$Count)), by=Sample])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
292 VGenes = merge(VGenes, TotalPerSample, by="Sample")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
293 VGenes$Frequency = VGenes$Count * 100 / VGenes$total
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
294 VPlot = ggplot(VGenes)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
295 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)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
296 ggtitle("Distribution of V gene families") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
297 ylab("Percentage of sequences")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
298 png("VFPlot.png")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
299 VPlot
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
300 dev.off();
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
301 write.table(x=VGenes, file="VFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
302
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
303 if(useD){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
304 DGenes = PRODF[,c("Sample", "Top.D.Gene")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
305 DGenes$Top.D.Gene = gsub("-.*", "", DGenes$Top.D.Gene)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
306 DGenes = data.frame(data.table(DGenes)[, list(Count=.N), by=c("Sample", "Top.D.Gene")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
307 TotalPerSample = data.frame(data.table(DGenes)[, list(total=sum(.SD$Count)), by=Sample])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
308 DGenes = merge(DGenes, TotalPerSample, by="Sample")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
309 DGenes$Frequency = DGenes$Count * 100 / DGenes$total
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
310 DPlot = ggplot(DGenes)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
311 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)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
312 ggtitle("Distribution of D gene families") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
313 ylab("Percentage of sequences")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
314 png("DFPlot.png")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
315 print(DPlot)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
316 dev.off();
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
317 write.table(x=DGenes, file="DFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
318 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
319
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
320 JGenes = PRODF[,c("Sample", "Top.J.Gene")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
321 JGenes$Top.J.Gene = gsub("-.*", "", JGenes$Top.J.Gene)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
322 JGenes = data.frame(data.table(JGenes)[, list(Count=.N), by=c("Sample", "Top.J.Gene")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
323 TotalPerSample = data.frame(data.table(JGenes)[, list(total=sum(.SD$Count)), by=Sample])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
324 JGenes = merge(JGenes, TotalPerSample, by="Sample")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
325 JGenes$Frequency = JGenes$Count * 100 / JGenes$total
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
326 JPlot = ggplot(JGenes)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
327 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)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
328 ggtitle("Distribution of J gene families") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
329 ylab("Percentage of sequences")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
330 png("JFPlot.png")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
331 JPlot
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
332 dev.off();
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
333 write.table(x=JGenes, file="JFFrequency.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
334
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
335 # ---------------------- Plotting the cdr3 length ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
336
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
337 CDR3Length = data.frame(data.table(PRODF)[, list(Count=.N), by=c("Sample", "CDR3.Length.DNA")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
338 TotalPerSample = data.frame(data.table(CDR3Length)[, list(total=sum(.SD$Count)), by=Sample])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
339 CDR3Length = merge(CDR3Length, TotalPerSample, by="Sample")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
340 CDR3Length$Frequency = CDR3Length$Count * 100 / CDR3Length$total
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
341 CDR3LengthPlot = ggplot(CDR3Length)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
342 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)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
343 ggtitle("Length distribution of CDR3") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
344 xlab("CDR3 Length") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
345 ylab("Percentage of sequences")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
346 png("CDR3LengthPlot.png",width = 1280, height = 720)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
347 CDR3LengthPlot
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
348 dev.off()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
349 write.table(x=CDR3Length, file="CDR3LengthPlot.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
350
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
351 # ---------------------- Plot the heatmaps ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
352
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
353
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
354 #get the reverse order for the V and D genes
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
355 revVchain = Vchain
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
356 revDchain = Dchain
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
357 revVchain$chr.orderV = rev(revVchain$chr.orderV)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
358 revDchain$chr.orderD = rev(revDchain$chr.orderD)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
359
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
360 if(useD){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
361 plotVD <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
362 if(length(dat[,1]) == 0){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
363 return()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
364 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
365 img = ggplot() +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
366 geom_tile(data=dat, aes(x=factor(reorder(Top.D.Gene, chr.orderD)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
367 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
368 scale_fill_gradient(low="gold", high="blue", na.value="white") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
369 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
370 xlab("D genes") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
371 ylab("V Genes")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
372
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
373 png(paste("HeatmapVD_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Dchain$v.name)), height=100+(15*length(Vchain$v.name)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
374 print(img)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
375 dev.off()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
376 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)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
377 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
378
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
379 VandDCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.D.Gene", "Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
380
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
381 VandDCount$l = log(VandDCount$Length)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
382 maxVD = data.frame(data.table(VandDCount)[, list(max=max(l)), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
383 VandDCount = merge(VandDCount, maxVD, by.x="Sample", by.y="Sample", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
384 VandDCount$relLength = VandDCount$l / VandDCount$max
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
385
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
386 cartegianProductVD = expand.grid(Top.V.Gene = Vchain$v.name, Top.D.Gene = Dchain$v.name, Sample = unique(inputdata$Sample))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
387
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
388 completeVD = merge(VandDCount, cartegianProductVD, all.y=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
389 completeVD = merge(completeVD, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
390 completeVD = merge(completeVD, Dchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
391 VDList = split(completeVD, f=completeVD[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
392
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
393 lapply(VDList, FUN=plotVD)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
394 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
395
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
396 plotVJ <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
397 if(length(dat[,1]) == 0){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
398 return()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
399 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
400 cat(paste(unique(dat[3])[1,1]))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
401 img = ggplot() +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
402 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.V.Gene, chr.orderV)), fill=relLength)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
403 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
404 scale_fill_gradient(low="gold", high="blue", na.value="white") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
405 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
406 xlab("J genes") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
407 ylab("V Genes")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
408
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
409 png(paste("HeatmapVJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Vchain$v.name)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
410 print(img)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
411 dev.off()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
412 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)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
413 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
414
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
415 VandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.V.Gene", "Top.J.Gene", "Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
416
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
417 VandJCount$l = log(VandJCount$Length)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
418 maxVJ = data.frame(data.table(VandJCount)[, list(max=max(l)), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
419 VandJCount = merge(VandJCount, maxVJ, by.x="Sample", by.y="Sample", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
420 VandJCount$relLength = VandJCount$l / VandJCount$max
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
421
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
422 cartegianProductVJ = expand.grid(Top.V.Gene = Vchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(inputdata$Sample))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
423
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
424 completeVJ = merge(VandJCount, cartegianProductVJ, all.y=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
425 completeVJ = merge(completeVJ, revVchain, by.x="Top.V.Gene", by.y="v.name", all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
426 completeVJ = merge(completeVJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
427 VJList = split(completeVJ, f=completeVJ[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
428 lapply(VJList, FUN=plotVJ)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
429
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
430 if(useD){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
431 plotDJ <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
432 if(length(dat[,1]) == 0){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
433 return()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
434 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
435 img = ggplot() +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
436 geom_tile(data=dat, aes(x=factor(reorder(Top.J.Gene, chr.orderJ)), y=factor(reorder(Top.D.Gene, chr.orderD)), fill=relLength)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
437 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
438 scale_fill_gradient(low="gold", high="blue", na.value="white") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
439 ggtitle(paste(unique(dat$Sample), " (N=" , sum(dat$Length, na.rm=T) ,")", sep="")) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
440 xlab("J genes") +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
441 ylab("D Genes")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
442
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
443 png(paste("HeatmapDJ_", unique(dat[3])[1,1] , ".png", sep=""), width=150+(15*length(Jchain$v.name)), height=100+(15*length(Dchain$v.name)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
444 print(img)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
445 dev.off()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
446 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)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
447 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
448
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
449
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
450 DandJCount = data.frame(data.table(PRODF)[, list(Length=.N), by=c("Top.D.Gene", "Top.J.Gene", "Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
451
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
452 DandJCount$l = log(DandJCount$Length)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
453 maxDJ = data.frame(data.table(DandJCount)[, list(max=max(l)), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
454 DandJCount = merge(DandJCount, maxDJ, by.x="Sample", by.y="Sample", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
455 DandJCount$relLength = DandJCount$l / DandJCount$max
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
456
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
457 cartegianProductDJ = expand.grid(Top.D.Gene = Dchain$v.name, Top.J.Gene = Jchain$v.name, Sample = unique(inputdata$Sample))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
458
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
459 completeDJ = merge(DandJCount, cartegianProductDJ, all.y=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
460 completeDJ = merge(completeDJ, revDchain, by.x="Top.D.Gene", by.y="v.name", all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
461 completeDJ = merge(completeDJ, Jchain, by.x="Top.J.Gene", by.y="v.name", all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
462 DJList = split(completeDJ, f=completeDJ[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
463 lapply(DJList, FUN=plotDJ)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
464 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
465
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
466
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
467 # ---------------------- calculating the clonality score ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
468
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
469 if("Replicate" %in% colnames(inputdata)) #can only calculate clonality score when replicate information is available
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
470 {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
471 if(clonality_method == "boyd"){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
472 samples = split(clonalityFrame, clonalityFrame$Sample, drop=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
473
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
474 for (sample in samples){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
475 res = data.frame(paste=character(0))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
476 sample_id = unique(sample$Sample)[[1]]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
477 for(replicate in unique(sample$Replicate)){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
478 tmp = sample[sample$Replicate == replicate,]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
479 clone_table = data.frame(table(tmp$clonaltype))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
480 clone_col_name = paste("V", replicate, sep="")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
481 colnames(clone_table) = c("paste", clone_col_name)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
482 res = merge(res, clone_table, by="paste", all=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
483 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
484
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
485 res[is.na(res)] = 0
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
486 infer.result = infer.clonality(as.matrix(res[,2:ncol(res)]))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
487
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
488 write.table(data.table(infer.result[[12]]), file=paste("lymphclon_clonality_", sample_id, ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
489
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
490 res$type = rowSums(res[,2:ncol(res)])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
491
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
492 coincidence.table = data.frame(table(res$type))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
493 colnames(coincidence.table) = c("Coincidence Type", "Raw Coincidence Freq")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
494 write.table(coincidence.table, file=paste("lymphclon_coincidences_", sample_id, ".csv", sep=""), sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
495 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
496 } else {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
497 write.table(clonalityFrame, "clonalityComplete.csv", sep=",",quote=F,row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
498
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
499 clonalFreq = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "clonaltype")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
500 clonalFreqCount = data.frame(data.table(clonalFreq)[, list(Count=.N), by=c("Sample", "Type")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
501 clonalFreqCount$realCount = clonalFreqCount$Type * clonalFreqCount$Count
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
502 clonalSum = data.frame(data.table(clonalFreqCount)[, list(Reads=sum(realCount)), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
503 clonalFreqCount = merge(clonalFreqCount, clonalSum, by.x="Sample", by.y="Sample")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
504
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
505 ct = c('Type\tWeight\n2\t1\n3\t3\n4\t6\n5\t10\n6\t15')
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
506 tcct = textConnection(ct)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
507 CT = read.table(tcct, sep="\t", header=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
508 close(tcct)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
509 clonalFreqCount = merge(clonalFreqCount, CT, by.x="Type", by.y="Type", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
510 clonalFreqCount$WeightedCount = clonalFreqCount$Count * clonalFreqCount$Weight
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
511
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
512 ReplicateReads = data.frame(data.table(clonalityFrame)[, list(Type=.N), by=c("Sample", "Replicate", "clonaltype")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
513 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(Reads=.N), by=c("Sample", "Replicate")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
514 clonalFreqCount$Reads = as.numeric(clonalFreqCount$Reads)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
515 ReplicateReads$squared = ReplicateReads$Reads * ReplicateReads$Reads
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
516
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
517 ReplicatePrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
518 write.table(dat[-1], paste("ReplicateReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
519 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
520
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
521 ReplicateSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
522 lapply(ReplicateSplit, FUN=ReplicatePrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
523
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
524 ReplicateReads = data.frame(data.table(ReplicateReads)[, list(ReadsSum=sum(as.numeric(Reads)), ReadsSquaredSum=sum(as.numeric(squared))), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
525 clonalFreqCount = merge(clonalFreqCount, ReplicateReads, by.x="Sample", by.y="Sample", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
526
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
527 ReplicateSumPrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
528 write.table(dat[-1], paste("ReplicateSumReads_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
529 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
530
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
531 ReplicateSumSplit = split(ReplicateReads, f=ReplicateReads[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
532 lapply(ReplicateSumSplit, FUN=ReplicateSumPrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
533
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
534 clonalFreqCountSum = data.frame(data.table(clonalFreqCount)[, list(Numerator=sum(WeightedCount, na.rm=T)), by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
535 clonalFreqCount = merge(clonalFreqCount, clonalFreqCountSum, by.x="Sample", by.y="Sample", all.x=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
536 clonalFreqCount$ReadsSum = as.numeric(clonalFreqCount$ReadsSum) #prevent integer overflow
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
537 clonalFreqCount$Denominator = (((clonalFreqCount$ReadsSum * clonalFreqCount$ReadsSum) - clonalFreqCount$ReadsSquaredSum) / 2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
538 clonalFreqCount$Result = (clonalFreqCount$Numerator + 1) / (clonalFreqCount$Denominator + 1)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
539
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
540 ClonalityScorePrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
541 write.table(dat$Result, paste("ClonalityScore_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
542 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
543
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
544 clonalityScore = clonalFreqCount[c("Sample", "Result")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
545 clonalityScore = unique(clonalityScore)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
546
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
547 clonalityScoreSplit = split(clonalityScore, f=clonalityScore[,"Sample"])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
548 lapply(clonalityScoreSplit, FUN=ClonalityScorePrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
549
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
550 clonalityOverview = clonalFreqCount[c("Sample", "Type", "Count", "Weight", "WeightedCount")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
551
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
552
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
553
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
554 ClonalityOverviewPrint <- function(dat){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
555 write.table(dat[-1], paste("ClonalityOverView_", unique(dat[1])[1,1] , ".csv", sep=""), sep=",",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
556 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
557
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
558 clonalityOverviewSplit = split(clonalityOverview, f=clonalityOverview$Sample)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
559 lapply(clonalityOverviewSplit, FUN=ClonalityOverviewPrint)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
560 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
561 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
562
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
563 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")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
564 if(all(imgtcolumns %in% colnames(inputdata)))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
565 {
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
566 print("found IMGT columns, running junction analysis")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
567 newData = data.frame(data.table(PRODF)[,list(unique=.N,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
568 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
569 P1=mean(.SD$P3V.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
570 N1=mean(.SD$N1.REGION.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
571 P2=mean(.SD$P5D.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
572 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
573 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
574 P3=mean(.SD$P3D.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
575 N2=mean(.SD$N2.REGION.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
576 P4=mean(.SD$P5J.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
577 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
578 Total.Del=( mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
579 mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
580 mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
581 mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T)),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
582
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
583 Total.N=( mean(.SD$N1.REGION.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
584 mean(.SD$N2.REGION.nt.nb, na.rm=T)),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
585
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
586 Total.P=( mean(.SD$P3V.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
587 mean(.SD$P5D.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
588 mean(.SD$P3D.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
589 mean(.SD$P5J.nt.nb, na.rm=T))),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
590 by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
591 print(newData)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
592 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
593 write.table(newData, "junctionAnalysisProd.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
594
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
595 newData = data.frame(data.table(UNPROD)[,list(unique=.N,
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
596 VH.DEL=mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
597 P1=mean(.SD$P3V.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
598 N1=mean(.SD$N1.REGION.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
599 P2=mean(.SD$P5D.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
600 DEL.DH=mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
601 DH.DEL=mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
602 P3=mean(.SD$P3D.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
603 N2=mean(.SD$N2.REGION.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
604 P4=mean(.SD$P5J.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
605 DEL.JH=mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
606 Total.Del=(mean(.SD$X3V.REGION.trimmed.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
607 mean(.SD$X5D.REGION.trimmed.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
608 mean(.SD$X3D.REGION.trimmed.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
609 mean(.SD$X5J.REGION.trimmed.nt.nb, na.rm=T)),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
610 Total.N=( mean(.SD$N1.REGION.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
611 mean(.SD$N2.REGION.nt.nb, na.rm=T)),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
612 Total.P=( mean(.SD$P3V.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
613 mean(.SD$P5D.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
614 mean(.SD$P3D.nt.nb, na.rm=T) +
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
615 mean(.SD$P5J.nt.nb, na.rm=T))),
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
616 by=c("Sample")])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
617 newData[,sapply(newData, is.numeric)] = round(newData[,sapply(newData, is.numeric)],1)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
618 write.table(newData, "junctionAnalysisUnProd.csv" , sep=",",quote=F,na="-",row.names=F,col.names=F)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
619 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
620
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
621 # ---------------------- AA composition in CDR3 ----------------------
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
622
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
623 AACDR3 = PRODF[,c("Sample", "CDR3.Seq")]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
624
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
625 TotalPerSample = data.frame(data.table(AACDR3)[, list(total=sum(nchar(as.character(.SD$CDR3.Seq)))), by=Sample])
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
626
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
627 AAfreq = list()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
628
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
629 for(i in 1:nrow(TotalPerSample)){
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
630 sample = TotalPerSample$Sample[i]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
631 AAfreq[[i]] = data.frame(table(unlist(strsplit(as.character(AACDR3[AACDR3$Sample == sample,c("CDR3.Seq")]), ""))))
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
632 AAfreq[[i]]$Sample = sample
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
633 }
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
634
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
635 AAfreq = ldply(AAfreq, data.frame)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
636 AAfreq = merge(AAfreq, TotalPerSample, by="Sample", all.x = T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
637 AAfreq$freq_perc = as.numeric(AAfreq$Freq / AAfreq$total * 100)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
638
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
639
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
640 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")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
641 AAfreq = merge(AAfreq, AAorder, by.x='Var1', by.y='AA', all.x=TRUE)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
642
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
643 AAfreq = AAfreq[!is.na(AAfreq$order.aa),]
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
644
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
645 AAfreqplot = ggplot(AAfreq)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
646 AAfreqplot = AAfreqplot + geom_bar(aes( x=factor(reorder(Var1, order.aa)), y = freq_perc, fill = Sample), stat='identity', position='dodge' )
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
647 AAfreqplot = AAfreqplot + annotate("rect", xmin = 0.5, xmax = 2.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
648 AAfreqplot = AAfreqplot + annotate("rect", xmin = 3.5, xmax = 4.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
649 AAfreqplot = AAfreqplot + annotate("rect", xmin = 5.5, xmax = 6.5, ymin = 0, ymax = Inf, fill = "blue", alpha = 0.2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
650 AAfreqplot = AAfreqplot + annotate("rect", xmin = 6.5, xmax = 7.5, ymin = 0, ymax = Inf, fill = "red", alpha = 0.2)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
651 AAfreqplot = AAfreqplot + ggtitle("Amino Acid Composition in the CDR3") + xlab("Amino Acid, from Hydrophilic (left) to Hydrophobic (right)") + ylab("Percentage")
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
652
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
653 png("AAComposition.png",width = 1280, height = 720)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
654 AAfreqplot
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
655 dev.off()
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
656 write.table(AAfreq, "AAComposition.csv" , sep=",",quote=F,na="-",row.names=F,col.names=T)
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
657
28fbbdfd7a87 Uploaded
davidvanzessen
parents:
diff changeset
658