Skip to main content

A simple library for executing BLAST searches with ncbi-blast+

Project description

simple_blast

This is a library that provides a (decreasingly) basic wrapper around ncbi-blast+. Currently, the library supports searches with blastn only, but I may expand the library to include wrappers for other BLAST executables if I need them.

Requirements

This library depends on Pandas for parsing BLAST output. The library has been tested with Pandas 1.5.3, but it likely works with other versions.

Of course, this library assumes that ncbi-blast+ is installed. The library has been tested with ncbi-blast 2.12.0+, and it likely works with newer versions of the software as well.

Basic usage

You can define a blastn search to be carried out using the BlastnSearch class. BlastnSearchobjects are constructed with two required arguments—the subject sequence and the query sequence files, in that order. For example, to set up a balstn search for sequences in seqs1.fasta against those in seqs2.fasta using output format 11 (BLAST Archive ASN.1), you could construct a BlastnSearch object like this:

from simple_blast import BlastnSearch

search = BlastnSearch("seqs2.fasta", "seqs1.fasta", 11)

The BLAST search is not carried out until you ask for the results by running the get_output() function.

results = search.get_output()

blastn can output binary data, so the get_output() function appropriately returns bytes.

Often, it's convenient to use output format 6, a tabular representation of the HSPs. For that purpose, you can use TabularBlastnSearch.

from simple_blast import TabularBlastnSearch

search = TabularBlastnSearch("seqs2.fasta", "seqs1.fasta", 11)

The hits property of the search returns a Pandas dataframe containing the HSPs identified in the BLAST search.

results = search.hits

The columns in the output may be configured by passing either the out_columns or additional_columns arguments when constructing the TabularBlastnSearch. The former argument overrides the set of output columns; the latter argument is added to the list of default output columns.

Sequences from memory

simple_blast can handle BLAST searches with sequences stored in memory (i.e., not in a file). It works with sequences stored as strings or in BioPython SeqRecord objects.

from Bio.SeqRecord import SeqRecord, Seq
from simple_blast import TabularBlastnSearch

# Define some data.
subjects = [
    SeqRecord(
	    Seq(
		"AAGGCGTACGGGCCTTTCGCTTCCGAAAACTTCCTCTTAGGTCGCTGTTACTGGATGTCGAGTCAGCACA"
		"TGGGAAACTCCACGCATCGGCGGGATTTCACAACGCCTAGAACACCGGTAATGCGAGTATCCGTATCGGT"
		"AACAAATATCTTTGGGATACTACAGGAATATCCGTAGGAGTTCGCCGCGATTAGGTGCCTCGATGATATG"
		"CAGCTGTCACTGGAGATAACACACTATGCAGCAGTAATGGATGTTATTGCTACTAAGGTTCCCTGTCACC"
	    ),
		id="My Sequence 1"
	),
    SeqRecord(
	    Seq(
		"TTCATTGGTGGGCTTTCTGGTTCACGCCCATCTCAATGTACATTTTCCGTGACGTGATGATAATCATAAC"
		"TCGTTGGTAGTAATAGGGTAAGGGAATTTGGCAGGTAGTCGGGGCAAGACTGCCGTTACAAGCTAATCAT"
		"CTGCCAACTAACTTTAGCCGTAATTGGCACTAACAGTTAACCTTCGCGCGTTTCTCAGTGTAGAGTGAGA"
		"CTATGTGATTACTTTCAGCGCCCAGCGGTGGTAGGTAGTAAAAAGTGGCCACCGAACCGAATGCT"
	    ),
		id="My Sequence 2"
	)
]
queries = [
    SeqRecord(
        Seq("TGGGAAACTCCACGCATCGGCGGGATTTCACAACGCCTAGAACACCGGTAATGCGAGTATCCGT"),
        id="Query 1"
    )
]

with TabularBlastnSearch.from_sequences(subjects, queries) as search:
    results = search.hits

or, using a list of strings:

from simple_blast import TabularBlastnSearch

# Define some data.
subjects = [
    (
        "AAGGCGTACGGGCCTTTCGCTTCCGAAAACTTCCTCTTAGGTCGCTGTTACTGGATGTCGAGTCAGCACA"
        "TGGGAAACTCCACGCATCGGCGGGATTTCACAACGCCTAGAACACCGGTAATGCGAGTATCCGTATCGGT"
        "AACAAATATCTTTGGGATACTACAGGAATATCCGTAGGAGTTCGCCGCGATTAGGTGCCTCGATGATATG"
        "CAGCTGTCACTGGAGATAACACACTATGCAGCAGTAATGGATGTTATTGCTACTAAGGTTCCCTGTCACC"
    ),
    (
        "TTCATTGGTGGGCTTTCTGGTTCACGCCCATCTCAATGTACATTTTCCGTGACGTGATGATAATCATAAC"
        "TCGTTGGTAGTAATAGGGTAAGGGAATTTGGCAGGTAGTCGGGGCAAGACTGCCGTTACAAGCTAATCAT"
        "CTGCCAACTAACTTTAGCCGTAATTGGCACTAACAGTTAACCTTCGCGCGTTTCTCAGTGTAGAGTGAGA"
        "CTATGTGATTACTTTCAGCGCCCAGCGGTGGTAGGTAGTAAAAAGTGGCCACCGAACCGAATGCT"
    )
]
queries = ["TGGGAAACTCCACGCATCGGCGGGATTTCACAACGCCTAGAACACCGGTAATGCGAGTATCCGT"]

with TabularBlastnSearch.from_sequences(subjects, queries) as search:
    results = search.hits

When using a list of strings, sequences are automatically named seq_i, where i is the position of the sequence in the list.

You can use SeqRecords together with lists of strings, and you can also use in-memory sequences together with files by providing the subject or query keyword arguments to from_sequences.

BlastnSearch.from_sequences(["CATGAACTA"], query="seqs1.fasta")

Since using a context manager is slightly cumbersome, you can also use the blastn_from_sequences convenience function to get the hits for a search.

from simple_blast import blastn_from_sequences

# Define some data.
subjects = [
    (
        "AAGGCGTACGGGCCTTTCGCTTCCGAAAACTTCCTCTTAGGTCGCTGTTACTGGATGTCGAGTCAGCACA"
        "TGGGAAACTCCACGCATCGGCGGGATTTCACAACGCCTAGAACACCGGTAATGCGAGTATCCGTATCGGT"
        "AACAAATATCTTTGGGATACTACAGGAATATCCGTAGGAGTTCGCCGCGATTAGGTGCCTCGATGATATG"
        "CAGCTGTCACTGGAGATAACACACTATGCAGCAGTAATGGATGTTATTGCTACTAAGGTTCCCTGTCACC"
    ),
    (
        "TTCATTGGTGGGCTTTCTGGTTCACGCCCATCTCAATGTACATTTTCCGTGACGTGATGATAATCATAAC"
        "TCGTTGGTAGTAATAGGGTAAGGGAATTTGGCAGGTAGTCGGGGCAAGACTGCCGTTACAAGCTAATCAT"
        "CTGCCAACTAACTTTAGCCGTAATTGGCACTAACAGTTAACCTTCGCGCGTTTCTCAGTGTAGAGTGAGA"
        "CTATGTGATTACTTTCAGCGCCCAGCGGTGGTAGGTAGTAAAAAGTGGCCACCGAACCGAATGCT"
    )
]
queries = ["TGGGAAACTCCACGCATCGGCGGGATTTCACAACGCCTAGAACACCGGTAATGCGAGTATCCGT"]

results = blastn_from_sequences(subjects, queries)

Note: Searching from in-memory sequences is implemented using Unix FIFOs, so this feature currently will not work on Windows.

DB caches

When the same sequence file is used as a subject in multiple searches, it can be efficient to build a BLAST database up front. The BlastDBCache class can be used to handle this mostly automatically. To make a BlastDBCache, you need to specify the location of the on the file system.

from simple_blast import BlastDBCache

cache = BlastDBCache("cache_dir")

To add a file to the cache, use the makedb method.

cache.makedb("seqs2.fasta")

When constructing a BlastnSearch object, give it the BlastDBCache as the db_cache parameter to make the BlastnSearch object use the cache for searches.

search = BlastnSearch("seqs2.fasta", "seqs1.fasta", db_cache=cache)

Now search will use the database we created for seqs2.fasta.

Format conversions

It's sometimes useful to convert between different BLAST output formats. ncbi-blast+ comes with a utility, blast_formatter, that can convert output in the "Blast4 Archive" format (ASN.1, output format 11) to any other BLAST format.

Using blast_formatter with simple_blast.convert

You can use blast_formatter directly with the simple_blast.convert module. For example,

from simple_blast.convert import blast_format_file

# Convert to output format 11.
blast_format_file(12, "my_blast_results.asn1", "my_blast_results.json")

If you don't specify the output file, you can get the output as bytes.

seqalign_bytes = blast_format_file(12, "my_blast_results.asn1")

You can also use the similar blast_format_bytes to provide bytes as input.

Using MultiformatBlastnSearch

You can create a search with output format 11 using the MultiformatBlastnSearch class.

from simple_blast.multiformat import MultiformatBlastnSearch

search = MultiformatBlastnSearch("seqs2.fasta", "seqs1.fasta")

You can convert the output to another format using the to method.

seqalign_bytes = search.to(12)

For output formats with an associated subclass of BlastnSearch, you can also convert directly to that subclass with to_search..

tabular_search = search.to_search(6)
results = tabular_search.hits # A Pandas DataFrame

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

simple_blast-0.5.0.tar.gz (82.8 kB view details)

Uploaded Source

Built Distribution

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

simple_blast-0.5.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file simple_blast-0.5.0.tar.gz.

File metadata

  • Download URL: simple_blast-0.5.0.tar.gz
  • Upload date:
  • Size: 82.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for simple_blast-0.5.0.tar.gz
Algorithm Hash digest
SHA256 fea21d29cb662133773ba009503d5076c2e7c250a20073c908446b75279da9ac
MD5 19378b2a7c020b57333527247fe22ceb
BLAKE2b-256 69684b22343ac1e69969b6376af65c53d8923e7e5c0d3b40bf72ae89865e54be

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_blast-0.5.0.tar.gz:

Publisher: package.yml on actapia/simple_blast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simple_blast-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: simple_blast-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for simple_blast-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44db6c2f9660e1fbf179b6f3466f75e2f131db5d5821c8ea256a60f218842a90
MD5 af4f810173220ddfa90fd64e580a29b1
BLAKE2b-256 240e8d4e75627ee7704b8b482f85931a75c1dc8b3d85427e5685823b1972ebf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_blast-0.5.0-py3-none-any.whl:

Publisher: package.yml on actapia/simple_blast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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