def peptide_mutation_interval(
        peptide_start_in_protein,
        peptide_length,
        mutation_start_in_protein,
        mutation_end_in_protein):
    """
    Half-open interval of mutated residues in the peptide, determined
    from the mutation interval in the original protein sequence.

    Parameters
    ----------
    peptide_start_in_protein : int
        Position of the first peptide residue within the protein
        (starting from 0)

    peptide_length : int

    mutation_start_in_protein : int
        Position of the first mutated residue starting from 0. In the case of a
        deletion, the position where the first residue had been.

    mutation_end_in_protein : int
        Position of the last mutated residue in the mutant protein. In the case
        of a deletion, this is equal to the mutation_start_in_protein.
    )
    """
    if peptide_start_in_protein > mutation_end_in_protein:
        raise ValueError("Peptide starts after mutation")
    elif peptide_start_in_protein + peptide_length < mutation_start_in_protein:
        raise ValueError("Peptide ends before mutation")

    # need a half-open start/end interval
    peptide_mutation_start_offset = min(
        peptide_length,
        max(0, mutation_start_in_protein - peptide_start_in_protein))
    peptide_mutation_end_offset = min(
        peptide_length,
        max(0, mutation_end_in_protein - peptide_start_in_protein))
    return (peptide_mutation_start_offset, peptide_mutation_end_offset)
Half-open interval of mutated residues in the peptide, determined
    from the mutation interval in the original protein sequence.

    Parameters
    ----------
    peptide_start_in_protein : int
        Position of the first peptide residue within the protein
        (starting from 0)

    peptide_length : int

    mutation_start_in_protein : int
        Position of the first mutated residue starting from 0. In the case of a
        deletion, the position where the first residue had been.

    mutation_end_in_protein : int
        Position of the last mutated residue in the mutant protein. In the case
        of a deletion, this is equal to the mutation_start_in_protein.
    )