Hello Riccardo, Adrian , Greg and everyone else,
Thank you for all the replies:
I like the mol_from_pkl that greg pointed out
Thanks for pointers to the razi project ...I had looked at sqlalchemy but
decided to go with pure django ORM and then use raw() calls to get the
models from the database.
I was super excited when I saw the postgres support kickstarter on hacker
news and backed it ..its great to see that it will hopefully become a
reality and that will be great for the rdkit cartridge.
Thanks to django and the rdkit postgres cartridge I have managed to get a
reasonable working django project with the following:
1) A custom field that links the cartridge "mol" type to my django model (
feedback appreciated)
2) With a database trigger, I create the rdkit "mol" from the smiles text
model attribute or SQL column everytime an insert or update happens.
3) All of my django views that will use substructure and smarts lookups can
be done with raw() calls that return querysets to be handled in django.
Here is some of the code that I have cobbled together ( see below). Any
feedback is greatly appreciated. A lot of the code has hard baked column
names and model field names..as I go ahead I hope to make the code less
hard-wired. But I am happy that the rdkit cartridge makes it so easy to use
with Django and raw() calls.
With the model below. I can run examples like
pyridines = compound.objects.raw("select rdkit_mol from strucinfo_compound
where rdkit_mol @>'c1ccncc1'")
And that works pretty well for my views
Thanks again for all your suggestions and feedback.
Hari
My code is pasted here:
# A south migration that defines a function called write_rdkit_mol_south in
PL/PGSQL
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
db.execute("""create function write_rdkit_mol_south() RETURNS
trigger as $write_rdkit_mol_south$
BEGIN
NEW.rdkit_mol := mol_from_smiles(NEW.smiles::cstring);
RETURN NEW;
END;
$write_rdkit_mol_south$ LANGUAGE plpgsql;""")
db.execute(
"create TRIGGER write_rdkit_mol_trig BEFORE INSERT OR UPDATE on
strucinfo_compound FOR EACH ROW EXECUTE PROCEDURE
write_rdkit_mol_south();")
# Note: Don't use "from appname.models import ModelName".
# Use orm.ModelName to refer to models in this application,
# and orm['appname.ModelName'] for models in other applications.
def backwards(self, orm):
"Write your backwards methods here."
db.execute("drop TRIGGER write_rdkit_mol_trig ON
strucinfo_compound;")
db.execute("DROP FUNCTION write_rdkit_mol_south();")
# My Django model:
class compound(models.Model):
internalid = models.CharField(max_length=10 ,db_index=True)
externalid = models.CharField(max_length=15,db_index=True)
smiles = models.TextField()
rdkit_mol = RdkitMolField()
def save(self,*args,**kwargs):
self.rdkit_mol = ""
super(compound,self).save(*args,**kwargs)
# The custom field
class RdkitMolField(models.Field):
description = "Rdkit molecule field"
def __init__(self,*args,**kwds):
super(RdkitMolField,self).__init__(*args,**kwds)
def db_type(self, connection):
if connection.settings_dict['ENGINE'] ==
'django.db.backends.postgresql_psycopg2':
return None
else:
raise DatabaseError('Field type only supported for Postgres
with rdkit cartridge')
def to_python(self, value):
if isinstance(value,Chem.Mol):
return value
if isinstance(value,basestring):
# The database normally returns the smiles string
return Chem.MolFromSmiles(str(value))
else:
if value:
#if mol_send was used then we will have a pickled object
return Chem.Mol(str(value))
else:
# The None Case
return "NO MOL"
def get_prep_value(self, value):
# This gets called during save
# the method should return data in a format that has been prepared
for use as a parameter in a query : say the docs
# rdkit_mol queries do accept smiles strings
if isinstance(value,basestring):
db_smiles = str(value)
if db_smiles:
my_mol = Chem.MolFromSmiles(db_smiles)
else:
return None
if my_mol:
# Roundtrip with object could be avoided
return str(Chem.MolToSmiles(my_mol))
elif isinstance(value,(str,unicode)):
valid_smiles =
str(Chem.MolToSmiles(Chem.MolFromSmiles(str(value))))
if valid_smiles:
return valid_smiles
else:
# This is the None case
# The database trigger will handle this as this should
happen only during insert or update
return None
def validate(self, value, model_instance):
# This field is handled by database trigger so we do not want it to
be used for object initiation
if value is None:
return
else:
super(RdkitMolField,self).validate(value,model_instance)
On Fri, Feb 28, 2014 at 5:15 PM, Riccardo Vianello <
[email protected]> wrote:
> On Wed, Feb 26, 2014 at 7:26 PM, Adrian Jasiński <
> [email protected]> wrote:
>
>> you can try use razi:
>>
>
> Unfortunately, razi is currenly a bit old and unmaintained. Moreover it's
> based on sqlalchemy and it wouldn't therefore integrate easily with the
> django database api. One might consider having a look at django-chem, but
> that is also quite old (iirc it targeted django 1.3), and it's based on an
> implementation approach that I think is becoming in part obsolete.
>
> As it has been also mentioned in reply to Hari's question on
> stackoverflow, the upcoming version 1.7 of django will support custom
> lookup operators. This is likely to help, but I also partly agree that
> extending the database api is still quite a lot of work compared to the
> fraction of domain-specific queries, and a single application project could
> just consider using raw SQL queries in the few places where it is needed.
>
> Of course things are much simpler if one doesn't need/want to abstract the
> api from the database backend and it could be also of interest that there's
> currently a kickstarter project which is aimed at contributing extended
> postgresql support to django:
> https://www.kickstarter.com/projects/mjtamlyn/improved-postgresql-support-in-django
> .
>
> Best regards,
> Riccardo
>
>
>
>>
>> http://razi.readthedocs.org/en/latest/database_creation_tutorial.html
>>
>> pozdrawiam
>> Adrian
>>
>>
>> 2014-02-26 18:34 GMT+01:00 Greg Landrum <[email protected]>:
>>
>> Hi Hari,
>>>
>>> You can use "mol_from_pkl()". Here's a quick demo:
>>> In [20]: m = Chem.MolFromSmiles('c1cnccc1')
>>>
>>> In [21]: pkl = buffer(m.ToBinary())
>>>
>>> In [22]: curs.execute('select mol_from_pkl(%s)',(pkl,))
>>>
>>> In [23]: curs.fetchone()
>>> Out[23]: ('c1ccncc1',)
>>>
>>> Is that enough to get you started?
>>> -greg
>>>
>>>
>>>
>>> On Tue, Feb 25, 2014 at 8:28 PM, hari jayaram <[email protected]> wrote:
>>>
>>>> Hi ,
>>>> I am a newbie to postgres and the rdkit postgres database cartridge.
>>>>
>>>> I have gotten the cartridge installed and can query sub-structure
>>>> similarity and do other things as documented in the cartridge documentation
>>>> page.
>>>>
>>>> To create a molecule from the cartridge the documentation
>>>> <http://www.rdkit.org/docs/Cartridge.html>recommends something like:
>>>>
>>>>
>>>> >>> curs.execute('select molregno,mol_send(m) from rdk.mols where m@
>>>> >%s',('c1cccc2c1nncc2',))
>>>> >>> row = curs.fetchone()
>>>> >>> row
>>>> (9830, <read-only buffer for 0x...>)
>>>>
>>>> Then the pickled output of mol_send will be used to create a new
>>>> molecule
>>>>
>>>> >>> from rdkit import Chem
>>>> >>> m = Chem.Mol(str(row[1]))
>>>> >>> Chem.MolToSmiles(m,True)
>>>> 'CC(C)Sc1ccc(CC2CCN(C3CCN(C(=O)c4cnnc5ccccc54)CC3)CC2)cc1'
>>>>
>>>>
>>>> My question is : how about the other way .Is the only way to use the
>>>> smiles and do an sql insert with the smiles and the mol_from_smiles
>>>> function as in :
>>>>
>>>> insert into compounds (smiles,rdkit_mol,internal,external) VALUES
>>>> ('CCCCC',mol_from_smiles('CCCCC);'ID-1111111','EI-2222222');
>>>>
>>>> Is there a way to insert the python rdkit mol object into the database
>>>> rdkit "mol" column directly.
>>>>
>>>> This is in some ways connected to a question I just asked ( django
>>>> related) on stack
>>>> overflow.<http://stackoverflow.com/questions/22022163/how-to-handle-map-custom-postgresql-type-to-django-model>
>>>>
>>>> Thanks a tonne for your help in advance
>>>> Hari
>>>>
>>>>
>>>>
>>>>
>>>> ------------------------------------------------------------------------------
>>>> Flow-based real-time traffic analytics software. Cisco certified tool.
>>>> Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
>>>> Customize your own dashboards, set traffic alerts and generate reports.
>>>> Network behavioral analysis & security monitoring. All-in-one tool.
>>>>
>>>> http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
>>>> _______________________________________________
>>>> Rdkit-discuss mailing list
>>>> [email protected]
>>>> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>>>>
>>>>
>>>
>>>
>>> ------------------------------------------------------------------------------
>>> Flow-based real-time traffic analytics software. Cisco certified tool.
>>> Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
>>> Customize your own dashboards, set traffic alerts and generate reports.
>>> Network behavioral analysis & security monitoring. All-in-one tool.
>>>
>>> http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
>>> _______________________________________________
>>> Rdkit-discuss mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>>>
>>>
>>
>>
>> ------------------------------------------------------------------------------
>> Flow-based real-time traffic analytics software. Cisco certified tool.
>> Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
>> Customize your own dashboards, set traffic alerts and generate reports.
>> Network behavioral analysis & security monitoring. All-in-one tool.
>>
>> http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
>> _______________________________________________
>> Rdkit-discuss mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>>
>>
>
>
> ------------------------------------------------------------------------------
> Flow-based real-time traffic analytics software. Cisco certified tool.
> Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
> Customize your own dashboards, set traffic alerts and generate reports.
> Network behavioral analysis & security monitoring. All-in-one tool.
>
> http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
> _______________________________________________
> Rdkit-discuss mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
>
------------------------------------------------------------------------------
Flow-based real-time traffic analytics software. Cisco certified tool.
Monitor traffic, SLAs, QoS, Medianet, WAAS etc. with NetFlow Analyzer
Customize your own dashboards, set traffic alerts and generate reports.
Network behavioral analysis & security monitoring. All-in-one tool.
http://pubads.g.doubleclick.net/gampad/clk?id=126839071&iu=/4140/ostg.clktrk
_______________________________________________
Rdkit-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss