def related(term, level=1, create_session=None): ''' for relname, to_term in related(term, create_session={backend.create_session}): .... Parameters ---------- term : str or unicode GO term level : int, optional Maximum recursion level. Use ``-1`` for infinite recursion create_session : callable Returns ------- iterator ''' if create_session is None: from waldo.backend import create_session session = create_session() seen = set() def recurse(term, level): if level == 0: return q = session\ .query(TermRelationship)\ .filter_by(from_term=term) for rel in q.all(): if rel.to_term not in seen: seen.add(rel.to_term) recurse(rel.to_term, level-1) recurse(term, level) for r in seen: yield r
def test_oop(): # This just checks that all methods exist &c ensemblgene = 'ENSMUSG00000064345' session = backend.create_session() for module in (waldo.uniprot, waldo.mgi, waldo.locate): name = module.retrieve.from_ensembl_gene_id(ensemblgene, session) entry = module.retrieve.retrieve_entry(name, session) if name is not None and entry is not None: if module.name == 'LOCATE': evidence = '<br />'.join( ['<a href="http://locate.imb.uq.edu.au/data_images/%s/%s"><img height="50" width="50" src="http://locate.imb.uq.edu.au/data_images/%s/%s" /></a>' % (img.filename[0:3], img.filename, img.filename[0:3], img.filename) for img in entry.images]), else: try: evidence = '<br />'.join((annot.evidence or 'None') for annot in entry.go_annotations), except: evidence = '-' d = { 'protein': entry.name, 'organism' : '; '.join(entry.organisms), 'celltype': '-', 'condition':'-', 'location': '; '.join(module.retrieve.retrieve_go_annotations(entry.internal_id)), 'references': '<br />'.join([paper.title for paper in entry.references]), 'evidence' : evidence, 'evidence_code' : '<br />'.join([go_annot.evidence_code for go_annot in entry.go_annotations]), 'source':'<a href="%s">%s</a>' % (module.retrieve.gen_url(entry.name),module.name), }
def vocabulary(go_id, session=None): ''' voc = vocabulary(go_id, session={new session}) Return the vocabulary to which the `go_id` belongs. Parameters ---------- go_id : str A GO id (e.g., GO:123456789) session : SQLAlchemy session object, optional Returns ------- voc: str Vocabulary (one of 'cellular_component', 'molecular_process', 'biological_function') None if not found. ''' if session is None: session = create_session() entry = session.query(models.Term) \ .filter(models.Term.id == go_id) \ .first() if entry: return entry.namespace
def term_to_id(go_term, session=None): ''' go_id = term_to_id(go_term, session={waldo.backend.create_session()}) For a GO term, returns the corresponding ID. If no ID is found, returns the original term. Parameters ---------- term : string A GO term (e.g. "mitochondrion") session : SQLAlchemy session, optional Returns ------- go_id : string A GO ID (e.g., GO:0005739) ''' if session is None: session = create_session() term = session.query(Term).filter(Term.name == go_term).first() if term is None: # if there's no mapping, return the original id return go_term return term.id
def map_to_goslim(go_id, set_name, session=None): ''' go_slim_term = map_to_goslim(go_id, set_name, session={backend.create_session()}) Map a GO term to a GO slim term in sub-vocabulary `set_name` Parameters ---------- go_id : string GO id set_name : string name of GO Slim to use session : sqlalchemy session, optional sqlalchemy session (default : `backend.create_session()`) Returns ------- go_slim_term : str or None GO Slim Term corresponding to input `go_id` ''' if set_name not in _known_slimsets: raise KeyError('waldo.goslim.map_to_goslim: Unknown GO Slim `%s` (known are %s)' % (set_name, _known_slimsets)) if session is None: from waldo.backend import create_session session = create_session() entry = (session.query(SlimTerm) .join(SlimMapping) .join(SlimSet) .filter(SlimSet.id == set_name) .filter(SlimMapping.full_id == go_id) .first()) if entry is None: return None return entry.name
def retrieve_orthologs(ensembl_id, session=None): ''' orthologs = retrieve_orthologs(ensembl_id, session={backend.create_session()}) Retrieve orthologs by Ensembl peptide id. Parameters ---------- ensembl_id : str Ensembl Peptide ID session : SQLAlchemy session, optional to use (default: call backend.create_session()) Returns ------- orthologs : list of str or None List of Ensembl IDs. If the protein is not found, None is returned. If the protein is found, but no orthologs are defined, an empty list is returned. ''' if session is None: session = backend.create_session() entry = session.query(NogEntry).filter(NogEntry.ensembl_peptide == ensembl_id).first() if entry is None: return None entries = session.query(NogEntry).filter(NogEntry.orthologous_group == entry.orthologous_group).all() entries = [e.ensembl_peptide for e in entries] entries = [e for e in entries if e != ensembl_id] return entries
def retrieve_go_annotations(id, session=None): ''' go_ids = retrieve_go_annotations(name, session={backend.create_session()}) Retrieve GO ids by LOCATE identifier. Parameters ---------- id : str LOCATE protein uid session : SQLAlchemy session object, optional session to use (default: call backend.create_session()) Returns ------- go_ids : list of str go terms (of the form "GO:00...") ''' if session is None: session = backend.create_session() entry = session.query(Entry).filter(Entry.id == id).first() locations = set([ann.go_id for ann in entry.go_annotations]) for predict in entry.predictions: locations.update(predict.go_id.split(';')) for reference in entry.references: for location in reference.locations: locations.update(location.go_id.split(';')) for annotation in entry.go_annotations: for location in annotation.locations: locations.update(location.go_id.split(';')) return list(locations)
def retrieve_entry(id, session=None): ''' entry = retrieve_entry(id, session={backend.create_session()}) Retrieve an Entry object based on its ID Parameters ---------- id : LOCATE database ID session : SQLAlchemy session to use (default: create a new one) Returns ------- entry : A models.Entry object ''' if session is None: session = backend.create_session() return session.query(Entry).filter(Entry.id == id).first()
def id_to_term(go_id, session=None): ''' term = id_to_term(go_id, session={waldo.backend.create_session()}) For a GO ID, returns the corresponding term. Parameters ---------- go_id : str A GO ID (e.g., GO:0005739) session : SQLAlchemy session object, optional Returns ------- term : str A GO term (e.g. "mitochondrion") ''' if session is None: session = create_session() term = session.query(Term).filter(Term.id == go_id).first() if term is None: # if there's no mapping, return the original id return go_id else: return term.name