%% 2022-03-27
% Generate volcano plot from .xlsx file
% Input = "Data Fig 6B.xlsx"

[tableFile,tableDir] = uigetfile('*.xlsx','Select "Data Fig 6B.xlsx"');

vtable = readtable(strcat(tableDir,tableFile));

%%
markerSize = 30;

% Plot all 2020 metabolomics data
figure;
hold on;
s1 = scatter(vtable.Log2,vtable.x_LOG_pval_,markerSize,'MarkerEdgeColor','none','MarkerFaceColor',[0.9 0.9 0.9]);

namedCmpds = find(~cellfun(@isempty,vtable.Consensus_ID));

% Plot compounds that were observed in both 2019 and 2020
scatter(vtable.Log2(vtable.Overlap_19_20==1),vtable.x_LOG_pval_(vtable.Overlap_19_20==1),markerSize,'MarkerEdgeColor','none','MarkerFaceColor',[0.2 0.2 0.2]);

% Plot compounds unique to infected flies
scatter(vtable.Log2(vtable.Infected_specific_19_20==1),vtable.x_LOG_pval_(vtable.Infected_specific_19_20==1),markerSize,'MarkerEdgeColor','none','MarkerFaceColor',[1 0.5 0]);

% Plot compounds unique to summiting flies
scatter(vtable.Log2(vtable.Summit_specific_19_20==1),vtable.x_LOG_pval_(vtable.Summit_specific_19_20==1),markerSize,'MarkerEdgeColor','none','MarkerFaceColor',[1 0 0]);

% Outline compounds that have tentative ID
scatter(vtable.Log2(namedCmpds),vtable.x_LOG_pval_(namedCmpds),markerSize/2,'p','MarkerEdgeColor','none','MarkerFaceColor',[1 1 1],'LineWidth',0.5);

% Mark compounds with x that were observed in previous experiment but
% trended in opposite directions
%scatter(vtable.Log2(vtable.Direction_match_19_20==0),vtable.x_LOG_pval_(vtable.Direction_match_19_20==0),markerSize,'wx');

% P-value cutoff to determine significance
pCutOff = -1*log10(0.05);

% Pull out compounds that have significant pvalues, per cutoff
vsigLog = vtable.Log2(vtable.x_LOG_pval_>pCutOff);
vsigPval = vtable.x_LOG_pval_(vtable.x_LOG_pval_>pCutOff);
vsigName = vtable.Consensus_ID(vtable.x_LOG_pval_>pCutOff);

%annotation('arrow',vsigLog(vsigLog<-1),vsigPval(vsigLog<-1),vsigName(vsigLog<-1))

plot(ones(1,length(0:0.1:16)),0:0.1:16,'k--');
plot(repmat(-1,1,length(0:0.1:16)),0:0.1:16,'k--');
plot(-8:0.1:8,repmat(pCutOff,1,length(-8:0.1:8)),'k--');

xlim([-8 8]);
%ylim([0 15]);
ylim([0 6]);

%alpha(0.4);

%set(gca,'ytick',0:3:15);
set(gca,'ytick',0:1:6);

xlabel('log2(fold change)','FontSize', 16);
ylabel('-log10(p-value)', 'FontSize', 16);
%title(['Hemolymph metabolite abundance',newline,'Summiting flies vs. exposed, non-summiting flies'],'FontSize',18);
set(gca,'fontsize',20,'TickLength',[0 0]);

set(gcf,'PaperUnits','inches','PaperPosition',[0 0 8 6]);
print(strcat(tableDir,'2022-03-27-Volcano_highlight_unique_and_repeat'),'-dpdf');

shift = 0.15;
t0 = text(vtable.Log2(namedCmpds),vtable.x_LOG_pval_(namedCmpds),vtable.Consensus_ID(namedCmpds),'FontSize',8);
%t1 = text(vsigLog(vsigLog<-1),vsigPval(vsigLog<-1)+shift,vsigName(vsigLog<-1),'FontSize',8);
%t2 = text(vsigLog(vsigLog>1),vsigPval(vsigLog>1)+shift,vsigName(vsigLog>1),'FontSize',8);
print(strcat(tableDir,'2022-03-27-Volcano_highlight_unique_and_repeat_IDs'),'-dpdf');
close();