Package: pyswrd
🐍🗡️ PySWRDCythonbindings and Python interface toSWORD(Smith Waterman On Reduced Database), a method for fast database search.🗺️ OverviewSearching 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-acceleratedOpal[4]library.PySWRD is aPythonmodule that provides bindings to
the heuristic filter part ofSWORDusingCython. 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 aPyOpaldatabase,
usingOpalfor 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.🔧 InstallingPySWRD is available for all modern Python versions (3.6+).It can be installed directly fromPyPI,
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:$pipinstallpyswrd💡 ExamplePySWRD does not provide I/O, so the sequences to be used have to be loaded through
another library, such asBiopython. PySWRD only requires
the sequences to be available as Python strings:targets=['MAFSAEDVLKEYDRRRRMEALLLSLYYPNDRKLLDYKEWSPPRVQVECPK','MSIIGATRLQNDKSDTYSAGPCYAGGCSAFTPRGTCGKDWDLGEQTCASG','MASNTVSAQGGSNRPVRDFSNIQDVAQFLLFDPIWNEQPGSIVPWKMNRE','MYQAINPCPQSWYGSPQLEREIVCKMSGAPHYPNYYPVHPNALGGAWFDT','MARPLLGKTSSVRRRLESLSACSIFFFLRKFCQKMASLVFLNSPVYQMSN']queries=['MASNTVSAQGGSNRPVRDFSNIQDVAQFLLFDPIWNEQPG','MSFKVYDPIAELIATQFPTSNPDLQIINNDVLVVSPHKIT','MEQVPIKEMRLSDLRPNNKSIDTDLGGTKLVVIGKPGSGK']Use the high-levelsearchfunction, 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:importpyswrdforhitinpyswrd.search(queries,targets):print(hit.query_index,hit.target_index,hit.score,hit.evalue)Different parameters can be passed topyswrd.searchand are passed to the
SWORD filter and Opal alignment. For instance, to run SWORD infastmode
instead of the defaultsensitivemode, and using the PAM70 matrix instead
of BLOSUM62, use:forhitinpyswrd.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 byos.cpu_count, but it can be changed with thethreadsargument:forhitinpyswrd.search(queries,targets,threads=1):print(hit.query_index,hit.target_index,hit.score,hit.evalue)You can also use thepyswrd.HeuristicFilterclass directly if you wish to
manage the data yourself, or if you want to use a different aligner.⏱️ BenchmarksThe table below shows the time for runningpyswrd.searchusing 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=12max_candidates=100.87s0.83s0.83s0.80s0.76smax_candidates=500.98s0.91s0.98s0.97s1.04smax_candidates=1001.24s1.33s1.44s1.63s1.67smax_candidates=5001.86s1.83s1.95s2.09s2.15smax_candidates=10002.87s2.64s2.83s2.82s2.90smax_candidates=50009.33s8.11s7.59s6.60s6.06smax_candidates=1500021.50s15.85s14.74s11.83s11.34smax_candidates=3000023.44s16.13s14.61s12.47s11.08sno filter (Opal)31.38s23.60s19.57s15.43s14.60sThemax_candidatesparameter 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 infastmode and 30,000 candidates insensitivemode by default.
This was benchmarked against theNCBI NRdatabase, which contains more than 54M sequences; it is likely a smallermax_candidatesvalue can
be selected for smaller databases and/or databases with less redundant sequences without loss of sensitivity.💭 Feedback⚠️ Issue TrackerFound a bug ? Have an enhancement request ? Head over to theGitHub issue trackerif 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.🏗️ ContributingContributions are more than welcome! SeeCONTRIBUTING.mdfor more details.📋 ChangelogThis project adheres toSemantic Versioningand provides achangelogin theKeep a Changelogformat.⚖️ LicenseThis library is provided under theGNU General Public License v3.0.
SWORD was written byRobert Vaserand is distributed under the terms of the
GPLv3 as well. Seevendor/sword/LICENSEfor more information. SWORD redistributes additional
libraries under the terms of theMIT License.This project is in no way not affiliated, sponsored, or otherwise endorsed
by theSWORD authors. It was developed
byMartin Larraldeduring his PhD project
at theEuropean Molecular Biology Laboratoryin
theZeller team.📚 References[1]Stephen F. Altschul, Warren Gish, Webb Miller, Eugene W. Myers, David J. Lipman. Basic local alignment search tool. J Mol Biol. 1990 Oct 5;215(3):403-10.doi:10.1016/S0022-2836(05)80360-2.PMID:2231712.[2]Robert Vaser, Dario Pavlović, Mile Šikić. SWORD—a highly efficient protein database search. Bioinformatics, Volume 32, Issue 17, September 2016, Pages i680–i684,doi:10.1093/bioinformatics/btw445.[3]David J. Lipman, William R. Pearson. Rapid and sensitive protein similarity searches. Science. 1985 Mar 22;227(4693):1435-41.doi:10.1126/science.2983426.PMID:2983426.[4]Korpar Matija, Martin Šošić, Dino Blažeka, Mile Šikić. SW#db: ‘GPU-Accelerated Exact Sequence Similarity Database Search’. PLoS One. 2015 Dec 31;10(12):e0145857.doi:10.1371/journal.pone.0145857.PMID:26719890.PMC4699916.