rotifer.db.ncbi.ftp.GenomeCursor#

class rotifer.db.ncbi.ftp.GenomeCursor(progress=True, tries=3, batch_size=4, threads=15, timeout=10, cache='/tmp', *args, **kwargs)[source]#

Bases: GenomeCursor, SimpleParallelProcessCursor

Fetch genome sequences from the NCBI FTP site.

Genomes are located by their assembly accession, downloaded as GenBank flat files, verified against the published MD5 checksums and parsed into Bio.SeqRecord objects.

Parameters:
progressbool, default True

Whether to print a progress bar.

triesint, default 3

Number of attempts to download data.

batch_sizeint, default 4

Number of accessions per batch. The default may be changed by the module configuration.

threadsint, default 15

Number of processes used for parallel downloads.

timeoutint, default 10

Maximum time, in seconds, to wait for a server connection.

cachestr, optional

Directory for temporary files. Defaults to the rotifer cache directory.

See also

rotifer.db.ncbi.GenomeCursor

delegator that also checks local mirrors

Examples

Load a sample of genomes:

>>> from rotifer.db.ncbi import ftp
>>> g = ['GCA_018744545.1', 'GCA_901308185.1']
>>> gc = ftp.GenomeCursor()
>>> genomes = gc.fetchall(g)
genome_path(accession, assembly_reports=None)[source]#

Find the path of a genome at the NCBI FTP site.

When the accession matches several versions, the newest one is chosen.

Parameters:
accessionstr

Genome assembly accession.

assembly_reportspandas.DataFrame, optional

NCBI assembly summary table, as loaded by rotifer.db.ncbi.assemblies(). When given, the path is read from its ftp_path column instead of being searched on the server.

Returns:
tuple of str

The directory and file name of the genome’s GBFF file. Empty when the genome is not found.

Examples

>>> from rotifer.db.ncbi import ftp
>>> gc = ftp.GenomeCursor()
>>> path = gc.genome_path("GCA_900547725.1")
>>> print("/".join(path))
genome_report(accession)[source]#

Fetch and parse a genome’s assembly report.

Parameters:
accessionstr

Genome assembly accession.

Returns:
tuple

A pair (contigs, properties). contigs is a pandas.DataFrame listing the assembly’s sequences. properties is a pandas.DataFrame, indexed by property name, holding the assembly metadata, similar to a row of rotifer.db.ncbi.assemblies(). When the genome is not found, contigs is an empty list.

Examples

>>> from rotifer.db.ncbi import ftp
>>> gc = ftp.GenomeCursor()
>>> contigs, assembly = gc.genome_report("GCA_900547725.1")
open_genome(accession, assembly_reports=None)[source]#

Open the GBFF file of a genome from the NCBI FTP site.

The file’s MD5 checksum is downloaded first and the genome download is repeated until the checksum matches, at most tries times.

Parameters:
accessionstr

Genome assembly accession.

assembly_reportspandas.DataFrame, optional

NCBI assembly summary table, as loaded by rotifer.db.ncbi.assemblies(). When given, the genome path is read from its ftp_path column instead of being searched on the server.

Returns:
file-like or None

The open data stream, with the assembly accession in an assembly attribute, or None when the genome is not found.

Raises:
IOError

If the checksum file cannot be parsed or the GBFF file cannot be opened.

Examples

Open and parse a genome:

>>> from rotifer.db.ncbi import ftp
>>> from Bio import SeqIO
>>> gc = ftp.GenomeCursor()
>>> fh = gc.open_genome("GCA_900547725.1")
>>> s = [x for x in SeqIO.parse(fh, "genbank")]
>>> fh.close()