% Data structure

Folder = '/home/sophie/Documents/DataSetToPutOnline'; % modify depending on where you downloaded data to
addpath(genpath([Folder filesep 'Code']))
cd(Folder)
% Main data set is saved in Data_XX.mat files containing: 
% data : neurons/ROIs x time x sounds x trials
% t : time steps

% For 2P data, we also provide a dataset based on clustering neural
% responses to aid with visualisation of responses

% To navigate the sounds use the code SoundNomenclature;
SoundNom = SoundNomenclature(Folder);

%
% Look at some responses

Structure = 'IC';
Neuron = 2000;
load(['Data_',Structure,'.mat'])

% Trial-averaged response of a given neuron to all sounds
subplot(2,2,[1,3])
imagesc(t,1:140,squeeze(nanmean(Data(Neuron,:,:,:),4))') % average over trials
LabelMyPlotSoundsY
xlabel('Time (s)')

% Response of this neuron to preffered sound, show all trials
subplot(222)
[val,ind_BF] = max(mean(squeeze(nanmean(Data(Neuron,:,:,:),4))));
plot(t,squeeze(Data(Neuron,:,ind_BF,:)))
hold on
plot(t,nanmean(squeeze(Data(Neuron,:,ind_BF,:))'),'k','linewidth',3)
xlabel('Time (s)'), ylabel('Resp. amp.')

% Look at the corresponding sound
subplot(224)
imagesc(t_spec,f,squeeze(SoundNom.Spectrograms(ind_BF,:,:))), axis xy
title(SoundNom.StimName{ind_BF})
xlabel('time (s)'), ylabel('Frequency')

%
% Data with spatial location - IC ephys

% IC  - electrophysiology
% Corrdondates are given as AP, ML and DV coordinates based on Allen brain
% atlas (https://mouse.brain-map.org/static/atlas) 
load('Data_ICE.mat')
load('AnatInfo_ICE.mat')

figure
% Get the neurons preferred frequency (ind_BF) and response level 
BF = ((squeeze(nanmean(nanmean(Data(:,find_BF(t>0,1,'first'):find_BF(t>0.75,1,'first'),SoundNom.DetailGroups{3},:),4),2))')');
[val,ind_BF] = max(BF');
BF(BF<0) = 0; % get rid of inhibitoary responses

% X,Y coordinates for ML / DV map
X = [200:100:1800];
Y = [-1600:100:0];
clear H


for freq =1:11
    
    NeurFOI = AnatInfo_IC.Struct<4 & ind_BF==freq; % Just the neurons inside IC that have the right best frequency 
    
    H(freq,:,:) = hist2w([AnatInfo_IC.ML(NeurFOI);-AnatInfo_IC.DV(NeurFOI)]',(BF(NeurFOI,freq)),X,Y);
    
end

figure

% plot difference between low and high frequency maps
subplot(121)
Low = smooth2a(squeeze(nanmean(H(1:4,:,:))),1,1); Low  = Low./norm(Low);
Hi = smooth2a(squeeze(nanmean(H(8:11,:,:))),1,1); Hi  = Hi./norm(Hi);
[~,Cc] =  contour(X,Y,Hi-Low,15,'linewidth',2);
colormap jet
caxis([-0.15 0.15])
xlabel('ML'), ylabel('DV'), box off
title('Hi - Low')

% plot the position of each unit colored accoridng to the brain area it is
% attributed to
subplot(122)
for zn = 1:3
    NeurFOI = AnatInfo_IC.Struct==zn;
    plot(AnatInfo_IC.ML(NeurFOI),-AnatInfo_IC.DV(NeurFOI),'.','MarkerSize',10)
    hold on
    
end
xlim([X(1) X(end)])
ylim([Y(1) Y(end)])
box off
legend(RegionNames_IC)
title('SU location')

% Data with spatial location - AC 2P

% Coordinates are given in the x,y plane of the brain surface after
% realigning all mice fo the tonotopic map using intrinsic imaging
% brain areas are defined based on nomenclature from Romero et al., 2020

load('Data_AC.mat')
load('AnatInfo_AC.mat')

% Get the neurons preferred frequency (ind_BF) and response level 
BF = ((squeeze(nanmean(nanmean(Data(:,find_BF(t>0,1,'first'):find_BF(t>0.75,1,'first'),SoundNom.DetailGroups{3},:),4),2))')');
[val,ind_BF] = max(BF');
BF(BF<0) = 0; % get rid of inhibitoary responses

% Coordinates in the XY plane of the realigned brain surface
X = [500:100:1800];
Y = [600:100:1800];
clear H

for freq =1:11
    
    NeurFOI = AnatInfo_AC.area>0 & ind_BF==freq; % Just the neurons inside AC that have that best frequency
    
    H(freq,:,:) = hist2w([AnatInfo_AC.x(NeurFOI);AnatInfo_AC.y(NeurFOI)]',(BF(NeurFOI,freq)),X,Y);
    
end


figure

% plot difference between low and high frequency maps
subplot(121)
Low = smooth2a(squeeze(nanmean(H(1:4,:,:))),1,1)'; Low  = Low./norm(Low);
Hi = smooth2a(squeeze(nanmean(H(8:11,:,:))),1,1)'; Hi  = Hi./norm(Hi);
[~,Cc] =  contour(X,Y,Hi-Low,15,'linewidth',2);
colormap jet
caxis([-0.05 0.05])
xlabel('ML'), ylabel('DV'), box off
title('Hi - Low')
xlim([X(1) X(end)])
ylim([Y(1) Y(end)])

% plot the position of each unit colored accoridng to the brain area it is
% attributed to
subplot(122)
for zn = 1:6
    NeurFOI = AnatInfo_AC.area==zn;
    plot(AnatInfo_AC.x(NeurFOI),AnatInfo_AC.y(NeurFOI),'.','MarkerSize',10)
    hold on
    
end
xlim([X(1) X(end)])
ylim([Y(1) Y(end)])
box off
legend(RegionNames_AC)
title('SU location')