Skip to content

[BUG] Sire selection allows out-of-bounds indexing of molecules #286

@lohedges

Description

@lohedges

Not sure if this is the intended behaviour or not, but it appears that out-of-bounds indexing using MolIdx in a Sire selection always gives the last molecule, e.g.:

In [1]: import sire as sr

In [2]: mols = sr.load_test_files("ala.crd", "ala.top")


In [3]: mols.num_molecules()
Out[3]: 631

In [4]: mols["molidx 10000"]
Out[4]: Molecule( WAT:621 num_atoms=3 num_residues=1 )

In [5]: mols[-1]
Out[5]: Molecule( WAT:621 num_atoms=3 num_residues=1 )

This isn't true for other indexing, e.g. atoms and residues:

In [6]: mols.num_residues()
Out[6]: 633

In [7]: mols["residx 10000"]
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ in <module>:1                                                                                    │
│                                                                                                  │
│ /home/lester/.conda/envs/openbiosim/lib/python3.12/site-packages/sire/system/_system.py:75 in    │
│ __getitem__                                                                                      │
│                                                                                                  │
│     72 │   │   return self.__str__()                                                             │
│     73 │                                                                                         │
│     74def __getitem__(self, key):                                                           │
│ ❱   75 │   │   return self.molecules()[key]                                                      │
│     76 │                                                                                         │
│     77def __iadd__(self, molecules):                                                        │
│     78 │   │   self.add(molecules)                                                               │
│                                                                                                  │
│ /home/lester/.conda/envs/openbiosim/lib/python3.12/site-packages/sire/mol/__init__.py:505 in     │
│ __fixed__getitem__                                                                               │
│                                                                                                  │
│    502 │   │   │   # try to search for the object - this will raise                              │503 │   │   │   # a SyntaxError if this is not a search term                                  │504 │   │   │   # (and is instead a name)                                                     │
│ ❱  505 │   │   │   return __from_select_result(obj.search(key, map=map))                         │
│    506 │   │   except SyntaxError:                                                               │
│    507 │   │   │   # ignore SyntaxErrors as this is a name                                       │508 │   │   │   pass                                                                          │
│                                                                                                  │
│ /home/lester/.conda/envs/openbiosim/lib/python3.12/site-packages/sire/mol/__init__.py:364 in     │
│ __from_select_result                                                                             │
│                                                                                                  │
│    361 │   │   )                                                                                 │
│    362 │                                                                                         │
│    363if obj.list_count() == 0:                                                             │
│ ❱  364 │   │   raise KeyError("Nothing matched the search.")                                     │
│    365 │                                                                                         │
│    366typ = obj.get_common_type()                                                           │
│    367                                                                                           │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
KeyError: 'Nothing matched the search.'

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions