-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.R
1228 lines (944 loc) · 41.4 KB
/
functions.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
QCplot <- function(object) {
#plot of counts. Note that these are raw, not normalized counts.
Dmds<-tibble(
count=colSums(object@edgeR$counts),
library_id=object@library_id,
individual=object@individual,
treatment=object@treatment
) %>% dplyr::arrange( treatment ) %>% tibble::add_column(species=rep(object@species, length(object@treatment)))
total <- ggplot( Dmds, aes( factor( library_id,levels=unique( library_id ) ) ,count ) ) +
geom_bar( stat = "identity", aes( fill=treatment ) ) +
xlab( "Samples" ) +
ylab( "Total read counts" ) +
theme( axis.text.x=element_text( angle=40, hjust=1 )) + labs(title=Dmds$species[[1]])
# plot of 0 reads
ZeroReads <- tibble(
count=colSums(object@edgeR$counts==0),
library_id=object@library_id,
individual=object@individual,
treatment=object@treatment
) %>%
dplyr::arrange( treatment ) %>%
tibble::add_column(species=rep(object@species, length(object@treatment)))
zero <- ggplot(ZeroReads, aes(factor(library_id, levels=unique(library_id)),count)) +
geom_bar(stat = "identity", aes(fill=treatment)) +
xlab("Samples") +
ylab("Total 0 counts") +
theme(axis.text.x=element_text(angle=40, hjust=1)) + labs(title=ZeroReads$species[[1]])
#plot of rRNA
rRNA <- tibble(
count=object@rRNA,
library_id=object@library_id,
individual=object@individual,
treatment=object@treatment
) %>%
dplyr::arrange( treatment ) %>%
tibble::add_column(species=rep(object@species, length(object@treatment)))
rRNA <- ggplot( rRNA, aes( factor( library_id, levels=unique( library_id ) ), count ) ) +
geom_bar( stat = "identity", aes( fill=treatment ) ) +
xlab( "Samples" ) +
ylab( "rRNA counts" ) +
theme( axis.text.x=element_text( angle=40, hjust=1 ) ) + labs(title=rRNA$species[[1]])
df <-
as.data.frame(log2(object@edgeR$counts + 1), stringsAsFactors=FALSE) %>%
tidyr::gather( everything(), key="Samples", value="value" ) %>%
dplyr::left_join(
Dmds %>%
dplyr::select( library_id, individual, treatment, species ) %>%
dplyr::mutate( library_id=as.character( library_id ) ),
by=c( "Samples" = "library_id" )
) %>%
dplyr::arrange( treatment ) %>%
dplyr::mutate( Samples = as.factor( Samples ) )
density <- ggplot(df, aes(x = value, colour = Samples, fill=Samples)) +
ylim(c(0, 0.25)) +
geom_density(alpha = 0.05, size = 0.5) +
facet_wrap(~treatment) +
theme(legend.position = "top") +
xlab(expression(log[2](count + 1)))
density_all <- ggplot(df, aes(x = value, colour = Samples, fill=Samples)) +
ylim(c(0, 0.25)) + geom_density(alpha = 0.05, size = 0.5) + theme(legend.position = "top") + xlab(expression(log[2](count + 1)))
setClass("gg")
setClass(
Class="QCPlot",
representation = representation(
total="gg",
zero="gg",
rRNA="gg",
density="gg",
density_all="gg",
Dmds="data.frame",
df="data.frame"
)
)
QCPlot <- methods::new( "QCPlot" )
QCPlot@total<-total
QCPlot@zero <- zero
QCPlot@rRNA<- rRNA
QCPlot@density<- density
QCPlot@Dmds<-as.data.frame(Dmds)
QCPlot@df<-df
QCPlot@density_all<- density_all
return(QCPlot)
}
####dge results function#####
dgeresults <- function( object ){
if( length( levels( object@individual ) ) >=2 ) {
ddsMF <- agalmar::create_DESeq2( object, design = ~individual + treatment )
###collapse technical replicates
run=paste(ddsMF$individual,ddsMF$treatment,sep="_")
if(length(unique(run))!=length(run)){
ddsMF$run = ddsMF$run=as.numeric(as.factor(run))
ddsMF <- DESeq2::collapseReplicates( ddsMF,ddsMF$run, renameCols=FALSE )
}
ddsMF <- DESeq2::DESeq( ddsMF )
#plots
plotMA1 <- function(ddsMF){
pairs <- combn(as.character(unique(ddsMF$treatment)),2)
plotMA1<-list()
for(i in seq(from=1, to=ncol(pairs))){
resulttable <- DESeq2::results( ddsMF,contrast=c( "treatment", as.character( pairs[,i] ) ), pAdjustMethod="bonferroni", alpha=0.05 )
resulttable <- as.data.frame(resulttable)
resulttable$padj[is.na(resulttable$padj)]<-1
plotMA1[[i]]<-ggplot(resulttable)+geom_point(alpha=0.8,aes(x=log(baseMean),y=log2FoldChange,colour= padj < 0.05))+scale_colour_manual(values =c("TRUE"="red","FALSE"="grey40"))+ggtitle(paste( as.character( pairs[,i]),collapse =" vs " ))
plotMA1<<-plotMA1
}
}
plotMA1(ddsMF)
#Transforming data
rld <- DESeq2::rlog( ddsMF ) #rlog transform
vsd <- DESeq2::varianceStabilizingTransformation( ddsMF ) #variance Stabilizing Transformation
notAllZero <- ( rowSums( DESeq2::counts( ddsMF ) )>0 )
#adding heatmap
select <- order( rowMeans( counts( ddsMF,normalized=TRUE ) ), decreasing=TRUE )[1:20]
nt <- DESeq2::normTransform( ddsMF )
log2.norm.counts <- assay( nt )[ select, ]
df <- as.data.frame( ddsMF@colData[ ,c( "treatment","individual" ) ] )
count_heatmap <- pheatmap::pheatmap( log2.norm.counts, cluster_rows=FALSE, show_rownames=FALSE, cluster_cols=FALSE, annotation_col=df )
rld_heatmap<- pheatmap::pheatmap( assay( rld )[select,], cluster_rows=FALSE, show_rownames=FALSE,
cluster_cols=FALSE, annotation_col=df )
vsd_heatmap<- pheatmap::pheatmap( assay( vsd )[select,], cluster_rows=FALSE, show_rownames=FALSE, cluster_cols=FALSE, annotation_col=df )
#sample distances
sampleDists <- stats::dist( t( assay( rld ) ) )
sampleDistMatrix <- as.matrix( sampleDists )
rownames( sampleDistMatrix ) <- paste( rld$treatment, rld$individual, sep="-" )
colnames( sampleDistMatrix ) <- NULL
colors <- grDevices::colorRampPalette( rev( brewer.pal( 9, "Greens" ) ) )( 255 )
distance_heatmap<- pheatmap::pheatmap( sampleDistMatrix, clustering_distance_rows=sampleDists, clustering_distance_cols=sampleDists, col=colors )
#PCA plot
data <- DESeq2::plotPCA( rld, intgroup=c("treatment", "individual"), returnData=TRUE )
percentVar <- round( 100 * attr(data, "percentVar") )
pca <- ggplot( data, aes( PC1, PC2, color=individual, shape=treatment ) ) + scale_shape_manual(values=0:length(data$treatment)) + geom_point(size=3) + xlab(paste0("PC1: ",percentVar[1],"% variance")) + ylab(paste0("PC2:",percentVar[2],"% variance"))
setClass("gg")
setClass(
Class="DGEplotresults",
representation = representation(
ddsMF="DESeqDataSet",
plotMA="list",
count_heatmap="pheatmap",
rld_heatmap="pheatmap",
vsd_heatmap="pheatmap",
distance_heatmap="pheatmap",
pca="gg",
rld="DESeqTransform",
vsd="DESeqTransform"
)
)
DGEPlots <- methods::new( "DGEplotresults" )
DGEPlots@ddsMF<-ddsMF
DGEPlots@plotMA<-plotMA1
DGEPlots@count_heatmap<-pheatmap( log2.norm.counts, cluster_rows=FALSE, show_rownames=FALSE, cluster_cols=FALSE, annotation_col=df )
DGEPlots@rld_heatmap<-pheatmap( assay( rld )[select,], cluster_rows=FALSE, show_rownames=FALSE,cluster_cols=FALSE, annotation_col=df )
DGEPlots@vsd_heatmap<-pheatmap( assay( vsd )[select,], cluster_rows=FALSE, show_rownames=FALSE, cluster_cols=FALSE, annotation_col=df )
DGEPlots@distance_heatmap<-pheatmap( sampleDistMatrix, clustering_distance_rows=sampleDists, clustering_distance_cols=sampleDists, col=colors )
DGEPlots@pca<-pca
DGEPlots@rld<-rld
DGEPlots@vsd<-vsd
return(DGEPlots)
}
}
#######tree manipulation functions ###########################
# Clone gene tree edge lengths from species tree
clone_edge_lengths <- function( nhx, node_ages, ... ) {
tags <- nhx@data
# Only consider internal nodes
tags <- tags[ is.na( tags$species ), ]
# only consider speciation events
tags <- tags[ ( tags$Ev == "S" ), ]
# Only consider nodes for which age is available
tags <- tags[ tags$S %in% names( node_ages ) , ]
ages <- node_ages[ match( tags$S, names( node_ages ) ) ]
calibration<-data.frame(
node=tags$node,
age.min=ages,
age.max=ages,
soft.bounds=rep( TRUE, nrow( tags ) )
)
# Verify the coherence of node ages, ie make sure that all calibration points are older than
# calibrations on younger nodes.
oldest_descendant <- sapply( 1:max( nhx@phylo$edge ), function( x ) {
progeny <- hutan::descendants( nhx@phylo, x )
oldest <- 0
if ( length( progeny ) > 0 ) {
indices <- match( progeny, tags$node )
if ( ! all( is.na( indices ) ) ) {
oldest <- max( ages[ indices ], na.rm = TRUE )
}
}
return( oldest )
})
ages_all <- rep( NA,max( nhx@phylo$edge ) )
ages_all[ calibration$node ] <- calibration$age.min
stopifnot( all( ages_all > oldest_descendant, na.rm=TRUE ) )
# ggtree(nhx, branch.length="none") + geom_text(aes(label=round(oldest_descendant,3)), vjust=-.5, hjust=1, size=2.5, col="darkslategray4")+ geom_text(aes(label=S), vjust=1.5, hjust=2, size=2.5, col="blue") + geom_point(aes(color=Ev)) + geom_text(aes(label=round(ages_all,3)), vjust=-.5, hjust=2, size=2.5, col="red") + geom_text(aes(label=1:max(nhx@phylo$edge)), vjust=1.5, hjust=3, size=2.5, col="green")
calibrated <- try( ape::chronos( nhx@phylo, calibration=calibration) )
# Return the try error if failed, otherwise return updated nhx object
if( "try-error" %in% class( calibrated ) ) {
return( calibrated )
}
else{
class( calibrated ) = "phylo"
nhx@phylo=calibrated
return( nhx )
}
}
# Returns a logical vector indicating which rows in nhx_tags are for tips
is.tip.nhx <- function( nhx ) {
is.tip <- rep( FALSE, nrow( nhx@data ) )
is.tip[ 1:length( nhx@phylo$tip.label ) ] = TRUE
is.tip
}
# Drops tips without expression data.
drop_empty_tips <- function( nhx, col_values ){
if ( class( nhx ) != "treedata" ) {
return( NA )
}
if (length(col_values)==1){
to_drop <-which(is.na(nhx@data[ is.tip.nhx( nhx ), ][,names(nhx@data[is.tip.nhx( nhx ), ]) == col_values]))
return( drop.tip.mod( nhx, to_drop ) )
}
if(length(col_values)>1){
if(length(which(names(nhx@data[is.tip.nhx( nhx ), ]) %in% col_values))==1){
to_drop <-which(is.na(nhx@data[ is.tip.nhx( nhx ), ][,names(nhx@data[is.tip.nhx( nhx ), ]) %in% col_values]))
return( drop.tip.mod( nhx, to_drop ) )
}
else{ to_drop <- nhx@data[ is.tip.nhx( nhx ), ][rowSums(nhx@data[ is.tip.nhx( nhx ), ][,names(nhx@data[is.tip.nhx( nhx ), ]) %in% col_values],na.rm=TRUE)==0,]$node
return( drop.tip.mod( nhx, to_drop ))
}
}
}
# Run pic and add results to nhx object
pic.nhx <- function( nhx, col_value ) {
if ( class( nhx ) != "treedata" ) {
return( NA )
}
if(col_value %in% names(nhx@data)){
p <- ape::pic( nhx@data[,names(nhx@data)==col_value][ is.tip.nhx( nhx ) ], nhx@phylo, var.contrasts=TRUE )
# Verify that the pic names match the internal node names
stopifnot( all( ( rownames( p ) == nhx@data$phy_node_names[ ! is.tip.nhx( nhx ) ] ) ) )
m<-data.frame(pic=c( rep( NA, length( nhx@phylo$tip.label ) ), p[ ,1 ] ), var_exp=c( rep( NA, length( nhx@phylo$tip.label ) ), p[ ,2 ] ))
names(m)<-c(paste("pic",col_value,sep="_"),paste("var_exp",col_value,sep="_"))
nhx@data<-dplyr::bind_cols(nhx@data,m)
return( nhx )
}
}
# Transfers pic and var_exp results from nhx1 to nhx2, where nhx1 is a subtree of nhx2
merge_pic <- function( nhx1, nhx2 ) {
if ( class( nhx2 )!="treedata" ) {
return( nhx2 )
}
sub1 <- nhx1@data[ ,names( nhx1@data ) %in% c( "ND", "pic", "var_exp" ) ]
merged <- merge( nhx2@data, sub1, all=TRUE, by="ND" )
merged <- dplyr::arrange( merged, node )
nhx2@data <- merged
return( nhx2 )
}
# put node ages in @data
store_node_age <- function( nhx ) {
if ( class( nhx ) != "treedata" ) {
return( nhx )
}
node_age <- hutan::distance_from_tip( nhx@phylo )
# make sure the dataframe is ordered by consecutive nodes
stopifnot( all( nhx@data$node == 1:length( nhx@data$node ) ) )
nhx@data$node_age <- node_age
return( nhx )
}
add_de_to_nhx <- function( nhx, combined_de ){
# Get the de rows that apply to these tips
de <- combined_de[ match( nhx@data$sequence_ids, combined_de$sequence_ids ) , ]
de$species <- NULL
de$gene_tree <- NULL
nhx@data <- dplyr::bind_cols( nhx@data, de[,2:ncol(de)] )
return(nhx)
}
# Return the first value in a vector that isn't NA
first_not_na <- function ( x ) {
x <- x[ ! is.na( x ) ]
if ( length( x ) == 0 ) {
return( NA )
}
else{
return ( x[1] )
}
}
# Calculate the phylogenetic signal on an nhx tree for the variable in a specified nhx tag
phylosignal.nhx <- function ( nhx, tag, ... ) {
if ( class( nhx ) != "treedata" ) {
return( NA )
}
#exclude tags where number of tips <3
if ( length(nhx@data[[ tag ]][!is.na(nhx@data[[ tag ]])]) < 3 ) {
return( NA )
}
x <- nhx@data[[ tag ]]
x <- x[ 1:length( nhx@phylo$tip.label ) ]
names( x ) <- nhx@phylo$tip.label
return ( phytools::phylosig( nhx@phylo, x, ... ) )
}
##from https://bitbucket.org/caseywdunn/sicb2013/
regularization_by_thresholding <- function ( cor_matrix, n ) {
# Regularization by thresholding
# Bickel, P. J. & Levina, E. Covariance regularization by thresholding.
# Ann. Statist. 36, 2577–2604 (2008). http://dx.doi.org/10.1214/08-AOS600
p <- ncol( cor_matrix )
regularized <- cor_matrix * ( abs(cor_matrix) > sqrt(log(p)/n) )
return( regularized )
}
short_pairs<-function(pair){
short<- pair%>%
stringr::str_split(.," ")%>%
unlist() %>%
stringr::str_sub(.,1,3) %>%
stringr::str_c(.,collapse="")
return(short)
}
short_pairs_tpm<-function(pair){
short<- pair%>%
stringr::str_c(.,collapse="")
return(short)
}
#Function to get DGE results for every pair
resultsdge <- function (object,pair) {
treat1<-pair[1]
treat2<-pair[2]
if("CMN0040" %in% colnames(object@ddsMF) & treat1=="Gastrozooid mature")
{
treat1<-"Gastrozooid white mature"
}
else if("CMN0040" %in% colnames(object@ddsMF) & treat2=="Gastrozooid mature"){
treat2<-"Gastrozooid white mature"
}
#check that the two treatments are present, and that there are more than two replicates
if( (length(unique(object@ddsMF@colData$treatment)[unique(object@ddsMF@colData$treatment) %in% c(treat1,treat2) ==TRUE])==2)&((length(object@ddsMF@colData$treatment[object@ddsMF@colData$treatment==treat1])>=2)&(length(object@ddsMF@colData$treatment[object@ddsMF@colData$treatment==treat2])>=2))) {
short<-short_pairs(c(treat1,treat2))
result <- data.frame()
res<-DESeq2::results(object@ddsMF, contrast=c("treatment", treat1, treat2)) %>% #write results to res
as.data.frame
names(res)<- paste(names(res), short ,sep="_")
res<-tibble::rownames_to_column(res, var="sequence_ids")
res$sequence_ids<-as.numeric(res$sequence_ids)
result<-res
return(result)
}
if((length(unique(object@ddsMF@colData$treatment)[unique(object@ddsMF@colData$treatment) %in% c(treat1,treat2) ==TRUE])<=1)){
return(NA)
}
if((length(object@ddsMF@colData$treatment[object@ddsMF@colData$treatment==treat1])<=1)|(length(object@ddsMF@colData$treatment[object@ddsMF@colData$treatment==treat2])<=1)){
return(NA)
}
}
#pull out DGE results for every pair
pairsdge<- function(object){
y<-list()
for(i in 1:ncol(pairs)){
res<-resultsdge(object,pairs[,i])
y[[i]] <- res
}
return(y)
}
sort_by_pair<-function(object){
m<-list()
for(i in 1:length(pairs[1,])){
m[[i]]<-lapply(object, extract, i) %>% bind_rows(.)
}
return(m)
}
extract<-function(object,i){
if(!is.data.frame(object[[i]])){
return(NULL)
}
else{
return(object[[i]])
}
}
#pull out log tpm values
tpm_output<-function(object, treatment){
if(object@species=="Bargmannia elongata" & treatment=="Gastrozooid mature"){
treatment="Gastrozooid white mature"
}
if(length(object@treatment[object@treatment==treatment])>=2) {
Short_treat<- treatment%>%
stringr::str_split(.," ")%>%
unlist() %>%
stringr::str_sub(.,1,3) %>%
stringr::str_c(.,collapse="")
count<-data.frame(rowMeans(object@tpm[,object@treatment ==treatment]))
count<-log(count+1)
names(count)<-Short_treat
count<-tibble::rownames_to_column(count, var="sequence_ids")
count$sequence_ids<-as.numeric(count$sequence_ids)
return(count)
}
else if(length(object@treatment[object@treatment==treatment])<=1){
return(NA)
}
}
#pull out tpm values for each treatment
tpmtreat<-function(object){
y<-list()
for(i in 1:length(focal_treatments)){
res<-tpm_output(object,focal_treatments[i])
y[[i]] <- res
}
return(y)
}
sort_tpm<-function(object){
m<-list()
for(i in 1:length(focal_treatments)){
m[[i]]<-lapply(object, extract, i) %>% bind_rows(.)
}
return(m)
}
#This code is modified from the function drop.tip from, https://github.com/GuangchuangYu/treeio. This is used to keep the original node labels associated with the data even after subsetting
add_node_name<-function(object){
if ( class( object ) != "treedata" ) {
return( NA )
}
if (is.null(object@phylo$node.label)) {
object@phylo$node.label <- ape::Ntip(object@phylo) + (1:treeio::Nnode(object@phylo))
}
object@data$node.label_original <- c(object@phylo$tip.label, as.character(object@phylo$node.label))
return(object)
}
###This is modified code from https://github.com/GuangchuangYu/treeio . This code is temporary, as there are issues calling drop.tip (see https://github.com/GuangchuangYu/treeio/issues/3). This will be fixed in the next release.
drop.tip.mod <- function (object, tip) {
node_label_name = "cd8128f329f72c167a8028cf8"
if (!is.null(object@phylo$node.label)) {
# Tree has node labels. Put these in data
# for safe keeping and remove them from tree
# for now
labels = c(
rep( NA, length( object@phylo$tip.label ) ),
object@phylo$node.label
)
object@data[[ node_label_name ]] <- labels
object@phylo$node.label <- NULL
}
## label the internal tree nodes by their number
object@phylo$node.label <- ape::Ntip(object@phylo) + (1:treeio::Nnode(object@phylo))
## Prepare the nhx object for subsampling
object@data$node <- as.numeric(object@data$node)
object@data <- object@data[order(object@data$node),]
## add a colmn that has labels for both tips and internal nodes
object@data$node.label <- c(object@phylo$tip.label, as.character(object@phylo$node.label))
## Will need to take different approaches for subsampling tips
## and internal nodes, add a column to make it easy to tell them apart
object@data$is_tip <- object@data$node <= ape::Ntip(object@phylo)
## Remove tips
object@phylo = ape::drop.tip( object@phylo, tip )
## Subsample the tags
object@data = object@data[object@data$node.label %in% (c(object@phylo$tip.label, as.character(object@phylo$node.label))),]
## Update tip node numbers
tip_nodes <- object@data$node.label[ object@data$is_tip ]
object@data$node[ object@data$is_tip ] = match(object@phylo$tip.label, tip_nodes)
internal_nodes <- object@data$node.label[ !object@data$is_tip ]
object@data$node[ !object@data$is_tip ] = match(object@phylo$node.label, internal_nodes) + length(object@phylo$tip.label)
## Clean up
object@data$node.label = NULL
object@data$is_tip = NULL
## Add node labels back to tree, if there were any
if (node_label_name %in% names( object@data ) ) {
labels = object@data[[ node_label_name ]]
ntips = length ( object@phylo$tip.label )
labels = labels[ (ntips+1):nrow( object@data ) ]
object@phylo$node.label = labels
object@data[[ node_label_name ]] <- NULL
}
return(object)
}
anc.recon.new<-function(x,phy){
#make new node labels that can be put back in later
phy$node.label <- paste("node",ape::Ntip(phy)+(1:treeio::Nnode(phy)))
x=as.data.frame(x)
#drop tips with missing data
phy=ape::drop.tip(phy,which(is.na(x)))
#Run ancestral reconstruction
y= Rphylopars::anc.recon(x[!is.na(x)],phy,CI=TRUE)
#add the old rownames back in
rownames(y$Yhat)=phy$node.label
rownames(y$lowerCI)=phy$node.label
rownames(y$upperCI)=phy$node.label
return(y)
}
add_ace_to_tree<- function(nhx,col_value) {
if ( class( nhx ) != "treedata" ) {
return( NA )
}
#Node storage methods used in this function adapted from treeio::drop.tip
node_label_name = "cd8128f329f72c167a8028cf8"
if (!is.null(nhx@phylo$node.label)) {
# Tree has node labels. Put these in data
# for safe keeping and remove them from tree
# for now
labels = c(
rep( NA, length( nhx@phylo$tip.label ) ),
nhx@phylo$node.label
)
nhx@data[[ node_label_name ]] <- labels
nhx@phylo$node.label <- NULL
}
# y=anc.recon.new(nhx@data[,names(nhx@data)==col_value][is.tip.nhx(nhx),],nhx@phylo)
y=anc.recon.new(dplyr::pull(nhx@data, col_value)[is.tip.nhx(nhx)],nhx@phylo)
nhx@phylo$node.label <- paste("node",Ntip(nhx@phylo)+(1:treeio::Nnode(nhx@phylo)))
nhx@data$node.label <- c(nhx@phylo$tip.label, as.character(nhx@phylo$node.label))
#add the ace values into a new column
nhx@data[,paste("ace",col_value,sep="_")]<-rep(NA,nrow(nhx@data)) %>% as.numeric()
nhx@data[,paste("ace_CI_1",col_value,sep="_")]<-rep(NA,nrow(nhx@data)) %>% as.numeric()
nhx@data[,paste("ace_CI_2",col_value,sep="_")]<-rep(NA,nrow(nhx@data))%>% as.numeric()
nhx@data[,paste("ace",col_value,sep="_")][which(nhx@data$node.label %in% rownames(y$Yhat)),]<-y$Yhat %>% as.numeric()
nhx@data[,paste("ace_CI_1",col_value,sep="_")][which(nhx@data$node.label %in% rownames(y$Yhat)),]<-y$lowerCI %>% as.numeric()
nhx@data[,paste("ace_CI_2",col_value,sep="_")][which(nhx@data$node.label %in% rownames(y$Yhat)),]<-y$upperCI %>% as.numeric()
## Add node labels back to tree, if there were any
if (node_label_name %in% names(nhx@data ) ) {
labels = nhx@data[[ node_label_name ]]
ntips = length ( nhx@phylo$tip.label )
labels = labels[ (ntips+1):nrow( nhx@data ) ]
nhx@phylo$node.label = labels
nhx@data[[ node_label_name ]] <- NULL
}
return(nhx)
}
ace_trees<- function(nhx,col_value){
n_focal_tips <-function( nhx, col_value ){
if ( class( nhx ) != "treedata" ) {
return( NA )
}
x<-sum(!is.na(nhx@data[,names(nhx@data)==col_value]))
return(x)
}
y<-lapply(nhx,n_focal_tips,col_value) %>%
unlist()
subtree <- nhx[ y >= min_tips ]
subtree_ace<-lapply(subtree,add_ace_to_tree, col_value)
return(subtree_ace)
}
merge_ace <- function( nhx1, nhx2) {
#Where sub is a subtree that is reduced down to only the added ace and node.label columns.
if ( class( nhx2 )!="treedata" ) {
return( nhx2 )
}
if (any(grep("ace",names(nhx1@data)))==FALSE) {
return( nhx2 )
}
else{
sub1 <- nhx1@data[ ,c(paste(names(nhx1@data)[grep("ace",names(nhx1@data))]),"ND")]
merged <- merge( nhx2@data, sub1, all=TRUE, by="ND" )
merged <- dplyr::arrange( merged, node )
nhx2@data <- as_tibble(merged)
return( nhx2 )
}
}
###modified from agalmar::summarize_edges
summarize_ace_edges = function ( nhx, default_length_val=NA ) {
if ( class( nhx ) == "treedata" ) {
# Create a data frame of internal node annotations
tags = nhx@data
tags$node = tags$node
tags$S = tags$S
tags$ND = tags$ND
tags = tags[order( tags$node ),]
parents = nhx@phylo$edge[,1]
children = nhx@phylo$edge[,2]
terminal = rep( FALSE, nrow( nhx@phylo$edge ) )
terminal[ children <= length( nhx@phylo$tip.label ) ] = TRUE
#add ace values to one column
edge_length=nhx@phylo$edge.length
df = data.frame(
gene_tree = rep( digest::digest( nhx ), nrow( nhx@phylo$edge ) ),
length = edge_length,
Ev_parent = tags$Ev[parents],
S_parent = tags$S[parents] ,
ND_parent = tags$ND[parents] ,
node_parent = tags$node[parents],
node_depth_parent=tags$node_depth[parents] ,
node_age_parent=tags$node_age[parents],
Gasdev_parent= tags$Gasdev[parents] ,
Palmat_parent= tags$Palmat[parents] ,
Gasmat_parent= tags$Gasmat[parents] ,
Necdev_parent= tags$Necdev[parents] ,
Pne_parent= tags$Pne[parents] ,
Bradev_parent= tags$Bradev[parents] ,
Gonmal_parent= tags$Gonmal[parents] ,
Gonfem_parent= tags$Gonfem[parents] ,
PalmatGasmat_parent=(tags$Palmat[parents]+1)/(tags$Gasmat[parents]+1) ,
GasdevGasmat_parent=(tags$Gasdev[parents]+1)/(tags$Gasmat[parents]+1) ,
NecdevGasmat_parent=(tags$Necdev[parents]+1)/(tags$Gasmat[parents]+1) ,
PneGasmat_parent=(tags$Pne[parents]+1)/(tags$Gasmat[parents]+1) ,
tau_parent=tags$tau[parents],
Ev_child = tags$Ev[children],
S_child = tags$S[children] ,
ND_child = tags$ND[children] ,
node_child = tags$node[children],
node_depth_child=tags$node_depth[children] ,
node_age_child=tags$node_age[children],
Gasdev_child= tags$Gasdev[children] ,
Palmat_child= tags$Palmat[children] ,
Gasmat_child= tags$Gasmat[children] ,
Necdev_child= tags$Necdev[children] ,
Pne_child= tags$Pne[children] ,
Bradev_child= tags$Bradev[children] ,
Gonmal_child= tags$Gonmal[children] ,
Gonfem_child= tags$Gonfem[children] ,
PalmatGasmat_child=(tags$Palmat[children]+1)/(tags$Gasmat[children]+1) ,
GasdevGasmat_child=(tags$Gasdev[children]+1)/(tags$Gasmat[children]+1) ,
NecdevGasmat_child=(tags$Necdev[children]+1)/(tags$Gasmat[children]+1) ,
PneGasmat_child=(tags$Pne[children]+1)/(tags$Gasmat[children]+1) ,
tau_child=tags$tau[children],
Gasdev_scaled=(tags$Gasdev[children] -tags$Gasdev[parents] )/edge_length,
Palmat_scaled=(tags$Palmat[children] -tags$Palmat[parents] )/edge_length,
Gasmat_scaled=(tags$Gasmat[children] -tags$Gasmat[parents] )/edge_length,
Necdev_scaled=(tags$Necdev[children] -tags$Necdev[parents] )/edge_length,
Pne_scaled=(tags$Pne[children] -tags$Pne[parents] )/edge_length,
Bradev_scaled=(tags$Bradev[children] -tags$Bradev[parents] )/edge_length,
Gonmal_scaled=(tags$Gonmal[children] -tags$Gonmal[parents] )/edge_length,
Gonfem_scaled=(tags$Gonfem[children] -tags$Gonfem[parents] )/edge_length,
PalmatGasmat_scaled = ( ((tags$Palmat[children]+1)/(tags$Gasmat[children]+1)) - ((tags$Palmat[parents]+1)/(tags$Gasmat[parents]+1)) )/edge_length ,
GasdevGasmat_scaled = ( ((tags$Gasdev[children]+1)/(tags$Gasmat[children]+1)) - ((tags$Gasdev[parents]+1)/(tags$Gasmat[parents]+1)) )/edge_length ,
NecdevGasmat_scaled = ( ((tags$Necdev[children]+1)/(tags$Gasmat[children]+1)) - ((tags$Necdev[parents]+1)/(tags$Gasmat[parents]+1)) )/edge_length ,
PneGasmat_scaled = ( ((tags$Pne[children]+1)/(tags$Gasmat[children]+1)) - ((tags$Pne[parents]+1)/(tags$Gasmat[parents]+1)) )/edge_length ,
tau_scaled=(tags$tau[children]-tags$tau[parents])/edge_length,
terminal = terminal,
default_length = FALSE,
stringsAsFactors = FALSE
)
if ( ! is.na( default_length_val ) ){
df$default_length = dplyr::near( nhx@phylo$edge.length, default_length_val )
}
return( df )
}
}
summarize_trees = function( gene_trees, col_value ) {
tree_summary =
lapply(
gene_trees,
function( nhx ){
if ( class( nhx ) == "treedata" ) {
tags = nhx@data
phy = nhx@phylo
x = nhx@data[,names(nhx@data)==col_value][1:length(phy$tip.label),] %>% unlist()
names( x ) = phy$tip.label
if(length(x[!is.na(x)])>3){
species<-tags$species[!is.na(x)][1:length(phy$tip.label)]
phy=ape::drop.tip(phy,which(is.na(x)))
event<-nhx@data$Ev[nhx@data$phy_node_names %in% phy$node.label]
x<-x[!is.na(x)]
tibble(
gene = digest( nhx ),
zooid = col_value,
n_tips = length( x ),
mean = mean( x ),
var = var( x ),
K = phytools::phylosig( phy, x, method="K" ),
n_species = length(unique(species)),
n_dup = length(event[event=="D"]),
n_spec = length(event[event=="S"])
)
}
}
}
) %>%
bind_rows()
return( tree_summary )
}
summarize_nodes.mod = function ( nhx, default_length_val=NA ) {
if ( class( nhx ) == "treedata" ) {
# Create a data frame of internal node annotations
tags = cbind(
gene_tree= digest::digest( nhx ),
nhx@data
)
# Add a boolean column that indicates if nodes are parents to
# edges with default length
tags$default_length = FALSE
if ( ! is.na( default_length_val ) ){
default_edges = dplyr::near( nhx@phylo$edge.length, default_length_val )
parent_nodes = nhx@phylo$edge[ , 1 ]
default_nodes = parent_nodes[ default_edges ]
tags$default_length[ default_nodes ] = TRUE
}
tags %<>%
dplyr::mutate_if( is.factor, as.character )
return( tags )
}
}
tau=function(nhx){
if ( class( nhx ) != "treedata" ) {
return( NA )
}
x=nhx@data
x=x[,names(nhx@data) %in% c("Gasdev","Palmat","Gasmat","Necdev","Pne","Gonmal","Gonfem")]
tau=apply(x, 1, function(y) sum(y/max(y))/length(y))
nhx@data$tau<-tau
return(nhx)
}
sim_values = function(nhx, col_value, dup_adjust=1, a=NA ) {
if ( class( nhx ) != "treedata" ) {
return( NA )
}
phy = nhx@phylo
stopifnot(nhx@data$phy_node_names[is.tip.nhx(nhx)]==phy$tip.label)
col_value_original = dplyr::pull(nhx@data,col_value)[is.tip.nhx(nhx)]
names( col_value_original ) = phy$tip.label
phy <- ape::drop.tip(phy,names(which(is.na(col_value_original))))
col_value_mod = col_value_original[!is.na(col_value_original)]
brownian_model = tryCatch(fitCont(
phy,
col_value_mod,
model="BM"), error=function(e) NULL)
if(!is.null(brownian_model)){
# Simulate trait given the tree and parameter estimates - bounding between 0 to 9 as this matches empirical observations
x = phytools::fastBM(
phy,
a = brownian_model$a,
sig2 = brownian_model$sig2,
bounds=c(0,9)
) %>% abs()
#remove ancestral trait reconstructions
nhx@data[,names(nhx@data)==col_value][! is.tip.nhx(nhx), ]<-rep(NA,length(nhx@data[,names(nhx@data)==col_value][! is.tip.nhx(nhx),]))
names( x ) = NULL
nhx@data[,names(nhx@data)==col_value][which(nhx@data$phy_node_names %in% phy$tip.label),] <- x
#subset data to only include only values of interest
nhx@data<-nhx@data[,names(nhx@data) %in% c("ND",col_value)]
rm(phy)
return( nhx )
}
else{
return(NA)
}
}
#must subset down to ensure that n=3 in the trees for BM & OU reconstruction
add_model_parameters_trees<- function(nhx,col_value){
n_focal_tips <-function( nhx, col_value ){
if ( class( nhx ) != "treedata" ) {
return( 0 )
}
x<-sum(!is.na(nhx@data[,names(nhx@data)==col_value]))
return(x)
}
y<-lapply(nhx,n_focal_tips,col_value) %>%
unlist()
subtree <- nhx[ y >= 3 ]
#simulate BM models on the subtree
subtree_sim<-lapply(subtree,sim_values, col_value)
#generate ace values for the simulated data
subtree_ace<-parallel::mclapply(subtree_sim,add_ace_to_tree, col_value, mc.cores=cores)
return(subtree_ace)
}
merge_sim <- function( nhx1, nhx2) {
if ( class( nhx1 ) != "treedata" ) {
return( nhx2 )
}
#Where sub is a subtree that is reduced down to only the added ace and node.label columns.
if ( class( nhx2 )!="treedata" ) {
return( nhx2 )
}
if (any(grep("ace",names(nhx1@data)))==FALSE) {
return( nhx2 )
}
sub1<- nhx1@data[,!(names(nhx1@data) %in% c("Ev","S","node","phy_node_names","species","sequence_ids","node_depth","blast_hit","node_age","node.label_original","node.label"))]
merged <- merge( nhx2@data, sub1, all=TRUE, by="ND" )
merged <- dplyr::arrange( merged, node )
nhx2@data <- as_tibble(merged)
return( nhx2 )
}
####code below from the very helpful Liam Revell http://blog.phytools.org/2014/10/alternative-implementations-of.html######
## helper function to get the root node number
getRoot<-function(tree) ape::Ntip(tree)+1
## here is our fitContinuous lite function
fitCont<-function(tree,x,model="BM",interval=NULL){
if(model!="BM"){
lk<-function(par,tree,x,model) phytools::brownie.lite(phytools::paintSubTree(geiger::rescale(tree,model,par),getRoot(tree),"1"),x)$logL1
oFit<-stats::optimize(lk,interval,tree=tree,x=x,model=model,maximum=TRUE)