%% Create Variants from Grouped Data
% Import sample data. The data contain three groups (individuals) with some time course measurement data. The Cl_Central and Central columns represent group-specific variant values.

tbl = readtable('sample_data_variants_simbiology.xlsx')

%
% Convert to a groupedData object.

gdata = groupedData(tbl);

%
% By default, the function uses the "Group" column as the group variable.

gdata.Properties.GroupVariableName

%
% Create a variant for each group for using the Cl_Central and Central variables from the data.

variants = createVariants(gdata,["Cl_Central","Central"])
variants(1)
variants(2)
variants(3)

% Note that individual 3 has a variant value for Cl_Central but not Central. Hence the function created only one variant content, for Cl_Central.
%
% You can also specify which group to create variants for. For example, create variants for individuals 1 and 2 only.

variants = createVariants(gdata,["Cl_Central","Central"],["1","2"])
variants(1)
variants(2)

%
% By default, the function assigns the type as parameter for each variable. You can specify which type (species, parameter, or compartment) by using the Types name-value argument. Specify Cl_Central as a parameter and Central as a compartment.

variants = createVariants(gdata,["Cl_Central","Central"],Types=["parameter","compartment"]);
variants(1)
variants(2)
variants(3)

%
% If you know the name of model component that maps to each variable, you can use the component name to define the Name of each variant content. For example, map the data variables to the model components named "Clearance" and "Central".

variants = createVariants(gdata,["Cl_Central","Central"],Types=["parameter","compartment"],Names=["Clearance","Central"]);
variants(1)
variants(2)
variants(3)

%
% If your data variables have the same names as the corresponding model components, you can use the model as the source and the function automatically does the mapping.

% Create a two-compartment model.
pkmd                                    = PKModelDesign;
pkc1                                    = addCompartment(pkmd,'Central');
pkc1.DosingType                         = 'Bolus';
pkc1.EliminationType                    = 'linear-clearance';
pkc1.HasResponseVariable                = true;
pkc2                                    = addCompartment(pkmd,'Peripheral');
model                                   = construct(pkmd);
variants = createVariants(gdata,["Cl_Central","Central"],Model=model);
variants(1)

%
% Alternatively, if you have the corresponding model component objects, you can use them as well to map to the data variables.

% Extract the parameter and compartment object.
clearance = sbioselect(model,Name="Cl_Central");
centralVol = sbioselect(model,Name="Central");
% Map the data variables to these objects.
variants = createVariants(gdata,["Cl_Central","Central"],Components=[clearance,centralVol])
variants(1)

%
% Copyright 2021 The MathWorks, Inc.