clear all

% Plot Cy5-TCEP quenching data and fit with model.  
%
% Executes properly on MATLAB version 9.4.0.813654 (R2018a).
% Failed to execute in OCTAVE version 4.2.1.
% Joshua C. Vaughan 5/17/2020

pth = 'F:\Josh\CHEM452\build_files\MATLAB_scripts\data_files';
nm = ['2011_06_13_Cy5_TCEP.csv'];

D = dlmread([pth '\' nm]);

l = D(:,1);
ab = D(:,3:end);

% Set all spectra to zero at mean of 800 & 900 nm to correct for
% spectrometer drift.
ab2 = ab - repmat(mean(ab(701:800,:),1),801,1);

% groom data before fitting
OD645 = ab2(446,:); % for Cy5

TCEPconc_orig = 62.5*[0 1/512 1/256 1/128 1/64 1/32 1/16 1/8 1/4 1/2 1];
Cy5conc_orig = OD645/0.25;

keepers = [1:11];
TCEPconc = TCEPconc_orig(keepers);
Cy5conc = Cy5conc_orig(keepers);
OD645 = OD645(keepers);

% fit to curve
s = fitoptions('Method','NonlinearLeastSquares','Lower',[0 0],'Upper',[20 20],'Startpoint',[.5 .5]);
f = fittype('Ao/(K*x+1)','options',s);
O = fit(TCEPconc',Cy5conc',f);
Ao = O.Ao;

% evaluate fit
Keq = O.K;
T = linspace(0,max(TCEPconc),128);
A = Ao./(Keq*T + 1);

% create plot
ax_lw = 2;
pl_lw = 2;
ax_fs = 28;
la_fs = 28;
lg_fs = 28;
fontname = 'Arial';

figure(1); clf
hold on
plot(TCEPconc,Cy5conc,'k.','markersize',25)
plot(T,A,'r','linewidth',2)
plot(TCEPconc,Cy5conc,'k.','markersize',25)
hold off
xlim([-1 max(TCEPconc)+1])
ylim([0 max(Cy5conc)*1.05])
set(gca,'fontsize',ax_fs)
set(gca,'linewidth',ax_lw)
xlabel('TCEP concentration (mM)','fontsize',la_fs)
ylabel(['Cy5 concentration (' char(181) 'M)'],'fontsize',la_fs)
set(gca,'tickdir','out')
set(gca,'box','on')
set(gcf,'color',[1 1 1])
set(gca,'fontname',fontname)
set(gca,'LooseInset',get(gca,'TightInset'));

h = legend('data','fit');
set(h,'box','off')
set(h,'fontsize',lg_fs)

