liveBook · Manning

QSAR model

As explained in Build AI Drug Discovery Pipelines

QSAR modeling predicts molecular properties or biological activities from quantitative relationships between chemical structure and measured outcomes.

A quantitative structure–activity relationship (QSAR) model relates molecular structure to measured biological activity. It can help screen compounds or suggest which structural properties to change when optimizing activity. QSAR models need not use machine learning; quantitative structure–property relationship (QSPR) is the broader term for relationships with any molecular property. section 2.1.1 section 6.3.2

A model represents each molecule using descriptors: numerical measurements of its structural properties. It learns a relationship between those descriptors and an experimental endpoint, such as binding to an RNA target. The goal is to make useful predictions for compounds not used in training, not merely to fit known examples. section 6.0 section 2.1.1

A practical small-data workflow for predicting RNA binding uses these steps:

  • Calculate structural descriptors and choose measured binding endpoints. Include possible molecular orientations when computing descriptors. section 6.0 section 6.5
  • Split the data and fit scaling on the training set. Use cross-validated Lasso regression to select descriptors by shrinking some coefficients to zero. section 6.3.1
  • Fit multiple linear regression models to combinations of selected descriptors. Limit model complexity: one rule of thumb recommends at least five training compounds per descriptor. section 6.3.1
  • Compare fit and predictive performance, check descriptor significance, and prefer a simpler model when candidates otherwise tie. With only about 48 ligands, treat results cautiously; using test performance to select models also makes that test less independent as a final assessment. section 6.3.1
Listing 6.7 Lasso Feature Selection and Exhaustive MLR Search
def mask_train_test_split(X, y, test_idxs):
    mask = y.index.isin(test_idxs)
    sc = StandardScaler() 
    X_train = sc.fit_transform(X[~mask])
    X_test = sc.transform(X[mask])
    return X_train, X_test, y[~mask], y[mask]
 
def lasso_feature_selection(X_train, X_test, y_train, y_test):
 
    param_grid = {'alpha': np.logspace(2, -6, num=1000)}
    lasso = Lasso()
 
    grid_search = GridSearchCV(
        lasso, param_grid, cv=5, scoring='r2', n_jobs=-1
    )
    grid_search.fit(X_train, y_train)
 
    best_alpha = grid_search.best_params_['alpha']
    best_model = grid_search.best_estimator_
 
    y_pred = best_model.predict(X_test)
    r2 = r2_score(y_test, y_pred)
 
    print(f"Best alpha: {best_alpha}")
    print(f"Best R2: {r2}")
 
    selected_features = np.where(best_model.coef_ != 0)[0]
    return best_alpha, selected_features
 
def compute_feature_pvals(model, X, y):
    # Calculate the standard errors of the coefficients
    n = len(y)
    p = X.shape[1]
    y_pred = model.predict(X)
    residuals = y - y_pred
    mse = np.sum(residuals**2) / (n - p - 1)
    var_b = mse * (np.linalg.inv(np.dot(X.T, X)).diagonal())
    std_err = np.sqrt(var_b)
 
    # Calculate the t-statistics and p-values
    t_stat = model.coef_ / std_err
    p_values = 2 * (1 - stats.t.cdf(np.abs(t_stat), n - p - 1))
 
    return np.round(p_values, 3)
 
def MLR_exhaustive_search(
    X_train, X_test, y_train, y_test,
    selected_features, threshold=0.7, max_descs=7
):
 
    best_score = -np.inf
    best_model = None
    best_p_values = None
    best_features = None
 
    X_train = X_train[:, selected_features]
    X_test = X_test[:, selected_features]
 
    for r in range(2, max_descs+1):
        for feature_combination in itertools.combinations(
            range(X_train.shape[1]), r
        ):
            model = LinearRegression()
            model.fit(X_train[:, feature_combination], y_train)
 
            y_train_pred = model.predict(X_train[:, feature_combination])
            y_test_pred = model.predict(X_test[:, feature_combination])
 
            r2_train = r2_score(y_train, y_train_pred)
            q2_test = r2_score(y_test, y_test_pred)
 
            if r2_train <= threshold or q2_test <= threshold:
                continue
 
            p_values = compute_feature_pvals(
                model, X_train[:, feature_combination], y_train
            )
            if q2_test > best_score and np.all(p_values < 0.05):
                best_score = q2_test
                best_model = model
                best_features = feature_combination
                best_p_values = p_values
 
    best_selected_features = [
        selected_features[feature] for feature in best_features
    ]
 
    print(f"Best Q2: {best_score}")
    print(f"Best Features: {best_features}")
    return best_model, best_selected_features, best_score, best_p_values
 
def paper_MLR_workflow(X, y, test_idxs, threshold=0.7, max_descs=7):
 
    X_train, X_test, y_train, y_test = mask_train_test_split(
        X, y, test_idxs
    )
 
    print("Begin Lasso Feature Selection")
    best_alpha, selected_features = lasso_feature_selection(
        X_train, X_test, y_train, y_test
    )
 
    print("Screen Performative MLR Models")
    return MLR_exhaustive_search(
        X_train, X_test, y_train, y_test,
        selected_features, threshold, max_descs
    )
 
best_model_kon, best_features_kon, best_score_kon, best_p_values_kon = \
    paper_MLR_workflow(
        hiv_tar1_lnkon.drop(columns=["lnkon"]),
        hiv_tar1_lnkon["lnkon"], test_idxs_paper
    )
 
best_model_koff, best_features_koff, best_score_koff, best_p_values_koff = paper_MLR_workflow(hiv_tar1_lnkoff.drop(columns=["lnkoff"]), hiv_tar1_lnkoff["lnkoff"], test_idxs_paper, threshold=0.6)
 
best_model_kd, best_features_kd, best_score_kd, best_p_values_kd = \
    paper_MLR_workflow(
        hiv_tar1_lnkd.drop(columns=["lnKD"]),
        hiv_tar1_lnkd["lnKD"], test_idxs_paper, max_descs=4
    )

The listing implements training-set scaling, Lasso feature selection, and an exhaustive search over linear models. section 6.3.1

Interpretation can turn predictions into testable hypotheses. For example, descriptors counting basic atoms and nitrogen atoms influence a model of the binding dissociation rate. Other descriptors are harder to interpret directly. For regulatory use, important considerations include a defined experimental endpoint, an unambiguous algorithm, a well-defined applicability domain, and appropriate validation. section 6.3.2

RNA-binding versus protein-binding molecules

A classifier can also compare groups rather than predict a single binding measurement. Using 1,670 molecular descriptors, an XGBClassifier distinguished 2,003 RNA-binding molecules from 77,678 protein-binding molecules. In stratified 10-fold cross-validation, its average area under the precision–recall curve was 0.79, compared with reported averages of 0.37 for Lasso and 0.78 for a neural network. section 6.4.1

How Build AI Drug Discovery Pipelines covers it

How deep it goes
Works through descriptor selection, linear model search, validation, and interpretation, with a separate gradient-boosting comparison.
Examples
Uses HIV-1 TAR binding measurements and a comparison of RNA-binding versus protein-binding compounds.
What you can do after
Apply QSAR via Lasso, multiple linear regression, and XGBClassifier to predict or compare molecular binding behavior.
How it connects
Connects QSAR to ligand-based virtual screening, molecular descriptors, Lasso regression, multiple linear regression, and applicability domains.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI Drug Discovery Pipelines. The section links point to the parts of the book it draws on.