Skip to content
GitLab
Menu
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
e2013000
Commit
e2013000
authored
Dec 09, 2019
by
Kenzo-Hugo Hillion
♻
Browse files
Make specific routes for eggnog and kegg
parent
66d3f01e
Pipeline
#19621
passed with stages
in 2 minutes and 29 seconds
Changes
12
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/metagenedb/api/catalog/urls.py
View file @
e2013000
from
django.conf.urls
import
url
,
include
from
rest_framework.routers
import
DefaultRouter
,
DynamicRoute
,
Route
from
metagenedb.api.catalog
.views
import
FunctionViewSet
,
GeneViewSet
,
TaxonomyViewSet
from
metagenedb.api.catalog
import
views
class
CustomRouter
(
DefaultRouter
):
...
...
@@ -52,9 +52,11 @@ class CustomRouter(DefaultRouter):
api_router
=
CustomRouter
()
api_router
.
register
(
r
'functions'
,
FunctionViewSet
,
basename
=
'functions'
)
api_router
.
register
(
r
'genes'
,
GeneViewSet
,
basename
=
'genes'
)
api_router
.
register
(
r
'taxonomy'
,
TaxonomyViewSet
,
basename
=
'taxonomy'
)
api_router
.
register
(
r
'functions'
,
views
.
FunctionViewSet
,
basename
=
'functions'
)
api_router
.
register
(
r
'kegg-orthologies'
,
views
.
KeggOrthologyViewSet
,
basename
=
'kegg-orthologies'
)
api_router
.
register
(
r
'eggnogs'
,
views
.
EggNogViewSet
,
basename
=
'eggnogs'
)
api_router
.
register
(
r
'genes'
,
views
.
GeneViewSet
,
basename
=
'genes'
)
api_router
.
register
(
r
'taxonomy'
,
views
.
TaxonomyViewSet
,
basename
=
'taxonomy'
)
urlpatterns
=
[
...
...
backend/metagenedb/api/catalog/views/__init__.py
View file @
e2013000
from
.function
import
FunctionViewSet
# noqa
from
.function
import
EggNogViewSet
,
KeggOrthologyViewSet
,
FunctionViewSet
# noqa
from
.gene
import
GeneViewSet
# noqa
from
.taxonomy
import
TaxonomyViewSet
# noqa
backend/metagenedb/api/catalog/views/bulk_viewset.py
View file @
e2013000
...
...
@@ -78,11 +78,6 @@ class BulkViewSet(ModelViewSet):
update_serializer
=
self
.
_get_update_serializer
(
instances
,
data_to_update
)
create_serializer
=
self
.
_get_create_serializer
(
data_to_create
)
# if getattr(instance, '_prefetched_objects_cache', None):
# # If 'prefetch_related' has been applied to a queryset, we need to
# # forcibly invalidate the prefetch cache on the instance.
# instance._prefetched_objects_cache = {}
headers
=
self
.
get_success_headers
(
update_serializer
.
data
)
return
Response
(
self
.
_updated_payload
(
create_serializer
,
update_serializer
,
request
),
...
...
backend/metagenedb/api/catalog/views/function.py
View file @
e2013000
...
...
@@ -3,11 +3,12 @@ import logging
from
marshmallow.exceptions
import
ValidationError
from
rest_framework.response
import
Response
from
rest_framework.status
import
HTTP_422_UNPROCESSABLE_ENTITY
from
rest_framework.viewsets
import
ModelViewSet
from
metagenedb.api.catalog.filters
import
FunctionFilter
from
metagenedb.api.catalog.qparams_validators.function
import
FunctionQueryParams
from
metagenedb.apps.catalog.models
import
Function
from
metagenedb.apps.catalog.serializers
import
Function
Serializer
from
metagenedb.apps.catalog.models
import
EggNog
,
Function
,
KeggOrthology
from
metagenedb.apps.catalog.serializers
import
EggNogSerializer
,
FunctionSerializer
,
KeggOrthology
Serializer
from
metagenedb.common.utils.external_api.togows
import
GetFunctionExternalInfo
from
.bulk_viewset
import
BulkViewSet
...
...
@@ -21,11 +22,16 @@ class FunctionViewSet(BulkViewSet):
serializer_class
=
FunctionSerializer
lookup_field
=
'function_id'
filterset_class
=
FunctionFilter
class
KeggOrthologyViewSet
(
ModelViewSet
):
queryset
=
KeggOrthology
.
objects
.
all
()
serializer_class
=
KeggOrthologySerializer
lookup_field
=
'function_id'
query_params_parser
=
FunctionQueryParams
def
_get_external_info
(
self
,
db_data
):
detailed_info_retriever
=
GetFunctionExternalInfo
(
db_data
[
'function_id'
],
db_data
[
'source'
])
detailed_info_retriever
=
GetFunctionExternalInfo
(
db_data
[
'function_id'
],
'kegg'
)
try
:
detailed_data
=
detailed_info_retriever
.
get_details
()
except
NotImplementedError
as
not_implemented_error
:
...
...
@@ -36,7 +42,7 @@ class FunctionViewSet(BulkViewSet):
def
retrieve
(
self
,
request
,
*
args
,
**
kwargs
):
try
:
query_params
=
self
.
_get_qparams
(
request
.
query_params
)
query_params
=
self
.
query_params_parser
().
load
(
request
.
query_params
)
except
ValidationError
as
validation_error
:
error_message
=
validation_error
.
normalized_messages
()
error_message
.
update
({
...
...
@@ -49,3 +55,9 @@ class FunctionViewSet(BulkViewSet):
if
query_params
.
get
(
'detailed'
,
False
)
is
True
:
returned_data
=
self
.
_get_external_info
(
returned_data
)
return
Response
(
returned_data
)
class
EggNogViewSet
(
ModelViewSet
):
queryset
=
EggNog
.
objects
.
all
()
serializer_class
=
EggNogSerializer
lookup_field
=
'function_id'
backend/metagenedb/api/catalog/views/test_function.py
View file @
e2013000
...
...
@@ -2,16 +2,19 @@ from rest_framework.test import APITestCase
import
mock
from
metagenedb.apps.catalog.factory
import
FunctionFactory
from
metagenedb.common.utils.mocks.metagenedb
import
MetageneDBCatalogFunctionAPIMock
from
metagenedb.apps.catalog.factory
import
EggNogFactory
,
KeggOrthologyFactory
from
metagenedb.common.utils.mocks.metagenedb
import
(
MetageneDBCatalogFunctionAPIMock
,
MetageneDBCatalogKeggOrthologyAPIMock
)
class
Test
Function
ViewSet
(
APITestCase
):
class
Test
KeggOrthology
ViewSet
(
APITestCase
):
def
setUp
(
self
):
self
.
function_api
=
MetageneDBCatalogFunctionAPIMock
(
self
.
client
)
self
.
kegg_function
=
FunctionFactory
.
create
(
source
=
'kegg'
)
self
.
eggnog_function
=
FunctionFactory
.
create
(
source
=
'eggnog'
)
self
.
kegg_ortho_api
=
MetageneDBCatalogKeggOrthologyAPIMock
(
self
.
client
)
self
.
kegg_function
=
KeggOrthologyFactory
.
create
()
self
.
eggnog_function
=
EggNogFactory
.
create
()
def
test_retrieve
(
self
):
for
function
in
[
self
.
kegg_function
,
self
.
eggnog_function
]:
...
...
@@ -34,20 +37,5 @@ class TestFunctionViewSet(APITestCase):
}
with
mock
.
patch
(
class_to_mock
)
as
MockGetFunctionExternalInfo
:
MockGetFunctionExternalInfo
.
return_value
.
get_details
.
return_value
=
detailed_kegg
tested_dict
=
self
.
function
_api
.
get
(
self
.
kegg_function
.
function_id
,
params
=
query_params
)
tested_dict
=
self
.
kegg_ortho
_api
.
get
(
self
.
kegg_function
.
function_id
,
params
=
query_params
)
self
.
assertDictEqual
(
tested_dict
,
detailed_kegg
)
def
test_retrieve_detailed_unavailable
(
self
):
"""
eggnog is not available so it is a good example and should return the DB value.
"""
query_params
=
{
'detailed'
:
'true'
}
expected_function
=
{
'function_id'
:
self
.
eggnog_function
.
function_id
,
'name'
:
self
.
eggnog_function
.
name
,
'source'
:
self
.
eggnog_function
.
source
}
tested_dict
=
self
.
function_api
.
get
(
self
.
eggnog_function
.
function_id
,
params
=
query_params
)
self
.
assertDictEqual
(
tested_dict
,
expected_function
)
backend/metagenedb/apps/catalog/factory/__init__.py
View file @
e2013000
from
.function
import
Function
Factory
# noqa
from
.function
import
EggNogFactory
,
FunctionFactory
,
KeggOrthology
Factory
# noqa
from
.gene
import
GeneFactory
# noqa
from
.taxonomy
import
TaxonomyFactory
# noqa
backend/metagenedb/apps/catalog/factory/function.py
View file @
e2013000
...
...
@@ -10,9 +10,23 @@ faker = Factory.create()
SELECTED_SOURCE
=
[
i
[
0
]
for
i
in
models
.
Function
.
SOURCE_CHOICES
]
class
FunctionFactory
(
DjangoModelFactory
):
class
BaseFunctionFactory
(
DjangoModelFactory
):
function_id
=
FuzzyLowerText
(
prefix
=
'function-'
,
length
=
15
)
class
FunctionFactory
(
BaseFunctionFactory
):
class
Meta
:
model
=
models
.
Function
source
=
fuzzy
.
FuzzyChoice
(
SELECTED_SOURCE
)
function_id
=
FuzzyLowerText
(
prefix
=
'function-'
,
length
=
15
)
class
EggNogFactory
(
BaseFunctionFactory
):
class
Meta
:
model
=
models
.
EggNog
class
KeggOrthologyFactory
(
BaseFunctionFactory
):
class
Meta
:
model
=
models
.
KeggOrthology
backend/metagenedb/apps/catalog/serializers/__init__.py
View file @
e2013000
from
.function
import
Function
Serializer
# noqa
from
.function
import
EggNogSerializer
,
FunctionSerializer
,
KeggOrthology
Serializer
# noqa
from
.gene
import
GeneSerializer
# noqa
from
.taxonomy
import
TaxonomySerializer
# noqa
backend/metagenedb/apps/catalog/serializers/function.py
View file @
e2013000
from
rest_framework
import
serializers
from
metagenedb.apps.catalog.models
import
Function
from
metagenedb.apps.catalog.models
import
EggNog
,
Function
,
KeggOrthology
from
.bulk_list
import
BulkListSerializer
...
...
@@ -16,3 +16,32 @@ class FunctionSerializer(serializers.ModelSerializer):
model
=
Function
list_serializer_class
=
FunctionListSerializer
fields
=
(
'function_id'
,
'source'
,
'name'
)
class
EggNogListSerializer
(
BulkListSerializer
):
class
Meta
:
model
=
EggNog
class
EggNogSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
EggNog
list_serializer_class
=
EggNogListSerializer
fields
=
(
'function_id'
,
'name'
,
'functional_category'
)
class
KeggOrthologyListSerializer
(
BulkListSerializer
):
class
Meta
:
model
=
KeggOrthology
class
KeggOrthologySerializer
(
serializers
.
ModelSerializer
):
ec_number
=
serializers
.
CharField
(
required
=
False
)
class
Meta
:
model
=
KeggOrthology
list_serializer_class
=
KeggOrthologyListSerializer
fields
=
(
'function_id'
,
'name'
,
'long_name'
,
'ec_number'
)
backend/metagenedb/common/utils/mocks/metagenedb.py
View file @
e2013000
...
...
@@ -84,3 +84,11 @@ class MetageneDBCatalogTaxonomyAPIMock(MetageneDBAPIMock):
class
MetageneDBCatalogFunctionAPIMock
(
MetageneDBAPIMock
):
KEY_ID
=
'function_id'
REVERSE_PATH
=
'catalog:v1:functions'
class
MetageneDBCatalogKeggOrthologyAPIMock
(
MetageneDBCatalogFunctionAPIMock
):
REVERSE_PATH
=
'catalog:v1:kegg-orthologies'
class
MetageneDBCatalogEggNogAPIMock
(
MetageneDBCatalogFunctionAPIMock
):
REVERSE_PATH
=
'catalog:v1:eggnogs'
backend/scripts/populate_db/load_kegg_ko.py
View file @
e2013000
...
...
@@ -5,7 +5,7 @@ import requests
import
sys
import
time
from
bioapi
import
MetageneDBCatalog
Function
API
from
bioapi
import
MetageneDBCatalog
KeggOrthology
API
from
metagenedb.common.utils.chunks
import
generate_chunks
from
metagenedb.common.utils.parsers
import
KEGGLineParser
...
...
@@ -17,7 +17,7 @@ KEGG_KO_LIST_API = "http://rest.kegg.jp/list/ko"
class
ImportKEGGKO
(
object
):
METAGENEDB_FUNCTION_API
=
MetageneDBCatalog
Function
API
METAGENEDB_FUNCTION_API
=
MetageneDBCatalog
KeggOrthology
API
ORM_SOURCE_KEY
=
'source'
KEGG_SOURCE
=
'kegg'
...
...
frontend/src/components/KeggCard.vue
View file @
e2013000
...
...
@@ -150,7 +150,7 @@ export default {
this
.
request_done
=
true
;
return
;
}
axios
.
get
(
'
/api/catalog/v1/
function
s/
'
+
this
.
kegg_id
+
'
?detailed=true
'
,
{
axios
.
get
(
'
/api/catalog/v1/
kegg-orthologie
s/
'
+
this
.
kegg_id
+
'
?detailed=true
'
,
{
headers
:
{
Accept
:
'
application/json
'
,
},
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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