mgkit.db.mongo module

New in version 0.2.1.

This module contains functions and classes to use for a DB like representation of annotations using the pymongo package, a driver to MongoDB.

In a MongoDB document, exported from an annotation, using the mgkit.io.gff.Annotation.to_mongodb() method, the keys that are defined are:

seq_id, source, feat_type, start, end, score, strand,
phase, gene_id, taxon_id, bitscore, exp_nonsyn, exp_syn,
length, dbq, coverage, map

These are defined because they have values that are not strings (defined as properties in mgkit.io.gff.Annotation. The rest of the attributes defined are kept as well, but no ckeck for the data type is made.

Note

lineage is added as a key, whose values are taxon_id, if a function has been passed to mgkit.io.gff.Annotation.to_mongodb()

The exception is the map key in the document. It store both the EC mappings (EC attribute in the GFF), as well as all mappings whose attribute starts with map_. The former is usually accessed from mgkit.io.gff.Annotation.get_ec() while the latter from mgkit.io.gff.Annotation.get_mapping() or mgkit.io.gff.Annotation.get_mappings().

These 3 methods return a list and this list is used in the MongoDB document. The MongoDB document will contain a map key where the values are the type of mappings, and the values the list of IDs the annoation maps to.

Example for the map dictionary

Type

GFF

Annotation

MongoDB Document

MongoDB Query

EC

EC

get_ec

ec

map.ec

KO

map_KO

get_mapping(‘ko’)

ko

map.ko

eggNOG

map_EGGNOG

get_mapping(‘eggnog’)

eggnog

map.eggnog

class mgkit.db.mongo.GFFDB(db, collection, uri=None, timeout=5)[source]

Bases: object

Changed in version 0.3.4: added timeout parameter

Wrapper to a MongoDB connection/db. It is used to automate the convertion of MongoDB records into mgkit.io.gff.Annotation instances.

__getitem__(uid)[source]

New in version 0.3.1.

Retrieves an annotation from the DB by its uid

__iter__()[source]

New in version 0.3.1.

Iterates over all annotations

conn = None
convert_record(record)[source]

Changed in version 0.3.1: removes lineage from the attributes

Converts the record (a dictionary instance) to an Annotation

cursor(query=None)[source]

Returns a cursor for the query

db = None
find_annotation(query=None)[source]

Iterate over a cursor created using query and yields each record after converting it to a mgkit.io.gff.Annotation instance, using mgkit.db.mongo.GFFDB.convert_record().

insert_many(annotations)[source]

New in version 0.3.4.

Inserts annotations into the DB

Warning

The object must be a mgkit.io.gff.Annotation

insert_one(annotation)[source]

New in version 0.3.4.

Inserts an annotation into the DB

Raises

TypeError – if the passed object is not an annotation

items()[source]

New in version 0.3.1.

Iterates over all the annotations in the db/collection, yielding a tuple (annotation.uid, annotation)

iteritems()[source]

New in version 0.3.1.

Alias for GFFDB.items()

itervalues()[source]

New in version 0.3.1.

Alias for GFFDB.values()

keys()[source]

New in version 0.3.1.

Iterates over all the uid in the db/collection

values()[source]

New in version 0.3.1.

Iterates over all the annotations in the db/collection