rotifer.db.ncbi.mirror.GenomeCursor#

class rotifer.db.ncbi.mirror.GenomeCursor(progress=False, tries=1, batch_size=None, threads=2, path='/databases/genomes', *args, **kwargs)[source]#

Bases: GenomeCursor, SimpleParallelProcessCursor

Fetch genome sequences from a local mirror of the NCBI genomes repository.

Genomes are located by their assembly accession and parsed from the mirror’s GenBank flat files into Bio.SeqRecord objects.

Parameters:
progressbool, default False

Whether to print a progress bar.

triesint, default 1

Accepted for interface compatibility; local reads are attempted only once.

batch_sizeint, optional

Number of accessions per batch.

threadsint, optional

Number of processes used for parallel reads. Defaults to half of the CPU count.

pathstr, optional

Path to a mirror of the genomes section of the NCBI FTP site. Contents are expected to be the same as, or a subset of, the genomes directory.

See also

rotifer.db.ncbi.ftp.GenomeCursor

the same interface over FTP

Examples

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

Find the path of a genome in the local NCBI mirror.

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 derived from its ftp_path column instead of being searched in the mirror tree.

Returns:
tuple

The directory and file name of the genome’s GBFF file. The file name is None when no GBFF file exists in the genome’s directory.

Raises:
FileNotFoundError

If the mirror has no directory for the accession.

IOError

If the genome’s directory cannot be read.

Examples

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

Fetch and parse a genome’s assembly report from the mirror.

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 or its report is not found, contigs is an empty list.

Examples

>>> from rotifer.db.ncbi import mirror
>>> gc = mirror.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 local mirror.

Compressed files are uncompressed on the fly.

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 derived from its ftp_path column instead of being searched in the mirror tree.

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.

Examples

Open and parse a genome:

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