
> library(ape)

> library(shipunov)
package 'shipunov', version 1.3-1

> chars <- read.table("data/picramniaceae_chars.txt", h=TRUE, sep="\t", as.is=TRUE)

> DIR <-  dir("data")

> trees <- c("semistrict_mb")

> for(t in trees) {
+ assign(t, read.tree(paste0("data/",  DIR[grepl(t, DIR)])))
+ }

> tree <- semistrict_mb

> tree <-  drop.tip(tree, grep("Kirkia", tree$tip.label))

> tree <-  drop.tip(tree, grep("Leitneria", tree$tip.label))

> SP <- gsub("__.*$", "", tree$tip.label)

> SP <- gsub("_", " ", SP)

> SP <- gsub("cf. ", "", SP)

> SP <- gsub("Picramnia aff. glazioviana", "Picramnia glazioviana", SP)

> SP <- gsub("Picramnia elliptica Minas Gerais", "Picramnia elliptica", SP)

> SP <- gsub("Picramnia oreadica penduliflora", "Picramnia oreadica", SP)

> SP <- gsub("Picramnia sp. Nadruz 2552", "Picramnia sellowii", SP)

> ## label duplicates with numbers
> SP <- make.unique(SP)

> ## return changed labels back to trees
> tree$tip.label <- SP

> ## drop duplicates, leave only the first one
> tree <- drop.tip(tree, tree$tip.label[grepl("\\.[1-9]", tree$tip.label)])

> # ===
> 
> chars.regs <- chars[, c("Darien.to.Peru.plus.Amazon", "Central.America.and.West.Indies", "Bolivia.to.extra.Amazonian.Brazil")]

> tmp <- apply(as.matrix(chars.regs), 1, function(.x) which(.x == 1))

> regs <- factor(unlist(lapply(tmp, "[", 1)), labels=colnames(chars.regs)) # takes first (supposedly plesiomorphic) value

> REGS <- factor(Recode(tree$tip.label, chars[, 1], regs), levels=levels(regs))

> (ans <- ace(REGS, tree, type="d"))

    Ancestral Character Estimation

Call: ace(x = REGS, phy = tree, type = "d")

    Log-likelihood: -42.86448 

Rate index matrix:
                                  Darien.to.Peru.plus.Amazon
Darien.to.Peru.plus.Amazon                                 .
Central.America.and.West.Indies                            1
Bolivia.to.extra.Amazonian.Brazil                          1
                                  Central.America.and.West.Indies
Darien.to.Peru.plus.Amazon                                      1
Central.America.and.West.Indies                                 .
Bolivia.to.extra.Amazonian.Brazil                               1
                                  Bolivia.to.extra.Amazonian.Brazil
Darien.to.Peru.plus.Amazon                                        1
Central.America.and.West.Indies                                   1
Bolivia.to.extra.Amazonian.Brazil                                 .

Parameter estimates:
 rate index estimate std-err
          1   5.3235  2.1373

Scaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):
       Darien.to.Peru.plus.Amazon   Central.America.and.West.Indies 
                        0.4283956                         0.3112649 
Bolivia.to.extra.Amazonian.Brazil 
                        0.2603395 

> pdf("1.pdf", width=36, height=28)

> par(mfrow=c(2, 3), mar=rep(0, 4))

> #pdf("data/picramniaceae_ace_regs.pdf", width=12, height=14)
> #par(mar=c(0,0,0,0))
> plot(tree, type="cladogram", use.edge.length=FALSE, label.offset=1, no.margin=TRUE)

> co <- rainbow(nlevels(REGS))

> tiplabels(pch=22, bg=co[as.numeric(REGS)], cex=2, adj=1)

> nodelabels(thermo=ans$lik.anc, piecol=co, cex=0.75)

> legend("bottomleft", fill=co, legend=levels(REGS))

> #dev.off()
> 
> ## ===
> 
> ## regs2 <- factor(apply(chars.regs, 1, paste, collapse=""), levels=c("111", "110", "101", "100", "010", "001"), labels=c("All", "D+C", "D+B", "D", "C", "B")) # polymorphisms encoded as states
> ## REGS2 <- factor(Recode(tree$tip.label, chars[, 1], regs2), levels=levels(regs2))
> ## (ans <- ace(REGS2, tree, type="d"))
> 
> ## pdf("data/picramniaceae_ace_regs2.pdf", width=12, height=14)
> ## par(mar=c(0,0,0,0))
> ## plot(tree, type="cladogram", use.edge.length=FALSE, label.offset=1, no.margin=TRUE)
> ## co <- rainbow(nlevels(REGS2))
> ## tiplabels(pch=22, bg=co[as.numeric(REGS2)], cex=2, adj=1)
> ## nodelabels(thermo=ans$lik.anc, piecol=co, cex=0.75)
> ## legend("bottomleft", fill=co, legend=levels(REGS2))
> ## dev.off()
> 
> ## ===
> 
> chars.merosity <- chars[, c("fl.5.6..merous", "fl.4.merous", "fl.3.merous")] # used as recoding order

> tmp <- apply(as.matrix(chars.merosity), 1, function(.x) which(.x == 1))

> merosity <- factor(unlist(lapply(tmp, "[", 1)), labels=colnames(chars.merosity))

> MER <- factor(Recode(tree$tip.label, chars[, 1], merosity), levels=levels(merosity))

> (ans <- ace(MER, tree, type="d"))

    Ancestral Character Estimation

Call: ace(x = MER, phy = tree, type = "d")

    Log-likelihood: -30.14368 

Rate index matrix:
               fl.5.6..merous fl.4.merous fl.3.merous
fl.5.6..merous              .           1           1
fl.4.merous                 1           .           1
fl.3.merous                 1           1           .

Parameter estimates:
 rate index estimate std-err
          1   1.4253  0.4121

Scaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):
fl.5.6..merous    fl.4.merous    fl.3.merous 
    0.93408597     0.03278690     0.03312713 

> #pdf("data/picramniaceae_ace_merosity.pdf", width=12, height=14)
> #par(mar=c(0,0,0,0))
> plot(tree, type="cladogram", use.edge.length=FALSE, label.offset=1, no.margin=TRUE)

> co <- rainbow(nlevels(MER))

> tiplabels(pch=22, bg=co[as.numeric(MER)], cex=2, adj=1)

> nodelabels(thermo=ans$lik.anc, piecol=co, cex=0.75)

> legend("bottomleft", fill=co, legend=levels(MER))

> #dev.off()
> 
> ## ===
> 
> chars.carpels <- chars[, c("carpels.2", "carpels.3.4.", "carpels.1")] # used as recoding order

> tmp <- apply(as.matrix(chars.carpels), 1, function(.x) which(.x == 1))

> carpels <- factor(unlist(lapply(tmp, "[", 1)), labels=colnames(chars.carpels))

> CRP <- factor(Recode(tree$tip.label, chars[, 1], carpels), levels=levels(carpels))

> (ans <- ace(CRP, tree, type="d"))

    Ancestral Character Estimation

Call: ace(x = CRP, phy = tree, type = "d")

    Log-likelihood: -28.50435 

Rate index matrix:
             carpels.2 carpels.3.4. carpels.1
carpels.2            .            1         1
carpels.3.4.         1            .         1
carpels.1            1            1         .

Parameter estimates:
 rate index estimate std-err
          1   1.7455  0.5614

Scaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):
   carpels.2 carpels.3.4.    carpels.1 
   0.2753129    0.5875579    0.1371292 

> #pdf("data/picramniaceae_ace_carpels.pdf", width=12, height=14)
> #par(mar=c(0,0,0,0))
> plot(tree, type="cladogram", use.edge.length=FALSE, label.offset=1, no.margin=TRUE)

> co <- rainbow(nlevels(CRP))

> tiplabels(pch=22, bg=co[as.numeric(CRP)], cex=2, adj=1)

> nodelabels(thermo=ans$lik.anc, piecol=co, cex=0.75)

> legend("bottomleft", fill=co, legend=levels(CRP))

> #dev.off()
> 
> ## ===
> 
> chars.inflp <- chars[, c("inflp..sub.terminal", "inflp.axillary.or.lateral")] # used as recoding order

> tmp <- apply(as.matrix(chars.inflp), 1, function(.x) which(.x == 1))

> inflp <- factor(unlist(lapply(tmp, "[", 1)), labels=colnames(chars.inflp))

> INFLP <- factor(Recode(tree$tip.label, chars[, 1], inflp), levels=levels(inflp))

> (ans <- ace(INFLP, tree, type="d"))

    Ancestral Character Estimation

Call: ace(x = INFLP, phy = tree, type = "d")

    Log-likelihood: -14.87852 

Rate index matrix:
                          inflp..sub.terminal inflp.axillary.or.lateral
inflp..sub.terminal                         .                         1
inflp.axillary.or.lateral                   1                         .

Parameter estimates:
 rate index estimate std-err
          1   0.8861  0.3869

Scaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):
      inflp..sub.terminal inflp.axillary.or.lateral 
               0.98846184                0.01153816 

> #pdf("data/picramniaceae_ace_inflp.pdf", width=12, height=14)
> #par(mar=c(0,0,0,0))
> plot(tree, type="cladogram", use.edge.length=FALSE, label.offset=1, no.margin=TRUE)

> co <- rainbow(nlevels(INFLP))

> tiplabels(pch=22, bg=co[as.numeric(INFLP)], cex=2, adj=1)

> nodelabels(thermo=ans$lik.anc, piecol=co, cex=0.75)

> legend("bottomleft", fill=co, legend=levels(INFLP))

> #dev.off()
> 
> ## ===
> 
> chars.inflt <- chars[, c("inflt.2.pleiothyrse", "inflt.monothyrse", "inflt.thyrsoid", "inflt.raceme")] # used as recoding order

> tmp <- apply(as.matrix(chars.inflt), 1, function(.x) which(.x == 1))

> inflt <- factor(unlist(lapply(tmp, "[", 1)), labels=colnames(chars.inflt))

> INFLT <- factor(Recode(tree$tip.label, chars[, 1], inflt), levels=levels(inflt))

> (ans <- ace(INFLT, tree, type="d"))

    Ancestral Character Estimation

Call: ace(x = INFLT, phy = tree, type = "d")

    Log-likelihood: -46.11016 

Rate index matrix:
                    inflt.2.pleiothyrse inflt.monothyrse inflt.thyrsoid
inflt.2.pleiothyrse                   .                1              1
inflt.monothyrse                      1                .              1
inflt.thyrsoid                        1                1              .
inflt.raceme                          1                1              1
                    inflt.raceme
inflt.2.pleiothyrse            1
inflt.monothyrse               1
inflt.thyrsoid                 1
inflt.raceme                   .

Parameter estimates:
 rate index estimate std-err
          1   2.0825  0.4947

Scaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):
inflt.2.pleiothyrse    inflt.monothyrse      inflt.thyrsoid        inflt.raceme 
          0.2236868           0.1452296           0.4595886           0.1714951 

> #pdf("data/picramniaceae_ace_inflt.pdf", width=12, height=14)
> #par(mar=c(0,0,0,0))
> plot(tree, type="cladogram", use.edge.length=FALSE, label.offset=1, no.margin=TRUE)

> co <- rainbow(nlevels(INFLT))

> tiplabels(pch=22, bg=co[as.numeric(INFLT)], cex=2, adj=1)

> nodelabels(thermo=ans$lik.anc, piecol=co, cex=0.75)

> legend("bottomleft", fill=co, legend=levels(INFLT))

> #dev.off()
> 
> ## ===
> 
> chars.trich <- chars[, c("trich.attenuate", "trich.clavate", "trich.lacking")] # used as recoding order

> tmp <- apply(as.matrix(chars.trich), 1, function(.x) which(.x == 1))

> trich <- factor(unlist(lapply(tmp, "[", 1)), labels=colnames(chars.trich))

> TRICH <- factor(Recode(tree$tip.label, chars[, 1], trich), levels=levels(trich))

> (ans <- ace(TRICH, tree, type="d"))

    Ancestral Character Estimation

Call: ace(x = TRICH, phy = tree, type = "d")

    Log-likelihood: -26.94859 

Rate index matrix:
                trich.attenuate trich.clavate trich.lacking
trich.attenuate               .             1             1
trich.clavate                 1             .             1
trich.lacking                 1             1             .

Parameter estimates:
 rate index estimate std-err
          1   0.8489  0.2887

Scaled likelihoods at the root (type '...$lik.anc' to get them for all nodes):
trich.attenuate   trich.clavate   trich.lacking 
     0.97691485      0.01154257      0.01154257 

> #pdf("data/picramniaceae_ace_trich.pdf", width=12, height=14)
> #par(mar=c(0,0,0,0))
> plot(tree, type="cladogram", use.edge.length=FALSE, label.offset=1, no.margin=TRUE)

> co <- rainbow(nlevels(TRICH))

> tiplabels(pch=22, bg=co[as.numeric(TRICH)], cex=2, adj=1)

> nodelabels(thermo=ans$lik.anc, piecol=co, cex=0.75)

> legend("bottomleft", fill=co, legend=levels(TRICH))

> #dev.off()
> dev.off()
null device 
          1 

> ## ===
> 
> ## trich2 <- factor(apply(chars.trich, 1, paste, collapse=""), levels=c("101", "100", "010", "001"), labels=c("trich.attenuate.or.lacking", "trich.attenuate", "trich.clavate", "trich.lacking")) # polymorphisms as states
