import numpy as np
from rdkit import Chem
from rdkit.Chem.rdFingerprintGenerator import GetMorganGenerator

# Morgan fingerprint generator (new API)
morgan_gen = GetMorganGenerator(radius=2, fpSize=2048)

amino_acids = "ACDEFGHIKLMNPQRSTVWY"


def smiles_to_fp(smiles):
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        raise ValueError("Invalid SMILES string")

    return np.array(morgan_gen.GetFingerprint(mol))


def protein_to_features(seq):
    if not isinstance(seq, str) or len(seq) == 0:
        raise ValueError("Invalid protein sequence")

    seq = seq.upper()
    length = len(seq)

    return np.array([seq.count(aa) / length for aa in amino_acids])


def create_features(smiles, protein):
    fp = smiles_to_fp(smiles)
    prot = protein_to_features(protein)

    return np.concatenate([fp, prot]).reshape(1, -1)
