alethiotx.artemis.cv

1from .pipeline import prepare, run
2
3__all__ = ['prepare', 'run']
def prepare( X: pandas.core.frame.DataFrame, y: pandas.core.frame.DataFrame, pathway_genes: List = [], known_targets: List = [], term_num=None, bins: int = 3, rand_seed: int = 12345) -> dict:
  9def prepare(X: DataFrame, y: DataFrame, pathway_genes: List = [], known_targets: List = [], term_num = None, bins: int = 3, rand_seed: int = 12345) -> dict:
 10    """
 11    Prepare and preprocess data for machine learning model training.
 12
 13    This function processes knowledge graph (KG) features and clinical scores to create
 14    training datasets for drug target prediction models. It handles positive targets,
 15    negative samples, and pathway genes, then returns formatted features and labels.
 16
 17    :param X: Knowledge graph features with genes as columns
 18    :type X: DataFrame
 19    :param y: Clinical data containing target genes and clinical scores
 20    :type y: DataFrame
 21    :param pathway_genes: List of pathway-associated genes to include as a separate class
 22    :type pathway_genes: List, optional
 23    :param known_targets: List of known target genes to exclude from negative sampling
 24    :type known_targets: List, optional
 25    :param term_num: Number of random KG features to sample; if None, uses all features
 26    :type term_num: int, optional
 27    :param bins: Number of bins for discretizing positive target clinical scores
 28    :type bins: int, optional
 29    :param rand_seed: Random seed for reproducibility
 30    :type rand_seed: int, optional
 31    :return: Dictionary containing:
 32    * ``X``: Feature matrix (KG features) for modeling
 33    * ``y``: Continuous clinical scores (log2-transformed)
 34    * ``y_encoded``: Categorical labels with binned targets and pathway genes
 35    * ``y_binary``: Binary labels (1 for targets/pathway genes, 0 for non-targets)
 36    :rtype: dict
 37
 38    :raises Exception: May raise exceptions during quantile binning if insufficient unique values exist
 39
 40    .. note::
 41
 42        - clinical scores are log2-transformed as ``log2(score + 1)``
 43        - Positive targets are binned into categories: ``target_0``, ``target_1``, etc.
 44        - Negative samples are randomly selected to match the number of positive + pathway genes
 45        - Pathway genes are assigned a clinical score of 1 and labeled as ``pathway_gene``
 46        - The function sets pandas chained assignment warnings to None
 47    """
 48    options.mode.chained_assignment = None  # default='warn'
 49    random.seed(rand_seed)
 50    # prepare KG features
 51    if term_num:
 52        X = X.sample(n = term_num, axis = 1, random_state=rand_seed)
 53    # prepare clinical scores
 54    y = y[['Target Gene', 'Clinical Score']]
 55    y['Clinical Score'] = log2(y['Clinical Score'] + 1)
 56    y.index = y['Target Gene']
 57    y = y.drop(columns=['Target Gene'])
 58    # merge clinical scores and KG features
 59    y = y.join(X, how = 'right')
 60    # add negative targets as the number of positive
 61    y_pos = y[~y['Clinical Score'].isna()]
 62    try:
 63        y_pos['Clinical Score Binned'] = qcut(y_pos['Clinical Score'], q=bins, labels=['target_' + str(x) for x in list(range(bins))])
 64    except:
 65        y_pos['Clinical Score Binned'] = "target"
 66        print("Binning of cancer targets didn't work, using only one bin!")
 67    # prepare pathway genes
 68    y_pg = DataFrame()
 69    if pathway_genes:
 70        pathway_genes = [g for g in pathway_genes if g not in y_pos.index.tolist() and g not in known_targets]
 71        y_pg = y[y.index.isin(pathway_genes)]
 72        y_pg['Clinical Score'] = 1
 73        y_pg['Clinical Score Binned'] = 'pathway_gene'
 74    else:
 75        print("No pathway genes were provided!")
 76    y_neg = y[(y['Clinical Score'].isna()) & (~y.index.isin(pathway_genes)) & (~y.index.isin(known_targets))].sample(y_pos.shape[0] + y_pg.shape[0], random_state=rand_seed)
 77    y_neg['Clinical Score'] = -1
 78    y_neg['Clinical Score Binned'] = 'not_target'
 79    y = concat([y_pos, y_neg, y_pg])
 80    # shuffle targets
 81    y = y.sample(frac=1, random_state=rand_seed)
 82
 83    # CREATE OBJECTS FOR MODELLING
 84    # create X for modelling
 85    X_out = y.iloc[:,1:-1]
 86    X_out.index = y.index
 87    # create y for modelling
 88    y_out = y['Clinical Score']
 89    y_out.index = y.index
 90    y_encoded = y['Clinical Score Binned']
 91    y_encoded.index = y.index
 92    # binarise y for binary classification
 93    y_binary = (y_out > 0).astype(int)
 94
 95    return({
 96        'X': X_out,
 97        'y': y_out,
 98        'y_encoded': y_encoded,
 99        'y_binary': y_binary
100    })

Prepare and preprocess data for machine learning model training.

This function processes knowledge graph (KG) features and clinical scores to create training datasets for drug target prediction models. It handles positive targets, negative samples, and pathway genes, then returns formatted features and labels.

Parameters
  • X: Knowledge graph features with genes as columns
  • y: Clinical data containing target genes and clinical scores
  • pathway_genes: List of pathway-associated genes to include as a separate class
  • known_targets: List of known target genes to exclude from negative sampling
  • term_num: Number of random KG features to sample; if None, uses all features
  • bins: Number of bins for discretizing positive target clinical scores
  • rand_seed: Random seed for reproducibility
Returns

Dictionary containing:

  • X: Feature matrix (KG features) for modeling
  • y: Continuous clinical scores (log2-transformed)
  • y_encoded: Categorical labels with binned targets and pathway genes
  • y_binary: Binary labels (1 for targets/pathway genes, 0 for non-targets)
Raises
  • Exception: May raise exceptions during quantile binning if insufficient unique values exist
  • clinical scores are log2-transformed as log2(score + 1)
  • Positive targets are binned into categories: target_0, target_1, etc.
  • Negative samples are randomly selected to match the number of positive + pathway genes
  • Pathway genes are assigned a clinical score of 1 and labeled as pathway_gene
  • The function sets pandas chained assignment warnings to None
def run( X: pandas.core.frame.DataFrame, y: pandas.core.frame.DataFrame, y_slot='y_binary', bins: int = 3, pathway_genes: List = [], classifier: sklearn.ensemble._forest.RandomForestClassifier = RandomForestClassifier(), cv: sklearn.model_selection._split.StratifiedKFold = StratifiedKFold(n_splits=5, random_state=None, shuffle=False), scoring='roc_auc', n_iterations=10, shuffle_scores=False) -> List:
102def run(X: DataFrame, y: DataFrame, y_slot = 'y_binary', bins: int = 3, pathway_genes: List = [], classifier: RandomForestClassifier = RandomForestClassifier(), cv: StratifiedKFold = StratifiedKFold(), scoring = 'roc_auc', n_iterations = 10, shuffle_scores = False) -> List:
103    """
104    Perform cross-validation pipeline for classification tasks.
105
106    This function executes a cross-validation pipeline over multiple iterations,
107    preprocessing data and evaluating a classifier using specified scoring metrics.
108
109    :param X: Input features DataFrame
110    :type X: DataFrame
111    :param y: Target variable DataFrame
112    :type y: DataFrame
113    :param y_slot: Column name in the preprocessed result to use as target variable, defaults to ``y_binary``
114    :type y_slot: str, optional
115    :param bins: Number of bins for data preprocessing, defaults to 3
116    :type bins: int, optional
117    :param pathway_genes: List of pathway genes to be used in preprocessing, defaults to []
118    :type pathway_genes: List, optional
119    :param classifier: Classifier instance to use for cross-validation, defaults to RandomForestClassifier()
120    :type classifier: RandomForestClassifier, optional
121    :param cv: Cross-validation splitting strategy, defaults to StratifiedKFold()
122    :type cv: StratifiedKFold, optional
123    :param scoring: Scoring metric for evaluation, defaults to ``roc_auc``
124    :type scoring: str, optional
125    :param n_iterations: Number of iterations to run the pipeline, defaults to 10
126    :type n_iterations: int, optional
127    :param shuffle_scores: Whether to shuffle target variable for permutation testing, defaults to False
128    :type shuffle_scores: bool, optional
129    :return: List of cross-validation scores from each iteration
130    :rtype: List
131    """
132    score = []
133    for i in range(n_iterations):
134        res = prepare(X, y, bins = bins, pathway_genes = pathway_genes, rand_seed = i)
135        if shuffle_scores:
136            score.append(mean(cross_val_score(classifier, res['X'], res[y_slot].sample(frac=1), scoring=scoring, cv = cv)))
137        else:
138            score.append(mean(cross_val_score(classifier, res['X'], res[y_slot], scoring=scoring, cv = cv)))
139
140    return(score)

Perform cross-validation pipeline for classification tasks.

This function executes a cross-validation pipeline over multiple iterations, preprocessing data and evaluating a classifier using specified scoring metrics.

Parameters
  • X: Input features DataFrame
  • y: Target variable DataFrame
  • y_slot: Column name in the preprocessed result to use as target variable, defaults to y_binary
  • bins: Number of bins for data preprocessing, defaults to 3
  • pathway_genes: List of pathway genes to be used in preprocessing, defaults to []
  • classifier: Classifier instance to use for cross-validation, defaults to RandomForestClassifier()
  • cv: Cross-validation splitting strategy, defaults to StratifiedKFold()
  • scoring: Scoring metric for evaluation, defaults to roc_auc
  • n_iterations: Number of iterations to run the pipeline, defaults to 10
  • shuffle_scores: Whether to shuffle target variable for permutation testing, defaults to False
Returns

List of cross-validation scores from each iteration