Commit 543dba4b authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

formatting models

parent 5826d95b
Pipeline #9262 passed with stages
in 11 minutes and 15 seconds
......@@ -202,6 +202,7 @@ class Domain(AutoFillableModel):
pfam_id = models.CharField('Pfam Family Identifier', max_length=20)
pfam_description = models.CharField('Pfam Description', max_length=100)
domain_family = models.CharField('Domain family', max_length=25)
# TODO: what is this field? check database
# contents
......@@ -224,7 +225,7 @@ class Domain(AutoFillableModel):
class ProteinDomainComplex(models.Model):
"""
Protein-Domain association
Protein-Domain association
"""
protein = models.ForeignKey('Protein', models.CASCADE)
domain = models.ForeignKey('Domain', models.CASCADE)
......@@ -277,11 +278,13 @@ class Symmetry(models.Model):
class Disease(models.Model):
name = models.CharField('Disease', max_length=30, unique=True)
# is there any database/nomenclature for diseases?
def __str__(self):
return self.name
class PpiFamily(models.Model):
"""
PPI Family
......@@ -358,11 +361,12 @@ class CompoundManager(models.Manager):
"""
def get_queryset(self):
# @formatter:off
qs = super().get_queryset()
# with number of publications
qs = qs.annotate(pubs=Count('refcompoundbiblio', distinct=True))
# with best activity
qs = qs.annotate(best_activity=Max('compoundactivityresult__activity'))
qs = qs.annotate(best_activity=Max('compoundactivityresult__activity'))
# with LE
qs = qs.annotate(le=Cast(1.37 * Max('compoundactivityresult__activity') / F('nb_atom_non_h'), FloatField()))
# with LLE
......@@ -418,13 +422,15 @@ class CompoundManager(models.Manager):
qs = qs.annotate(cytoxtest_av=Cast(Max(Case(When(refcompoundbiblio__bibliography__cytotox=True, then=1), default=0, output_field=IntegerField())), BooleanField()))
# in silico st performed
qs = qs.annotate(insilico_av=Cast(Max(Case(When(refcompoundbiblio__bibliography__in_silico=True, then=1), default=0, output_field=IntegerField())), BooleanField()))
#@formatter:on
return qs
class Compound(AutoFillableModel):
"""
Chemical compound
"""
objects = CompoundManager()
objects = CompoundManager()
canonical_smile = models.TextField(
'Canonical Smile', unique=True)
is_macrocycle = models.BooleanField('Contains one or more macrocycles')
......@@ -500,20 +506,24 @@ class Compound(AutoFillableModel):
'IUPAC name', max_length=255, blank=True, null=True)
class Meta:
ordering = ['id']
ordering = ['id']
def compute_drugbank_compound_similarity(self):
""" compute Tanimoto similarity to existing DrugBank compounds """
self.save()
# fingerprints to compute drugbank similarities are in settings module, default FP2
fingerprinter = FingerPrinter(getattr(settings, "DRUGBANK_FINGERPRINTS", "FP2"))
#1. compute tanimoto for SMILES query vs all compounds
smiles_dict = {c.id:c.canonical_smiles for c in DrugBankCompound.objects.all()}
# 1. compute tanimoto for SMILES query vs all compounds
smiles_dict = {c.id: c.canonical_smiles for c in DrugBankCompound.objects.all()}
tanimoto_dict = fingerprinter.tanimoto_smiles(self.canonical_smile, smiles_dict)
tanimoto_dict = dict(sorted(tanimoto_dict.items(), key=operator.itemgetter(1), reverse=True)[:15])
dbcts = []
for id_, tanimoto in tanimoto_dict.items():
dbcts.append(DrugbankCompoundTanimoto(compound=self, drugbank_compound=DrugBankCompound.objects.get(id=id_), tanimoto=tanimoto))
dbcts.append(DrugbankCompoundTanimoto(
compound=self,
drugbank_compound=DrugBankCompound.objects.get(id=id_),
tanimoto=tanimoto,
))
DrugbankCompoundTanimoto.objects.bulk_create(dbcts)
@property
......@@ -599,11 +609,11 @@ class Compound(AutoFillableModel):
return the all PPI families for PPIs involved in the compound activity of the compound
"""
return list(set([ca.ppi.family for ca in self.compoundaction_set.all()]))
@property
def sorted_similar_drugbank_compounds(self):
return self.drugbankcompoundtanimoto_set.order_by('-tanimoto')
def autofill(self):
# compute InChi and InChiKey
self.inchi = smi2inchi(self.canonical_smile)
......@@ -613,6 +623,7 @@ class Compound(AutoFillableModel):
def __str__(self):
return 'Compound #{}'.format(self.id)
class CompoundTanimoto(models.Model):
canonical_smiles = models.TextField(
'Canonical Smile')
......@@ -625,22 +636,25 @@ class CompoundTanimoto(models.Model):
unique_together = (
('canonical_smiles', 'fingerprint', 'compound'))
def create_tanimoto(smiles_query, fingerprint):
"""
Compute the Tanimoto similarity between a given SMILES and the compounds
then insert the results in CompoundTanimoto
"""
if CompoundTanimoto.objects.filter(canonical_smiles=smiles_query, fingerprint=fingerprint).count()==0:
smiles_dict = {c.id:c.canonical_smile for c in Compound.objects.all()}
if CompoundTanimoto.objects.filter(canonical_smiles=smiles_query, fingerprint=fingerprint).count() == 0:
smiles_dict = {c.id: c.canonical_smile for c in Compound.objects.all()}
fingerprinter = FingerPrinter(fingerprint)
#1. compute tanimoto for SMILES query vs all compounds
# 1. compute tanimoto for SMILES query vs all compounds
tanimoto_dict = fingerprinter.tanimoto_smiles(smiles_query, smiles_dict)
#2. insert results in a table with three fields: SMILES query, compound id, tanimoto index
# 2. insert results in a table with three fields: SMILES query, compound id, tanimoto index
cts = []
for id_, smiles in smiles_dict.items():
cts.append(CompoundTanimoto(canonical_smiles=smiles_query, fingerprint=fingerprint, compound=Compound.objects.get(id=id_), tanimoto=tanimoto_dict[id_]))
cts.append(CompoundTanimoto(canonical_smiles=smiles_query, fingerprint=fingerprint,
compound=Compound.objects.get(id=id_), tanimoto=tanimoto_dict[id_]))
CompoundTanimoto.objects.bulk_create(cts)
class PcaBiplotData(models.Model):
"""
PCA biplot data
......@@ -686,21 +700,48 @@ class TestActivityDescription(models.Model):
('F', 'Full length'),
('U', 'Unspecified')
)
biblio = models.ForeignKey(Bibliography, models.CASCADE)
biblio = models.ForeignKey(
Bibliography,
on_delete=models.CASCADE,
)
protein_domain_bound_complex = models.ForeignKey(
ProteinDomainBoundComplex, models.CASCADE)
ProteinDomainBoundComplex,
on_delete=models.CASCADE,
)
ppi = models.ForeignKey(Ppi, models.CASCADE, blank=True, null=True)
test_name = models.CharField('Test name', max_length=100)
is_primary = models.BooleanField('Is primary')
test_name = models.CharField(
verbose_name='Test name',
max_length=100,
)
is_primary = models.BooleanField(
verbose_name='Is primary',
)
protein_bound_construct = models.CharField(
'Protein bound construct', max_length=5, choices=PROTEIN_BOUND_CONSTRUCTS, blank=True, null=True)
test_type = models.CharField('Test type', max_length=5, choices=TEST_TYPES)
verbose_name='Protein bound construct',
max_length=5,
choices=PROTEIN_BOUND_CONSTRUCTS,
blank=True,
null=True,
)
test_type = models.CharField(
verbose_name='Test type',
max_length=5,
choices=TEST_TYPES,
)
test_modulation_type = models.CharField(
'Test modulation type', max_length=1, choices=TEST_MODULATION_TYPES)
verbose_name='Test modulation type',
max_length=1,
choices=TEST_MODULATION_TYPES,
)
nb_active_compounds = models.IntegerField(
'Total number of active compounds')
verbose_name='Total number of active compounds',
)
cell_line = models.ForeignKey(
CellLine, models.CASCADE, blank=True, null=True)
CellLine,
on_delete=models.CASCADE,
blank=True,
null=True,
)
def get_complexes(self):
"""
......@@ -758,7 +799,11 @@ class CompoundActivityResult(models.Model):
('compound', 'test_activity_description', 'activity_type'),)
def __str__(self):
return 'Compound activity result for {} test {} on {}'.format(self.activity_type, self.test_activity_description.id, self.compound.id)
return 'Compound activity result for {} test {} on {}'.format(
self.activity_type,
self.test_activity_description.id,
self.compound.id,
)
def is_best(self):
return self.compound.best_pXC50_compound_activity_result.id == self.id
......@@ -788,7 +833,10 @@ class CompoundCytotoxicityResult(models.Model):
unique_together = (('compound', 'test_cytotoxicity_description'),)
def __str__(self):
return 'Compound cytotoxicity result for test {} on {}'.format(self.test_cytotoxicity_description.id, self.compound.id)
return 'Compound cytotoxicity result for test {} on {}'.format(
self.test_cytotoxicity_description.id,
self.compound.id,
)
class TestPKDescription(models.Model):
......@@ -871,6 +919,7 @@ class CompoundAction(models.Model):
def __str__(self):
return 'Action of {} on {}'.format(self.compound, self.ppi)
class RefCompoundBiblio(models.Model):
"""
Compound-Bibliographic reference association
......@@ -883,6 +932,7 @@ class RefCompoundBiblio(models.Model):
class Meta:
unique_together = (('compound', 'bibliography'),)
class DrugBankCompound(models.Model):
"""
Drugbank compound
......@@ -893,6 +943,7 @@ class DrugBankCompound(models.Model):
canonical_smiles = models.TextField(
'Canonical SMILES')
class DrugbankCompoundTanimoto(models.Model):
"""
Drugbank compound-compound tanimoto similarity
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment