From bf4feb29b03aa05257b613e120cf0152dab78b6c Mon Sep 17 00:00:00 2001
From: Kenzo-Hugo Hillion <kenzo-hugo.hillion1@pasteur.fr>
Date: Mon, 10 Aug 2020 16:39:19 +0200
Subject: [PATCH] use KEGGAPI instead of TOGOWS from dabeplech to retrieve KEGG
 details

---
 .../common/utils/external_api/test_togows.py  |  6 +-
 .../common/utils/external_api/togows.py       |  8 +-
 frontend/src/components/keggcard/keggcard.js  | 89 ++++++++++---------
 3 files changed, 55 insertions(+), 48 deletions(-)

diff --git a/backend/metagenedb/common/utils/external_api/test_togows.py b/backend/metagenedb/common/utils/external_api/test_togows.py
index 1752ead..88ad41c 100644
--- a/backend/metagenedb/common/utils/external_api/test_togows.py
+++ b/backend/metagenedb/common/utils/external_api/test_togows.py
@@ -14,11 +14,11 @@ class TestGetFunctionExternalInfo(TestCase):
             external_info_retriever.get_details()
 
     def test_get_details_kegg(self):
-        with mock.patch('metagenedb.common.utils.external_api.togows.TogoWSEntryAPI') as MockTogoWSEntryAPI:
-            MockTogoWSEntryAPI.return_value.get.return_value = [{"info": "some_info"}]
+        with mock.patch('metagenedb.common.utils.external_api.togows.KEGGAPI') as MockKEGGAPI:
+            MockKEGGAPI.return_value.get.return_value.dict.return_value = {"info": "some_info"}
             test_url = "http://test.com/"
             test_id = "test_kegg_id"
-            MockTogoWSEntryAPI.return_value.url = test_url
+            MockKEGGAPI.return_value.url = test_url
             expected_dict = {
                 'info': 'some_info',
                 settings.API_KEY_ADDITIONAL_INFO: {
diff --git a/backend/metagenedb/common/utils/external_api/togows.py b/backend/metagenedb/common/utils/external_api/togows.py
index 560b55c..14ff0a7 100644
--- a/backend/metagenedb/common/utils/external_api/togows.py
+++ b/backend/metagenedb/common/utils/external_api/togows.py
@@ -2,7 +2,7 @@ import logging
 
 from django.conf import settings
 
-from dabeplech.togows import TogoWSEntryAPI
+from dabeplech import KEGGAPI
 
 
 logger = logging.getLogger(__name__)
@@ -21,8 +21,9 @@ class GetFunctionExternalInfo:
         """
         Get detailed information from KEGG orthology through Togows.
         """
-        kegg_api = TogoWSEntryAPI("kegg-orthology")
-        response = kegg_api.get(self.function_id)[0]
+        logger.info("Retrieving information from KEGG API")
+        kegg_api = KEGGAPI()
+        response = kegg_api.get(self.function_id).dict()
         response[settings.API_KEY_ADDITIONAL_INFO] = {
             'comment': f"Information retrieved from external source: {kegg_api.url}",
             'url': f"{kegg_api.url}{self.function_id}"
@@ -30,5 +31,4 @@ class GetFunctionExternalInfo:
         return response
 
     def get_details(self):
-        logger.info("Retrieving information from KEGG through togows")
         return getattr(self, f"_get_{self.source}", self._get_unknown_source)()
diff --git a/frontend/src/components/keggcard/keggcard.js b/frontend/src/components/keggcard/keggcard.js
index 5e783a1..fd92c8a 100644
--- a/frontend/src/components/keggcard/keggcard.js
+++ b/frontend/src/components/keggcard/keggcard.js
@@ -69,7 +69,6 @@ export default {
         this.buildPathways(response),
         this.buildDiseases(response),
         this.buildModules(response),
-        this.buildReferences(response),
       ];
     },
     buildPathways(response) {
@@ -78,17 +77,19 @@ export default {
         icon: 'fas fa-bezier-curve',
         content: [],
       };
-      Object.entries(response.data.pathways).forEach(([key, value]) => {
-        pathways.content.push(
-          {
-            id: key,
-            name: value,
-            url: `https://www.genome.jp/kegg-bin/show_pathway?${key}+${this.keggId}`,
-            url_label: "Open in KEGG",
-            fetch: true,
-          },
-        );
-      });
+      if(response.data.pathways) {
+        Object.entries(response.data.pathways).forEach(([key, value]) => {
+          pathways.content.push(
+            {
+              id: key,
+              name: value,
+              url: `https://www.genome.jp/kegg-bin/show_pathway?${key}+${this.keggId}`,
+              url_label: "Open in KEGG",
+              fetch: true,
+            },
+          );
+        });
+      }
       return pathways;
     },
     buildModules(response) {
@@ -97,16 +98,18 @@ export default {
         icon: 'fas fa-bezier-curve',
         content: [],
       };
-      Object.entries(response.data.modules).forEach(([key, value]) => {
-        modules.content.push(
-          {
-            id: key,
-            name: value,
-            url: `https://www.genome.jp/kegg-bin/show_module?${key}+${this.keggId}`,
-            url_label: "Open in KEGG"
-          },
-        );
-      });
+      if(response.data.modules) {
+        Object.entries(response.data.modules).forEach(([key, value]) => {
+          modules.content.push(
+            {
+              id: key,
+              name: value,
+              url: `https://www.genome.jp/kegg-bin/show_module?${key}+${this.keggId}`,
+              url_label: "Open in KEGG"
+            },
+          );
+        });
+      }
       return modules;
     },
     buildDiseases(response) {
@@ -115,15 +118,17 @@ export default {
         icon: 'fas fa-laptop-medical',
         content: [],
       };
-      Object.entries(response.data.diseases).forEach(([key, value]) => {
-        diseases.content.push(
-          {
-            id: key,
-            name: value,
-            url: `https://www.genome.jp/dbget-bin/www_bget?ds:${key}`,
-          },
-        );
-      });
+      if(response.data.diseases) {
+        Object.entries(response.data.diseases).forEach(([key, value]) => {
+          diseases.content.push(
+            {
+              id: key,
+              name: value,
+              url: `https://www.genome.jp/dbget-bin/www_bget?ds:${key}`,
+            },
+          );
+        });
+      }
       return diseases;
     },
     buildReferences(response) {
@@ -132,16 +137,18 @@ export default {
         icon: 'fas fa-book-open',
         content: [],
       };
-      for (let i = 0; i < response.data.references.length; i++) {
-        references.content.push(
-          {
-            id: response.data.references[i].title,
-            name: `${response.data.references[i].authors[0]} et al. ${response.data.references[i].journal}`,
-            url: `https://www.ncbi.nlm.nih.gov/pubmed/${response.data.references[i].pubmed}`,
-            url_label: "Open in Pubmed"
-          },
-        );
-      };
+      if (response.data.references) {
+        for (let i = 0; i < response.data.references.length; i++) {
+          references.content.push(
+            {
+              id: response.data.references[i].title,
+              name: `${response.data.references[i].authors[0]} et al. ${response.data.references[i].journal}`,
+              url: `https://www.ncbi.nlm.nih.gov/pubmed/${response.data.references[i].pubmed}`,
+              url_label: "Open in Pubmed"
+            },
+          );
+        };
+      }
       return references;
     },
   },
-- 
GitLab