chemfiles / chemfiles

Library for reading and writing chemistry files
http://chemfiles.org
BSD 3-Clause "New" or "Revised" License
164 stars 47 forks source link

ranged selection for numerical properties #338

Open mdimura opened 4 years ago

mdimura commented 4 years ago

Chemfiles selection syntax does not allow for ranged numerical properties. Would it be possible to introduce something like resid 3 to 17 (like VMD)? Similar syntax exists in VMD, pymol, mdtraj, etc., and it is more compact than chemfiles' current alternative: resid >=3 and resid <=17. Possible syntax options are:

  1. between(resid, 3, 17)
  2. resid 3 to 17
  3. 3 <= resid <= 17

My personal favorite is resid 3 to 17. 3 <= resid <= 17 is OK, but less compact and does not exist in any of the other tools I am familiar with (pymol, vmd, mdtraj, cpptraj). I don't know if there are some implementation considerations though.

Luthaf commented 4 years ago

Thank you for opening the issue!

The resid 3-17 syntax would clash with resid == 3 - 17 which is interpreted as resid == -14

resid 3 to 17 is very nice from the user point of view, but might be hard to fit within the current framework. In particular, resid is not a special construct in our selection language, only something that gives us a numerical value that can then be combined and compared with other values. For example, resid + x^2 < y^2, although a bit strange, is a valid selection. resid 3 works by being expanded to resid == 3; and resid 3 4 is expanded to resid == 3 or resid == 4.

My favourite alternatives would be 3 <= resid <= 17, or between(resid, 3, 17). `


but less compact and does not exist in any of the other tools I am familiar with (pymol, vmd, mdtraj, cpptraj).

What syntax do these tools use?

mdimura commented 4 years ago

Would it be possible to introduce to as a keyword, which expands to >= L and <=R? Here are some examples of ranged selection in other similar tools: vmd: mass 5 to 11.5 pymol: resi 100-200 mdtraj: mass 5.5 to 20 cpptraj: :6-10 mdanalysis (derived from CHARMM’s syntax): resnum 1:5 I guess, options resid 3:17 and resid 3..17 could also be considered.

Luthaf commented 4 years ago

resid 3:17 could work too, or something like resid in 3:17. This would compose nicely with math operations (x + y^2 in cos(z):(3 + sin(z))). I'll have to think a bit more about how a to keyword could work 😃

Luthaf commented 3 years ago

Note for myself: here is a nice explanation of how a <= b > c < e works in Python https://softwareengineering.stackexchange.com/a/316983.