creating selection from gromacs index file

257 views
Skip to first unread message

Max Linke

unread,
Oct 24, 2016, 9:17:42 AM10/24/16
to MDnalysis-devel
Hi

Do we have a function to generate selections based on gromacs index
files? I couldn't find anything when a colleague asked me. I gave him a
quick snippet to parse the index file into a dict and explained him how
he can use that to do his selections

```python

with open('indexfile') as f:
data = f.readlines()

selections = {}
for i, line in enumerate(data):
print line
if '[' in line:
selections[line[1:-2]] = np.int64(data[i+1].replace('\n',
'').split()) - 1
```

best Max

Richard Gowers

unread,
Oct 24, 2016, 9:57:30 AM10/24/16
to MDnalysis-devel
Once you've got the numpy array of indices, you should just be able to slice u.atoms with it?

ie
sel = np.array([0, 4, 6, 7])  # this is what you read from the ndx file

ag = u.atoms[sel]

Would be a cool convenience function to turn an ndx file into a dict of label:atomgroup

Max Linke

unread,
Oct 24, 2016, 10:04:12 AM10/24/16
to mdnalys...@googlegroups.com
On 10/24/2016 03:57 PM, Richard Gowers wrote:
> Once you've got the numpy array of indices, you should just be able to
> slice u.atoms with it?

He is doing that now after he parsed the ndx file with the code below.

> Would be a cool convenience function to turn an ndx file into a dict of
> label:atomgroup

I'm not sure where something like this would fit best. This also raises
the question if we should write wrappers for other selection types
similar to how we write different selections for vmd/charmm/gromacs/...

>
> On Monday, 24 October 2016 14:17:42 UTC+1, Max Linke wrote:
>
> Hi
>
> Do we have a function to generate selections based on gromacs index
> files? I couldn't find anything when a colleague asked me. I gave
> him a
> quick snippet to parse the index file into a dict and explained him how
> he can use that to do his selections
>
> ```python
>
> with open('indexfile') as f:
> data = f.readlines()
>
> selections = {}
> for i, line in enumerate(data):
> print line
> if '[' in line:
> selections[line[1:-2]] = np.int64(data[i+1].replace('\n',
> '').split()) - 1
> ```
>
> best Max
>
> --
> You received this message because you are subscribed to the Google
> Groups "MDnalysis-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mdnalysis-dev...@googlegroups.com
> <mailto:mdnalysis-dev...@googlegroups.com>.
> To post to this group, send email to mdnalys...@googlegroups.com
> <mailto:mdnalys...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mdnalysis-devel/3a1cd676-20ac-49ea-bc2f-7c705a45794e%40googlegroups.com
> <https://groups.google.com/d/msgid/mdnalysis-devel/3a1cd676-20ac-49ea-bc2f-7c705a45794e%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Max Linke

unread,
Oct 24, 2016, 10:41:09 AM10/24/16
to mdnalys...@googlegroups.com
I just wrote these two function myself for that. If we know where this
would best fit into MDAnalysis we can add it.


```python
def ndx_to_selections(ndx):
"""generate name atom id pairs from a gromacs ndx file.
The indices are parsed as is 1-indexed.

Parameters
----------
ndx : str
gromacs ndx file

Yields
------
label, indices
"""
with open(ndx) as f:
lines = f.readlines()

for i, line in enumerate(lines):
if '[' in line and ']' in line:
yield line.strip()[1:-1], np.int64(lines[i +
1].strip().split())


def ndx_to_atomgroups(ndx, universe):
"""convert a gromacs ndx file to a dictionary of atom selections in
universe.

Parameters
----------
ndx : str
gromacs ndx file
universe : str or mda.Universe
Valid mda.universe to filepath to a topology

Returns
-------
dictionary with selection label as key and atomgroup as value

"""

selections = ndx_to_selections(ndx)
u = mda.as_Universe(universe)

# ndx files are 1 indexed
return {label: u.atoms[sel - 1] for label, sel in selections}

```

On 10/24/2016 03:57 PM, Richard Gowers wrote:

Manuel Nuno Melo

unread,
Oct 24, 2016, 11:02:21 AM10/24/16
to mdnalys...@googlegroups.com
I vote that at least 'ndx_to_atomgroups' be a method of a Universe, perhaps with a simpler name:

ndx_grp_dict = u.from_ndx('file.ndx')
(could be generalized later to handle multiple formats)
or
ndx_grp_dict = u.ndx_groups('file.ndx')

A caveat: ndx files can have different groups with the same name. In mdreader I implement something similar but instead of making a dict I build a list of atomgroups to which I append an attribute 'ndx_name' (or something along these lines). Of course my use case is different there and a dictionary feels better here.


--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-devel+unsubscribe@googlegroups.com.
To post to this group, send email to mdnalysis-devel@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-devel/4843088b-572b-6c1a-1299-faf8dfaaeb47%40gmail.com.

Max Linke

unread,
Oct 24, 2016, 11:06:01 AM10/24/16
to mdnalys...@googlegroups.com

How does gromacs handle the case the a group exists twice?


--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-dev...@googlegroups.com.
To post to this group, send email to mdnalys...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-dev...@googlegroups.com.
To post to this group, send email to mdnalys...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-devel/CAERWk%3D6k07AaCMXe2%2Bq28qTtWfo-Y_q3%3D8e6VxQ6qC_sJNkrxQ%40mail.gmail.com.

Manuel Nuno Melo

unread,
Oct 24, 2016, 1:14:11 PM10/24/16
to mdnalys...@googlegroups.com
Ok, did some GROMACS (5.1.1) testing.
If there are multiple groups with the same label:
 grompp will use the first to resolve names in the .mdp file.
 trjconv will show both in the choice list. Its behavior depends:
   if a group is chosen by number all works fine;
   if a group is specified by name trjconv yields two consecutive errors:
   Error: Multiple groups 'XXX' selected
   Error: No such group 'XXX'

Didn't test but imagine that other tools that present index choices will behave as trjconv.



--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-devel+unsubscribe@googlegroups.com.
To post to this group, send email to mdnalysis-devel@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-devel+unsubscribe@googlegroups.com.
To post to this group, send email to mdnalysis-devel@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-devel+unsubscribe@googlegroups.com.
To post to this group, send email to mdnalysis-devel@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-devel/CAKRyePDscOU4-wkjy-A27AP4Pqo5iRetzjAqtjXrFevHcUcnzw%40mail.gmail.com.

Oliver Beckstein

unread,
Oct 24, 2016, 2:45:07 PM10/24/16
to mdnalys...@googlegroups.com

> On 24 Oct, 2016, at 08:02, Manuel Nuno Melo <manuel.n...@gmail.com> wrote:
>
> I vote that at least 'ndx_to_atomgroups' be a method of a Universe, perhaps with a simpler name:

Although I like the interoperability idea a lot, I don't like adding highly format-specific things to the top level name space. Can't we do this in a more un-intrusive manner, similar to what the readers/writers and selection writers do? It should also be more abstract, i.e., the idea to store selections. In principle you are asking for a selection Reader. So then think about how this should work in a general sense.

(MDSynthesis has a hack to store selections, which is very useful but it is pretty hackish at the moment and thus a more general approach would be good. The important idea is to have named selections where you access the same atoms with the same selection name but the actual selection string or indices can differ depending on your simulation/pdb file.)

Oliver
> an email to mdnalysis-dev...@googlegroups.com
> <mailto:mdnalysis-dev...@googlegroups.com>.
> To post to this group, send email to mdnalys...@googlegroups.com
> <mailto:mdnalys...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mdnalysis-devel/3a1cd676-20ac-49ea-bc2f-7c705a45794e%40googlegroups.com
> <https://groups.google.com/d/msgid/mdnalysis-devel/3a1cd676-20ac-49ea-bc2f-7c705a45794e%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-dev...@googlegroups.com.
> To post to this group, send email to mdnalys...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-dev...@googlegroups.com.
> To post to this group, send email to mdnalys...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-devel/CAERWk%3D6k07AaCMXe2%2Bq28qTtWfo-Y_q3%3D8e6VxQ6qC_sJNkrxQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
Oliver Beckstein * orbe...@gmx.net
skype: orbeckst * orbe...@gmail.com

Max Linke

unread,
Oct 24, 2016, 4:14:01 PM10/24/16
to mdnalys...@googlegroups.com


On 10/24/2016 08:45 PM, Oliver Beckstein wrote:
>
>> On 24 Oct, 2016, at 08:02, Manuel Nuno Melo <manuel.n...@gmail.com> wrote:
>>
>> I vote that at least 'ndx_to_atomgroups' be a method of a Universe, perhaps with a simpler name:
>
> Although I like the interoperability idea a lot, I don't like adding highly format-specific things to the top level name space. Can't we do this in a more un-intrusive manner, similar to what the readers/writers and selection writers do? It should also be more abstract, i.e., the idea to store selections. In principle you are asking for a selection Reader. So then think about how this should work in a general sense.

I agree with Oliver that we shouldn't add this to the Universe. Right
now I can only see this go into the docs as an example how to use
MDAnalysis with gromacs. But I would agree a good system to store and
share selections would be nice.

> (MDSynthesis has a hack to store selections, which is very useful but it is pretty hackish at the moment and thus a more general approach would be good. The important idea is to have named selections where you access the same atoms with the same selection name but the actual selection string or indices can differ depending on your simulation/pdb file.)

Can we extend that approach?

Manuel Nuno Melo

unread,
Oct 25, 2016, 4:51:30 AM10/25/16
to mdnalys...@googlegroups.com

I get the point, and it makes sense to tuck the code away under some selection Reader section.

But wouldn't a format-independent wrapper at the Universe level make sense at some point?


Oliver

To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-devel+unsubscribe@googlegroups.com.
To post to this group, send email to mdnalysis-devel@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-devel/4843088b-572b-6c1a-1299-faf8dfaaeb47%40gmail.com.

For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-devel+unsubscribe@googlegroups.com.
To post to this group, send email to mdnalysis-devel@googlegroups.com.

--
Oliver Beckstein * orbe...@gmx.net
skype: orbeckst  * orbe...@gmail.com

--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-devel+unsubscribe@googlegroups.com.
To post to this group, send email to mdnalysis-devel@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-devel/fbd7baf0-5444-e654-b619-1570e76250f7%40gmail.com.

Max Linke

unread,
Oct 26, 2016, 4:34:53 AM10/26/16
to MDnalysis-devel
Is there an issue on github for a persistent selection API? I think we talked about this as well for using multiprocessing.
Oliver

To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-dev...@googlegroups.com.
To post to this group, send email to mdnalys...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/mdnalysis-devel/4843088b-572b-6c1a-1299-faf8dfaaeb47%40gmail.com.

For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-dev...@googlegroups.com.
To post to this group, send email to mdnalys...@googlegroups.com.

--
Oliver Beckstein * orbe...@gmx.net
skype: orbeckst  * orbe...@gmail.com


--
You received this message because you are subscribed to the Google Groups "MDnalysis-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mdnalysis-dev...@googlegroups.com.
To post to this group, send email to mdnalys...@googlegroups.com.

Max Linke

unread,
Nov 7, 2016, 9:01:52 AM11/7/16
to MDnalysis-devel
Btw having a persistent selection API for pickling and exchanging selections with colleagues sounds like a nice GSoC project for next year.
Anyone interested to write up some details?

best Max
Reply all
Reply to author
Forward
0 new messages