changes in behavior of iterations over molecules container

4 views
Skip to first unread message

julien

unread,
Oct 23, 2014, 4:43:22 PM10/23/14
to sire-de...@googlegroups.com

Hi Chris, 


I think I managed to fix the buggy matrix operations, although not in a safe manner. I will upload the code in a while. 


I have hit another issue...It seems like the order in which I pull molecules out of a molecules container has changed ?


Before I used to be able to write:

        

molecules = system[MGName("all")].molecules()

molnums = molecules.molNums()

      

water_results = {}


    for x in range(0,len(molnums)):

         molnum = molnums[x]


and the molecules in molnums were sorted by molecule number i.e 



(Pdb) molnums

[MolNum(1), MolNum(2), MolNum(3), MolNum(4), MolNum(5), MolNum(6), MolNum(7), MolNum(8), MolNum(9), MolNum(10), MolNum(11), MolNum(12), MolNum(13), MolNum(14), MolNum(15), MolNum(16), MolNum(17), MolNum(18), MolNum(19), MolNum(20), MolNum(21), MolNum(22), MolNum(23), MolNum(24), MolNum(25), MolNum(26), MolNum(27), MolNum(28), ..... ]


Now with the new code it looks like the molecules are randomly ordered


(Pdb) molnums

[MolNum(240), MolNum(241), MolNum(254), MolNum(255), MolNum(252), MolNum(253), MolNum(250), MolNum(251), MolNum(248), MolNum(249), MolNum(262), MolNum(263), MolNum(260), MolNum(261), MolNum(258), MolNum(259), MolNum(256), MolNum(257), MolNum(270), MolNum(271), MolNum(268), MolNum(269), MolNum(266), MolNum(267), MolNum(264), MolNum(265), MolNum(278),....)


This unfortunately breaks many of the scripts I wrote (I suspect this also explains why the DCD writer code is no longer working properly as the order of the molecules is no longer in sync with the order in which their coordinates were loaded). 

using 

molnums.sort()

gives me back the desired behaviour.

For what reason is the code behaving like this now? I'm not clear on whether this is a python3 or Sire change. 

Is it safe to use .sort() throughout my scripts to reorder molecules?

Best wishes, 

Julien

(Sorry for all the questions)

Christopher Woods

unread,
Oct 23, 2014, 6:15:55 PM10/23/14
to sire-de...@googlegroups.com

  Hi Julien,

The problem was caused by the change in behaviour of QHash from Qt4 to Qt5. In the old Sire (which used Qt4) QHash by chance saved the order of items in the dictionary. This was by chance and should not have been relied upon. I'm Qt5 they changed the hash algorithm and now the order of items in the dictionary is completely random (even when running the same script multiple times). This means that you do indeed need to use molnums.sort() if you want to iterate through the items in order.

So you are right that you need to use,

molnums = molecules.molNums()
molnums.sort()
for molnum in molnums:
   Etc.

The molnums are not automatically sorted as the ethos of Molecules over MoleculeGroup is to not spend the computational cost or memory cost of sorting. You can force sorting by using MoleculeGroup (which internally maintains a list that records the order that molecules were added to the group). 

This change is what prompted me to change Amber to return a MoleculeGroup rather than a Molecules object (so the order in the top file is saved). It was also what prompted me to write the MolWithResID class to find molecules in Molecules, e.g. to find the ligand with residue called LIG, use

ligand = molecules[ MolWithResID("LIG") ].molecule()

Gaetano encountered this problem when he got back after his last visit and we fixed as much as we could then. I'm sorry if this change has caused difficulty. Unfortunately there are some things that could be called "undefined behaviour" that we can begin to rely on, and that will break as the dependencies under Sire are upgraded.

  Best wishes,

  Christopher
--
You received this message because you are subscribed to the Google Groups "Sire Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sire-develope...@googlegroups.com.
To post to this group, send email to sire-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/sire-developers.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages