Rich posted an approach that mostly works elsewhere on this thread, but the
whole thing got me thinking about the way the RDKit interacts with the copy
module.
I did a bit of reading and realized that it was possible to support copy
and deepcopy properly, allowing Mol objects to be used in a much more
Pythonic manner.
I just checked in a set of of changes (
https://github.com/rdkit/rdkit/issues/467) that enables this:
In [2]: import copy
In [3]: m1 =Chem.MolFromSmiles('CCC')
In [4]: m1.SetProp('Foo','bar')
In [5]: m1.foo = ['bar']
In [6]: m2 = copy.copy(m1)
In [7]: m3 = copy.deepcopy(m1)
In [8]: m2.GetProp('Foo')
Out[8]: 'bar'
In [9]: m3.GetProp('Foo')
Out[9]: 'bar'
In [10]: m2.foo
Out[10]: ['bar']
In [11]: m3.foo
Out[11]: ['bar']
In [12]: m2.foo.append(4)
In [13]: m2.foo
Out[13]: ['bar', 4]
In [14]: m1.foo
Out[14]: ['bar', 4]
In [15]: m3.foo
Out[15]: ['bar']
This will be in the next release (heading to beta very, very soon).
The advantage of directly supporting copy and deepcopy over using the Mol
copy constructor (as Rich suggested) is that it also copies attributes that
have been added to the object at the python level (this is the m1.foo
attribute in the above examples).
-greg
On Fri, Mar 27, 2015 at 10:41 AM, Dave Wood <[email protected]> wrote:
> Dear All,
>
> I was a little suprised by this behaviour when making a copy of a
> molecule. Is it the expected behaviour?
>
> It isn't a problem for me as I can copy the properties across seperately,
> but I thought it could be worth mentioning.
>
> Dave
>
> IPython 1.2.1 -- An enhanced Interactive Python.
> ? -> Introduction and overview of IPython's features.
> %quickref -> Quick reference.
> help -> Python's own help system.
> object? -> Details about 'object', use 'object??' for extra details.
>
> In [1]: from rdkit import Chem
> In [2]: import copy
> In [3]: mol = Chem.MolFromSmiles("c1ccccc1")
> In [4]: mol.SetProp("_Name", "One")
> In [5]: mol.SetProp("Prop", "1")
> In [6]: mol2 = copy.deepcopy(mol)
> In [7]: print mol2.GetProp("_Name")
> ---------------------------------------------------------------------------
> KeyError Traceback (most recent call last)
> <ipython-input-7-9ad92b63b870> in <module>()
> ----> 1 print mol2.GetProp("_Name")
>
> KeyError: '_Name'
>
> In [8]: print mol2.GetProp("Prop")
> ---------------------------------------------------------------------------
> KeyError Traceback (most recent call last)
> <ipython-input-8-668c3857671a> in <module>()
> ----> 1 print mol2.GetProp("Prop")
>
> KeyError: 'Prop'
>
>
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website,
> sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for
> all
> things parallel software development, from weekly thought leadership blogs
> to
> news, videos, case studies, tutorials and more. Take a look and join the
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> Rdkit-discuss mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>
>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Rdkit-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss