## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup, eval=FALSE--------------------------------------------------------
# library(OdysseusPathwayModule)
# library(Eunomia)
# 
# connectionDetails <- Eunomia::getEunomiaConnectionDetails()

## ----cohorts, eval=FALSE------------------------------------------------------
# generationSet <- Eunomia::createCohorts(connectionDetails)
# 
# generationSet

## ----post_index, eval=FALSE---------------------------------------------------
# postIndexResults <- executeCohortPathways(
#   connectionDetails = connectionDetails,
#   cohortDatabaseSchema = "main",
#   cohortTableName = "cohort",
#   targetCohortIds = 4,
#   eventCohortIds = c(1, 2, 3),
#   maxDepth = 3,
#   collapseWindow = 30
# )

## ----post_index_names, eval=FALSE---------------------------------------------
# names(postIndexResults)

## ----post_index_inspect, eval=FALSE-------------------------------------------
# head(postIndexResults$pathwaysAnalysisPathsData)
# head(postIndexResults$pathwayAnalysisCodesLong)

## ----pre_index, eval=FALSE----------------------------------------------------
# preIndexResults <- executeCohortPathways(
#   connectionDetails = connectionDetails,
#   cohortDatabaseSchema = "main",
#   cohortTableName = "cohort",
#   targetCohortIds = 4,
#   eventCohortIds = c(1, 2, 3),
#   analysisType = "pre-index",
#   lookbackStartDay = -365,
#   lookbackEndDay = -1,
#   maxDepth = 3,
#   collapseWindow = 30
# )

## ----pre_index_90, eval=FALSE-------------------------------------------------
# preIndex90 <- executeCohortPathways(
#   connectionDetails = connectionDetails,
#   cohortDatabaseSchema = "main",
#   cohortTableName = "cohort",
#   targetCohortIds = 4,
#   eventCohortIds = c(1, 2, 3),
#   analysisType = "pre-index",
#   lookbackStartDay = -90,
#   lookbackEndDay = -1,
#   maxDepth = 3
# )

## ----code_mapping, eval=FALSE-------------------------------------------------
# subset(
#   postIndexResults$pathwayAnalysisCodesLong,
#   select = c(pathwayAnalysisGenerationId, code, targetCohortId, eventCohortId, isCombo, numberOfEvents)
# )

## ----separate_tables, eval=FALSE----------------------------------------------
# connection <- DatabaseConnector::connect(connectionDetails)
# 
# DatabaseConnector::executeSql(connection, "DROP TABLE IF EXISTS target_cohorts;")
# DatabaseConnector::executeSql(connection, "DROP TABLE IF EXISTS event_cohorts;")
# 
# DatabaseConnector::executeSql(
#   connection,
#   "CREATE TABLE target_cohorts AS
#      SELECT *
#      FROM main.cohort
#      WHERE cohort_definition_id = 4;"
# )
# 
# DatabaseConnector::executeSql(
#   connection,
#   "CREATE TABLE event_cohorts AS
#      SELECT *
#      FROM main.cohort
#      WHERE cohort_definition_id IN (1, 2, 3);"
# )
# 
# resultsSeparateTables <- executeCohortPathways(
#   connectionDetails = connectionDetails,
#   cohortDatabaseSchema = "main",
#   cohortTableName = "target_cohorts",
#   outcomeDatabaseSchema = "main",
#   outcomeTableName = "event_cohorts",
#   targetCohortIds = 4,
#   eventCohortIds = c(1, 2, 3)
# )
# 
# DatabaseConnector::disconnect(connection)

## ----esg_mock_data, eval=FALSE------------------------------------------------
# # --- Mock cpResults with multi-step pathways ---
# # Bitmask combo codes: 2 = Celecoxib, 4 = Diclofenac, 8 = GiBleed
# mockPathsData <- data.frame(
#   pathwayAnalysisGenerationId = rep(1L, 5),
#   targetCohortId              = rep(4L, 5),
#   step1      = c( 2L,  2L,  4L,  4L,  2L),
#   step2      = c( 4L,  8L,  2L,  8L, NA),
#   step3      = c( 8L, NA,   8L, NA,  NA),
#   countValue = c(120L, 80L, 95L, 65L, 40L)
# )
# 
# mockCodesLong <- data.frame(
#   pathwayAnalysisGenerationId = rep(1L, 3),
#   code           = c(2L, 4L, 8L),
#   targetCohortId = rep(4L, 3),
#   eventCohortId  = c(1L, 2L, 3L),
#   isCombo        = rep(0L, 3),
#   numberOfEvents = rep(1L, 3)
# )
# 
# mockIsCombo <- data.frame(
#   targetCohortId = rep(4L, 3),
#   comboId        = c(2L, 4L, 8L),
#   numberOfEvents = rep(1L, 3),
#   isCombo        = rep(0L, 3)
# )
# 
# mockCpResults <- list(
#   pathwayAnalysisStatsData   = data.frame(
#     pathwayAnalysisGenerationId = 1L,
#     targetCohortId = 4L,
#     countValue = 400L
#   ),
#   pathwaysAnalysisPathsData  = mockPathsData,
#   pathwaysAnalysisEventsData = data.frame(eventCohortId = 1:3, countValue = c(240L, 215L, 360L)),
#   pathwaycomboIds            = data.frame(comboIds = c(2L, 4L, 8L)),
#   pathwayAnalysisCodesLong   = mockCodesLong,
#   isCombo                    = mockIsCombo,
#   pathwayAnalysisCodesData   = data.frame(
#     pathwayAnalysisGenerationId = rep(1L, 3),
#     code    = c(2L, 4L, 8L),
#     isCombo = rep(0L, 3)
#   )
# )

## ----event_sequence_graph, eval=FALSE-----------------------------------------
# # Map cohort IDs to human-readable names
# generationSet <- data.frame(
#   cohortId   = c(1L, 2L, 3L, 4L),
#   cohortName = c("Celecoxib", "Diclofenac", "GiBleed", "NSAIDs")
# )
# 
# esg <- buildEventSequenceGraph(
#   cpResults     = mockCpResults,
#   generationSet = generationSet,
#   maxSteps      = 3,
#   minCount      = 1
# )
# 
# # Print a summary
# esg

## ----esg_real_note, eval=FALSE------------------------------------------------
# # With real data:
# # generationSet <- Eunomia::createCohorts(connectionDetails)
# # generationSet$cohortName <- generationSet$name
# #
# # esg <- buildEventSequenceGraph(
# #   cpResults     = postIndexResults,
# #   generationSet = generationSet,
# #   maxSteps      = 3,
# #   minCount      = 5
# # )

## ----graph_components, eval=FALSE---------------------------------------------
# # The igraph object — vertices are (event, step) pairs, edges are transitions
# ig <- esg$graph
# 
# # Vertex attributes
# igraph::V(ig)$eventName   # human-readable event names
# igraph::V(ig)$step        # pathway step number
# igraph::V(ig)$count       # patient count at this node
# igraph::V(ig)$share       # share within the step (sums to 1)
# 
# # Edge attributes
# igraph::E(ig)$weight      # patient count crossing this transition
# igraph::E(ig)$probability # transition probability (sums to 1 per source)
# igraph::E(ig)$sourceStep
# igraph::E(ig)$targetStep
# 
# # Decoded pathways
# head(esg$sequences)
# 
# # Summary statistics
# esg$summary

## ----plot_esg, eval=FALSE-----------------------------------------------------
# # Default plot
# plot(esg)
# 
# # Customized plot
# plot(esg,
#   colorPalette    = c("#1b9e77", "#d95f02", "#7570b3"),
#   edgeWidthRange  = c(1, 10),
#   vertexSizeRange = c(10, 30),
#   main            = "Post-Index Treatment Pathways"
# )

## ----transition_probs, eval=FALSE---------------------------------------------
# # Extract edge data frame
# edgeDf <- igraph::as_data_frame(esg$graph, what = "edges")
# 
# # View transitions from Step 1 to Step 2
# edgeDf[edgeDf$sourceStep == 1, ]

## ----igraph_analysis, eval=FALSE----------------------------------------------
# ig <- esg$graph
# 
# # Out-degree: how many distinct next-step events each node leads to
# igraph::degree(ig, mode = "out")
# 
# # Weighted betweenness (inverse weight = lower traffic → higher betweenness)
# igraph::betweenness(ig, weights = 1 / igraph::E(ig)$weight)
# 
# # Shortest weighted paths between all pairs
# igraph::distances(ig, weights = 1 / igraph::E(ig)$weight)
# 
# # Identify hubs and authorities (HITS)
# igraph::hub_score(ig, weights = igraph::E(ig)$weight)$vector
# igraph::authority_score(ig, weights = igraph::E(ig)$weight)$vector
# 
# # Export to data frames for use outside igraph
# vertDf <- igraph::as_data_frame(ig, what = "vertices")
# edgeDf <- igraph::as_data_frame(ig, what = "edges")

