Assembly containing segments of polypeptide, divided according to separation of contiguous residues.

    Parameters
    ----------
    polypeptide : Polypeptide
    residues : iterable containing Residues
    max_dist : float
        Separation beyond which splitting of Polymer occurs.

    Returns
    -------
    segments : Assembly
        Each segment contains a subset of residues, each not separated by more than max_dist from the previous Residue.

def find_contiguous_packing_segments(polypeptide, residues, max_dist=10.0):
    """ Assembly containing segments of polypeptide, divided according to separation of contiguous residues.

    Parameters
    ----------
    polypeptide : Polypeptide
    residues : iterable containing Residues
    max_dist : float
        Separation beyond which splitting of Polymer occurs.

    Returns
    -------
    segments : Assembly
        Each segment contains a subset of residues, each not separated by more than max_dist from the previous Residue.
    """
    segments = Assembly(assembly_id=polypeptide.ampal_parent.id)
    residues_in_polypeptide = list(sorted(residues.intersection(set(polypeptide.get_monomers())),
                                          key=lambda x: int(x.id)))
    if not residues_in_polypeptide:
        return segments
    # residue_pots contains separate pots of residues divided according to their separation distance.
    residue_pots = []
    pot = [residues_in_polypeptide[0]]
    for r1, r2 in zip(residues_in_polypeptide, residues_in_polypeptide[1:]):
        d = distance(r1['CA'], r2['CA'])
        if d <= max_dist:
            pot.append(r2)
            if sum([len(x) for x in residue_pots] + [len(pot)]) == len(residues_in_polypeptide):
                residue_pots.append(pot)
        else:
            residue_pots.append(pot)
            pot = [r2]
    for pot in residue_pots:
        segment = polypeptide.get_slice_from_res_id(pot[0].id, pot[-1].id)
        segment.ampal_parent = polypeptide.ampal_parent
        segments.append(segment)
    return segments