%% Perform locality analysis (Schulz & Zampieri et al., SciSig 2014)on
% metabolites and transcripts of epidermal skin samples from young and old
% donors (Kuehne & Hildebrand et al. 2016)

% Input variables
% metData: data strcuture of the non-targeted metabolomics measurements
% geneExpData: data strcuture of the transcriptomics measurements
% KEGG_hsa_RpModel: KEGG main reactant pair model specific for homo sapiens
% (hsa)

% Important note: Since the p-value calculation of the locality analysis
% depends on a permutation test, the p-values might slightly vary between
% individual runs of the algorithm.
%% import the data
load('skinAging_locality_data.mat');

%% correlate metabolites and gene expression
corrMetGene_skinAging = corrMetGeneExp_skinAging(metData, geneExpData);

%% perform locality analysis
[localityTableOldVsYoung, localityStructOldVsYoung] = localityMetGene_skinAging(KEGG_hsa_RpModel, metData, ...
    geneExpData, corrMetGene_skinAging, 'KEGGS', 'linlin','by gene - only connected');

%% generate excel worksheet of locality output
filename = 'localityOvsY_byGene - only connected.xls';
xlswrite(filename, localityTableOldVsYoung);

% add age correlation to excel worksheet
addAgeCorrelation2localityOut(filename, corrMetGene_skinAging, localityStructOldVsYoung, geneExpData);

%% make cytoscape output
locality2cytoscape_skinAging(localityStructOldVsYoung, KEGG_hsa_RpModel, corrMetGene_skinAging, metData, 'p', 0.01, 10)