Source code for schrodinger.structutils.residue

import itertools

from schrodinger import structure


[docs]def get_residues_between(res1: structure._Residue, res2: structure._Residue) -> list: """ Returns a list of `structure._Residue` objects between (and including) the specified residues by order of connectivity according to N->C order Note that residues must belong to the same chain and have the same molecule number, i.e., be really connected. :param res1: One of the residues in a connected sequence of residues :param res2: The other residue in a connected sequence of residues :raises ValueError: Residues must belong to the same chain and have the same molecule number :return: The residues from one of the specified residues to the other, in N->C order. """ if res1 == res2: return [res1] chain = res1.atom[1].getChain() if chain.name != res2.chain: raise ValueError("Specified residues are on different chains " f"({res1.chain} and {res2.chain}).") st = res1.structure if st != res2.structure: raise ValueError("Specified residues belong to different structures.") if res1.molecule_number != res2.molecule_number: raise ValueError("Specified residues are not connected.") residues = structure.get_residues_by_connectivity(chain) not_start_or_end = lambda res: res not in (res1, res2) up_to_first_res = itertools.dropwhile(not_start_or_end, residues) first_res = next(up_to_first_res, None) last_res = res2 if res1 == first_res else res1 residues_between = itertools.takewhile(not_start_or_end, up_to_first_res) return list(itertools.chain([first_res], residues_between, [last_res]))