From 48aac0125ea2ba13ded96225a23be27e2a27b560 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20M=C3=A9nager?= <herve.menager@pasteur.fr>
Date: Tue, 26 May 2020 12:27:21 +0200
Subject: [PATCH] add an admin task to update Compound cross-links

---
 ippisite/ippidb/tasks.py            | 58 +++++++++++++++++++++++++++--
 ippisite/ippisite/admin.py          | 13 +++++++
 ippisite/templates/admin/index.html |  7 +++-
 3 files changed, 74 insertions(+), 4 deletions(-)

diff --git a/ippisite/ippidb/tasks.py b/ippisite/ippidb/tasks.py
index 2fe4374d..6a836e42 100644
--- a/ippisite/ippidb/tasks.py
+++ b/ippisite/ippidb/tasks.py
@@ -424,6 +424,24 @@ def generate_pca_plot():
     print("Successfully generated PCA biplot data")
 
 
+def set_compound_links(compound_ids: List[int]) -> List[int]:
+    """
+    set/update links with external databases:
+    DrugBank, ChEMBL, PubChem
+    for each compound of a queryset
+
+    :param qs: queryset over the compounds to be processed
+    :type qs: QuerySet
+    """
+    qs = Compound.objects.filter(id__in=compound_ids)
+    for c in qs:
+        c.set_drugbank_link()
+        c.set_chembl_link()
+        c.set_pubchem_link()
+        c.save()
+    return [c.id for c in qs]
+
+
 @task(base=MonitorTask, bind=True)
 def run_compute_compound_properties(self: MonitorTask, compound_id: int) -> int:
     """
@@ -506,9 +524,30 @@ def run_compute_drugbank_similarity(
 
 
 @task(base=MonitorTask, bind=True)
-def run_validate(
-    self: MonitorTask, compound_ids: List[int] = None
-) -> List[int]:
+def run_set_compound_links(self: MonitorTask, compound_id: int) -> int:
+    """
+    task to set/update the DB cross-links for a compound
+
+    :param self: the task the function is binded to as a method
+    :type self: MonitorTask
+    :param compound_id: the ID of the compound
+    :type compound_id: int
+    :return: the ID of the compound
+    :rtype: int
+    """
+    self.update_state(state=states.STARTED)
+    cj = CompoundJob()
+    cj.compound = Compound.objects.get(id=compound_id)
+    cj.job = Job.objects.get(task_result__task_id=self.task_id)
+    cj.save()
+    self.write(std_out=f"Starting setting external cross-links for {compound_id}")
+    result_compound_ids = set_compound_links([compound_id])
+    self.write(std_out=f"Finished setting external cross-links for {compound_id}")
+    return result_compound_ids[0]
+
+
+@task(base=MonitorTask, bind=True)
+def run_validate(self: MonitorTask, compound_ids: List[int] = None) -> List[int]:
     """
     task "run method" to validate a list of compounds
 
@@ -598,3 +637,16 @@ def launch_plots_computing():
     """
     workflow = chain(run_le_lle_plot.si(), run_pca_plot.si())
     workflow.delay()
+
+
+def launch_set_compound_links():
+    """
+    Launch the tasks to set/update compound links
+    """
+    link_jobs_group = group(
+        [
+            run_set_compound_links.si(compound.id)
+            for compound in Compound.objects.validated()
+        ]
+    )
+    link_jobs_group.delay()
diff --git a/ippisite/ippisite/admin.py b/ippisite/ippisite/admin.py
index 08b377b4..cab9f769 100644
--- a/ippisite/ippisite/admin.py
+++ b/ippisite/ippisite/admin.py
@@ -6,6 +6,7 @@ from ippidb.tasks import (
     launch_update_compound_cached_properties,
     run_compute_drugbank_similarity,
     launch_plots_computing,
+    launch_set_compound_links,
 )
 from django.shortcuts import render
 from ippidb.models import Job
@@ -36,6 +37,10 @@ class IppidbAdmin(admin.AdminSite):
                 "launch_plots_computing/",
                 self.admin_view(self.launch_plots_computing_view),
             ),
+            path(
+                "launch_set_compound_links/",
+                self.admin_view(self.launch_set_compound_links_view),
+            ),
             path("tasklog/", self.admin_view(self.tasklog), name="tasklog"),
         ]
         return my_urls + urls
@@ -90,3 +95,11 @@ class IppidbAdmin(admin.AdminSite):
         launch_plots_computing()
         messages.add_message(request, messages.INFO, "Plots computing launched")
         return redirect("/admin/ippidb/job")
+
+    def launch_set_compound_links_view(self, request):
+        """
+        This view launches the task to set/update compound DB cross-links.
+        """
+        launch_set_compound_links()
+        messages.add_message(request, messages.INFO, "DB links update launched")
+        return redirect("/admin/ippidb/job")
\ No newline at end of file
diff --git a/ippisite/templates/admin/index.html b/ippisite/templates/admin/index.html
index 7db55abf..4da111df 100644
--- a/ippisite/templates/admin/index.html
+++ b/ippisite/templates/admin/index.html
@@ -58,7 +58,12 @@
               style="display:block">{% csrf_token %}
             <input type="submit" value="Plots generation" name="_save"/>
         </form>
-        <hr/>        
+        <hr/>
+        <form method="POST" action="/admin/launch_set_compound_links/"
+              style="display:block">{% csrf_token %}
+            <input type="submit" value="DB links update" name="_save"/>
+        </form>
+        <hr/>
     </div>
 
 
-- 
GitLab