Skip to main content

A Python library for manipulation of CIGAR, MSA, and other alignment formats.

Project description

codecov types - Mypy Ruff License - AGPL3 PRs Welcome

Alignment tools

This Python library provides robust tools for handling CIGAR strings and related alignment formats in bioinformatics. CIGAR ("Compact Idiosyncratic Gapped Alignment Report") strings succinctly represent alignment data between sequences - such as genomic sequences - highlighting matches, mismatches, insertions, deletions, and more within aligned sequences. The functionality within this library abstracts and simplifies interactions with CIGAR strings, making it easier to manipulate, interpret, and convert these alignment specifications for analytical and visualization purposes.

Features

  • Easy CIGAR String Parsing and Construction: Easily parse CIGAR strings from text or create them programmatically with detailed annotations for operations like matching, inserting, or deleting.
  • Alignment Manipulation: Slice, trim, and concatenate sequence alignments for advanced genomic analyses.
  • Coordinate Mapping: Provides bi-directional mapping between reference and query sequences, enabling you to track and modify coordinates with precision.
  • Gap Identification: Detect gaps, insertions, and deletions within alignments to facilitate adjustments and further analysis.
  • Multiple Sequence Alignment (MSA): Convert CIGAR strings into multiple sequence alignment (MSA) representations for visualization or further processing.
  • CIGAR String Normalization: Merge consecutive identical operations and support merging non-overlapping alignments to normalize complex CIGAR strings.

Examples

Parsing and Manipulating a CIGAR String

You can easily parse a CIGAR string and perform various operations using the Cigar class:

from aligntools import Cigar

# Parse a CIGAR string
cigar = Cigar.parse("10M1I5D5M")

# Enumerate operations
for operation in cigar.iterate_operations():
    print(operation)

# Output:
# M M M M M M M M M M I D D D D D M M M M M

# Calculate the lengths of the reference and query sequences
print(f"Reference Length: {cigar.ref_length}")
print(f"Query Length: {cigar.query_length}")

# Output:
# Reference Length: 20
# Query Length: 16

Cutting a CIGAR String by Reference Position

CIGAR strings can be split at specific reference positions, allowing for more fine-grained control over the alignment:

from aligntools import Cigar, CigarHit

# Parse a CIGAR string and create a CigarHit
cigar = Cigar.coerce("10M5I10M")
hit = CigarHit(cigar, r_st=0, r_ei=19, q_st=0, q_ei=24)

# Cut the alignment at reference position 10.5 (at midpoint between positions 10 and 11).
left, right = hit.cut_reference(10.5)

print("Left slice:", left)
print("Right slice:", right)

# Output:
# Left slice: 10M5I1M@[0,15]->[0,10]
# Right slice: 9M@[16,24]->[11,19]

Trimming Query and Reference Sequences

You can trim unmatched regions from either the query or reference sequence:

from aligntools import Cigar

# Parse a CIGAR string with unmatched regions
cigar = Cigar.coerce("5S10M5S")

# Trim unmatched regions from the query sequence
trimmed_cigar = cigar.rstrip_query()

print(f"Trimmed CIGAR: {trimmed_cigar}")

# Output:
# Trimmed CIGAR: 5S10M

Converting CIGAR Strings to Multiple Sequence Alignments (MSA)

Convert a CIGAR string to a multiple sequence alignment (MSA) representation for better visualization of how sequences align:

from aligntools import Cigar

# Parse a CIGAR string
cigar = Cigar.coerce("5M2I5M")

# Define reference and query sequences
ref_seq =   "ACGTACGTAC"
query_seq = "ACGTTACGTATG"

# Convert to MSA
ref_msa, query_msa = cigar.to_msa(ref_seq, query_seq)

print(f"Reference MSA: {ref_msa}")
print(f"Query MSA:     {query_msa}")

# Output:
# Reference MSA: ACGTA--CGTAC
# Query MSA:     ACGTTACGTATG

Merging Consecutive Alignments

aligntools can merge two consecutive CIGAR strings into a single normalized CIGAR string:

from aligntools import Cigar

# Parse two CIGAR strings
cigar1 = Cigar.coerce("5M5D")
cigar2 = Cigar.coerce("10M")

# Merge the two alignments
merged_cigar = cigar1 + cigar2

print(f"Merged CIGAR: {merged_cigar}")
# Output: Merged CIGAR: 5M5D10M

Advanced Usage: Coordinate Mapping Between Reference and Query Sequences

You can manage coordinate translations between the reference and query using CoordinateMapping:

from aligntools import Cigar

# Create a CIGAR and its coordinate mapping
cigar = Cigar.coerce("5M2I3M")
mapping = cigar.coordinate_mapping

# Translate reference and query coordinates
ref_coordinate = 3
query_coordinate = mapping.ref_to_query[ref_coordinate]

print(f"Query coordinate: {query_coordinate}")
# Output:
# Query coordinate: 3

ref_coordinate = 6
query_coordinate = mapping.ref_to_query[ref_coordinate]
print(f"Query coordinate: {query_coordinate}")
# Output:
# Query coordinate: 8

Using CigarHit for Complex Alignment Manipulations

The CigarHit class is a higher-level abstraction that allows you to perform complex operations on a sequence alignment:

from aligntools import Cigar, CigarHit

# Define a complex CIGAR string and create a CigarHit
cigar = Cigar.coerce("5M2D5M")
hit = CigarHit(cigar, r_st=0, r_ei=11, q_st=0, q_ei=9)

# Cut and trim the alignment
left, right = hit.cut_reference(4.5)
trimmed_hit = right.rstrip_reference()

print("Left Hit:           ", left)
print("Right (trimmed) Hit:", trimmed_hit)

# Output:
# Left Hit:            5M@[0,4]->[0,4]
# Right (trimmed) Hit: 2D5M@[5,9]->[5,11]

Installation

To use aligntools for your projects, simply run pip install aligntools.

License

This project is licensed under the AGPLv3.0 License. See the COPYING file for details.

Project details


Download files

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

Source Distribution

aligntools-1.2.2.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aligntools-1.2.2-py2.py3-none-any.whl (28.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file aligntools-1.2.2.tar.gz.

File metadata

  • Download URL: aligntools-1.2.2.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for aligntools-1.2.2.tar.gz
Algorithm Hash digest
SHA256 3d54f5603735daab059e93068642933390326992419ce2d0af4ed11689db7345
MD5 3426c58c2fed959830ef2c445e734a66
BLAKE2b-256 2100d04c844fc9a937c116742335fb37e16e2ea00ad89257872763c9e9e96ca3

See more details on using hashes here.

File details

Details for the file aligntools-1.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: aligntools-1.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for aligntools-1.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 015369c567e7abcef75dc6454b9a80126345468c95d408518ff471e71f0e6b11
MD5 d1b9e064190f03d582bca3a56872142c
BLAKE2b-256 49a04fe32e325d99de8ad02e2d8d5325a9a76ffc0974e8fb3420625b12a2dba2

See more details on using hashes here.

Supported by

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