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