Commit df77b021 authored by Hervé  MENAGER's avatar Hervé MENAGER
Browse files

add tanimoto similarity to the compounds list.


Former-commit-id: 7bb99ec1f757c6e9ee91fa5148aae3da9a85f702
parent 0584b448
......@@ -2,6 +2,9 @@
<div class="col-sm-3 border-info d-flex justify-content-center align-content-center">
<a href="/compounds/{{ compound.id }}">{% include "compound_smiles_draw.html" with id=compound.id smile=compound.canonical_smile %}</a>
<h2 class="position-absolute" style="top:0.3em; left:0.3em;"><span class="badge badge-dark"><a href="/compounds/{{ compound.id }}">{{ compound.id }}</a></span></h2>
{% if compound.tanimoto %}
<h2 class="position-absolute" style="top:0.3em; right:0.3em;" title="tanimoto similarity value"><span class="badge badge-dark text-warning">{{ compound.tanimoto }}</span></h2>
{% endif %}
</div>
<div class="col-sm-9">
<ul class="list-group">
......
<table class="table table-striped">
<thead>
<tr>
{% include "compound_t_colhead.html" with col_id="tanimoto" col_name="Tanimoto" col_title="Tanimoto similarity" %}
{% include "compound_t_colhead.html" with col_id="id" col_name="#" col_title="Compound's iPPIDB ID" %}
{% include "compound_t_colhead.html" with col_id="canonical_smiles" col_name="Structure" %}
{% include "compound_t_colhead.html" with col_id="common_name" col_name="common name" %}
......@@ -28,6 +29,9 @@
<tbody>
{% for compound in compounds %}
<tr>
{% if compound.tanimoto %}
<td title="tanimoto similarity value">{{ compound.tanimoto }}</td>
{% endif %}
<td scope="col"><span class="badge badge-dark"><a href="/compounds/{{ compound.id }}">{{ compound.id }}</a></span></td>
{% if "canonical_smiles" in fields %}
<td scope="col" style="width:150px">{% include "compound_smiles_draw.html" with id=compound.id smile=compound.canonical_smile width="150" height="150"%}</td>
......
......@@ -2,6 +2,9 @@
<div class="card border border-info">
<a href="/compounds/{{ compound.id }}">{% include "compound_smiles_draw.html" with id=compound.id smile=compound.canonical_smile %}</a>
<h2 class="position-absolute" style="top:0.3em; left:0.3em;"><span class="badge badge-dark"><a href="/compounds/{{ compound.id }}">{{ compound.id }}</a></span></h2>
{% if compound.tanimoto %}
<h2 class="position-absolute" style="top:0.3em; right:0.3em;" title="tanimoto similarity value"><span class="badge badge-dark text-warning">{{ compound.tanimoto }}</span></h2>
{% endif %}
<div class="card-body" style="overflow:hidden; word-wrap:break-word;">
{%if compound.common_name %}<p title="Common name">{{ compound.common_name }}</p>{% endif %}
<p title="PPI Family">{{ compound.best_pXC50_activity_ppi_family }}</p>
......
......@@ -2,7 +2,7 @@ import json
import math
from collections import OrderedDict
from django.db.models import Max, Min, Count
from django.db.models import Max, Min, Count, F
from django.shortcuts import render
from django.urls import reverse
from django.http import HttpResponseRedirect, Http404, JsonResponse
......@@ -15,7 +15,7 @@ from formtools.wizard.views import SessionWizardView, NamedUrlSessionWizardView
import ippidb
from .forms import *
from .utils import mol2smi
from .models import Protein, Bibliography, ProteinDomainComplex, ProteinDomainBoundComplex, RefCompoundBiblio, TestActivityDescription, Compound, Ppi, Disease, Taxonomy, LeLleBiplotData, PcaBiplotData, PpiFamily
from .models import Protein, Bibliography, ProteinDomainComplex, ProteinDomainBoundComplex, RefCompoundBiblio, TestActivityDescription, Compound, Ppi, Disease, Taxonomy, LeLleBiplotData, PcaBiplotData, PpiFamily, CompoundTanimoto, create_tanimoto
from .ws import get_pdb_uniprot_mapping
......@@ -202,6 +202,11 @@ class CompoundSimilarityFilterHandler(object):
""" to be called during queryset filtering """
if self.value:
self.filter_context[self.parameter_name] = self.value
#FIXME: test if exists before running
create_tanimoto(self.filter_context[self.parameter_name])
queryset = queryset.filter(compoundtanimoto__canonical_smiles=self.filter_context[self.parameter_name]).annotate(tanimoto=F('compoundtanimoto__tanimoto'))
# TODO: determine what should be done with tanimoto: only sort, or filter on n most similar
# or above a given cutoff?
return queryset
def post_process(self, compound_ids, queryset):
......@@ -255,8 +260,13 @@ class CompoundListView(ListView):
sort_by_option_ids = ['id', 'molecular_weight', 'a_log_p', 'nb_aromatic_sssr', 'nb_chiral_centers', 'pubs']
def get_ordering(self):
sort_by = self.request.GET.get('sort_by', 'id')
return sort_by
if 'similar_to' in self.request.GET:
return '-tanimoto'
else:
sort_by = self.request.GET.get('sort_by', 'id')
if sort_by not in self.sort_by_option_ids:
sort_by = 'id'
return sort_by
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
......@@ -266,22 +276,27 @@ class CompoundListView(ListView):
display = 'v'
context['display']=display
# displayed fields in "table" mode
fields = self.request.GET.getlist('fields', self.table_view_default_fields)
fields = self.request.GET.getlist('fields', self.table_view_default_fields.copy())
if 'id' not in fields:
fields.append('id')
if 'similar_to' in self.request.GET:
fields.append('tanimoto')
context['fields'] = fields
# filters
context.update(self.filter_context)
# sort by options
compound_fields = {f.name: f.verbose_name for f in Compound._meta.get_fields() if not(f.is_relation)}
sort_by_options = OrderedDict()
for sort_by_option_id in self.sort_by_option_ids:
if sort_by_option_id == 'pubs':
name = 'Number of publications'
else:
name = compound_fields.get(sort_by_option_id)
sort_by_options[sort_by_option_id] = {'name': name, 'order':'ascending', 'id': sort_by_option_id}
sort_by_options['-'+sort_by_option_id] = {'name': name, 'order':'descending', 'id': sort_by_option_id}
if 'similar_to' in self.request.GET:
sort_by_options['-tanimoto'] = {'name': 'Tanimoto similarity', 'order':'descending', 'id': 'tanimoto'}
else:
for sort_by_option_id in self.sort_by_option_ids:
if sort_by_option_id == 'pubs':
name = 'Number of publications'
else:
name = compound_fields.get(sort_by_option_id)
sort_by_options[sort_by_option_id] = {'name': name, 'order':'ascending', 'id': sort_by_option_id}
sort_by_options['-'+sort_by_option_id] = {'name': name, 'order':'descending', 'id': sort_by_option_id}
context['sort_by'] = self.get_ordering()
context['sort_by_options'] = sort_by_options
# return context
......
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