Skip to content
Back to skills

Ape

ASecurity

R ape package for phylogenetic analysis. Use for reading, writing, and analyzing phylogenetic trees.

  • 5 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added June 4, 2026
datanode

Security analysis

A100/100

Scanned June 4, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill ape --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Ape?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Ape
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-ape/badge)](https://www.skillsdirectory.com/skills/leolin990405-ape)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: ape
description: R ape package for phylogenetic analysis. Use for reading, writing, and analyzing phylogenetic trees.
---

# ape

Analyses of Phylogenetics and Evolution.

## Reading Trees

```r
library(ape)

# Read Newick format
tree <- read.tree("tree.nwk")
tree <- read.tree(text = "((A:0.1,B:0.2):0.3,C:0.4);")

# Read Nexus format
tree <- read.nexus("tree.nex")

# Multiple trees
trees <- read.tree("trees.nwk")
```

## Tree Structure

```r
# Tree components
tree$tip.label      # Tip names
tree$edge           # Edge matrix
tree$edge.length    # Branch lengths
tree$Nnode          # Number of internal nodes

# Number of tips
Ntip(tree)

# Number of nodes
Nnode(tree)
```

## Plotting Trees

```r
# Basic plot
plot(tree)

# Phylogram
plot(tree, type = "phylogram")

# Cladogram
plot(tree, type = "cladogram")

# Fan/radial
plot(tree, type = "fan")

# Unrooted
plot(tree, type = "unrooted")

# With options
plot(tree,
     show.tip.label = TRUE,
     tip.color = "blue",
     edge.color = "gray",
     edge.width = 2,
     cex = 0.8
)

# Add scale bar
add.scale.bar()
```

## Tree Manipulation

```r
# Root tree
rooted <- root(tree, outgroup = "A")
rooted <- root(tree, node = 5)

# Unroot tree
unrooted <- unroot(tree)

# Drop tips
pruned <- drop.tip(tree, c("A", "B"))

# Keep tips
kept <- keep.tip(tree, c("C", "D", "E"))

# Rotate nodes
rotated <- rotate(tree, node = 5)

# Ladderize
ladderized <- ladderize(tree)
```

## Distance Matrices

```r
# Compute pairwise distances
dist_matrix <- cophenetic(tree)

# From sequences
library(ape)
dna <- read.dna("sequences.fasta", format = "fasta")
dist_matrix <- dist.dna(dna, model = "K80")
```

## Tree Building

```r
# Neighbor-joining
nj_tree <- nj(dist_matrix)

# UPGMA
upgma_tree <- hclust(as.dist(dist_matrix), method = "average")
upgma_tree <- as.phylo(upgma_tree)

# Minimum evolution
me_tree <- fastme.bal(dist_matrix)
```

## Bootstrap

```r
# Bootstrap analysis
boot_trees <- boot.phylo(tree, dna, function(x) nj(dist.dna(x)),
                         B = 100, trees = TRUE)

# Plot with bootstrap values
plot(tree)
nodelabels(boot_trees$BP, cex = 0.7)
```

## Comparative Methods

```r
# Phylogenetic independent contrasts
pic_result <- pic(trait, tree)

# Ancestral state reconstruction
anc <- ace(trait, tree, type = "continuous")
anc <- ace(trait, tree, type = "discrete")

# Plot ancestral states
plot(tree)
nodelabels(pie = anc$lik.anc, cex = 0.5)
```

## Writing Trees

```r
# Write Newick
write.tree(tree, file = "output.nwk")

# Write Nexus
write.nexus(tree, file = "output.nex")

# As text
write.tree(tree)
```

## Tree Comparison

```r
# Robinson-Foulds distance
RF.dist(tree1, tree2)

# Cophenetic correlation
cor(cophenetic(tree1), cophenetic(tree2))
```

## Sequence Data

```r
# Read sequences
dna <- read.dna("sequences.fasta", format = "fasta")
dna <- read.dna("sequences.phy", format = "sequential")

# Write sequences
write.dna(dna, file = "output.fasta", format = "fasta")

# Sequence statistics
base.freq(dna)
GC.content(dna)
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.