예제 #1
0
def at_most_one_codon_per_amino(
        T: CodeRef,
        codons: Sequence[CodonRef] = z3_enum_codons,
        aminos: Sequence[AminoRef] = z3_enum_aminos,
        exclude: Optional[AminoRef] = None
) -> List[ConstraintRef]:
    """
    :param aminos:
    :param T: genetic code
    :param codons: domain of T (list or list-like of codons)
    :param exclude: range of T to ignore (list or list-like of aminos)
    :return: list of constraints
    """
    if exclude is None:
        exclude = (get_null(aminos), get_stop(aminos))

    def _any_codon_in_exclude(T, codons, exclude):
        return Or([
            Or([decode(T, codon) == exc for codon in codons])
            for exc in exclude
        ])

    return [
        Or(
            decode(T, c1) != decode(T, c2),
            # check that codons aren't identical
            _any_codon_in_exclude(T, (c1, c2), exclude)
            # or that codons are in exclude
        ) for c1, c2 in itertools.combinations(codons, r=2)
    ]
예제 #2
0
def protein_variables(
        T: CodeRef,
        seq_variables: Sequence[NucleotideRef],
        part: Part,
        offset: int = 0,
        amino_sort: AminoSort = AminoBitVecSort,
) -> List[AminoRef]:
    """
    A function that generates z3 variables corresponding to the amino acid
    sequence of the translated Part

    """
    begin = part.location.start - offset
    end = part.location.end - offset

    codons = get_codons(seq_variables[begin:end])
    if isinstance(T, dict):
        prot_seq = [
            Const(f"{part.name}_{i}", amino_sort)
            for i, _ in enumerate(codons)
        ]

    elif isinstance(T, FuncDeclRef):
        prot_seq = [
            decode(T, codon)
            for codon in codons
        ]

    else:
        raise TypeError(f"T is not of type CodeRef")

    return prot_seq
예제 #3
0
def compatible_with_standard_code(
        T: CodeRef,
        codons: Sequence[CodonRef] = triplet_dna_codons,
        aminos: Sequence[AminoRef] = z3_enum_aminos,
        amino_dict: Dict[str, AminoRef] = amino_to_z3_enum_amino
) -> List[ConstraintRef]:
    null = get_null(aminos)

    sc_constraints = standard_code(T, codons, amino_dict)

    return [
        Or(sc_const, decode(T, codon) == null)
        for codon, sc_const in zip(codons, sc_constraints)
    ]
예제 #4
0
def standard_code(
        T: CodeRef,
        codons: Sequence[CodonRef] = triplet_dna_codons,
        amino_dict: Dict[str, AminoRef] = amino_to_z3_bv_amino
) -> List[ConstraintRef]:
    dna_to_rna = dict(zip(codons, triplet_rna_codons))
    sc = {
        codon: amino_dict[Code()[dna_to_rna[codon]]]
        for codon in codons
    }

    return [
        decode(T, codon) == sc[codon]
        for codon in codons
    ]
예제 #5
0
def n_sense_codons(
        T: CodeRef,
        n_codons: int,
        codons: Sequence[CodonRef] = z3_enum_codons,
        aminos: Sequence[AminoRef] = z3_enum_aminos,
        exclude: Optional[AminoRef] = None
) -> List[ConstraintRef]:
    if exclude is None:
        exclude = (get_null(aminos), get_stop(aminos))
    return [PbEq(
        [
            (And([decode(T, c) != aa for aa in exclude]), 1)
            for c in codons
        ], k=n_codons
    )]
예제 #6
0
def specific_code(
        T: CodeRef,
        code: Code,
        codons: Sequence[CodonRef] = triplet_dna_codons,
        amino_dict: Dict[str, AminoRef] = amino_to_z3_enum_amino
) -> List[ConstraintRef]:
    dna_to_rna = dict(zip(codons, triplet_rna_codons))
    z3_code = {
        codon: amino_dict[code[dna_to_rna[codon]]]
        for codon in codons
    }

    return [
        decode(T, codon) == z3_code[codon]
        for codon in codons
    ]
예제 #7
0
def exactly_one_codon_per_amino(
        T: CodeRef,
        codons: Sequence[CodonRef] = z3_enum_codons,
        aminos: Sequence[AminoRef] = z3_enum_aminos,
        exclude: Optional[AminoRef] = None
) -> List[ConstraintRef]:
    """

    :param T: genetic code
    :param codons: list of CodonRef objects
    :param aminos: list of AminoRef objects
    :param exclude: range of T to ignore (list or list-like of aminos)
    :return: list of constraints
    """
    if exclude is None:
        exclude = (get_null(aminos), get_stop(aminos))

    return [
        PbEq([(decode(T, c) == aa, 1) for c in codons], k=1)
        for aa in aminos if aa not in exclude
    ]
예제 #8
0
def at_least_one_codon_per_amino(
        T: CodeRef,
        codons: Sequence[CodonRef] = z3_enum_codons,
        aminos: Sequence[AminoRef] = z3_enum_aminos,
        exclude: Optional[AminoRef] = None
) -> List[ConstraintRef]:
    """

    :param T: genetic code
    :param codons: domain of T (list or list-like of codons)
    :param aminos: range of T (list or list-like of aminos)
    :param exclude: range of T to ignore (list or list-like of aminos) (
    default=NULL)
    :return: list of constraints
    """
    if exclude is None:
        exclude = (get_null(aminos),)

    return [
        Or([decode(T, codon) == aa for codon in codons])
        for aa in aminos if aa not in exclude
    ]
예제 #9
0
def keep_all_stops(
        T: CodeRef,
        codons: Sequence[CodonRef] = triplet_dna_codons,
        aminos: Sequence[AminoRef] = z3_enum_aminos,
        amino_dict: Dict[str, AminoRef] = amino_to_z3_enum_amino
) -> List[ConstraintRef]:
    stop = get_stop(aminos)
    sc = Code()

    dna_to_rna = dict(zip(codons, triplet_rna_codons))
    def get_amino(codon): return amino_dict[sc[dna_to_rna[codon]]]

    stop_codons = {
        codon: get_amino(codon)
        for codon in codons
        if get_amino(codon) == stop
    }

    return [
        decode(T, stop_codon) == stop
        for stop_codon in stop_codons.keys()
    ]
예제 #10
0
def translation_constraints(
        T: CodeRef,
        dna_variables: Sequence[NucleotideRef],
        prot_variables: Sequence[AminoRef],
        location: Location,
        offset: int = 0,
        nucleotides: Sequence[NucleotideRef] = z3_enum_nucleotides,
        aminos: Sequence[AminoRef] = z3_enum_aminos,
        start_flag: bool = False,
        stop_flag: bool = False
) -> List[ConstraintRef]:
    """
    A function that generates constraints on DNA variables that are involved
    in encoding proteins (must start with "M", no NULL codons in middle,
    must end with STOP)

    :param T: genetic code
    :param dna_variables:
    :param prot_variables:
    :param location:
    :param offset:
    :param nucleotides:
    :param aminos:
    :param start_flag:
    :param stop_flag:
    :return:
    """
    # translation signals
    START = get_start(aminos)
    STOP = get_stop(aminos)
    NULL = get_null(aminos)

    # ensure proteins start with Met
    start_constraints = []
    if start_flag:
        start_constraints += [prot_variables[0] == START]

    # ensure no NULL in middle of protein and proper termination
    null_constraints = [
        aa != NULL for aa in prot_variables
    ]

    stop_constraints = [
        aa != STOP for aa in prot_variables[:-1]
    ]

    if stop_flag:
        stop_constraints += [prot_variables[-1] == STOP]
    else:
        stop_constraints += [prot_variables[-1] != STOP]

    if isinstance(T, dict):
        start = location.start - offset
        end = location.end - offset
        codon_variables = get_codons(dna_variables[start:end])

        code_implications = [
            Implies(And(codon_variable[0] == z3codon[0],
                        codon_variable[1] == z3codon[1],
                        codon_variable[2] == z3codon[2]),
                    z3amino == decode(T, z3codon))
            for codon_variable, z3amino in zip(codon_variables, prot_variables)
            for z3codon in itertools.product(nucleotides, repeat=3)
        ]
    else:
        code_implications = []

    return start_constraints + stop_constraints \
           + null_constraints + code_implications
예제 #11
0
 def _any_codon_in_exclude(T, codons, exclude):
     return Or([
         Or([decode(T, codon) == exc for codon in codons])
         for exc in exclude
     ])
예제 #12
0
        def amino(codon): return str(model[decode(code, codon)])

        dict_ = {