affinegap

raw JSON →
1.12 verified Fri May 01 auth: no python

A Cython implementation of the affine gap string distance for measuring string similarity with affine gap penalties. Current version 1.12, stable but rarely updated.

pip install affinegap
error ImportError: cannot import name 'affinegap'
cause Trying to do `from affinegap import affinegap` instead of `import affinegap`.
fix
Replace with import affinegap and use affinegap.affineGapDistance.
error AttributeError: module 'affinegap' has no attribute 'affinegap'
cause Same as above - the module does not contain a function named affinegap.
fix
Use affinegap.affineGapDistance.
error TypeError: expected string or Unicode object
cause Passing a non-string (e.g., integer) to affineGapDistance.
fix
Convert arguments to strings: affinegap.affineGapDistance(str(a), str(b))
gotcha The function is named `affineGapDistance` (capital G) not `affinegap`. Many users mistakenly try `affinegap.distance()`.
fix Use `affinegap.affineGapDistance(s1, s2)`
gotcha Default behavior: by default, match_weight=0, mismatch_weight=1, gap_weight=1, spatial_gap_weight=1. This means identical strings get distance 0, but a single mismatch or gap adds 1. This may not be what you expect for 'similarity'.
fix Adjust parameters: e.g., match_weight=-1, mismatch_weight=1, gap_weight=-1 for similarity-like scores.
gotcha Affine gap distance is not symmetric? Actually it is symmetric, but careful: the function expects two strings; if you pass integer or None, it may throw TypeError.
fix Ensure both arguments are strings.

Basic usage: compute affine gap string distance. The function returns an integer distance or similarity score depending on penalties.

import affinegap

# Compute affine gap distance between two strings
dist = affinegap.affineGapDistance("string1", "string2")
print(dist)

# With custom gap penalties
dist = affinegap.affineGapDistance("hello", "hallo", match_weight=0, mismatch_weight=1, gap_weight=1, spatial_gap_weight=1)
print(dist)