models.py 25.6 KB
Newer Older
Hervé  MENAGER's avatar
Hervé MENAGER committed
1
2
3
from __future__ import unicode_literals

from django.db import models
4
from django_pandas.managers import DataFrameManager
5
from django.db.models import Max
Hervé  MENAGER's avatar
Hervé MENAGER committed
6

7
from .ws import get_pubmed_info, get_epo_info, get_uniprot_info, get_taxonomy_info, get_go_info, get_pfam_info
8

Hervé  MENAGER's avatar
Hervé MENAGER committed
9

10
class AutoFillableModel(models.Model):
Hervé  MENAGER's avatar
Hervé MENAGER committed
11

12
13
14
15
16
17
18
19
20
21
22
23
24
    """
    AutoFillableModel makes it possible to automatically fill model fields from
    external sources in the autofill() method
    The save method allows to either include autofill or not. in autofill kwarg is
    set to True, save() will first call autofill(), otherwise it won't
    """

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        if kwargs.get('autofill') is True:
            self.autofill()
25
26
        if 'autofill' in kwargs:
            del kwargs['autofill']
Hervé  MENAGER's avatar
Hervé MENAGER committed
27
        super(AutoFillableModel, self).save(*args, **kwargs)
28
29
30


class Bibliography(AutoFillableModel):
Hervé  MENAGER's avatar
Hervé MENAGER committed
31

Hervé  MENAGER's avatar
Hervé MENAGER committed
32
33
34
35
    """
    Bibliography data table
    """
    SOURCES = (
36
37
38
        ('PM', 'PubMed ID'),
        ('PT', 'Patent'),
        ('DO', 'DOI ID')
Hervé  MENAGER's avatar
Hervé MENAGER committed
39
    )
Hervé  MENAGER's avatar
Hervé MENAGER committed
40
41
    source = models.CharField(
        'Bibliographic type', max_length=2, choices=SOURCES, default='PM')
Hervé  MENAGER's avatar
Hervé MENAGER committed
42
43
    id_source = models.CharField('Bibliographic ID', max_length=25)
    title = models.CharField('Title', max_length=300)
44
    journal_name = models.CharField('Journal name', max_length=50, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
45
46
    authors_list = models.CharField('Authors list', max_length=500)
    biblio_year = models.PositiveSmallIntegerField('Year')
47
    cytotox = models.BooleanField('Cytotoxicity data', default=False)
Rachel TORCHET's avatar
Rachel TORCHET committed
48
49
50
51
    in_silico = models.BooleanField('in silico study', default=False)
    in_vitro = models.BooleanField('in vitro study', default=False)
    in_vivo = models.BooleanField('in vivo study', default=False)
    in_cellulo = models.BooleanField('in cellulo study', default=False)
Hervé  MENAGER's avatar
Hervé MENAGER committed
52
53
    pharmacokinetic = models.BooleanField(
        'pharmacokinetic study', default=False)
Rachel TORCHET's avatar
Rachel TORCHET committed
54
    xray = models.BooleanField('X-Ray data', default=False)
Hervé  MENAGER's avatar
Hervé MENAGER committed
55

56
    def autofill(self):
57
58
59
60
61
62
63
64
        if self.source == 'PM':
            info = get_pubmed_info(self.id_source)
        else:
            info = get_epo_info(self.id_source)
        self.title = info['title']
        self.journal_name = info['journal_name']
        self.authors_list = info['authors_list']
        self.biblio_year = info['biblio_year']
Hervé  MENAGER's avatar
Hervé MENAGER committed
65

Hervé  MENAGER's avatar
Hervé MENAGER committed
66
67
68
    class Meta:
        verbose_name_plural = "bibliographies"

69
70
    def __str__(self):
        return '{}, {}'.format(self.source, self.id_source)
71

Hervé  MENAGER's avatar
Hervé MENAGER committed
72

73
class Taxonomy(AutoFillableModel):
Hervé  MENAGER's avatar
Hervé MENAGER committed
74
75
    taxonomy_id = models.DecimalField(
        'NCBI TaxID', unique=True, max_digits=9, decimal_places=0)
Hervé  MENAGER's avatar
Hervé MENAGER committed
76
    name = models.CharField('Organism name', max_length=200)
77

78
    def autofill(self):
79
80
81
        info = get_taxonomy_info(self.taxonomy_id)
        self.name = info['scientific_name']

82
83
84
    def __str__(self):
        return self.name

Hervé  MENAGER's avatar
Hervé MENAGER committed
85
86
    class Meta:
        verbose_name_plural = "taxonomies"
Hervé  MENAGER's avatar
Hervé MENAGER committed
87

Hervé  MENAGER's avatar
Hervé MENAGER committed
88

89
class MolecularFunction(AutoFillableModel):
Hervé  MENAGER's avatar
Hervé MENAGER committed
90
91
    go_id = models.CharField('Gene Ontology ID', unique=True, max_length=10)
                             # GO term id format: 'GO:0000000'
Hervé  MENAGER's avatar
Hervé MENAGER committed
92
93
    description = models.CharField('description', max_length=500)

94
    def autofill(self):
95
96
97
        info = get_go_info(self.go_id)
        self.description = info['label']

98
99
100
    def __str__(self):
        return self.description

Hervé  MENAGER's avatar
Hervé MENAGER committed
101

102
class Protein(AutoFillableModel):
Hervé  MENAGER's avatar
Hervé MENAGER committed
103
    uniprot_id = models.CharField('Uniprot ID', unique=True, max_length=10)
Hervé  MENAGER's avatar
Hervé MENAGER committed
104
105
    recommended_name_long = models.CharField(
        'Uniprot Recommended Name (long)', max_length=75)
Hervé  MENAGER's avatar
Hervé MENAGER committed
106
107
108
    short_name = models.CharField('Short name', max_length=50)
    gene_name = models.CharField('Gene name', unique=True, max_length=30)
    entry_name = models.CharField('Entry name', max_length=30)
109
    organism = models.ForeignKey('Taxonomy', models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
110
111
    molecular_functions = models.ManyToManyField(MolecularFunction)

112
    def autofill(self):
113
        info = get_uniprot_info(self.uniprot_id)
114
        self.recommended_name_long = info['recommended_name']
115
116
        self.gene_name = info['gene']
        self.entry_name = info['entry_name']
117
        self.short_name = info['short_name']
118
119
120
121
122
        try:
            taxonomy = Taxonomy.objects.get(taxonomy_id=info['organism'])
        except Taxonomy.DoesNotExist:
            taxonomy = Taxonomy()
            taxonomy.taxonomy_id = info['organism']
123
            taxonomy.save(autofill=True)
124
        self.organism = taxonomy
125
        super(Protein, self).save()
126
127
128
129
130
131
        for go_id in info['molecular_functions']:
            try:
                mol_function = MolecularFunction.objects.get(go_id=go_id)
            except MolecularFunction.DoesNotExist:
                mol_function = MolecularFunction()
                mol_function.go_id = go_id
132
                mol_function.save(autofill=True)
133
            self.molecular_functions.add(mol_function)
134

135
136
137
    def __str__(self):
        return '{} ({})'.format(self.uniprot_id, self.recommended_name_long)

Hervé  MENAGER's avatar
Hervé MENAGER committed
138

139
class Domain(AutoFillableModel):
Hervé  MENAGER's avatar
Hervé MENAGER committed
140
141
    pfam_acc = models.CharField('Pfam Accession', max_length=10, unique=True)
    pfam_id = models.CharField('Pfam Family Identifier', max_length=20)
Hervé  MENAGER's avatar
Hervé MENAGER committed
142
    pfam_description = models.CharField('Pfam Description', max_length=100)
Hervé  MENAGER's avatar
Hervé MENAGER committed
143
144
145
    domain_family = models.CharField('Domain family', max_length=25)
                                     # TODO: what is this field? check database
                                     # contents
146

147
    def autofill(self):
148
149
150
        info = get_pfam_info(self.pfam_acc)
        self.pfam_id = info['id']
        self.pfam_description = info['description']
Hervé  MENAGER's avatar
Hervé MENAGER committed
151

152
153
154
    def __str__(self):
        return '{} ({}-{})'.format(self.pfam_acc, self.pfam_id, self.pfam_description)

Hervé  MENAGER's avatar
Hervé MENAGER committed
155

156
class ProteinDomainComplex(models.Model):
157
158
    protein = models.ForeignKey('Protein', models.CASCADE)
    domain = models.ForeignKey('Domain', models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
159
160
161
    ppc_copy_nb = models.IntegerField(
        'Number of copies of the protein in the complex')

Hervé  MENAGER's avatar
Hervé MENAGER committed
162
163
    class Meta:
        verbose_name_plural = "complexes"
164

165
166
167
    def __str__(self):
        return '{}-{}'.format(self.protein_id, self.domain_id)

168
169
    def name(self):
        return self.protein.short_name
Hervé  MENAGER's avatar
Hervé MENAGER committed
170

171
class ProteinDomainBoundComplex(ProteinDomainComplex):
Hervé  MENAGER's avatar
Hervé MENAGER committed
172
173
174
    ppp_copy_nb_per_p = models.IntegerField(
        'Number of copies of the protein in the pocket')

175
176
    class Meta:
        verbose_name_plural = "bound complexes"
Hervé  MENAGER's avatar
Hervé MENAGER committed
177
178


179
class ProteinDomainPartnerComplex(ProteinDomainComplex):
Hervé  MENAGER's avatar
Hervé MENAGER committed
180

181
182
    class Meta:
        verbose_name_plural = "partner complexes"
Hervé  MENAGER's avatar
Hervé MENAGER committed
183

Hervé  MENAGER's avatar
Hervé MENAGER committed
184

185
186
187
class Symmetry(models.Model):
    code = models.CharField('Symmetry code', max_length=2)
    description = models.CharField('Description', max_length=300)
Hervé  MENAGER's avatar
Hervé MENAGER committed
188

189
190
191
    class Meta:
        verbose_name_plural = "symmetries"

192
193
194
    def __str__(self):
        return '{} ({})'.format(self.code, self.description)

195
196

class Disease(models.Model):
Hervé  MENAGER's avatar
Hervé MENAGER committed
197
198
    name = models.CharField('Disease', max_length=30, unique=True)
                            # is there any database/nomenclature for diseases?
199
200
201
202

    def __str__(self):
        return self.name

Hervé  MENAGER's avatar
Hervé MENAGER committed
203

204
class Ppi(models.Model):
205
    pdb_id = models.CharField('PDB ID', max_length=4, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
206
207
    pockets_nb = models.IntegerField(
        'Total number of pockets in the complex', default=1)
208
    symmetry = models.ForeignKey(Symmetry, models.CASCADE)
209
    diseases = models.ManyToManyField(Disease)
Hervé  MENAGER's avatar
Hervé MENAGER committed
210

211
212
213
    def __str__(self):
        return '{} PPI, PDB:{}'.format(self.symmetry.description, self.pdb_id or 'unknown')

214
215
216
217
218
219
220
221
222
223
    def get_ppi_complexes(self):
        """
        return all ppi complexes belonging to this ppi
        """
        return PpiComplex.objects.filter(ppi=self)

    def get_ppi_bound_complexes(self):
        """
        return bound ppi complexes belonging to this ppi
        """
Hervé  MENAGER's avatar
Hervé MENAGER committed
224
        # this is the less efficient query ever seen, FIXME
225
226
        return PpiComplex.objects.filter(ppi=self, complex__in=ProteinDomainBoundComplex.objects.all())

227
228
229
230
231
232
233
234
235
236
237
    @property
    def name(self):
        all_protein_names = set([ppi_complex.complex.protein.short_name for ppi_complex in self.get_ppi_complexes()])
        bound_protein_names = set([ppi_complex.complex.protein.short_name for ppi_complex in self.get_ppi_bound_complexes()])
        partner_protein_names = all_protein_names - bound_protein_names
        bound_str = ','.join(bound_protein_names)
        partner_str = ','.join(partner_protein_names)
        name = bound_str
        if partner_str!='':
            name += ' / ' + partner_str
        return name
238

Hervé  MENAGER's avatar
Hervé MENAGER committed
239
class PpiComplex(models.Model):
240
241
    ppi = models.ForeignKey(Ppi, models.CASCADE)
    complex = models.ForeignKey(ProteinDomainComplex, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
242
243
    cc_nb = models.IntegerField(
        'Number of copies of the complex in the PPI', default=1)
Hervé  MENAGER's avatar
Hervé MENAGER committed
244
245
246
247

    class Meta:
        verbose_name_plural = "Ppi complexes"

248
249
250
    def __str__(self):
        return 'PPI {}, Complex {} ({})'.format(self.ppi, self.complex, self.cc_nb)

251
class Compound(models.Model):
Hervé  MENAGER's avatar
Hervé MENAGER committed
252
253
254
255
256
257
258
    canonical_smile = models.CharField(
        'Canonical Smile', unique=True, max_length=500)
    is_macrocycle = models.BooleanField('Contains one or more macrocycles')
    aromatic_ratio = models.DecimalField(
        'Aromatic ratio', max_digits=3, decimal_places=2)
    balaban_index = models.DecimalField(
        'Balaban index', max_digits=3, decimal_places=2)
259
    fsp3 = models.DecimalField('Fsp3', max_digits=3, decimal_places=2)
Hervé  MENAGER's avatar
Hervé MENAGER committed
260
261
262
263
264
265
266
267
268
269
    gc_molar_refractivity = models.DecimalField(
        'GC Molar Refractivity', max_digits=5, decimal_places=2)
    log_d = models.DecimalField(
        'LogD (Partition coefficient octanol-1/water, with pKa information)', max_digits=4, decimal_places=2)
    a_log_p = models.DecimalField(
        'ALogP (Partition coefficient octanol-1/water)', max_digits=4, decimal_places=2)
    mean_atom_vol_vdw = models.DecimalField(
        'Mean atom volume computed with VdW radii', max_digits=4, decimal_places=2)
    molecular_weight = models.DecimalField(
        'Molecular weight', max_digits=6, decimal_places=2)
270
271
272
273
    nb_acceptor_h = models.IntegerField('Number of hydrogen bond acceptors')
    nb_aliphatic_amines = models.IntegerField('Number of aliphatics amines')
    nb_aromatic_bonds = models.IntegerField('Number of aromatic bonds')
    nb_aromatic_ether = models.IntegerField('Number of aromatic ethers')
Hervé  MENAGER's avatar
Hervé MENAGER committed
274
275
276
277
    nb_aromatic_sssr = models.IntegerField(
        'Number of aromatic Smallest Set of System Rings (SSSR)')
    nb_atom = models.IntegerField('Number of atoms')
    nb_atom_non_h = models.IntegerField('Number of non hydrogen atoms')
278
279
    nb_benzene_like_rings = models.IntegerField('Number of benzene-like rings')
    nb_bonds = models.IntegerField('Number of bonds')
Hervé  MENAGER's avatar
Hervé MENAGER committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    nb_bonds_non_h = models.IntegerField(
        'Number of bonds not involving a hydrogen')
    nb_br = models.IntegerField('Number of Bromine atoms')
    nb_c = models.IntegerField('Number of Carbon atoms')
    nb_chiral_centers = models.IntegerField('Number of chiral centers')
    nb_circuits = models.IntegerField('Number of circuits')
    nb_cl = models.IntegerField('Number of Chlorine atoms')
    nb_csp2 = models.IntegerField('Number of sp2-hybridized carbon atoms')
    nb_csp3 = models.IntegerField('Number of sp3-hybridized carbon atoms')
    nb_donor_h = models.IntegerField('Number of hydrogen bond donors')
    nb_double_bonds = models.IntegerField('Number of double bonds')
    nb_f = models.IntegerField('Number of fluorine atoms')
    nb_i = models.IntegerField('Number of iodine atoms')
    nb_multiple_bonds = models.IntegerField('Number of multiple bonds')
294
    nb_n = models.IntegerField('Number of nitrogen atoms')
Hervé  MENAGER's avatar
Hervé MENAGER committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    nb_o = models.IntegerField('Number of oxygen atoms')
    nb_rings = models.IntegerField('Number of rings')
    nb_rotatable_bonds = models.IntegerField('Number of rotatable bonds')
    randic_index = models.DecimalField(
        'Randic index', max_digits=4, decimal_places=2)
    rdf070m = models.DecimalField(
        'RDF070m, radial distribution function weighted by the atomic masses at 7Å', max_digits=5, decimal_places=2)
    rotatable_bond_fraction = models.DecimalField(
        'Fraction of rotatable bonds', max_digits=3, decimal_places=2)
    sum_atom_polar = models.DecimalField(
        'Sum of atomic polarizabilities', max_digits=5, decimal_places=2)
    sum_atom_vol_vdw = models.DecimalField(
        'Sum of atom volumes computed with VdW radii', max_digits=6, decimal_places=2)
    tpsa = models.DecimalField(
        'Topological Polar Surface Area (TPSA)', max_digits=5, decimal_places=2)
    ui = models.DecimalField(
        'Unsaturation index', max_digits=4, decimal_places=2)
    wiener_index = models.IntegerField('Wiener index')
    common_name = models.CharField(
        'Common name', unique=True, max_length=20, blank=True, null=True)
    pubchem_id = models.CharField(
        'Pubchem ID', max_length=10, blank=True, null=True)
    chemspider_id = models.CharField(
        'Chemspider ID', unique=True, max_length=10, blank=True, null=True)
    chembl_id = models.CharField(
        'Chembl ID', max_length=30, blank=True, null=True)
    iupac_name = models.CharField(
        'IUPAC name', max_length=255, blank=True, null=True)
    mddr_compound = models.ForeignKey(
324
        'MDDRCompoundImport', models.CASCADE, blank=True, null=True)
325

326
327
    objects = DataFrameManager()

Hervé  MENAGER's avatar
Hervé MENAGER committed
328
329
330
331
332
333
    @property
    def biblio_refs(self):
        """
        return all RefCompoundBiblio related to this complex
        """
        return RefCompoundBiblio.objects.filter(compound=self)
334

335
336
337
338
339
340
341
342
343
344
345
346
347
    @property
    def pfam_ids(self):
        """
        return all PFAM ids for the domain of the proteins of the bound
        complexes in the PPIs this compound has an action on
        """
        pfam_ids = set()
        for ca in self.compoundaction_set.all():
            ca.get_complexes()
            for bound_complex in ca.ppi.get_ppi_bound_complexes():
                pfam_ids.add(bound_complex.complex.domain.pfam_id)
        return pfam_ids

348
349
350
351
352
353
354
355
356
357
    @property
    def compound_action_pdb_ids(self):
        """
        return all PDB codes of the corresponding compound actions
        """
        pdb_ids = set()
        for ca in self.compoundaction_set.all():
            pdb_ids.add(ca.pdb_id)
        return pdb_ids

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    @property
    def hba_hbd(self):
        return self.nb_acceptor_h + self.nb_donor_h

    @property
    def lipinsky_mw(self):
        return self.molecular_weight <= 500

    @property
    def lipinsky_hba(self):
        return self.nb_acceptor_h <= 10

    @property
    def lipinsky_hbd(self):
        return self.nb_donor_h <= 5

    @property
    def lipinsky_a_log_p(self):
        return self.a_log_p <= 5

    @property
    def lipinsky_global(self):
        return int(self.lipinsky_mw) + int(self.lipinsky_hba) + \
            int(self.lipinsky_hbd) + int(self.lipinsky_a_log_p) >= 3

    @property
    def veber_hba_hbd(self):
        return self.nb_acceptor_h + self.nb_donor_h <= 12

    @property
    def veber_tpsa(self):
        return self.tpsa <= 140

    @property
    def veber_rb(self):
        return self.nb_rotatable_bonds <= 10

    @property
    def veber_global(self):
        return self.veber_rb and (self.veber_hba_hbd or self.veber_tpsa)

    @property
    def pfizer_a_log_p(self):
        return self.a_log_p <= 3

    @property
    def pfizer_tpsa(self):
        return self.tpsa >= 75

    @property
    def pfizer_global(self):
        return self.pfizer_a_log_p and self.pfizer_tpsa

411
412
    @property
    def best_pXC50_activity(self):
413
        return self.compoundactivityresult_set.aggregate(Max('activity'))['activity__max']
414
415
416
417
418
419

    @property
    def best_pXC50_compound_activity_result(self):
        best_pXC50_activity = self.best_pXC50_activity
        if best_pXC50_activity is None:
            return None
420
        return self.compoundactivityresult_set.filter(activity=best_pXC50_activity)[0]
421

422
423
424
425
426
    @property
    def le(self):
        """
        LE: Ligand Efficiency
        """
427
428
429
430
        best_pXC50_activity = self.best_pXC50_activity
        if best_pXC50_activity is None:
            return None
        return (1.37 * float(best_pXC50_activity))/self.nb_atom_non_h
431
432
433
434
435
436

    @property
    def lle(self):
        """
        LLE: Lipophilic Efficiency
        """
437
438
439
440
        best_pXC50_activity = self.best_pXC50_activity
        if best_pXC50_activity is None:
            return None
        return float(best_pXC50_activity - self.a_log_p)     
441

442
    @property
443
    def best_pXC50_activity_ppi_name(self):
444
445
446
        """
        LE: Ligand Efficiency
        """
447
448
449
        best_activity_car = self.best_pXC50_compound_activity_result
        if best_activity_car is None:
            return None
450
451
452
        ppi_name = best_activity_car.test_activity_description.ppi.name
        return ppi_name

453
454
455
class LeLleBiplotData(models.Model):
    le_lle_biplot_data = models.CharField('LE-LLE biplot JSON data', max_length=150000, blank=True, null=True)

456
class MDDRActivityClass(models.Model):
Hervé  MENAGER's avatar
Hervé MENAGER committed
457
    name = models.CharField('Activity Class', max_length=100, unique=True)
458
459
460
461
462

    class Meta:
        verbose_name_plural = "MDDR activity classes"

    def __str__(self):
463
        return self.name
464

Hervé  MENAGER's avatar
Hervé MENAGER committed
465

466
class MDDRCompoundImport(models.Model):
467
468

    MDDR_DEVELOPMENT_PHASES = (
Hervé  MENAGER's avatar
Hervé MENAGER committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
        ('Biological Testing', ''),
        ('Preclinical', ''),
        ('Phase III', ''),
        ('Phase II', ''),
        ('Phase I/II', ''),
        ('Phase I', ''),
        ('Launched', ''),
        ('Pre-Registered', ''),
        ('Not Applicable', ''),
        ('Discontinued', ''),
        ('Clinical', ''),
        ('Withdrawn', ''),
        ('Registered', ''),
        ('Not Determined', ''),
        ('Phase II/III', ''),
        ('IND Filed', ''),
485
486
    )

Hervé  MENAGER's avatar
Hervé MENAGER committed
487
488
489
490
491
492
493
    mddr_name = models.CharField('MDDR name', max_length=40)
    dvpmt_phase = models.CharField(
        'Development phase', max_length=20, choices=MDDR_DEVELOPMENT_PHASES)
    canonical_smile = models.CharField(
        'Canonical Smile', max_length=500, blank=True, null=True)
    # TODO index this table on canonical_smile
    db_import_date = models.DateTimeField('MDDR release year/month')
494
    activity_classes = models.ManyToManyField(MDDRActivityClass)
Hervé  MENAGER's avatar
Hervé MENAGER committed
495

496
497
    class Meta:
        # over multiple releases of the MDDR database, the same compound can evolve in its development phase
Hervé  MENAGER's avatar
Hervé MENAGER committed
498
499
        # the same compound can have different names and development phases in
        # the same MDDR release
500
        unique_together = (('mddr_name', 'dvpmt_phase', 'canonical_smile'),)
Hervé  MENAGER's avatar
Hervé MENAGER committed
501
        verbose_name_plural = "MDDR compound imports"
Hervé  MENAGER's avatar
Hervé MENAGER committed
502

503
504
505
    def __str__(self):
        return "{}, {}".format(self.mddr_name, self.dvpmt_phase)

Hervé  MENAGER's avatar
Hervé MENAGER committed
506

507
class MDDRSimilarity(models.Model):
Hervé  MENAGER's avatar
Hervé MENAGER committed
508
509
510
511
512
    canonical_smile_ippidb = models.CharField(
        'Canonical Smile for IPPIDB compound', max_length=500, unique=True, blank=True, null=True)
    canonical_smile_mddr = models.CharField(
        'Canonical Smile for MDDR Compound', max_length=500, unique=True, blank=True, null=True)
    tanimoto = models.DecimalField('Tanimoto', max_digits=6, decimal_places=5)
Hervé  MENAGER's avatar
Hervé MENAGER committed
513
514

    class Meta:
515
        unique_together = (('canonical_smile_ippidb', 'canonical_smile_mddr'),)
Hervé  MENAGER's avatar
Hervé MENAGER committed
516
        verbose_name_plural = "MDDR similarities"
517

Hervé  MENAGER's avatar
Hervé MENAGER committed
518

519
520
class CellLine(models.Model):
    name = models.CharField('Name', max_length=50, unique=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
521

522
523
524
    def __str__(self):
        return self.name

Hervé  MENAGER's avatar
Hervé MENAGER committed
525

526
527
528
529
530
531
532
533
534
535
class TestActivityDescription(models.Model):
    TEST_TYPES = (
        ('BIOCH', 'Biochemical assay'),
        ('CELL', 'Cellular assay')
    )
    TEST_MODULATION_TYPES = (
        ('B', 'Binding'),
        ('I', 'Inhibition'),
        ('S', 'Stabilization')
    )
536
537
538
539
    PROTEIN_BOUND_CONSTRUCTS = (
        ('F', 'Full length'),
        ('U', 'Unspecified')
    )
540
    biblio = models.ForeignKey(Bibliography, models.CASCADE)
541
    protein_domain_bound_complex = models.ForeignKey(ProteinDomainBoundComplex, models.CASCADE)
542
    ppi = models.ForeignKey(Ppi, models.CASCADE, blank=True, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
543
    test_name = models.CharField('Test name', max_length=100)
544
545
    is_primary = models.BooleanField('Is primary')
    protein_bound_construct = models.CharField('Protein bound construct', max_length=5, choices=PROTEIN_BOUND_CONSTRUCTS, blank=True, null=True)
546
    test_type = models.CharField('Test type', max_length=5, choices=TEST_TYPES)
Hervé  MENAGER's avatar
Hervé MENAGER committed
547
548
549
550
    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')
551
    cell_line = models.ForeignKey(CellLine, models.CASCADE, blank=True, null=True)
552

Hervé  MENAGER's avatar
Hervé MENAGER committed
553
    def get_complexes(self):
554
555
556
557
        """
        get the complexes tested for this PPI
        depends on the modulation type
        """
Hervé  MENAGER's avatar
Hervé MENAGER committed
558
        if self.test_modulation_type == 'I':
559
560
561
            return self.ppi.get_ppi_complexes()
        else:
            return self.ppi.get_ppi_bound_complexes()
562

Hervé  MENAGER's avatar
Hervé MENAGER committed
563

564
class CompoundActivityResult(models.Model):
565
566
567
568
569
    MODULATION_TYPES = (
        ('B', 'Binding'),
        ('I', 'Inhibition'),
        ('S', 'Stabilization')
    )
Hervé  MENAGER's avatar
Hervé MENAGER committed
570
    ACTIVITY_TYPES = (
Hervé  MENAGER's avatar
Hervé MENAGER committed
571
572
573
574
        ('pIC50', 'pIC50 (half maximal inhibitory concentration, -log10)'),
        ('pEC50', 'pEC50 (half maximal effective concentration, -log10)'),
        ('pKd', 'pKd (dissociation constant, -log10)'),
        ('pKi', 'pKi (inhibition constant, -log10)'),
Hervé  MENAGER's avatar
Hervé MENAGER committed
575
    )
576
577
    compound = models.ForeignKey(Compound, models.CASCADE)
    test_activity_description = models.ForeignKey(TestActivityDescription, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
578
579
580
581
    activity_type = models.CharField(
        'Activity type', max_length=5, choices=ACTIVITY_TYPES)
    activity = models.DecimalField(
        'Activity', max_digits=12, decimal_places=10)
582
583
    inhibition_percentage = models.DecimalField(
        'Inhibition percentage', max_digits=3, decimal_places=0, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
584
585
    modulation_type = models.CharField(
        'Modulation type', max_length=1, choices=MODULATION_TYPES)
Hervé  MENAGER's avatar
Hervé MENAGER committed
586
587

    class Meta:
Hervé  MENAGER's avatar
Hervé MENAGER committed
588
589
590
        unique_together = (
            ('compound', 'test_activity_description', 'activity_type'),)

591
592
    def __str__(self):
        return 'Compound activity result for {} test {} on {}'.format(self.activity_type, self.test_activity_description.id, self.compound.id)
Hervé  MENAGER's avatar
Hervé MENAGER committed
593

594
class TestCytotoxDescription(models.Model):
595
    biblio = models.ForeignKey(Bibliography, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
596
    test_name = models.CharField('Cytotoxicity test name', max_length=100)
597
    cell_line = models.ForeignKey(CellLine, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
598
599
600
    compound_concentration = models.DecimalField(
        'Compound concentration in μM', max_digits=7, decimal_places=3, blank=True, null=True)

Hervé  MENAGER's avatar
Hervé MENAGER committed
601

602
class CompoundCytotoxicityResult(models.Model):
603
604
    compound = models.ForeignKey(Compound, models.CASCADE)
    test_cytotoxicity_description = models.ForeignKey(TestCytotoxDescription, models.CASCADE)
605
    toxicity = models.BooleanField('Toxicity', default=False)
Hervé  MENAGER's avatar
Hervé MENAGER committed
606
607

    class Meta:
608
        unique_together = (('compound', 'test_cytotoxicity_description'),)
609

610
611
612
    def __str__(self):
        return 'Compound cytotoxicity result for test {} on {}'.format(self.test_cytotoxicity_description.id, self.compound.id)

Hervé  MENAGER's avatar
Hervé MENAGER committed
613

614
class TestPKDescription(models.Model):
615
616
617
618
619
620
    ADMINISTRATION_MODES = (
        ('IV', ''),
        ('PO', ''),
        ('IP', ''),
        ('SL', 'SL')
    )
621
    biblio = models.ForeignKey(Bibliography, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
622
    test_name = models.CharField('Pharmacokinetic test name', max_length=100)
623
    organism = models.ForeignKey(Taxonomy, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
624
625
    administration_mode = models.CharField(
        'Administration mode', max_length=2, choices=ADMINISTRATION_MODES, blank=True, null=True)
626
627
    concentration = models.DecimalField(
        'Concentration in mg/l', max_digits=7, decimal_places=3, blank=True, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
628
    dose = models.DecimalField(
629
        'Dose in mg/kg', max_digits=9, decimal_places=4, blank=True, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
630
631
632
    dose_interval = models.IntegerField(
        'Dose interval, in hours', blank=True, null=True)

633
634

class CompoundPKResult(models.Model):
635
636
    compound = models.ForeignKey(Compound, models.CASCADE)
    test_pk_description = models.ForeignKey(TestPKDescription, models.CASCADE)
637
    tolerated = models.NullBooleanField('Tolerated', null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
638
639
640
641
642
643
644
645
646
647
648
649
    auc = models.IntegerField(
        'Area under curve (ng.mL-1.hr)', blank=True, null=True)
    clearance = models.DecimalField(
        'Clearance (mL/hr)', max_digits=7, decimal_places=3, blank=True, null=True)
    cmax = models.DecimalField(
        'Maximal concentration (ng/mL)', max_digits=7, decimal_places=3, blank=True, null=True)
    oral_bioavailability = models.IntegerField(
        'Oral Bioavailability (%F)', blank=True, null=True)
    t_demi = models.IntegerField('t½', blank=True, null=True)
    t_max = models.IntegerField('tmax', blank=True, null=True)
    voldistribution = models.DecimalField(
        'Volume distribution (Vd)', max_digits=5, decimal_places=2, blank=True, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
650
651

    class Meta:
652
        unique_together = (('compound', 'test_pk_description'),)
Hervé  MENAGER's avatar
Hervé MENAGER committed
653

654
655
656
    def __str__(self):
        return 'Compound PK result for test {} on {}'.format(self.test_pk_description.id, self.compound.id)

Hervé  MENAGER's avatar
Hervé MENAGER committed
657

658
class CompoundAction(models.Model):
659
660
    ACTIVATION_MODES = (
        ('O', 'Orthosteric'),
661
662
        ('A', 'Allosteric'),
        ('U', 'Unspecified')
663
    )
664
    compound = models.ForeignKey(Compound, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
665
666
    activation_mode = models.CharField(
        'Activation mode', max_length=1, choices=ACTIVATION_MODES)
667
    ppi = models.ForeignKey(Ppi, models.CASCADE)
668
    pdb_id = models.CharField('PDB ID', max_length=4, blank=True, null=True)
Hervé  MENAGER's avatar
Hervé MENAGER committed
669
670
    nb_copy_compounds = models.IntegerField(
        'Number of copies for the compound')
Hervé  MENAGER's avatar
Hervé MENAGER committed
671
672

    class Meta:
Hervé  MENAGER's avatar
Hervé MENAGER committed
673
674
675
        unique_together = (('ppi', 'compound', 'activation_mode', 'pdb_id'),)

    def get_complexes(self):
676
677
678
679
        """
        get the complexes involved in the compound action
        which are always the bound complexes
        """
680
        return self.ppi.get_ppi_bound_complexes()
Hervé  MENAGER's avatar
Hervé MENAGER committed
681

Hervé  MENAGER's avatar
Hervé MENAGER committed
682

683
class RefCompoundBiblio(models.Model):
684
685
    compound = models.ForeignKey(Compound, models.CASCADE)
    bibliography = models.ForeignKey(Bibliography, models.CASCADE)
Hervé  MENAGER's avatar
Hervé MENAGER committed
686
687
    compound_name = models.CharField(
        'Compound name in the publication', max_length=50)
Hervé  MENAGER's avatar
Hervé MENAGER committed
688
689

    class Meta:
690
        unique_together = (('compound', 'bibliography'),)