3737# ' @export
3838as.Splits <- function (x , tipLabels = NULL , ... ) UseMethod(' as.Splits' )
3939
40- # ' @keywords internal
41- # ' @export
42- .TipLabels <- function (x ) UseMethod(' .TipLabels' )
43-
44- # ' @keywords internal
45- # ' @export
46- .TipLabels.phylo <- function (x ) x $ tip.label
47-
48- # ' @keywords internal
49- # ' @export
50- .TipLabels.list <- function (x ) {
51- .TipLabels(x [[1 ]])
52- }
53-
54- # ' @keywords internal
55- # ' @export
56- .TipLabels.matrix <- function (x ) colnames(x )
57-
58- # ' @keywords internal
59- # ' @export
60- .TipLabels.multiPhylo <- function (x ) {
61- .TipLabels(x [[1 ]])
62- }
63-
64- # ' @keywords internal
65- # ' @export
66- .TipLabels.Splits <- function (x ) attr(x , ' tip.label' )
67-
68- # ' @keywords internal
69- # ' @export
70- .TipLabels.default <- function (x ) x
71-
72- # ' @keywords internal
73- # ' @export
74- .TipLabels.numeric <- function (x ) NextMethod(' .TipLabels' , as.character(x ))
75-
76-
7740# ' @describeIn as.Splits Convert object of class `phylo` to `Splits`.
7841# ' @param asSplits Logical specifying whether to return a `Splits` object,
7942# ' or an unannotated two-dimensional array (useful where performance is
@@ -83,7 +46,7 @@ as.Splits.phylo <- function (x, tipLabels = NULL, asSplits = TRUE, ...) {
8346 if (! is.null(tipLabels )) {
8447 x <- RenumberTips(x , .TipLabels(tipLabels ))
8548 }
86- x <- Cladewise (x )
49+ x <- Preorder (x )
8750 splits <- cpp_edge_to_splits(x $ edge )
8851 nSplits <- dim(splits )[1 ]
8952 # Return:
@@ -336,18 +299,20 @@ names.Splits <- function (x) rownames(x)
336299c.Splits <- function (... ) {
337300 splits <- list (... )
338301 nTip <- unique(vapply(splits , attr , 1 , ' nTip' ))
339- if (length(nTip ) > 1 ) {
302+ if (length(nTip ) > 1L ) {
340303 stop(" Splits must relate to identical tips." )
341304 }
342- tips <- vapply (splits , attr , character ( nTip ) , ' tip.label' )
343- if (dim (unique(tips , MARGIN = 2 ))[ 2 ] != 1 ) {
344- stop(" Order of tip labels must be identical. " )
305+ tips <- lapply (splits , attr , ' tip.label' )
306+ if (length (unique(lapply( tips , sort ))) > 1L ) {
307+ stop(" All splits must bear identical tips " )
345308 }
309+ tipLabels <- tips [[1 ]]
310+ splits <- c(splits [1 ], lapply(splits [seq_along(splits )[- 1 ]], as.Splits ,
311+ tipLabels = tipLabels ))
346312
347- x <- rbind(... )
348- structure(x ,
313+ structure(do.call(rbind , splits ),
349314 nTip = nTip ,
350- tip.label = tips [, 1 ] ,
315+ tip.label = tipLabels ,
351316 class = ' Splits' )
352317}
353318
@@ -480,3 +445,51 @@ in.Splits <- function (x, table, incomparables = NULL) {
480445 duplicated(c(x , table ), fromLast = TRUE ,
481446 incomparables = incomparables )[seq_along(x ), ]
482447}
448+
449+
450+ # ' @keywords internal
451+ # ' @export
452+ .TipLabels <- function (x ) UseMethod(' .TipLabels' )
453+
454+ # ' @keywords internal
455+ # ' @export
456+ .TipLabels.default <- function (x ) {
457+ if (is.null(names(x ))) {
458+ if (any(duplicated(x ))) {
459+ NULL
460+ } else {
461+ x
462+ }
463+ } else {
464+ names(x )
465+ }
466+ }
467+
468+ # ' @keywords internal
469+ # ' @export
470+ .TipLabels.phylo <- function (x ) x $ tip.label
471+
472+ # ' @keywords internal
473+ # ' @export
474+ .TipLabels.list <- function (x ) {
475+ .TipLabels(x [[1 ]])
476+ }
477+
478+ # ' @keywords internal
479+ # ' @export
480+ .TipLabels.matrix <- function (x ) colnames(x )
481+
482+ # ' @keywords internal
483+ # ' @export
484+ .TipLabels.multiPhylo <- function (x ) {
485+ .TipLabels(x [[1 ]])
486+ }
487+
488+ # ' @keywords internal
489+ # ' @export
490+ .TipLabels.Splits <- function (x ) attr(x , ' tip.label' )
491+
492+
493+ # ' @keywords internal
494+ # ' @export
495+ .TipLabels.numeric <- function (x ) NextMethod(' .TipLabels' , as.character(x ))
0 commit comments