AmpliTrim

How AmpliTrim works

It computes what it can compute exactly, and measures what it cannot.

This page shows the design one step at a time. The pictures are schematic: they explain the idea and are not drawn from your data.

The idea

DADA2 does three things to your reads, in this order. AmpliTrim treats each one differently.

What the cut does

A forward and a reverse read each start at one end of the amplicon, the stretch of DNA that was amplified. truncLenF and truncLenR say where to cut each read. Together they have to be at least as long as the amplicon plus the minimum overlap (12 by default), so that the reads overlap, and no longer than the quality allows.

forward readreverse readoverlapnoisy end

AmpliTrim looks for the shortest cut that still lets your longest amplicons merge, and splits it between the two reads where the quality allows.

Step by step

AmpliTrim runs these steps on a sample of your reads.

  1. Take a sample of your reads

    AmpliTrim takes every Nth read pair across all your samples, up to 50,000 pairs, and uses the same pairs for everything below. The sample is the same every time you run it.

    In the code: build_subsample

    Schematic: a regular sample of read pairs, highlighted among all pairsevery Nth pair, across all samples
    Schematic, not your data.
  2. Measure your amplicons

    It merges the full-length reads with vsearch. The lengths of the merged reads are your amplicon lengths, so you do not need to look them up.

    Only pairs that overlap at full length can be seen. If fewer than 100 pairs do, the run stops with a message that says so.

    In the code: estimate_insert_sizes

    Schematic: a histogram of amplicon lengthsthe lengths of your merged reads = your amplicon lengths
    Schematic, not your data.
  3. Set the floor

    AmpliTrim leaves out the longest 0.2% of the amplicons and adds the minimum overlap: 12 by default, DADA2's own, or the value of --min-overlap (12 to 20). The result is the shortest total cut length that can merge the rest, and AmpliTrim never tests anything shorter.

    In the code: coverage_floor

    Schematic: a histogram of amplicon lengths, with the longest ones set aside and the floor markedlongest 0.2% set asidefloor+ 12amplicon length
    Schematic, not your data.
  4. Try cuts above the floor

    From the floor up to the full read length, in steps of 2 nt, AmpliTrim asks the quality table for the best split between the forward and reverse reads. That table holds the share of pairs that pass DADA2's filter at every cut.

    It then filters the reads at that cut and merges them with vsearch. The share that passes times the share that merges is the yield. The search stops when a longer cut gains less than 1 point.

    In the code: _search, pass_table, run_vsearch

    Schematic: a table of pass shares for every cut, and a curve of yield that rises and then levels offquality tableyield by total lengthforward length →outline = one total length, dot = its best splitstops herelonger total →
    Schematic, not your data.
  5. Pick the cut

    Cuts whose yield is within 1 point of the best are treated as tied. Among them, AmpliTrim picks the one with the least overlap, which means the shortest reads and the fewest noisy bases. A cut that cannot merge your longest amplicons is never picked.

    In the code: select_recommendation

    Schematic: five cuts ranked by yield; the cuts within one point of the best are tied, and the shortest of them is chosenbars = yieldwithin 1 pointof the bestshorterlonger✓the tie goes to the cut with the least overlap
    Schematic, not your data.
  6. Set maxEE

    At the chosen cut, for each read, AmpliTrim finds the limit that covers three quarters of the reads: the 75th percentile of their expected errors, plus 1. It is never below 2, DADA2's default. There is no upper limit: the report notes it when a limit goes above 5.

    In symbols. The expected errors of a read cut at length L are the sum, over its first L bases, of the chance that each base is wrong: EE = sum of 10^(-Q/10), where Q is the Phred quality of the base. For each read direction, over the reads that reach the cut: maxEE = max(2, int(P75 of EE) + 1), where P75 is the 75th percentile and int drops the decimals. For example, a 75th percentile of 3.4 gives int(3.4) + 1 = 4, and one of 0.6 gives int(0.6) + 1 = 1, which is below 2, so the limit is 2.

    If the limit is not the default for both reads, the search and the pick are repeated with it.

    In the code: _rule_max_ee

    Schematic: the distribution of expected errors, with the 75th percentile marked, a limit that starts at 2 and has no upper limit, and a note above 575% of readsexpected errors →limit25: a notelimit = 75th percentile + 1, from 2 upward, no upper limit
    Schematic, not your data.
  7. Write the report

    You get REPORT.md (the settings, copy-paste snippets for DADA2 and QIIME 2, what to expect, and warnings), summary.json (the same result for scripts) and summary.png (a figure).

    In the code: write_outputs

    Schematic: the three output filesREPORT.md{ }summary.jsonsummary.png
    Schematic, not your data.
  8. With --its

    ITS amplicons vary a lot in length, so DADA2's ITS workflow does not truncate. AmpliTrim evaluates exactly that: each read is cut at its first base of quality 2 or below, then kept if it is long enough, free of N and within maxEE.

    The floor and the search are skipped. The maxEE rule is measured on each read after this cut, with the same percentile and rounding. When fewer than 100% of the pairs pass at maxEE 2,2, the report advises trimming the poor read ends first (amplitrim trim: window 4, mean quality 15) and predicts the effect, and maxEE stays 2 for the trimmed reads. The report also shows where the pairs go.

    In the code: _run_its, _run_its_rules

    Schematic: for ITS, each read is cut at its first low-quality base and kept only if it is long enough, free of N and within the error limitone read, from the startfirst base of quality 2 or belowkept if the cut read is✓long enough✓free of N✓within maxEE
    Schematic, not your data.

Why these choices

Every choice is listed with how well it held up in our tests. Some are design choices with no test behind them, and we say which.

ChoiceWhyHow it held up
Replicate DADA2's quality filterThe share of pairs that pass is then computed, not guessed.Held

The predicted pass rate was within 0.8 points of DADA2's in every test dataset.

Measure merging with real merges, at the minimum overlap (default 12, DADA2's own)Merging depends on the real overlaps and mismatches in your reads, which arithmetic cannot know.Held

With these cuts, DADA2 merged as many pairs as with the other tool's cuts: a tie.

Find the amplicon length from your readsYou do not have to look it up, and it is right for your data.Design choice

Only pairs that overlap at full length are seen, so the longest amplicons are under-represented.

Use a sample of at most 50,000 pairsThe run time and the memory use stay bounded, however large your dataset is.Timed

The bound is by design. The released version 0.2.0 ran in 29 to 141 seconds and used at most 352 MB on 12 datasets (register section 30).

Count merges with a tolerant setting for the yieldA strict count tracked DADA2 less well in our tests.Optimistic

The yield is an estimate made before DADA2 denoises the reads. Use it to compare settings, and run DADA2 once to confirm. On ITS it can be far too high: DADA2 merged 46.9% where 72.4% was estimated and 14.0% where 82.1% was estimated, and on two of four independent gut datasets it overshot by 6 to 20 points.

Break ties by the least overlapShorter reads mean fewer noisy bases.Design choice

Cuts within the tolerance of the best are equivalent on yield, so this is a matter of preference.

Set maxEE to cover three quarters of the reads, never below 2Reads with noisy ends fail DADA2's default limit, and a looser limit can recover them.Sensible default, independently tested in part

It follows DADA2's author: keep maxEE 2 unless your reads need more, and relax it somewhat when they do. The 75th percentile is our own choice, made after seeing the validation data (the author gives no ceiling, so the rule has none; the report notes a limit above 5), and the rule did not reach the result we had decided in advance would make us adopt it. An independent test by a collaborator on two gut datasets where it relaxed was favourable (more pairs merged, community profiles unchanged). We keep it as a trade-off that we state openly.

Offer few optionsThree required arguments, --its and --verbose leave little room to misuse it.Design choice

Everything else is chosen for you, including maxEE.

The full results, including what did not hold, are on the validation page. The user guide explains how maxEE is chosen.

What AmpliTrim does not do

Where things are in the code

The package is nine modules. The last column shows which others each one uses.

ModuleWhat it doesUses
cli.pyThe command line: reads the arguments, runs the pipeline, prints the summary and writes the report.api, pipeline, report, trim
api.pyThe programmatic entry point, recommend(): runs the pipeline, writes the report and returns the summary. The command line calls it.pipeline, report
config.pyDefaults that mirror the DADA2 call you will run (minimum overlap, mismatches, maxEE), and how a maxEE value is read.nothing else in the package
subsample.pyFinds the read pairs and takes the shared, repeatable sample.nothing else in the package
trimmer.pyHolds the sample in memory and replicates DADA2's filter: the quality table, filtering at a cut, and the error counts.config, subsample
trim.pyamplitrim trim: cuts the poor ends of ITS reads before the first window of 4 bases whose mean quality is below 15, the cut tested in the validation.nothing else in the package
merge_wrapper.pyRuns vsearch to merge reads, and reads its log.nothing else in the package
ranking.pyDefines a candidate cut, the floor, and the rule that picks the recommendation.config
pipeline.pyThe whole run: the steps in order, the maxEE rule, the ITS mode with its trim advice, and the warnings.config, merge_wrapper, ranking, subsample, trim, trimmer
report.pyWrites REPORT.md, summary.json and summary.png, and the summary for the terminal.config, pipeline, ranking

If you want to change something

To changeLook in
A default, such as the minimum overlap or the share of long amplicons set asideconfig.py, and the constants at the top of subsample.py and pipeline.py
How reads are filteredtrimmer.py
How the best cut is chosenranking.py (select_recommendation)
How maxEE is setpipeline.py (_rule_max_ee)
What the report saysreport.py

Contributions are welcome: see CONTRIBUTING.md.