# Neighbor Joining and Unrooted Trees

Here are two programs.

nejo
does clustering using the Neighbor Joining algorithm
utree
draws an unrooted tree of the results

A typical usage is like this:

```    nejo diffile.txt | utree > unrooted.eps
```

Or, with a few parameters:

```    nejo NLdif.txt | utree -f 14 -h > NLunrooted.eps
```

## Modifying the tree

Read how to swap branches of an unrooted tree.

## Related clustering methods in R

You need the following R packages:
Then you can run these demos: The ape package provides a few related clustering methods:
• nj — Neighbor-Joining Tree Estimation
• bionj — Tree Estimation Based on an Improved Version of the NJ Algorithm
• fastme.bal &mdash Tree Estimation Based on the Minimum Evolution Algorithm (balanced)
• fastme.ols &mdash Tree Estimation Based on the Minimum Evolution Algorithm (OLS)
The first is identical to the nejo program.

## Import/export between nejo/utree and R

### From nejo to R

Use the clnewick program to transform the output of nejo into the Newick tree format:

```    nejo NLdif.txt | clnewick > NLnewick.txt
```

(clnewick is part of RuG/L04. You need at least version 0.03)

You can then import the clustering into R:

```    library(ape)
plot(t, type="unrooted", no.margin=TRUE)
```

### From R to utree

Here is a function that you can use to export the result in R of nj, bionj, fastme.bal or fastme.ols into the format understood by the utree program:

Here is an example of how to use it in R:

```    source("tree2cluster.R")
library(ape)
library(iL04)
data(PA)
t <- fastme.bal(PA)
tree2cl(t, "PAfastME.txt")
```

Then you can draw an unrooted tree with the utree program:

```    utree PAfastME.txt > PAfastME.eps
```