Skip to content

IndexError in find_atom_pairs: matches/iso_matches index mismatch (frag.network.decorate) #31

Description

@alanbchristie

frag.network.decorate.find_atom_pairs() raises IndexError: list index out of range for some molecules.

Where (frag/network/decorate.py, v1.1.0)

def find_atom_pairs(smiles_input, get_indices, iso_smiles):
    matches = re.findall(XE_PATT, smiles_input)
    if iso_smiles:
        iso_matches = re.findall(XE_PATT, iso_smiles)
    else:
        iso_matches = None
        isotope = None
    ind_list = []
    for i, match in enumerate(matches):
        index = int(match[:-2])
        if iso_matches:
            isotope = int(iso_matches[i][:-2])   # IndexError when len(iso_matches) < len(matches)
        indices = ret_comb_index(index, get_indices, isotope)
        ind_list.append(indices)
    return ind_list

Cause

matches comes from smiles_input; iso_matches comes from a different string, iso_smiles. The loop runs over matches but subscripts iso_matches[i], assuming the two lists are equal length and aligned 1:1. When iso_smiles yields fewer [<n>Xe] tokens than smiles_input, iso_matches[i] goes out of range.

Reproduced via

get_vect_indices_for_mol() -> del_link_coord(..., iso_labels=False) -> get_atom_coords -> find_atom_pairs. Hit in production via the fragalysis-backend /api/vector endpoint (xchem/fragalysis-backend#941):

  File "frag/network/decorate.py", line 225, in find_atom_pairs
    isotope = int(iso_matches[i][:-2])
IndexError: list index out of range

Suggested fix

Don't assume positional correspondence between matches and iso_matches -- bounds-check i < len(iso_matches), or rework how isotope labels are paired to the plain matches.

Version: 1.1.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions