Skip to main content

Build Status PyPI version Python Version Code style License

Needleman-Wunsch and Smith-Waterman algorithms in python for any iterable objects.

Algorithms

Needleman-Wunsch

The Needleman–Wunsch algorithm is an algorithm used in bioinformatics to align protein or nucleotide sequences. It was one of the first applications of dynamic programming to compare biological sequences. The algorithm was developed by Saul B. Needleman and Christian D. Wunsch and published in 1970. The algorithm essentially divides a large problem (e.g. the full sequence) into a series of smaller problems and uses the solutions to the smaller problems to reconstruct a solution to the larger problem. It is also sometimes referred to as the optimal matching algorithm and the global alignment technique. The Needleman–Wunsch algorithm is still widely used for optimal global alignment, particularly when the quality of the global alignment is of the utmost importance.

-- From the Wikipedia article

Smith-Waterman

The Smith–Waterman algorithm performs local sequence alignment; that is, for determining similar regions between two strings of nucleic acid sequences or protein sequences. Instead of looking at the entire sequence, the Smith–Waterman algorithm compares segments of all possible lengths and optimizes the similarity measure.

-- From the Wikipedia article

Usage

from minineedle import needle, smith, core

# Use miniseq objects
# Load sequences as miniseq FASTA object
import miniseq
fasta = miniseq.FASTA(filename="myfasta.fa")
seq1, seq2 = fasta[0], fasta[1]

# Or use strings, lists, etc
# seq1, seq2 = "ACTG", "ATCTG"
# seq1, seq2 = ["A","C","T","G"], ["A","T","C","T","G"]

# Create the instance
alignment: needle.NeedlemanWunsch[str] = needle.NeedlemanWunsch(seq1, seq2)
# or
# alignment smith.SmithWaterman[str] = smith.SmithWaterman(seq1, seq2)

# Make the alignment
alignment.align()

# Get the score
alignment.get_score()

# Get the sequences aligned as lists
al1, al2 = alignment.get_aligned_sequences(core.AlignmentFormat.list) # or "list"

# Get the sequences as strings
al1, al2 = alignment.get_aligned_sequences(core.AlignmentFormat.str) # or "str

# Change the matrix and run again
alignment.change_matrix(core.ScoreMatrix(match=4, miss=-4, gap=-2))
alignment.align()

# Print the sequences aligned
print(alignment)

# Change gap character
alignment.gap_character = "-gap-"
print(alignment)

# Sort a list of alignments by score
first_al  = needle.NeedlemanWunsch(seq1, seq2)
second_al = needle.NeedlemanWunsch(seq3, seq4)

for align in sorted([first_al, second_al], reverse=True):
    print(align)

Install

pip install minineedle

Classes

NeedlemanWunsch

Needleman-Wunsch alignment class. It has the following attributes:

  • seq1
  • seq2
  • alseq1
  • alseq2
  • nmatrix
  • pmatrix
  • smatrix
  • score
  • identity
  • gap_character

To create the instance you have to provide two iterable objects with elements that can be compared with "==".

SmithWaterman

Smith-Waterman alignment class. It has the following attributes:

  • seq1
  • seq2
  • alseq1
  • alseq2
  • nmatrix
  • pmatrix
  • smatrix
  • score
  • identity

To create the instance you have to provide two iterable objects with elements that can be compared with "==".

ScoreMatrix

With this class you can define your own score matrices. It has three attributes:

  • match
  • miss
  • gap

Methods

align()

Performs the alignment.

get_score()

Returns the score of the alignment. It runs align() if it has not been done yet.

change_matrix(newmatrix)

Takes a ScoreMatrix object and updates the matrix for the alignment. You still have to run it calling align().

get identity()

Returns the % of identity (rounded with 2 decimal points).

get_almatrix()

Return the alignment matrix as a list of lists.

Metadata

Release files for minineedle 3.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for minineedle 3.1.7
File Size Uploaded
minineedle-3.1.7.tar.gz 17.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for minineedle 3.1.7
File Interpreter ABI Platform
minineedle-3.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 37.7 kB

Release files / minineedle-3.1.7.tar.gz

Download URL minineedle-3.1.7.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8efa983c50fd6449663a1b054898e3599fe2ffe4682bb4634d82f8b7aa892b3f
BLAKE2b-256 checksum
How to use checksums
e26d6bf1d277c8e221d452359df227bd5198835ba077e7bd9d1cc41a548cc0b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.13.1 Linux/6.17.0-1022-azure

Release files / minineedle-3.1.7-py3-none-any.whl

Download URL minineedle-3.1.7-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4aeeb6dc69b4b75b58e2e3c973b6f1e9cefedf6c9e2503e426d11f6939b14d24
BLAKE2b-256 checksum
How to use checksums
3befab8456473a2cf6018299bfe05a621b52de87a0559e8f51f8adefab8ecb8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.13.1 Linux/6.17.0-1022-azure

Release history Release notifications | RSS feed

This release

3.1.7 This release

2 release files

3.1.5

2 release files

3.1.4

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1

1 release file

2.0

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page