diff --git a/ippisite/ippidb/tasks.py b/ippisite/ippidb/tasks.py index 2fe4374d4a2b4ddbbd3f24c58f25bfefda2a24e8..6a836e42d864e639efe74617bb1c5f758aabcb02 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 08b377b4d99afaf9a2b913780f939c91113b9a5d..cab9f7695f18b44bbce5c02741ec441f9e4483cd 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 7db55abf631e8e2409e7556e2ecf6f2880c08dfb..4da111df4ed3bf53ab49d8b0ab17d8a586e05869 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>