OntologiesΒΆ
Biological ontologies such as the Sequence Ontology (SO) or the
Gene Ontology are read from OBO files into an Ontology: a directed
acyclic graph of OntologyTerm objects linked by is_a and part_of relationships.
from urllib.request import urlopen
from picea import Ontology
SO_URL = "https://raw.githubusercontent.com/The-Sequence-Ontology/SO-Ontologies/master/Ontology_Files/so.obo"
with urlopen(SO_URL) as response:
so = Ontology.from_obo(string=response.read().decode())
len(so)
2531
Terms are accessed by ID. OBO tags become attributes, with list values.
mrna = so["SO:0000234"]
mrna.name, mrna["def"]
(['mRNA'],
['"Messenger RNA is the intermediate molecule between DNA and protein. It includes UTR and coding sequences. It does not contain introns." [SO:ma]'])
parents and children are transitive: they contain all ancestors and all descendants of a term.
[(term.ID, term.name[0]) for term in mrna.parents]
[('SO:0000233', 'mature_transcript'),
('SO:0000673', 'transcript'),
('SO:0000831', 'gene_member_region'),
('SO:0001411', 'biological_region'),
('SO:0000001', 'region'),
('SO:0000110', 'sequence_feature')]
len(mrna.children)
95
Both return a new Ontology, so they can be filtered and grouped.
coding_rnas = mrna.children.filter(lambda term: "coding" in term.name[0])
sorted(term.name[0] for term in coding_rnas)
['coding_end',
'coding_start',
'distant_three_prime_recoding_signal',
'five_prime_recoding_site',
'flanking_three_prime_quadruplet_recoding_signal',
'recoding_pseudoknot',
'recoding_stimulatory_region',
'three_prime_recoding_site',
'three_prime_repeat_recoding_signal']