Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pyswrd

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pyswrd

Cython bindings and Python interface to SWORD (Smith Waterman On Reduced Database), a heuristic method for fast database search.

  • 0.3.0
  • PyPI
  • Socket score

Maintainers
1

🐍🗡️ PySWRD Stars

Cython bindings and Python interface to SWORD (Smith Waterman On Reduced Database), a method for fast database search.

Actions Coverage License PyPI Bioconda AUR Wheel Python Versions Python Implementations Source Mirror Issues Docs Changelog Downloads

🗺️ Overview

Searching a sequence inside a database of target sequences involves aligning the sequence to all the targets to find the highest scoring ones, which has a high computational cost. Several methods have been proposed over the years that use a pre-filter to select. In BLAST[1], k-mers are extracted from the query, and only targets containing high-scoring k-mers, with respect to the scoring matrix, are actually aligned.

SWORD[2] proposes a pre-filter built on perfect hashing of short mismatching k-mers. The k-mers generated from the query sequence also include k-mers with mismatches to improve sensitivity. When a k-mer is found in a target sequence, SWORD computes the diagonal where it is located, similarly to FASTA[3]. Target sequences are then selected based on the number of hits they have on the same diagonal. The pairwise alignment is then handled by the platform-accelerated Opal[4] library.

PySWRD is a Python module that provides bindings to the heuristic filter part of SWORD using Cython. It implements a user-friendly, Pythonic interface to build a heuristic filter, process a database in chunks, and produce the indices of targets passing the filter. The resulting indices can be used to filter a PyOpal database, using Opal for pairwise alignment like the original C++ implementation.

  • no binary dependency: PySWRD is distributed as a Python package, so you can add it as a dependency to your project, and stop worrying about the SWORD binary being present on the end-user machine.
  • no intermediate files: Everything happens in memory, in a Python object you control, so you don't have to invoke the SWORD CLI using a sub-process and temporary files.
  • better portability: Using only the heuristic filter of SWORD allows the code to be independent of the local CPU features, unlike SWORD and Opal which require SIMD. PySWRD delegates the SIMD compilation and dynamic dispatch to PyOpal to make the package easier to install. It also benefits from the wider platform support of PyOpal compared to the original Opal, featuring support for Windows and for Aarch64 CPUs.

🔧 Installing

PySWRD is available for all modern Python versions (3.6+).

It can be installed directly from PyPI, which hosts some pre-built x86-64 wheels for Linux, MacOS, and Windows, as well as the code required to compile from source with Cython:

$ pip install pyswrd

💡 Example

PySWRD does not provide I/O, so the sequences to be used have to be loaded through another library, such as Biopython. PySWRD only requires the sequences to be available as Python strings:

targets = [
    'MAFSAEDVLKEYDRRRRMEALLLSLYYPNDRKLLDYKEWSPPRVQVECPK', 
    'MSIIGATRLQNDKSDTYSAGPCYAGGCSAFTPRGTCGKDWDLGEQTCASG', 
    'MASNTVSAQGGSNRPVRDFSNIQDVAQFLLFDPIWNEQPGSIVPWKMNRE', 
    'MYQAINPCPQSWYGSPQLEREIVCKMSGAPHYPNYYPVHPNALGGAWFDT', 
    'MARPLLGKTSSVRRRLESLSACSIFFFLRKFCQKMASLVFLNSPVYQMSN'
]
queries = [
    'MASNTVSAQGGSNRPVRDFSNIQDVAQFLLFDPIWNEQPG', 
    'MSFKVYDPIAELIATQFPTSNPDLQIINNDVLVVSPHKIT', 
    'MEQVPIKEMRLSDLRPNNKSIDTDLGGTKLVVIGKPGSGK'
]

Use the high-level search function, which wraps the internal classes in a single function to quickly run many-to-many searches in the event all your sequences are in memory. It expects the sequences as iterable of Python strings, and yields hits passing E-value and alignment thresholds:

import pyswrd
for hit in pyswrd.search(queries, targets):
    print(hit.query_index, hit.target_index, hit.score, hit.evalue)

Different parameters can be passed to pyswrd.search and are passed to the SWORD filter and Opal alignment. For instance, to run SWORD in fast mode instead of the default sensitive mode, and using the PAM70 matrix instead of BLOSUM62, use:

for hit in pyswrd.search(queries, targets, scorer_name="PAM70", score_threshold=0, kmer_length=5):
    print(hit.query_index, hit.target_index, hit.score, hit.evalue)

By default multithreading is supported, using one thread per CPU on the local machine as reported by os.cpu_count, but it can be changed with the threads argument:

for hit in pyswrd.search(queries, targets, threads=1):
    print(hit.query_index, hit.target_index, hit.score, hit.evalue)

You can also use the pyswrd.HeuristicFilter class directly if you wish to manage the data yourself, or if you want to use a different aligner.

⏱️ Benchmarks

The table below shows the time for running pyswrd.search using 196 proteins as queries (uniprot_sprot196.fasta) against a database of 12,701 proteins (uniprot_sprot12071.fasta) pre-loaded into memory:

threads=1threads=2threads=4threads=8threads=12
max_candidates=100.87s0.83s0.83s0.80s0.76s
max_candidates=500.98s0.91s0.98s0.97s1.04s
max_candidates=1001.24s1.33s1.44s1.63s1.67s
max_candidates=5001.86s1.83s1.95s2.09s2.15s
max_candidates=10002.87s2.64s2.83s2.82s2.90s
max_candidates=50009.33s8.11s7.59s6.60s6.06s
max_candidates=1500021.50s15.85s14.74s11.83s11.34s
max_candidates=3000023.44s16.13s14.61s12.47s11.08s
no filter (Opal)31.38s23.60s19.57s15.43s14.60s
BLAST+ (blastp)7.46s4.97s4.01s3.63s3.66s

The max_candidates parameter controls the strictness of the SWORD heuristic filter, and reduces the total number of alignments made by Opal, at the cost of a lowered sensivity (see SWORD Supplementary Figs. S1 and S2.).

SWORD uses 15,000 candidates in fast mode and 30,000 candidates in sensitive mode by default. This was benchmarked against the NCBI NR database, which contains more than 54M sequences; it is likely a smaller max_candidates value can be selected for smaller databases and/or databases with less redundant sequences without loss of sensitivity.

💭 Feedback

⚠️ Issue Tracker

Found a bug ? Have an enhancement request ? Head over to the GitHub issue tracker if you need to report or ask something. If you are filing in on a bug, please include as much information as you can about the issue, and try to recreate the same bug in a simple, easily reproducible situation.

🏗️ Contributing

Contributions are more than welcome! See CONTRIBUTING.md for more details.

📋 Changelog

This project adheres to Semantic Versioning and provides a changelog in the Keep a Changelog format.

⚖️ License

This library is provided under the GNU General Public License v3.0. SWORD was written by Robert Vaser and is distributed under the terms of the GPLv3 as well. See vendor/sword/LICENSE for more information. SWORD redistributes additional libraries under the terms of the MIT License.

This project is in no way not affiliated, sponsored, or otherwise endorsed by the SWORD authors. It was developed by Martin Larralde during his PhD project at the European Molecular Biology Laboratory in the Zeller team.

📚 References

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc