From 98f174d842a6f95ac84c30e38f2376cec8597b15 Mon Sep 17 00:00:00 2001
From: Sandra Derozier <sandra.derozier@inrae.fr>
Date: Fri, 5 Jul 2024 11:01:39 +0200
Subject: [PATCH 1/6] #13 - Search relations - Add DocId param

---
 __init__.py |  4 +++-
 database.py | 12 +++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/__init__.py b/__init__.py
index 4b0132c..1219802 100644
--- a/__init__.py
+++ b/__init__.py
@@ -481,6 +481,7 @@ class ApiSearchOBT(Resource):
 @api.route('/api/search/relations/', doc=False)
 @api.route('/api/search/relations', doc={"description": "This function returns a list of relations that match the constraints given as parameters. All parameters are optional but at least one must be provided (otherwise 400). Constraints are connected with the intersection (AND) operator."})
 @api.doc(params={'source': 'Source name (example: PubMed, CIRM-BIA, CIRM-Levures, DSMZ, GenBank)'})
+@api.doc(params={'docid': 'Document ID (example: 25860123)'})
 @api.doc(params={'taxid': 'NCBI identifier of taxa ancestor (left-hand argument)'})
 @api.doc(params={'qps': 'Qualified Presumption of Safety (QPS) status (example: yes, no, true, false)'})
 @api.doc(params={'obtid': 'OBT identifier of OntoBiotope concept ancestor (right-hand argument)'})
@@ -493,6 +494,7 @@ class ApiSearchRelations(Resource):
     })
     def get(self):
         source = request.args.getlist('source', None)
+        docid = request.args.get('docid', None)
         taxid = request.args.getlist('taxid', None)
         qps = request.args.get('qps', None)
         obtid = request.args.getlist('obtid', None)
@@ -500,7 +502,7 @@ class ApiSearchRelations(Resource):
         if source == None and taxid == None and qps == None and obtid == None and type == None:
             return {'error': 'At least one parameter required'}, 400
         else:
-            return jsonify(search_relations(app, conn, source, taxid, qps, obtid, type))
+            return jsonify(search_relations(app, conn, source, taxid, qps, obtid, type, docid))
 
 # ***** search/join-relations ***** #
 @api.route('/api/search/join-relations/', doc=False)
diff --git a/database.py b/database.py
index d409d6c..f7bb6ed 100644
--- a/database.py
+++ b/database.py
@@ -175,7 +175,7 @@ def search_ontobiotope(app, conn, root, type, name):
         return results
 
 # Search relations: /search/relations
-def search_relations(app, conn, source, taxonids, qps, ontobiotopeids, types):
+def search_relations(app, conn, source, taxonids, qps, ontobiotopeids, types, docid):
     conn = get_db(app)
     if conn != None:
         cursor = conn.cursor()
@@ -184,21 +184,21 @@ def search_relations(app, conn, source, taxonids, qps, ontobiotopeids, types):
         if taxonids == [] and ontobiotopeids != []:
             taxonid = None
             for ontobiotopeid in ontobiotopeids:
-                results += create_sql_relation(source, qps, types, taxonid, ontobiotopeid, cursor)
+                results += create_sql_relation(source, qps, types, taxonid, ontobiotopeid, docid, cursor)
         elif taxonids != [] and ontobiotopeids == []:
             ontobiotopeid = None
             for taxonid in taxonids:
-                results += create_sql_relation(source, qps, types, taxonid, ontobiotopeid, cursor)
+                results += create_sql_relation(source, qps, types, taxonid, ontobiotopeid, docid, cursor)
         else:
             for taxonid in taxonids:
                 for ontobiotopeid in ontobiotopeids:
-                    results += create_sql_relation(source, qps, types, taxonid, ontobiotopeid, cursor)
+                    results += create_sql_relation(source, qps, types, taxonid, ontobiotopeid, docid, cursor)
 
         cursor.close()
         return results
 
 # Create SQL query for Search relations
-def create_sql_relation(source, qps, types, taxonid, ontobiotopeid, cursor):
+def create_sql_relation(source, qps, types, taxonid, ontobiotopeid, docid, cursor):
     check = 'yes'
     if taxonid == None:
         taxroot = "null"
@@ -220,6 +220,8 @@ r.type, r.source, e.identifier, t.taxonid, e.name, t.name"
             query += "r.source = '" + ss + "' OR "
         query += ")"
         query = re.sub(" OR \)", ")", query)
+    if docid != None:
+        query += " AND id_source like '%"+ docid +"%'"
     if qps in ("yes", "true"):
         query += " AND t.qps = 'yes'"
     if types != []:
-- 
GitLab


From 0ca9efdb33719c203c8c2a19da4d2949ee504642 Mon Sep 17 00:00:00 2001
From: Sandra Derozier <sandra.derozier@inrae.fr>
Date: Fri, 5 Jul 2024 11:51:48 +0200
Subject: [PATCH 2/6] #14 - Case sensitivity

---
 database.py | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/database.py b/database.py
index f7bb6ed..f25451c 100644
--- a/database.py
+++ b/database.py
@@ -67,7 +67,7 @@ def get_taxon(app, conn, id):
         try:
             cursor.execute("SELECT taxonid, name, path, qps \
                             FROM taxon \
-                            WHERE taxonid = '" + id +"'")
+                            WHERE lower(taxonid) = '" + id.lower() +"'")
         except Exception as err:
             print_psycopg2_exception(err)
         results = dict()
@@ -89,7 +89,7 @@ def get_ontobiotope(app, conn, id):
         try:
             cursor.execute("SELECT identifier, name, path, type, synonym \
                             FROM element \
-                            WHERE identifier = '" + id +"'")
+                            WHERE lower(identifier) = '" + id.lower() +"'")
         except Exception as err:
             print_psycopg2_exception(err)
         results = dict()
@@ -134,9 +134,9 @@ def search_taxon(app, conn, root, qps, name):
     if qps in ("yes", "true"):
         query += "qps = 'yes' AND "
     if name != None:
-        query += "lower(name) ~ '" + name + "' AND "
+        query += "lower(name) ~ '" + name.lower() + "' AND "
     if root != None:
-        query += "(taxonid = '" + root + "' or path like '%/" + root + "/%')"
+        query += "(lower(taxonid) = '" + root.lower() + "' or lower(path) like '%/" + root.lower() + "/%')"
     query = re.sub('AND $', '', query)
     conn = get_db(app)
     if conn != None:
@@ -157,9 +157,9 @@ def search_ontobiotope(app, conn, root, type, name):
     if type != None:
         query += "type = '" + type + "' AND "
     if name != None:
-        query += "(lower(name) ~ '" + name + "' or lower(synonym)  ~ '" + name + "') AND "
+        query += "(lower(name) ~ '" + name.lower() + "' or lower(synonym)  ~ '" + name.lower() + "') AND "
     if root != None:
-        query += "(identifier = '" + root + "' or path like '%/" + root + "/%')"
+        query += "(lower(identifier) = '" + root.lower() + "' or path like '%/" + root.lower() + "/%')"
     query = re.sub('AND $', '', query)
     conn = get_db(app)
     if conn != None:
@@ -231,9 +231,9 @@ r.type, r.source, e.identifier, t.taxonid, e.name, t.name"
         query += ")"
         query = re.sub(' OR \)', ')', query)
     if taxonid != None:
-        query += " AND r.id_taxon in (SELECT id FROM taxon WHERE taxonid = '"+taxonid+"' OR path like '%/"+taxonid+"/%')"
+        query += " AND r.id_taxon in (SELECT id FROM taxon WHERE lower(taxonid) = '"+taxonid.lower()+"' OR path like '%/"+taxonid.lower()+"/%')"
     if ontobiotopeid != None:
-        query += " AND r.id_element in (SELECT id FROM element WHERE identifier = '"+ontobiotopeid+"' OR path like '%/"+ontobiotopeid+"/%')"
+        query += " AND r.id_element in (SELECT id FROM element WHERE lower(identifier) = '"+ontobiotopeid.lower()+"' OR path like '%/"+ontobiotopeid.lower()+"/%')"
     # Only if taxID and OBTID exist
     if check == 'yes':
         try:
@@ -300,10 +300,10 @@ def search_join_relations_by_taxon(app, conn, source, qps, lefttype, leftroots,
                 query += "AND lr.type = '" + lefttype + "' "
                 query += "AND rr.type = '" + righttype + "' "
                 query += "AND le.id != re.id "
-                query += "AND (le.path like '%/" + leftroot + "/%' OR le.identifier = '"+ leftroot +"') "
-                query += "AND (re.path like '%/" + rightroot + "/%' OR re.identifier = '"+ rightroot +"') "
+                query += "AND (lower(le.path) like '%/" + leftroot.lower() + "/%' OR lower(le.identifier) = '"+ leftroot.lower() +"') "
+                query += "AND (lower(re.path) like '%/" + rightroot.lower() + "/%' OR lower(re.identifier) = '"+ rightroot.lower() +"') "
                 if joinroot != None:
-                    query += "AND (je.path like '%/" + joinroot +"/%' OR je.taxonid = '"+ joinroot +"') "
+                    query += "AND (lower(je.path) like '%/" + joinroot.lower() +"/%' OR lower(je.taxonid) = '"+ joinroot.lower() +"') "
                 if qps in ("yes", "true"):
                     query += "AND je.qps = 'yes' "
                 if source != []:
@@ -352,10 +352,10 @@ def search_join_relations_by_taxon_aggregate(app, conn, source, qps, lefttype, l
                 query += "AND lr.type = '" + lefttype + "' "
                 query += "AND rr.type = '" + righttype + "' "
                 query += "AND le.id != re.id "
-                query += "AND (le.path like '%/" + leftroot + "/%' OR le.identifier = '"+ leftroot +"') "
-                query += "AND (re.path like '%/" + rightroot + "/%' OR re.identifier = '"+ rightroot +"') "
+                query += "AND (lower(le.path) like '%/" + leftroot.lower() + "/%' OR lower(le.identifier) = '"+ leftroot.lower() +"') "
+                query += "AND (lower(re.path) like '%/" + rightroot.lower() + "/%' OR lower(re.identifier) = '"+ rightroot.lower() +"') "
                 if joinroot != None:
-                    query += "AND (je.path like '%/" + joinroot +"/%' OR je.taxonid = '"+ joinroot +"') "
+                    query += "AND (lower(je.path) like '%/" + joinroot.lower() +"/%' OR lower(je.taxonid) = '"+ joinroot.lower() +"') "
                 if qps in ("yes", "true"):
                     query += "AND je.qps = 'yes' "
                 if source != []:
@@ -411,10 +411,10 @@ def search_join_relations_by_ontobiotope(app, conn, source, qps, lefttype, leftr
                 query += "AND lr.type = '" + jointype + "' "
                 query += "AND rr.type = '" + jointype + "' "
                 query += "AND le.id != re.id "
-                query += "AND (le.path like '%/" + leftroot + "/%' OR le.taxonid = '"+ leftroot +"') "
-                query += "AND (re.path like '%/" + rightroot + "/%' OR re.taxonid = '"+ rightroot +"') "
+                query += "AND (lower(le.path) like '%/" + leftroot.lower() + "/%' OR lower(le.taxonid) = '"+ leftroot.lower() +"') "
+                query += "AND (lower(re.path) like '%/" + rightroot.lower() + "/%' OR lower(re.taxonid) = '"+ rightroot.lower() +"') "
                 if joinroot != None:
-                    query += "AND (je.path like '%/" + joinroot +"/%' OR je.identifier = '"+ joinroot +"') "
+                    query += "AND (lower(je.path) like '%/" + joinroot.lower() +"/%' OR lower(je.identifier) = '"+ joinroot.lower() +"') "
                 if qps in ("yes", "true"):
                     query += "AND je.qps = 'yes' "
                 if source != []:
@@ -463,10 +463,10 @@ def search_join_relations_by_ontobiotope_aggregate(app, conn, source, qps, leftt
                 query += "AND lr.type = '" + jointype + "' "
                 query += "AND rr.type = '" + jointype + "' "
                 query += "AND le.id != re.id "
-                query += "AND (le.path like '%/" + leftroot + "/%' OR le.taxonid = '"+ leftroot +"') "
-                query += "AND (re.path like '%/" + rightroot + "/%' OR re.taxonid = '"+ rightroot +"') "
+                query += "AND (lower(le.path) like '%/" + leftroot.lower() + "/%' OR lower(le.taxonid) = '"+ leftroot.lower() +"') "
+                query += "AND (lower(re.path) like '%/" + rightroot.lower() + "/%' OR lower(re.taxonid) = '"+ rightroot.lower() +"') "
                 if joinroot != None:
-                    query += "AND (je.path like '%/" + joinroot +"/%' OR je.identifier = '"+ joinroot +"') "
+                    query += "AND (lower(je.path) like '%/" + joinroot.lower() +"/%' OR lower(je.identifier) = '"+ joinroot.lower() +"') "
                 if qps in ("yes", "true"):
                     query += " AND je.qps = 'yes'"
                 if source != []:
-- 
GitLab


From b1c404229b6b88517ee47107472831c6132dc4b7 Mon Sep 17 00:00:00 2001
From: Sandra Derozier <sandra.derozier@inrae.fr>
Date: Fri, 5 Jul 2024 14:22:34 +0200
Subject: [PATCH 3/6] #16 - Return several URLs

---
 __init__.py |  5 +++--
 database.py | 22 +++++++++++++++++-----
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/__init__.py b/__init__.py
index 1219802..dd182cc 100644
--- a/__init__.py
+++ b/__init__.py
@@ -314,7 +314,8 @@ taxon_model = api.model('Taxon', {
     "id": fields.String(default="ncbi:1423", description="Taxon identifier", example="ncbi:1423"),
     "name": fields.String(description="Canonical name of this taxon", example="Bacillus subtilis"),
     "path": fields.List(fields.String(description="Taxon lineage from root to self", example="ncbi:1/ncbi:131567/ncbi:2/ncbi:1783272/ncbi:1239/ncbi:91061/ncbi:1385/ncbi:186817/ncbi:1386/ncbi:653685/ncbi:1423")),
-    "qps": fields.String(description="Either this taxon is listed in QPS", enum=['yes', 'true', 'no', 'false'], example="yes")
+    "qps": fields.String(description="Either this taxon is listed in QPS", enum=['yes', 'true', 'no', 'false'], example="yes"),
+    "url": fields.String(description="URL", example="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=1423&rettype=xml")
 })
 obt_model = api.model('Obt', {
     "id": fields.String(description="OntoBiotope identifier", example="OBT:000427"),
@@ -324,7 +325,7 @@ obt_model = api.model('Obt', {
     "path": fields.List(fields.String(description="Path from root to this concept", example="OBT:000001/OBT:000013/OBT:000158/OBT:000427"))
 })
 doc_model = api.model('Doc', {
-    "url": fields.String(description="Document identifiers where relations were found", example="https://www.ncbi.nlm.nih.gov/pubmed/6229999")
+    "url": fields.List(fields.String(description="Document identifiers where relations were found", example="https://pubmed.ncbi.nlm.nih.gov/6229999"))
 })
 taxonid_model = api.model('TaxonID', {
     "id": fields.String(description="Taxon identifier", example="ncbi:1423")
diff --git a/database.py b/database.py
index f25451c..12b18c2 100644
--- a/database.py
+++ b/database.py
@@ -42,7 +42,8 @@ sources = {
             "CIRM-CFBP".lower() : "",
             "DSMZ".lower() : "https://bacdive.dsmz.de/strain/",
             "GenBank".lower() : "https://www.ncbi.nlm.nih.gov/nuccore/",
-            "PubMed".lower() : "https://pubmed.ncbi.nlm.nih.gov/"
+            # "PubMed".lower() : "https://pubmed.ncbi.nlm.nih.gov/"
+            "PubMed".lower() : ["https://pubmed.ncbi.nlm.nih.gov/", "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=PMID&rettype=xml"]
           }
 
 # Get version: /get/version
@@ -78,6 +79,10 @@ def get_taxon(app, conn, id):
             for path in row[2].split(","):
                 results['path'].append(path)
             results['qps'] = row[3]
+            if "ncbi" in row[0]:
+                results['url'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
+            else:
+                results['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
         cursor.close()
         return results
 
@@ -115,14 +120,21 @@ def get_doc(app, conn, source, id):
             cursor = conn.cursor()
             ss = check_source(source)
             try:
-                cursor.execute("SELECT id_source FROM relation \
-                                WHERE id_source like '%" + str(id) +"%' \
-                                AND source = '" + ss + "'")
+                query = "SELECT id_source FROM relation \
+                         WHERE id_source like '%" + str(id) +"%' \
+                         AND source = '" + ss + "'"
+                cursor.execute(query)
+                print(query)
             except Exception as err:
                 print_psycopg2_exception(err)
             results = dict()
             if cursor.fetchall() != []:
-                results['url'] = sources[source.lower()] + id
+                if source.lower() == 'pubmed':
+                    results['url'] = []
+                    results['url'].append(sources[source.lower()][0] + id)
+                    results['url'].append(sources[source.lower()][1].replace("PMID", id))
+                else:
+                    results['url'] = sources[source.lower()] + id
             cursor.close()
     else:
         results = "no"
-- 
GitLab


From 5c47734d24bc1647fb0d20b46f67ca5395d70440 Mon Sep 17 00:00:00 2001
From: Sandra Derozier <sandra.derozier@inrae.fr>
Date: Fri, 5 Jul 2024 16:15:12 +0200
Subject: [PATCH 4/6] #20 - API - Add Get_taxons

---
 __init__.py | 21 +++++++++++++++++++++
 database.py | 31 +++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/__init__.py b/__init__.py
index dd182cc..12c7af7 100644
--- a/__init__.py
+++ b/__init__.py
@@ -317,6 +317,10 @@ taxon_model = api.model('Taxon', {
     "qps": fields.String(description="Either this taxon is listed in QPS", enum=['yes', 'true', 'no', 'false'], example="yes"),
     "url": fields.String(description="URL", example="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=1423&rettype=xml")
 })
+taxons_model = api.model('Taxons', {
+    "name": fields.String(default="Bacillus subtilis", description="Taxon name", example="Bacillus subtilis"),
+    "taxons": fields.List(fields.Nested(taxon_model))
+})
 obt_model = api.model('Obt', {
     "id": fields.String(description="OntoBiotope identifier", example="OBT:000427"),
     "type": fields.String(description="OntoBiotope type", enum=['habitat', 'phenotype', 'use'], example="habitat"),
@@ -393,6 +397,23 @@ class ApiGetTaxon(Resource):
         else:
             return jsonify(get_taxon(app, conn, taxid))
 
+# ***** /get/taxons ***** #
+@api.route('/api/get/taxons/<string:name>', doc={"description": "Returns the list of taxa."})
+@api.route('/api/get/taxons/', doc=False)
+@api.route('/api/get/taxons', doc=False)
+@api.doc(params={'name': 'Taxon name (example: Bacillus subtilis)'})
+class ApiGetTaxon(Resource):
+    @api.doc(model=taxons_model)
+    @api.doc(responses={
+        200: 'Success',
+        404: 'Missing mandatory parameter'
+    })
+    def get(self, name=None):
+        if name == None:
+            return {'error': 'Name required'}, 404
+        else:
+            return jsonify(get_taxons(app, conn, name))
+
 # ***** /get/obt ***** #
 @api.route('/api/get/obt/<string:obtid>', doc={"description": "Returns the properties of an OntoBiotope concept given an OBT identifier."})
 @api.route('/api/get/obt/', doc=False)
diff --git a/database.py b/database.py
index 12b18c2..c88c344 100644
--- a/database.py
+++ b/database.py
@@ -85,6 +85,37 @@ def get_taxon(app, conn, id):
                 results['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
         cursor.close()
         return results
+    
+# Get taxons: /get/taxons
+def get_taxons(app, conn, name):
+    conn = get_db(app)
+    if conn != None:
+        cursor = conn.cursor()
+        try:
+            cursor.execute("SELECT taxonid, name, path, qps \
+                            FROM taxon \
+                            WHERE lower(name) ~ '"+ name.lower() +"'")
+        except Exception as err:
+            print_psycopg2_exception(err)
+        results = dict()
+        results['name'] = name
+        taxons = []
+        for row in cursor.fetchall():
+            taxon = dict()
+            taxon['id'] = row[0]
+            taxon['name'] = row[1]
+            taxon['path'] = []
+            for path in row[2].split(","):
+                taxon['path'].append(path)
+            taxon['qps'] = row[3]
+            if "ncbi" in row[0]:
+                taxon['url'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
+            else:
+                taxon['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
+            taxons.append(taxon)
+        results['taxons'] = taxons
+        cursor.close()
+        return results
 
 # Get OntoBiotope: /get/ontobiotope
 def get_ontobiotope(app, conn, id):
-- 
GitLab


From d9747870e460d82db4fca212d2300a9f46ece783 Mon Sep 17 00:00:00 2001
From: Sandra Derozier <sandra.derozier@inrae.fr>
Date: Thu, 5 Sep 2024 09:48:53 +0200
Subject: [PATCH 5/6] #16 - Add names to URLs in the returned object

---
 __init__.py | 7 ++++++-
 database.py | 9 +++++----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/__init__.py b/__init__.py
index 12c7af7..0f46888 100644
--- a/__init__.py
+++ b/__init__.py
@@ -328,8 +328,13 @@ obt_model = api.model('Obt', {
     "synonyms": fields.List(fields.String(description="All the synonyms of this concept", example="soilborne")),
     "path": fields.List(fields.String(description="Path from root to this concept", example="OBT:000001/OBT:000013/OBT:000158/OBT:000427"))
 })
+docs_model = api.model('Docs', {
+    "pubmed-web": fields.String(description="PubMed Web URL", example="https://pubmed.ncbi.nlm.nih.gov/6229999"),
+    "entrez-api": fields.String(description="PubMed API", example="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=6229999&rettype=xml")
+})
 doc_model = api.model('Doc', {
-    "url": fields.List(fields.String(description="Document identifiers where relations were found", example="https://pubmed.ncbi.nlm.nih.gov/6229999"))
+    "url": fields.Nested(docs_model)
+    #"url": fields.List(fields.String(description="Document identifiers where relations were found", example="https://pubmed.ncbi.nlm.nih.gov/6229999"))
 })
 taxonid_model = api.model('TaxonID', {
     "id": fields.String(description="Taxon identifier", example="ncbi:1423")
diff --git a/database.py b/database.py
index c88c344..b621ea0 100644
--- a/database.py
+++ b/database.py
@@ -161,11 +161,12 @@ def get_doc(app, conn, source, id):
             results = dict()
             if cursor.fetchall() != []:
                 if source.lower() == 'pubmed':
-                    results['url'] = []
-                    results['url'].append(sources[source.lower()][0] + id)
-                    results['url'].append(sources[source.lower()][1].replace("PMID", id))
+                    results['url'] = {}
+                    results['url']['pubmed-web'] = sources[source.lower()][0] + id
+                    results['url']['entrez-api'] = sources[source.lower()][1].replace("PMID", id)
                 else:
-                    results['url'] = sources[source.lower()] + id
+                    results['url'] = {}
+                    results['url'][source.lower()+'-web'] = sources[source.lower()] + id
             cursor.close()
     else:
         results = "no"
-- 
GitLab


From 001659a45bab1daef7f3662da5df0ebdd0948039 Mon Sep 17 00:00:00 2001
From: Sandra Derozier <sandra.derozier@inrae.fr>
Date: Thu, 5 Sep 2024 10:44:17 +0200
Subject: [PATCH 6/6] #16 - Add names to URLs in the returned object

---
 __init__.py |  7 ++++++-
 database.py | 22 ++++++++++++++++++----
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/__init__.py b/__init__.py
index 0f46888..66923fe 100644
--- a/__init__.py
+++ b/__init__.py
@@ -310,12 +310,17 @@ version_model = api.model('Version', {
     "dsmz": fields.String(description="Version number or ISO 8601", example="2018-01-26"),
     "genbank": fields.String(description="Version number or ISO 8601", example="2021-09-20")
 })
+taxon_url_model = api.model('Taxon URL', {
+    "ncbi-web": fields.String(description="NCBI Web URL", example="https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=1423"),
+    "entrez-api": fields.String(description="NCBI API", example="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=1423&rettype=xml")
+})
 taxon_model = api.model('Taxon', {
     "id": fields.String(default="ncbi:1423", description="Taxon identifier", example="ncbi:1423"),
     "name": fields.String(description="Canonical name of this taxon", example="Bacillus subtilis"),
     "path": fields.List(fields.String(description="Taxon lineage from root to self", example="ncbi:1/ncbi:131567/ncbi:2/ncbi:1783272/ncbi:1239/ncbi:91061/ncbi:1385/ncbi:186817/ncbi:1386/ncbi:653685/ncbi:1423")),
     "qps": fields.String(description="Either this taxon is listed in QPS", enum=['yes', 'true', 'no', 'false'], example="yes"),
-    "url": fields.String(description="URL", example="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=1423&rettype=xml")
+    "url": fields.Nested(taxon_url_model)
+    # "url": fields.String(description="URL", example="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=1423&rettype=xml")
 })
 taxons_model = api.model('Taxons', {
     "name": fields.String(default="Bacillus subtilis", description="Taxon name", example="Bacillus subtilis"),
diff --git a/database.py b/database.py
index b621ea0..9a25ac8 100644
--- a/database.py
+++ b/database.py
@@ -80,9 +80,14 @@ def get_taxon(app, conn, id):
                 results['path'].append(path)
             results['qps'] = row[3]
             if "ncbi" in row[0]:
-                results['url'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
+                results['url'] = {}
+                results['url']['entrez-api'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
+                results['url']['ncbi-web'] = "https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id="+row[0].replace("ncbi:", "")
+                # results['url'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
             else:
-                results['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
+                results['url'] = {}
+                results['url']['dsmz-web'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
+                # results['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
         cursor.close()
         return results
     
@@ -108,10 +113,19 @@ def get_taxons(app, conn, name):
             for path in row[2].split(","):
                 taxon['path'].append(path)
             taxon['qps'] = row[3]
+            # if "ncbi" in row[0]:
+            #     taxon['url'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
+            # else:
+            #     taxon['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
             if "ncbi" in row[0]:
-                taxon['url'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
+                taxon['url'] = {}
+                taxon['url']['entrez-api'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
+                taxon['url']['ncbi-web'] = "https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id="+row[0].replace("ncbi:", "")
+                # results['url'] = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id="+row[0].replace("ncbi:", "")+"&rettype=xml"
             else:
-                taxon['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
+                taxon['url'] = {}
+                taxon['url']['dsmz-web'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
+                # results['url'] = "https://bacdive.dsmz.de/strain/"+row[0].replace("bd:", "")
             taxons.append(taxon)
         results['taxons'] = taxons
         cursor.close()
-- 
GitLab