Repository navigation
Expand file tree
/
Copy pathLoadMembow2.R
More file actions
647 lines (487 loc) · 24.2 KB
/
Copy pathLoadMembow2.R
File metadata and controls
647 lines (487 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# 29 Aug
# Load data for reconstruction of 10mer data for integrase paper
#useful commands for terminal:
#generate lineage based on the manual distance measure
# manualTree = upgma(as.dist(t(manualDist(as.character(barcodes),0.3,2/3,2 ))));manualTree$tip.label<- paste(names(barcodes),barcodes,sep="_")
getwd()
rm(list=ls())
library("ape")
library("phangorn")
library(factoextra)
library(stringr)
library(phytools)
#erase chace files
system("rm /Users/alejandrog/MEGA/Caltech/trees/GraceData/10mer_membow/editRate/*")
source("../GraceData/RF.experiment.R")
source("simulation2.R")
source("simMemoirStrDist3.R")
#choose the tree from the list:
fasta=F
clust.method=1
plot.all = 1
file.path="/Users/alejandrog/MEGA/Caltech/trees/GraceData/10mer_membow/"
#save plot for continous recording trees
plot.path=paste(file.path,"plotsReconstruction/",sep="")
plots.edit.rate = paste(file.path,"plotsEditRate/",sep="")
existing.files = list.files("../GraceData/10mer_membow/")
#read the xls with all the files info (some are not in the folder)
all.files=read.csv(paste(file.path,"results.guide.membow.csv",sep=""))
#read.newick(text=toString(all.files$newick[1]))
#this file information
##for(file.idx in c(10)){
for(file.idx in 1:length(all.files$file.name)){
#for(file.idx in 39){
files.extension = all.files$file.extension[file.idx]
file.name=all.files$file.name[file.idx]
stacked.rate.file=paste(file.path,"editRate/",toString(all.files$file.name[file.idx]),"_all_rates.txt",sep="",collapse="")
fileName=paste(file.path,file.name,sep="")
if(length(grep(file.name,existing.files))!=0){ #only if the corresponding txt file is in the directory
########
#how many groups for the colors in the dendrogram
ks= rep(4,length(all.files$Position))
k=ks[file.idx]
#estimMu = estimMu.s[file.idx]
#estimAlpha=alphas[file.idx]
estimG =all.files$generations[file.idx]
#read csv as dataframe
posInfo=read.csv(paste(fileName,files.extension,sep=""),sep="\t")
#estimate the edit rate from the fraction of unedited sites
lengthBarcode = 10
avgEditRate = array()
avgEditRate_r = array()
avgEditRate_x = array()
nCells_ = length(posInfo$state)
#features are in
#Sometimes there is no ID bc the cells did not appear in the field of view or bc they have to be excluded for diverse reasons.
#When there is no ID, the value becomes NA
barcodeLength = nchar(as.character(posInfo$state[1]))
#barcodes =posInfo$Summary
#FILTERING:::::
# £ £ £ # # # # # #
barcodes = posInfo$state[!is.na(posInfo$cell) & !posInfo$state==paste(rep("x",barcodeLength),collapse="")]
names(barcodes) =posInfo$cell[!is.na(posInfo$cell) & !posInfo$state==paste(rep("x",barcodeLength),collapse="")]
#After filtering the NA and XXXX.. we can calculte the edit rate per cell
nCells_=length(as.character(barcodes))
avgEditRate = array(); avgEditRate_r=array(); avgEditRate_x=array()
for(i in 1:nCells_){
avgEditRate[i] = sum(strsplit(toString(barcodes[i]),"")[[1]]=="u")/lengthBarcode #fraction of "u" in the array
avgEditRate_r[i] = sum(strsplit(toString(barcodes[i]),"")[[1]]=="r")/lengthBarcode
avgEditRate_x[i] = sum(strsplit(toString(barcodes[i]),"")[[1]]=="x")/lengthBarcode
}
pos.vals=!(avgEditRate_x==0 & avgEditRate_r==0) #which cells to take into accoutn
estimMu = 1-mean(avgEditRate[pos.vals])^(1/estimG)
estimAlpha = 1-mean(avgEditRate_x[pos.vals] / (avgEditRate_r[pos.vals] + avgEditRate_x[pos.vals]))
#trying a small hack, 1-alpha
#estimAlpha = 1-estimAlpha
#estimG = estimG * 10
# # # paper plot
#Lets calculate the edit rate per site (in theory should be even)
tt=apply(as.matrix(barcodes),2,strsplit,"")
mat.sites = t(do.call(cbind,do.call(cbind,tt)))
rate.per.site = apply(mat.sites!="u",2,sum)/dim(mat.sites)[1]
rate.per.site = t(rate.per.site)
#save to file for further processing and storage
#FILE 1
rate.file=paste(file.path,"editRate/All_rates.txt",sep="",collapse="")
write.table(rate.per.site,rate.file, append=T, row.names =F,col.names=F)
#order: x, r, u
#save rates as appended file independently: WORKS
alphabet =c("u","r","x")
#FILE 2
for (letter in alphabet){
this.file=paste(file.path,"editRate/",letter,"_rates.txt",sep="",collapse="")
letter.per.site = t(apply(mat.sites==letter,2,sum)/dim(mat.sites)[1])
write.table(letter.per.site,this.file, append=T, row.names =F,col.names=F)
}
#FILE 3
#individual edit rate (average for each tree)
write.table(mean(rate.per.site),paste(file.path,"editRate/distribution_rates_perTree.txt",sep="",collapse="") ,
append=T,row.names = F,col.names = F)
write.table(estimMu,paste(file.path,"editRate/distribution_Mu_perTree.txt",sep="",collapse="") ,
append=T,row.names = F,col.names = F)
#FILE 4
#Write all barcodes for all cells for all trees together in a single file
all.bc.file = paste(file.path,"editRate/","allBarcodes.txt",sep="")
write.table(mat.sites,all.bc.file,append=T,row.names=F,col.names =F)
#names(barcodes)=posInfo$Movie.ID
#convert data to fasta format
#this function inserts a big R at the end of the sequence so it has at least one "c"
#which will be constant. We do this because some calculations of base frequency need all actg bases to be present.
if(fasta==T){
fastaBarcodes = convertSimToFasta(barcodes)
fasIN <-paste(fileName,".fas",sep="")
rand.barcode.order<-sample(1:length(fastaBarcodes))
#We re-order the barcodes using a fixed (but random) order
fastaBarcodes<-fastaBarcodes[rand.barcode.order]
barcodes<-barcodes[rand.barcode.order]
write(fastaBarcodes,file=fasIN)
#cmds[1]="sed -i.bak 's/R/c/g'"
#cmds[2]="sed -i.bak 's/x/t/g'"
#cmds[3]="sed -i.bak 's/u/g/g'"
#cmds[4]="sed -i.bak 's/r/a/g'"
convertMemoirToDNA(fasIN)
#sequences have a "c" at the end
#now we can use the phyDat
memoirfas<-read.phyDat(fasIN,format="fasta",type="DNA")
#calculate distance methods
dm<-dist.ml(memoirfas)
dm.ham=dist.hamming(memoirfas)
print("distance calculated")
treeUPGMA<-upgma(dm)
treeUPGMA.ham <-upgma(dm.ham)
hc=as.hclust(reverseLabels(treeUPGMA))
x11()
fviz_dend(hc, k = k, # Cut in four groups
cex = 0.9, # label size
k_colors = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"),
color_labels_by_k = TRUE, # color labels by groups
rect = TRUE, # Add rectangle around groups
rect_border = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"),
rect_fill = TRUE,
main="reconstructed lineage (membow)",
xlab="cells",ylab="time")
#horizontal tree
x11()
fviz_dend(hc, k = k, cex = 0.8, horiz = TRUE, k_colors = "jco",
rect = TRUE, rect_border = "jco", rect_fill = TRUE,xlab="time",ylab="cells")
}
if(clust.method==1){
ground.truth = all.files$newick[file.idx]
true.tree=read.newick(text=toString(ground.truth))
#manualTree = upgma(as.dist(t(manualDist(as.character(barcodes),estimMu,estimAlpha,estimG ))));
#TEST manualTree using the ML function (should work slightly better)
matdist_=manualDistML(as.character(barcodes),estimMu,estimAlpha,estimG )
#colnames(matdist_)<-barcodes
#row.names(matdist_)<-barcodes
#alternative w/o plotting the actual heatmap, only hclust method
hclust.tree=as.phylo(hclust(as.dist(t(matdist_))))
manualTree = hclust.tree
tip.names = paste(names(barcodes),barcodes,sep="_")
for(tt in 1:length(tip.names)){
this.cell.n = str_extract(tip.names[tt],"\\d+")
if(nchar(this.cell.n)==1){
tip.names[tt]=paste("0",tip.names[tt],sep="")
}
}
tip.names=gsub("r","2",tip.names)
tip.names=gsub("x","1",tip.names)
tip.names=gsub("u","0",tip.names)
manualTree$tip.label<- tip.names
#sometimes there are edges with negative values (-6e-18) which should not happen
manualTree$edge.length[manualTree$edge.length<0]=0
#hc.manual=as.hclust(reverseLabels(manualTree))
hc.manual=as.hclust(manualTree)
# for saving ggsave("membow_31_2tree.pdf", device=CairoPDF)
#TEST FOR DIANA method # # # # # #
matdist_1 = matdist_
colnames(matdist_1)<-tip.names;row.names(matdist_1)<-tip.names
hc.manual_ = as.hclust( diana(as.dist(t(matdist_1))))
### EDIT FOR RF DISTRIBUTION 17th October 2018
#this function calculates the RF distance between the reconstruction and the groun truth.
#this should be comparable to previous measures of RF for the simulations.
#RF =0 means random clustering
#RF =1 means perfect reconstruction
RF.list = normalized.RF.experiment(true.tree, manualTree , barcodes,posInfo)
this.score = RF.list[[1]]
print(this.score)
alive.tree = RF.list[[2]]
rand.dist = RF.list[[3]]
#PLOTTING
# # # # # # # #
# # # # # # # #
if(plot.all==1){
if(nCells_<4){
k=1
}
fviz_dend(hc.manual, k = k, cex = 1.2, horiz = TRUE, k_colors = "jco",
rect = F, rect_border = "jco", rect_fill = TRUE,xlab="cells",ylab="time",
labels_track_height=2)
pdf.path = paste(plot.path,all.files$file.name[file.idx],".pdf",sep="")
scaling=0.7
ggsave(pdf.path, device=cairo_pdf,width = 9.32*scaling,height = 10.4*scaling,units="in")
pdf.path.truth = paste(plot.path,all.files$file.name[file.idx],"_groundTruth.pdf",sep="")
pdf(pdf.path.truth)
plot.phylo(true.tree)
dev.off()
#PDF WRITE for simplified ground.truth
pdf.path.truth.simple = paste(plot.path,all.files$file.name[file.idx],"_groundTruth_simple.pdf",sep="")
pdf(pdf.path.truth.simple)
plot.phylo(alive.tree)
dev.off()
#join the pdfs
system(paste("\'/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py\' -o ",plot.path,file.name,"_merged.pdf ",
pdf.path," ",pdf.path.truth.simple," ",pdf.path.truth, sep=""))
#remove individual files:
system(paste("rm ",pdf.path,sep=""))
system(paste("rm ",pdf.path.truth,sep=""))
system(paste("rm ",pdf.path.truth.simple,sep=""))
} #END PLOTTING
# # # # # #
# # # # # #
#we can make a vector of distances :
colnames(matdist_)<-tip.names;row.names(matdist_)<-tip.names
d1=this.score
d2=RF.dist(as.phylo(as.hclust( diana(matdist_+t(matdist_) ) )),alive.tree)
if(rand.dist>0){d2 = 1-d2/rand.dist}
#distance 2
if(length(barcodes)>4){
d3.list = normalized.RF.membow(true.tree,manualTree,barcodes,posInfo)
d3 =d3.list[[1]]
n.colony=d3.list[[2]]
}else { d3 = d1;alive.tree_ = alive.tree;n.colony = length(barcodes)}
b=as.matrix(do.call(cbind,strsplit(substr(tip.names,4,14),"")))
d4=sum(b=="0")/prod(dim(b)) #fraction of unedited sites (to filter bad colonies)
#this.score is a colum vector of many clustering methods, based on the same distance matrix
this.score = t( c (d1,d2,d3))
this.score = t(c(toString(all.files$file.name[file.idx]),d1,d3,n.colony,d4))
#WRITE FILE RF distance
write.table(this.score,paste(file.path,"editRate/distribution_RF_perTree.txt",sep="",collapse="") ,
append=T,row.names = F,col.names = F)
#WRITE FILE NEWICK reconstructed
newick.out=paste(fileName,".nwk",sep="")
write.tree(manualTree,file=newick.out)
}
# # # # # # # # # # #
# # # # # # # # # # #
#Alternative: use the hclust method from R
if(clust.method ==2){
matdist_names = matdist_
colnames(matdist_names)<- paste(names(barcodes),barcodes,sep="_")
dend<-hclust(as.dist(t(matdist_names)))
fviz_dend(dend, k = k, cex = 0.8, horiz = TRUE, k_colors = "jco",
rect = TRUE, rect_border = "jco", rect_fill = TRUE,xlab="time",ylab="cells")
}
}
}# # # # # # # #
# # # # # # # # #
# # # # # # # #
#END MAIN FOR
#Useful commands
#DATA FRAME WITH DISTANCE
# data.frame(RF=rate.dist,name=all.files$file.name[as.logical(all.files$FISH)])
# # # # # # # #
# # # # # # #
#Additional plot schemes for paper FIGURES
# "npg" is the palette inspired by nature-like papers
# x11();fviz_dend(hc.manual, k = k, cex = 1.4, horiz = TRUE, k_colors = "npg",
# rect = T, rect_border = "npg", rect_fill = T,xlab="cells",ylab="time",low_rect=100)
#
#FIGURES
#plot edit rates per site as a barplot with error bars WORKS
read.files <- function (file.path.membow,file.path.memoir){
rate.dist.file.membow = paste(file.path.membow,"editRate/distribution_RF_perTree.txt",sep="",
collapse="");
rate.dist.file.memoir = paste(file.path.memoir,"editRate/distribution_RF_perTree.txt",sep="",
collapse="");
rate.dist.membow = read.table(rate.dist.file.membow)
rate.dist.memoir = read.table(rate.dist.file.memoir)
return(list(rate.dist.membow,rate.dist.memoir))
}
barplot.edit.rate<-function(rate.file="/Users/alejandrog/MEGA/Caltech/trees/GraceData/10mer/editRate/All_rates.txt"){
all.rates = as.matrix(read.table(rate.file))
data<-data.frame(
name= as.character(1:10), value=apply(all.rates,2,mean), sd=apply(all.rates,2,sd),order=1:10
)
x11()
ggplot(data) + geom_bar( aes(x=reorder(name,order),y=value), stat="identity",fill="skyblue",alpha=0.7 ) +
geom_errorbar( aes(x=name, ymin = value-sd,ymax = value+sd ),width=0.4,colour="orange",alpha=0.9,size=1.3 ) +
theme(text = element_text(size=20)) + labs(x = "Element in array",y="Freq edited sites")
#axis.text.x = element_text(angle=90, hjust=1))
}
compare.ecdf<-function(file.path){
rate.dist.file = paste(file.path,"editRate/distribution_RF_perTree.txt",sep="",
collapse="");
rate.dist = read.table(rate.dist.file)
n = dim(rate.dist)[1]
for(i in 2:length(names(rate.dist) )) {
a = sort(rate.dist[,i])
if(i ==2){
plot(a,(1:n)/n,type = "l",col = "black",
ylab = "RF score",xlab="Fraction of colonies",main="MEMbow scores all colonies",
lwd = 2)
}else {
lines(a,(1:n)/n,lwd=2,col="red")
}
}
}
compare.ecdf2<- function(rate.dist){
metrics = c("Full RF","Clade RF")
rate.dist= rate.dist[rate.dist$V3>0, ]
n = dim(rate.dist)[1]
rate.dist.v2 =rate.dist[order(rate.dist$V2),]
rate.dist.v3 =rate.dist[order(rate.dist$V3),]
df= data.frame(RF=c(rate.dist.v2$V2,rate.dist.v3$V3) ,
probs =c( (1:n)/n, (1:n)/n),metric = factor(c(rep_len(metrics[1],n),rep_len(metrics[2],n))))
ggplot(data = df, aes(x = RF,y=probs,group = metric ,color=metric) ) +
geom_line(size = 1.2) + geom_point() +
theme(text = element_text(size=21)) + labs(x = "Tree score (RF)",y="Fraction of colonies") +
theme_minimal(base_size = 20) + xlim(0, 1) + ylim(0,1)
}
file.path.memoir ="/Users/alejandrog/MEGA/Caltech/trees/GraceData/10mer/"
file.path.membow = file.path
compare.ecdf.memoir.membow<- function(file.path.membow,file.path.memoir){
rate.dist.file.membow = paste(file.path.membow,"editRate/distribution_RF_perTree.txt",sep="",
collapse="");
rate.dist.file.memoir = paste(file.path.memoir,"editRate/distribution_RF_perTree.txt",sep="",
collapse="");
rate.dist.membow = read.table(rate.dist.file.membow)
rate.dist.memoir = read.table(rate.dist.file.memoir)
rate.dist.membow = rate.dist.membow[rate.dist.membow$V3>0, ]
n.membow = dim(rate.dist.membow)[1]
n.memoir = dim(rate.dist.memoir)[1]
metrics = c("MEMbow","MEMOIR")
rate.dist.v2 =rate.dist.memoir[order(rate.dist.memoir$V2),]
rate.dist.v3 =rate.dist.membow[order(rate.dist.membow$V3),]
df= data.frame(RF=c(rate.dist.v3$V3,rate.dist.v2$V2) ,
probs =c( (1:n.membow)/n.membow, (1:n.memoir)/n.memoir),metric = factor(c(rep_len(metrics[1],n.membow),rep_len(metrics[2],n.memoir))))
ggplot(data = df, aes(x = RF,y=probs,group = metric ,color=metric) ) +
geom_line(size = 1.2) + geom_point() +
theme(text = element_text(size=21)) + labs(x = "Tree score (RF)",y="Fraction of colonies") +
theme_minimal(base_size = 20) + xlim(0, 1) + ylim(0,1)
}
#boxplot for comparing RF scores for MEMbow and MEMOIR trees
#Oct 21th 2018
compare.hist.memoir.membow<-function (file.path.membow,file.path.memoir){
rate.dist.file.membow = paste(file.path.membow,"editRate/distribution_RF_perTree.txt",sep="",
collapse="");
rate.dist.file.memoir = paste(file.path.memoir,"editRate/distribution_RF_perTree.txt",sep="",
collapse="");
rate.dist.membow = read.table(rate.dist.file.membow)
rate.dist.memoir = read.table(rate.dist.file.memoir)
rate.dist.membow= rate.dist.membow[rate.dist.membow$V3>0, ]
n.membow = dim(rate.dist.membow)[1]
n.memoir = dim(rate.dist.memoir)[1]
metrics = c("MEMbow","MEMOIR")
rate.dist.v2 =rate.dist.memoir[order(rate.dist.memoir$V2),]
rate.dist.v3 =rate.dist.membow[order(rate.dist.membow$V3),]
df= data.frame(RF=c(rate.dist.v3$V3,rate.dist.v2$V2) ,
probs =c( (1:n.membow)/n.membow, (1:n.memoir)/n.memoir),metric = factor(c(rep_len(metrics[1],n.membow),rep_len(metrics[2],n.memoir))))
ggplot(df,aes(metric,RF,fill=metric)) + geom_boxplot(outlier.size=0) + scale_fill_manual(values=wes_palette(n=2, name="GrandBudapest2")) +
geom_jitter(aes(metric,RF), position = position_jitter(width=0.1,height=0),
alpha=0.6,size=3,show_guide =F) +
xlab("Recording mode") +
ylab("Reconstructability") +
theme(axis.title.x = element_text(size=20,hjust=0.5),
axis.title.y = element_text(size=20,vjust=1),
axis.text.x = element_text(size=14,color='black'),
axis.text.y = element_text(size=14,color='black'),
legend.text=element_text(size=16),
legend.title=element_text(size=18),
legend.key.size = unit(1.5,"cm")
)
}
n.vs.rf<-function(file.path.membow,file.path.memoir,bins){
results = read.files(file.path.membow,file.path.memoir)
rate.dist.membow = results[[1]]
rate.dist.memoir = results[[2]]
x = c(rate.dist.membow$V4[rate.dist.membow$V3>0],rate.dist.memoir$V3)
y = c(rate.dist.membow$V3[rate.dist.membow$V3>0],rate.dist.memoir$V2)
dat = data.frame(x = x,y = y)
p=ggplot(dat,aes(x=x,y=y)) + geom_point() + theme(legend.position = "none") + xlab("Colony size") +
ylab("Reconstructability") +
theme(text = element_text(size=21))
ggMarginal(p,type="histogram",fill="cyan4",xparams=list(bins=10),yparams = list(bins=bins))
}
#paper plot
#plot the frequency of each edit per site as a stacked barplot
#this plots uses ggplot2
stackedplot.edit.rate<-function(rate.file="/Users/alejandrog/MEGA/Caltech/trees/GraceData/10mer/editRate/All_rates.txt"){
alphabet = c("u","r","x")
N.sites=10
all.rates = matrix(0,length(alphabet), N.sites)
for(i in 1:length(alphabet)){
letter = alphabet[i]
this.file=paste(file.path,"editRate/",letter,"_rates.txt",sep="",collapse="")
letter.rates = as.matrix(read.table(this.file))
all.rates[i,] = apply(letter.rates,2,mean)
}
#since it is the average, not all colums add up to 1:
norm.rates = t(t(all.rates) / apply(all.rates,2,sum))
colnames(norm.rates)<-as.character(1:10)
row.names(norm.rates)<-alphabet
coul = brewer.pal(length(alphabet),"Pastel2")
all.rates=norm.rates
#make a data frame. Note that we can specify the order of factor to be plotted (order of stacks in the plot)
data<-data.frame(
letter = factor(c(rep("u",N.sites),rep("r",N.sites),rep("x",N.sites)), levels=c("r","x","u")),
siten=factor(names(c(all.rates[1,],all.rates[2,],all.rates[3,])), levels=as.character(1:10) ),
value=c(all.rates[1,],all.rates[2,],all.rates[3,])
)
#making a stacked barplot with ggplot2
ggplot() +
geom_bar(aes(y=value,x=siten,fill=letter),data=data,stat="identity") +
scale_fill_brewer(palette="Pastel2")
#NOTE
#This would be the old-school way with native R plotting:
#barplot(norm.rates,col=coul,ylab = "freq",xlab="site number",
#names.arg = as.character(1:10))
#legend("topright", inset=c(0.8,0.8),
# fill=coul,
# legend=alphabet,bg="white")
}
#plot 3
histogram.plot.editrate<-function(file.path=file.path){
rate.dist.file = paste(file.path,"editRate/distribution_rates_perTree.txt",sep="",collapse="")
rate.dist = read.table(rate.dist.file)
}
histogram.plot.RF<-function(file.path=file.path,posInfo){
rate.dist.file = paste(file.path,"editRate/distribution_RF_perTree.txt",sep="",collapse="")
rate.dist = read.table(rate.dist.file)
tree.analysis = data.frame(RF=rate.dist$V1,name=all.files$file.name[as.logical(all.files$FISH)])
return(tree.analysis)
}
#plot 4
#read ALL files in the results/ folder and calculate a global rate of edits, including "w" & "R"
stacked.plot.allTrees<-function(which.sites=1:10){
results.folder = "/Users/alejandrog/Downloads/results/"
all.results = list.files(results.folder)
alphabet = c("u","r","x")
N.sites=10
all.mat.sites = matrix(, nrow=0,ncol=10)
rate.per.site = c()
for(i in 1:length(all.results)){
posInfo=read.table(paste(results.folder,all.results[i],sep=""),header=T)
barcodes = posInfo$state[!is.na(posInfo$cell) & !posInfo$state==paste(rep("x",barcodeLength),collapse="")]
names(barcodes) =posInfo$cell[!is.na(posInfo$cell) & !posInfo$state==paste(rep("x",barcodeLength),collapse="")]
tt=apply(as.matrix(barcodes),2,strsplit,"")
mat.sites = t(do.call(cbind,do.call(cbind,tt)))
#save the average edit rate for each tree:
#choose which sites to include in the analysis (to look for biases in edit rates)
rate.per.site[i] = mean(apply(mat.sites[,which.sites]!="u",2,sum)/dim(mat.sites)[1])
all.mat.sites= rbind(all.mat.sites,mat.sites)
}
freq.per.site=matrix(0,length(alphabet),dim(all.mat.sites)[2])
j=1
for (letter in alphabet){
freq.per.site[j,] = t(apply(all.mat.sites==letter,2,sum)/dim(all.mat.sites)[1])
j=j+1
}
coul = brewer.pal(length(alphabet),"Pastel2")
#all.rates=norm.rates
#make a data frame. Note that we can specify the order of factor to be plotted (order of stacks in the plot)
all.rates = freq.per.site
all.rates = t(t(all.rates) / apply(all.rates,2,sum))
colnames(all.rates)<-as.character(1:10)
row.names(all.rates)<-alphabet
data<-data.frame(
letter = factor(c(rep("u",N.sites),rep("r",N.sites),rep("x",N.sites)), levels=c("r","x","u")),
siten=factor(names(c(all.rates[1,],all.rates[2,],all.rates[3,])), levels=as.character(1:10) ),
value=c(all.rates[1,],all.rates[2,],all.rates[3,])
)
#making a stacked barplot with ggplot2
ggplot() +
geom_bar(aes(y=value,x=siten,fill=letter),data=data,stat="identity") +
scale_fill_brewer(palette="Pastel2")
return(list(rate.per.site,data,all.mat.sites))
}
#use the object returned above as input for these two functions:
plot.stacked.frequencies<-function(a){
geom_bar(aes(y=value,x=siten,fill=letter),data=a[[2]],stat="identity") +
+ scale_fill_brewer(palette="Pastel2") + labs(x="Site N",y="Frequency",title="Frequency of edits per site (48hrs)")
}
#histogram
plot.histogram.edit.rates<-function(a){
qplot(a[[1]],geom="histogram",main=paste("Edit rate distribution, n=",toString(length(a[[1]])),sep=""),
binwidth=0.12,xlab="Fraction of edited sites",ylab="N of colonies",
fill=I("grey"),col=I("black"))
}