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.
frag.network.decorate.find_atom_pairs()raisesIndexError: list index out of rangefor some molecules.Where (
frag/network/decorate.py, v1.1.0)Cause
matchescomes fromsmiles_input;iso_matchescomes from a different string,iso_smiles. The loop runs overmatchesbut subscriptsiso_matches[i], assuming the two lists are equal length and aligned 1:1. Wheniso_smilesyields fewer[<n>Xe]tokens thansmiles_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/vectorendpoint (xchem/fragalysis-backend#941):Suggested fix
Don't assume positional correspondence between
matchesandiso_matches-- bounds-checki < len(iso_matches), or rework how isotope labels are paired to the plain matches.Version: 1.1.0.