Passed
Push — devel ( f2610c...8651b9 )
by Paolo
06:38
created

zooma.helpers.call_zooma()   A

Complexity

Conditions 2

Size

Total Lines 27
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 27
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Mon May  7 15:49:04 2018
5
6
@author: Paolo Cozzi <[email protected]>
7
8
Functions adapted from Jun Fan misc.py and use_zooma.py python scripts
9
"""
10
11
import logging
12
13
from image_validation.use_ontology import use_zooma
14
15
from common.constants import CONFIDENCES
16
17
18
# Get an instance of a logger
19
logger = logging.getLogger(__name__)
20
21
22
def call_zooma(label, zooma_type):
23
    """
24
    Wrapper around use_zooma: call zooma or catch exception
25
26
    Parameters
27
    ----------
28
    label : str
29
        Zooma query temr.
30
    zooma_type : str
31
        Zooma query type (species, breed, ...).
32
33
    Returns
34
    -------
35
    result : dict
36
        The results of use_zooma or None
37
38
    """
39
40
    result = None
41
42
    try:
43
        result = use_zooma(label, zooma_type)
44
45
    except Exception as exc:
46
        logger.error("Error in calling zooma: %s" % str(exc))
47
48
    return result
49
50
51
def annotate_generic(model, zooma_type):
52
    """Annotate missing terms from a generic DictTable
53
54
    Args:
55
        model (:py:class:`uid.models.DictBase`): A DictBase istance
56
        zooma_type (str): the type of zooma annotation (country, species, ...)
57
    """
58
59
    logger.debug("Processing %s" % (model))
60
61
    result = call_zooma(model.label, zooma_type)
62
63
    # update object (if possible)
64
    if result:
65
        url = result['ontologyTerms']
66
        # https://stackoverflow.com/a/7253830
67
        term = url.rsplit('/', 1)[-1]
68
69
        # The ontology seems correct. Annotate!
70
        logger.info("Updating %s with %s" % (model, result))
71
        url = result['ontologyTerms']
72
73
        model.term = term
74
75
        # get an int object for such confidence
76
        confidence = CONFIDENCES.get_value(
77
            result["confidence"].lower())
78
79
        model.confidence = confidence
80
        model.save()
81
82
83
def annotate_country(country_obj):
84
    """Annotate country objects using Zooma"""
85
86
    annotate_generic(country_obj, "country")
87
88
89
def annotate_breed(breed_obj):
90
    """Annotate breed objects using Zooma"""
91
92
    logger.debug("Processing %s" % (breed_obj))
93
94
    result = call_zooma(breed_obj.supplied_breed, "breed")
95
96
    # update object (if possible)
97
    if result:
98
        url = result['ontologyTerms']
99
        # https://stackoverflow.com/a/7253830
100
        term = url.rsplit('/', 1)[-1]
101
102
        # The ontology seems correct. Annotate!
103
        logger.info("Updating %s with %s" % (breed_obj, result))
104
        url = result['ontologyTerms']
105
106
        # this is slight different from annotate_generic
107
        breed_obj.mapped_breed_term = term
108
        breed_obj.mapped_breed = result['text']
109
110
        # get an int object for such confidence
111
        confidence = CONFIDENCES.get_value(
112
            result["confidence"].lower())
113
114
        breed_obj.confidence = confidence
115
        breed_obj.save()
116
117
118
def annotate_specie(specie_obj):
119
    """Annotate specie objects using Zooma"""
120
121
    annotate_generic(specie_obj, "species")
122
123
124
def annotate_organismpart(uberon_obj):
125
    """Annotate organism part objects using Zooma"""
126
127
    annotate_generic(uberon_obj, "organism part")
128
129
130
def annotate_develstage(dictdevelstage_obj):
131
    """Annotate developmental stage objects using Zooma"""
132
133
    annotate_generic(dictdevelstage_obj, "developmental stage")
134
135
136
def annotate_physiostage(dictphysiostage_obj):
137
    """Annotate physiological stage objects using Zooma"""
138
139
    annotate_generic(dictphysiostage_obj, "physiological stage")
140