# UseCase3_1 : PD model with PK parameters passed as input variables to MODEL PREDICTION.
# Population pharmacokinetic and pharmacodynamic model to describe warfarin and PCA response
# Dosing regimen: single oral administration
# Structural model 
#   - 1 compartment model (CL,V and TLAG)
#   - Indirect response model + inhibitory EMAX model on synthesis
# Covariate model : Weight on V and CL 
# Variability model: 
#   - IIV on CL, V, ka, TLAG,PCA0, C50 and TEQ
#   - Correlation between CL and V random variables
#   - PK: Combined residual error model 
#   - PD: Additive residual error model
# Passing PK parameters from the dataset to drive the PK model

warfarin_PKPD_turnover_dat = dataObj{
     DECLARED_VARIABLES{PCA_obs::observation; INPUT_KA::dosingTarget }

     DATA_INPUT_VARIABLES{
      ID : 	 { use is id }
      TIME : { use is idv }
      WT : 	 { use is covariate }
      AGE :  { use is covariate }
      SEX :  { use is catCov withCategories{female when 0, male when 1} }
      AMT :  { use is amt, variable = INPUT_KA }
      DVID : { use is dvid }
      DV :   { use is dv, variable=PCA_obs }
      MDV :  { use is mdv }
      CL : { use is variable }
      V : { use is variable }
      KA : { use is variable }
      TLAG : { use is variable }
   }# end DATA_INPUT_VARIABLES

   SOURCE{
      srcfile : {file="warfarin_conc_pca_PKparam.csv", 
      			 inputFormat is nonmemFormat }
   }# end SOURCE
} # end data object


warfarin_PKPD_turnover_prior = priorObj {

	PRIOR_PARAMETERS{
		MU_CL=ln(0.1)
		MU_V=ln(8)
		MU_KA=ln(0.362)
		MU_TLAG=ln(1)
		MU_PPV_CL=ln(0.1)
		MU_PPV_V=ln(0.1)
		MU_PPV_KA=ln(0.1)
		MU_CORR_CL_V=ln(0.01)
		MU_RUV_PROP=ln(0.1)
		MU_RUV_ADD=ln(0.1)

		VAR_CL=ln(0.1^2)^2
		VAR_V=ln(8^2)^2
		VAR_KA=ln(0.362^2)^2
		VAR_TLAG=ln(1^2)^2
		VAR_PPV_CL=ln(0.1^2)^2
		VAR_PPV_V=ln(0.1^2)^2
		VAR_PPV_KA=ln(0.1^2)^2
		VAR_CORR_CL_V=ln(0.01^2)^2
		VAR_RUV_PROP=ln(0.1^2)^2
		VAR_RUV_ADD=ln(0.1^2)^2
		
		MU_PCA0=ln(96.7)
		MU_C50=ln(1.2)
		MU_TEQ=ln(12.9)
		MU_PPV_PCA0=ln(0.1)
		MU_PPV_C50=ln(0.1)
		MU_PPV_TEQ=ln(0.1)
		MU_RUV_FX=ln(4)
		
		VAR_PCA0=ln(96.7^2)^2
		VAR_C50=ln(1.2^2)^2
		VAR_TEQ=ln(12.9^2)^2
		VAR_PPV_PCA0=ln(0.1^2)^2
		VAR_PPV_C50=ln(0.1^2)^2
		VAR_PPV_TEQ=ln(0.1^2)^2
		VAR_RUV_FX=ln(4^2)^2
	}
	
	PRIOR_VARIABLE_DEFINITION{
		BETA_CL_WT=0.75
		BETA_V_WT=1
		POP_EMAX=1
		PPV_TLAG=0.1
		PPV_EMAX=0.1	
		
		POP_CL = 0.1
		POP_V = 8
		POP_KA =0.36 
		POP_TLAG =1
		PPV_CL = 0.1
		PPV_V = 0.14
		PPV_KA = 0.94
		CORR_CL_V =0.29
		RUV_PROP = 0.11
		RUV_ADD =0.001

		POP_PCA0 =96
		POP_C50 =1.2
		POP_TEQ =12
		PPV_PCA0 =0.1
		PPV_C50 =0.1
		PPV_TEQ =0.1
		RUV_FX =4
		
		#POP_CL ~ LogNormal2(meanLog=MU_CL, varLog = VAR_CL)
		#POP_V ~ LogNormal2(meanLog=MU_V, varLog = VAR_V)
		#POP_KA ~ LogNormal2(meanLog=MU_KA, varLog = VAR_KA)
		#POP_TLAG ~ LogNormal2(meanLog=MU_TLAG, varLog = VAR_TLAG)
		#PPV_CL ~ LogNormal2(meanLog=MU_PPV_CL, varLog = VAR_PPV_CL)
		#PPV_V ~ LogNormal2(meanLog=MU_PPV_V, varLog = VAR_PPV_V)
		#PPV_KA ~ LogNormal2(meanLog=MU_PPV_KA, varLog = VAR_PPV_KA)
#		CORR_CL_V ~ LogNormal2(meanLog=MU_CORR_CL_V, varLog = VAR_CORR_CL_V)
#		RUV_PROP ~ LogNormal2(meanLog=MU_RUV_PROP, varLog = VAR_RUV_PROP)
#		RUV_ADD ~ LogNormal2(meanLog=MU_RUV_ADD, varLog = VAR_RUV_ADD)
		
#		POP_PCA0 ~ LogNormal2(meanLog=MU_PCA0, varLog = VAR_PCA0)
#		POP_C50 ~ LogNormal2(meanLog=MU_C50, varLog = VAR_C50)
#		POP_TEQ ~ LogNormal2(meanLog=MU_TEQ, varLog = VAR_TEQ)
#		PPV_PCA0 ~ LogNormal2(meanLog=MU_PPV_PCA0, varLog = VAR_PPV_PCA0)
#		PPV_C50 ~ LogNormal2(meanLog=MU_PPV_C50, varLog = VAR_PPV_C50)
#		PPV_TEQ ~ LogNormal2(meanLog=MU_PPV_TEQ, varLog = VAR_PPV_TEQ)
#		RUV_FX ~ LogNormal2(meanLog=MU_RUV_FX, varLog = VAR_RUV_FX)
	}
}


warfarin_PKPD_turnover_par = parObj{
   STRUCTURAL{
      POP_PCA0 : {value=96.7,lo=0.01,hi=200}
      POP_EMAX : {value=1, lo=0, fix=true}
      POP_C50 : {value=1.2,lo=0.01,hi=10}
      POP_TEQ : {value=12.9,lo=0.01,hi=100}
      RUV_FX : {value=4, lo=0 }
   }# end STRUCTURAL

   VARIABILITY{
      PPV_PCA0 : {value=0.1 ,type is sd}
      PPV_EMAX : {value=0 ,type is sd,fix=true}
      PPV_C50 : {value=0.1 ,type is sd}
      PPV_TEQ : {value=0.1 ,type is sd}
   }# end VARIABILITY

} # end of parameter object

warfarin_PKPD_turnover_mdl = mdlObj{
	IDV { T }

	VARIABILITY_LEVELS{
		ID : { level=2, type is parameter }
		DV : { level=1, type is observation }	
	}

   STRUCTURAL_PARAMETERS{
      POP_PCA0
      POP_EMAX
      POP_C50
      POP_TEQ
      RUV_FX
    }# end STRUCTURAL_PARAMETERS

   VARIABILITY_PARAMETERS{
      PPV_PCA0
      PPV_EMAX
      PPV_C50
      PPV_TEQ
   }# end VARIABILITY_PARAMETERS

   RANDOM_VARIABLE_DEFINITION(level=ID){
      ETA_PCA0 ~ Normal( mean=0, sd=PPV_PCA0)
      ETA_EMAX ~ Normal( mean=0, sd=PPV_EMAX)
      ETA_C50 ~ Normal( mean=0, sd=PPV_C50)
      ETA_TEQ ~ Normal( mean=0, sd=PPV_TEQ)
   }# end RANDOM_VARIABLE_DEFINITION

   INDIVIDUAL_VARIABLES{
	  PCA0 : {type is linear, trans is ln, pop = POP_PCA0, ranEff = ETA_PCA0 }
	  EMAX : {type is linear, pop = POP_EMAX, ranEff = ETA_EMAX }
	  C50 : {type is linear, trans is ln, pop = POP_C50, ranEff = ETA_C50 }
	  TEQ : {type is linear, trans is ln, pop = POP_TEQ, ranEff = ETA_TEQ }   
   }# end INDIVIDUAL_VARIABLES

   MODEL_PREDICTION{
   	  CL
   	  V
   	  KA
   	  TLAG
   	  KPCA=ln(2)/TEQ
      RPCA=PCA0*KPCA
	COMPARTMENT {
			INPUT_KA      : {type is depot,  to=CENTRAL, ka=KA, tlag=TLAG}
			CENTRAL       : {type is compartment}
                     	 :: {type is elimination, from=CENTRAL, v=V, cl=CL}
   		}# end COMPARTMENT

   DEQ{
      CC=CENTRAL/V

      DPCA=PCA
      PD=1-EMAX*CC/(C50+CC)			
      PCA : {deriv=RPCA*PD - KPCA*DPCA, init = PCA0}
   	  }# end ODE
   }# end MODEL_PREDICTION

	RANDOM_VARIABLE_DEFINITION(level=DV){
      EPS_PCA ~ Normal( mean=0, var=1)
	}

   OBSERVATION{
   	 PCA_obs : {type is additiveError, additive = RUV_FX, eps = EPS_PCA, prediction = PCA }
   }# end OBSERVATION
} # end of model object

warfarin_PKPD_turnover_task = taskObj {
ESTIMATE{ 
		set algo is mcmc
        TARGET_SETTINGS(target="BUGS"){
            set 
            nchains = 1,
            burnin = 1,
            niter = 1,
        #    parameters = "LAMBDA0_POP,LAMBDA1_POP,K1_POP,K2_POP,W0_POP,LAMBDA0,K2,CV",
         #    thinupdater=1,
          #  thinsamples=5, # uno dei due
#           odesolver="LSODA", #oppure LSODA
        #    dic="true",
        	winbugsgui="true" # oppure no
        }
}
} # end of task object

warfarin_PK_ODE_mog = mogObj {
    OBJECTS{
		warfarin_PKPD_turnover_dat : { type is dataObj }
		warfarin_PKPD_turnover_mdl : { type is mdlObj }
		warfarin_PKPD_turnover_prior : { type is priorObj }
		warfarin_PKPD_turnover_task : { type is taskObj }
	}
}
