From 2af7683b05b1b58684d4b91675819867d2ce8642 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20M=C3=A9nager?= <herve.menager@pasteur.fr>
Date: Thu, 23 Apr 2020 22:29:20 +0200
Subject: [PATCH] remove the need to specify "protein bound construct" in Test
 step

the value for the field is:
- automatically deducted if there is only one ProteinDomainBoundComplex
  in the PPI
- NULL otherwise

WIP on #204
---
 ippisite/ippidb/forms.py                      | 27 +++----------------
 .../migrations/0052_auto_20200423_2019.py     | 19 +++++++++++++
 ippisite/ippidb/models.py                     |  2 +-
 ...tation_and_storage_pIC50_2380_002_e-9.yaml |  1 -
 ...ation_and_storage_pIC50_4_9846073_e-6.yaml |  1 -
 ...ation_and_storage_pIC50_639_39406_e-3.yaml |  1 -
 ...omputation_and_storage_pIC50_6_85_e-0.yaml |  1 -
 ippisite/ippidb/tests/test_basic_entry.yaml   |  1 -
 .../ippidb/tests/test_complex_no_pfam.yaml    |  1 -
 ippisite/ippidb/tests/test_entry_28.yaml      |  1 -
 .../ippidb/tests/test_simple_heterodimer.yaml |  1 -
 .../tests/test_simple_heterodimer_208.yaml    |  2 --
 .../test_simple_stabilized_heterodimer.yaml   |  1 -
 .../ippidb/tests/test_stabilizer_204.yaml     |  1 -
 .../ippidb/tests/test_with_all_tests.yaml     |  1 -
 ippisite/ippidb/tests/tests_contribute_e2e.py |  1 -
 ippisite/ippidb/views/contribute.py           |  8 +++++-
 17 files changed, 30 insertions(+), 40 deletions(-)
 create mode 100644 ippisite/ippidb/migrations/0052_auto_20200423_2019.py

diff --git a/ippisite/ippidb/forms.py b/ippisite/ippidb/forms.py
index 29892e0b..8b8d64f5 100644
--- a/ippisite/ippidb/forms.py
+++ b/ippisite/ippidb/forms.py
@@ -1053,13 +1053,6 @@ class TestActivityDescriptionForm(forms.ModelForm):
         required=False,
     )
 
-    protein_complex = forms.ModelChoiceField(
-        queryset=models.Protein.objects.none(),
-        label=_("protein_domain_bound_complex_label"),
-        help_text=_("protein_domain_bound_complex_help_text"),
-        required=True,
-    )
-
     class Meta:
         model = models.TestActivityDescription
         fields = "__all__"
@@ -1115,13 +1108,6 @@ class TestActivityDescriptionForm(forms.ModelForm):
                     name=self.cleaned_data["cell_line_name"]
                 )
                 self.instance.cell_line = cell_line
-            if "protein_complex" in self.cleaned_data:
-                try:
-                    self.instance.protein_domain_bound_complex = self.complexes[
-                        self.cleaned_data["protein_complex"].pk
-                    ]
-                except KeyError:
-                    pass
         return super().save(commit=commit)
 
 
@@ -1148,13 +1134,6 @@ class TestActivityDescriptionBaseModelFormSet(BaseInlineNestedFormSet):
         )
         form.nested.set_compound_names(self.__compound_names)
         form.nested.set_modulation_type(self.__modulation_type)
-        form.fields["protein_complex"].queryset = models.Protein.objects.filter(
-            id__in=self.__protein_subset_ids
-        )
-        if models.Protein.objects.filter(id__in=self.__protein_subset_ids).count() == 1:
-            form.fields["protein_complex"].initial = models.Protein.objects.filter(
-                id__in=self.__protein_subset_ids
-            ).first()
 
     def set_compound_names(self, compound_names):
         """
@@ -1173,15 +1152,15 @@ class TestActivityDescriptionBaseModelFormSet(BaseInlineNestedFormSet):
         """
         self.__protein_subset_ids = protein_subset_ids
 
-    def set_complexes(self, complexes):
+    def set_complex(self, protein_domain_bound_complex):
         """
-        Provided to the form the complexes dictionary received from the wizard
+        Set the protein domain bound complex for all the form instances in the formset
 
         :param complexes:
         :return:
         """
         for form in self.forms:
-            form.complexes = complexes
+            form.instance.protein_domain_bound_complex = protein_domain_bound_complex
 
     def set_compounds(self, compounds):
         """
diff --git a/ippisite/ippidb/migrations/0052_auto_20200423_2019.py b/ippisite/ippidb/migrations/0052_auto_20200423_2019.py
new file mode 100644
index 00000000..e25caeff
--- /dev/null
+++ b/ippisite/ippidb/migrations/0052_auto_20200423_2019.py
@@ -0,0 +1,19 @@
+# Generated by Django 2.2.1 on 2020-04-23 20:19
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ippidb', '0051_remove_testactivitydescription_protein_bound_construct'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='testactivitydescription',
+            name='protein_domain_bound_complex',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='ippidb.ProteinDomainBoundComplex'),
+        ),
+    ]
diff --git a/ippisite/ippidb/models.py b/ippisite/ippidb/models.py
index 93733c7b..43e189f8 100644
--- a/ippisite/ippidb/models.py
+++ b/ippisite/ippidb/models.py
@@ -1014,7 +1014,7 @@ class TestActivityDescription(models.Model):
     )
     biblio = models.ForeignKey(Bibliography, on_delete=models.CASCADE)
     protein_domain_bound_complex = models.ForeignKey(
-        ProteinDomainBoundComplex, on_delete=models.CASCADE
+        ProteinDomainBoundComplex, on_delete=models.CASCADE, null=True, blank=True
     )
     ppi = models.ForeignKey(Ppi, models.CASCADE, blank=True, null=True)
     test_type = models.CharField(
diff --git a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_2380_002_e-9.yaml b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_2380_002_e-9.yaml
index 61828344..d23e8c9a 100644
--- a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_2380_002_e-9.yaml
+++ b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_2380_002_e-9.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '2'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_4_9846073_e-6.yaml b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_4_9846073_e-6.yaml
index 1206c530..963453ff 100644
--- a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_4_9846073_e-6.yaml
+++ b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_4_9846073_e-6.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '2'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_639_39406_e-3.yaml b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_639_39406_e-3.yaml
index 306f48ab..8dd666a7 100644
--- a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_639_39406_e-3.yaml
+++ b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_639_39406_e-3.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '2'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_6_85_e-0.yaml b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_6_85_e-0.yaml
index eb93af6d..a11ee0b6 100644
--- a/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_6_85_e-0.yaml
+++ b/ippisite/ippidb/tests/test_activity_computation_and_storage_pIC50_6_85_e-0.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '2'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_basic_entry.yaml b/ippisite/ippidb/tests/test_basic_entry.yaml
index 6f28b468..af3ac0d2 100644
--- a/ippisite/ippidb/tests/test_basic_entry.yaml
+++ b/ippisite/ippidb/tests/test_basic_entry.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '2'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_complex_no_pfam.yaml b/ippisite/ippidb/tests/test_complex_no_pfam.yaml
index cc7429b4..9ff962bf 100644
--- a/ippisite/ippidb/tests/test_complex_no_pfam.yaml
+++ b/ippisite/ippidb/tests/test_complex_no_pfam.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '1'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_entry_28.yaml b/ippisite/ippidb/tests/test_entry_28.yaml
index 7fddcc4b..5923d952 100644
--- a/ippisite/ippidb/tests/test_entry_28.yaml
+++ b/ippisite/ippidb/tests/test_entry_28.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 1
-  protein_complex: '1'
   test_modulation_type: B
   test_name: biolayer interferometry assay
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_simple_heterodimer.yaml b/ippisite/ippidb/tests/test_simple_heterodimer.yaml
index 72cd7db1..31762772 100644
--- a/ippisite/ippidb/tests/test_simple_heterodimer.yaml
+++ b/ippisite/ippidb/tests/test_simple_heterodimer.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '1'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_simple_heterodimer_208.yaml b/ippisite/ippidb/tests/test_simple_heterodimer_208.yaml
index a3c8e2a8..84608560 100644
--- a/ippisite/ippidb/tests/test_simple_heterodimer_208.yaml
+++ b/ippisite/ippidb/tests/test_simple_heterodimer_208.yaml
@@ -33,7 +33,6 @@ activity_tests:
     test_modulation_type: I
     nb_active_compounds: 1
     cell_line_name: ''
-    protein_complex: '1'
     compound_activity_results:
     - activity_mol: 6
       activity_type: pIC50
@@ -46,7 +45,6 @@ activity_tests:
     test_modulation_type: I
     nb_active_compounds: 1
     cell_line_name: 'Human small-cell lung cancer'
-    protein_complex: '1'
     compound_activity_results:
     - activity_mol: 20
       activity_type: "KdRat"
diff --git a/ippisite/ippidb/tests/test_simple_stabilized_heterodimer.yaml b/ippisite/ippidb/tests/test_simple_stabilized_heterodimer.yaml
index 57c5b923..3dc5abcd 100644
--- a/ippisite/ippidb/tests/test_simple_stabilized_heterodimer.yaml
+++ b/ippisite/ippidb/tests/test_simple_stabilized_heterodimer.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: S
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '1'
   test_modulation_type: I
   test_name: pull down
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/test_stabilizer_204.yaml b/ippisite/ippidb/tests/test_stabilizer_204.yaml
index e7d9d13a..32e3ded6 100644
--- a/ippisite/ippidb/tests/test_stabilizer_204.yaml
+++ b/ippisite/ippidb/tests/test_stabilizer_204.yaml
@@ -30,7 +30,6 @@ activity_tests:
   test_name: Fluorescence Polarization
   test_modulation_type: B
   nb_active_compounds: 1
-  protein_complex: '1'
   compound_activity_results:
   - activity_mol: 20
     activity_type: pKd
diff --git a/ippisite/ippidb/tests/test_with_all_tests.yaml b/ippisite/ippidb/tests/test_with_all_tests.yaml
index 26fc81b4..154992d5 100644
--- a/ippisite/ippidb/tests/test_with_all_tests.yaml
+++ b/ippisite/ippidb/tests/test_with_all_tests.yaml
@@ -8,7 +8,6 @@ activity_tests:
     modulation_type: I
   is_primary: true
   nb_active_compounds: 2
-  protein_complex: '2'
   test_modulation_type: I
   test_name: test
   test_type: BIOCH
diff --git a/ippisite/ippidb/tests/tests_contribute_e2e.py b/ippisite/ippidb/tests/tests_contribute_e2e.py
index ad6959b0..09c8983f 100644
--- a/ippisite/ippidb/tests/tests_contribute_e2e.py
+++ b/ippisite/ippidb/tests/tests_contribute_e2e.py
@@ -369,7 +369,6 @@ class ContributionE2ETestCase(TestCase):
                 ]
                 if "cell_line_name" in activity_test:
                     data[f"{idx}-cell_line_name"] = activity_test["cell_line_name"]
-                data[f"{idx}-protein_complex"] = activity_test["protein_complex"]
                 data[
                     f"{idx}-compoundactivityresult_set-activity-results-TOTAL_FORMS"
                 ] = 1  # len(activity_test.get("compound_activity_results",[]))
diff --git a/ippisite/ippidb/views/contribute.py b/ippisite/ippidb/views/contribute.py
index 6e88540f..31e28ae6 100644
--- a/ippisite/ippidb/views/contribute.py
+++ b/ippisite/ippidb/views/contribute.py
@@ -489,7 +489,13 @@ class IppiWizard(
 
             # Step 8.1 Activity Tests
             # We set the dicts to translates attributes into model
-            form_dict["ActivityDescriptionFormSet"].set_complexes(complexes)
+            bound_complexes = [
+                protein_domain_complex
+                for protein_domain_complex in complexes.values()
+                if isinstance(protein_domain_complex, models.ProteinDomainBoundComplex)
+            ]
+            if len(bound_complexes) == 1:
+                form_dict["ActivityDescriptionFormSet"].set_complex(bound_complexes[0])
             form_dict["ActivityDescriptionFormSet"].set_compounds(compounds)
             # We set the constants attributes among the instances to be created by the modelformset
             form_dict["ActivityDescriptionFormSet"].instance = dict(
-- 
GitLab