% Load Files Needed for Analysis

%%All of this data is in Additional File 1.
MAPS=readtable("Z:\Amy\Gottschalk et al 2020\Computational Data and Scripts\MAPS_List.csv");  
MAPS=MAPS(:,3:6);
Proteins=readtable("Z:\Amy\Gottschalk et al 2020\Computational Data and Scripts\Total Proteins.csv");
Neurons=readtable("Z:\Amy\Gottschalk et al 2020\Computational Data and Scripts\Neuron_Counts.csv");

%
% Generate data table for Figures

Species=categorical(MAPS.Species_Uniprot_ID);           %% convert species to a categorical variable
cats_Species=categories(Species);                       %%extract species categories
counts_Species=countcats(Species);                      %%count number of microtubule associated proteins per species
N=size(counts_Species,1);                               %% define N as the total numbrer of species
Taxa_Species=strings(N,1);                              %% initialize variable to label each species with a taon

for i=1:size(counts_Species,1)                                      %%this loop identifies a taxon for each species
     idx=strcmp(MAPS.Species_Uniprot_ID,cats_Species(i));
     first=find(idx,1,'first');
     Taxa_Species(i)=Taxa(first);
end

Taxa_Species=categorical(Taxa_Species);                             %%now we convert Taxa_Species into a categorical variable

Proteins_Species=NaN(N,1);                                                  %%initialize a variable for total number of proteins per species
for i=1:N
    idx=find(strcmp(Proteins.UniprotID,cats_Species(i)),1,"first");         %%find the matching protein count for each Uniprot ID
    Proteins_Species(i)=Proteins.ProteinCount(idx);                         %% add it to the list
end

N2=size(Neurons,1);                                                         %%How many neuron counts do we have
Neurons_Species=NaN(N,1);                                                   %%initial variable for neuron counts
for i=1:N2
    idx=strcmp(cats_Species,Neurons.UniprotID(i));                          %%this matches each neuron count to a species
    Neurons_Species(idx)=Neurons.Neurons(i);
      if Neurons.Region(i)=="Whole Nervous System"                          %%only if it's a whole nervous system count
           Neurons_Species(idx)=Neurons.Neurons(i);
      end
end

%%%At this point you have the following variables
    %%List of all species in cats_Species
    %%List of MAP counts in counts_Species
    %%Taxon in Taxa_Species
    %%Total protein # in Protein_Species
    %%Neuron number in Neurons_Species
    %%Type in Type_Species (Sensory associated =1, whole brain =2)

%



%
% Total Proteins by Taxon

%%initialize the subplot in panel
subplot(2,2,1);                  

%%ANOVA analysis
anova=anova1(Proteins_Species,Taxa_Species,"off");
anova=round(anova,2,"significant");
exp=(floor(log10(anova)));
rem=round(anova/10^exp,2,"significant");
txt=sprintf('ANOVA\np=%gx10^{%d}',rem,exp);   %% converts p-value into proper a x 10^b notation rather than aeb

%%make the actual plot
boxplot(Proteins_Species,Taxa_Species,"Color",[0 0 0],"Symbol",'.')
ylabel("Total Proteins")
text(6,7e4,txt,"HorizontalAlignment","center");
ylim([0 8e4])
ttl=title('A');
ttl.Units = 'Normalize'; 
ttl.Position(1) = 0; % use negative values (ie, -0.1) to move further left
ttl.HorizontalAlignment = 'left';  
ax.TitleHorizontalAlignment = 'left';

%
% Microtubule Associated Proteins by Taxon

%%initialize the subplot in panel
subplot(2,2,2);

%%ANOVA analysis
anova=anova1(counts_Species,Taxa_Species,"off");
anova=round(anova,2,"significant")
exp=(floor(log10(anova)));
rem=round(anova/10^exp,2,"significant");
txt=sprintf('ANOVA\np=%gx10^{%d}',rem,exp);

%%make the actual plot
boxplot(counts_Species,Taxa_Species,"Symbol",'.',"Color",[0 0 0])
ylabel("Number of MAPs")
text(6,8,txt,"HorizontalAlignment","center");
ylim([0 10]);
ttl=title('B');
ttl.Units = 'Normalize'; 
ttl.Position(1) = 0; % use negative values (ie, -0.1) to move further left
ttl.HorizontalAlignment = 'left';  
ax.TitleHorizontalAlignment = 'left';

%
%
%
% Number of Neurons versus Number of MAPs

%initialize subplot 
subplot(2,2,3);

%this removes species that don't have neuron counts
idx=~isnan(Neurons_Species);
xx=Neurons_Species(idx);
yy=counts_Species(idx);

%%this makes the actual plot
scatter(xx,yy,"filled",'o','MarkerFaceColor',[0 0 0]);
xlabel("Number of Neurons");
ylabel("Number of MAPs");

%calculate a Pearson correlation coefficient and convert to a label
R=corrcoef(xx,yy);
R2=round(R(1,2)^2,2,"significant"); 
txt="R^2="+R2;
text(1e10,5,txt);
set(gca,'xscale','log');
ttl=title('C');
ttl.Units = 'Normalize'; 
ttl.Position(1) = 0; 
ttl.HorizontalAlignment = 'left';  
ax.TitleHorizontalAlignment = 'left';

%
% Number of Proteins vs. Number of MAPs

%initialize subplot 
subplot(2,2,4)

%makes the plot
scatter(Proteins_Species,counts_Species,"filled",'o','MarkerFaceColor',[0 0 0])
xlabel("Total Number of Proteins")
ylabel("Number of MAPs")

%correlation coefficient 
R=corrcoef(Proteins_Species,counts_Species);
R2=round(R(1,2)^2,2,"significant");
txt="R^2="+R2;
text(2e3,7,txt);
set(gca,'xscale','log');
xlim([1e3 1e5])
ttl=title('D');
ttl.Units = 'Normalize'; 
ttl.Position(1) = 0; % use negative values (ie, -0.1) to move further left
ttl.HorizontalAlignment = 'left';  
ax.TitleHorizontalAlignment = 'left';

%
% Number of Binding Regions

%this shuts off the panels
close all

%calculates anova and converts to a label
anova=anova1(MAPS.Binding_Regions,MAPS.Taxon,"off");
anova=round(anova,2,"significant");
exp=(floor(log10(anova)));
rem=round(anova/10^exp,2,"significant");
txt=sprintf('ANOVA\np=%gx10^{%d}',rem,exp);
MAPS.Taxon=categorical(MAPS.Taxon);

%makes the actual plot
boxplot(MAPS.Binding_Regions,MAPS.Taxon,"Color",[0 0 0],"Symbol",'.')
text(2,8,txt,"HorizontalAlignment","center");
ylabel("Number of MT Binding Regions")

%
% Generate summary table

%initialize variables
Taxons=categories(Taxa_Species);
Median_MAPs=NaN(7,1);
SD_MAPs=NaN(7,1);
Median_Prots=NaN(7,1);
SD_Prots=NaN(7,1);
N=size(Taxons,1);

%does the median and standard deviation calculations
for i=1:N
    idx=Taxa_Species==Taxons(i);
    Median_MAPs(i)=median(counts_Species(idx));
    SD_MAPs(i)=std(counts_Species(idx));
    Median_Prots(i)=median(Proteins_Species(idx));
    SD_Prots(i)=std(Proteins_Species(idx));
end

Median_MTBR=NaN(7,1);
SD_MTBR=NaN(7,1);

for i=1:N
    idx=strcmp(MAPS.Taxon,Taxons(i));
    Median_MTBR(i)=median(MAPS.Binding_Regions(idx));
    SD_MTBR(i)=std(MAPS.Binding_Regions(idx));
end

%makes the actual table
summary=table(Taxons,Median_MAPs,SD_MAPs,Median_Prots,SD_Prots,Median_MTBR,SD_MTBR)