# ===============================================
# USER CONFIGURATION
# ===============================================
MAKEFLAGS += --output-sync

# ===============================================
# MODELS SELECTION PARALLEL RUN
# ===============================================
MODEL ?= model1 # Single model
MODELS = modelA modelB modelC # Multiple models for parallel run

# ===============================================
# PER-MODEL OVERRIDES
# ===============================================
ifeq ($(MODEL), modelA)
  PHENO_FILE = $(DATA_DIR)/$(STEP1)/phenoA.csv

else ifeq ($(MODEL), modelB)
  PHENO_FILE = $(DATA_DIR)/$(STEP1)/phenoB.csv

else ifeq ($(MODEL), modelC)
  PHENO_FILE = $(DATA_DIR)/$(STEP1)/phenoC.csv

else # default (model1)
  PHENO_FILE = $(DATA_DIR)/$(STEP1)/pheno.csv

endif

# ===============================================
# DIRECTORIES
# ===============================================
LOGS_DIR = logs
DATA_DIR = data
RDATA_DIR = rData
RESULTS_DIR = results
FIGURES_DIR = figures
STEP1 = preprocessingMinfiEwasWater
STEP2 = svaEnmix
STEP3 = preprocessingPheno
STEP4 = methylationGLM
STEP5 = methylationLME
STEP6 = reports
METRICS_DIR = metrics
IDAT_DIR = idats
OBJ_DIR = objects
MERGE_DIR = mergeData
MODEL_DIR = models
CPG_DIR = cpgs
SUMMARY_DIR = summary
ENMIX_DIR = enmix
SVA_DIR = sva
QC_DIR = qc
R_DIR = ~/R/x86_64-pc-linux-gnu-library/4.4

# ===============================================
# GLOBAL PARAMETERS
# ===============================================
# STEP 1-5 PARAMETERS
SEED = 123
SEP_TYPE = NULL
SAMPLE_ID = Sample_Name
N_SAMPLES = 30
ARRAY_TYPE = IlluminaHumanMethylationEPICv2
ANNOTATION_VERSION = 20a1.hg38
IDAT_FORCE = FALSE
TIFF_WIDTH = 2000
TIFF_HEIGHT = 1000
TIFF_RES = 150
SEX_COLUMN = Sex
REMOVE_SEX_MISMATCH = FALSE
SENTRIX_ID_COLUMN = Sentrix_ID
SENTRIX_POSITION_COLUMN = Sentrix_Position
BASENAME_COLUMN = Basename
TIME_VAR = Timepoint
METHYLATION_SCALE = beta
PHENO_ORDER = $(SAMPLE_ID);$(TIME_VAR);$(SEX_COLUMN);PredSex;$(BASENAME_COLUMN);$(SENTRIX_ID_COLUMN);$(SENTRIX_POSITION_COLUMN)

# STEP 4-5 PARAMETERS
COVARIATES = Sex
FACTOR_VARS = Sex,TreatmentGroup
SCALE_VARS = NULL
SCALE_VARS_GLM = $(SCALE_VARS)
SCALE_VARS_LME = $(SCALE_VARS)
CPG_PREFIX = cg
CPG_LIMIT = NA
PRS_MAP = NULL
SUMMARY_PVAL = NA
N_CORES = 64
SAVE_TXT_SUMMARIES = TRUE
CHUNK_SIZE = 10000
FDR_THRESHOLD = 0.05
PADJ_METHOD = fdr
ANNOTATION_PACKAGE = IlluminaHumanMethylationEPICv2anno.20a1.hg38
ANNOTATION_COLS = Name,chr,pos,UCSC_RefGene_Group,UCSC_RefGene_Name,Relation_to_Island,GencodeV41_Group

# ===============================================
# STEP 1 PARAMETERS
# ===============================================
QC_CUTOFF = 10.5
DET_PTYPE = m+u
DET_PTHRESHOLD = 0.05
PVAL_THRESHOLD = 0.01
CHR_TO_REMOVE = chrX,chrY
SNPS_TO_REMOVE = SBE,CpG
PROBE_EXCLUSION_FILE = $(DATA_DIR)/$(STEP1)/12864_2024_10027_MOESM8_ESM.csv
PROBE_EXCLUSION_ID_COLUMN = NULL
USE_EPICV2_MANIFEST = FALSE
EPICV2_MANIFEST_CH_WGBS_EVIDENCE = TRUE
EPICV2_MANIFEST_CH_BLAT = TRUE
EPICV2_MANIFEST_MISSING_POS = TRUE
EPICV2_MANIFEST_MISMATCH_POS = FALSE
MAF_THRESHOLD = 0.1
PLOT_GROUP_VAR = TreatmentGroup
LC_REF = salivaEPIC

# ===============================================
# STEP 2 PARAMETERS
# ===============================================
CTRL_SVA_PERC_VAR = 0.90
CTRL_SVA_FLAG = 1

# ===============================================
# STEP 3 PARAMETERS
# ===============================================
TIMEPOINTS = 1,2
COMBINE_TIMEPOINTS = 1,2

# ===============================================
# STEP 4 PARAMETERS
# ===============================================
PHENOTYPES_GLM = TreatmentGroup
GLM_LIBS = glm2
INTERACTION_GLM = NULL
SUMMARY_RESIDUAL_SD = TRUE
SAVE_SIGNIFICANT_CPGS = TRUE
SIGNIFICANT_CPG_PVAL = 0.1

# ==============================================
# STEP 5 PARAMETERS
# ==============================================
PHENOTYPES_LME = TreatmentGroup
PERSON_VAR = person
LME_LIBS = lme4,lmerTest
LME_CORRELATION_STRUCTURE = none
LME_CORRELATION_VAR = NULL
INTERACTION_LME = NULL
SAVE_SIGNIFICANT_INTERACTIONS = TRUE
SIGNIFICANT_INTERACTION_PVAL = 0.1

# ===============================================
# ARGUMENT NORMALISATION
# ===============================================

ifeq ($(PRS_MAP),NULL)
  PRS_MAP_ARG = NULL
else
  PRS_MAP_ARG = '$(PRS_MAP)'
endif

ifeq ($(strip $(SEP_TYPE)),)
  SEP_TYPE_ARG = NULL
else
ifeq ($(strip $(SEP_TYPE)),NULL)
  SEP_TYPE_ARG = NULL
else
  SEP_TYPE_ARG = '$(SEP_TYPE)'
endif
endif

ifeq ($(R_DIR),NULL)
  R_DIR_ARG = NULL
else
  R_DIR_ARG = '$(R_DIR)'
endif

ifeq ($(strip $(PROBE_EXCLUSION_ID_COLUMN)),)
  PROBE_EXCLUSION_ID_COLUMN_ARG = NULL
else
ifeq ($(strip $(PROBE_EXCLUSION_ID_COLUMN)),NULL)
  PROBE_EXCLUSION_ID_COLUMN_ARG = NULL
else
  PROBE_EXCLUSION_ID_COLUMN_ARG = '$(PROBE_EXCLUSION_ID_COLUMN)'
endif
endif

ifeq ($(INTERACTION_GLM),NULL)
  INTERACTION_GLM_ARG = NULL
else
  INTERACTION_GLM_ARG = '$(INTERACTION_GLM)'
endif

ifeq ($(INTERACTION_LME),NULL)
  INTERACTION_LME_ARG = NULL
else
  INTERACTION_LME_ARG = '$(INTERACTION_LME)'
endif

ifeq ($(strip $(LME_CORRELATION_VAR)),)
  LME_CORRELATION_VAR_ARG = NULL
else
ifeq ($(strip $(LME_CORRELATION_VAR)),NULL)
  LME_CORRELATION_VAR_ARG = NULL
else
  LME_CORRELATION_VAR_ARG = '$(LME_CORRELATION_VAR)'
endif
endif

# ===============================================
# INCLUDE PIPELINE FROM dnaEPICO
# ===============================================
DNAPIPE_EMPTY :=
DNAPIPE_SPACE := $(DNAPIPE_EMPTY) $(DNAPIPE_EMPTY)
DNAPIPE_MK_RAW := $(shell Rscript -e "cat(system.file('extdata','make','Makefile.rules.pipeline',package='dnaEPICO'))")
DNAPIPE_MK := $(subst $(DNAPIPE_SPACE),\ ,$(DNAPIPE_MK_RAW))
include $(DNAPIPE_MK)

