contribute.py 5.43 KB
Newer Older
1
2
3
4
5
6
7
8
"""
iPPI-DB contribution views
"""

from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from formtools.wizard.views import NamedUrlSessionWizardView

Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
9
from ippidb import  forms as ippidb_forms 
10
11
12
from ippidb.models import Protein, Bibliography, ProteinDomainComplex, ProteinDomainBoundComplex, RefCompoundBiblio, TestActivityDescription, Compound, Ppi, Disease, Taxonomy, LeLleBiplotData, PcaBiplotData, PpiFamily, CompoundTanimoto, create_tanimoto
from ippidb.ws import get_pdb_uniprot_mapping

Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
13
14
15
FORMS = [("IdForm", ippidb_forms.IdForm),
         ("BibliographyForm", ippidb_forms.BibliographyForm),
         ("PDBForm", ippidb_forms.PDBForm),
16
         ("ProteinDomainComplexTypeForm",
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
17
18
19
20
21
          ippidb_forms.ProteinDomainComplexTypeForm),
         ("ProteinDomainComplexForm", ippidb_forms.ComplexCompositionFormSet),
         ("PpiForm", ippidb_forms.PpiAndComplexForm),
         ("CompoundForm", ippidb_forms.CompoundFormSet),
         ("TestsForm", ippidb_forms.TestsFormSet),
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
         ]

TEMPLATES = {"IdForm": "IdForm.html",
             "BibliographyForm": "BibliographyForm.html",
             "PDBForm": "PDBForm.html",
             "ProteinDomainComplexTypeForm": "ProteinDomainComplexTypeForm.html",
             "ProteinDomainComplexForm": "ProteinDomainComplexForm.html",
             "PpiForm": "PpiForm.html",
             "CompoundForm": "CompoundForm.html",
             "TestsForm": "TestsForm.html",
             }


class IppiWizard(LoginRequiredMixin, NamedUrlSessionWizardView):

    def get_context_data(self, **kwargs):
        context = super(IppiWizard, self).get_context_data(**kwargs)
        if self.steps.current == 'BibliographyForm':
            context['id_source'] = self.storage.get_step_data('IdForm').get('IdForm-id_source')
            context['source'] = self.storage.get_step_data('IdForm').get('IdForm-source')
            print(self.storage.get_step_data('IdForm').get('IdForm-id_source'))
        if self.steps.current == 'ProteinDomainComplexForm':
            context['complex_choice'] = self.storage.get_step_data('ProteinDomainComplexTypeForm').get('ProteinDomainComplexTypeForm-complexChoice')
            context['complex_type'] = self.storage.get_step_data('ProteinDomainComplexTypeForm').get('ProteinDomainComplexTypeForm-complexType')
            context['pdb'] = self.storage.get_step_data('PDBForm').get('PDBForm-pdb_id')
        if self.steps.current == 'PpiForm':
            context['complex_choice'] = self.storage.get_step_data('ProteinDomainComplexTypeForm').get('ProteinDomainComplexTypeForm-complexChoice')
            context['complex_type'] = self.storage.get_step_data('ProteinDomainComplexTypeForm').get('ProteinDomainComplexTypeForm-complexType')
            context['pdb_id'] = self.storage.get_step_data('PDBForm').get('PDBForm-pdb_id')
        return context

Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
53
    def get_form_kwargs(self, step=None):
54
        # change args pass to a form
Bryan  BRANCOTTE's avatar
Bryan BRANCOTTE committed
55
        kwargs = super().get_form_kwargs(step)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        if step == 'ProteinDomainComplexForm':
            pks = self.storage.get_step_data('PDBForm').get('pks')
            # print("test pks: ",pks)
            kwargs['form_kwargs'] = {'pks': pks}
        return kwargs

    def get_form_initial(self, step):
        # Works for form.forms
        # change value pass to a form
        initial = super().get_form_initial(step)
        if step == 'PpiForm':
            initial['pdb_id'] = self.storage.get_step_data('PDBForm').get('PDBForm-pdb_id')
        return initial

    def process_step(self, form):
        """
        This method overrides the one used to postprocess the form data.
        The added code just sets the form model for use in later forms
        when appropriate
        """
        data = super(IppiWizard, self).process_step(form).copy()
        if self.steps.current == 'IdForm':
78
79
80
81
82
            b, created = form.get_or_create()
            data['IdForm-id'] = b.id
        # if self.steps.current in ['IdForm', 'Bibliography']:
        #     form.get_or_create()
        #     data['pk'] = b.id
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        if self.steps.current == 'PDBForm':
            pdb_id = form.cleaned_data['pdb_id']
            uniprot_ids = []
            protein_ids = []
            uniprot_ids += get_pdb_uniprot_mapping(pdb_id)
            for uniprot_id in uniprot_ids:
                try:
                    p = Protein.objects.get(uniprot_id=uniprot_id)
                except Protein.DoesNotExist:
                    p = Protein()
                    p.uniprot_id = uniprot_ids[0]
                    p.autofill()
                    p.save()
                protein_ids.append(p.id)
            data['pks'] = protein_ids
        return data

    def get_form_instance(self, step):
        # Works only for Modelform
        if self.steps.current == 'BibliographyForm':
103
104
            pk = self.storage.get_step_data('IdForm').get('IdForm-id')
            return models.Bibliography.objects.get(pk=pk)
105
106
107
108
109
110
111
112
113
114
115
116
117
        if self.steps.current == 'ProteinDomainComplexTypeForm':
            print("blablabla", self.storage.get_step_data('PDBForm').get('pks'))
            pks = self.storage.get_step_data('PDBForm').get('pks')
            print("blablabla", self.storage.get_step_data('PDBForm').get('pks'))
            return Protein.objects.filter(id__in=pks)

    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list, **kwargs):
        return render(self.request, 'done.html', {
            'form_data': [form.cleaned_data for form in form_list],
            })