rotifer.db.neighbors.rneighbors#

class rotifer.db.neighbors.rneighbors(above=3, below=3, outformat='gi2operons', input_file='', gacc=[], asm=[], database='rotifer', port='5433', host='10.1.1.1', user='rotifer')[source]#

Bases: object

Query gene neighborhoods on a PostgreSQL feature database.

Call runRneighbors() to connect, submit the query accessions and print the results.

Parameters:
aboveint, default 3

Maximum number of neighbors upstream of each target.

belowint, default 3

Maximum number of neighbors downstream of each target.

outformatstr, default ‘gi2operons’

Output format. One of gi2operons (blocks), table (tab separated), missing (queries with no match) or query (queries with a match).

input_fileiterable of str

Protein accessions to search for.

gacclist of str, default []

Restrict the search to these genomic accessions.

asmlist of str, default []

Restrict the search to these assemblies.

databasestr, default ‘rotifer’

PostgreSQL database name.

portstr, default ‘5433’

PostgreSQL server port.

hoststr, default ‘10.1.1.1’

PostgreSQL server address.

userstr, default ‘rotifer’

PostgreSQL user name.

Attributes:
dbpsycopg2.extensions.connection

Database connection, opened by connect().

baseSql()[source]#

Build the statements that create the temporary tables.

Formats self.input_file into a values list, stored as self.ids.

Returns:
list of str

Statements creating the upid and uquery temporary tables and loading the query accessions into upid.

basicSql()[source]#

Build the unfiltered statement that populates uquery.

Returns:
list of str

A single statement selecting from ftable every feature whose protein accession appears in upid.

connect()[source]#

Open the connection to the PostgreSQL server.

The connection is stored in self.db and uses psycopg2.extras.DictCursor so rows can be addressed by column name.

Notes

On failure this method logs a critical message and calls exit(1) instead of raising.

fetch_contigs()[source]#

Fetch the nucleotide sequences carrying at least one query.

Returns:
psycopg2.extensions.cursor

Cursor over the distinct assembly, genomic accession, organism name and lineage of every match, ordered by lineage and organism.

fetch_intervals(cursor, ntid, above, below)[source]#

Select the neighborhood boundaries of every query on a contig.

Boundaries are computed by the first_upstream_neighbor and last_downstream_neighbor functions defined in the database.

Parameters:
cursorpsycopg2.extensions.cursor

Cursor used to run the query.

ntidstr

Genomic accession to search.

aboveint

Number of neighbors upstream of each target.

belowint

Number of neighbors downstream of each target.

Returns:
psycopg2.extensions.cursor

The same cursor, ready to iterate over rows carrying the gomin and gomax boundaries.

fetch_missing_queries(cursor)[source]#

Fetch the query accessions that matched nothing.

Parameters:
cursorpsycopg2.extensions.cursor

Cursor used to run the query.

Returns:
list of psycopg2.extras.DictRow

One row per accession absent from uquery.

fetch_queries(cursor)[source]#

Fetch the query accessions that matched a feature.

Parameters:
cursorpsycopg2.extensions.cursor

Cursor used to run the query.

Returns:
list of psycopg2.extras.DictRow

One row per accession found in uquery.

gi2operons(cursor, contig, interval)[source]#

Print one neighborhood in the gi2operons block format.

Column widths are computed from the block being printed, so each block is aligned independently. Target features are marked with --> in the first column.

Parameters:
cursorpsycopg2.extensions.cursor

Cursor used to run the queries.

contigpsycopg2.extras.DictRow

Row describing the nucleotide sequence, as returned by fetch_contigs().

intervalpsycopg2.extras.DictRow

Row describing the neighborhood boundaries, as returned by fetch_intervals().

lsqlAsm()[source]#

Build the statements that filter queries by assembly.

Returns:
list of str

Statements inserting self.asm into the uasm temporary table and populating uquery from ftable.

lsqlGacc()[source]#

Build the statements that filter queries by genomic accession.

Returns:
list of str

Statements inserting self.gacc into the ugacc temporary table and populating uquery from ftable.

missing(cursor)[source]#

Log the query accessions that matched nothing.

Parameters:
cursorpsycopg2.extensions.cursor

Cursor used to run the query.

post_ids()[source]#

Submit the query accessions to the database.

Creates the temporary tables, loads the query accessions into upid and populates uquery, choosing the assembly filter, the genomic accession filter or no filter at all according to self.asm and self.gacc. The number of accessions submitted and matched is logged at debug level.

Notes

The filter is chosen by two consecutive if statements rather than if/elif, so an assembly filter is discarded whenever self.gacc is empty. See docs/OPEN_QUESTIONS.md.

query(cursor)[source]#

Log the query accessions that matched a feature.

Parameters:
cursorpsycopg2.extensions.cursor

Cursor used to run the query.

Notes

This method raises before logging: it calls str(x[0], lost) and join(lost) where str(x[0]) and '\n'.join(lost) are meant. See docs/OPEN_QUESTIONS.md.

runRneighbors(stop=True)[source]#

Connect, run the query and print the neighborhoods.

Consecutive intervals closer than one feature apart are merged before printing, so overlapping neighborhoods appear as a single block.

Parameters:
stopbool, default True

Whether to call sys.exit(0) when finished.

Notes

The missing and query output formats are selected by two consecutive if statements and the else branch binds only to the second, so missing also runs the neighborhood branch. See docs/OPEN_QUESTIONS.md.

table(cursor, contig, interval, print_header)[source]#

Print one neighborhood in the tab separated format.

Parameters:
cursorpsycopg2.extensions.cursor

Cursor used to run the query.

contigpsycopg2.extras.DictRow

Row describing the nucleotide sequence.

intervalpsycopg2.extras.DictRow

Row describing the neighborhood boundaries.

print_headerint

Whether the column header still has to be printed.

Returns:
int

0 once the header has been printed, so that callers can pass the value to the next call and emit it only once.