/
140928-SeriouslyFinalFigures.R
1570 lines (1488 loc) · 88.2 KB
/
140928-SeriouslyFinalFigures.R
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Required packages: Limma,data.table,ggplot2,reshape2
require(limma)||install.packages(limma)
require(data.table)||install.packages(data.table)
require(ggplot2)||install.packages(ggplot2)
require(reshape2)||install.packages(reshape2)
require(DESeq2)||install.packages(DESeq2)
require(grid)||install.packages(grid)
require(dplyr)||install.packages(dplyr)
#The (ACTUAL) Figure code used in the manuscript for blm as of 14/09/29. No changes to the figures will be made other than through this code!
#Except figure 1, because figure 1 is from a powerpoint. Fig1BW.ppt
##This code all runs off of count tables - these count tables should also be added to git (and have their locations in the code suitably refactored) -
#current count table locations:
# SEQC : ~/Desktop/MainSEQC/ subdirectories SEQC_MAIN_ILM_rawCounts_ZSu.2012_04_14 & SEQC_LifescopeCounts
# BLM : ~/Downloads/RawData/BLM/Countdata #added as PAPERUSED.zip (but makerefmetdfb isn't quite fixed yet)
#Figure2: LineFigure()
LineFigure<-function(){
theme_set(theme_bw(base_size=18))
df<-calcmodel(assignIdentity(makerefmetdfb(norm="UQN")))
ggplot()+geom_point(data=subset(df,identity!="external"),aes(x=log2(a1),y=log2(pred1)))+xlab("log2(Observed Mix1 Counts)")+ylab("log2(Predicted Mix1 Counts)")
}
#Figure3:targetfigure()
targetfigure<-function(){
theme_set(theme_bw(base_size=18))
a<-makeTargetPlot(type="BLM",df=makerefmetdfb(norm="UQN"))
return(a+coord_cartesian(ylim=c(0,0.75),xlim=c(0,0.75))+scale_x_continuous(breaks=c(0,0.25,0.5,0.75))+scale_y_continuous(breaks=c(0,0.25,0.5,0.75)))
}
#Figure6: call dendrogramplot())
dendrogramplot<-function(testing=FALSE){
require(stats)
require(RColorBrewer)
require(DESeq2)
SEQCDF<-makeseqcdf()
vsndata<-varstabdata()
test<-dist(t(assay(vsndata)))
test<-as.matrix(test)
rownames(test)<-colnames(test)<-with(colData(vsndata),paste(sample,site,sep=":"))
hcr <- suppressWarnings(hclust(dist(test)))
hcr$labels<-paste0(c(rep(c("A","B","C","D","Cm","Dm"),6)),c(rep(1,6),rep(2,6),rep(3,6),rep(4,6),rep(5,6),rep(6,6)))
hcrd<-as.dendrogram(hcr)
#labelColors = c("#CDB380", "#036564", "#EB6841", "#EDC951")
labelColors<<-brewer.pal(n = 4,name = "PuOr")
# cut dendrogram in 4 clusters
clusMember<<- cutree(hcr, 4)
# using dendrapply
colLab<-function(n) {
if (is.leaf(n)) {
a <- attributes(n)
labCol <- labelColors[clusMember[which(names(clusMember) == a$label)]]
attr(n, "nodePar") <- list(a$nodePar, lab.col = labCol, lab.cex=1.5)
}
n
}
clusDendro = dendrapply(hcrd, colLab)
if(testing==TRUE){return(clusDendro)}
# make plot
output<-plot(clusDendro, main = "",axes=FALSE)
rm(clusMember,labelColors,pos=1)
return(output)
}
DistributionFigure<-function(indf){
if(missing(indf)){indf<-makeseqcdf()}
{seqcmfrac<-NULL;for(I in levels(indf$site)){seqcmfrac<-rbind(seqcmfrac,calcmrnafrac(subset(indf,site==I)[,c(1,3:18)],selection=2:17))}
seqcmfrac<-as.data.frame(seqcmfrac)
seqcmfrac$site<-levels(indf$site)
seqcmfrac<-melt(seqcmfrac,id.vars="site")
seqcmfrac$replicate<-substr(seqcmfrac$variable,2,2)
seqcmfrac$sample<-substr(seqcmfrac$variable,1,1)
}#setup the data frame
summarize(group_by(seqcmfrac,sample,site),mean(value))
pme<-summarize(group_by(seqcmfrac,site),mean(value),AtoB=mean(value[sample=="A"])/mean(value[sample=="B"]))
#gives an A to B ratio. This value is to be compared to the shippydf values (1.39-1.476)
#The raw data from Shippy Et al (Nature Biotechnology 24, 1123 - 1131 (2006)): Just mean +sd
randshipsq<-data.frame(atob=rnorm(10000,mean=2.870,sd=0.095)/rnorm(100,mean=2.003,sd=0.124),atoc=rnorm(10000,mean=2.870,sd=0.095)/(rnorm(10000,mean=2.870,sd=0.095)*0.75+rnorm(10000,mean=2.003,sd=0.124)*0.25),
atod=rnorm(10000,mean=2.870,sd=0.095)/(rnorm(10000,mean=2.870,sd=0.095)*0.25+rnorm(10000,mean=2.003,sd=0.124)*0.75))
#convert to a distribution rather than boxplots
trial<-rbind(melt(randshipsq),subset(pm4,site!="PSU")[,3:4])
trial$type<-c(rep("Previous Measurement",30000),(rep("ERCC-estimated",96)))
levels(trial$variable)<-c("Sample A:B","Sample A:C","Sample A:D")
ggplot(data=trial)+geom_density(aes(x=value,fill=type,alpha=type))+facet_wrap(~variable)+xlab("Ratio")+scale_alpha_manual(values=c(0.75,0.75),name="mRNA fraction Ratio")+scale_fill_manual(values=c("grey20","grey60"),name="mRNA fraction Ratio")+ylab("")
}
sf1<-function(){
adf<-(subset(rbind(assignIdentity(makerefmetdfb(norm=0)),assignIdentity(makerefmetdfb(norm=1)),assignIdentity(makerefmetdfb(norm="UQN"))),identity!="unclassified"))
adf$norm<-as.factor(adf$norm);levels(adf$norm)[1]<-"None";levels(adf$norm)[2]<-"Library Size";levels(adf$norm)[3]<-"Upper Quartile" #just some sad cleaning...
adf$norm<-relevel(adf$norm,ref="None")
g<-ggplot(adf)
return(print(g+geom_point(data=subset(adf,identity!="unclassified"),aes(x=log2(a1*a2)/2,y=log2((a1)/a2),col=identity))+geom_point(data=subset(adf,identity=="external"),aes(x=log2(a1*a2)/2,y=log2((a1)/a2)),size=3)+facet_grid(. ~ norm)+geom_hline(yintercept=0)+ggtitle("Normalization")+
theme(legend.position=c(0.9,0.9))+theme(strip.background = element_rect(fill = 'white'))+ylim(c(-2,2))+scale_colour_manual(values=c("#CC6666","black","#99CC66","#6699CC"),labels=c("Brain","ERCC","Liver","Muscle"))+xlim(c(0,18))+
theme(legend.title=element_blank())+theme(title=element_text(size=20))+
xlab("Mean Counts (BLM-1+BLM-2)")+ylab("Ratio of counts (BLM-1/BLM-2)")+theme(strip.text.x=element_text(size=24))+theme(axis.title=element_text(size=24))+theme(axis.text=element_text(size=20))+
guides(colour=guide_legend(override.aes=list(size=5)))))
}#Sup fig 1; MA plots : by normalization
sf5<-function(indf){
require(reshape2)
if(missing(indf)){
indf<-makeseqcdf()}
#indf<-normalizeSEQC(indf) #apparently normalization of these data pre-analysis make for some issues...but only for naive/mymethod, decon is unaffected.
final<-NULL;finalMod<-NULL;finalNaive<-NULL
dset<-cbind(indf[,1:2],rowMeans(indf[,3:6]),rowMeans(indf[,7:10]),(indf[,11:14]),(indf[,15:18]))
names(dset)<-c("gene_id","site","A","B","C","C","C","C","D","D","D","D")
require("DeconRNASeq")
###SEQClm(subset(SEQCDF,site=="AGR")) #Debugging - the data that're getting plotted are NOT these...
###value Dval
###c1 0.7412337 0.2505380
###c2 0.7458750 0.2577192
###c3 0.7427236 0.2474030
###c4 0.7599289 0.2429792
###SEQClm(subset(SEQCDF,site=="AGR"),ignoremrna=TRUE)
###value Dval
###c1 0.7864730 0.3006213
###c2 0.7905321 0.3086469
###c3 0.7877769 0.2971081
###c4 0.8027694 0.2921405
for(I in levels(as.factor(dset$site))){
tmp<-DeconRNASeq(datasets=subset(dset,site==I)[c(5:12)],signatures=subset(dset,site==I)[,c(3,4)])
tmp<-tmp$out.all
tmp<-as.data.frame(tmp)
tmp$mix<-c(rep("C",4),rep("D",4))
tmp<-melt(tmp)
tmp$site<-I
naive<-SEQClm(subset(indf,site==I),ignoremrna=TRUE)
naive<-melt(naive);naive$site<-I;naive$mix<-c(rep("C",4),rep("D",4));naive$variable<-"A"
modeled<-SEQClm(subset(indf,site==I))
modeled<-melt(modeled);modeled$site<-I;modeled$mix<-c(rep("C",4),rep("D",4));modeled$variable<-"A"
final<-rbind(tmp,final)
finalNaive<-rbind(naive,finalNaive)
finalMod<-rbind(modeled,finalMod)
}
#calculates meanvalues
final$method<-"DeconRNASeq"
finalNaive$method<-"Naive"
finalMod$method<-"mRNA-correcting"
final<-rbind(final,finalNaive,finalMod)
final<-cbind(subset(final,mix=="C"),subset(final,mix=="D")[,3])
names(final)[6]<-"dval"
final$site<-as.factor(final$site)
levels(final$site)<-c("Lab 1 - ILM", "Lab 2 - ILM", "Lab 3 - ILM", "Lab 4 - ILM", "Lab 5 - ILM" , "Lab 6 - ILM" , "Lab 7 - LT" , "Lab 8 - LT", "Lab 9 - LT")
require(dplyr)
fmean<-group_by(final,method,site,variable)%>%summarise(mean(value),mean(dval),sd(value),sd(dval))
names(fmean)<-c("method","site","variable","meanc","meand","sdc","sdd")
g<-ggplot(final)
g+geom_point(aes(x=value,y=dval,color=(method),pch=method),alpha=0.65,size=4)+coord_cartesian(ylim=c(0.1,0.4),xlim=c(0.6,0.9))+
facet_wrap(~ site)+ylab("Amount of SEQC-A in SEQC-C")+xlab("Amount of SEQC-A in SEQC-D")+geom_point(aes(x=0.75,y=0.25),col="grey70")+theme(legend.position="none")+
geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.1,npoints=25),aes(x,y),col="grey")+
geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.05,npoints=25),aes(x,y),col="grey")+
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+theme(panel.margin=unit(1,"cm"))+theme(aspect.ratio=1)+
theme(legend.text=element_text(size=rel(1.4)))+theme(axis.title=element_text(size=rel(1.6)))+theme(axis.text=element_text(size=rel(1)))+theme(strip.background = element_rect(fill = 'white'))+theme(strip.text=element_text(size=rel(1.3)))+
geom_pointrange(data=fmean,aes(x=meanc,y=meand,ymax=meand+2*sdd,ymin=meand-2*sdd),size=1.15,shape=1)+geom_errorbarh(data=fmean,aes(x=meanc,y=meand,xmax=meanc+2*sdc,xmin=meanc-2*sdc),size=1.3)
#if axes along each facet are needed, http://stackoverflow.com/questions/17661052/force-x-axis-text-on-for-all-facets-of-a-facet-grid-plot # it's not simple though...
}#now SF3. Target plot: Naive vs Decon vs Us
sf2a<-function(){
gdfa<-(assignIdentity(makerefmetdfb(norm="UQN")))
gdfa$plat<-"HiSeq"
gdfa$mrat<-calcmrnafrac(gdfa)[7]/calcmrnafrac(gdfa)[1] # since the plot is comparing A1 to A2, I take the ratio of their mrna fractions as a term to use later.
gdfb<-assignIdentity(makerefmetdfb(norm=0,platform = "5500"))
gdfb$plat<-"SOLiD 5500"
gdfb$mrat<-calcmrnafrac(gdfb)[8]/calcmrnafrac(gdfb)[1] #this plot is comparing A1 to B2, because i want to look at the ERCC pool effects - thus this mrna fraction ratio correction term
# rownames(gdfa)<-gdfa$gene_id;rownames(gdfb)<-gdfb$gene_id
gdf<-rbind(gdfa,gdfb) #it's later now.
gdf$pme[gdf$plat=="SOLiD 5500"]<-gdfb[,9]#/sum(gdfb[,9])*sum(gdfb[,2]) #this printme variable corresponds to B2 in the data, because yeah that is what's going on in the 5500 data - but is normalized by the library size.
gdf$pme[gdf$plat=="HiSeq"]<-gdfa[,8]#/sum(gdfa[,8])*sum(gdfa[,2]) #this "printme" variable is A2. because. A2 is what i want to look at for this dataset
g<-ggplot(subset(gdf,identity!="unclassified"))
g+geom_point(aes(y=log2(a1*mrat/pme),x=log2(a1/mrat*pme)/2,col=identity,size=identity))+facet_grid(. ~ plat)+scale_colour_manual(values=c("#CC6666","black","#99CC66","#6699CC"),labels=c("Brain","ERCC","Liver","Muscle"))+
theme(legend.position=c(0.9,0.9))+labs(x="A",y=expression(paste(Log[2]," Ratio BLM-1 : BLM-2")))+scale_alpha_manual(values=c(0.7,1,0.7,0.7))+
scale_size_manual(values=c(1.5,3,1.5,1.5),labels=c("Brain","ERCC","Liver","Muscle"))+theme(legend.title=element_blank())+theme(strip.background = element_rect(fill = 'white'))+
scale_x_continuous(limits=c(0,20),expand=c(0,0))+scale_y_continuous(limits=c(-2,2),expand=c(0,0))+guides(colour=guide_legend(override.aes = list(size=4)))+theme(strip.text.x=element_text(size=24))+
theme(axis.title=element_text(size=24))+theme(axis.text=element_text(size=20))+geom_point(data=subset(gdf,identity=="external"),aes(y=log2(a1*mrat/pme),x=log2(a1/mrat*pme)/2,col=identity,size=identity))
}#no longer referred to in the paper
sf4<-function(indf){
require(reshape2);require(grid);require(data.table);require(ggplot2)
if(missing(indf)){
indf<-makeseqcdf()}
#do UQN for ILM sites:
indfILM<-subset(indf,!site%in%c("NWU","PSU","SQW")) #subset to ILM only
indfILM$site<-droplevels(indfILM$site) #clear out those pesky empty factor levels
indfILM<-normalizeSEQC(indfILM,type="Both")
indfLT<-subset(indf,site%in%c("NWU","PSU","SQW"))
indf<-rbind(indfILM,indfLT)
final<-NULL
dset<-cbind(indf[,1:2],rowMeans(indf[,3:6]),rowMeans(indf[,7:10]),(indf[,11:14]),(indf[,15:18]))
names(dset)<-c("gene_id","site","A","B","C","C","C","C","D","D","D","D")
for(I in levels(as.factor(dset$site))){
modeled<-SEQClm(subset(indf,site==I))
modeled<-suppressMessages(melt(modeled));modeled$site<-I;modeled$mix<-c(rep("C",4),rep("D",4));modeled$variable<-"A"
final<-rbind(modeled,final)
}
#calculates meanvalues
final$method<-"mRNA-correcting"
final<-cbind(subset(final,mix=="C"),subset(final,mix=="D")[,2])
names(final)[6]<-"dval"
final$site<-as.factor(final$site)
levels(final$site)<-c("Lab 1 - ILM", "Lab 2 - ILM", "Lab 3 - ILM", "Lab 4 - ILM", "Lab 5 - ILM" , "Lab 6 - ILM" , "Lab 7 - LT" , "Lab 8 - LT", "Lab 9 - LT")
require(dplyr)
fmean<-group_by(final,method,site,variable)%>%summarise(mean(value),mean(dval),sd(value),sd(dval))
names(fmean)<-c("method","site","variable","meanc","meand","sdc","sdd")
g<-ggplot(final)
g+geom_point(aes(x=value,y=dval,color=(site),pch=method),alpha=0.65,size=4)+coord_cartesian(ylim=c(0.1,0.4),xlim=c(0.6,0.9))+
facet_wrap(~ site)+ylab("Amount of SEQC-A in SEQC-C")+xlab("Amount of SEQC-A in SEQC-D")+geom_point(aes(x=0.75,y=0.25),col="grey70")+theme(legend.position="none")+
geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.1,npoints=25),aes(x,y),col="grey")+
geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.05,npoints=25),aes(x,y),col="grey")+
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+theme(panel.margin=unit(1,"cm"))+theme(aspect.ratio=1)+
theme(legend.text=element_text(size=rel(1.4)))+theme(axis.title=element_text(size=rel(1.6)))+theme(axis.text=element_text(size=rel(1)))+theme(strip.background = element_rect(fill = 'white'))+theme(strip.text=element_text(size=rel(1.3)))+
geom_pointrange(data=fmean,aes(x=meanc,y=meand,ymax=meand+2*sdd,ymin=meand-2*sdd),size=1.15,shape=1)+geom_errorbarh(data=fmean,aes(x=meanc,y=meand,xmax=meanc+2*sdc,xmin=meanc-2*sdc),size=1.3)
#if axes along each facet are needed, http://stackoverflow.com/questions/17661052/force-x-axis-text-on-for-all-facets-of-a-facet-grid-plot # it's not simple though...
}#Now Figure5. SEQC-C and D estimates for interlab.
#Supplemental Figure3:
sf3<-function(){figure<-makeTargetPlot(df=makerefmetdfb(norm="UQN",mapper="RSEM",method="RSEM",readtype="mostcomplex"),
df2 = makerefmetdfb(norm="UQN",mapper="RSEM",method="RSEM",readtype = "default"),Discriminator = "FPKM",numrings = 2)
figure+scale_alpha_manual(name="Unit",breaks=c(0,1),labels=c("Count","FPKM"),values=c(0.3,1))}#Target plot: FPKM (Threw an error last time i used it...)
#Supplemental Figure4:
#Supplemental Table1:
stab1<-function(){
Figure2<-makerefmetdfb(platform = "5500",norm=0) #we use the 5500 data here because the Illumina data has some inconsistencies with the actual spike-in mix amounts. Those inconsistencies don't affect
#ratios, just the absolute values, and it's a pain to understand/explain, but the 5500 data illustrate it just fine.
count<-rbind(colSums(Figure2b2[match(ercc96$name[ercc96$pool=="C"],Figure2b2$gene_id),2:14]),colSums(Figure2b2[grep("ERCC-",Figure2b2$gene_id,invert=TRUE),2:14]))
#must only consider the C subpool of erccs here because pools A and B have designed differences between BLM1a and BLM1b (mixa having 2x as much pool a as mixb)
#I can't conceptually justify this distinction, but it is absolutely important.
#You can get the correct ratios from pool A or pool B after you account for the differences in designed spike-in amount,
#but for a reason that i don't understand, you can NOT just look at the entire set of 96 spike-ins as a whole and assume that the subpools even out (even though they were designed to do so!)
ercc.targ <- c(.08,.08/8,.08*8,.08,.08/8,.08*8,.08,.08,.08/8,.08*8,.003,.003,.003)
table<-rbind(count[1,]/count[2,],ercc.targ,ercc.targ*count[2,]/count[1,])
rownames(table)<-c("ERCC Count Ratio","ERCC spike proportion","Rho")
return(table)
}
sf2<-function(){ #stuff i did to compare mix models using by uqn/tmm to that achieved by calcmrnafrac
##for SEQC:
require(edgeR);require(ggplot2);require(data.table);require(dplyr)
seqcmixmodeling<-function(SEQCDF=SEQCDF,nmethod="TMM",factor="ercc"){
SEQCDFN<-NULL
nflist<-NULL
mflist<-NULL
for(I in levels(SEQCDF$site)){
tmp<-subset(SEQCDF,site==I)
fac<-calcNormFactors(tmp[c(3:18)],method=nmethod)
#first use the normalization factors as they were intended...
tmp[,c(3:18)]<-tmp[,c(3:18)]*fac
if(factor=="ercc"){ fac<-calcmrnafrac(tmp,selection=c(3:18),e5="G")}
if(factor=="none"){ fac<-c(rep(1,8))}
if(factor==nmethod){}
tmp$modelC1<-(fac[1]*tmp$A1*.75)+(fac[5]*tmp$B1*.25)
tmp$modelC1<-tmp$modelC1/sum(tmp$modelC1)*sum(tmp$C1)
tmp$modelC2<-(fac[2]*tmp$A2*.75)+(fac[6]*tmp$B2*.25)
tmp$modelC2<-tmp$modelC2/sum(tmp$modelC2)*sum(tmp$C2)
tmp$modelC3<-(fac[3]*tmp$A3*.75)+(fac[7]*tmp$B3*.25)
tmp$modelC3<-tmp$modelC3/sum(tmp$modelC3)*sum(tmp$C3)
tmp$modelC4<-(fac[4]*tmp$A4*.75)+(fac[8]*tmp$B4*.25)
tmp$modelC4<-tmp$modelC4/sum(tmp$modelC4)*sum(tmp$C4)
tmp$modelD1<-fac[1]*tmp$A1*.25+fac[5]*tmp$B1*.75
tmp$modelD1<-tmp$modelD1/sum(tmp$modelD1)*sum(tmp$D1)
tmp$modelD2<-fac[2]*tmp$A2*.25+fac[6]*tmp$B2*.75
tmp$modelD2<-tmp$modelD2/sum(tmp$modelD2)*sum(tmp$D2)
tmp$modelD3<-fac[3]*tmp$A3*.25+fac[7]*tmp$B3*.75
tmp$modelD3<-tmp$modelD3/sum(tmp$modelD3)*sum(tmp$D3)
tmp$modelD4<-fac[4]*tmp$A4*.25+fac[8]*tmp$B4*.75
tmp$modelD4<-tmp$modelD4/sum(tmp$modelD4)*sum(tmp$D4)
nflist<-rbind(nflist,fac)
mflist<-rbind(mflist,calcmrnafrac(tmp,selection=c(3:18),e5="G"))
SEQCDFN<-rbind(SEQCDFN,tmp)
}
return(SEQCDFN)
}
blmmixnormmodels<-function(normtype="TMM",factor="ercc"){
tmp<-makerefmetdfb()
if(normtype=="TMM"){
require(edgeR)
fac<-c(calcNormFactors(tmp[2:14],method="TMM"))#,calcNormFactors(tmp[12:14],method="TMM"))
# nfac<-c(nfac,calcNormFactors(tdf[12:14]))
for(I in 2:14){tmp[I]<-tmp[I]*fac[I-1]}
# return(tmp)
}
if(normtype=="upperquartile"|normtype=="UQN"){
require(edgeR)
fac<-c(calcNormFactors(tmp[2:11],method="upperquartile"),calcNormFactors(tmp[12:14],method="upperquartile"))
# nfac<-c(nfac,calcNormFactors(tdf[12:14]))
for(I in 2:14){tmp[I]<-tmp[I]*fac[I-1]}
# return(tmp)
}
mixFrac1<-c(.25,.25,.5);mixFrac2<-c(.25,.5,.25)
if(factor=="ercc"){fac<-calcmrnafrac(tmp)}
tmp$pred1<-(tmp$bep*mixFrac1[1]*fac[11])+(tmp$lep*mixFrac1[2]*fac[12])+(tmp$mep*mixFrac1[3]*fac[13])
tmp$pred2<-(tmp$bep*mixFrac2[1]*fac[11])+(tmp$lep*mixFrac2[2]*fac[12])+(tmp$mep*mixFrac2[3]*fac[13])
tmp$pred1<-tmp$pred1/sum(tmp$pred1)*sum(tmp$a1)
tmp$pred2<-tmp$pred2/sum(tmp$pred2)*sum(tmp$a2)
return(tmp)
}
tmm<-seqcmixmodeling(makeseqcdf(),factor="none")
uqn<-seqcmixmodeling(makeseqcdf(),nmethod="upperquartile",factor="none")
tmm$norm<-"tmm"
uqn$norm<-"uqn"
fdf<-rbind(tmm,uqn)
fdf$xm<-rowMeans(fdf[,c("D1","D2","D3","D4")])
fdf$Mxm<-rowMeans(fdf[,c("modelD1","modelD2","modelD3","modelD4")])
fdf$dset<-"SEQC"
tmm<-blmmixnormmodels(factor="none")
uqn<-blmmixnormmodels(normtype="upperquartile",factor="none")
tmm$norm<-"tmm"
uqn$norm<-"uqn"
mdf<-rbind(tmm,uqn)
mdf$dset<-"BLM"
mdf$xm<-mdf$a1
mdf$Mxm<-mdf$pred1
odf<-rbind(fdf[,c("Mxm","xm","norm","dset")],mdf[,c("Mxm","xm","norm","dset")])
#none<-blmmixnormmodels()
g<-ggplot(summarise(group_by(odf,norm,dset),rmsd=sqrt(mean((Mxm-xm)^2))))
fig<-g+geom_bar(aes(x=norm,y=rmsd),stat="identity")+facet_wrap(~dset)
return(fig)
}#figure at least demonstrates the gist of what i'm going for here...
#Reviewer-Suggested Figures:
RevS1<-function(){
#dispersion:
g<-ggplot(subset(Figure2,identity!="external"&putativeoutlier!=1));theme_set(theme_bw(base_size=18));g+geom_point(aes(x=log2(a1),y=cv))+xlab("mean expression")
#linearity
g+geom_point(aes(x=log2(a1),y=log2(pred1*1603/1374)))+xlab("log counts (observed)")+ylab("log counts(predicted)")
#deviation from linearity
g+geom_point(aes(x=log2(a1),y=log2(pred1*1603/1374)-log2(a1)))+xlab("log counts (observed)")+ylab("deviation from linearity (predicted - observed)")
#note: the 1603/1374 term is simply because the sum of predicted counts isn't equal to the sum of observed counts - it's just a normalizing term for (predicted) library size
}#"Rich plot of the linearity, dispersion, etc"
####Actual code to do stuff. Called by Figure Functions####
circleFun<-function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}#Generates dataframes of npoints points that form a circle centered at point center.
makerefmetdfb<-function(platform="ILLUMINA",mapper="TH",method="HTS",reference="UCSC",readtype="default",norm=0,reps=1,...){
#goal is to make this updated to use fread rather than fread because hella slow...problems: Also quite a pain in the ass...
require(data.table)
{ string<-paste(platform,mapper,method,reference,sep="/")
a1<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM1a.ct",sep="")
a1d<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM1aD.ct",sep="")
a1u<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM1aU.ct",sep="")
b1<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM1b.ct",sep="")
b1d<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM1bD.ct",sep="")
b1u<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM1bU.ct",sep="")
a2<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM2a.ct",sep="")
b2<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM2b.ct",sep="")
b2d<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM2bD.ct",sep="")
b2u<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BLM2bU.ct",sep="")
bep<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BEP.ct",sep="")
lep<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/LEP.ct",sep="")
mep<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/MEP.ct",sep="")
clct<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/CL.ct",sep="")
bct<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/BCT.ct",sep="")
lct<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/LCT.ct",sep="")
mct<-paste("/Volumes/Archive2b/RawData/BLM/Countdata/",string,"/MCT.ct",sep="")
if(file.exists(b1)==FALSE){if(file.exists(clct)==FALSE){return(0)}}##check to see if there is actually data to collect. If not, don't throw an error. How massive!
if(length(grep("HTS",method))==1){
int<-fread(a1,sep="\t",select=1)
a1<-rowSums(fread(a1,sep="\t",drop=1))
a1d<-rowSums(fread(a1d,sep="\t",drop=1))
a1u<-rowSums(fread(a1u,sep="\t",drop=1))
b1<-rowSums(fread(b1,sep="\t",drop=1))
b1d<-rowSums(fread(b1d,sep="\t",drop=1))
b1u<-rowSums(fread(b1u,sep="\t",drop=1))
a2<-rowSums(fread(a2,sep="\t",drop=1))
b2<-rowSums(fread(b2,sep="\t",drop=1))
b2d<-rowSums(fread(b2d,sep="\t",drop=1))
b2u<-rowSums(fread(b2u,sep="\t",drop=1))
bep<-rowSums(fread(bep,sep="\t",drop=1))
lep<-rowSums(fread(lep,sep="\t",drop=1))
mep<-rowSums(fread(mep,sep="\t",drop=1))
}#some specific bits to make input from hts
else if(length(grep("LS",method))==1){
if(platform=="5500"){
int<-(fread(a1,sep="\t",select=1))
a1<-fread(a1,sep="\t",select=6)#6 is count, 8 is RPKM
a1d<-(fread(a1d,sep="\t",select=6))
a1u<-(fread(a1u,sep="\t",select=6))
b1<-(fread(b1,sep="\t",select=6))
b1d<-(fread(b1d,sep="\t",select=6))
b1u<-(fread(b1u,sep="\t",select=6))
a2<-(fread(a2,sep="\t",select=6))
b2<-(fread(b2,sep="\t",select=6))
b2d<-(fread(b2d,sep="\t",select=6))
b2u<-(fread(b2u,sep="\t",select=6))
bep<-(fread(bep,sep="\t",select=6))
lep<-(fread(lep,sep="\t",select=6))
mep<-(fread(mep,sep="\t",select=6))
}
else if (platform=="SOLID4"){
int<-fread(a1,select=1)
a1<-rowSums(fread(a1)[2:3])
a1d<-rowSums(fread(a1d)[2:3])
a1u<-rowSums(fread(a1u)[2:3])
b1<-rowSums(fread(b1)[2:3])
b1d<-rowSums(fread(b1d)[2:3])
b1u<-rowSums(fread(b1u)[2:3])
a2<-rowSums(fread(a2)[2:3])
b2<-rowSums(fread(b2)[2:3])
b2d<-rowSums(fread(b2d)[2:3])
b2u<-rowSums(fread(b2u)[2:3])
bep<-rowSums(fread(bep)[2:3])
lep<-rowSums(fread(lep)[2:3])
mep<-rowSums(fread(mep)[2:3])
}
}# LS input
else if (method=="CUFF"){
if(reps==1){
int<-(fread(a1,sep="\t",select=1))
a1<-(fread(a1,sep="\t",header=FALSE,select=8))
a1d<-(fread(a1d,sep="\t",header=FALSE,select=8))
a1u<-(fread(a1u,sep="\t",header=FALSE,select=8))
b1<-(fread(b1,sep="\t",header=FALSE,select=8))
b1d<-(fread(b1d,sep="\t",header=FALSE,select=8))
b1u<-(fread(b1u,sep="\t",header=FALSE,select=8))
a2<-(fread(a2,sep="\t",header=FALSE,select=8))
b2<-(fread(b2,sep="\t",header=FALSE,select=8))
b2d<-(fread(b2d,sep="\t",header=FALSE,select=8)) #this one might need to switch to 8 depending on how i set it up....instead use reversecuffdiffoutput.pl
b2u<-(fread(b2u,sep="\t",header=FALSE,select=8)) #if so, this one should be back to 7, probably...
bep<-(fread(bep,sep="\t",header=FALSE,select=8))
lep<-(fread(lep,sep="\t",header=FALSE,select=8))
mep<-(fread(mep,sep="\t",header=FALSE,select=8)) #same with this one
}
else if(reps==0){
int<-(fread(a1,sep="\t",select=1))
a1<-(fread(a1,sep="\t",header=FALSE,select=8))
a1d<-(fread(a1d,sep="\t",header=FALSE,select=8))
a1u<-(fread(a1u,sep="\t",header=FALSE,select=8))
b1<-(fread(b1,sep="\t",header=FALSE,select=8))
b1d<-(fread(b1d,sep="\t",header=FALSE,select=8))
b1u<-(fread(b1u,sep="\t",header=FALSE,select=8))
a2<-(fread(a2,sep="\t",header=FALSE,select=8))
b2<-(fread(b2,sep="\t",header=FALSE,select=8))
b2d<-(fread(b2d,sep="\t",header=FALSE,select=8)) #this one might need to switch to 8 depending on how i set it up....instead use reversecuffdiffoutput.pl
b2u<-(fread(b2u,sep="\t",header=FALSE,select=8)) #if so, this one should be back to 7, probably...
bep<-(fread(bep,sep="\t",header=FALSE,select=8))
lep<-(fread(lep,sep="\t",header=FALSE,select=8))
mep<-(fread(mep,sep="\t",header=FALSE,select=8)) #same with this one
}
}#more specific bits for CUFF
else if (method=="CUFFtx"|method=="CUFFTX"){
if(reps==1){
int<-(fread(a1,sep="\t",select=1))
a1<-(fread(a1,sep="\t",header=FALSE,select=8))
a1d<-(fread(a1d,sep="\t",header=FALSE,select=8))
a1u<-(fread(a1u,sep="\t",header=FALSE,select=8))
b1<-(fread(b1,sep="\t",header=FALSE,select=8))
b1d<-(fread(b1d,sep="\t",header=FALSE,select=8))
b1u<-(fread(b1u,sep="\t",header=FALSE,select=8))
a2<-(fread(a2,sep="\t",header=FALSE,select=8))
b2<-(fread(b2,sep="\t",header=FALSE,select=8))
b2d<-(fread(b2d,sep="\t",header=FALSE,select=8)) #this one might need to switch to 8 depending on how i set it up....instead use reversecuffdiffoutput.pl
b2u<-(fread(b2u,sep="\t",header=FALSE,select=8)) #if so, this one should be back to 7, probably...
bep<-(fread(bep,sep="\t",header=FALSE,select=8))
lep<-(fread(lep,sep="\t",header=FALSE,select=8))
mep<-(fread(mep,sep="\t",header=FALSE,select=8)) #same with this one
}
else if(reps==0){
int<-(fread(a1,sep="\t",select=1))
a1<-(fread(a1,sep="\t",header=FALSE,select=8))
a1d<-(fread(a1d,sep="\t",header=FALSE,select=8))
a1u<-(fread(a1u,sep="\t",header=FALSE,select=8))
b1<-(fread(b1,sep="\t",header=FALSE,select=8))
b1d<-(fread(b1d,sep="\t",header=FALSE,select=8))
b1u<-(fread(b1u,sep="\t",header=FALSE,select=8))
a2<-(fread(a2,sep="\t",header=FALSE,select=8))
b2<-(fread(b2,sep="\t",header=FALSE,select=8))
b2d<-(fread(b2d,sep="\t",header=FALSE,select=8)) #this one might need to switch to 8 depending on how i set it up....instead use reversecuffdiffoutput.pl
b2u<-(fread(b2u,sep="\t",header=FALSE,select=8)) #if so, this one should be back to 7, probably...
bep<-(fread(bep,sep="\t",header=FALSE,select=8))
lep<-(fread(lep,sep="\t",header=FALSE,select=8))
mep<-(fread(mep,sep="\t",header=FALSE,select=8)) #same with this one
}
}#more specific bits for CUFF
else if(method=="CLCT"){
if(mapper=="LS"){
a1<-(fread(clct,sep="\t",header=TRUE,select=1))
b1<-(fread(clct,sep="\t",header=TRUE,select=16))
a2<-(fread(clct,sep="\t",header=TRUE,select=31))
b2<-(fread(clct,sep="\t",header=TRUE,select=36))
b2d<-(fread(clct,sep="\t",header=TRUE,select=46))
a1d<-(fread(clct,sep="\t",header=TRUE,select=11))
b1d<-(fread(clct,sep="\t",header=TRUE,select=26))
a1u<-(fread(clct,sep="\t",header=TRUE,select=6))
b1u<-(fread(clct,sep="\t",header=TRUE,select=21))
b2u<-(fread(clct,sep="\t",header=TRUE,select=41))
bep<-fread(bct,sep="\t",header=TRUE,select=1)
lep<-fread(lct,sep="\t",header=TRUE,select=6)
mep<-fread(mct,sep="\t",header=TRUE,select=11)
#idk what int should be here...do i have to recalibrate everything?
}
else if(mapper=="TH"){
if(platform=="ILLUMINA"){
a1<-(fread(clct,sep="\t",header=TRUE,select=1))
b1<-(fread(clct,sep="\t",header=TRUE,select=6))
a2<-(fread(clct,sep="\t",header=TRUE,select=11))
b2<-(fread(clct,sep="\t",header=TRUE,select=16))
b2d<-(fread(clct,sep="\t",header=TRUE,select=21))
a1d<-(fread(clct,sep="\t",header=TRUE,select=26))
b1d<-(fread(clct,sep="\t",header=TRUE,select=31))
a1u<-(fread(clct,sep="\t",header=TRUE,select=36))
b1u<-(fread(clct,sep="\t",header=TRUE,select=41))
b2u<-(fread(clct,sep="\t",header=TRUE,select=46))
bep<-fread(bct,sep="\t",header=TRUE,select=1)
lep<-fread(lct,sep="\t",header=TRUE,select=6)
mep<-fread(mct,sep="\t",header=TRUE,select=11)
}
else if(platform=="5500"){
a1<-(fread(clct,sep="\t",header=TRUE,select=1))
b1<-(fread(clct,sep="\t",header=TRUE,select=16))
a2<-(fread(clct,sep="\t",header=TRUE,select=31))
b2<-(fread(clct,sep="\t",header=TRUE,select=46))
b2d<-(fread(clct,sep="\t",header=TRUE,select=36))
a1d<-(fread(clct,sep="\t",header=TRUE,select=6))
b1d<-(fread(clct,sep="\t",header=TRUE,select=26))
a1u<-(fread(clct,sep="\t",header=TRUE,select=11))
b1u<-(fread(clct,sep="\t",header=TRUE,select=21))
b2u<-(fread(clct,sep="\t",header=TRUE,select=41))
bep<-fread(bct,sep="\t",header=TRUE,select=1)
lep<-fread(lct,sep="\t",header=TRUE,select=6)
mep<-fread(mct,sep="\t",header=TRUE,select=11)
}
else if(platform=="SOLID4"){
a1<-(fread(clct,sep="\t",header=TRUE,select=1))
a1d<-(fread(clct,sep="\t",header=TRUE,select=6))
a1u<-(fread(clct,sep="\t",header=TRUE,select=11))
b1<-(fread(clct,sep="\t",header=TRUE,select=16))
b1d<-(fread(clct,sep="\t",header=TRUE,select=21))
b1u<-(fread(clct,sep="\t",header=TRUE,select=26))
a2<-(fread(clct,sep="\t",header=TRUE,select=31))
b2<-(fread(clct,sep="\t",header=TRUE,select=36))
b2d<-(fread(clct,sep="\t",header=TRUE,select=41))
b2u<-(fread(clct,sep="\t",header=TRUE,select=46))
bep<-fread(bct,sep="\t",header=TRUE,select=1)
lep<-fread(lct,sep="\t",header=TRUE,select=6)
mep<-fread(mct,sep="\t",header=TRUE,select=11)
}
}
}
else if(method=="SAIL"){
if(readtype=="default"){a1t<-a1
a1<-(fread(a1,sep="\t",select=3))###3 is TPM, 4 is FPKM
a1d<-(fread(a1d,sep="\t",select=3))
a1u<-(fread(a1u,sep="\t",select=3))
b1<-(fread(b1,sep="\t",select=3))
b1d<-(fread(b1d,sep="\t",select=3))
b1u<-(fread(b1u,sep="\t",select=3))
a2<-(fread(a2,sep="\t",select=3))
b2<-(fread(b2,sep="\t",select=3))
b2d<-(fread(b2d,sep="\t",select=3))
b2u<-(fread(b2u,sep="\t",select=3))
bep<-(fread(bep,sep="\t",select=3))
lep<-(fread(lep,sep="\t",select=3))
mep<-(fread(mep,sep="\t",select=3))
int<-(fread(a1t,sep="\t",select=1))
}
else if(readtype=="mostcomplex"){a1t<-a1
a1<-(fread(a1,sep="\t",select=4))###3 is TPM, 4 is FPKM
a1d<-(fread(a1d,sep="\t",select=4))
a1u<-(fread(a1u,sep="\t",select=4))
b1<-(fread(b1,sep="\t",select=4))
b1d<-(fread(b1d,sep="\t",select=4))
b1u<-(fread(b1u,sep="\t",select=4))
a2<-(fread(a2,sep="\t",select=4))
b2<-(fread(b2,sep="\t",select=4))
b2d<-(fread(b2d,sep="\t",select=4))
b2u<-(fread(b2u,sep="\t",select=4))
bep<-(fread(bep,sep="\t",select=4))
lep<-(fread(lep,sep="\t",select=4))
mep<-(fread(mep,sep="\t",select=4))
int<-(fread(a1t,sep="\t",select=1))
}
}
else if (method=="RSEM"){
if(readtype=="default"){a1t<-a1
a1<-(fread(a1,sep="\t",select=7))###5 is ExpectedCount, 6 is TPM, 7 is FPKM
a1d<-(fread(a1d,sep="\t",select=7))
a1u<-(fread(a1u,sep="\t",select=7))
b1<-(fread(b1,sep="\t",select=7))
b1d<-(fread(b1d,sep="\t",select=7))
b1u<-(fread(b1u,sep="\t",select=7))
a2<-(fread(a2,sep="\t",select=7))
b2<-(fread(b2,sep="\t",select=7))
b2d<-(fread(b2d,sep="\t",select=7))
b2u<-(fread(b2u,sep="\t",select=7))
bep<-(fread(bep,sep="\t",select=7))
lep<-(fread(lep,sep="\t",select=7))
mep<-(fread(mep,sep="\t",select=7))
int<-(fread(a1t,sep="\t",select=1))
}
if(readtype=="mostcomplex"){
a1t<-a1
a1<-(fread(a1,sep="\t",select=6))###5 is ExpectedCount, 6 is TPM, 7 is FPKM
a1d<-(fread(a1d,sep="\t",select=6))
a1u<-(fread(a1u,sep="\t",select=6))
b1<-(fread(b1,sep="\t",select=6))
b1d<-(fread(b1d,sep="\t",select=6))
b1u<-(fread(b1u,sep="\t",select=6))
a2<-(fread(a2,sep="\t",select=6))
b2<-(fread(b2,sep="\t",select=6))
b2d<-(fread(b2d,sep="\t",select=6))
b2u<-(fread(b2u,sep="\t",select=6))
bep<-(fread(bep,sep="\t",select=6))
lep<-(fread(lep,sep="\t",select=6))
mep<-(fread(mep,sep="\t",select=6))
int<-(fread(a1t,sep="\t",select=1))
}
if(readtype=="mostbasic"){
a1t<-a1
a1<-(fread(a1,sep="\t",select=5))###5 is ExpectedCount, 6 is TPM, 7 is FPKM
a1d<-(fread(a1d,sep="\t",select=5))
a1u<-(fread(a1u,sep="\t",select=5))
b1<-(fread(b1,sep="\t",select=5))
b1d<-(fread(b1d,sep="\t",select=5))
b1u<-(fread(b1u,sep="\t",select=5))
a2<-(fread(a2,sep="\t",select=5))
b2<-(fread(b2,sep="\t",select=5))
b2d<-(fread(b2d,sep="\t",select=5))
b2u<-(fread(b2u,sep="\t",select=5))
bep<-(fread(bep,sep="\t",select=5))
lep<-(fread(lep,sep="\t",select=5))
mep<-(fread(mep,sep="\t",select=5))
int<-(fread(a1t,sep="\t",select=1))
}
}#RSEM input
else if (method=="RSEMc"){
a1t<-a1
a1<-(fread(a1,header=TRUE,sep="\t",select=5))###5 is ExpectedCount, 6 is TPM, 7 is FPKM
a1d<-(fread(a1d,header=TRUE,sep="\t",select=5))
a1u<-(fread(a1u,header=TRUE,sep="\t",select=5))
b1<-(fread(b1,header=TRUE,sep="\t",select=5))
b1d<-(fread(b1d,header=TRUE,sep="\t",select=5))
b1u<-(fread(b1u,header=TRUE,sep="\t",select=5))
a2<-(fread(a2,header=TRUE,sep="\t",select=5))
b2<-(fread(b2,header=TRUE,sep="\t",select=5))
b2d<-(fread(b2d,header=TRUE,sep="\t",select=5))
b2u<-(fread(b2u,header=TRUE,sep="\t",select=5))
bep<-(fread(bep,header=TRUE,sep="\t",select=5))
lep<-(fread(lep,header=TRUE,sep="\t",select=5))
mep<-(fread(mep,header=TRUE,sep="\t",select=5))
int<-(fread(a1t,header=TRUE,sep="\t",select=1))
int2<-(fread(a1t,header=TRUE,sep="\t",select=2))
}
else if (method=="RSEMt"){
if(readtype=="default"){
a1t<-a1
a1<-(fread(a1,sep="\t",header=TRUE,select=5))###5 is ExpectedCount, 6 is TPM, 7 is FPKM
a1d<-(fread(a1d,sep="\t",header=TRUE,select=5))
a1u<-(fread(a1u,sep="\t",header=TRUE,select=5))
b1<-(fread(b1,sep="\t",header=TRUE,select=5))
b1d<-(fread(b1d,sep="\t",header=TRUE,select=5))
b1u<-(fread(b1u,sep="\t",header=TRUE,select=5))
a2<-(fread(a2,sep="\t",header=TRUE,select=5))
b2<-(fread(b2,sep="\t",header=TRUE,select=5))
b2d<-(fread(b2d,sep="\t",header=TRUE,select=5))
b2u<-(fread(b2u,sep="\t",header=TRUE,select=5))
bep<-(fread(bep,sep="\t",header=TRUE,select=5))
lep<-(fread(lep,sep="\t",header=TRUE,select=5))
mep<-(fread(mep,sep="\t",header=TRUE,select=5))
int<-(fread(a1t,sep="\t",header=TRUE,select=1))
int2<-(fread(a1t,sep="\t",header=TRUE,select=2))
}
else if(readtype=="mostbasic"){
a1t<-a1
a1<-(fread(a1,sep="\t",header=TRUE,select=6))###5 is ExpectedCount, 6 is TPM, 7 is FPKM
a1d<-(fread(a1d,sep="\t",header=TRUE,select=6))
a1u<-(fread(a1u,sep="\t",header=TRUE,select=6))
b1<-(fread(b1,sep="\t",header=TRUE,select=6))
b1d<-(fread(b1d,sep="\t",header=TRUE,select=6))
b1u<-(fread(b1u,sep="\t",header=TRUE,select=6))
a2<-(fread(a2,sep="\t",header=TRUE,select=6))
b2<-(fread(b2,sep="\t",header=TRUE,select=6))
b2d<-(fread(b2d,sep="\t",header=TRUE,select=6))
b2u<-(fread(b2u,sep="\t",header=TRUE,select=6))
bep<-(fread(bep,sep="\t",header=TRUE,select=6))
lep<-(fread(lep,sep="\t",header=TRUE,select=6))
mep<-(fread(mep,sep="\t",header=TRUE,select=6))
int<-(fread(a1t,sep="\t",header=TRUE,select=1))
int2<-(fread(a1t,sep="\t",header=TRUE,select=2))
}
if(readtype=="mostcomplex"){
a1t<-a1
a1<-(fread(a1,sep="\t",header=TRUE,select=7))###5 is ExpectedCount, 6 is TPM, 7 is FPKM
a1d<-(fread(a1d,sep="\t",header=TRUE,select=7))
a1u<-(fread(a1u,sep="\t",header=TRUE,select=7))
b1<-(fread(b1,sep="\t",header=TRUE,select=7))
b1d<-(fread(b1d,sep="\t",header=TRUE,select=7))
b1u<-(fread(b1u,sep="\t",header=TRUE,select=7))
a2<-(fread(a2,sep="\t",header=TRUE,select=7))
b2<-(fread(b2,sep="\t",header=TRUE,select=7))
b2d<-(fread(b2d,sep="\t",header=TRUE,select=7))
b2u<-(fread(b2u,sep="\t",header=TRUE,select=7))
bep<-(fread(bep,sep="\t",header=TRUE,select=7))
lep<-(fread(lep,sep="\t",header=TRUE,select=7))
mep<-(fread(mep,sep="\t",header=TRUE,select=7))
int<-(fread(a1t,sep="\t",header=TRUE,select=1))
int2<-(fread(a1t,sep="\t",header=TRUE,select=2))
}
}#RSEM transcriptome input
else if (method=="xpress"|method=="xpress2"|method=="xpress3"){
if(readtype=="default"){
a1t<-a1
a1<-(fread(a1,sep="\t",header=TRUE,select=8))###7 is expected counts, 8 is effective counts
a1d<-(fread(a1d,sep="\t",header=TRUE,select=8))
a1u<-(fread(a1u,sep="\t",header=TRUE,select=8))
b1<-(fread(b1,sep="\t",header=TRUE,select=8))
b1d<-(fread(b1d,sep="\t",header=TRUE,select=8))
b1u<-(fread(b1u,sep="\t",header=TRUE,select=8))
a2<-(fread(a2,sep="\t",header=TRUE,select=8))
b2<-(fread(b2,sep="\t",header=TRUE,select=8))
b2d<-(fread(b2d,sep="\t",header=TRUE,select=8))
b2u<-(fread(b2u,sep="\t",header=TRUE,select=8))
bep<-(fread(bep,sep="\t",header=TRUE,select=8))
lep<-(fread(lep,sep="\t",header=TRUE,select=8))
mep<-(fread(mep,sep="\t",header=TRUE,select=8))
int<-(fread(a1t,sep="\t",header=TRUE,select=2)) #1 is txid, 2 is gene_id
}
else if(readtype=="mostbasic"){
a1t<-a1
a1<-(fread(a1,sep="\t",header=TRUE,select=7))###7 is expected counts, 8 is effective counts
a1d<-(fread(a1d,sep="\t",header=TRUE,select=7))
a1u<-(fread(a1u,sep="\t",header=TRUE,select=7))
b1<-(fread(b1,sep="\t",header=TRUE,select=7))
b1d<-(fread(b1d,sep="\t",header=TRUE,select=7))
b1u<-(fread(b1u,sep="\t",header=TRUE,select=7))
a2<-(fread(a2,sep="\t",header=TRUE,select=7))
b2<-(fread(b2,sep="\t",header=TRUE,select=7))
b2d<-(fread(b2d,sep="\t",header=TRUE,select=7))
b2u<-(fread(b2u,sep="\t",header=TRUE,select=7))
bep<-(fread(bep,sep="\t",header=TRUE,select=7))
lep<-(fread(lep,sep="\t",header=TRUE,select=7))
mep<-(fread(mep,sep="\t",header=TRUE,select=7))
int<-(fread(a1t,sep="\t",header=TRUE,select=2)) #1 is txid, 2 is gene_id
}
else if(readtype=="mostcomplex"){
a1t<-a1
a1<-(fread(a1,sep="\t",header=TRUE,select=11))###7 is expected counts, 8 is effective counts
a1d<-(fread(a1d,sep="\t",header=TRUE,select=11))
a1u<-(fread(a1u,sep="\t",header=TRUE,select=11))
b1<-(fread(b1,sep="\t",header=TRUE,select=11))
b1d<-(fread(b1d,sep="\t",header=TRUE,select=11))
b1u<-(fread(b1u,sep="\t",header=TRUE,select=11))
a2<-(fread(a2,sep="\t",header=TRUE,select=11))
b2<-(fread(b2,sep="\t",header=TRUE,select=11))
b2d<-(fread(b2d,sep="\t",header=TRUE,select=11))
b2u<-(fread(b2u,sep="\t",header=TRUE,select=11))
bep<-(fread(bep,sep="\t",header=TRUE,select=11))
lep<-(fread(lep,sep="\t",header=TRUE,select=11))
mep<-(fread(mep,sep="\t",header=TRUE,select=11))
int<-(fread(a1t,sep="\t",header=TRUE,select=2)) #1 is txid, 2 is gene_id
}
}#xpress transcriptome input
if(length(names(a1))>1000){
tdf<-data.frame(names(a1),a1,a1d,a1u,b1,b1d,b1u,a2,b2,b2d,b2u,bep,lep,mep)} #ensures naming accuracy
else if(length(grep("RSEM|LS|RSEMt|xpress|SAIL|CUFF",method))>0){
tdf<-data.frame(int,a1,a1d,a1u,b1,b1d,b1u,a2,b2,b2d,b2u,bep,lep,mep)} #reverts from separate RSEM naming convention to 'standard'
else if(length(grep("RSEMt|RSEMc",method))>0){
tdf<-data.frame(int,a1,a1d,a1u,b1,b1d,b1u,a2,b2,b2d,b2u,bep,lep,mep,int2)} #reverts from separate RSEM naming convention to 'standard'}
else{tdf<-data.frame(int,a1,a1d,a1u,b1,b1d,b1u,a2,b2,b2d,b2u,bep,lep,mep)} #standard naming
if(length(tdf)==14){
colnames(tdf)<-c("gene_id","a1","a1d","a1u","b1","b1d","b1u","a2","b2","b2d","b2u","bep","lep","mep")}
else if(length(tdf)==15){ colnames(tdf)<-c("gene_id","a1","a1d","a1u","b1","b1d","b1u","a2","b2","b2d","b2u","bep","lep","mep","geneid")}
}
#read in and name count data from output files
{tdf<-tdf[grep("unknown|no_feature|ambiguous|too_low_aQual|not_aligned|alignment_not_unique",tdf$gene_id,invert=TRUE),]
tdf$norm<-norm
if(norm>0){tdf<-normcountdf(tdf,norm)}#do norm if norm==1
#tdf$mean1<-rowMeans(data.frame(tdf$a1,tdf$a1d,tdf$a1u,tdf$b1,tdf$b1d,tdf$b1u))
#tdf$mean2<-rowMeans(data.frame(tdf$a2,tdf$b2,tdf$b2d,tdf$b2u))
#tdf$obsM<-log2((tdf$mean1)/(tdf$mean2))
#tdf$obsM[is.na(tdf$obsM)]<-0
#tdf$obsM[tdf$obsM==Inf]<-0
#tdf$obsM[tdf$obsM==-Inf]<-0
#tdf$obsA<-log2(tdf$mean1*tdf$mean2)/2
#tdf$obsA[tdf$obsA==-Inf]<-0;tdf$obsA[is.na(tdf$obsA)]<-0
# tdf$obsM2<-log2(round(tdf$mean1)/round(tdf$mean2))
#tdf$expM<-log2((.25*tdf$bep+.5*tdf$mep+.25*tdf$lep)/(.25*tdf$bep+.5*tdf$lep+.25*tdf$mep))
# tdf$expM[is.na(tdf$expM)]<-0
# tdf$expM[tdf$expM==Inf]<-0
# tdf$expM[tdf$expM==-Inf]<-0
# act<-calculateexpectedmrnafractions(tdf)
#return(act) ##comment this line out when i'm through today (06/19/13)
# tdf$expM2<-log2((act[1]*tdf$bep+act[2]*tdf$mep+act[3]*tdf$lep)/(act[4]*tdf$bep+act[5]*tdf$lep+act[6]*tdf$mep))
# tdf$expM2[is.na(tdf$expM2)]<-0
# tdf$expM2[tdf$expM2==Inf]<-0
# tdf$expM2[tdf$expM2==-Inf]<-0
# tdf$expM3<-log2((act[1]*tdf$bep+act[2]*tdf$lep+act[3]*tdf$mep)/(act[4]*tdf$bep+act[5]*tdf$lep+act[6]*tdf$mep))
# tdf$expM3[is.na(tdf$expM3)]<-0
# tdf$expM3[tdf$expM3==Inf]<-0
# tdf$expM3[tdf$expM3==-Inf]<-0
# tdf$eM1<-((act[1]*tdf$bep+act[2]*tdf$lep+act[3]*tdf$mep))
# tdf$eM2<-((act[4]*tdf$bep+act[5]*tdf$lep+act[6]*tdf$mep))
# tdf$reference<-reference
#tdf$platform<-platform
#tdf$mapper<-mapper
#tdf$method<-method
tdf$uid<-paste(platform,mapper,method,reference,sep="")
tdf[tdf==Inf]<-NA
tdf[tdf==-Inf]<-NA
#tdf$cutoff<-summary(tdf$obsA[tdf$obsA>0])[3]
#trying to just do it all at once:
}#remove HTS_unknown features ; run normcountdf ; calculate means/MA, add metadata, change Inf to NA
#tdf$var1<-(((tdf$a1-tdf$mean1)^2+(tdf$a1d-tdf$mean1)^2+(tdf$a1u-tdf$mean1)^2+(tdf$b1-tdf$mean1)^2+(tdf$b1u-tdf$mean1)^2+(tdf$b1d-tdf$mean1)^2)*1/5)
#tdf$var2<-(((tdf$a2-tdf$mean2)^2+(tdf$b2-tdf$mean2)^2+(tdf$b2u-tdf$mean2)^2+(tdf$b2d-tdf$mean2)^2)*1/3)
#tdf<-predictmixfromblm(tdf)
#tdf<-bootstrapdMranges(tdf,reps)
return(tdf)
}#reads input files (Must upload those files somewhere & change the code to read those files from the remote server)
makeTargetPlot<-function(type="BLM",df,df2,Discriminator="CheeseValue",numrings=4,dfr=FALSE){
require(reshape2)
require(ggplot2)
require(dplyr)
theme_set(theme_bw(base_size=16))
if(type=="BLM"){ a<-blmmixfraction(df);
a$mixnum<-c(1,1,1,1,1,1,2,2,2,2);
a[11,]<-c(0,0,0,2);a[12,]<-c(0,0,0,2);
a<-a[c(1:4,7:10),]#we *are* ignoring the points on the line.
a<-as.data.frame(melt(a,id.vars=4))
a<-data.frame(a[a$mixnum==1,],a[a$mixnum==2,])
colnames(a)<-c("mixnum","Component","value1","blah","blah","value2")
a<-a[,c("Component","value1","value2")]
}
if(type=="BLM"){
if(!missing(df2)){b<-blmmixfraction(df2);
b$mixnum<-c(1,1,1,1,1,1,2,2,2,2);
b[11,]<-c(0,0,0,2);b[12,]<-c(0,0,0,2);
b<-b[c(1:4,7:10),] #we *are* ignoring the points on the line.
b<-as.data.frame(melt(b,id.vars=4))
b<-data.frame(b[b$mixnum==1,],b[b$mixnum==2,])
colnames(b)<-c("mixnum","Component","value1","blah","blah","value2")
b<-b[,c("Component","value1","value2")]
#sumdf2<-data.frame(amean1=c(mean(b$value1[b$variable=="mixpropA"&b$value1>0]),mean(b$value1[b$variable=="mixpropB"&b$value1>0]),mean(b$value1[b$variable=="mixpropC"&b$value1>0])),
# amean2=c(mean(b$value2[b$variable=="mixpropA"&b$value2>0]),mean(b$value2[b$variable=="mixpropB"&b$value2>0]),mean(b$value2[b$variable=="mixpropC"&b$value2>0])),
# sd1=c(sd(b$value1[b$variable=="mixpropA"&b$value1>0]),sd(b$value1[b$variable=="mixpropB"&b$value1>0]),sd(b$value1[b$variable=="mixpropC"&b$value1>0])),
# sd2=c(sd(b$value2[b$variable=="mixpropA"&b$value2>0]),sd(b$value2[b$variable=="mixpropB"&b$value2>0]),sd(b$value2[b$variable=="mixpropC"&b$value2>0])))
sumdf2<-summarize(group_by(b,Component),amean1=mean(value1),amean2=mean(value2),sd1=sd(value1),sd2=sd(value2))
}
#sumdf<-data.frame(amean1=c(mean(a$value1[a$variable=="mixpropA"&a$value1>0]),mean(a$value1[a$variable=="mixpropB"&a$value1>0]),mean(a$value1[a$variable=="mixpropC"&a$value1>0])),
# amean2=c(mean(a$value2[a$variable=="mixpropA"&a$value2>0]),mean(a$value2[a$variable=="mixpropB"&a$value2>0]),mean(a$value2[a$variable=="mixpropC"&a$value2>0])),
# sd1=c(sd(a$value1[a$variable=="mixpropA"&a$value1>0]),sd(a$value1[a$variable=="mixpropB"&a$value1>0]),sd(a$value1[a$variable=="mixpropC"&a$value1>0])),
# sd2=c(sd(a$value2[a$variable=="mixpropA"&a$value2>0]),sd(a$value2[a$variable=="mixpropB"&a$value2>0]),sd(a$value2[a$variable=="mixpropC"&a$value2>0])))
sumdf<-summarize(group_by(a,Component),amean1=mean(value1),amean2=mean(value2),sd1=sd(value1),sd2=sd(value2))
}
#do the plotting
if(type=="SEQC"){
a<-GeneralLMest(dat,spikeID="ERCC_",components=c("A1","A2","A3","A4","B1","B2","B3","B4"),mixes=c("C1","C2","C3","C4","D1","D2","D3","D4"))
}
if(!missing(df2)&type=="SEQC"){
g<-ggplot(subset(a,sample=="C"))
g+geom_point(aes(x=Apct,y=a$Apct[a$sample=="D"],color=as.factor(LID)),alpha=0.7,size=6)+
scale_y_continuous(limits=c(0,0.5),expand=c(0,0))+scale_x_continuous(limits=c(0.5,1),expand=c(0,0))+
facet_wrap(~ LID)+ylab("Amount of SEQC-A in SEQC-C")+xlab("Amount of SEQC-A in SEQC-D")+
geom_point(aes(x=0.75,y=0.25),col="grey70")+theme(legend.position="none")+geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.1,npoints=25),aes(x,y),col="grey")+geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.05,npoints=25),aes(x,y),col="grey")+geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.15,npoints=25),aes(x,y),col="grey")+geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.2,npoints=25),aes(x,y),col="grey")+
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+theme(panel.margin=unit(1,"cm"))+
theme(legend.text=element_text(size=rel(1.4)))+theme(axis.title=element_text(size=rel(1.6)))+theme(axis.text=element_text(size=rel(1)))+
theme(strip.background = element_rect(fill = 'white'))+geom_pointrange(data=sumdf,aes(x=amean1,y=amean2,ymax=amean2+sd2,ymin=amean2-sd2))+theme(aspect.ratio=1)+
geom_errorbarh(data=sumdf,aes(x=amean1,y=amean2,xmax=amean1+sd1,xmin=amean1-sd1))+geom_point(data=subset(b,sample=="C"),aes(x=Apct,y=b$Apct[b$sample=="D"],color="red"))
}
if(type=="SEQC"){
g<-ggplot(subset(a,sample=="C"))
return(g+geom_point(aes(x=Apct,y=a$Apct[a$sample=="D"],color=as.factor(LID)),alpha=0.7,size=6)+
scale_y_continuous(limits=c(0,0.5),expand=c(0,0))+scale_x_continuous(limits=c(0.5,1),expand=c(0,0))+
facet_wrap(~ LID)+ylab("Amount of SEQC-A in SEQC-C")+xlab("Amount of SEQC-A in SEQC-D")+
geom_point(aes(x=0.75,y=0.25),col="grey70")+theme(legend.position="none")+geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.1,npoints=25),aes(x,y),col="grey")+
geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.05,npoints=25),aes(x,y),col="grey")+geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.15,npoints=25),aes(x,y),col="grey")+
geom_path(data=circleFun(center=c(0.75,0.25),diameter=0.2,npoints=25),aes(x,y),col="grey")+
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+theme(panel.margin=unit(1,"cm"))+
theme(legend.text=element_text(size=rel(1.4)))+theme(axis.title=element_text(size=rel(1.6)))+theme(axis.text=element_text(size=rel(1)))+
theme(strip.background = element_rect(fill = 'white'))+geom_pointrange(data=sumdf,aes(x=amean1,y=amean2,ymax=amean2+sd2,ymin=amean2-sd2))+
theme(aspect.ratio=1)+
geom_errorbarh(data=sumdf,aes(x=amean1,y=amean2,xmax=amean1+sd1,xmin=amean1-sd1)))
}
if(!missing(df2)&type=="BLM"){
a$mcor<-1
b$mcor<-0
pathdf<-NULL;pathdf2<-NULL;pathdf3<-NULL
for(I in 1:numrings){pathdf<-rbind(pathdf,circleFun(center=c(0.25,0.25),diameter=0.05*I,npoints=25))
pathdf2<-rbind(pathdf2,circleFun(center=c(0.25,0.5),diameter=0.05*I,npoints=25))
pathdf3<-rbind(pathdf3,circleFun(center=c(0.5,0.25),diameter=0.05*I,npoints=25))}
mergedm<-rbind(a,b)
merged<-rbind(sumdf,sumdf2)
merged$mcor<-c(1,1,1,0,0,0)
g<-ggplot(mergedm)
return(g+geom_path(data=pathdf,aes(x,y),col="grey")+geom_path(data=pathdf2,aes(x,y),col="grey")+geom_path(data=pathdf3,aes(x,y),col="grey")+
geom_point(aes(x=value1,y=value2,col=Component,alpha=as.factor(mcor)),size=5)+
xlab("Amount of Tissue in BLM-1")+ylab("Amount of Tissue in BLM-2")+geom_point(aes(x=c(0.25,0.25,.5),y=c(0.25,0.5,0.25)),col="grey70",size=3)+
theme(legend.position=c(0.6,0.7))+scale_color_manual(name="Tissue",breaks=c("mixpropA","mixpropB","mixpropC","mixpropA","mixpropB","mixpropC"),
labels=c("Brain","Liver","Muscle","Brain","Liver","Muscle"),values=rep(c("#CC6666","#99CC66","#6699CC"),2))+coord_cartesian(ylim=c(0,1),xlim=c(0,1))+
theme(axis.text=element_text(size=rel(1.3)))+theme(axis.title=element_text(size=rel(1.5)))+theme(legend.text=element_text(size=rel(1.5)))+theme(legend.title=element_text(size=rel(1.5)))+
geom_pointrange(data=merged,aes(x=amean1,y=amean2,ymax=amean2+sd2,ymin=amean2-sd2,alpha=as.factor(mcor)),size=1.15)+
geom_errorbarh(data=merged,aes(x=amean1,y=amean2,xmax=amean1+sd1,xmin=amean1-sd1,height=0,alpha=as.factor(mcor)),size=1.3)+scale_alpha_manual(name=Discriminator,breaks=c(1,0),labels=c("True","False"),values=c(0.3,1))+
theme(aspect.ratio=1)+theme(axis.ticks.margin=unit(x = 0.25,units = "cm"))+theme(axis.text=element_text(size=16)))
}
if(missing(df2)&type=="BLM"){
mergedm<-a
merged<-sumdf
g<-ggplot(mergedm)
#do fun things to add a table: this needs to go into MTP...
# dt<-summarize(group_by(mergedm,Component),Observed1=round(mean(value1),digits=3),SD1=round(sd(value1),digits=3),Observed2=round(mean(value2),digits=3),SD2=round(sd(value2),digits=3))
dt<-summarize(group_by(mergedm,Component),Observed1=round(mean(value1),digits=3),Observed2=round(mean(value2),digits=3))
levels(dt$Component)<-c("Brain","Liver","Muscle")
dt$Designed1=c(.25,.25,.5)
dt$Designed2=c(.25,.5,.25)
#dt<-dt[,c(1,2,6,3,4,7,5)]
dt<-dt[,c(1,2,4,3,5)]
#####The actual output i want for the DT table:
dt<-as.data.frame(t(dt))
colnames(dt)<-c("Brain","Liver","Muscle")
dt<-dt[2:length(dt[,1]),]
#####/stuff added 4/15####
# dt2<-summarize(dt,DistanceFromTruth=round(sqrt(sum(abs(Mix1Mean-Mix1))^2+sum(abs(Mix2Mean-Mix2))^2),digits=3),Xdistance=round(sum(abs(Mix1Mean-Mix1)),digits=3),Ydistance=round(sum(abs(Mix2Mean-Mix2)),digits=3))
require(gridExtra)
pathdf<-NULL;pathdf2<-NULL;pathdf3<-NULL
#pathdf<-"geom_path(data=circleFun(center=c(0.25,0.25),diameter=0.05,npoints=25),aes(x,y),col=\"grey\")"
#pathdf2<-"geom_path(data=circleFun(center=c(0.5,0.25),diameter=0.05,npoints=25),aes(x,y),col=\"grey\")"
#pathdf3<-"geom_path(data=circleFun(center=c(0.25,0.5),diameter=0.05,npoints=25),aes(x,y),col=\"grey\")"
for(I in 2:numrings){
#instead of actually making dfs, make a text block
# pathdf<-paste0(pathdf,"+geom_path(data=circleFun(center=c(0.25,0.25),diameter=",0.05*I,",npoints=25),aes(x,y),col=\"grey\")") # I can't make this work.
# pathdf2<-paste0(pathdf2,"+geom_path(data=circleFun(center=c(0.25,0.5),diameter=",0.05*I,",npoints=25),aes(x,y),col=\"grey\")")
# pathdf3<-paste0(pathdf3,"+geom_path(data=circleFun(center=c(0.5,0.25),diameter=",0.05*I,",npoints=25),aes(x,y),col=\"grey\")")
pathdf<-rbind(pathdf,circleFun(center=c(0.25,0.25),diameter=0.05*(I-1),npoints=25))
pathdf2<-rbind(pathdf2,circleFun(center=c(0.25,0.5),diameter=0.05*(I-1),npoints=25))
pathdf3<-rbind(pathdf3,circleFun(center=c(0.5,0.25),diameter=0.05*(I-1),npoints=25))
}
if(dfr==TRUE){return(dt)}
{#redefine tablegrob
drawDetails.table <- function (x, recording = TRUE)
{
lg <- if(!is.null(x$lg)) {
x$lg
} else {
with(x, gridExtra:::makeTableGrobs(as.character(as.matrix(d)),
rows, cols, NROW(d), NCOL(d), parse, row.just = row.just,
col.just = col.just, core.just = core.just, equal.width = equal.width,
equal.height = equal.height, gpar.coretext = gpar.coretext,
gpar.coltext = gpar.coltext, gpar.rowtext = gpar.rowtext,
h.odd.alpha = h.odd.alpha, h.even.alpha = h.even.alpha,
v.odd.alpha = v.odd.alpha, v.even.alpha = v.even.alpha,
gpar.corefill = gpar.corefill, gpar.rowfill = gpar.rowfill,
gpar.colfill = gpar.colfill))
}
widthsv <- convertUnit(lg$widths + x$padding.h, "mm", valueOnly = TRUE)
heightsv <- convertUnit(lg$heights + x$padding.v, "mm", valueOnly = TRUE)
widthsv[1] <- widthsv[1] * as.numeric(x$show.rownames)
widths <- unit(widthsv, "mm")
heightsv[1] <- heightsv[1] * as.numeric(x$show.colnames)
heights <- unit(heightsv, "mm")
cells = viewport(name = "table.cells", layout = grid.layout(lg$nrow +
1, lg$ncol + 1, widths = widths, heights = heights))
pushViewport(cells)
tg <- gridExtra:::arrangeTableGrobs(lg$lgt, lg$lgf, lg$nrow, lg$ncol,
lg$widths, lg$heights, show.colnames = x$show.colnames,
show.rownames = x$show.rownames, padding.h = x$padding.h,
padding.v = x$padding.v, separator = x$separator, show.box = x$show.box,
show.vlines = x$show.vlines, show.hlines = x$show.hlines,
show.namesep = x$show.namesep, show.csep = x$show.csep,
show.rsep = x$show.rsep)
upViewport()
}}
customtable<-tableGrob(dt)
customtable$lg$lgt[[7]]$gp$col<-"red"