From 1b6dcfbdfc7b4b600267e3a231a5e63f03dfdbd9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Tue, 23 May 2017 22:16:02 +0200
Subject: [PATCH] migrate TestActivityDescription and CellLine

- CellLine is an optional field in TestActivityDescription
- fixes #12, fixes #27


Former-commit-id: 0d2cf5154164153b3047befd87a46a1b6885b60c
---
 ippisite/db.sqlite3.REMOVED.git-id            |  2 +-
 .../management/commands/import_v1_data.py     | 38 ++++++++++++++++++-
 .../migrations/0024_auto_20170523_2000.py     | 21 ++++++++++
 ippisite/ippidb/models.py                     |  2 +-
 4 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 ippisite/ippidb/migrations/0024_auto_20170523_2000.py

diff --git a/ippisite/db.sqlite3.REMOVED.git-id b/ippisite/db.sqlite3.REMOVED.git-id
index 226017b6..79d648ce 100644
--- a/ippisite/db.sqlite3.REMOVED.git-id
+++ b/ippisite/db.sqlite3.REMOVED.git-id
@@ -1 +1 @@
-0029ba376cb55048b9799efa8aaeccaaf33d350d
\ No newline at end of file
+eb310574640f27558fc3118d82ffb14ea7912b79
\ No newline at end of file
diff --git a/ippisite/ippidb/management/commands/import_v1_data.py b/ippisite/ippidb/management/commands/import_v1_data.py
index 622ede96..1b2bf7bb 100644
--- a/ippisite/ippidb/management/commands/import_v1_data.py
+++ b/ippisite/ippidb/management/commands/import_v1_data.py
@@ -7,7 +7,7 @@ from pybel import readfile
 
 from ippidb.models import Bibliography, Protein, Taxonomy, MolecularFunction, \
     Domain, ProteinDomainBoundComplex, ProteinDomainPartnerComplex, Symmetry, Ppi, PpiComplex, Disease, \
-    Compound, MDDRCompoundImport, MDDRActivityClass
+    Compound, MDDRCompoundImport, MDDRActivityClass, TestActivityDescription, CellLine
 
 class MyConverter(mysql.connector.conversion.MySQLConverter):
 
@@ -75,6 +75,13 @@ class Command(BaseCommand):
             default=False,
             help='Flush and migrate compounds',
         )
+        parser.add_argument(
+            '--test-activity-description',
+            action='store_true',
+            dest='test-activity-description',
+            default=False,
+            help='Flush and migrate test activity descriptions',
+        )
         parser.add_argument(
             '--stoponfail',
             action='store_true',
@@ -234,6 +241,35 @@ select distinct protein.NumUniprot, domain.PfamNumAccession  , complexe.NbCopy,
                         self.stdout.write(self.style.ERROR('Failed inserting {} {}'.format(row[0], row[1])))
                 else:
                     self.stdout.write(self.style.SUCCESS('Successfully inserted {} {}'.format(row[0], row[1])))
+            cursor.execute("""SELECT * FROM testActivityDescription""")
+            rows = cursor.fetchall()
+            TestActivityDescription.objects.all().delete()
+            CellLine.objects.all().delete()
+            self.stdout.write(self.style.SUCCESS('Successfully flushed test activity descriptions table and cell lines table'))
+            for row in rows:
+                try:
+                    tad = TestActivityDescription()
+                    cursor.execute("""select IDSource from biblio where IDBiblio={}""".format(row[2]))
+                    biblio_row = cursor.fetchone()
+                    biblio = Bibliography.objects.get(id_source=biblio_row[0])
+                    tad.biblio = biblio
+                    Ppi.objects.get(id=ppi_ids_mapping[row[3]])
+                    tad.test_name = row[4]
+                    tad.test_type = row[7].upper()
+                    tad.test_modulation_type = row[8][0]
+                    tad.nb_active_compounds = row[9]
+                    if row[16] is not None:
+                        tad.cell_line, created = CellLine.objects.get_or_create(name=row[16])
+                    tad.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:
+                        self.stdout.write(self.style.ERROR('Failed inserting {} {}'.format(row[1], row[2])))
+                else:
+                    self.stdout.write(self.style.SUCCESS('Successfully inserted {}'.format(row[2])))
         if options['mddr']:
             MDDRCompoundImport.objects.all().delete()
             MDDRActivityClass.objects.all().delete()
diff --git a/ippisite/ippidb/migrations/0024_auto_20170523_2000.py b/ippisite/ippidb/migrations/0024_auto_20170523_2000.py
new file mode 100644
index 00000000..d107a840
--- /dev/null
+++ b/ippisite/ippidb/migrations/0024_auto_20170523_2000.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.1 on 2017-05-23 20:00
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ippidb', '0023_auto_20170523_1858'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='testactivitydescription',
+            name='cell_line',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='ippidb.CellLine'),
+        ),
+    ]
diff --git a/ippisite/ippidb/models.py b/ippisite/ippidb/models.py
index f012c911..8ca9305e 100644
--- a/ippisite/ippidb/models.py
+++ b/ippisite/ippidb/models.py
@@ -314,7 +314,7 @@ class TestActivityDescription(models.Model):
     test_type = models.CharField('Test type', max_length=5, choices=TEST_TYPES)
     test_modulation_type = models.CharField('Test modulation type', max_length=1, choices=TEST_MODULATION_TYPES)
     nb_active_compounds = models.IntegerField('Total number of active compounds')  
-    cell_line = models.ForeignKey(CellLine)
+    cell_line = models.ForeignKey(CellLine, blank=True, null=True)
 
     def get_complexes(self):
         """
-- 
GitLab