# NEXUS to FASTA Converter

Convert NEXUS DATA or CHARACTERS sequence matrices to FASTA locally, with interleaved matrices, comments, matchchar expansion, TAXA labels, gaps, and wrapping.

## Run it

- **CLI:** `gizza tool nexus-to-fasta "#NEXUS
begin data;
  dimensions ntax=2 nchar=8;
  format datatype=dna gap=-;
  matrix
    Alpha  ACGTACGT
    Beta   ACGTTCGT
  ;
end;"`
- **Web:** https://gizza.ai/tools/nexus-to-fasta/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/nexus-to-fasta/tool.json

## Inputs

- `nexus` — NEXUS alignment _(field)_
- `layout` — Matrix layout _(field)_
- `wrap` — FASTA line width (0 = one line) _(field)_
- `case` — Residue case _(field)_
- `remove_gaps` — Strip gaps _(field)_
- `expand_matchchar` — Expand matchchar (.) from first taxon _(field)_
- `underscores_to_spaces` — Unquoted underscores become spaces _(field)_
- `tolerant` — Tolerant — convert despite dimension mismatches _(field)_

## Output

- FASTA output (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `nexus` — NEXUS alignment
- `layout` — Matrix layout
- `wrap` — FASTA line width (0 = one line)
- `case` — Residue case
- `remove_gaps` — Strip gaps
- `expand_matchchar` — Expand matchchar (.) from first taxon
- `underscores_to_spaces` — Unquoted underscores become spaces
- `tolerant` — Tolerant — convert despite dimension mismatches

Example: `https://gizza.ai/tools/nexus-to-fasta/?nexus=%23NEXUS%0Abegin%20data%3B%0A%20%20dimensions%20ntax%3D2%20nchar%3D8%3B%0A%20%20format%20datatype%3Ddna%20gap%3D-%3B%0A%20%20matrix%0A%20%20%20%20Alpha%20%20ACGTACGT%0A%20%20%20%20Beta%20%20%20ACGTTCGT%0A%20%20%3B%0Aend%3B&layout=auto&wrap=60&case=keep&remove_gaps=true&expand_matchchar=true&underscores_to_spaces=true&tolerant=true`

---

## What this tool does

Convert a **NEXUS** sequence-alignment matrix into standard **FASTA**. Paste the
file text, including `#NEXUS` and its `begin data;` or `begin characters;` block,
and the tool extracts the `matrix` command into one `>name` FASTA record per
taxon. It runs locally in your browser, so the alignment is not uploaded.

NEXUS is flexible: comments can appear almost anywhere, taxon labels may be quoted,
DATA and CHARACTERS blocks are both common, and matrices may be sequential or
interleaved. This converter handles those everyday forms and reports dimension
mismatches clearly.

## Supported NEXUS features

| Feature | Handling |
| --- | --- |
| `DATA` and `CHARACTERS` blocks | The first matching block is used; TAXA/TREES/ASSUMPTIONS are ignored except for `taxlabels`. |
| `dimensions ntax=… nchar=…` | Used to validate taxon count and site count. |
| `format interleave` | Honoured by Auto-detect; you can also force sequential or interleaved. |
| `format gap=…` | The declared gap symbol is preserved, or stripped when **Strip gaps** is on. |
| `format matchchar=.` | Expanded from the first taxon's residue by default. |
| `format labels=no` | Rows are paired with labels from the TAXA block's `taxlabels` command. |
| Bracketed `[comments]` | Removed before parsing, including nested comments. |
| Quoted taxon labels | Single-quoted labels with spaces are preserved. |

## Options

| Option | What it does |
| --- | --- |
| **Matrix layout** | `Auto-detect` (default), `Sequential`, or `Interleaved`. Auto honours the `interleave` flag and otherwise chooses the parse that matches `nchar`. |
| **FASTA line width** | Wrap each sequence at this many characters. Default `60`; `0` writes one long line per sequence. |
| **Residue case** | Keep, uppercase, or lowercase sequence residues. Taxon labels are not case-normalised. |
| **Strip gaps** | Remove the declared `gap=` symbol plus common `-` and `.` gap marks. |
| **Expand matchchar** | Replace a declared `matchchar` (usually `.`) with the first taxon's residue at the same site. On by default. |
| **Unquoted underscores become spaces** | Apply the NEXUS convention where `Homo_sapiens` in an unquoted label means `Homo sapiens`. Off by default because many FASTA workflows prefer underscore-stable headers. |
| **Tolerant** | Convert anyway when `ntax` or `nchar` checks fail. Off by default. |

## Worked example

A simple DATA block:

```
#NEXUS
begin data;
  dimensions ntax=2 nchar=8;
  format datatype=dna gap=-;
  matrix
    Alpha  ACGTACGT
    Beta   ACGTTCGT
  ;
end;
```

With **FASTA line width** set to `0`, the output is:

```
>Alpha
ACGTACGT
>Beta
ACGTTCGT
```

An interleaved CHARACTERS block with `matchchar=.` expands the dots from the first
taxon when **Expand matchchar** is on:

```
#NEXUS
begin characters;
  dimensions ntax=2 nchar=8;
  format datatype=dna gap=- matchchar=. interleave;
  matrix
    Alpha  ACGT
    Beta   ....

    Alpha  TGCA
    Beta   ....
  ;
end;
```

```
>Alpha
ACGTTGCA
>Beta
ACGTTGCA
```

## Limits and edge cases

- The document must start with `#NEXUS` after leading whitespace.
- A `begin data;` or `begin characters;` block with a `matrix` command is required.
- Strict mode checks the number of parsed taxa against `ntax` and each sequence's
  site count against `nchar`. The error names the offending taxon when possible.
- `nchar` counts NEXUS state sets such as `(01)` or `{AC}` as one site; wrapping
  will not split the bracketed set.
- The converter does not output trees from a TREES block. It only converts the
  sequence matrix.
- FASTA has no datatype field, so `datatype=dna`, `protein`, and `standard` are
  treated as alignment text rather than changing the output format.
- FASTA line width is capped at 1000 characters per line.

## FAQ

<details>
<summary>Does this convert TREE blocks too?</summary>

No. It only extracts the sequence matrix from a `DATA` or `CHARACTERS` block and
writes FASTA. NEXUS tree definitions are a different data shape and are ignored.

</details>

<details>
<summary>Why does the output differ when Expand matchchar is on?</summary>

In NEXUS, a declared `matchchar` such as `.` means "use the same state as the
first taxon at this site." Expanding it writes the actual residue into the FASTA
record, which is usually what downstream FASTA tools expect. Turn the option off
when you want literal dots preserved.

</details>

<details>
<summary>How are spaces in taxon names handled?</summary>

Single-quoted labels such as `'Homo sapiens'` keep the space. For unquoted labels,
NEXUS treats underscores as spaces; this tool can apply that convention with
**Unquoted underscores become spaces**, but keeps underscores by default for safer
FASTA headers.

</details>

<details>
<summary>Can I convert interleaved NEXUS files?</summary>

Yes. Auto-detect honours `format interleave` and also tries the interleaved parse
when no flag is present. Force **Matrix layout** to `Interleaved` if the file is
malformed and you still want that interpretation.

</details>

<details>
<summary>What does a dimension mismatch mean?</summary>

The `dimensions` command declares how many taxa and sites the matrix should have.
A mismatch usually means the file is truncated, a wrong layout was forced, or the
matrix contains labels/no-labels in a different shape than declared. Use
**Tolerant** only when you want the partial sequences anyway.

</details>

## Related tools

- [PHYLIP to FASTA Converter](https://gizza.ai/tools/phylip-to-fasta/): Convert PHYLIP alignments to FASTA in your browser. Sequential and interleaved layouts, strict or relaxed taxon names, gap stripping. Nothing is uploaded.
- [FASTQ to FASTA Converter](https://gizza.ai/tools/fastq-to-fasta/): Convert FASTQ sequencing reads to clean FASTA in your browser — strip quality lines, filter by length and mean quality, wrap, rename, discard N reads. No upload.
- [Convert SAM Alignment Records to CSV](https://gizza.ai/tools/sam-to-csv/): Convert SAM sequence-alignment records into CSV or TSV with named columns, decoded FLAG bits, optional tag columns, filters, and computed spans.
- [Convert VCF Variants to TSV](https://gizza.ai/tools/genomic-vcf-to-tsv/): Flatten Variant Call Format (VCF) records into tidy TSV, exploding INFO fields and optional sample genotype columns. Runs locally in your browser.
- [Absolute value, sign, or negation for a whole column](https://gizza.ai/tools/absolute-value-transformer/): Paste a column of numbers and apply absolute value, sign extraction (-1/0/1), sign flipping, or force-negative to every value at once, with rounding and an audit table.
