[973924]: / qiita_db / ontology.py

Download this file

118 lines (97 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
r"""
Ontology and Controlled Vocabulary objects (:mod:`qiita_db.study`)
==================================================================
.. currentmodule:: qiita_db.ontology
This module provides the encapsulation of an ontology. The resulting object can
be used to interrogate the terms in an ontology.
Classes
-------
.. autosummary::
:toctree: generated/
Ontology
"""
import qiita_db as qdb
class Ontology(qdb.base.QiitaObject):
"""Object to access ontologies and associated terms from the database
Attributes
----------
terms
shortname
"""
_table = 'ontology'
def __contains__(self, value):
with qdb.sql_connection.TRN:
sql = """SELECT EXISTS (
SELECT *
FROM qiita.term t
JOIN qiita.{0} o ON t.ontology_id = o.ontology_id
WHERE o.ontology_id = %s
AND term = %s)""".format(self._table)
qdb.sql_connection.TRN.add(sql, [self._id, value])
return qdb.sql_connection.TRN.execute_fetchlast()
@property
def terms(self):
with qdb.sql_connection.TRN:
sql = """SELECT term
FROM qiita.term
WHERE ontology_id = %s AND user_defined = false"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchflatten()
@property
def user_defined_terms(self):
with qdb.sql_connection.TRN:
sql = """SELECT term
FROM qiita.term
WHERE ontology_id = %s AND user_defined = true"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchflatten()
@property
def shortname(self):
return qdb.util.convert_from_id(self.id, 'ontology')
def add_user_defined_term(self, term):
"""Add a user defined term to the ontology
Parameters
----------
term : str
New user defined term to add into a given ontology
"""
# we don't need to add an existing term
terms = self.user_defined_terms + self.terms
if term not in terms:
sql = """INSERT INTO qiita.term
(ontology_id, term, user_defined)
VALUES (%s, %s, true);"""
qdb.sql_connection.perform_as_transaction(sql, [self.id, term])
def term_type(self, term):
"""Get the type of a given ontology term
Parameters
----------
term : str
String for which the method will check the type
Returns
-------
str
The term type: 'ontology' if the term is part of the ontology,
'user_defined' if the term is part of the ontology and was
user-defined and 'not_ontology' if the term is not part of the
ontology.
"""
with qdb.sql_connection.TRN:
sql = """SELECT user_defined FROM
qiita.term
WHERE term = %s AND ontology_id = %s"""
qdb.sql_connection.TRN.add(sql, [term, self.id])
result = qdb.sql_connection.TRN.execute_fetchindex()
if not result:
return 'not_ontology'
elif result[0][0]:
return 'user_defined'
elif not result[0][0]:
return 'ontology'