Skip to main content

Efficient implementations of Needleman-Wunsch and other sequence alignment algorithms in Rust with Python bindings.

Project description

sequence_align

Efficient implementations of Needleman-Wunsch and other sequence alignment algorithms written in Rust with Python bindings via PyO3.

Installation

sequence_align is distributed via PyPi for Python 3.7+, making installation as simple as the following -- no special setup required for cross-platform compatibility, Rust installation, etc.!

pip install sequence_align

Alternatively, if one wishes to develop for sequence_align, first ensure that both Python and Rust are installed on your system. Then, install Maturin and run maturin develop (optionally with the -r flag to compile a release build, instead of an unoptimized debug build) from the root of your cloned repo to build and install sequence_align in your active Python environment.

Quick Start

Pairwise sequence algorithms are available in sequence_align.pairwise. Currently, two algorithms are implemented: the Needleman-Wunsch algorithm and Hirschberg’s algorithm. Needleman-Wunsch is commonly used for global sequence alignment, but suffers from the fact that it uses O(M*N) space, where M and N are the lengths of the two sequences being aligned. Hirschberg’s algorithm modifies Needleman-Wunsch to have the same time complexity (O(M*N)), but only use O(min{M, N}) space, making it an appealing option for memory-limited applications or extremely large sequences.

Using these algorithms is straightforward:

from sequence_align.pairwise import hirschberg, needleman_wunsch


# See https://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm#/media/File:Needleman-Wunsch_pairwise_sequence_alignment.png
# Use Needleman-Wunsch default scores (match=1, mismatch=-1, indel=-1)
seq_a = ["G", "A", "T", "T", "A", "C", "A"]
seq_b = ["G", "C", "A", "T", "G", "C", "G"]

aligned_seq_a, aligned_seq_b = needleman_wunsch(
    seq_a,
    seq_b,
    match_score=1.0,
    mismatch_score=-1.0,
    indel_score=-1.0,
    gap="_",
)

# Expects ["G", "_", "A", "T", "T", "A", "C", "A"]
print(aligned_seq_a)

# Expects ["G", "C", "A", "_", "T", "G", "C", "G"]
print(aligned_seq_b)


# See https://en.wikipedia.org/wiki/Hirschberg%27s_algorithm#Example
seq_a = ["A", "G", "T", "A", "C", "G", "C", "A"]
seq_b = ["T", "A", "T", "G", "C"]

aligned_seq_a, aligned_seq_b = hirschberg(
    seq_a,
    seq_b,
    match_score=2.0,
    mismatch_score=-1.0,
    indel_score=-2.0,
    gap="_",
)

# Expects ["A", "G", "T", "A", "C", "G", "C", "A"]
print(aligned_seq_a)

# Expects ["_", "_", "T", "A", "T", "G", "C", "_"]
print(aligned_seq_b)

Performance Benchmarks

All tests below were conducted sequentially on a AWS R5.4 instance with 16 cores and 128 GB of memory. The pair of sequences for alignment consist of a character sequence of randomly selected A/C/G/T nucleotide bases along with another that is identical, except with 10% of the characters randomly perturbed by deletion, insertion of another randomly-selected character after the entry, or replacement with a different randomly-selected character.

As one can see, while sequence_align is comparable to some other toolkits in terms of speed, its memory performance is best-in-class, even when compared to toolkits using the same algorithm, such as Needleman-Wunsch being used in pyseq-align.

(Please note that some lines terminate early, as some toolkits took prohibitively long and/or ran out of memory at higher scales.)

License

Licensed under the Apache 2.0 License. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2023-present Kensho Technologies, LLC. The present date is determined by the timestamp of the most recent commit in the repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

sequence_align-0.1.0-cp37-abi3-win_amd64.whl (115.8 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

sequence_align-0.1.0-cp37-abi3-win32.whl (107.9 kB view hashes)

Uploaded CPython 3.7+ Windows x86

sequence_align-0.1.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (546.7 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

sequence_align-0.1.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (649.1 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ s390x

sequence_align-0.1.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (556.4 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ppc64le

sequence_align-0.1.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (548.4 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARMv7l

sequence_align-0.1.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (532.1 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

sequence_align-0.1.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl (557.2 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.5+ i686

sequence_align-0.1.0-cp37-abi3-macosx_11_0_arm64.whl (206.4 kB view hashes)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

sequence_align-0.1.0-cp37-abi3-macosx_10_7_x86_64.whl (220.0 kB view hashes)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page