# emcAdr : Evolutionary Markov Chain for Adverse Drug Reaction

    library(emcAdr)
    #> Welcome to this Emc algorithm package ! If you notice any unexpected behavior of a method, please bring it to my attention (<jules.bangard@etu.unistra.fr>).
    library(gridExtra)
    library(ggplot2)

## Aim

The general purpose of this package is to detect drug and drug interactions that could be linked to an Adverse Event (AE). The expected data input for the function of this package are the following.

## Data

    data("ATC_Tree_UpperBound_2024") ## The ATC tree containing the upper bound
    head(ATC_Tree_UpperBound_2024)
    #>   ATCCode                            Name ATC_length upperBound
    #> 1       A alimentary tract and metabolism          1        858
    #> 2     A01     stomatological preparations          3         49
    #> 3    A01A     stomatological preparations          4         49
    #> 4   A01AA      caries prophylactic agents          5         10
    #> 5 A01AA01                 sodium fluoride          7          5
    #> 6 A01AA02      sodium monofluorophosphate          7          6

    data("FAERS_myopathy") ## The Individual Case Safety Reports in the following format
    head(FAERS_myopathy) ## First column is "patientATC" containing vector of index of drug intake for patient at row i in the ATC tree.
    #>                                                         patientATC patientADR
    #> 1                                                             4533      FALSE
    #> 2                                                             4329      FALSE
    #> 3                                                             4784      FALSE
    #> 4 2333, 2997, 4666, 4692, 4724, 4732, 4772, 5092, 5249, 5840, 5841      FALSE
    #> 5                         1691, 1806, 1854, 1907, 3003, 3152, 3166      FALSE
    #> 6                                                        903, 1417      FALSE
    ## Second column is "patientADR", value is true if the patient at row i experience the considered AE and false otherwise.

## Score distribution estimation

This data enables us to run the MCMC algorithm in order to estimate the score distribution among this dataset of patients.

    estimated_distribution_size1_300ksteps_t500 <- DistributionApproximation(10,
                            ATC_Tree_UpperBound_2024, FAERS_myopathy,
                            temperature = 500, nbResults = 200,
                            Smax = 1,num_thread = 8)
    #> openMP available

Here we estimate the distribution of the score among cocktails of size 1 `(Smax=1)`.

If desired, we can compute the true distribution of score for size 1 and size 2 cocktails. Let’s do it for size one cocktails in order to compare our estimation with the truth.

    true_distribution_size1 <- trueDistributionDrugs(ATC_Tree_UpperBound_2024, FAERS_myopathy,
                                    beta = 4, num_thread = 8)

## Visualize and compare the results

    qq_plot_output(estimated_distribution_size1_300ksteps_t500,
                   true_distribution_size1, filtered = T)

Here is the quantile-quantile plot to compare the estimation vs the true distribution. We can also plot both distribution

    plot_estimated_distribution <- plot_frequency(
        estimated_distribution_size1_300ksteps_t500$Filtered_score_distribution[2:length(estimated_distribution_size1_300ksteps_t500$Filtered_score_distribution)], binwidth = .3, sqrt = F, xlab = "H(C)") + labs(title = "Estimated distribution of risks among size 1 cocktails") + theme(plot.title = element_text(size=20)) + ylim(c(0,0.35))

    plot_true_distribution <- plot_frequency(
        true_distribution_size1$Filtered_score_distribution[2:length(true_distribution_size1$Filtered_score_distribution)], binwidth = .3, xlab = "H(C)") + labs(title = "True Distribution of Risks among size 1 cocktails") + theme(plot.title = element_text(size=20)) + ylim(c(0,0.35))

    grid.arrange(plot_estimated_distribution, plot_true_distribution ,nrow = 2)
    #> Warning: Removed 2 rows containing missing values or values outside the scale range
    #> (`geom_bar()`).

## Find cocktails with higher score

We can apply the genetic algorithm in order to find riskiest drug cocktails.

    genetic_algorithm_results <- GeneticAlgorithm(epochs = 20, nbIndividuals = 100,
                                                  ATC_Tree_UpperBound_2024, FAERS_myopathy,
                                                  num_thread = 2, diversity = T,
                                                  p_mutation = 0.2, alpha = 1.5)
    #> epoch : 0 | mean : 0.00509511 | best score : 0.509511 | best cocktail : 4109 3217
    #> epoch : 1 | mean : 0 | best score : -0 | best cocktail : 826 2830 1787 4491
    #> epoch : 2 | mean : 0.00794073 | best score : 0.397036 | best cocktail : 4109
    #> epoch : 3 | mean : 0.00997361 | best score : 0.332454 | best cocktail : 4110
    #> epoch : 4 | mean : 0.00927101 | best score : 0.309034 | best cocktail : 4109
    #> epoch : 5 | mean : 0.0401577 | best score : 0.833939 | best cocktail : 4112
    #> epoch : 6 | mean : 0.052531 | best score : 0.436763 | best cocktail : 4112
    #> epoch : 7 | mean : 0.0561617 | best score : 0.19752 | best cocktail : 4112
    #> epoch : 8 | mean : 0.0557976 | best score : 0.109974 | best cocktail : 4112
    #> epoch : 9 | mean : 0.0366881 | best score : 0.0730608 | best cocktail : 4096
    #> epoch : 10 | mean : 0.0397563 | best score : 0.0642737 | best cocktail : 4096
    #> epoch : 11 | mean : 0.0408007 | best score : 0.0600362 | best cocktail : 4096
    #> epoch : 12 | mean : 0.0435734 | best score : 0.0655201 | best cocktail : 4077
    #> epoch : 13 | mean : 0.0459531 | best score : 0.0626359 | best cocktail : 4077
    #> epoch : 14 | mean : 0.0482383 | best score : 0.0563427 | best cocktail : 4077 6395
    #> epoch : 15 | mean : 0.0488505 | best score : 0.0971556 | best cocktail : 4096 1462
    #> epoch : 16 | mean : 0.0516229 | best score : 0.105453 | best cocktail : 4096 1462
    #> epoch : 17 | mean : 0.0518819 | best score : 0.10402 | best cocktail : 4096 1467
    #> epoch : 18 | mean : 0.0534572 | best score : 0.0993622 | best cocktail : 4096 1462
    #> epoch : 19 | mean : 0.056275 | best score : 0.100011 | best cocktail : 4096 1462

It is now possible to compute the p-value of each found solutions

    ## We put the estimation of risk distribution of each cocktails size in a list
    distribution_list <- list(estimated_distribution_size1_300ksteps_t500)

    p_values <- p_value_genetic_results(distribution_list, genetic_algorithm_results)
    p_values
    #>   [1] NaN NaN NaN NaN NaN Inf NaN NaN NaN NaN NaN NaN Inf NaN NaN NaN NaN NaN
    #>  [19] NaN Inf NaN NaN NaN NaN NaN NaN NaN NaN NaN Inf Inf NaN NaN NaN NaN Inf
    #>  [37] NaN NaN NaN NaN Inf Inf NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
    #>  [55] NaN NaN NaN NaN Inf NaN NaN NaN Inf NaN NaN NaN NaN NaN NaN Inf Inf NaN
    #>  [73] NaN NaN NaN NaN NaN Inf NaN NaN NaN NaN Inf NaN NaN NaN NaN NaN NaN Inf
    #>  [91] NaN NaN NaN NaN Inf NaN Inf Inf Inf NaN