From 3e2b9dd3cf471a8c06efc3edf27f100ba30284ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Sun, 8 Apr 2018 17:42:47 +0200
Subject: [PATCH] precompute and store the LE-LLE biplot data in the database

precompute with python manage lle_le


Former-commit-id: c831ad2782e66bdbf4127ee870d5748b89a67abe
---
 ippisite/db.sqlite3.REMOVED.git-id            |  2 +-
 ippisite/ippidb/management/commands/lle_le.py | 20 ++++++++++++-----
 .../ippidb/migrations/0032_lellebiplotdata.py | 22 +++++++++++++++++++
 ippisite/ippidb/models.py                     |  3 +++
 ippisite/ippidb/templates/compound_card.html  |  6 +----
 ippisite/ippidb/views.py                      |  6 ++---
 6 files changed, 44 insertions(+), 15 deletions(-)
 create mode 100644 ippisite/ippidb/migrations/0032_lellebiplotdata.py

diff --git a/ippisite/db.sqlite3.REMOVED.git-id b/ippisite/db.sqlite3.REMOVED.git-id
index d23a7914..6947594a 100644
--- a/ippisite/db.sqlite3.REMOVED.git-id
+++ b/ippisite/db.sqlite3.REMOVED.git-id
@@ -1 +1 @@
-b3413ac6f067ab89b2dad63c444b3021ffeef640
\ No newline at end of file
+8e1155c407f37772a3cec105df9f551c3bbe8232
\ No newline at end of file
diff --git a/ippisite/ippidb/management/commands/lle_le.py b/ippisite/ippidb/management/commands/lle_le.py
index 8b87716a..6a0d08c8 100644
--- a/ippisite/ippidb/management/commands/lle_le.py
+++ b/ippisite/ippidb/management/commands/lle_le.py
@@ -1,9 +1,10 @@
 import glob
+import json
 
 from django.utils import timezone
 from django.core.management import BaseCommand, CommandError
 
-from ippidb.models import Compound
+from ippidb.models import Compound, LeLleBiplotData
 
 class Command(BaseCommand):
 
@@ -11,9 +12,16 @@ class Command(BaseCommand):
 
     def handle(self, *args, **options):
         self.stdout.write(self.style.SUCCESS('Generating the LE vs. LLE biplot...'))
+        le_lle_data = []
+        LeLleBiplotData.objects.all().delete()
+        self.stdout.write(
+            self.style.SUCCESS('Successfully flushed LE-LLE biplot data table'))
         for comp in Compound.objects.all():
-            print(comp.id, comp.le, comp.lle)
-        #df = Compound.objects.all().to_dataframe()
-        #df['le']=df[
-        #print(df.to_csv())
-        #print(df.columns)
+            le = round(comp.le, 7)
+            lle = round(comp.lle, 7)
+            le_lle_data.append({'x': le, 'y': lle, 'id': comp.id, 'family':'TBD'})
+        le_lle_json = json.dumps(le_lle_data, separators=(',',':'))
+        new = LeLleBiplotData()
+        new.le_lle_biplot_data = le_lle_json
+        new.save()
+        print(len(le_lle_json))
diff --git a/ippisite/ippidb/migrations/0032_lellebiplotdata.py b/ippisite/ippidb/migrations/0032_lellebiplotdata.py
new file mode 100644
index 00000000..8eae1a75
--- /dev/null
+++ b/ippisite/ippidb/migrations/0032_lellebiplotdata.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11 on 2018-04-08 15:22
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ippidb', '0031_compoundactivityresult_inhibition_percentage'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='LeLleBiplotData',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('le_lle_biplot_data', models.CharField(blank=True, max_length=150000, null=True, verbose_name='LE-LLE biplot JSON data')),
+            ],
+        ),
+    ]
diff --git a/ippisite/ippidb/models.py b/ippisite/ippidb/models.py
index 32cf38fd..0cc0f182 100644
--- a/ippisite/ippidb/models.py
+++ b/ippisite/ippidb/models.py
@@ -422,6 +422,9 @@ class Compound(models.Model):
         """
         return float(self.compoundactivityresult_set.aggregate(Max('activity'))['activity__max'] - self.a_log_p)     
 
+class LeLleBiplotData(models.Model):
+    le_lle_biplot_data = models.CharField('LE-LLE biplot JSON data', max_length=150000, blank=True, null=True)
+
 class MDDRActivityClass(models.Model):
     name = models.CharField('Activity Class', max_length=100, unique=True)
 
diff --git a/ippisite/ippidb/templates/compound_card.html b/ippisite/ippidb/templates/compound_card.html
index 5a1495d0..24f97c84 100644
--- a/ippisite/ippidb/templates/compound_card.html
+++ b/ippisite/ippidb/templates/compound_card.html
@@ -165,11 +165,7 @@
                     { label: 'Other PPI families',
                       borderColor: "rgba(211,211,211, 0.8)",
                       backgroundColor: "rgba(211,211,211, 0.8)",
-                      data:[
-                        {% for c in all_compounds %}
-                            {x: {{ c.le }}, y: {{ c.lle }}, id: {{ c.id }} },
-                        {% endfor %}
-                        ]
+                      data: {{ biplot_data | safe }}
                     }
                 ]};
                 var ctx = document.getElementById('le_lle_biplot').getContext('2d');
diff --git a/ippisite/ippidb/views.py b/ippisite/ippidb/views.py
index 04b2f150..c72d6598 100644
--- a/ippisite/ippidb/views.py
+++ b/ippisite/ippidb/views.py
@@ -5,7 +5,7 @@ from django.http import HttpResponseRedirect, Http404
 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 from formtools.wizard.views import SessionWizardView, NamedUrlSessionWizardView
 from .forms import IdForm, BibliographyForm, PDBForm, ProteinForm, ComplexCompositionForm, ComplexCompositionFormSet, ProteinDomainComplexTypeForm, ProteinDomainComplexForm, PpiForm, PpiComplexForm, ProteinFormSet,TestsForm, CompoundForm, CompoundFormSet
-from .models import Protein, Bibliography, ProteinDomainComplex, ProteinDomainBoundComplex, RefCompoundBiblio, TestActivityDescription, Compound, Ppi, Disease, Taxonomy
+from .models import Protein, Bibliography, ProteinDomainComplex, ProteinDomainBoundComplex, RefCompoundBiblio, TestActivityDescription, Compound, Ppi, Disease, Taxonomy, LeLleBiplotData
 from .ws import get_pdb_uniprot_mapping
 
 
@@ -235,7 +235,7 @@ def compound_list(request):
 def compound_card(request, compound_id):
     try:
         compound = Compound.objects.get(id=int(compound_id))
-        all_compounds = Compound.objects.all()
     except Compound.DoesNotExist:
         raise Http404("No compound data for %s:%s" % (compound_id))
-    return render(request, 'compound_card.html', {'compound': compound, 'all_compounds': all_compounds}) 
+    biplot_data = LeLleBiplotData.objects.get().le_lle_biplot_data 
+    return render(request, 'compound_card.html', {'compound': compound, 'biplot_data': biplot_data}) 
-- 
GitLab