% Load Files Needed for Analysis

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 Proteins per Taxon Table

Taxa=MAPS.Taxon;
Taxa=categorical(MAPS.Taxon);
cats_Taxa=categories(Taxa);
counts_Taxa=countcats(Taxa);

%
% Generate data table for Figures

Species=categorical(MAPS.Species_Uniprot_ID);
cats_Species=categories(Species);
counts_Species=countcats(Species);
N=size(counts_Species,1);
Taxa_Species=strings(N,1);
for i=1:size(counts_Species,1)
     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);

Proteins_Species=NaN(N,1);
for i=1:N
    idx=find(strcmp(Proteins.UniprotID,cats_Species(i)),1,"first");
    Proteins_Species(i)=Proteins.ProteinCount(idx);
end

N2=size(Neurons,1);
Neurons_Species=NaN(N,1);
Type_Species=strings(N,1);
for i=1:N2
    idx=strcmp(cats_Species,Neurons.UniprotID(i));
    Neurons_Species(idx)=Neurons.Neurons(i);
      if Neurons.Region(i)=="Whole Nervous System"
           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

subplot(2,2,1);
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);
% txt=strcat("ANOVA" + newline + "p=", rem, "x10^{", exp(1),"}");
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

subplot(2,2,2);
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);
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

subplot(2,2,3);
idx=~isnan(Neurons_Species);
xx=Neurons_Species(idx);
yy=counts_Species(idx);
cc=categorical(Type_Species(idx));
scatter(xx,yy,"filled",'o','MarkerFaceColor',[0 0 0]);
xlabel("Number of Neurons");
ylabel("Number of MAPs");
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; % use negative values (ie, -0.1) to move further left
ttl.HorizontalAlignment = 'left';  
ax.TitleHorizontalAlignment = 'left';

%
% Number of Proteins vs. Number of MAPs

subplot(2,2,4)
scatter(Proteins_Species,counts_Species,"filled",'o','MarkerFaceColor',[0 0 0])
xlabel("Total Number of Proteins")
ylabel("Number of MAPs")
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

close all
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);
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

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);

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
summary=table(Taxons,Median_MAPs,SD_MAPs,Median_Prots,SD_Prots,Median_MTBR,SD_MTBR)

%

FASTAdata=fastaread('Z:\Amy\Gottschalk et al 2020\Computational Data and Scripts\MAPS.fasta.fasta');
test=FASTAdata;

N=size(test,1);
labels=strings(N,1);
for i=1:N
    lab=test(i,:).Header;
    lab2=strsplit(lab,"OS=");
    lab3=lab2(2);
    lab4=strsplit(string(lab3)," OX=");
    lab4=lab4(1);
    labels(i)=lab4;
end

Proteome_Species=["Monodelphis domestica" "Drosophila melanogaster" "Gallus gallus" "Homo Sapiens" "Mus musculus" "Oryctolagus cuniculus" "Oryzias melastigma" "Rattus norvegicus" "Sus scrofa" "Bos taurus" "Caenorhabditis elegans" "Canis lupus familiaris" "Cricetulus griseus" "Danio rerio"]
idx=ismember(labels,Proteome_Species)
test=test(idx,:)
N=size(test,1);
labels=labels(idx,:)
distances=seqpdist(test,'Method','Jukes-Cantor','Alpha','protein');
UPGMAtree = seqlinkage(distances,'UPGMA',"leafNodelabels");

%

h=plot(UPGMAtree,'TerminalLabels','false');

labels2=strings(N,1)
aa=h.leafNodeLabels
aa
for i=1:N
    bb=order(i);
    cc=bb.String;
    idx=strsplit(cc);
    idx=idx{2};
    idx=str2double(idx);
    labels2(i)=labels(idx);
end

yticklabels(labels2);
scale = 0.9;
pos = get(gca, 'Position');
%pos(2) = pos(2)+scale*pos(4);
pos(3) = 0.5*pos(3)
set(gca, 'Position', pos)

%

Model_Species=["Taeniopygia guttata" "Drosophila melanogaster" "Gallus gallus" "Homo Sapiens" "Mus musculus" "Oryctolagus cuniculus" "Oryzias melastigma" "Rattus norvegicus" "Sus scrofa" "Bos taurus" "Caenorhabditis elegans" "Canis lupus familiaris" "Cricetulus griseus" "Danio rerio"]