[med-svn] [Git][med-team/axe-demultiplexer][master] 11 commits: Update Homepage and Source; d/watch: version=5

Andreas Tille (@tille) gitlab at salsa.debian.org
Wed Nov 11 17:03:35 GMT 2026



Andreas Tille pushed to branch master at Debian Med / axe-demultiplexer


Commits:
99a8e144 by Andreas Tille at 2026-11-11T17:41:12+01:00
Update Homepage and Source; d/watch: version=5

- - - - -
6e293b79 by Andreas Tille at 2026-11-11T17:41:23+01:00
New upstream version 0.4.0+dfsg
- - - - -
5437d5a9 by Andreas Tille at 2026-11-11T17:41:23+01:00
New upstream version

- - - - -
1ce2d96f by Andreas Tille at 2026-11-11T17:41:23+01:00
Update upstream source from tag 'upstream/0.4.0+dfsg'

Update to upstream version '0.4.0+dfsg'
with Debian dir bb5f139b718497a3bfedbccc44445c98cfba6d0a
- - - - -
9f1ac0c6 by Andreas Tille at 2026-11-11T17:41:24+01:00
Standards-Version: 4.7.4 (routine-update)

- - - - -
e0ee2d23 by Andreas Tille at 2026-11-11T17:41:33+01:00
Changes made by cme

- - - - -
b53c9128 by Andreas Tille at 2026-11-11T17:41:46+01:00
Reformat the Last-Update date as ISO YYYY-MM-DD.

Changes-By: lintian-brush
Fixes: lintian: source: invalid-dep3-format-patch-wrong-last-update [debian/patches/0005-do_not_depend_python2.patch]
See-also: https://lintian.debian.org/tags/invalid-dep3-format-patch-wrong-last-update.html

- - - - -
6773bb9f by Andreas Tille at 2026-11-11T17:41:46+01:00
Replace FSF postal address with a reference to https://www.gnu.org/licenses/.

Changes-By: lintian-brush
Fixes: lintian: source: old-fsf-address-in-copyright-file
See-also: https://lintian.debian.org/tags/old-fsf-address-in-copyright-file.html

- - - - -
a51c0963 by Andreas Tille at 2026-11-11T17:41:49+01:00
debputy lint --auto-fix (routine-update)

- - - - -
2db6849e by Andreas Tille at 2026-11-11T17:59:17+01:00
Rebase patches to latest upstream version

- - - - -
bf9eb79b by Andreas Tille at 2026-11-11T17:59:43+01:00
Resort changelog entries

- - - - -


21 changed files:

- + .github/workflows/cmake-multi-platform.yml
- CMakeLists.txt
- README.md
- debian/changelog
- debian/control
- debian/copyright
- debian/patches/cmake-version → debian/patches/0001-cmake-version.patch
- debian/patches/0002-Remove-libqes-from-CML.txt.patch
- − debian/patches/0005-do_not_depend_python2.patch
- − debian/patches/0006-update-cmake-version.patch
- debian/patches/series
- debian/watch
- docs/CMakeLists.txt
- docs/gitversion.py
- docs/usage.txt
- src/axe.c
- src/main.c
- tests/CMakeLists.txt
- tests/axe_cli_tests.py
- + tests/axe_simulation_tests.py
- tests/test_libaxe.c


Changes:

=====================================
.github/workflows/cmake-multi-platform.yml
=====================================
@@ -0,0 +1,70 @@
+# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
+# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
+name: CMake on multiple platforms
+
+on:
+  push:
+    branches: [ "master" ]
+  pull_request:
+    branches: [ "master" ]
+
+jobs:
+  build:
+    runs-on: ${{ matrix.os }}
+
+    strategy:
+      # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
+      fail-fast: false
+
+      # axe is POSIX-only (unistd.h, getopt.h, ...), so no MSVC/Windows job.
+      matrix:
+        include:
+          - os: ubuntu-latest
+            build_type: Release
+            c_compiler: gcc
+            cpp_compiler: g++
+          - os: ubuntu-latest
+            build_type: Release
+            c_compiler: clang
+            cpp_compiler: clang++
+          - os: macos-latest
+            build_type: Release
+            c_compiler: clang
+            cpp_compiler: clang++
+
+    steps:
+    - uses: actions/checkout at v4
+
+    - uses: actions/setup-python at v5
+      with:
+        python-version: "3.x"
+
+    - name: Install test dependencies
+      run: python3 -m pip install numpy
+
+    - name: Set reusable strings
+      # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
+      id: strings
+      shell: bash
+      run: |
+        echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
+
+    - name: Configure CMake
+      # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
+      # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
+      run: >
+        cmake -B ${{ steps.strings.outputs.build-output-dir }}
+        -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
+        -DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
+        -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
+        -S ${{ github.workspace }}
+
+    - name: Build
+      # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
+      run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
+
+    - name: Test
+      working-directory: ${{ steps.strings.outputs.build-output-dir }}
+      # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
+      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
+      run: ctest --build-config ${{ matrix.build_type }} --output-on-failure


=====================================
CMakeLists.txt
=====================================
@@ -1,5 +1,5 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 3.5...3.31)
 PROJECT(axe C)
-CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
 
 LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules")
 
@@ -8,6 +8,7 @@ SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 ENABLE_TESTING()
+SET(CMAKE_MACOSX_RPATH ON)
 
 IF (NOT CMAKE_BUILD_TYPE)
         SET(CMAKE_BUILD_TYPE Release)
@@ -17,10 +18,16 @@ IF (NOT AXE_VERSION)
 	# git describe as versioning
 	EXECUTE_PROCESS(COMMAND git describe
 			WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+			ERROR_QUIET
 			OUTPUT_VARIABLE AXE_VERSION
 			OUTPUT_STRIP_TRAILING_WHITESPACE)
 ENDIF()
 
+IF (NOT AXE_VERSION)
+	# Not a git checkout (e.g. a release tarball), or no tags
+	SET(AXE_VERSION "unknown")
+ENDIF()
+
 MESSAGE(STATUS "${CMAKE_BUILD_TYPE} build of axe version: ${AXE_VERSION}")
 
 ###############################


=====================================
README.md
=====================================
@@ -10,22 +10,36 @@ AXE
 [![Join the chat at https://gitter.im/axe-demultipexer/Lobby](https://badges.gitter.im/axe-demultipexer/Lobby.svg)](https://gitter.im/axe-demultipexer/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
 
 
-AXE very rapidly selects the optimal barcode present in a sequence read, even
+AXE very rapidly selects the optimal index present in a sequence read, even
 in the presence of sequencing errors. The algorithm is able to handle
-combinatorial barcoding, barcodes of differing length, and several mismatches
-per barcode. Early results indicate far improved accuracy and speed over
+combinatorial indexing, indexes of differing length, and several mismatches
+per index. Benchmarking results indicate far improved accuracy and speed over
 existing de-multiplexers. Unscientific trials show AXE processes more than
 500,000 reads per second.
 
-**Warning**: Axe has not yet been comprehensively tested. However, in the
-spirit of "release early and often", here it is.
 
-### Tests:
+Quick Start
+-----------
+
+### `conda`
+
+[![](https://img.shields.io/badge/Anaconda.org-0.3.3-blue.svg?style=flat-square)](https://anaconda.org/kdm801/axe-demultiplexer/)
+
+```
+mamba install -c kdm801 axe-demultiplexer
+```
+
+
+### From source
+
+To install on UNIX-like systems (Mac, Linux), get the dependencies (zlib and cmake, see below), and:
+
+    git clone --recursive https://github.com/kdmurray91/axe.git axe
+    cd axe
+    cmake .
+    sudo make install
 
-| TravisCI          | [![Build Status](https://travis-ci.org/kdmurray91/axe.svg?branch=master)](https://travis-ci.org/kdmurray91/axe) |
-| ----------------- | --- |
-| Test Coverage     | [![Coverage Status](https://img.shields.io/coveralls/kdmurray91/axe.svg)](https://coveralls.io/r/kdmurray91/axe?branch=master) |
-| Coverity Scans    | [![Coverity Scan Build Status](https://scan.coverity.com/projects/2666/badge.svg)](https://scan.coverity.com/projects/2666) |
+A tutorial on the usage of Axe is available at <https://axe-demultiplexer.readthedocs.org/en/latest/>, along with full documentation and a basic description of the algorithm.
 
 Important Note
 --------------
@@ -35,6 +49,7 @@ with version 0.3.0. Apologies for the inconvenience, this was required to
 make ``axe`` installable in Debian and its derivatives. Command-line usage
 did not change.**
 
+
 Installation:
 -------------
 
@@ -44,14 +59,6 @@ possible, I just don't have machines available to test. Please report any
 installation issues on any system as GitHub bugs and I'll do my best to sort
 them out.
 
-In short, on *NIX, get the dependencies (see below), and:
-
-    git clone --recursive https://github.com/kdmurray91/axe.git axe
-    cd axe
-    mkdir -p build && cd build
-    cmake ..
-    make
-    sudo make install
 
 To install to a prefix, as you would with `./configure --prefix` with the
 autotools build system, please use the following cmake command in place of the
@@ -67,7 +74,7 @@ For me, using `~/` as the prefix will install `axe` under `/home/kevin/bin` on
 GNU/Linux, and (if I had one) `/Users/kevin/bin` on Mac OSX.It's also wise to
 use `make install` not `sudo make install` when installing to a home directory.
 
-###Dependencies:
+### Dependencies:
 
 - cmake. This is installable via `sudo apt-get install cmake` on Debian based
   systems, or `brew install cmake` using homebrew on OS X.
@@ -75,6 +82,7 @@ use `make install` not `sudo make install` when installing to a home directory.
 - libqes, tinytest, libgsl and libdatrie (bundled in source, if you used
   `git clone --recursive` or an installation tarball. Otherwise, run
   `git submodule update --init`).
+- on OSX: `brew install libomp` to get OpenMP
 
 You'll possibly need to install zlib to your chosen prefix (e.g. `~/`) on
 supercomputers, which often have very old versions of zlib. To do so:
@@ -90,18 +98,11 @@ And then, use the following cmake command, assuming your prefix is `~/`:
     cmake -DCMAKE_INSTALL_PREFIX=$HOME -DZLIB_ROOT=$HOME ..
 
 
-Usage:
-------
-
-Full documentation, including a basic description of the algorithm, is hosted
-at https://axe-demultiplexer.readthedocs.org/en/latest/ .
-
-
-
 Publication
 -----------
 
-We have a preprint describing AXE up at http://www.biorxiv.org/content/early/2017/07/07/160606
+We have a paper describing AXE published in Bioinformatics https://academic.oup.com/bioinformatics/article/34/22/3924/5026649
+A [PDF is available](https://raw.githubusercontent.com/kdm9/axe-paper/master/oxford-final/axe-formatted.pdf?token=GHSAT0AAAAAABVH27O27FSFX47CB5FAUFZ6YV5NNQA)
 
 Versioning
 ----------


=====================================
debian/changelog
=====================================
@@ -1,3 +1,19 @@
+axe-demultiplexer (0.4.0+dfsg-1) UNRELEASED; urgency=medium
+
+  * Team upload.
+  * New upstream version
+  * Update Homepage and Source
+  * d/watch: version=5
+  * Standards-Version: 4.7.4 (routine-update)
+  * Reflow Uploaders field (cme)
+  * Remove Priority field (cme)
+  * Reformat the Last-Update date as ISO YYYY-MM-DD.
+  * Replace FSF postal address with a reference to
+    https://www.gnu.org/licenses/.
+  * debputy lint --auto-fix (routine-update)
+
+ -- Andreas Tille <tille at debian.org>  Wed, 11 Nov 2026 17:41:23 +0100
+
 axe-demultiplexer (0.3.3+dfsg-5) unstable; urgency=medium
 
   * Team upload.


=====================================
debian/control
=====================================
@@ -1,23 +1,25 @@
 Source: axe-demultiplexer
+Standards-Version: 4.7.4
 Maintainer: Debian Med Packaging Team <debian-med-packaging at lists.alioth.debian.org>
-Uploaders: Kevin Murray <kdmfoss at gmail.com>
+Uploaders:
+ Kevin Murray <kdmfoss at gmail.com>,
 Section: science
-Priority: optional
-Build-Depends: debhelper-compat (= 13),
-               cmake,
-               zlib1g-dev,
-               libqes-dev,
-               libgsl-dev,
-               python3-sphinx:native
-Standards-Version: 4.7.2
+Build-Depends:
+ debhelper-compat (= 14),
+ cmake,
+ libgsl-dev,
+ libqes-dev,
+ python3-sphinx:native,
+ zlib1g-dev,
 Vcs-Browser: https://salsa.debian.org/med-team/axe-demultiplexer
 Vcs-Git: https://salsa.debian.org/med-team/axe-demultiplexer.git
-Homepage: https://github.com/kdmurray91/axe
+Homepage: https://github.com/kdm9/axe
 
 Package: axe-demultiplexer
 Architecture: any
-Depends: ${shlibs:Depends},
-         ${misc:Depends}
+Depends:
+ ${misc:Depends},
+ ${shlibs:Depends},
 Description: Trie-based DNA sequencing read demultiplexer
  Axe very rapidly selects the optimal barcode present in a sequence read, even
  in the presence of sequencing errors. The algorithm is able to handle


=====================================
debian/copyright
=====================================
@@ -1,7 +1,7 @@
 Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
 Upstream-Name: axe
 Upstream-Contact: Kevin Murray <spam at kdmurray.id.au>
-Source: https://github.com/kdmurray91/axe
+Source: https://github.com/kdm9/axe
 Files-Excluded: src/gsl src/libqes
 
 Files: *.*
@@ -72,9 +72,8 @@ License: LGPL-2.1+
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  .
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 License: BSD-3-clause
  Redistribution and use in source and binary forms, with or without


=====================================
debian/patches/cmake-version → debian/patches/0001-cmake-version.patch
=====================================
@@ -7,11 +7,9 @@ Get program version from Debian Changelog, not from git.
  CMakeLists.txt | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
 
-diff --git a/CMakeLists.txt b/CMakeLists.txt
-index 27d91f3..f4ea21e 100644
 --- a/CMakeLists.txt
 +++ b/CMakeLists.txt
-@@ -13,13 +13,11 @@ IF (NOT CMAKE_BUILD_TYPE)
+@@ -14,14 +14,12 @@ IF (NOT CMAKE_BUILD_TYPE)
          SET(CMAKE_BUILD_TYPE Release)
  ENDIF()
  
@@ -19,14 +17,16 @@ index 27d91f3..f4ea21e 100644
 -	# git describe as versioning
 -	EXECUTE_PROCESS(COMMAND git describe
 -			WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+-			ERROR_QUIET
 -			OUTPUT_VARIABLE AXE_VERSION
 -			OUTPUT_STRIP_TRAILING_WHITESPACE)
 -ENDIF()
 +# Get version from debian changelog
 +EXECUTE_PROCESS(COMMAND dpkg-parsechangelog -S Version
-+		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-+		OUTPUT_VARIABLE AXE_VERSION
-+		OUTPUT_STRIP_TRAILING_WHITESPACE)
- 
- MESSAGE(STATUS "${CMAKE_BUILD_TYPE} build of axe version: ${AXE_VERSION}")
++               WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
++               ERROR_QUIET
++               OUTPUT_VARIABLE AXE_VERSION
++               OUTPUT_STRIP_TRAILING_WHITESPACE)
  
+ IF (NOT AXE_VERSION)
+ 	# Not a git checkout (e.g. a release tarball), or no tags


=====================================
debian/patches/0002-Remove-libqes-from-CML.txt.patch
=====================================
@@ -6,11 +6,9 @@ Subject: Remove libqes from CML.txt
  CMakeLists.txt | 2 --
  1 file changed, 2 deletions(-)
 
-diff --git a/CMakeLists.txt b/CMakeLists.txt
-index f4ea21e..aa04d79 100644
 --- a/CMakeLists.txt
 +++ b/CMakeLists.txt
-@@ -57,8 +57,6 @@ INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})
+@@ -64,8 +64,6 @@ INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})
  CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/src/axe_config.h.in
                 ${CMAKE_BINARY_DIR}/axe_config.h)
  


=====================================
debian/patches/0005-do_not_depend_python2.patch deleted
=====================================
@@ -1,30 +0,0 @@
-Author: Andreas Tille <tille at debian.org>
-Last-Update: Thu, 26 Apr 2018 15:14:56 +0200
-Description: Enable running tests using Python3 instead of Python2
- When switching Build-Depends from python-sphinx to python3-sphinx
- the Python 2 interpreter was not installed any more in the build
- time environment.  This was used to trigger the build time test
- suite.  By changing the script tp Python3 it is possible to get
- rid of this dependency.
-
---- a/tests/axe_cli_tests.py
-+++ b/tests/axe_cli_tests.py
-@@ -1,5 +1,5 @@
--#!/usr/bin/env python
--from __future__ import print_function
-+#!/usr/bin/env python3
-+
- import hashlib
- import logging
- import os
---- a/tests/CMakeLists.txt
-+++ b/tests/CMakeLists.txt
-@@ -19,7 +19,7 @@ ADD_TEST(NAME "UnitTests" COMMAND test_a
- SET(COVERAGE_CMD test_axe)
- SET(COVERAGE_OUT "${CMAKE_BINARY_DIR}/coverage_html")
- 
--ADD_TEST(NAME "IntegrationTests" COMMAND python
-+ADD_TEST(NAME "IntegrationTests" COMMAND python3
-          ${CMAKE_BINARY_DIR}/bin/axe_cli_tests.py
-          ${CMAKE_BINARY_DIR})
- 


=====================================
debian/patches/0006-update-cmake-version.patch deleted
=====================================
@@ -1,15 +0,0 @@
-From de61528152105a045fdd12894e9a7996c64e879e Mon Sep 17 00:00:00 2001
-From: "Dr. K.D. Murray" <foss at kdmurray.id.au>
-Date: Tue, 20 May 2025 07:36:51 +1000
-Subject: update cmake version
-Bug-Debian: https://bugs.debian.org/1112715
-
---- a/CMakeLists.txt
-+++ b/CMakeLists.txt
-@@ -1,5 +1,5 @@
-+CMAKE_MINIMUM_REQUIRED(VERSION 3.5...3.31)
- PROJECT(axe C)
--CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
- 
- LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules")
- 


=====================================
debian/patches/series
=====================================
@@ -1,6 +1,4 @@
-cmake-version
+0001-cmake-version.patch
 0002-Remove-libqes-from-CML.txt.patch
 0003-Use-standard-include-path-of-gsl-headers.patch
 0004-Dynamically-link-to-libqes.patch
-0005-do_not_depend_python2.patch
-0006-update-cmake-version.patch


=====================================
debian/watch
=====================================
@@ -1,3 +1,6 @@
-version=4
-opts="repacksuffix=+dfsg,dversionmangle=s/\+dfsg.*//,filenamemangle=s/(?:.*?)?v?(\d[\d.]*)\.tar\.gz/axe-demultiplexer_$1.tar.gz/" \
-  https://github.com/kdmurray91/axe/tags .*/v?(\d\S*)\.tar\.gz
+Version: 5
+
+Template: Github
+Dist: https://github.com/kdm9/axe
+Dversion-Mangle: auto
+Repack-Suffix: +dfsg


=====================================
docs/CMakeLists.txt
=====================================
@@ -1,5 +1,8 @@
+# Docs are not built by default: a broken sphinx install shouldn't stop axe
+# from building. Use -DBUILD_DOCS=ON to build (and install) them.
+OPTION(BUILD_DOCS "Build the documentation with sphinx" OFF)
 FIND_PROGRAM(SPHINXBUILD sphinx-build)
-IF(SPHINXBUILD)
+IF(SPHINXBUILD AND BUILD_DOCS)
     SET(ALLSPHINXOPTS -q -D latex_paper_size=a4)
     ADD_CUSTOM_TARGET(doc_html
         COMMAND ${SPHINXBUILD} -b html ${ALLSPHINXOPTS} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_BINARY_DIR}/doc/html
@@ -20,7 +23,7 @@ IF(SPHINXBUILD)
     ADD_CUSTOM_TARGET(doc ALL DEPENDS doc_man doc_html doc_onehtml)
     INSTALL(FILES ${CMAKE_BINARY_DIR}/doc/man/axe.1 DESTINATION "share/man/man1" RENAME "axe-demux.1")
     SET_DIRECTORY_PROPERTIES(PROPERTY ADDITIONAL_MAKE_CLEAN_FILES doc/)
-ELSE()
-    MESSAGE(WARNING "Cannot build documenation, sphinx isn't installed")
+ELSEIF(BUILD_DOCS)
+    MESSAGE(FATAL_ERROR "BUILD_DOCS is ON, but sphinx-build wasn't found")
 ENDIF()
 


=====================================
docs/gitversion.py
=====================================
@@ -8,8 +8,8 @@
 # versioneer-0.13 (https://github.com/warner/python-versioneer)
 
 # these strings will be replaced by git during git-archive
-git_refnames = " (tag: 0.3.3)"
-git_full = "4b985d2ac95b046e96f0a83cb98f1f5e31fa49bd"
+git_refnames = " (tag: 0.4.0)"
+git_full = "c353573118ff16631ecc40a928deb5304e8ed82e"
 
 # these strings are filled in when 'setup.py versioneer' creates _version.py
 tag_prefix = ""


=====================================
docs/usage.txt
=====================================
@@ -12,13 +12,14 @@ OPTIONS:
     -2, --trim-r2	Trim barcode from R2 read as well as R1. [flag, default OFF]
     -b, --barcodes	Barcode file. See --help for example. [file]
     -f, --fwd-in	Input forward read. [file]
-    -F, --fwd-out	Output forward read prefix. [file]
+    -F, --fwd-out	Output forward read prefix. [file prefix or existing directory]
     -r, --rev-in	Input reverse read. [file]
-    -R, --rev-out	Output reverse read prefix. [file]
+    -R, --rev-out	Output reverse read prefix. [file prefix or existing directory]
     -i, --ilfq-in	Input interleaved paired reads. [file]
-    -I, --ilfq-out	Output interleaved paired reads prefix. [file]
+    -I, --ilfq-out	Output interleaved paired reads prefix. [file prefix or existing directory]
     -t, --table-file	Output a summary table of demultiplexing statistics to file. [file]
     -h, --help		Print this usage plus additional help.
     -V, --version	Print version string.
     -v, --verbose	Be more verbose. Additive, -vv is more vebose than -v.
     -q, --quiet		Be very quiet.
+


=====================================
src/axe.c
=====================================
@@ -180,15 +180,20 @@ _axe_format_outfile_path (const char *prefix, const char *id, int read,
     }
 
     prefix_len = strlen(prefix);
+    our_prefix = qes_malloc(prefix_len + 2);
+    memcpy(our_prefix, prefix, prefix_len);
+    our_prefix[prefix_len] = '\0';
     if (stat(prefix, &statbuff) == 0 && S_ISDIR(statbuff.st_mode)) {
-        /* Our prefix is a directory, don't add '_' */
-        our_prefix = strdup(prefix);
+        /* Our prefix is a directory, so files go inside it. Add a path
+         * separator rather than '_', unless there already is one. */
+        if (prefix_len == 0 || prefix[prefix_len - 1] != '/') {
+            our_prefix[prefix_len] = '/';
+            our_prefix[prefix_len + 1] = '\0';
+        }
     } else {
-        /* Duplicate and append an underscore to prefix */
-        our_prefix = qes_malloc(prefix_len + 2);
-        our_prefix[prefix_len + 1] = '\0';
-        strncpy(our_prefix, prefix, prefix_len);
+        /* Append an underscore to prefix */
         our_prefix[prefix_len] = '_';
+        our_prefix[prefix_len + 1] = '\0';
     }
 
     if (read > 0) {
@@ -320,6 +325,25 @@ error:
     return NULL;
 }
 
+/* Upper-case ``seq`` in place, and check that it only has bases that the
+ * barcode tries can hold. */
+static int
+axe_barcode_seq_ok(char *seq)
+{
+    char *c = NULL;
+
+    if (seq == NULL || seq[0] == '\0') {
+        return 0;
+    }
+    for (c = seq; *c != '\0'; c++) {
+        *c = (char)toupper((unsigned char)*c);
+        if (strchr("ACGTN", *c) == NULL) {
+            return 0;
+        }
+    }
+    return 1;
+}
+
 int
 axe_read_barcodes(struct axe_config *config)
 {
@@ -334,6 +358,7 @@ axe_read_barcodes(struct axe_config *config)
     size_t linesz = 128;
     ssize_t linelen = 0;
     size_t iii = 0;
+    size_t jjj = 0;
 
     if (!axe_config_ok(config)) {
         return -1;
@@ -343,6 +368,14 @@ axe_read_barcodes(struct axe_config *config)
     line = qes_malloc(linesz);
 
     while ((linelen = qes_file_readline_realloc(qf, &line, &linesz)) > 0) {
+        /* Skip blank lines */
+        tmp = line;
+        while (*tmp != '\0' && isspace((unsigned char)*tmp)) {
+            tmp++;
+        }
+        if (*tmp == '\0') {
+            continue;
+        }
         /* Skip an optional header line */
         if (strncmp(line, "Barcode", 7) == 0 || \
             strncmp(line, "barcode", 7) == 0) {
@@ -372,14 +405,52 @@ axe_read_barcodes(struct axe_config *config)
                                     "UNIX line endings.");
             goto error;
         }
+        /* Barcodes are read into fixed-size buffers, so detect truncation. */
+        if (this_barcode->len1 >= 99 || this_barcode->len2 >= 99 ||
+                this_barcode->idlen >= 99) {
+            qes_log_format_fatal(config->logger,
+                    "Barcode or ID is too long (max 98 characters) in "
+                    "line '%s'\n", line);
+            goto error;
+        }
+        if (!axe_barcode_seq_ok(this_barcode->seq1) ||
+                (config->match_combo &&
+                 !axe_barcode_seq_ok(this_barcode->seq2))) {
+            qes_log_format_fatal(config->logger,
+                    "Barcodes may only contain A, C, G, T or N, in "
+                    "line '%s'\n", line);
+            goto error;
+        }
         /* Replace all bad chars with '-' */
         tmp = strpbrk(this_barcode->id, bad_fname_chars);
         while (tmp != NULL) {
             *tmp = '-'; /* Replace with dash */
             tmp = strpbrk(tmp + 1, bad_fname_chars);
         }
+        /* Two samples with one ID would write to the same file */
+        for (jjj = 0; jjj < n_barcode_pairs; jjj++) {
+            if (strcmp(barcodes[jjj]->id, this_barcode->id) == 0) {
+                qes_log_format_fatal(config->logger,
+                        "Sample ID '%s' is used more than once in the "
+                        "barcode file\n", this_barcode->id);
+                goto error;
+            }
+        }
         /* Add the barcode to the array */
         barcodes[n_barcode_pairs++] = this_barcode;
+        this_barcode = NULL;
+    }
+    if (linelen < -1) {
+        qes_log_format_fatal(config->logger,
+                             "Error reading barcode file %s\n",
+                             config->barcode_file);
+        goto error;
+    }
+    if (n_barcode_pairs == 0) {
+        qes_log_format_fatal(config->logger,
+                             "No barcodes found in barcode file %s\n",
+                             config->barcode_file);
+        goto error;
     }
 
     /* Save the array to the config struct */
@@ -395,11 +466,11 @@ axe_read_barcodes(struct axe_config *config)
     return 0;
 
 error:
-    if (barcodes != NULL) {
-        for (iii = 0; iii < n_barcode_pairs; iii++) {
-            axe_barcode_destroy(barcodes[iii]);
-        }
+    axe_barcode_destroy(this_barcode);
+    for (iii = 0; iii < n_barcode_pairs; iii++) {
+        axe_barcode_destroy(barcodes[iii]);
     }
+    qes_free(barcodes);
     qes_file_close(qf);
     qes_free(line);
     return 1;
@@ -451,7 +522,7 @@ setup_barcode_lookup_combo(struct axe_config *config)
     assert(seq1_trie != NULL && seq2_trie != NULL);
     for (iii = 0; iii < config->n_barcode_pairs; iii++) {
         this_barcode = config->barcodes[iii];
-        if (!axe_barcode_ok(this_barcode)) {
+        if (!axe_barcode_ok_combo(this_barcode)) {
             qes_log_format_fatal(config->logger,
                                  "setup_lookup -- Bad barcode at %zu\n",
                                  iii);
@@ -485,6 +556,14 @@ setup_barcode_lookup_combo(struct axe_config *config)
         res = axe_trie_get(seq2_trie, this_barcode->seq2,
                             (intptr_t *)(&bcd2));
         if (!res) goto error;
+        if (config->barcode_lookup[bcd1][bcd2] >= 0) {
+            qes_log_format_fatal(config->logger,
+                    "setup_lookup -- Barcode pair %s,%s is used by both "
+                    "%s and %s\n", this_barcode->seq1, this_barcode->seq2,
+                    config->barcodes[config->barcode_lookup[bcd1][bcd2]]->id,
+                    this_barcode->id);
+            goto error;
+        }
         config->barcode_lookup[bcd1][bcd2] = iii;
     }
     ret = 0;
@@ -568,227 +647,240 @@ axe_make_zmode(const struct axe_config *config)
     return strdup("wT");
 }
 
-static inline int
-load_tries_combo(struct axe_config *config)
+struct axe_load_ctx {
+    struct axe_trie *trie;
+    int failed;
+};
+
+/* Copy the mutants collected for one mismatch level into the real trie. */
+static bool
+axe_load_candidate_cb(const AlphaChar *key, TrieData data, void *user_data)
 {
-    int bcd1 = -1;
-    int bcd2 = -1;
-    int retval = 0;
-    char **mutated = NULL;
-    size_t num_mutated = 0;
-    int ret = 0;
+    struct axe_load_ctx *ctx = user_data;
+
+    /* Ambiguous mutants are kept (with a negative value) so that they also
+     * block more distant mutants of other barcodes. They never match. */
+    if (axe_trie_add(ctx->trie, key, data) != 0) {
+        ctx->failed = 1;
+        return false;
+    }
+    return true;
+}
+
+/* Load ``n`` barcode sequences into ``trie``, as well as all of their mutants
+ * of up to ``config->mismatches`` substitutions. ``idx[i]`` is the value that
+ * ``seqs[i]`` and its mutants map to. Sequences must be unique.
+ *
+ * The exact barcodes are all loaded first, then mutants are added one
+ * mismatch level at a time, so that a closer match always wins. A mutant that
+ * collides with a closer entry, or with another barcode's mutant at the same
+ * level, is an error unless --permissive was given. In that case such mutants
+ * are left out (so those reads only match a barcode more closely, or not at
+ * all). */
+static int
+axe_load_trie(struct axe_config *config, struct axe_trie *trie,
+              const char *what, char *const *seqs, const size_t *lens,
+              const intptr_t *idx, size_t n)
+{
+    size_t level = 0;
     size_t iii = 0;
-    size_t jjj = 0;
     size_t mmm = 0;
-    struct axe_barcode *this_bcd = NULL;
-    intptr_t tmp = 0;
 
-    if (!axe_config_ok(config)) {
-        fprintf(stderr, "[load_tries] Bad config\n");
-        ret = -1;
-        goto exit;
-    }
-    /* Make mutated barcodes and add to trie */
-    for (iii = 0; iii < config->n_barcode_pairs; iii++) {
-        this_bcd = config->barcodes[iii];
-        if (!axe_barcode_ok(this_bcd)) {
+    for (iii = 0; iii < n; iii++) {
+        if (axe_trie_add(trie, seqs[iii], idx[iii]) != 0) {
             qes_log_format_fatal(config->logger,
-                    "load_tries -- Bad R1 barcode at %zu\n", iii);
-            ret = -1;
-            goto exit;
+                    "load_tries -- Could not load %s barcode %s into trie "
+                    "(is it a duplicate?)\n", what, seqs[iii]);
+            return 1;
         }
-        /* Either lookup the index of the first read in the barcode table, or
-         * insert this barcode into the table, storing its index.
-         * Note the NOT here. */
-        if (!axe_trie_get(config->fwd_trie, this_bcd->seq1, &tmp)) {
-            ret = axe_trie_add(config->fwd_trie, this_bcd->seq1, ++bcd1);
-            if (ret != 0) {
-                qes_log_format_fatal(config->logger,
-                        "load_tries -- Could not load barcode %s into trie %zu\n",
-                        this_bcd->seq1, iii);
-                ret = 1;
-                goto exit;
-            }
-        } else {
-            continue;
+    }
+    for (level = 1; level <= config->mismatches; level++) {
+        struct axe_trie *cand = axe_trie_create();
+        struct axe_load_ctx ctx = {trie, 0};
+        size_t n_shadowed = 0;
+        size_t n_ambiguous = 0;
+        size_t n_cand = 0;
+
+        if (cand == NULL) {
+            return 1;
         }
-        for (jjj = 1; jjj <= config->mismatches; jjj++) {
-            /* Do the forwards read barcode */
-            mutated = hamming_mutate_dna(&num_mutated, this_bcd->seq1,
-                                         this_bcd->len1, jjj, 0);
+        for (iii = 0; iii < n; iii++) {
+            char **mutated = NULL;
+            size_t num_mutated = 0;
+
+            if (lens[iii] < level) {
+                /* Can't have more mismatches than bases */
+                continue;
+            }
+            mutated = hamming_mutate_dna(&num_mutated, seqs[iii], lens[iii],
+                                         level, 0);
             if (mutated == NULL) {
-                ret = 1;
-                goto exit;
+                qes_log_format_fatal(config->logger,
+                        "load_tries -- Could not mutate barcode %s\n",
+                        seqs[iii]);
+                axe_trie_destroy(cand);
+                return 1;
             }
             for (mmm = 0; mmm < num_mutated; mmm++) {
-                ret = axe_trie_add(config->fwd_trie, mutated[mmm], bcd1);
-                if (ret != 0) {
-                    if (config->permissive) {
-                        if (config->verbosity >= 0) {
-                            qes_log_format_warning(config->logger,
-                                "load_tries -- warning: Will only match to %dmm\n",
-                                 (int)jjj - 1);
+                intptr_t other = -1;
+                int conflict = 0;
+
+                if (axe_trie_get(trie, mutated[mmm], &other)) {
+                    /* Collides with an exact barcode, or a closer mutant */
+                    conflict = other != idx[iii];
+                    if (conflict && config->permissive) {
+                        n_shadowed++;
+                    }
+                } else if (axe_trie_get(cand, mutated[mmm], &other)) {
+                    /* Equally close to some other barcode: ambiguous */
+                    conflict = other != idx[iii];
+                    if (conflict && config->permissive) {
+                        if (other >= 0) {
+                            trie_store(cand->trie, mutated[mmm], -2);
+                            n_ambiguous++;
                         }
-                        axe_trie_delete(config->fwd_trie, mutated[mmm]);
-                        qes_free(mutated[mmm]);
-                        continue;
                     }
+                } else if (axe_trie_add(cand, mutated[mmm], idx[iii]) != 0) {
+                    qes_log_format_fatal(config->logger,
+                            "load_tries -- Could not load mutant of %s\n",
+                            seqs[iii]);
+                    goto fail;
+                } else {
+                    n_cand++;
+                }
+                if (conflict && !config->permissive) {
                     qes_log_format_fatal(config->logger,
-                            "load_tries -- Barcode %s already in fwd trie (%dmm) %s\n",
-                            mutated[mmm], (int)jjj, this_bcd->seq1);
-                    retval = 1;
-                    goto exit;
+                            "load_tries -- %s barcode %s with %zu mismatch(es)"
+                            " (%s) is also within %zu mismatch(es) of another "
+                            "barcode. Use fewer mismatches, or --permissive.\n",
+                            what, seqs[iii], level, mutated[mmm], level);
+                    goto fail;
                 }
                 qes_free(mutated[mmm]);
+                continue;
+
+fail:
+                for (; mmm < num_mutated; mmm++) {
+                    qes_free(mutated[mmm]);
+                }
+                qes_free(mutated);
+                axe_trie_destroy(cand);
+                return 1;
             }
             qes_free(mutated);
         }
+        /* (enumerating an empty trie is not safe) */
+        if (n_cand > 0 &&
+                (!trie_enumerate(cand->trie, axe_load_candidate_cb, &ctx) ||
+                 ctx.failed)) {
+            qes_log_format_fatal(config->logger,
+                    "load_tries -- Could not load %s barcodes with %zu "
+                    "mismatch(es)\n", what, level);
+            axe_trie_destroy(cand);
+            return 1;
+        }
+        axe_trie_destroy(cand);
+        if ((n_shadowed > 0 || n_ambiguous > 0) && config->verbosity >= 0) {
+            qes_log_format_warning(config->logger,
+                    "load_tries -- %s barcodes: %zu mutated barcodes with %zu "
+                    "mismatch(es) conflict with a closer barcode, and %zu are "
+                    "ambiguous. These will not be matched.\n", what,
+                    n_shadowed, level, n_ambiguous);
+        }
     }
-    /* Ditto for the reverse read */
-    for (iii = 0; iii < config->n_barcode_pairs; iii++) {
-        this_bcd = config->barcodes[iii];
-        /* Likewise for the reverse read index */
-        if (!axe_trie_get(config->rev_trie, this_bcd->seq2, &tmp)) {
-            ret = axe_trie_add(config->rev_trie, this_bcd->seq2, ++bcd2);
-            if (ret != 0) {
+    return 0;
+}
+
+static int
+load_tries_combo(struct axe_config *config)
+{
+    size_t which = 0;
+    size_t iii = 0;
+    int ret = 0;
+
+    if (!axe_config_ok(config)) {
+        fprintf(stderr, "[load_tries] Bad config\n");
+        return -1;
+    }
+    /* Unique barcodes of R1 then R2, numbered in order of first appearance.
+     * This has to match setup_barcode_lookup_combo. */
+    for (which = 0; which < 2 && ret == 0; which++) {
+        struct axe_trie *seen = axe_trie_create();
+        struct axe_trie *target = which == 0 ? config->fwd_trie :
+                                               config->rev_trie;
+        char **seqs = qes_calloc(config->n_barcode_pairs, sizeof(*seqs));
+        size_t *lens = qes_calloc(config->n_barcode_pairs, sizeof(*lens));
+        intptr_t *idx = qes_calloc(config->n_barcode_pairs, sizeof(*idx));
+        size_t n = 0;
+        intptr_t tmp = 0;
+
+        for (iii = 0; iii < config->n_barcode_pairs && ret == 0; iii++) {
+            struct axe_barcode *bcd = config->barcodes[iii];
+            char *seq = which == 0 ? bcd->seq1 : bcd->seq2;
+            size_t len = which == 0 ? bcd->len1 : bcd->len2;
+
+            if (!axe_barcode_ok_combo(bcd)) {
                 qes_log_format_fatal(config->logger,
-                        "load_tries -- Could not load barcode %s into trie %zu\n",
-                        this_bcd->seq2, iii);
-                retval = 1;
-                goto exit;
-            }
-        } else {
-            continue;
-        }
-        for (jjj = 1; jjj <= config->mismatches; jjj++) {
-            num_mutated = 0;
-            mutated = hamming_mutate_dna(&num_mutated, this_bcd->seq2,
-                                         this_bcd->len2, jjj, 0);
-            if (mutated == NULL) {
+                        "load_tries -- Bad barcode at %zu\n", iii);
                 ret = 1;
-                goto exit;
-            }
-            for (mmm = 0; mmm < num_mutated; mmm++) {
-                ret = axe_trie_add(config->rev_trie, mutated[mmm], bcd2);
-                if (ret != 0) {
-                    if (config->permissive) {
-                        if (config->verbosity >= 0) {
-                            qes_log_format_warning(config->logger,
-                                "load_tries -- Will only match %s to %dmm\n",
-                                this_bcd->id, (int)jjj - 1);
-                        }
-                        trie_delete(config->rev_trie->trie,
-                                    mutated[mmm]);
-                        qes_free(mutated[mmm]);
-                        continue;
-                    }
-                    qes_log_format_fatal(config->logger,
-                            "load_tries -- Barcode %s already in rev trie (%dmm) %s\n",
-                            mutated[mmm], (int)jjj, this_bcd->seq1);
-                    retval = 1;
-                    goto exit;
+            } else if (!axe_trie_get(seen, seq, &tmp)) {
+                if (axe_trie_add(seen, seq, (intptr_t)n) != 0) {
+                    ret = 1;
+                    continue;
                 }
-                qes_free(mutated[mmm]);
+                seqs[n] = seq;
+                lens[n] = len;
+                idx[n] = (intptr_t)n;
+                n++;
             }
-            qes_free(mutated);
         }
-    }
-    /* we got here, so we succeeded. set retval accordingly */
-    retval = 0;
-
-exit:
-    if (mutated != NULL) {
-        for (mmm = 0; mmm < num_mutated; mmm++) {
-            qes_free(mutated[mmm]);
+        if (ret == 0) {
+            ret = axe_load_trie(config, target, which == 0 ? "R1" : "R2",
+                                seqs, lens, idx, n);
         }
-        qes_free(mutated);
+        axe_trie_destroy(seen);
+        qes_free(seqs);
+        qes_free(lens);
+        qes_free(idx);
     }
-    return retval;
+    return ret;
 }
 
-static inline int
+static int
 load_tries_single(struct axe_config *config)
 {
-    char **mutated = NULL;
-    size_t num_mutated = 0;
-    int ret = 0;
+    char **seqs = NULL;
+    size_t *lens = NULL;
+    intptr_t *idx = NULL;
     size_t iii = 0;
-    size_t jjj = 0;
-    size_t mmm = 0;
-    intptr_t tmp = 0;
-    int retval = -1;
-    struct axe_barcode *this_bcd = NULL;
+    int ret = 0;
 
     if (!axe_config_ok(config)) {
         fprintf(stderr, "[load_tries] Bad config\n");
         return -1;
     }
-    /* Make mutated barcodes and add to trie */
+    seqs = qes_calloc(config->n_barcode_pairs, sizeof(*seqs));
+    lens = qes_calloc(config->n_barcode_pairs, sizeof(*lens));
+    idx = qes_calloc(config->n_barcode_pairs, sizeof(*idx));
     for (iii = 0; iii < config->n_barcode_pairs; iii++) {
-        this_bcd = config->barcodes[iii];
-        if (!axe_barcode_ok(this_bcd)) {
-            fprintf(stderr, "[load_tries] Bad barcode at %zu\n", iii);
-            return -1;
-        }
-        /* Either lookup the index of the first read in the barcode table, or
-         * insert this barcode into the table, storing its index.
-         * Note the NOT here. */
-        if (!axe_trie_get(config->fwd_trie, this_bcd->seq1, &tmp)) {
-            ret = axe_trie_add(config->fwd_trie, this_bcd->seq1, (int)iii);
-            if (ret != 0) {
-                fprintf(stderr,
-                        "ERROR: Could not load barcode %s into trie %zu\n",
-                        this_bcd->seq1, iii);
-                return 1;
-            }
-        } else {
-            fprintf(stderr, "ERROR: Duplicate barcode %s\n", this_bcd->seq1);
-            return 1;
-        }
-        for (jjj = 1; jjj <= config->mismatches; jjj++) {
-            mutated = hamming_mutate_dna(&num_mutated, this_bcd->seq1,
-                                         this_bcd->len1, jjj, 0);
-            if (mutated == NULL) {
-                ret = 1;
-                goto exit;
-            }
-            for (mmm = 0; mmm < num_mutated; mmm++) {
-                ret = axe_trie_add(config->fwd_trie, mutated[mmm], iii);
-                if (ret != 0) {
-                    if (config->permissive) {
-                        if (config->verbosity >= 0) {
-                            fprintf(stderr,
-                                    "[%s] warning: Will only match to %dmm\n",
-                                    __func__, (int)jjj - 1);
-                        }
-                        trie_delete(config->fwd_trie->trie,
-                                    mutated[mmm]);
-                        qes_free(mutated[mmm]);
-                        continue;
-                    }
-                    fprintf(stderr,
-                            "[%s] ERROR: Barcode %s already in trie (%dmm)\n",
-                            __func__, mutated[mmm], (int)jjj);
-                    retval = 1;
-                    goto exit;
-                }
-                qes_free(mutated[mmm]);
-            }
-            qes_free(mutated);
-            num_mutated = 0;
+        if (!axe_barcode_ok(config->barcodes[iii])) {
+            qes_log_format_fatal(config->logger,
+                    "load_tries -- Bad barcode at %zu\n", iii);
+            ret = 1;
+            goto exit;
         }
+        seqs[iii] = config->barcodes[iii]->seq1;
+        lens[iii] = config->barcodes[iii]->len1;
+        idx[iii] = (intptr_t)iii;
     }
-    /* we got here, so we succeeded */
-    retval = 0;
+    ret = axe_load_trie(config, config->fwd_trie, "R1", seqs, lens, idx,
+                        config->n_barcode_pairs);
 
 exit:
-    if (mutated != NULL) {
-        for (mmm = 0; mmm < num_mutated; mmm++) {
-            qes_free(mutated[mmm]);
-        }
-        qes_free(mutated);
-    }
-    return retval;
+    qes_free(seqs);
+    qes_free(lens);
+    qes_free(idx);
+    return ret;
 }
 
 int
@@ -805,6 +897,14 @@ axe_make_outputs(struct axe_config *config)
         fprintf(stderr, "[make_outputs] Bad config\n");
         return -1;
     }
+    if (config->out_mode == READS_SINGLE &&
+            (config->in_mode == READS_PAIRED ||
+             config->in_mode == READS_INTERLEAVED) &&
+            config->verbosity >= 0) {
+        qes_log_message_warning(config->logger,
+                "make_outputs -- Paired input, but no reverse/interleaved "
+                "output given (-R or -I). Reverse reads will be discarded.\n");
+    }
     file_ext = axe_make_file_ext(config);
     zmode = axe_make_zmode(config);
     config->outputs = qes_calloc(config->n_barcode_pairs,
@@ -878,6 +978,8 @@ axe_make_outputs(struct axe_config *config)
                 name_fwd);
         goto error;
     }
+    qes_free(name_fwd);
+    qes_free(name_rev);
     qes_free(file_ext);
     qes_free(zmode);
     return 0;
@@ -909,81 +1011,106 @@ axe_load_tries(struct axe_config *config)
     return ret;
 }
 
-static inline int
-write_barcoded_read_combo(struct axe_output *out, struct qes_seq *seq1,
-                          struct qes_seq *seq2, size_t bcd1_len,
-                          size_t bcd2_len)
+/* Write ``seq`` to ``sf`` with its first ``trim`` bases removed. The record is
+ * temporarily advanced, and *always* restored before returning, so that the
+ * seq can still be freed/reused. If the read is no longer than ``trim``, we
+ * write a single N rather than an empty/underflowed record, so that read
+ * counts (and mate pairing) are preserved. */
+static int
+write_trimmed(struct qes_seqfile *sf, struct qes_seq *seq, size_t trim)
 {
-    int ret = 0;
+    size_t qtrim = 0;
+    ssize_t ret = 0;
+
+    if (sf == NULL) {
+        /* Output mode has no file for this read (e.g. R2 w/ single output) */
+        return 1;
+    }
+    if (trim > 0 && seq->seq.len <= trim) {
+        seq->seq.str[0] = 'N';
+        seq->seq.str[1] = '\0';
+        seq->seq.len = 1;
+        if (seq->qual.len > 0) {
+            /* Keep first qual 'base' */
+            seq->qual.str[1] = '\0';
+            seq->qual.len = 1;
+        }
+        trim = 0;
+    }
+    qtrim = trim < seq->qual.len ? trim : seq->qual.len;
+    seq->seq.str += trim;
+    seq->seq.len -= trim;
+    seq->qual.str += qtrim;
+    seq->qual.len -= qtrim;
+    ret = qes_seqfile_write(sf, seq);
+    seq->seq.str -= trim;
+    seq->seq.len += trim;
+    seq->qual.str -= qtrim;
+    seq->qual.len += qtrim;
+    return ret < 1;
+}
 
-    if (seq1->seq.len <= bcd1_len) {
-        /* Truncate seqs to N */
-        seq1->seq.str[0] = 'N';
-        seq1->seq.str[1] = '\0';
-        seq1->seq.len = 1;
-        /* Keep first qual 'base' */
-        seq1->qual.str[1] = '\0';
-        seq1->qual.len = 1;
-    }
-    if (seq2->seq.len <= bcd2_len) {
-        /* Truncate seqs to N */
-        seq2->seq.str[0] = 'N';
-        seq2->seq.str[1] = '\0';
-        seq2->seq.len = 1;
-        /* Keep first qual 'base' */
-        seq2->qual.str[1] = '\0';
-        seq2->qual.len = 1;
-    }
-    /* Bit of the ol' switcheroo. We keep the seq's char pointers, so we
-       need to switch them back to their orig. values, but don't want to
-       copy. Kludgy, I know. */
-    seq1->seq.str += bcd1_len;
-    seq1->seq.len -= bcd1_len;
-    seq1->qual.str += bcd1_len;
-    seq1->qual.len -= bcd1_len;
-    ret = qes_seqfile_write(out->fwd_file, seq1);
-    if (ret < 1) {
-        fprintf(stderr,
-                "[process_file] Error: writing to fwd file %s failed\n%s\n",
-                out->fwd_file->qf->path,
-                qes_file_error(out->fwd_file->qf));
-        seq1->seq.str -= bcd1_len;
-        seq1->seq.len += bcd1_len;
-        seq1->qual.str -= bcd1_len;
-        seq1->qual.len += bcd1_len;
+/* Write a (possibly paired) matched read to its output, trimming ``trim1``
+ * bases from seq1 and ``trim2`` from seq2. Returns 0 on success. */
+static int
+write_matched_reads(struct axe_config *config, struct axe_output *out,
+                    struct qes_seq *seq1, struct qes_seq *seq2,
+                    size_t trim1, size_t trim2)
+{
+    if (write_trimmed(out->fwd_file, seq1, trim1)) {
+        qes_log_format_fatal(config->logger,
+                "process_file -- Writing to file %s failed\n%s\n",
+                out->fwd_file->qf->path, qes_file_error(out->fwd_file->qf));
         return 1;
     }
-    seq1->seq.str -= bcd1_len;
-    seq1->seq.len += bcd1_len;
-    seq1->qual.str -= bcd1_len;
-    seq1->qual.len += bcd1_len;
-    seq2->seq.str += bcd2_len;
-    seq2->seq.len -= bcd2_len;
-    seq2->qual.str += bcd2_len;
-    seq2->qual.len -= bcd2_len;
+    if (seq2 == NULL) {
+        return 0;
+    }
     if (out->mode == READS_INTERLEAVED) {
-        ret = qes_seqfile_write(out->fwd_file, seq2);
-        if (ret < 1) {
-            fprintf(stderr,
-                    "[process_file] Error: writing to il file %s failed\n%s\n",
+        if (write_trimmed(out->fwd_file, seq2, trim2)) {
+            qes_log_format_fatal(config->logger,
+                    "process_file -- Writing to file %s failed\n%s\n",
                     out->fwd_file->qf->path,
                     qes_file_error(out->fwd_file->qf));
             return 1;
         }
     } else if (out->mode == READS_PAIRED) {
-        ret = qes_seqfile_write(out->rev_file, seq2);
-        if (ret < 1) {
-            fprintf(stderr,
-                    "process_file -- Error: writing to rev file %s failed\n%s\n",
+        if (write_trimmed(out->rev_file, seq2, trim2)) {
+            qes_log_format_fatal(config->logger,
+                    "process_file -- Writing to file %s failed\n%s\n",
                     out->rev_file->qf->path,
                     qes_file_error(out->rev_file->qf));
             return 1;
         }
     }
-    seq2->seq.str -= bcd2_len;
-    seq2->seq.len += bcd2_len;
-    seq2->qual.str -= bcd2_len;
-    seq2->qual.len += bcd2_len;
+    return 0;
+}
+
+/* Write a read (pair) that could not be demultiplexed */
+static int
+write_unknown(struct axe_config *config, struct qes_seq *seq1,
+              struct qes_seq *seq2)
+{
+    struct axe_output *out = config->unknown_output;
+    struct qes_seqfile *dest = NULL;
+
+    if (write_trimmed(out->fwd_file, seq1, 0)) {
+        qes_log_format_fatal(config->logger,
+                "process_file -- Writing to file %s failed\n%s\n",
+                out->fwd_file->qf->path, qes_file_error(out->fwd_file->qf));
+        return 1;
+    }
+    if (seq2 == NULL) {
+        return 0;
+    }
+    dest = out->mode == READS_INTERLEAVED ? out->fwd_file : out->rev_file;
+    /* With single-end output mode there is nowhere to write R2 */
+    if (dest != NULL && write_trimmed(dest, seq2, 0)) {
+        qes_log_format_fatal(config->logger,
+                "process_file -- Writing to file %s failed\n%s\n",
+                dest->qf->path, qes_file_error(dest->qf));
+        return 1;
+    }
     return 0;
 }
 
@@ -1007,7 +1134,7 @@ process_read_pair_single(struct axe_config *config, struct qes_seq *seq1,
                         struct qes_seq *seq2)
 {
     int ret = 0;
-    ssize_t bcd = -1;
+    intptr_t bcd = -1;
     size_t barcode_pair_index = 0;
     struct axe_output *outfile = NULL;
     size_t bcd_len = 0;
@@ -1016,88 +1143,103 @@ process_read_pair_single(struct axe_config *config, struct qes_seq *seq1,
     increment_reads_print_progress(config);
     if (ret != 0) {
         /* No match */
-        qes_seqfile_write(config->unknown_output->fwd_file, seq1);
-        if (seq2 != NULL) {
-            if (config->out_mode == READS_INTERLEAVED) {
-                qes_seqfile_write(config->unknown_output->fwd_file, seq2);
-            } else {
-                qes_seqfile_write(config->unknown_output->rev_file, seq2);
-            }
-        }
         config->reads_failed++;
-        return 0;
+        return write_unknown(config, seq1, seq2);
     }
     /* Found a match */
+    if (bcd < 0 || (size_t)bcd >= config->n_barcodes_1) {
+        qes_log_format_fatal(config->logger,
+                             "process_file -- Bad barcode index %zd\n",
+                             (ssize_t)bcd);
+        return 1;
+    }
     config->reads_demultiplexed++;
-    /* FIXME: we need to check bcd doesn't cause segfault */
     barcode_pair_index = config->barcode_lookup[bcd][0];
     outfile = config->outputs[barcode_pair_index];
     bcd_len = config->barcodes[barcode_pair_index]->len1;
-    config->barcodes[bcd]->count++;
-    if (seq1->seq.len <= bcd_len) {
-        /* Don't write out seqs shorter than the barcode */
-        return 0;
-    }
-    /* Bit of the ol' switcheroo. We keep the seq's char pointers, so we need
-     * to switch them back to their orig. values, but don't want to copy.
-     * Kludgy, I know. */
-    seq1->seq.str += bcd_len;
-    seq1->seq.len -= bcd_len;
-    seq1->qual.str += bcd_len;
-    seq1->qual.len -= bcd_len;
-    ret = qes_seqfile_write(outfile->fwd_file, seq1);
-    if (ret < 1) {
-        fprintf(stderr,
-                "[write_read_single] Error: writing to R1 file %s failed\n%s\n",
-                outfile->fwd_file->qf->path,
-                qes_file_error(outfile->fwd_file->qf));
-        seq1->seq.str -= bcd_len;
-        seq1->seq.len += bcd_len;
-        seq1->qual.str -= bcd_len;
-        seq1->qual.len += bcd_len;
-        return 1;
-    }
-    seq1->seq.str -= bcd_len;
-    seq1->seq.len += bcd_len;
-    seq1->qual.str -= bcd_len;
-    seq1->qual.len += bcd_len;
-    /* And do the same with seq2, if we have one */
-    if (seq2 != NULL) {
-        if (config->trim_rev) {
-            seq2->seq.str += bcd_len;
-            seq2->seq.len -= bcd_len;
-            seq2->qual.str += bcd_len;
-            seq2->qual.len -= bcd_len;
+    config->barcodes[barcode_pair_index]->count++;
+    return write_matched_reads(config, outfile, seq1, seq2, bcd_len,
+                               config->trim_rev ? bcd_len : 0);
+}
+
+typedef int (*axe_read_func)(struct axe_config *, struct qes_seq *,
+                             struct qes_seq *);
+
+/* Iterate over all reads (pairs) in the input, calling ``func`` on each.
+ * Unlike the QES_SEQFILE_ITER macros, this distinguishes the end of the file
+ * from a malformed record, and complains if paired files differ in length.
+ * Returns 0 on success, 1 on any error. */
+static int
+axe_iterate_reads(struct axe_config *config, struct qes_seqfile *fwdsf,
+                  struct qes_seqfile *revsf, enum read_mode mode,
+                  axe_read_func func)
+{
+    struct qes_seq *seq1 = qes_seq_create();
+    struct qes_seq *seq2 = mode == READS_SINGLE ? NULL : qes_seq_create();
+    const char *fwdname = config->infiles[0];
+    int retval = 0;
+    ssize_t r1 = 0;
+    ssize_t r2 = 0;
+
+    while (1) {
+        r1 = qes_seqfile_read(fwdsf, seq1);
+        if (r1 == EOF) {
+            if (mode == READS_PAIRED &&
+                    (r2 = qes_seqfile_read(revsf, seq2)) != EOF) {
+                qes_log_format_fatal(config->logger,
+                        "process_file -- %s has more reads than %s\n",
+                        config->infiles[1], fwdname);
+                retval = 1;
+            }
+            break;
         }
-        if (outfile->mode == READS_INTERLEAVED) {
-            ret = qes_seqfile_write(outfile->fwd_file, seq2);
-            if (ret < 1) {
-                qes_log_format_fatal(
-                        config->logger,
-                        "process_file -- Writing to file %s failed\n%s\n",
-                        outfile->fwd_file->qf->path,
-                        qes_file_error(outfile->fwd_file->qf));
-                return 1;
+        if (r1 < 0) {
+            qes_log_format_fatal(config->logger,
+                    "process_file -- Malformed record after %zu reads in "
+                    "%s (error %zd)\n", fwdsf->n_records, fwdname, r1);
+            retval = 1;
+            break;
+        }
+        if (mode == READS_PAIRED) {
+            r2 = qes_seqfile_read(revsf, seq2);
+            if (r2 == EOF) {
+                qes_log_format_fatal(config->logger,
+                        "process_file -- %s has more reads than %s\n",
+                        fwdname, config->infiles[1]);
+            } else if (r2 < 0) {
+                qes_log_format_fatal(config->logger,
+                        "process_file -- Malformed record after %zu reads "
+                        "in %s (error %zd)\n", revsf->n_records,
+                        config->infiles[1], r2);
             }
-        } else if (outfile->mode == READS_PAIRED) {
-            ret = qes_seqfile_write(outfile->rev_file, seq2);
-            if (ret < 1) {
-                qes_log_format_fatal(
-                        config->logger,
-                        "process_file -- Writing to file %s failed\n%s\n",
-                        outfile->rev_file->qf->path,
-                        qes_file_error(outfile->rev_file->qf));
-                return 1;
+            if (r2 < 0) {
+                retval = 1;
+                break;
+            }
+        } else if (mode == READS_INTERLEAVED) {
+            r2 = qes_seqfile_read(fwdsf, seq2);
+            if (r2 == EOF) {
+                qes_log_format_fatal(config->logger,
+                        "process_file -- Interleaved file %s has an odd "
+                        "number of reads\n", fwdname);
+            } else if (r2 < 0) {
+                qes_log_format_fatal(config->logger,
+                        "process_file -- Malformed record after %zu reads "
+                        "in %s (error %zd)\n", fwdsf->n_records, fwdname, r2);
+            }
+            if (r2 < 0) {
+                retval = 1;
+                break;
             }
         }
-        if (config->trim_rev) {
-            seq2->seq.str -= bcd_len;
-            seq2->seq.len += bcd_len;
-            seq2->qual.str -= bcd_len;
-            seq2->qual.len += bcd_len;
+        if (func(config, seq1, seq2) != 0) {
+            retval = 1;
+            break;
         }
     }
-    return 0;
+    qes_seq_destroy(seq1);
+    qes_seq_destroy(seq2);
+    return retval;
 }
 
 
@@ -1106,8 +1248,7 @@ process_file_single(struct axe_config *config)
 {
     struct qes_seqfile *fwdsf = NULL;
     struct qes_seqfile *revsf = NULL;
-    int ret = 0;
-    int retval = -1;
+    int retval = 1;
 
     if (!axe_config_ok(config)) {
         return -1;
@@ -1119,12 +1260,15 @@ process_file_single(struct axe_config *config)
                              config->infiles[0]);
         goto exit;
     }
-    switch(config->in_mode) {
+    if (fwdsf->format == FASTA_FMT) {
+        qes_log_format_fatal(config->logger,
+                "process_file -- %s is FASTA, but only FASTQ input is "
+                "supported\n", config->infiles[0]);
+        goto exit;
+    }
+    switch (config->in_mode) {
     case READS_SINGLE:
-        goto single;
-        break;
     case READS_INTERLEAVED:
-        goto interleaved;
         break;
     case READS_PAIRED:
         revsf = qes_seqfile_create(config->infiles[1], "r");
@@ -1134,7 +1278,6 @@ process_file_single(struct axe_config *config)
                                  config->infiles[1]);
             goto exit;
         }
-        goto paired;
         break;
     case READS_UNKNOWN:
     default:
@@ -1142,32 +1285,10 @@ process_file_single(struct axe_config *config)
                              "process_file_single -- Bad infile mode %u\n",
                              config->in_mode);
         goto exit;
-        break;
-    }
-
-single:
-    QES_SEQFILE_ITER_SINGLE_BEGIN(fwdsf, seq, seqlen) {
-        ret = process_read_pair_single(config, seq, NULL);
     }
-    QES_SEQFILE_ITER_SINGLE_END(seq);
-    retval = ret == 0 ? 0 : 1;
-    goto exit;
+    retval = axe_iterate_reads(config, fwdsf, revsf, config->in_mode,
+                               process_read_pair_single);
 
-interleaved:
-    QES_SEQFILE_ITER_INTERLEAVED_BEGIN(fwdsf, seq1, seq2, seqlen1, seqlen2) {
-        ret = process_read_pair_single(config, seq1, seq2);
-    }
-    QES_SEQFILE_ITER_INTERLEAVED_END(seq1, seq2);
-    retval = ret == 0 ? 0 : 1;
-    goto exit;
-
-paired:
-    QES_SEQFILE_ITER_PAIRED_BEGIN(fwdsf, revsf, seq1, seq2, seqlen1, seqlen2) {
-        ret = process_read_pair_single(config, seq1, seq2);
-    }
-    QES_SEQFILE_ITER_PAIRED_END(seq1, seq2);
-    retval = ret == 0 ? 0 : 1;
-    goto exit;
 exit:
     qes_seqfile_destroy(fwdsf);
     qes_seqfile_destroy(revsf);
@@ -1193,35 +1314,30 @@ process_read_pair_combo(struct axe_config *config, struct qes_seq *seq1,
     increment_reads_print_progress(config);
     if (r1_ret != 0 || r2_ret != 0) {
         /* No match */
-        qes_seqfile_write(config->unknown_output->fwd_file, seq1);
-        if (config->out_mode == READS_INTERLEAVED) {
-            qes_seqfile_write(config->unknown_output->fwd_file, seq2);
-        } else {
-            qes_seqfile_write(config->unknown_output->rev_file, seq2);
-        }
         config->reads_failed++;
-        return 0;
+        return write_unknown(config, seq1, seq2);
     }
     /* Found a match */
+    if (bcd1 < 0 || bcd2 < 0 || (size_t)bcd1 >= config->n_barcodes_1 ||
+            (size_t)bcd2 >= config->n_barcodes_2) {
+        qes_log_format_fatal(config->logger,
+                             "process_file -- Bad barcode index %zd,%zd\n",
+                             (ssize_t)bcd1, (ssize_t)bcd2);
+        return 1;
+    }
     barcode_pair_index = config->barcode_lookup[bcd1][bcd2];
     if (barcode_pair_index < 0) {
         /* Invalid match */
-        qes_seqfile_write(config->unknown_output->fwd_file, seq1);
-        if (config->out_mode == READS_INTERLEAVED) {
-            qes_seqfile_write(config->unknown_output->fwd_file, seq2);
-        } else {
-            qes_seqfile_write(config->unknown_output->rev_file, seq2);
-        }
         config->reads_failed++;
-        return 0;
+        return write_unknown(config, seq1, seq2);
     }
     config->reads_demultiplexed++;
     outfile = config->outputs[barcode_pair_index];
     bcd1_len = config->barcodes[barcode_pair_index]->len1;
     bcd2_len = config->barcodes[barcode_pair_index]->len2;
     config->barcodes[barcode_pair_index]->count++;
-    return write_barcoded_read_combo(outfile, seq1, seq2, bcd1_len,
-                                     bcd2_len);
+    return write_matched_reads(config, outfile, seq1, seq2, bcd1_len,
+                               bcd2_len);
 }
 
 
@@ -1230,7 +1346,7 @@ process_file_combo(struct axe_config *config)
 {
     struct qes_seqfile *fwdsf = NULL;
     struct qes_seqfile *revsf = NULL;
-    int have_error = 0;
+    int retval = 1;
 
     if (!axe_config_ok(config)) {
         return -1;
@@ -1240,21 +1356,25 @@ process_file_combo(struct axe_config *config)
         qes_log_format_fatal(config->logger,
                              "process_file -- Couldn't open seqfile %s\n",
                              config->infiles[0]);
-        goto error;
+        goto exit;
+    }
+    if (fwdsf->format == FASTA_FMT) {
+        qes_log_format_fatal(config->logger,
+                "process_file -- %s is FASTA, but only FASTQ input is "
+                "supported\n", config->infiles[0]);
+        goto exit;
     }
-    switch(config->in_mode) {
+    switch (config->in_mode) {
     case READS_INTERLEAVED:
-        goto interleaved;
         break;
     case READS_PAIRED:
         revsf = qes_seqfile_create(config->infiles[1], "r");
         if (revsf == NULL) {
             qes_log_format_fatal(config->logger,
                                  "process_file -- Couldn't open seqfile %s\n",
-                                 config->infiles[0]);
-            goto error;
+                                 config->infiles[1]);
+            goto exit;
         }
-        goto paired;
         break;
     case READS_SINGLE:
     case READS_UNKNOWN:
@@ -1262,51 +1382,71 @@ process_file_combo(struct axe_config *config)
         qes_log_format_fatal(config->logger,
                              "process_file_combo -- Bad infile mode %u\n",
                              config->in_mode);
-        goto error;
-        break;
-    }
-
-interleaved:
-    QES_SEQFILE_ITER_INTERLEAVED_BEGIN(fwdsf, seq1, seq2, seqlen1, seqlen2)
-    if (process_read_pair_combo(config, seq1, seq2)) {
-        have_error = 1;
-        break;
-    }
-    QES_SEQFILE_ITER_INTERLEAVED_END(seq1, seq2)
-    if (!have_error) goto clean_exit;
-    else goto error;
-
-paired:
-    QES_SEQFILE_ITER_PAIRED_BEGIN(fwdsf, revsf, seq1, seq2, seqlen1, seqlen2)
-    if (process_read_pair_combo(config, seq1, seq2)) {
-        have_error = 1;
-        break;
+        goto exit;
     }
-    QES_SEQFILE_ITER_PAIRED_END(seq1, seq2)
-    if (!have_error) goto clean_exit;
-    else goto error;
+    retval = axe_iterate_reads(config, fwdsf, revsf, config->in_mode,
+                               process_read_pair_combo);
 
-clean_exit:
-    qes_seqfile_destroy(fwdsf);
-    qes_seqfile_destroy(revsf);
-    return 0;
-error:
+exit:
     qes_seqfile_destroy(fwdsf);
     qes_seqfile_destroy(revsf);
-    return 1;
+    return retval;
+}
+
+
+/* Close an output, reporting whether the final flush/close succeeded. This
+ * matters for gzipped output, where the last block is written on close. */
+static int
+axe_close_output_file(struct qes_seqfile *sf)
+{
+    int res = 0;
+
+    if (sf == NULL || sf->qf == NULL || sf->qf->fp == NULL) {
+        return 0;
+    }
+    res = QES_ZCLOSE(sf->qf->fp);
+    sf->qf->fp = NULL;
+    return res != 0;
 }
 
+static int
+axe_close_outputs(struct axe_config *config)
+{
+    size_t iii = 0;
+    int failed = 0;
+    struct axe_output *out = NULL;
+
+    for (iii = 0; iii <= config->n_barcode_pairs; iii++) {
+        if (iii < config->n_barcode_pairs) {
+            out = config->outputs != NULL ? config->outputs[iii] : NULL;
+        } else {
+            out = config->unknown_output;
+        }
+        if (out == NULL) {
+            continue;
+        }
+        if (axe_close_output_file(out->fwd_file) ||
+                axe_close_output_file(out->rev_file)) {
+            failed = 1;
+            qes_log_message_fatal(config->logger,
+                    "process_file -- Failed to finish writing an output "
+                    "file. Is the disk full?\n");
+        }
+    }
+    return failed;
+}
 
 int
 axe_process_file(struct axe_config *config)
 {
     int ret = 0;
-    clock_t start = 0;
+    struct timespec start = {0, 0};
+    struct timespec end = {0, 0};
 
     if (!axe_config_ok(config)) {
         return -1;
     }
-    start = clock();
+    clock_gettime(CLOCK_MONOTONIC, &start);
     if (config->verbosity >= 0) {
         axe_format_bold(config->logger,
                         "process_file -- (%s) Starting demultiplexing\n",
@@ -1317,7 +1457,12 @@ axe_process_file(struct axe_config *config)
     } else {
         ret = process_file_single(config);
     }
-    config->time_taken = (float)(clock() - start) / CLOCKS_PER_SEC;
+    if (axe_close_outputs(config) != 0) {
+        ret = 1;
+    }
+    clock_gettime(CLOCK_MONOTONIC, &end);
+    config->time_taken = (float)(end.tv_sec - start.tv_sec) +
+                         (float)(end.tv_nsec - start.tv_nsec) / 1e9f;
     if (config->verbosity >= 0) {
         /* Jump to new line so we don't clobber the progress bar */
         fprintf(stderr, "\n");
@@ -1362,6 +1507,10 @@ product(int64_t len, int64_t elem, uintptr_t *choices, int at_start)
     return 0;
 }
 
+/* Generate all strings at *exactly* ``dist`` substitutions from ``str``, over
+ * the alphabet ACGT. No string is returned twice. If ``keep_original`` is
+ * set, ``str`` itself is also returned (once, first). Returns NULL on error,
+ * or if ``dist`` > ``len``. */
 char **
 hamming_mutate_dna(size_t *n_results_o, const char *str, size_t len,
                    unsigned int dist, int keep_original)
@@ -1377,36 +1526,48 @@ hamming_mutate_dna(size_t *n_results_o, const char *str, size_t len,
     int alpha_ret = 0;
     gsl_combination *mut_idx_comb;
 
-    if (str == NULL || len < 1 || dist < 1) {
+    if (n_results_o == NULL || str == NULL || len < 1 || dist < 1 ||
+            dist > len || dist > n_letters) {
         return NULL;
     }
     result = qes_malloc(results_alloced * sizeof(*result));
+    if (keep_original) {
+        result[results++] = strndup(str, len);
+    }
     alphabet_indicies = qes_calloc(dist, sizeof(*alphabet_indicies));
     mut_idx_comb = gsl_combination_calloc(len, dist);
+    if (mut_idx_comb == NULL) {
+        qes_free(alphabet_indicies);
+        qes_free(result);
+        return NULL;
+    }
     do {
         while ((alpha_ret = product(n_letters, dist, alphabet_indicies,
                                     !alpha_ret)) == 1) {
-            tmp = strndup(str, len+1);
+            int is_exact = 1;
+
+            tmp = strndup(str, len);
             for (iii = 0; iii < dist; iii++) {
                 char replacement = alphabet[alphabet_indicies[iii]];
                 size_t mut_idx = gsl_combination_get(mut_idx_comb, iii);
                 if (tmp[mut_idx] == replacement) {
-                    continue;
+                    /* Not a substitution, so this has < dist mismatches. It
+                     * is generated by a lower ``dist``. */
+                    is_exact = 0;
+                    break;
                 }
                 tmp[mut_idx] = replacement;
             }
-            if (strncmp(str, tmp, len) == 0 && !keep_original) {
+            if (!is_exact) {
                 qes_free(tmp);
                 continue;
-            } else {
-                if (results + 1 > results_alloced) {
-                    results_alloced = qes_roundupz(results_alloced);
-                    result = qes_realloc(result,
-                                         results_alloced * sizeof(*result));
-                }
-                result[results++] = strndup(tmp, len);
-                qes_free(tmp);
             }
+            if (results + 1 > results_alloced) {
+                results_alloced = qes_roundupz(results_alloced);
+                result = qes_realloc(result,
+                                     results_alloced * sizeof(*result));
+            }
+            result[results++] = tmp;
         }
     } while (gsl_combination_next(mut_idx_comb) == GSL_SUCCESS);
     gsl_combination_free(mut_idx_comb);
@@ -1487,60 +1648,52 @@ axe_trie_add(struct axe_trie *trie, const char *str, intptr_t data)
     return 1;
 }
 
-inline int
-axe_match_read (struct axe_config *config, ssize_t *value,
+/* Find the barcode that is a prefix of ``seq`` (the longest, if more than one),
+ * and store its trie value in ``*value``. Returns 0 on a match, 1 if there is
+ * none, and -1 on error. ``*value`` is -1 unless a match is found. Ambiguous
+ * entries (negative values) are not matches. */
+int
+axe_match_read (struct axe_config *config, intptr_t *value,
                 struct axe_trie *trie, const struct qes_seq *seq)
 {
-    TrieState *trie_iter = NULL;
-    TrieState *last_good_state = NULL;
+    TrieState *iter = NULL;
+    TrieState *probe = NULL;
     size_t seq_pos = 0;
+    int ret = 1;
 
-    /* value is set to -1 on anything bad happening including failed lookup */
     if (value == NULL || !axe_trie_ok(trie) || !qes_seq_ok(seq)) {
         return -1;
     }
-    /* Set *value here, then we just don't update it on error */
     *value = -1;
-    if (seq->seq.len < trie->min_len) {
-        return 1;
-    }
-    /* Only look until the maximum of the largest barcode, or seq len */
-    /* Grab tree root iter, and check it. */
-    trie_iter = trie_root(trie->trie);
-    if (trie_iter == NULL) {
+    iter = trie_root(trie->trie);
+    probe = trie_root(trie->trie);
+    if (iter == NULL || probe == NULL) {
         qes_log_message_fatal(config->logger,
                               "match_read -- trie_root() returned NULL!\n");
-        return -1;
+        ret = -1;
+        goto exit;
     }
-    /* Consume seq until we can't */
-    do {
-        trie_state_walk(trie_iter, seq->seq.str[seq_pos]);
-        if (trie_state_is_terminal(trie_iter)) {
-            if (last_good_state != NULL) {
-                trie_state_free(last_good_state);
+    /* Consume the read for as long as it follows a path in the trie, and no
+     * further than its end. At the end of each barcode, look up its value. */
+    for (seq_pos = 0; seq_pos < seq->seq.len; seq_pos++) {
+        if (!trie_state_walk(iter, seq->seq.str[seq_pos])) {
+            break;
+        }
+        if (trie_state_is_terminal(iter)) {
+            trie_state_copy(probe, iter);
+            /* Step through the terminator to reach the leaf with the data */
+            if (trie_state_walk(probe, '\0') &&
+                    (intptr_t)trie_state_get_data(probe) >= 0) {
+                *value = (intptr_t)trie_state_get_data(probe);
+                ret = 0;
             }
-            last_good_state = trie_state_clone(trie_iter);
         }
-    } while (trie_state_is_walkable(trie_iter, seq->seq.str[++seq_pos]));
-    /* If we get to a terminal state, then great! */
-    if (trie_state_is_terminal(trie_iter)) {
-        trie_state_walk(trie_iter, '\0');
-        trie_state_free(last_good_state);
-        *value =  (ssize_t) trie_state_get_data(trie_iter);
-        trie_state_free(trie_iter);
-        return 0;
-    } else if (last_good_state != NULL) {
-        trie_state_free(trie_iter);
-        trie_state_walk(last_good_state, '\0');
-        *value =  (ssize_t) trie_state_get_data(last_good_state);
-        trie_state_free(last_good_state);
-        return 0;
-    }
-    trie_state_free(trie_iter);
-    if (last_good_state != NULL) {
-        trie_state_free(last_good_state);
     }
-    return 1;
+
+exit:
+    trie_state_free(iter);
+    trie_state_free(probe);
+    return ret;
 }
 
 int
@@ -1596,13 +1749,30 @@ axe_write_table(const struct axe_config *config)
     return 0;
 }
 
+/* Print a count as e.g. "12", "3.40K" or "1.20M" */
+static const char *
+axe_human_count(uint64_t n, char *buf, size_t bufsz)
+{
+    if (n >= 1000000) {
+        snprintf(buf, bufsz, "%.2fM", (double)n / 1e6);
+    } else if (n >= 1000) {
+        snprintf(buf, bufsz, "%.2fK", (double)n / 1e3);
+    } else {
+        snprintf(buf, bufsz, "%" PRIu64, n);
+    }
+    return buf;
+}
+
 int
 axe_print_summary(const struct axe_config *config)
 {
     const char *tmp;
+    char processed[32];
+    char demuxed[32];
+    char failed[32];
+    double rate = 0.0;
+    double pct_failed = 0.0;
 
-#define hr(r) ((float)((r) / ((r) > 1000000.0 ? 1000000.0 : 1000.0)))
-#define unit(r) ((r) > 1000000.0 ? 'M' : 'K')
     if (!axe_config_ok(config)) {
         return -1;
     }
@@ -1610,6 +1780,13 @@ axe_print_summary(const struct axe_config *config)
         /* Say nothing if we're being quiet */
         return 0;
     }
+    if (config->time_taken > 0) {
+        rate = (double)config->reads_processed / 1000.0 / config->time_taken;
+    }
+    if (config->reads_processed > 0) {
+        pct_failed = 100.0 * (double)config->reads_failed /
+                     (double)config->reads_processed;
+    }
     axe_message_bold(config->logger, "\nRun Summary:\n");
     if (config->verbosity > 1) {
         qes_log_message_debug(config->logger,
@@ -1617,17 +1794,16 @@ axe_print_summary(const struct axe_config *config)
     }
     tmp = config->out_mode == READS_SINGLE ? "reads" : "read pairs";
     axe_format_bold(config->logger,
-            "Processed %.2f%c %s in %0.1f seconds (%0.1fK %s/sec)\n",
-            hr(config->reads_processed), unit(config->reads_processed), tmp, config->time_taken,
-            (float)(config->reads_processed / 1000) / config->time_taken, tmp);
-    axe_format_bold(config->logger,
-            "%.2f%c %s contained valid barcodes\n",
-            hr(config->reads_demultiplexed), unit(config->reads_demultiplexed), tmp);
+            "Processed %s %s in %0.1f seconds (%0.1fK %s/sec)\n",
+            axe_human_count(config->reads_processed, processed,
+                            sizeof(processed)),
+            tmp, config->time_taken, rate, tmp);
+    axe_format_bold(config->logger, "%s %s contained valid barcodes\n",
+            axe_human_count(config->reads_demultiplexed, demuxed,
+                            sizeof(demuxed)), tmp);
     axe_format_bold(config->logger,
-            "%.2f%c %s could not be demultiplexed (%0.1f%%)\n",
-            hr(config->reads_failed), unit(config->reads_failed), tmp,
-            ((float)config->reads_failed/(float)(config->reads_processed)*100.0));
+            "%s %s could not be demultiplexed (%0.1f%%)\n",
+            axe_human_count(config->reads_failed, failed, sizeof(failed)),
+            tmp, pct_failed);
     return 0;
-#undef hr
-#undef unit
 }


=====================================
src/main.c
=====================================
@@ -71,9 +71,9 @@ print_usage(FILE *stream)
     fprintf(stream, "\nUSAGE:\n");
     fprintf(stream, "axe-demux [-mzc2pt] -b (-f [-r] | -i) (-F [-R] | -I)\n");
     fprintf(stream, "axe-demux -h\n");
-    fprintf(stream, "axe-demux -v\n\n");
+    fprintf(stream, "axe-demux -V\n\n");
     fprintf(stream, "OPTIONS:\n");
-    fprintf(stream, "    -m, --mismatch\tMaximum hamming distance mismatch. [int, default 1]\n");
+    fprintf(stream, "    -m, --mismatch\tMaximum hamming distance mismatch. [int, default 0]\n");
     fprintf(stream, "    -z, --ziplevel\tGzip compression level, or 0 for plain text [int, default 0]\n");
     fprintf(stream, "    -c, --combinatorial\tUse combinatorial barcode matching. [flag, default OFF]\n");
     fprintf(stream, "    -p, --permissive\tDon't error on barcode mismatch confict, matching only\n");
@@ -81,11 +81,11 @@ print_usage(FILE *stream)
     fprintf(stream, "    -2, --trim-r2\tTrim barcode from R2 read as well as R1. [flag, default OFF]\n");
     fprintf(stream, "    -b, --barcodes\tBarcode file. See --help for example. [file]\n");
     fprintf(stream, "    -f, --fwd-in\tInput forward read. [file]\n");
-    fprintf(stream, "    -F, --fwd-out\tOutput forward read prefix. [file]\n");
+    fprintf(stream, "    -F, --fwd-out\tOutput forward read prefix. [file prefix or existing directory]\n");
     fprintf(stream, "    -r, --rev-in\tInput reverse read. [file]\n");
-    fprintf(stream, "    -R, --rev-out\tOutput reverse read prefix. [file]\n");
+    fprintf(stream, "    -R, --rev-out\tOutput reverse read prefix. [file prefix or existing directory]\n");
     fprintf(stream, "    -i, --ilfq-in\tInput interleaved paired reads. [file]\n");
-    fprintf(stream, "    -I, --ilfq-out\tOutput interleaved paired reads prefix. [file]\n");
+    fprintf(stream, "    -I, --ilfq-out\tOutput interleaved paired reads prefix. [file prefix or existing directory]\n");
     fprintf(stream, "    -t, --table-file\tOutput a summary table of demultiplexing statistics to file. [file]\n");
     fprintf(stream, "    -h, --help\t\tPrint this usage plus additional help.\n");
     fprintf(stream, "    -V, --version\tPrint version string.\n");
@@ -96,7 +96,7 @@ print_usage(FILE *stream)
 
 static const char *axe_opts = "m:z:c2pb:f:F:r:R:i:I:t:hVvqd";
 static const struct option axe_longopts[] = {
-    { "mismatch",   optional_argument,  NULL,   'm' },
+    { "mismatch",   required_argument,  NULL,   'm' },
     { "ziplevel",   required_argument,  NULL,   'z' },
     { "combinatorial", no_argument,     NULL,   'c' },
     { "trim-r2",    no_argument,        NULL,   '2' },
@@ -124,7 +124,10 @@ parse_args(struct axe_config *config, int argc, char * const *argv)
     bool fullhelp = false;
 
     if (argc < 2 ) {
-        goto printhelp;
+        /* Nothing to do: that's a usage error, not a request for help */
+        print_usage(stderr);
+        axe_config_destroy(config);
+        exit(1);
     }
     if (!axe_config_ok(config) || argv == NULL) {
         goto error;
@@ -132,18 +135,36 @@ parse_args(struct axe_config *config, int argc, char * const *argv)
     /* Set some sane defaults */
     /* Most things will default to 0 as we `calloc` the config struct, so we
      * don't need to explicity set them. */
-    config->mismatches = 1;
+    config->mismatches = 0;
     config->verbosity = 0;
     config->out_compress_level = 0;
     /* Parse argv using getopt */
     while ((c = getopt_long(argc, argv, axe_opts, axe_longopts, &optind)) > 0){
         switch (c) {
-            case 'm':
-                config->mismatches = atol(optarg);
+            case 'm': {
+                char *end = NULL;
+                long mm = strtol(optarg, &end, 10);
+
+                if (end == optarg || *end != '\0' || mm < 0) {
+                    fprintf(stderr, "ERROR: Bad mismatch level '%s'\n",
+                            optarg);
+                    goto error;
+                }
+                config->mismatches = (size_t)mm;
                 break;
-            case 'z':
-                config->out_compress_level = atoi(optarg);
+            }
+            case 'z': {
+                char *end = NULL;
+                long zl = strtol(optarg, &end, 10);
+
+                if (end == optarg || *end != '\0' || zl < 0 || zl > 9) {
+                    fprintf(stderr, "ERROR: Bad compression level '%s' "
+                            "(must be 0-9)\n", optarg);
+                    goto error;
+                }
+                config->out_compress_level = (int)zl;
                 break;
+            }
             case 'c':
                 config->match_combo |= 1;
                 break;


=====================================
tests/CMakeLists.txt
=====================================
@@ -10,6 +10,7 @@ ADD_CUSTOM_TARGET(setup_tests ALL
                    ${CMAKE_BINARY_DIR}/data
 	COMMAND ${CMAKE_COMMAND} -E copy
                    ${CMAKE_CURRENT_SOURCE_DIR}/axe_cli_tests.py
+                   ${CMAKE_CURRENT_SOURCE_DIR}/axe_simulation_tests.py
                    ${CMAKE_BINARY_DIR}/bin
 	COMMAND ${CMAKE_COMMAND} -E make_directory
                    ${CMAKE_BINARY_DIR}/out)
@@ -19,10 +20,14 @@ ADD_TEST(NAME "UnitTests" COMMAND test_axe)
 SET(COVERAGE_CMD test_axe)
 SET(COVERAGE_OUT "${CMAKE_BINARY_DIR}/coverage_html")
 
-ADD_TEST(NAME "IntegrationTests" COMMAND python
+ADD_TEST(NAME "IntegrationTests" COMMAND python3
          ${CMAKE_BINARY_DIR}/bin/axe_cli_tests.py
          ${CMAKE_BINARY_DIR})
 
+ADD_TEST(NAME "SimulationTests" COMMAND python3
+         ${CMAKE_BINARY_DIR}/bin/axe_simulation_tests.py
+         ${CMAKE_BINARY_DIR})
+
 IF (CMAKE_BUILD_TYPE STREQUAL "Coverage")
 	FIND_PROGRAM( GCOV_PATH gcov )
 	FIND_PROGRAM( LCOV_PATH lcov )


=====================================
tests/axe_cli_tests.py
=====================================
@@ -1,5 +1,5 @@
-#!/usr/bin/env python
-from __future__ import print_function
+#!/usr/bin/env python3
+import gzip
 import hashlib
 import logging
 import os
@@ -8,6 +8,7 @@ import re
 import shutil
 import subprocess as sp
 import sys
+import tempfile
 import unittest
 
 
@@ -19,8 +20,11 @@ CMAKE_BINARY_DIR = sys.argv.pop(1)
 
 
 def md5sum(filename):
+    """md5 of a file's contents, un-gzipped if need be (compressed bytes
+    differ between zlib implementations)."""
     h = hashlib.md5()
-    with open(filename, 'rb') as fh:
+    opener = gzip.open if filename.endswith('.gz') else open
+    with opener(filename, 'rb') as fh:
         while True:
             hunk = fh.read(1024 ** 2)
             if not hunk:
@@ -29,6 +33,11 @@ def md5sum(filename):
     return h.hexdigest()
 
 
+def gz(files):
+    """Expected md5s (of contents) of the gzipped version of some outputs"""
+    return {name + '.gz': md5 for name, md5 in files.items()}
+
+
 class AxeTest(unittest.TestCase):
     maxDiff=  None
 
@@ -79,6 +88,19 @@ class TestBadUsage(AxeTest):
 
 
 class TestPareSE(AxeTest):
+    FILES = {
+        'pare_se_1_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
+        'pare_se_2_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
+        'pare_se_3_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
+        'pare_se_4_R1.fastq': '8e5eef3323e597b209f79dc9fcd74c9a',
+        'pare_se_5_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
+        'pare_se_6_R1.fastq': '7228a165f353920328360dedc3a41205',
+        'pare_se_7_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
+        'pare_se_8_R1.fastq': 'b349d3276ba7c7515d0093b1a49b3959',
+        'pare_se_9_R1.fastq': '74b4763271aefcc135425b06730874ba',
+        'pare_se_unknown_R1.fastq': 'd450569dd8fd4bdddffbfaeec4980273',
+    }
+
     def __init__(self, methodName='runTest'):
         super(TestPareSE, self).__init__(methodName)
         self.infq = path.join(self.data, "pare.fq.gz")
@@ -93,19 +115,7 @@ class TestPareSE(AxeTest):
             '-b', self.barcodes,
         ]
         self.assertTrue(self.run_and_check_stdout(command))
-        files = {
-            'pare_se_1_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
-            'pare_se_2_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
-            'pare_se_3_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
-            'pare_se_4_R1.fastq': '8e5eef3323e597b209f79dc9fcd74c9a',
-            'pare_se_5_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
-            'pare_se_6_R1.fastq': '7228a165f353920328360dedc3a41205',
-            'pare_se_7_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
-            'pare_se_8_R1.fastq': 'b349d3276ba7c7515d0093b1a49b3959',
-            'pare_se_9_R1.fastq': '74b4763271aefcc135425b06730874ba',
-            'pare_se_unknown_R1.fastq': 'd450569dd8fd4bdddffbfaeec4980273',
-        }
-        self.assertDictEqual(files, self.get_md5_dict())
+        self.assertDictEqual(self.FILES, self.get_md5_dict())
 
     def test_pare_se_zip(self):
         command = [self.axe,
@@ -114,32 +124,10 @@ class TestPareSE(AxeTest):
             '-b', self.barcodes,
             '-z', '9',
         ]
-        files = {
-            'pare_se_1_R1.fastq.gz': '4a4dd3598707603b3f76a2378a4504aa',
-            'pare_se_2_R1.fastq.gz': '4a4dd3598707603b3f76a2378a4504aa',
-            'pare_se_3_R1.fastq.gz': '4a4dd3598707603b3f76a2378a4504aa',
-            'pare_se_4_R1.fastq.gz': '96d21b860a0fc70641ea43d350433d11',
-            'pare_se_5_R1.fastq.gz': '4a4dd3598707603b3f76a2378a4504aa',
-            'pare_se_6_R1.fastq.gz': 'd6044c04f79c358e4a1d443f8828df18',
-            'pare_se_7_R1.fastq.gz': '4a4dd3598707603b3f76a2378a4504aa',
-            'pare_se_8_R1.fastq.gz': 'da77b8e95827d362a1702ce4fe75c7a9',
-            'pare_se_9_R1.fastq.gz': '9c160b0daa0c73e5ef0994206774a5a0',
-            'pare_se_unknown_R1.fastq.gz': 'afd5737935814d756e89c365d2d61c7b',
-        }
         self.assertTrue(self.run_and_check_stdout(command))
-        self.assertDictEqual(files, self.get_md5_dict())
+        self.assertDictEqual(gz(self.FILES), self.get_md5_dict())
 
 class TestFakeSE(AxeTest):
-    files = {
-        'fake_se_1_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
-        'fake_se_2_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
-        'fake_se_unknown_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
-    }
-    zfiles = {
-        'fake_se_1_R1.fastq.gz': '3e07353d24a3ecd315067250a6be6047',
-        'fake_se_2_R1.fastq.gz': '3e07353d24a3ecd315067250a6be6047',
-        'fake_se_unknown_R1.fastq.gz': '3e07353d24a3ecd315067250a6be6047',
-    }
 
     def __init__(self, methodName='runTest'):
         super(TestFakeSE, self).__init__(methodName)
@@ -147,40 +135,62 @@ class TestFakeSE(AxeTest):
         self.outfq = path.join(self.out, "fake_se")
         self.nobcdfq = path.join(self.out, "fake_se_unknown_R1.fastq")
 
-    def _do_test(self, mm_level):
+    def _do_test(self, mm_level, mismatch=0):
         infq = path.join(self.data, "fake_{}mm_R1.fq.gz".format(mm_level))
         command = [self.axe,
             "-f", infq,
             "-F", self.outfq,
             '-b', self.barcodes,
+            '-m', str(mismatch),
         ]
         self.assertTrue(self.run_and_check_stdout(command))
 
-    def _do_test_zip(self, mm_level):
+    def _do_test_zip(self, mm_level, mismatch=0):
         infq = path.join(self.data, "fake_{}mm_R1.fq.gz".format(mm_level))
         command = [self.axe,
             "-f", infq,
             "-F", self.outfq,
             '-b', self.barcodes,
+            '-m', str(mismatch),
             '-z', '9',
         ]
         self.assertTrue(self.run_and_check_stdout(command))
 
     def test_fake_se_0mm(self):
         self._do_test(0)
-        self.assertDictEqual(self.files, self.get_md5_dict())
+        files = {
+            'fake_se_1_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_2_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_unknown_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+        }
+        self.assertDictEqual(files, self.get_md5_dict())
 
     def test_fake_se_0mm_zip(self):
         self._do_test_zip(0)
-        self.assertDictEqual(self.zfiles, self.get_md5_dict())
+        files = gz({
+            'fake_se_1_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_2_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_unknown_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+        })
+        self.assertDictEqual(files, self.get_md5_dict())
 
     def test_fake_se_1mm(self):
-        self._do_test(1)
-        self.assertDictEqual(self.files, self.get_md5_dict())
+        self._do_test(1, mismatch=1)
+        files = {
+            'fake_se_1_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_2_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_unknown_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+        }
+        self.assertDictEqual(files, self.get_md5_dict())
 
     def test_fake_se_1mm_zip(self):
-        self._do_test_zip(1)
-        self.assertDictEqual(self.zfiles, self.get_md5_dict())
+        self._do_test_zip(1, mismatch=1)
+        files = gz({
+            'fake_se_1_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_2_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+            'fake_se_unknown_R1.fastq': '836eaf06938d4a41122f284ed487a9c7',
+        })
+        self.assertDictEqual(files, self.get_md5_dict())
 
     def test_fake_se_2mm(self):
         self._do_test(2)
@@ -193,12 +203,287 @@ class TestFakeSE(AxeTest):
 
     def test_fake_se_2mm_zip(self):
         self._do_test_zip(2)
-        zfiles = {
-            'fake_se_1_R1.fastq.gz': '4a4dd3598707603b3f76a2378a4504aa',
-            'fake_se_2_R1.fastq.gz': '4a4dd3598707603b3f76a2378a4504aa',
-            'fake_se_unknown_R1.fastq.gz': 'ee6979b139dbd898f058fd7649f87da2',
-        }
-        self.assertDictEqual(zfiles, self.get_md5_dict())
+        files = gz({
+            'fake_se_1_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
+            'fake_se_2_R1.fastq': 'd41d8cd98f00b204e9800998ecf8427e',
+            'fake_se_unknown_R1.fastq': 'a6de105b6c5abbc2d0d16440333adc64',
+        })
+        self.assertDictEqual(files, self.get_md5_dict())
+
+
+class TestRegressions(AxeTest):
+    """Edge cases that used to crash, silently drop reads or give wrong output.
+    Inputs are generated on the fly."""
+
+    BARCODES = "Barcode\tID\nATCACG\tA\nCGATGT\tB\n"
+    COMBO_BARCODES = "ATCACG\tCCCC\tA\nCGATGT\tGGGG\tB\n"
+
+    def __init__(self, methodName='runTest'):
+        super(TestRegressions, self).__init__(methodName)
+
+    def setUp(self):
+        super(TestRegressions, self).setUp()
+        self.tmp = tempfile.mkdtemp()
+        self.barcodes = self.write("bc.tsv", self.BARCODES)
+        self.combo_barcodes = self.write("cbc.tsv", self.COMBO_BARCODES)
+        self.prefix = path.join(self.out, "reg")
+
+    def tearDown(self):
+        shutil.rmtree(self.tmp)
+        super(TestRegressions, self).tearDown()
+
+    def write(self, name, text):
+        fpath = path.join(self.tmp, name)
+        with open(fpath, "w") as fh:
+            fh.write(text)
+        return fpath
+
+    def fastq(self, name, reads):
+        text = "".join("@{}\n{}\n+\n{}\n".format(n, s, "I" * len(s))
+                       for n, s in reads)
+        return self.write(name, text)
+
+    def read_out(self, name):
+        with open(path.join(self.out, name)) as fh:
+            return fh.read()
+
+    def run_axe(self, *args):
+        proc = sp.run([self.axe, "-q"] + list(args), stdout=sp.PIPE,
+                      stderr=sp.STDOUT)
+        return proc.returncode, proc.stdout.decode(errors="replace")
+
+    def test_read_equals_barcode(self):
+        # Used to run off the end of the read buffer
+        infq = self.fastq("in.fq", [("r1", "ATCACG"), ("r2", "CGATGT")])
+        rc, out = self.run_axe("-f", infq, "-F", self.prefix,
+                               "-b", self.barcodes)
+        self.assertEqual(rc, 0, out)
+        # No sequence left after the barcode, so a single N is written
+        self.assertEqual(self.read_out("reg_A_R1.fastq"), "@r1\nN\n+\nI\n")
+        self.assertEqual(self.read_out("reg_B_R1.fastq"), "@r2\nN\n+\nI\n")
+        self.assertEqual(self.read_out("reg_unknown_R1.fastq"), "")
+
+    def test_read_shorter_than_barcode(self):
+        infq = self.fastq("in.fq", [("r1", "ATCA"), ("r2", "")])
+        rc, out = self.run_axe("-f", infq, "-F", self.prefix,
+                               "-b", self.barcodes)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R1.fastq"), "")
+        self.assertIn("@r1\nATCA\n", self.read_out("reg_unknown_R1.fastq"))
+
+    def test_invalid_first_base(self):
+        # Used to skip the unmatched first base and match the barcode at 1
+        infq = self.fastq("in.fq", [("r1", "XATCACGTTTT"),
+                                    ("r2", "ATCACGTTTT")])
+        rc, out = self.run_axe("-f", infq, "-F", self.prefix,
+                               "-b", self.barcodes)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R1.fastq"),
+                         "@r2\nTTTT\n+\nIIII\n")
+        self.assertEqual(self.read_out("reg_unknown_R1.fastq"),
+                         "@r1\nXATCACGTTTT\n+\nIIIIIIIIIII\n")
+
+    def test_malformed_record_is_an_error(self):
+        # Quality shorter than sequence in 2nd record. Used to silently
+        # stop processing and exit 0.
+        text = "@r1\nATCACGTT\n+\nIIIIIIII\n at r2\nATCACGTT\n+\nIII\n"
+        infq = self.write("in.fq", text +
+                          "@r3\nATCACGTT\n+\nIIIIIIII\n")
+        rc, out = self.run_axe("-f", infq, "-F", self.prefix,
+                               "-b", self.barcodes)
+        self.assertNotEqual(rc, 0, out)
+
+    def test_paired_files_of_unequal_length(self):
+        r1 = self.fastq("r1.fq", [("a", "ATCACGTT"), ("b", "ATCACGTT")])
+        r2 = self.fastq("r2.fq", [("a", "TTTT")])
+        rc, out = self.run_axe("-f", r1, "-r", r2, "-F", self.prefix,
+                               "-R", self.prefix, "-b", self.barcodes)
+        self.assertNotEqual(rc, 0, out)
+
+    def test_two_mismatches(self):
+        # 2+ mismatches used to fail with "already in trie"
+        infq = path.join(self.data, "fake_2mm_R1.fq.gz")
+        rc, out = self.run_axe("-m", "2", "-f", infq, "-F", self.prefix,
+                               "-b", path.join(self.data, "fake.barcodes"))
+        self.assertEqual(rc, 0, out)
+        files = self.get_md5_dict()
+        self.assertEqual(files["reg_1_R1.fastq"], files["reg_2_R1.fastq"])
+        self.assertEqual(files["reg_1_R1.fastq"],
+                         '836eaf06938d4a41122f284ed487a9c7')
+
+    def test_long_mismatch_option(self):
+        # Used to segfault on `--mismatch 1`
+        infq = path.join(self.data, "fake_1mm_R1.fq.gz")
+        rc, out = self.run_axe("--mismatch", "1", "-f", infq,
+                               "-F", self.prefix,
+                               "-b", path.join(self.data, "fake.barcodes"))
+        self.assertEqual(rc, 0, out)
+        files = self.get_md5_dict()
+        self.assertEqual(files["reg_1_R1.fastq"],
+                         '836eaf06938d4a41122f284ed487a9c7')
+
+    def test_bad_mismatch_value(self):
+        infq = self.fastq("in.fq", [("r1", "ATCACGTT")])
+        for bad in ("x", "-1", "1x", ""):
+            rc, out = self.run_axe("-m", bad, "-f", infq, "-F", self.prefix,
+                                   "-b", self.barcodes)
+            self.assertNotEqual(rc, 0, out)
+
+    def test_output_directory_prefix(self):
+        # `-F dir` (no trailing slash) used to write `dirA_R1.fastq`
+        outdir = path.join(self.out, "outdir")
+        os.makedirs(outdir)
+        infq = self.fastq("in.fq", [("r1", "ATCACGTT")])
+        for prefix in (outdir, outdir + "/"):
+            rc, out = self.run_axe("-f", infq, "-F", prefix,
+                                   "-b", self.barcodes)
+            self.assertEqual(rc, 0, out)
+            self.assertEqual(sorted(os.listdir(outdir)),
+                             ["A_R1.fastq", "B_R1.fastq",
+                              "unknown_R1.fastq"])
+            self.assertEqual(os.listdir(self.out), ["outdir"])
+
+    def test_trim_r2_short_mate(self):
+        # R2 shorter than the barcode used to underflow, then crash
+        r1 = self.fastq("r1.fq", [("a", "ATCACGTTTT")])
+        r2 = self.fastq("r2.fq", [("a", "AA")])
+        rc, out = self.run_axe("-2", "-f", r1, "-r", r2, "-F", self.prefix,
+                               "-R", self.prefix, "-b", self.barcodes)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R1.fastq"),
+                         "@a\nTTTT\n+\nIIII\n")
+        self.assertEqual(self.read_out("reg_A_R2.fastq"), "@a\nN\n+\nI\n")
+
+    def test_trim_r2_leaves_mate_untrimmed_by_default(self):
+        r1 = self.fastq("r1.fq", [("a", "ATCACGTTTT")])
+        r2 = self.fastq("r2.fq", [("a", "AA")])
+        rc, out = self.run_axe("-f", r1, "-r", r2, "-F", self.prefix,
+                               "-R", self.prefix, "-b", self.barcodes)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R2.fastq"),
+                         "@a\nAA\n+\nII\n")
+
+    def test_combo_short_reads(self):
+        # Read == barcode used to over-read; truncation used to underflow
+        r1 = self.fastq("r1.fq", [("a", "ATCACG"), ("b", "CGATGTAAA")])
+        r2 = self.fastq("r2.fq", [("a", "CCCCTTT"), ("b", "GGGG")])
+        rc, out = self.run_axe("-c", "-f", r1, "-r", r2, "-F", self.prefix,
+                               "-R", self.prefix, "-b", self.combo_barcodes)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R1.fastq"), "@a\nN\n+\nI\n")
+        self.assertEqual(self.read_out("reg_A_R2.fastq"),
+                         "@a\nTTT\n+\nIII\n")
+        self.assertEqual(self.read_out("reg_B_R1.fastq"),
+                         "@b\nAAA\n+\nIII\n")
+        self.assertEqual(self.read_out("reg_B_R2.fastq"), "@b\nN\n+\nI\n")
+
+    def test_conflicting_barcodes(self):
+        # Barcodes 1 mismatch apart can't be separated with -m 1
+        bcs = self.write("close.tsv", "AAAAAA\tA\nAAAAAC\tB\n")
+        infq = self.fastq("in.fq", [("a", "AAAAAATTT"), ("b", "AAAAACTTT"),
+                                    ("tie", "AAAAAGTTT"), ("a1", "CAAAAATTT")])
+        rc, out = self.run_axe("-m", "1", "-f", infq, "-F", self.prefix,
+                               "-b", bcs)
+        self.assertNotEqual(rc, 0, out)
+        # With --permissive, exact matches stay exact, and only unambiguous
+        # mutants are matched.
+        rc, out = self.run_axe("-m", "1", "-p", "-f", infq, "-F", self.prefix,
+                               "-b", bcs)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R1.fastq"),
+                         "@a\nTTT\n+\nIII\n at a1\nTTT\n+\nIII\n")
+        self.assertEqual(self.read_out("reg_B_R1.fastq"),
+                         "@b\nTTT\n+\nIII\n")
+        self.assertEqual(self.read_out("reg_unknown_R1.fastq"),
+                         "@tie\nAAAAAGTTT\n+\nIIIIIIIII\n")
+
+    def test_combo_close_barcodes(self):
+        # R1 barcodes within 1 mismatch of each other. B's exact barcode was
+        # once taken for a mutant of A, and so assigned to A.
+        bcs = self.write("cbc.tsv", "ATCACG\tCCCC\tA\nATCACT\tGGGG\tB\n")
+        r1 = self.fastq("r1.fq", [("a", "ATCACGAA"), ("b", "ATCACTAA")])
+        r2 = self.fastq("r2.fq", [("a", "CCCCTT"), ("b", "GGGGTT")])
+        rc, out = self.run_axe("-c", "-m", "1", "-p", "-f", r1, "-r", r2,
+                               "-F", self.prefix, "-R", self.prefix,
+                               "-b", bcs)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R1.fastq"),
+                         "@a\nAA\n+\nII\n")
+        self.assertEqual(self.read_out("reg_B_R1.fastq"),
+                         "@b\nAA\n+\nII\n")
+
+    def test_barcode_file_quirks(self):
+        # blank lines, comments, lower case and CRLF line endings are fine
+        bcs = self.write("q.tsv", "Barcode\tID\r\n\n# note\natcacg\tA\r\n"
+                         "\nCGATGT\tB\n\n")
+        infq = self.fastq("in.fq", [("r1", "ATCACGTT")])
+        rc, out = self.run_axe("-f", infq, "-F", self.prefix, "-b", bcs)
+        self.assertEqual(rc, 0, out)
+        self.assertEqual(self.read_out("reg_A_R1.fastq"),
+                         "@r1\nTT\n+\nII\n")
+
+    def test_bad_barcode_files(self):
+        infq = self.fastq("in.fq", [("r1", "ATCACGTT")])
+        for text in ("ATCACX\tA\n",                 # not a base
+                     "ATCACG\tA\nCGATGT\tA\n",    # duplicate ID
+                     "ATCACG\tA\nATCACG\tB\n",    # duplicate barcode
+                     "# nothing here\n",             # no barcodes
+                     "ATCACG\n"):                    # no ID
+            bcs = self.write("bad.tsv", text)
+            rc, out = self.run_axe("-f", infq, "-F", self.prefix, "-b", bcs)
+            self.assertNotEqual(rc, 0, (text, out))
+
+    def test_barcode_shorter_than_mismatches(self):
+        # Used to abort() inside GSL
+        bcs = self.write("short.tsv", "AC\tA\nGT\tB\n")
+        infq = self.fastq("in.fq", [("r1", "ACTT")])
+        rc, out = self.run_axe("-m", "3", "-p", "-f", infq, "-F", self.prefix,
+                               "-b", bcs)
+        self.assertEqual(rc, 0, out)
+
+    def test_corrupt_gzip_terminates(self):
+        # A read error in the middle of a record used to loop forever
+        import gzip
+        reads = [("r%d" % i, "ATCACG" + "ACGT" * 30) for i in range(5000)]
+        raw = path.join(self.tmp, "big.fq")
+        self.fastq("big.fq", reads)
+        with open(raw, "rb") as fh:
+            data = fh.read()
+        gz = bytearray(gzip.compress(data))
+        mid = len(gz) // 2
+        for i in range(mid, mid + 64):
+            gz[i] ^= 0xFF
+        bad = self.write("bad.fq.gz", "")
+        with open(bad, "wb") as fh:
+            fh.write(gz)
+        proc = sp.run([self.axe, "-q", "-f", bad, "-F", self.prefix,
+                       "-b", self.barcodes], stdout=sp.PIPE, stderr=sp.STDOUT,
+                      timeout=60)
+        self.assertNotEqual(proc.returncode, 0)
+
+    def test_fasta_input_rejected(self):
+        fa = self.write("in.fa", ">r1\nATCACGTT\n")
+        rc, out = self.run_axe("-f", fa, "-F", self.prefix,
+                               "-b", self.barcodes)
+        self.assertNotEqual(rc, 0, out)
+
+    def test_empty_input_and_summary(self):
+        # No reads: no division by zero in the summary, and a clean exit
+        infq = self.write("empty.fq", "")
+        proc = sp.run([self.axe, "-f", infq, "-F", self.prefix,
+                       "-b", self.barcodes], stdout=sp.PIPE, stderr=sp.STDOUT)
+        out = proc.stdout.decode(errors="replace")
+        self.assertEqual(proc.returncode, 0, out)
+        self.assertNotIn("nan", out.lower())
+        self.assertNotIn("inf", out.lower())
+
+    def test_usage_errors(self):
+        infq = self.fastq("in.fq", [("r1", "ATCACGTT")])
+        proc = sp.run([self.axe], stdout=sp.PIPE, stderr=sp.STDOUT)
+        self.assertNotEqual(proc.returncode, 0)
+        rc, out = self.run_axe("-z", "12", "-f", infq, "-F", self.prefix,
+                               "-b", self.barcodes)
+        self.assertNotEqual(rc, 0, out)
 
 
 if __name__ == '__main__':


=====================================
tests/axe_simulation_tests.py
=====================================
@@ -0,0 +1,161 @@
+#!/usr/bin/env python3
+"""Simulate 1M (pairs of) reads per scenario and compare axe's output to
+the demultiplexing a nearest-barcode oracle expects. Needs numpy."""
+import os
+import subprocess as sp
+import sys
+import tempfile
+import unittest
+
+try:
+    import numpy as np
+except ImportError:
+    print("numpy not available, skipping simulation tests")
+    sys.exit(0)
+
+BINDIR = sys.argv.pop(1) if len(sys.argv) > 1 else "."
+AXE = os.path.join(BINDIR, "bin", "axe-demux")
+N = 1000000
+GENOMIC = 50
+PSTI = b"TGCAG"
+ACGT = np.frombuffer(b"ACGT", np.uint8)
+CODE = np.zeros(256, np.uint8)
+CODE[ACGT] = range(4)
+
+
+def make_barcodes(rng, n, lengths):
+    seen = set()
+    while len(seen) < n:
+        seen.add(ACGT[rng.integers(0, 4, rng.choice(lengths))].tobytes())
+    return sorted(seen)
+
+
+def simulate(rng, bcs, who, mm, site):
+    """Reads of samples `who`, with 0..mm+1 barcode errors, 2% pure junk."""
+    n = len(who)
+    pad = np.zeros((len(bcs), 8), np.uint8)
+    for i, bc in enumerate(bcs):
+        pad[i, :len(bc)] = list(bc)
+    lens = np.array([len(bc) for bc in bcs])[who]
+    inside = np.arange(8) < lens[:, None]
+    bar = pad[who]
+    score = np.where(inside, rng.random((n, 8)), 2)
+    err = np.argsort(np.argsort(score, 1), 1) < rng.integers(0, mm + 2, n)[:, None]
+    mut = ACGT[(CODE[bar] + rng.integers(1, 4, (n, 8))) % 4]
+    bar = np.where(err & inside, mut, bar)
+    junk = rng.random(n) < 0.02
+    bar[junk] = ACGT[rng.integers(0, 4, (junk.sum(), 8))]
+    tail = np.hstack([np.tile(np.frombuffer(site, np.uint8), (n, 1)),
+                      ACGT[rng.integers(0, 4, (n, GENOMIC))]])
+    return [bar[i, :lens[i]].tobytes() + tail[i].tobytes() for i in range(n)]
+
+
+def assign(reads, bcs, mm):
+    """Index of the unique nearest barcode within mm (longest barcodes
+    first, as the prefix trie does), else -1."""
+    first = np.frombuffer(b"".join(r[:8] for r in reads), np.uint8).reshape(-1, 8)
+    out = np.full(len(reads), -1)
+    for length in sorted({len(bc) for bc in bcs}, reverse=True):
+        ids = np.array([i for i, bc in enumerate(bcs) if len(bc) == length])
+        bar = np.array([list(bcs[i]) for i in ids], np.uint8)
+        for s in range(0, len(reads), 50000):
+            dist = (first[s:s + 50000, None, :length] != bar).sum(2)
+            best = dist.min(1)
+            hit = ((best <= mm) & ((dist == best[:, None]).sum(1) == 1)
+                   & (out[s:s + 50000] < 0))
+            out[s:s + 50000][hit] = ids[dist.argmin(1)][hit]
+    return out
+
+
+def record(i, seq, trim=0):
+    return b"@r%d\n%s\n+\n%s\n" % (i, seq[trim:], b"I" * (len(seq) - trim))
+
+
+class Simulation(unittest.TestCase):
+    def setUp(self):
+        self.rng = np.random.default_rng(42)
+        self.tmp = tempfile.TemporaryDirectory()
+
+    def tearDown(self):
+        self.tmp.cleanup()
+
+    def path(self, name):
+        return os.path.join(self.tmp.name, name)
+
+    def write(self, name, chunks):
+        with open(self.path(name), "wb") as fh:
+            fh.writelines(chunks)
+        return self.path(name)
+
+    def run_axe(self, mm, *args):
+        cmd = [AXE, "-q", "-p", "-m", str(mm), "-F", self.path("o")] + list(args)
+        proc = sp.run(cmd, stdout=sp.PIPE, stderr=sp.STDOUT)
+        self.assertEqual(proc.returncode, 0, proc.stdout.decode())
+
+    def compare(self, expected):
+        names = {n for n in os.listdir(self.tmp.name) if n.startswith("o_")}
+        self.assertEqual(names, {"o_%s_%s.fastq" % k for k in expected})
+        for (sample, mate), recs in expected.items():
+            with open(self.path("o_%s_%s.fastq" % (sample, mate)), "rb") as fh:
+                self.assertEqual(fh.read(), b"".join(recs), (sample, mate))
+
+    def single_end(self, bcs, mm):
+        who = self.rng.integers(0, len(bcs), N)
+        reads = simulate(self.rng, bcs, who, mm, b"")
+        found = assign(reads, bcs, mm)
+        self.assertGreater((found == who).mean(), 0.2)
+        names = ["s%02d" % i for i in range(len(bcs))]
+        bcfile = self.write("bc.tsv", [b"%s\t%s\n" % (b, n.encode())
+                                       for b, n in zip(bcs, names)])
+        fq = self.write("in.fq", [record(i, r) for i, r in enumerate(reads)])
+        self.run_axe(mm, "-f", fq, "-b", bcfile)
+        expected = {(n, "R1"): [] for n in names + ["unknown"]}
+        for i, (r, a) in enumerate(zip(reads, found)):
+            if a < 0:
+                expected[("unknown", "R1")].append(record(i, r))
+            else:
+                expected[(names[a], "R1")].append(record(i, r, len(bcs[a])))
+        self.compare(expected)
+
+    def combinatorial(self, n1, n2, lengths, mm, site):
+        bc1 = make_barcodes(self.rng, n1, lengths)
+        bc2 = make_barcodes(self.rng, n2, lengths)
+        w1 = self.rng.integers(0, n1, N)
+        w2 = self.rng.integers(0, n2, N)
+        r1 = simulate(self.rng, bc1, w1, mm, site)
+        r2 = simulate(self.rng, bc2, w2, mm, site)
+        f1, f2 = assign(r1, bc1, mm), assign(r2, bc2, mm)
+        self.assertGreater(((f1 == w1) & (f2 == w2)).mean(), 0.2)
+        bcfile = self.write("bc.tsv", [b"%s\t%s\ts%d_%d\n" % (a, b, i, j)
+                                       for i, a in enumerate(bc1)
+                                       for j, b in enumerate(bc2)])
+        fq1 = self.write("r1.fq", [record(i, r) for i, r in enumerate(r1)])
+        fq2 = self.write("r2.fq", [record(i, r) for i, r in enumerate(r2)])
+        self.run_axe(mm, "-c", "-f", fq1, "-r", fq2, "-R", self.path("o"),
+                     "-b", bcfile)
+        expected = {("unknown", m): [] for m in ("R1", "R2")}
+        expected.update({("s%d_%d" % (i, j), m): [] for i in range(n1)
+                         for j in range(n2) for m in ("R1", "R2")})
+        for i in range(N):
+            a, b = f1[i], f2[i]
+            if a < 0 or b < 0:
+                expected[("unknown", "R1")].append(record(i, r1[i]))
+                expected[("unknown", "R2")].append(record(i, r2[i]))
+            else:
+                sample = "s%d_%d" % (a, b)
+                expected[(sample, "R1")].append(record(i, r1[i], len(bc1[a])))
+                expected[(sample, "R2")].append(record(i, r2[i], len(bc2[b])))
+        self.compare(expected)
+
+    def test_48_single_end_6bp_2mm(self):
+        self.single_end(make_barcodes(self.rng, 48, [6]), 2)
+
+    def test_96_combinatorial_8bp_4mm(self):
+        self.combinatorial(12, 8, [8], 4, b"")
+
+    def test_96_combinatorial_gbs_4_8bp_2mm_psti(self):
+        self.combinatorial(12, 8, [4, 5, 6, 7, 8], 2, PSTI)
+
+
+if __name__ == "__main__":
+    unittest.main()


=====================================
tests/test_libaxe.c
=====================================
@@ -48,48 +48,102 @@ end:
     ;
 }
 
+/* Number of positions at which two equal-length strings differ */
+static size_t
+hamming(const char *a, const char *b)
+{
+    size_t d = 0;
+
+    for (; *a != '\0' && *b != '\0'; a++, b++) {
+        d += (*a != *b);
+    }
+    return d;
+}
+
 static void
 test_hamming_mutate (void *ptr)
 {
     char **mutated = NULL;
     size_t count = 0;
     size_t iii = 0;
-    const char *str = "AAAA";
-    const char *truth[] = {
-        "AAAA", "ACAA", "AGAA", "ATAA", "CAAA", "CCAA", "CGAA", "CTAA", "GAAA",
-        "GCAA", "GGAA", "GTAA", "TAAA", "TCAA", "TGAA", "TTAA", "AAAA", "AACA",
-        "AAGA", "AATA", "CAAA", "CACA", "CAGA", "CATA", "GAAA", "GACA", "GAGA",
-        "GATA", "TAAA", "TACA", "TAGA", "TATA", "AAAA", "AAAC", "AAAG", "AAAT",
-        "CAAA", "CAAC", "CAAG", "CAAT", "GAAA", "GAAC", "GAAG", "GAAT", "TAAA",
-        "TAAC", "TAAG", "TAAT", "AAAA", "AACA", "AAGA", "AATA", "ACAA", "ACCA",
-        "ACGA", "ACTA", "AGAA", "AGCA", "AGGA", "AGTA", "ATAA", "ATCA", "ATGA",
-        "ATTA", "AAAA", "AAAC", "AAAG", "AAAT", "ACAA", "ACAC", "ACAG", "ACAT",
-        "AGAA", "AGAC", "AGAG", "AGAT", "ATAA", "ATAC", "ATAG", "ATAT", "AAAA",
-        "AAAC", "AAAG", "AAAT", "AACA", "AACC", "AACG", "AACT", "AAGA", "AAGC",
-        "AAGG", "AAGT", "AATA", "AATC", "AATG", "AATT", };
+    size_t jjj = 0;
+    const char *str = "ACGT";
 
     (void) ptr;
+    /* dist 2 of a 4-mer: C(4,2) * 3^2 = 54 strings, plus the original */
     mutated  = hamming_mutate_dna(&count, str, strlen(str), 2, 1);
     tt_ptr_op(mutated, !=, NULL);
-    tt_int_op(count, ==, 96);
-    for (iii = 0; iii < count; iii++) {
+    tt_int_op(count, ==, 55);
+    tt_str_op(mutated[0], ==, str);
+    for (iii = 1; iii < count; iii++) {
         tt_ptr_op(mutated[iii], !=, NULL);
-        tt_str_op(mutated[iii], ==, truth[iii]);
+        /* Exactly dist mismatches, and no duplicates */
+        tt_int_op(hamming(mutated[iii], str), ==, 2);
+        for (jjj = 0; jjj < iii; jjj++) {
+            tt_str_op(mutated[iii], !=, mutated[jjj]);
+        }
     }
+    for (iii = 0; iii < count; iii++) {
+        free(mutated[iii]);
+    }
+    free(mutated);
 
-end:
-    if (mutated != NULL) {
-        for (iii = 0; iii < count; iii++) {
-            if (mutated[iii] != NULL) {
-                free(mutated[iii]);
-            }
-        }
-        free(mutated);
+    /* Without the original, and dist 1: 4 * 3 */
+    mutated  = hamming_mutate_dna(&count, str, strlen(str), 1, 0);
+    tt_ptr_op(mutated, !=, NULL);
+    tt_int_op(count, ==, 12);
+    for (iii = 0; iii < count; iii++) {
+        tt_int_op(hamming(mutated[iii], str), ==, 1);
     }
+    for (iii = 0; iii < count; iii++) {
+        free(mutated[iii]);
+    }
+    free(mutated);
+    mutated = NULL;
+
+    /* Can't have more mismatches than bases */
+    tt_ptr_op(hamming_mutate_dna(&count, "AC", 2, 3, 0), ==, NULL);
+
+end:
+    ;
+}
+
+static void
+test_revcomp (void *ptr)
+{
+    char odd[] = "AACGT";
+    char even[] = "AACG";
+    char junk[] = "ACxT\n";
+    char *rc = NULL;
+
+    (void) ptr;
+    qes_sequtil_revcomp_inplace(odd, strlen(odd));
+    tt_str_op(odd, ==, "ACGTT");
+    qes_sequtil_revcomp_inplace(even, strlen(even));
+    tt_str_op(even, ==, "CGTT");
+    /* trailing whitespace is trimmed, other bases become N */
+    qes_sequtil_revcomp_inplace(junk, strlen(junk));
+    tt_str_op(junk, ==, "ANGT");
+    /* len larger than the string must not run off its end */
+    rc = qes_sequtil_revcomp("AACGT", 100);
+    tt_ptr_op(rc, !=, NULL);
+    tt_str_op(rc, ==, "ACGTT");
+    free(rc);
+    /* len smaller than the string uses only the first len bases */
+    rc = qes_sequtil_revcomp("AACGT", 3);
+    tt_ptr_op(rc, !=, NULL);
+    tt_str_op(rc, ==, "GTT");
+    free(rc);
+    rc = qes_sequtil_revcomp("", 5);
+    tt_ptr_op(rc, !=, NULL);
+    tt_str_op(rc, ==, "");
+end:
+    free(rc);
 }
 
 struct testcase_t core_tests[] = {
     { "product", test_product, 0, NULL, NULL},
     { "hamming_mutate", test_hamming_mutate, 0, NULL, NULL},
+    { "revcomp", test_revcomp, 0, NULL, NULL},
     END_OF_TESTCASES
 };



View it on GitLab: https://salsa.debian.org/med-team/axe-demultiplexer/-/compare/5861a7b8281dccc22b4a364da1c5e99570767b01...bf9eb79b5481cc7903d2e1b51123e0f0abd537e3

-- 
View it on GitLab: https://salsa.debian.org/med-team/axe-demultiplexer/-/compare/5861a7b8281dccc22b4a364da1c5e99570767b01...bf9eb79b5481cc7903d2e1b51123e0f0abd537e3
You're receiving this email because of your account on salsa.debian.org. Manage all notifications: https://salsa.debian.org/-/profile/notifications | Help: https://salsa.debian.org/help


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/debian-med-commit/attachments/20261111/1634ef31/attachment-0001.htm>


More information about the debian-med-commit mailing list