%% 2022-03-27 - combining information from overlap xlsx and data for volcano plot to make fig 6 panels for summit paper
% Generate supplemental fig plots for either 2019 (manual) or 2020
% (classifier) data sets

expChoice = '20'; % '19' for 2019 experiment, '20' for 2020 experiment

% Read in table summarizing overlapping data between 2019 and 2020
% experiments (derived from output of previous module + some manual
% editing)
[f,d] = uigetfile('Select overlap .xlsx');
overlapFile = strcat(d,f);
otable = readtable(overlapFile);

% Find mw of compounds from chosen experiment that are included in overlap
% set, and corresponding indices
[overlapSet,overlapIdx] = unique(otable{:,['MW_',expChoice]});

% For these indices, extract whether they are specific to infected flies,
% summiting flies, ID (if available) and if they trend in same direction
% between experiments
infSpec = otable{:,'Infected_specific_both'};
sumSpec = otable{:,'Summiting_specific_both'};
cmpdName = otable{:,'Consensus_name'};
dirTrend = otable{:,'SameDirection_'};

if strcmp(expChoice,'20')
    % Read in table with data from 2020 metabolomics experiment for volcano
    % plot
    [f2,d2] = uigetfile('Select classifier data .xlsx');
    tableFile = strcat(d2,f2);
    vtable = readtable(tableFile);

    % Write new column headers for variables we're going to add
    xlswrite(tableFile,{'Overlap_19_20'},1,'N1');
    xlswrite(tableFile,{'Infected_specific_19_20'},1,'J1');
    xlswrite(tableFile,{'Summit_specific_19_20'},1,'K1');
    xlswrite(tableFile,{'Consensus_ID'},1,'L1');
    xlswrite(tableFile,{'Direction_match_19_20'},1,'M1');

    % Go through each row and add info as available
    for i=1:height(vtable)
        disp(i);
        if ismember(vtable{i,'MW'},overlapSet)
            xlswrite(tableFile,1,1,strcat('N',num2str(i+1)));
            currIdx = overlapIdx(overlapSet==vtable{i,'MW'});
            xlswrite(tableFile,infSpec(currIdx),1,strcat('J',num2str(i+1)));
            xlswrite(tableFile,sumSpec(currIdx),1,strcat('K',num2str(i+1)));
            xlswrite(tableFile,cmpdName(currIdx),1,strcat('L',num2str(i+1)));
            xlswrite(tableFile,dirTrend(currIdx),1,strcat('M',num2str(i+1)));
        else
            xlswrite(tableFile,0,1,strcat('N',num2str(i+1)));
        end
    end
end

if strcmp(expChoice,'19')
% Read in table with data from 2019 metabolomics experiment for volcano
    % plot
    [f2,d2] = uigetfile('Select manual data .xlsx');
    tableFile = strcat(d2,f2);
    vtable = readtable(tableFile);

    % Write new column headers for variables we're going to add
    xlswrite(tableFile,{'Overlap_19_20'},1,'N1');
    xlswrite(tableFile,{'Infected_specific_19_20'},1,'J1');
    xlswrite(tableFile,{'Summit_specific_19_20'},1,'K1');
    xlswrite(tableFile,{'Consensus_ID'},1,'L1');
    xlswrite(tableFile,{'Direction_match_19_20'},1,'M1');

    % Go through each row and add info as available
    for i=1:height(vtable)
        disp(i);
        if ismember(vtable{i,'MW'},overlapSet)
            xlswrite(tableFile,1,1,strcat('N',num2str(i+1)));
            currIdx = overlapIdx(overlapSet==vtable{i,'MW'});
            xlswrite(tableFile,infSpec(currIdx),1,strcat('J',num2str(i+1)));
            xlswrite(tableFile,sumSpec(currIdx),1,strcat('K',num2str(i+1)));
            xlswrite(tableFile,cmpdName(currIdx),1,strcat('L',num2str(i+1)));
            xlswrite(tableFile,dirTrend(currIdx),1,strcat('M',num2str(i+1)));
        else
            xlswrite(tableFile,0,1,strcat('N',num2str(i+1)));
        end
    end
end
