# Avoiding large memory requirements by switching to \`nextalign\`

**URL:** <https://nextstrain.discourse.group/t/avoiding-large-memory-requirements-by-switching-to-nextalign/447>\
**Category:** General\
**Created:** [March 22, 2021, 5:39pm UTC](https://nextstrain.discourse.group/t/avoiding-large-memory-requirements-by-switching-to-nextalign/447 "2021-03-22T17:39:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rneher](https://yyz1.discourse-cdn.com/flex031/user_avatar/nextstrain.discourse.group/rneher/32/20_2.png) [@rneher](https://nextstrain.discourse.group/u/rneher)\
**Post date:** [March 22, 2021, 5:39pm UTC](https://nextstrain.discourse.group/t/avoiding-large-memory-requirements-by-switching-to-nextalign/447/1 "2021-03-22T17:39:09Z")

</div>

By default, Nextstrain’s pipeline to analyze SARS-CoV-2 uses `mafft` with the options `--addfragments --keeplength` to align all input sequences to the reference genome. For large data sets (\>500k) this requires a lot of memory.

To keep running the pipeline on the entire data set, we now use [`nextalign`](https://github.com/nextstrain/nextclade/tree/master/packages/nextalign_cli) instead of `mafft`. The reduces the memory requirements to a few Gb and is faster than `mafft`.

To use `nextalign` in your own workflow with the [`ncov`](https://github.com/nextstrain/ncov) workflow, you need to set two variables in your `parameters.yaml` or `builds.yaml` file (the config files used in the profiles):

```auto
use_nextalign: true
genes: ["ORF1a", "ORF1b", "S", "ORF3a", "M", "N"]

```

In our profile, this is happening here:  
[nextstrain\_profiles/nextstrain/builds.yaml](https://github.com/nextstrain/ncov/blob/b45c8ee/nextstrain_profiles/nextstrain/builds.yaml#L13-L14)

By defining the genes, you instruct `nextalign` to output translations of these genes along with the nucleotide alignment. These translations will be used to annotate the tree with amino acid changes since the default codon-by-codon translation of `augur` has trouble with out-of-frame gaps. In addition, `nextalign` will favor the opening of gaps at the beginning of a codon for the genes specified. This will result in gaps that respect codon boundaries when there is an ambiguity, i.e. if the gap can be shifted left or right without resulting in additional mismatches. The genes themselves are defined in the [genome annotation](https://github.com/nextstrain/ncov/blob/master/defaults/annotation.gff). Our selection of genes above is motivated by the fact that they cover most of the genome and avoid overlapping ORFs, which would complicate the gap placement choice.

In addition to these changes in your workflow, you need to install `nextalign` and there are several ways to get it

- `nextalign` is included in the latest conda environment
- `nextalign` is part of the docker image
- you can download and install it following these [instructions](https://github.com/nextstrain/nextclade/tree/master/packages/nextalign_cli). But make sure to copy the binary to a location that is in your `PATH`

A quick note on what nextalign does and its limitations:

- `nextalign` first matches seeds across the genome to infer rough placement and amount of indels
- then performs a banded alignment with an affine gap penalty (gap extension 0 by default) where the band width is estimated from the seed matches.
- extracts the nucleotide sequences corresponding to genes, translates them, and aligns the peptide sequence to the reference translation.
- the seed matching is currently not very robust and might fail if there is repeated sequence or if the sequences are too diverged. But this is not a problem for SARS-CoV-2 data.
