forms.py 17.1 KB
Newer Older
1
2
3
4
"""
iPPI-DB django forms
"""

5
from django import forms
6
from django.core.validators import RegexValidator
7
from django.forms import ModelForm, modelformset_factory, formset_factory, BaseFormSet
8

Rachel TORCHET's avatar
Rachel TORCHET committed
9
from .models import *
10
from .ws import pdb_entry_exists
11

Rachel TORCHET's avatar
Rachel TORCHET committed
12
""" Step 1 : IdForm """
13
class IdFormOld(ModelForm):
14
15
16
    class Meta:
        model = Bibliography
        fields = ['source','id_source']
17
18
19
        labels = {
            "id_source": "ID"
        }
20
21
        widgets = {
            'source' : forms.RadioSelect,
22
            'id_source': forms.TextInput(),
23
        }
Hervé  MENAGER's avatar
Hervé MENAGER committed
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
53
54
55
56

class IdForm(forms.Form):
    source = forms.ChoiceField(
        label='Bibliographic type',
        choices=Bibliography.SOURCES,
        initial='PM',
        widget=forms.RadioSelect,
    )
    id_source = forms.CharField(
        label='Bibliographic ID',
        max_length=25,
    )

    def clean(self):
        ret = super().clean()
        Bibliography.validate_source_id(ret["id_source"], ret["source"])
        return ret

    def get_or_create(self):
        kwargs = dict(source=self.cleaned_data["source"],
                      id_source=self.cleaned_data["id_source"],
                      )
        try:
            b = Bibliography.objects.get(**kwargs)
            return b, False
        except Bibliography.DoesNotExist:
            pass
        b = Bibliography(**kwargs)
        b.save(autofill=True)
        return b, True


Rachel TORCHET's avatar
Rachel TORCHET committed
57
""" Step 2 : BibliographyForm """
58
class BibliographyForm(ModelForm):
59
60
    class Meta:
        model = Bibliography
61
        exclude = ['biblio_year']
62
        widgets = {
63
64
65
66
67
            'source': forms.TextInput(attrs={'readonly':'readonly'}),
            'id_source': forms.TextInput(attrs={'readonly':'readonly'}),
            'title': forms.TextInput(attrs={'readonly':'readonly'}),
            'journal_name': forms.TextInput(attrs={'readonly':'readonly'}),
            'authors_list': forms.TextInput(attrs={'readonly':'readonly'}),
68
        }
69

70

Rachel TORCHET's avatar
Rachel TORCHET committed
71
""" Step 3 : PDBForm """
72
73
def validate_pdb_exists(value):
    if not(pdb_entry_exists(value)):
Hervé  MENAGER's avatar
Hervé MENAGER committed
74
75
76
        raise ValidationError(
            'PDB entry not found: %(value)s', params={'value': value}, code='invalid')

77

78
class PDBForm(forms.Form):
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    pdb_id = forms.CharField(
        label="PDB ID",
        max_length=4,
        widget=forms.TextInput(
            attrs={
                'placeholder': 'e.g 3u85',
                'required': 'required',
            }),
        required=True,
        validators=[
            RegexValidator(
                '^[0-9][a-zA-Z0-9]{3}$', message='PDB ID must be 1 numeric + 3 alphanumeric characters'),
            validate_pdb_exists
        ])

94
95

class ProteinForm(ModelForm):
Hervé  MENAGER's avatar
Hervé MENAGER committed
96
97
98
99
100
101
102
103
104

    class Meta:
        model = Protein
        exclude = ['recommended_name_long', 'short_name']
        widgets = {
            'uniprot_id': forms.TextInput(attrs={'readonly': 'readonly', 'class': 'readonly'}),
            'gene_name': forms.TextInput(attrs={'readonly': 'readonly', 'class': 'readonly'}),
            'entry_name': forms.TextInput(attrs={'readonly': 'readonly', 'class': 'readonly'}),
            'organism': forms.TextInput(attrs={'readonly': 'readonly', 'class': 'readonly'}),
105
        }
106

Hervé  MENAGER's avatar
Hervé MENAGER committed
107
108
ProteinFormSet = modelformset_factory(
    Protein, exclude=('recommended_name_long', 'short_name'), extra=0)
109

Rachel TORCHET's avatar
Rachel TORCHET committed
110
111

""" Step 4 : ProteinDomainComplexTypeForm aka. Architecture """
112
113
114
115
116
TYPE_COMPLEX = (
    ('inhibited', 'Inhibited'),
    ('stabilized', 'Stabilized'),
)

117
TYPE_CHOICES = (
Hervé  MENAGER's avatar
Hervé MENAGER committed
118
    ('Hetero2merAB', 'Inhib_Hetero 2-mer AB'),
119
120
121
122
123
124
125
126
127
128
129
    ('Homo2merA2', 'Inhib_Homo 2-mer A2'),
    ('custom', 'Inhib_Custom'),
    ('Hetero2merAB', 'Stab_Hetero 2-mer AB'),
    ('Homo2merA2', 'Stab_Homo 2-mer A2'),
    ('HomoLike2mer', 'Stab_Homo-Like 2-mer A2'),
    ('Homo3merA3', 'Stab_Homo 3-mer A3'),
    ('Homo3merA2', 'Stab_Homo 3-mer A3 inhibited A2-dimer'),
    ('Homo4merA4', 'Stab_Homo 4-mer A4'),
    ('RingHomo3mer', 'Stab_Ring-Like 3-mer A3'),
    ('RingHomo5mer', 'Stab_Ring-Like 5-mer A5'),
    ('custom', 'Stab_Custom'),
Hervé  MENAGER's avatar
Hervé MENAGER committed
130
131
)

132
133

class ProteinDomainComplexTypeForm(forms.Form):
Rachel TORCHET's avatar
Rachel TORCHET committed
134
135
    complexChoice=forms.CharField(label="PPI Complex Type", widget=forms.Select(choices=TYPE_COMPLEX))
    complexType=forms.CharField(widget=forms.RadioSelect(choices=TYPE_CHOICES))
136
    
Rachel TORCHET's avatar
Rachel TORCHET committed
137
138

""" Step 5 : ProteinDomainComplexForm aka. Composition """
139
class ProteinDomainComplexForm(ModelForm):
Hervé  MENAGER's avatar
Hervé MENAGER committed
140
141

    class Meta:
142
143
144
        model=ProteinDomainComplex
        fields=['protein', 'domain', 'ppc_copy_nb']

145
146
147
148
149
150
COMPLEX_TYPE = (
    ('Bound', 'Bound'),
    ('Partner', 'Partner')
)

class ComplexCompositionForm(forms.Form):
151
    complex_type=forms.CharField(widget=forms.Select(choices=COMPLEX_TYPE, attrs={'class':'form-control', 'onchange':'myFunction()'}))
152
153
    complex_protein=forms.ModelChoiceField(queryset=Protein.objects.all(), required=True, widget=forms.Select(attrs={'class':'form-control'}),empty_label=None)
    complex_domain=forms.ModelChoiceField(queryset=Domain.objects.all(), required=True,widget=forms.Select(attrs={'class':'form-control'}),empty_label=None)
154
    ppc_copy_nb=forms.IntegerField(initial=1,required=True,widget=forms.NumberInput(attrs={'class':'form-control'}))
155

156
    def __init__(self,pks=None,*args,**kwargs):
157
        super(ComplexCompositionForm, self).__init__(*args, **kwargs)
158
159
160
        if pks is not None:
            self.fields['complex_protein'].queryset = Protein.objects.filter(pk__in=pks)
            self.fields['complex_domain'].queryset = Domain.objects.all()
161

162

163
ComplexCompositionFormSet = formset_factory(ComplexCompositionForm, extra=1)
164
formset= ComplexCompositionFormSet()
Rachel TORCHET's avatar
Rachel TORCHET committed
165
#print(formset.is_valid())
Hervé  MENAGER's avatar
Hervé MENAGER committed
166

Rachel TORCHET's avatar
Rachel TORCHET committed
167
""" Step 6 : PpiForm """
168
class PpiForm(ModelForm):
Hervé  MENAGER's avatar
Hervé MENAGER committed
169
170
171

    class Meta:
        model = Ppi
Rachel TORCHET's avatar
Rachel TORCHET committed
172
        fields = ['pdb_id','symmetry','pockets_nb']
Hervé  MENAGER's avatar
Hervé MENAGER committed
173

Hervé  MENAGER's avatar
Hervé MENAGER committed
174
class PpiComplexForm(ModelForm):
Hervé  MENAGER's avatar
Hervé MENAGER committed
175
176
177

    class Meta:
        model = PpiComplex
Rachel TORCHET's avatar
Rachel TORCHET committed
178
        fields = ['cc_nb']
179

Rachel TORCHET's avatar
Rachel TORCHET committed
180
class PpiAndComplexForm(forms.Form):
181
182
183
184
    pdb_id=forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
    symmetry=forms.ModelChoiceField(queryset=Symmetry.objects.all(), required=True, widget=forms.Select(attrs={'class':'form-control'}), empty_label=None)
    pockets_nb=forms.IntegerField(initial=1,required=True,widget=forms.TextInput(attrs={'class':'form-control'}))
    cc_nb=forms.IntegerField(initial=1,required=True,widget=forms.TextInput(attrs={'class':'form-control'}))
Rachel TORCHET's avatar
Rachel TORCHET committed
185

Rachel TORCHET's avatar
Rachel TORCHET committed
186
""" Step 7 : CompoundForm """
187
class CompoundForm(ModelForm):
188

189
190
191
192
    class Meta:
        model = Compound
        fields = ['common_name','is_macrocycle']
        widgets = {
193
        'common_name': forms.TextInput(attrs={'class':'form-control', 'required': 'required'}),
194
        }
195

196
197
198
class RefCompoundBiblioForm(ModelForm):
    model=RefCompoundBiblio
    exclude = ['compound', 'bibliography']
Rachel TORCHET's avatar
Rachel TORCHET committed
199

200
201
202
203
204
205
206
207
208
209

TYPE_MOLECULE = (
    ('smiles', 'smiles'),
    ('iupac', 'iupac'),
    ('sketch','sketch'),
)

class BaseCompoundFormSet(BaseFormSet):
    def add_fields(self, form, index):
        super(BaseCompoundFormSet, self).add_fields(form, index)
210
211
212
        form.fields["compound_name"] = forms.CharField(required=True, widget=forms.TextInput(attrs={'class':'form-control','required':'required'}))
        form.fields["molecule"] = forms.CharField(required=True, widget=forms.RadioSelect(choices=TYPE_MOLECULE, attrs={'class':'form-control'}))
        form.fields["molecule_comp"] = forms.CharField(required=True, widget=forms.TextInput(attrs={'required':'required', 'class':'form-control'}))
213

214
CompoundFormSet = formset_factory(CompoundForm, formset=BaseCompoundFormSet, can_delete=True)
215
216
formset = CompoundFormSet()

Rachel TORCHET's avatar
Rachel TORCHET committed
217
""" Step 8 : TestsForm """
218
219
220
221
222
223
TEST_TYPE = (
    ('activity', 'Activity'),
    ('cytotoxicity', 'Cytotoxicity'),
    ('pk','PK'),
)

Rachel TORCHET's avatar
Rachel TORCHET committed
224
class TestsForm(forms.Form):
225
    test_type= forms.CharField(required=True, widget=forms.Select(choices=TEST_TYPE, attrs={'class': 'form-control'}))
226
    activityDesc_name=forms.ModelChoiceField(queryset=TestActivityDescription.objects.values_list('test_name', flat=True).distinct(), empty_label=None, widget=forms.Select(attrs={'class':'form-control'}))
227
    activityDesc_cell_line=forms.ModelChoiceField(queryset=CellLine.objects.all(), empty_label=None, widget=forms.Select(attrs={'class':'form-control'}))
228
    activityDesc_nb_active_compound=forms.IntegerField()
229
    activityDesc_type=forms.ModelChoiceField(required=False, widget=forms.RadioSelect(attrs={'class':'form-control'}),queryset=TestActivityDescription.objects.values_list('test_type', flat=True).distinct(), empty_label=None)
230
    activityDesc_test_modulation_type=forms.ModelChoiceField(queryset=TestActivityDescription.objects.values_list('test_modulation_type', flat=True).distinct(), empty_label=None, widget=forms.Select(attrs={'class':'form-control'}))
231
232
    activityDesc_is_primary = forms.ModelChoiceField(queryset=TestActivityDescription.objects.values_list('is_primary', flat=True).distinct(), empty_label=None, widget=forms.Select(attrs={'class':'form-control'}))
    activityDesc_protein_bound_construct = forms.ModelChoiceField(queryset=TestActivityDescription.objects.values_list('protein_bound_construct', flat=True).distinct(),  empty_label=None, widget=forms.Select(attrs={'class':'form-control'}))
233
    activityRes_compound = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}))
234
    activityRes_test_modulation_type = forms.ModelChoiceField(queryset=CompoundActivityResult.objects.values_list('modulation_type', flat=True).distinct(), empty_label=None,widget=forms.Select(attrs={'class': 'form-control'}))
235
    activityRes_activity_type = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
236
    activityRes_activity = forms.IntegerField(widget=forms.NumberInput(attrs={'class': 'form-control'}))
237
238
    activityRes_inhibition_percentage = forms.IntegerField(required=False)
    cytotoxDesc_name=forms.ModelChoiceField(queryset=TestCytotoxDescription.objects.values_list('test_name', flat=True).distinct(), empty_label=None, widget=forms.Select(attrs={'class':'form-control'}))
239
240
    cytotoxDesc_cell_line=forms.ModelChoiceField(queryset=CellLine.objects.all(), empty_label=None, widget=forms.Select(attrs={'class':'form-control'}),required=False)
    cytotoxDesc_compound_concentration=forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Compound concentration'}),required=False)
241
242
    cytotoxRes_compound = forms.ChoiceField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=False)
    cytotoxRes_toxicity = forms.ModelChoiceField(queryset=CompoundCytotoxicityResult.objects.all(), required=False)
243
    pkDesc_name=forms.ModelChoiceField(queryset=TestPKDescription.objects.values_list('test_name', flat=True).distinct(), empty_label=None, widget=forms.Select(attrs={'class':'form-control'}))
244
245
    pkDesc_organism=forms.ModelChoiceField(queryset=Taxonomy.objects.all(), empty_label="Choose a corresponding organism",required=False)
    pkDesc_administration_mode=forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Admnistration mode'}),required=False)
246
247
    pkDesc_dose=forms.IntegerField(required=False)
    pkDesc_dose_interval=forms.IntegerField(required=False)
248
    pkDesc_concentration=forms.IntegerField(required=False)
249
250
251
    pkRes_compound = forms.ChoiceField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=False)
    pkRes_tolerated = forms.ChoiceField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=False)
    pkRes_auc = forms.IntegerField(initial=1, required=False)
252
253
    pkRes_clearance = forms.DecimalField(required=False)
    pkRes_cmax = forms.DecimalField(required=False)
254
255
256
257
258
259
260
261
262
263
264
265
    pkRes_oral_bioavailability = forms.IntegerField(initial=1, required=False)
    pkRes_t_demi = forms.IntegerField(initial=1, required=False)
    pkRes_t_max = forms.IntegerField(initial=1, required=False)
    pkRes_voldistribution = forms.DecimalField(required=False)


TEST_TYPE = (
    ('activity', 'Activity'),
    ('cytotoxicity', 'Cytotoxicity'),
    ('pk','PK'),
)

266
267
268
269
270
UNIT_CONCENTRATION = (
    ('micro', 'µM'),
    ('nano', 'nM'),
    ('pico','pM'),
)
Rachel TORCHET's avatar
Rachel TORCHET committed
271
272
273
274
275
276


ADMINISTRATION_MODE = [
    ("Enteral",
        [('', "Oral"), ('', "Sublingual"), ('', "Rectal")]),
    ("Parenteral",
277
        [('', "Inhalational"), ('', "Intravenous"), ('', "Intramuscular"), ('', "Subcutaneous"), ('', "Intra-arterial"), ('', "Intra-articular"), ('', "Intrathecal"), ('', "Intradermal"), ('', "Transdermal")]),
Rachel TORCHET's avatar
Rachel TORCHET committed
278
279
280
281
282
283
    ("Local",
        [('', "Skin topical"), ('', "Intranasal"), ('', "Ocular drops"), ('', "Mucosal-throat"), ('', "Vagina"), ('', "Mouth"), ('', "Ear"), ('', "Transdermal")]),
]



284
285
286
287
class BaseTestsFormSet(BaseFormSet):
    def add_fields(self, form, index):
        super(BaseTestsFormSet, self).add_fields(form, index)
        form.fields["test_type"] = forms.CharField(required=True, widget=forms.Select(choices=TEST_TYPE, attrs={'class':'form-control'}))
288
289
290
291
292
        form.fields["activityDesc_name"] = forms.ModelChoiceField(queryset=TestActivityDescription.objects.values_list('test_name', flat=True).distinct(), empty_label=None, widget=forms.Select(attrs={'required':'required', 'class':'form-control'}))
        form.fields["activityDesc_cell_line"] = forms.ModelChoiceField(queryset=CellLine.objects.all(), empty_label=None,widget=forms.Select(attrs={'class': 'form-control'}))
        form.fields["activityDesc_type"] = forms.ModelChoiceField(required=False,widget=forms.Select(attrs={'class': 'form-control'}),queryset=TestActivityDescription.objects.values_list('test_type',flat=True).distinct(), empty_label=None)
        form.fields["activityDesc_test_modulation_type"] = forms.ModelChoiceField(queryset=TestActivityDescription.objects.values_list('test_modulation_type', flat=True).distinct(),empty_label=None, widget=forms.Select(attrs={'class': 'form-control'}))
        form.fields["activityDesc_protein_bound_construct"] = forms.ModelChoiceField(queryset=TestActivityDescription.objects.values_list('protein_bound_construct', flat=True).distinct(),empty_label=None, widget=forms.Select(attrs={'class': 'form-control'}))
293
        form.fields["activityRes_compound"] = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}))
294
        form.fields["activityRes_activity_type"] = forms.ModelChoiceField(required=False,widget=forms.Select(attrs={'class': 'form-control'}),queryset=CompoundActivityResult.objects.values_list('activity_type',flat=True).distinct(),empty_label=None)
Rachel TORCHET's avatar
Rachel TORCHET committed
295
        form.fields["activityRes_unit_concentration"] = forms.CharField(required=True, widget=forms.Select(choices=UNIT_CONCENTRATION, attrs={'class':'form-control'}))
296
        form.fields["activityRes_activity"] = forms.IntegerField(forms.NumberInput(attrs={'class': 'form-control'}))
297
298
299
        form.fields["cytotoxDesc_name"] = forms.ModelChoiceField(queryset=TestCytotoxDescription.objects.values_list('test_name', flat=True).distinct(), empty_label=None, required=False, widget=forms.Select(attrs={'required':'required', 'class':'form-control'}))
        form.fields["cytotoxDesc_cell_line"] = forms.ModelChoiceField(queryset=CellLine.objects.all(), empty_label=None, widget=forms.Select(attrs={'class': 'form-control'}),required=False)
        form.fields["cytotoxDesc_compound_concentration"] = forms.DecimalField(required=False)
Rachel TORCHET's avatar
Rachel TORCHET committed
300
        form.fields["cytotoxRes_unit_concentration"] = forms.CharField(required=True, widget=forms.Select(choices=UNIT_CONCENTRATION, attrs={'class':'form-control'}))
301
302
303
304
        form.fields["cytotoxRes_compound"] = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}),required=False)
        form.fields["cytotoxRes_toxicity"] = forms.ModelChoiceField(queryset=CompoundCytotoxicityResult.objects.values_list('toxicity', flat=True).distinct(), empty_label=None,required=False,widget=forms.Select(attrs={'class': 'form-control'}))
        form.fields["pkDesc_name"] = forms.ModelChoiceField(queryset=TestPKDescription.objects.values_list('test_name', flat=True).distinct(), empty_label=None,required=False, widget=forms.Select(attrs={'required':'required', 'class':'form-control'}))
        form.fields["pkDesc_organism"] = forms.ModelChoiceField(queryset=Taxonomy.objects.all(),empty_label=None, widget=forms.Select(attrs={'class': 'form-control'}),required=False)
Rachel TORCHET's avatar
Rachel TORCHET committed
305
        form.fields["pkDesc_administration_mode"] = forms.CharField(required=True, widget=forms.Select(choices=ADMINISTRATION_MODE, attrs={'class':'form-control'}))
306
307
        form.fields["pkDesc_dose"] = forms.IntegerField(required=False)
        form.fields["pkDesc_dose_interval"] = forms.IntegerField(required=False)
308
309
310
        form.fields["pkDesc_concentration"] = forms.DecimalField(required=False)
        form.fields["pkRes_compound"] = forms.ChoiceField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=False)
        form.fields["pkRes_tolerated"] = forms.ChoiceField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=False)
311
312
313
314
315
316
317
318
        form.fields["pkRes_auc"] = forms.IntegerField(initial=1, required=False)
        form.fields["pkRes_clearance"] = forms.DecimalField(required=False)
        form.fields["pkRes_cmax"] = forms.DecimalField(required=False)
        form.fields["pkRes_oral_bioavailability"] = forms.IntegerField(initial=1, required=False)
        form.fields["pkRes_t_demi"] = forms.IntegerField(initial=1, required=False)
        form.fields["pkRes_t_max"] = forms.IntegerField(initial=1, required=False)
        form.fields["pkRes_voldistribution"] = forms.DecimalField(required=False)

319
TestsFormSet = formset_factory(TestsForm, formset=BaseTestsFormSet, can_delete=True, can_order=True)
320
formset = TestsFormSet()