File size: 826 Bytes
c289d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from __future__ import annotations

from typing import Dict


AA_ALPHABET = "ACDEFGHIKLMNPQRSTVWY"


def compute_sequence_features(sequence: str) -> Dict[str, float]:
    """Compute simple amino-acid composition and coarse biochemical summaries."""
    seq = sequence.strip().upper()
    length = max(len(seq), 1)

    features: Dict[str, float] = {f"aa_frac_{aa}": seq.count(aa) / length for aa in AA_ALPHABET}
    aromatic = set("FWY")
    polar = set("STNQ")
    charged = set("KRHDE")

    features.update(
        {
            "seq_length": float(len(seq)),
            "frac_aromatic": sum(1 for aa in seq if aa in aromatic) / length,
            "frac_polar": sum(1 for aa in seq if aa in polar) / length,
            "frac_charged": sum(1 for aa in seq if aa in charged) / length,
        }
    )
    return features