Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Metagenomics
metagenedb
Commits
ead2f182
Commit
ead2f182
authored
Nov 04, 2019
by
Kenzo-Hugo Hillion
♻
Browse files
Optimise to not have to build hierarchy everytime
parent
0345bdf9
Pipeline
#17572
passed with stages
in 2 minutes and 32 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/metagenedb/api/catalog/views/taxonomy.py
View file @
ead2f182
...
...
@@ -15,6 +15,6 @@ class TaxonomyViewSet(BulkViewSet):
def
retrieve
(
self
,
request
,
*
args
,
**
kwargs
):
instance
=
self
.
get_object
()
instance
.
build_
parental_hierarchy
()
hierarchy
=
instance
.
parental_hierarchy
# noqa
serializer
=
self
.
get_serializer
(
instance
)
return
Response
(
serializer
.
data
)
backend/metagenedb/apps/catalog/models/taxonomy.py
View file @
ead2f182
...
...
@@ -97,11 +97,33 @@ class Taxonomy(models.Model):
def
__str__
(
self
):
return
f
"
{
self
.
name
}
"
def
build_parental_hierarchy
(
self
):
@
property
def
parental_hierarchy
(
self
):
if
self
.
kingdom
is
None
and
self
.
superkingdom
is
None
:
return
self
.
_build_parental_hierarchy
()
return
self
.
_dict_parental_hierarchy
()
def
_dict_parental_hierarchy
(
self
):
"""
Return parental hierarchy from
"""
ranks
=
[
"superkingdom"
,
"kingdom"
,
"phylum"
,
"class_rank"
,
"order"
,
"family"
,
"genus"
,
"species"
]
hierarchy
=
{}
for
rank
in
ranks
:
if
getattr
(
self
,
rank
,
None
)
is
not
None
:
hierarchy
[
rank
]
=
getattr
(
self
,
rank
)
return
hierarchy
def
_build_parental_hierarchy
(
self
):
"""
Build and save parental hierarchy for an entry
"""
hierarchy
=
{}
if
self
.
name
!=
'root'
and
self
.
parent
is
not
None
:
hierarchy
[
self
.
rank
]
=
self
hierarchy
=
{
**
hierarchy
,
**
self
.
parent
.
build_
parental_hierarchy
()
}
hierarchy
=
{
**
hierarchy
,
**
self
.
parent
.
parental_hierarchy
}
for
level
,
value
in
hierarchy
.
items
():
setattr
(
self
,
level
,
value
)
self
.
save
()
...
...
backend/metagenedb/apps/catalog/models/test_taxonomy.py
View file @
ead2f182
...
...
@@ -33,6 +33,9 @@ class TestBuildHierarchy(APITestCase):
'kingdom'
:
self
.
kingdom
}
self
.
assertNotEqual
(
getattr
(
self
.
phylum
,
'kingdom'
,
None
),
self
.
kingdom
)
test_dict
=
self
.
phylum
.
build_
parental_hierarchy
()
test_dict
=
self
.
phylum
.
parental_hierarchy
self
.
assertDictEqual
(
test_dict
,
expected_dict
)
self
.
assertEqual
(
getattr
(
self
.
phylum
,
'kingdom'
,
None
),
self
.
kingdom
)
# Now try a second time from saved information
test_dict
=
self
.
phylum
.
parental_hierarchy
self
.
assertDictEqual
(
test_dict
,
expected_dict
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment