From 9b2b88fa117014844aa76193d1d65a6ae4117fc9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Fri, 9 Mar 2018 15:49:15 +0100
Subject: [PATCH] migrate compound PK results (fix #17)

Former-commit-id: f198b586ce9d22b1e60e2dfa294e061f24dfa076
---
 .../management/commands/import_v1_data.py     | 35 +++++++++++++++++++
 ippisite/ippidb/models.py                     |  3 ++
 2 files changed, 38 insertions(+)

diff --git a/ippisite/ippidb/management/commands/import_v1_data.py b/ippisite/ippidb/management/commands/import_v1_data.py
index d2fe65cc..a5bbdfb8 100644
--- a/ippisite/ippidb/management/commands/import_v1_data.py
+++ b/ippisite/ippidb/management/commands/import_v1_data.py
@@ -448,6 +448,7 @@ select distinct protein.NumUniprot, domain.PfamNumAccession  , complexe.NbCopy,
             TestPKDescription.objects.all().delete()
             self.stdout.write(
                 self.style.SUCCESS('Successfully flushed test PK descriptions table'))
+            tpd_id_mapping = {}
             for row in rows:
                 try:
                     tpd = TestPKDescription()
@@ -472,6 +473,7 @@ select distinct protein.NumUniprot, domain.PfamNumAccession  , complexe.NbCopy,
                     tpd.dose = round(row[6], 4)
                     tpd.dose_interval = row[7]  
                     tpd.save()
+                    tpd_id_mapping[row[0]] = tpd.id
                 except Exception as e:
                     if options['stoponfail']:
                         import traceback
@@ -484,6 +486,39 @@ select distinct protein.NumUniprot, domain.PfamNumAccession  , complexe.NbCopy,
                 else:
                     self.stdout.write(
                         self.style.SUCCESS('Successfully inserted {}'.format(row[2])))
+            cursor.execute("""select c.CanonicalSmile, r.IDTestPK, r.Tolerated, r.AUC, r.Clearance, r.Cmax, r.OralBioavailability, r.Tdemi, r.Tmax, r.VolDistribution from cmpdPKResult as r inner join compound as c on r.IDCompound = c.IDCompound;""")
+            rows = cursor.fetchall()
+            CompoundPKResult.objects.all().delete()
+            self.stdout.write(
+                self.style.SUCCESS('Successfully flushed compound cytotoxicity PK table'))
+            for row in rows:
+                try:
+                    cpr = CompoundPKResult()
+                    cpr.test_pk_description = TestPKDescription.objects.get(id=tpd_id_mapping[row[1]])
+                    cpr.compound = Compound.objects.get(canonical_smile=row[0])
+                    cpr.tolerated = row[2] == 'Y'
+                    cpr.auc = row[3]
+                    cpr.clearance = row[4]
+                    cpr.cmax = row[5]
+                    cpr.oral_bioavailability = row[6]
+                    cpr.t_demi = row[7]
+                    cpr.t_max = row[8]
+                    cpr.voldistribution = row[9]
+                    cpr.save()
+                except Exception as e:
+                    if options['stoponfail']:
+                        import traceback
+                        self.stderr.write(traceback.format_exc())
+                        raise CommandError(
+                            'Failed inserting {} {}'.format(row[1], row[2]))
+                    else:
+                        import traceback
+                        self.stderr.write(traceback.format_exc())
+                        self.stdout.write(
+                            self.style.ERROR('Failed inserting {} {}'.format(row[1], row[2])))
+                else:
+                    self.stdout.write(
+                        self.style.SUCCESS('Successfully inserted {}'.format(str(cpr))))
         if options['mddr']:
             from pybel import readfile
             MDDRCompoundImport.objects.all().delete()
diff --git a/ippisite/ippidb/models.py b/ippisite/ippidb/models.py
index 76a635df..161b8dba 100644
--- a/ippisite/ippidb/models.py
+++ b/ippisite/ippidb/models.py
@@ -549,6 +549,9 @@ class CompoundPKResult(models.Model):
     class Meta:
         unique_together = (('compound', 'test_pk_description'),)
 
+    def __str__(self):
+        return 'Compound PK result for test {} on {}'.format(self.test_pk_description.id, self.compound.id)
+
 
 class CompoundAction(models.Model):
     ACTIVATION_MODES = (
-- 
GitLab