QSAR Workflow

library(rQSAR)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> Loading required package: corrplot
#> corrplot 0.95 loaded
#> Loading required package: tibble
#> Loading required package: gridExtra
#> 
#> Attaching package: 'gridExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     combine

Introduction

Quantitative Structure-Activity Relationship (QSAR) modeling is a valuable tool in computational chemistry and drug design, where it aims to predict the activity or property of chemical compounds based on their molecular structure. In this vignette, we present the rQSAR package, which provides functions for variable selection and QSAR modeling using Multiple Linear Regression (MLR), Partial Least Squares (PLS), and Random Forest algorithms.

Background

QSAR modeling relies on mathematical models to establish relationships between molecular descriptors (features representing chemical compounds) and their corresponding biological activities or properties. MLR, PLS, and Random Forest are commonly used algorithms for QSAR modeling, each with its own strengths and applications:

Multiple Linear Regression (MLR): MLR fits a linear equation to the data, allowing us to understand the linear relationship between predictors and the response variable. It is simple, interpretable, and provides insights into the importance of each predictor.

Partial Least Squares (PLS): PLS is a regression technique that combines the features of principal component analysis and MLR. It is particularly useful when dealing with multicollinearity and high-dimensional data, making it suitable for QSAR modeling with correlated predictors.

Random Forest: Random Forest is an ensemble learning method that builds multiple decision trees and combines their predictions to improve accuracy and reduce overfitting. It is robust to outliers and non-linear relationships, making it suitable for complex QSAR modeling tasks. # Basic Usage Generating Molecular Descriptors The generate_descriptors_from_sdf function can be used to generate molecular descriptors from an SDF file.

library(rQSAR)

# Path to the SDF file
sdf_file<-sdf_file <- system.file("sample.sdf", package = "rQSAR")

# Generate descriptors
descriptors <- generate_descriptors_from_sdf(sdf_file)
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer
#> Warning in FUN(X[[i]], ...): The molecule should be of type IBioPolymer

Variable Selection

The perform_variable_selection function allows users to perform variable selection based on a given outcome column in a dataset. This step is crucial for identifying relevant predictors and improving the performance of QSAR models.

descriptors<-system.file("descriptor1.csv", package = "rQSAR")
selected_data <- perform_variable_selection(descriptors, "Outcome_column_name")
#> Warning in leaps.setup(x, y, wt = weights, nbest = nbest, nvmax = nvmax, : 273
#> linear dependencies found
#> Reordering variables and trying again:
#> Warning in leaps.exhaustive(a, really.big = really.big): XHAUST returned error
#> code -999
print(head(selected_data))
#>        Fsp3 Weta2.unity Outcome
#> 1 0.7755102   0.3936616    6.04
#> 2 0.7800000   0.3833549    7.15
#> 3 0.7843137   0.3896566    6.92
#> 4 0.7884615   0.4427484    8.44
#> 5 0.7924528   0.4253881    8.18
#> 6 0.7843137   0.4288922    5.27
write.csv(selected_data, "descriptor2.csv")

This function reads the data from the specified CSV file, extracts predictors and the outcome variable, performs variable selection using the ‘leaps’ package, and returns a dataframe containing the selected variables along with the outcome.

NB: The descriptors (independent variables) starts from the first column while the dependent variable should be placed in the last column. Adjust accordingly. Also, replace “D:/QSAR DATA/rQSAR/inst/descriptor1.csv” with directory where your file is located

Visualization

We can visualize the correlation between selected variables and the outcome using the ‘corrplot’ package:

# Load the selected data
selected_data<-system.file("descriptor2.csv", package = "rQSAR")
selected_data <- read.csv(selected_data, header = TRUE)

# Compute the correlation matrix
correlation_matrix <- cor(selected_data)

# Plot the heatmap
corrplot(correlation_matrix, method = "color", type = "lower", tl.col = "black", tl.srt = 45)

This heatmap provides insights into the correlation structure of the selected variables.

NB: Replace “D:/QSAR DATA/rQSAR/inst/descriptor2.csv” with directory where your file is located

QSAR Modeling

The ‘build_qsar_models’ function builds QSAR models using MLR, PLS, and Random Forest algorithms with k-fold cross-validation. This approach helps to assess the predictive performance of the models and generalize their performance to unseen data.

# Example usage:
data_file <- system.file("descriptor2.csv", package = "rQSAR")
model_results <- build_qsar_models(data_file)
print(model_results)
#> $MLR
#> $MLR$results
#> $MLR$results$predictions
#>        1        2        4        8       17       23       33       34 
#> 5.120501 5.361065 5.915778 6.172501 6.113407 5.091590 6.687538 6.585744 
#>       39       40        6       14       18       20       21       28 
#> 6.683174 6.736475 6.154634 6.394147 6.202500 6.125597 6.326101 6.204348 
#>       31       32       47       49        9       11       12       22 
#> 6.323867 6.404179 6.421681 6.428979 6.207477 6.119419 6.407177 6.184664 
#>       24       30       36       37       43       46        5        7 
#> 6.731189 6.458638 6.682475 6.262385 6.109962 6.107343 6.219848 6.071311 
#>       10       15       16       27       35       41       42        3 
#> 6.439100 6.409383 6.324013 6.220146 6.543246 6.542108 6.220881 5.971172 
#>       13       19       25       26       29       38       44       45 
#> 6.568539 6.263262 5.920423 6.256888 6.040263 6.613179 6.275966 6.223006 
#>       48 
#> 6.508775 
#> 
#> $MLR$results$actuals
#>  [1] 6.04 7.15 8.44 6.44 6.68 5.61 5.74 5.50 5.45 5.84 5.27 7.62 6.27 5.00 7.48
#> [16] 5.15 5.74 6.01 6.11 6.58 6.57 8.54 7.48 7.50 6.01 5.60 6.00 6.16 5.54 6.61
#> [31] 8.18 5.45 8.23 6.19 5.43 6.15 5.66 5.68 5.64 6.92 6.70 5.31 6.06 5.93 4.99
#> [46] 5.81 5.31 6.11 6.71
#> 
#> 
#> $MLR$formula
#> $MLR$formula$MLR
#> train_y ~ Fsp3 + Weta2.unity
#> <environment: 0x555b5a86ee10>
#> 
#> 
#> 
#> $PLS
#> $PLS$results
#> $PLS$results$predictions
#>  [1] 5.517599 5.758073 5.875096 5.946661 6.142695 5.613506 6.809884 6.321487
#>  [9] 6.824363 6.647547 6.267337 6.274439 6.263349 6.302239 6.238090 6.341044
#> [17] 6.301709 6.353853 6.379801 6.326081 6.174841 5.984905 6.303282 6.057091
#> [25] 6.961499 6.501599 6.607872 6.143083 6.115823 6.112925 6.260557 6.072602
#> [33] 6.324637 6.184138 6.257772 6.261966 6.356817 6.351434 6.265443 6.196325
#> [41] 6.472996 6.564293 6.132394 6.430430 6.157533 6.420886 6.333380 6.387746
#> [49] 6.289362
#> 
#> $PLS$results$actuals
#>  [1] 6.04 7.15 8.44 6.44 6.68 5.61 5.74 5.50 5.45 5.84 5.27 7.62 6.27 5.00 7.48
#> [16] 5.15 5.74 6.01 6.11 6.58 6.57 8.54 7.48 7.50 6.01 5.60 6.00 6.16 5.54 6.61
#> [31] 8.18 5.45 8.23 6.19 5.43 6.15 5.66 5.68 5.64 6.92 6.70 5.31 6.06 5.93 4.99
#> [46] 5.81 5.31 6.11 6.71
#> 
#> 
#> $PLS$formula
#> $PLS$formula$PLS
#> train_y ~ Fsp3 + Weta2.unity
#> attr(,"variables")
#> list(train_y, Fsp3, Weta2.unity)
#> attr(,"factors")
#>             Fsp3 Weta2.unity
#> train_y        0           0
#> Fsp3           1           0
#> Weta2.unity    0           1
#> attr(,"term.labels")
#> [1] "Fsp3"        "Weta2.unity"
#> attr(,"order")
#> [1] 1 1
#> attr(,"intercept")
#> [1] 1
#> attr(,"response")
#> [1] 1
#> attr(,"predvars")
#> list(train_y, Fsp3, Weta2.unity)
#> attr(,"dataClasses")
#>     train_y        Fsp3 Weta2.unity 
#>   "numeric"   "numeric"   "numeric" 
#> <environment: 0x555b5a86ee10>
#> 
#> 
#> 
#> $Random_Forest
#> $Random_Forest$results
#> $Random_Forest$results$predictions
#>        1        2        4        8       17       23       33       34 
#> 5.985913 5.488108 6.040990 6.037438 5.948343 5.626310 6.649131 7.072931 
#>       39       40        6       14       18       20       21       28 
#> 6.636478 6.304557 6.449546 6.402560 6.592511 6.478428 6.250727 6.377901 
#>       31       32       47       49        9       11       12       22 
#> 6.135709 6.663884 6.095678 6.535379 5.695478 6.093356 6.367341 6.237664 
#>       24       30       36       37       43       46        5        7 
#> 5.778701 6.523424 6.613317 6.150720 5.989353 5.986830 6.092161 6.243621 
#>       10       15       16       27       35       41       42        3 
#> 6.351121 6.653265 6.079401 6.092161 6.534781 6.530591 6.092161 5.780296 
#>       13       19       25       26       29       38       44       45 
#> 6.671978 5.960340 5.620827 7.076002 5.943553 7.097230 6.496444 6.872345 
#>       48 
#> 6.511334 
#> 
#> $Random_Forest$results$actuals
#>  [1] 6.04 7.15 8.44 6.44 6.68 5.61 5.74 5.50 5.45 5.84 5.27 7.62 6.27 5.00 7.48
#> [16] 5.15 5.74 6.01 6.11 6.58 6.57 8.54 7.48 7.50 6.01 5.60 6.00 6.16 5.54 6.61
#> [31] 8.18 5.45 8.23 6.19 5.43 6.15 5.66 5.68 5.64 6.92 6.70 5.31 6.06 5.93 4.99
#> [46] 5.81 5.31 6.11 6.71
#> 
#> 
#> $Random_Forest$formula
#> $Random_Forest$formula$Random_Forest
#> [1] "randomForest(x = train_X, y = train_y)"

This function reads the data from the specified CSV file, splits it into training and testing sets, builds QSAR models using MLR, PLS, and Random Forest, and returns the model predictions along with the actual values.

Visualization

We can visualize the performance of QSAR models using correlation plots and residual plots:

# Correlation plots
plots <- correlation_plots(model_results)
for (i in seq_along(plots)) {
  print(plots[[i]])
}
#> `geom_smooth()` using formula = 'y ~ x'

#> `geom_smooth()` using formula = 'y ~ x'

#> `geom_smooth()` using formula = 'y ~ x'


# Residual plots
plots <- residual_plots(model_results)
grid.arrange(grobs = plots, ncol = 2)

These plots help evaluate the predictive performance of QSAR models.

Conclusion

In this vignette, we introduced the rQSAR package for QSAR modeling using MLR, PLS, and Random Forest algorithms. By leveraging variable selection techniques and cross-validation, users can build robust QSAR models for predicting chemical properties or activities. For further details and examples, please refer to the package documentation.