clear;
load miniDB;

subset.amplitude = amplitude(decay<=6);
subset.area = area(decay<=6);
subset.bath = bath(decay<=6);
subset.culture = culture(decay<=6);
subset.decay = decay(decay<=6);
subset.div = div(decay<=6);
subset.fid = fid(decay<=6);
subset.rise = rise(decay<=6);
subset.time = time(decay<=6);
subset.treatment = treatment(decay<=6);

ind_CNQX_drug = [1:65, 470:2753, 5903:6155, 6200:6407, 6761:6941, 7345:7746];
ind_CNQX_ctrl = [66:469, 2754:3018, 5644:5902, 6408:6632, 6633:6760,6942:7344];
ind_TTX_drug = [3282:3635,3888:4266,7747:7976,5010:5248,5456:5643];
ind_TTX_ctrl = [3019:3281,3636:3887,4267:4477,4478:5009,5249:5455];

CNQX.drug.amplitude = subset.amplitude(ind_CNQX_drug);
CNQX.drug.area = subset.area(ind_CNQX_drug);
CNQX.drug.fid = subset.fid(ind_CNQX_drug);

CNQX.ctrl.amplitude = subset.amplitude(ind_CNQX_ctrl);
CNQX.ctrl.area = subset.area(ind_CNQX_ctrl);
CNQX.ctrl.fid = subset.fid(ind_CNQX_ctrl);

TTX.drug.amplitude = subset.amplitude(ind_TTX_drug);
TTX.drug.area = subset.area(ind_TTX_drug);
TTX.drug.fid = subset.fid(ind_TTX_drug);

TTX.ctrl.amplitude = subset.amplitude(ind_TTX_ctrl);
TTX.ctrl.area = subset.area(ind_TTX_ctrl);
TTX.ctrl.fid = subset.fid(ind_TTX_ctrl);


%% CNQX minis
clear fidlist
fid_tmp = unique(CNQX.drug.fid);
for j = 1:length(fid_tmp)
    ind = strfind(fid_tmp{j},'_');
    if numel(ind)>=2
        fidlist{j} = fid_tmp{j}(1:ind(2)-1);
    else
        fidlist{j} = fid_tmp{j};
    end
end
fidlist = unique(fidlist);
for i = 1:length(fidlist)
    n = 30;
    x_tmp = CNQX.drug.amplitude;
    x_tmp = x_tmp(x_tmp>=5 & x_tmp<200);
    if length(x_tmp) >= n
        ind = randsample(length(x_tmp),n);
        CNQX.drug.ampSample{i,1} = x_tmp(ind);
    else
        continue
    end
end



%% control minis (paired with CNQX)
clear fidlist
fid_tmp = unique(CNQX.ctrl.fid);
for j = 1:length(fid_tmp)
    ind = strfind(fid_tmp{j},'_');
    if numel(ind)>=2
        fidlist{j} = fid_tmp{j}(1:ind(2)-1);
    else
        fidlist{j} = fid_tmp{j};
    end
end
fidlist = unique(fidlist);
for i = 1:length(fidlist)
    n = 30;
    x_tmp = CNQX.ctrl.amplitude;
    x_tmp = x_tmp(x_tmp>=5 & x_tmp<200);
    if length(x_tmp) >= n
        ind = randsample(length(x_tmp),n);
        CNQX.ctrl.ampSample{i,1} = x_tmp(ind);
    else
        continue
    end
end



%% TTX minis
clear fidlist
fid_tmp = unique(TTX.drug.fid);
for j = 1:length(fid_tmp)
    ind = strfind(fid_tmp{j},'_');
    if numel(ind)>=2
        fidlist{j} = fid_tmp{j}(1:ind(2)-1);
    else
        fidlist{j} = fid_tmp{j};
    end
end
fidlist = unique(fidlist);
for i = 1:length(fidlist)
    n = 30;
    x_tmp = TTX.drug.amplitude;
    x_tmp = x_tmp(x_tmp>=5 & x_tmp<200);
    if length(x_tmp) >= n
        ind = randsample(length(x_tmp),n);
        TTX.drug.ampSample{i,1} = x_tmp(ind);
    else
        continue
    end
end



%% control minis (paired with TTX)
clear fidlist
fid_tmp = unique(TTX.ctrl.fid);
for j = 1:length(fid_tmp)
    ind = strfind(fid_tmp{j},'_');
    if numel(ind)>=2
        fidlist{j} = fid_tmp{j}(1:ind(2)-1);
    else
        fidlist{j} = fid_tmp{j};
    end
end
fidlist = unique(fidlist);
for i = 1:length(fidlist)
    n = 30;
    x_tmp = TTX.ctrl.amplitude;
    x_tmp = x_tmp(x_tmp>=5 & x_tmp<200);
    if length(x_tmp) >= n
        ind = randsample(length(x_tmp),n);
        TTX.ctrl.ampSample{i,1} = x_tmp(ind);
    else
        continue
    end
end


