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:
objectQuery 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) orquery(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_fileinto a values list, stored asself.ids.- Returns:
- list of str
Statements creating the
upidanduquerytemporary tables and loading the query accessions intoupid.
- basicSql()[source]#
Build the unfiltered statement that populates
uquery.- Returns:
- list of str
A single statement selecting from
ftableevery feature whose protein accession appears inupid.
- connect()[source]#
Open the connection to the PostgreSQL server.
The connection is stored in
self.dband usespsycopg2.extras.DictCursorso 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_neighborandlast_downstream_neighborfunctions 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
gominandgomaxboundaries.
- 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
gi2operonsblock 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.asminto theuasmtemporary table and populatinguqueryfromftable.
- lsqlGacc()[source]#
Build the statements that filter queries by genomic accession.
- Returns:
- list of str
Statements inserting
self.gaccinto theugacctemporary table and populatinguqueryfromftable.
- 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
upidand populatesuquery, choosing the assembly filter, the genomic accession filter or no filter at all according toself.asmandself.gacc. The number of accessions submitted and matched is logged at debug level.Notes
The filter is chosen by two consecutive
ifstatements rather thanif/elif, so an assembly filter is discarded wheneverself.gaccis empty. Seedocs/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)andjoin(lost)wherestr(x[0])and'\n'.join(lost)are meant. Seedocs/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
missingandqueryoutput formats are selected by two consecutiveifstatements and theelsebranch binds only to the second, somissingalso runs the neighborhood branch. Seedocs/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.