clear
close all
f=figure(5);%plotting information
tiledlayout(2,2);
PWD=pwd;
cd ..
cd('Output CSVs')
DataSets={'Fig5and6_Awake','Fig5and6_Ana'}; %Add on data set naming
TitleName={'NPY','Michaelis-Menten kinetics of v_1','NPY_v_s_m'};
ylabelName={'\DeltaNPY','\Deltav_1','\DeltaNPY_v_s_m'};
xlabelName={'Seconds','\DeltaNPY','Seconds'};
PlotStyle={'r-','k-','ro','ko'};
YLimits=[0 0.2;0 1.2;0 0.01];
FontSize=9;
Vmax=10^(-0.6541);%Param for Michaelis Menten kinetics
K_m=10^(-4.4006);%Param for Michaelis Menten kinetics
for i=1:length(DataSets)
    T=readtable(strcat('sten2020_NVC_main_data_',DataSets{i},'.csv'),'VariableNamingRule','preserve');
    %1=time, 58=NPY, 60=NPY_vsm
    %[time, NPY, NPY_vsm,]
    PlotData=table2array(T(:,[1,58,60]));
    
    nexttile(1)%Plot the top left information for both data sets
    plot(PlotData(:,1),(PlotData(:,2)-PlotData(1,2)),PlotStyle{i})
    hold on
    [~,b]=max((PlotData(:,2)-PlotData(1,2)));
    plot(PlotData(b,1),(PlotData(b,2)-PlotData(1,2)),(PlotStyle{2+i}))
    title(TitleName{1},'FontSize',FontSize,'FontWeight','Bold')
    ylabel(ylabelName{1},'FontSize',FontSize)
    xlabel(xlabelName{1},'FontSize',FontSize)
    ylim([YLimits(1,1) YLimits(1,2)])
    xlim([-0.5 60])
    yticks(0:0.05:0.2)
    hold on
    
    %Compute Deltas for top right plot
    maxNPY=max(PlotData(:,2));
    NPYSS=PlotData(1,2);
    v1SS=(Vmax*PlotData(1,2))/(K_m+PlotData(1,2));
    vNPYmax=((Vmax*maxNPY)/(K_m+maxNPY));
    nexttile(2)%Plot the top right information for both data sets
    plot(maxNPY-NPYSS,(vNPYmax-v1SS)/(Vmax-v1SS),PlotStyle{2+i})
    title(TitleName{2},'FontSize',FontSize,'FontWeight','Bold')
    ylabel(ylabelName{2},'FontSize',FontSize)
    xlabel(xlabelName{2},'FontSize',FontSize)
    ylim([YLimits(2,1) YLimits(2,2)])
    xlim([-0.001 0.2])
    hold on

    
    nexttile(3,[1,2])%Plot the bottom information for both data sets
    plot(PlotData(:,1),(PlotData(:,3)-PlotData(1,3)),PlotStyle{i})
    hold on
    [~,b]=max((PlotData(:,3)-PlotData(1,3)));
    plot(PlotData(b,1),(PlotData(b,3)-PlotData(1,3)),(PlotStyle{2+i}))
    title(TitleName{3},'FontSize',FontSize,'FontWeight','Bold')
    ylabel(ylabelName{3},'FontSize',FontSize)
    xlabel(xlabelName{3},'FontSize',FontSize)
    ylim([YLimits(3,1) YLimits(3,2)])
    xlim([0 60])
    hold on
end

%Compute response function
NPY=PlotData(1,2):0.0001:(PlotData(1,2)+0.2);
v1=((Vmax*NPY)./(K_m+NPY));
nexttile(2)%Plot response function and asymptote in top right plot
plot(NPY-NPYSS,(v1-v1SS)/(Vmax-v1SS),'k-')
text(0.06,1.1,'V_m_a_x-v_1_,_S_t_e_a_d_y_S_t_a_t_e')
s=legend('GABAergic OG - awake','GABAergic OG - anesthesia','Response Dynamics');
set(s,'Location','South','FontSize',FontSize-2,'AutoUpdate','off')
plot([0 0.2],[1 1],'k--')
xticks(0:0.05:0.2)

cd(PWD)
set(gcf,'Position',[100 100 700 450])
exportgraphics(f,'Figure5.png','Resolution',600)