function locality2cytoscape_skinAging(localityStruct, model, corrStruct,metData, tType, t, noOfNodes)

if nargin < 5
    tType = 'hits';
end
if nargin < 6
    t = 10; %threshold; hits: no of top hits or p: p-value
end
if nargin < 7
    noOfNodes = 10; % no of nodes to plot for each gene;
end
if noOfNodes > length(localityStruct.metIdxData)
    noOfNodes = length(localityStruct.metIdxData);
end
[sortp, idxP] = sort(localityStruct.p,'ascend');

switch tType
    case 'hits'
        idxPselected = idxP(sortp <= sortp(t));
    case 'p'
        idxPselected = idxP(sortp <= t);
end

outTableEdge = {'NodeID_1' 'EdgeID', 'NodeID_2' 'EdgeType'};
outTableNode = {'NodeID' 'NodeType' 'NodeName' 'NodeDescription' 'NodeMetGeneScore' 'nodeMetGeneP' 'NodeAgeCorrelationRho' 'NodeAgeCorrelationP' ,'nodeDetected'};

% 
switch upper(localityStruct.parameters.modelType)
    case 'RECONX'
        met2metMat = model.met2metCoFactorRemoved;
    case 'KEGGS'
        met2metMat = model.CompoundstoCompoundsviaRP;
end
allShortestPaths = graphallshortestpaths(met2metMat,'Directed',false);
genesProcessed = {''};
for i = 1:length(idxPselected)
    if ~ismember(genesProcessed, localityStruct.geneName(idxPselected(i)))
        genesProcessed = [genesProcessed; localityStruct.geneName(idxPselected(i))];
        % first print Node information to table info
        outTableNode = [outTableNode; writeNode(idxPselected(i), 'gene', localityStruct, corrStruct)];
        
        %write all reactions and metabolites directly connected to the gene
        switch upper(localityStruct.parameters.modelType)
            case 'RECONX'
                rxnIds = find(model.rxnGeneMat(:,localityStruct.geneIdxModel(idxPselected(i))) ~= 0);
            case 'KEGGS'
                ecIds = find(model.GeneToEC(localityStruct.geneIdxModel(idxPselected(i)),:) ~= 0);
                rxnIds = [];
                for e = 1:length(ecIds)
                    rxnIdsTemp = find(model.ECtoRP(ecIds(e),:) ~= 0);
                    rxnIds = [rxnIds; rxnIdsTemp'];
                end
                
        end
        
        
        metIds = [];
        %add all closest metabolites as start points
        for r = 1:length(rxnIds)
            outTableEdge = [outTableEdge; writeEdge(idxPselected(i),rxnIds(r),'gene-reaction', localityStruct, model,[],[])];
            outTableNode = [outTableNode; writeNode(rxnIds(r), 'reaction', localityStruct,[],[],...
                model,idxPselected(i))];
            switch upper(localityStruct.parameters.modelType)
                case 'RECONX'
                    metIdsTemp = find(model.S(:,rxnIds(r)) ~= 0);
                case 'KEGGS'
                    metIdsTemp = find(model.CompoundstoRP(:,rxnIds(r)) ~= 0);
            end
            for m = 1:length(metIdsTemp)
                outTableEdge = [outTableEdge; writeEdge(rxnIds(r),metIdsTemp(m),'reaction-metabolite', ...
                    localityStruct, model,[],localityStruct.geneName{idxPselected(i)})];
                outTableNode = [outTableNode; writeNode(metIdsTemp(m), 'metabolite', ...
                    localityStruct,corrStruct,metData,model,idxPselected(i))];
            end
            metIds = [metIds; metIdsTemp];
        end
        
        %get Ids of x closest nodes go through them and add them
        tempGene2MetDistance = localityStruct.gene2metDistMatRed(idxPselected(i),:);
        [sortDistTemp, idxDistSort] = sort(tempGene2MetDistance, 'ascend');
        if ~isinf(sortDistTemp(noOfNodes))
            idxDistSel = idxDistSort(sortDistTemp <= sortDistTemp(noOfNodes)) ;
        else
            uDist = unique(sortDistTemp);
            idxDistSel = idxDistSort(sortDistTemp <= uDist(end -1));
        end
        for m = 1:length(idxDistSel)
            if tempGene2MetDistance(idxDistSel(m)) > 1
                metIdxModelTemp = localityStruct.metIdxModel(idxDistSel(m));
                spTemp = allShortestPaths(metIds, metIdxModelTemp);
                idxClosestMet = find(spTemp == min(spTemp));
                for j = 1:length(idxClosestMet)
                    if spTemp(idxClosestMet(j)) == 1
                        outTableEdge = [outTableEdge; writeEdge(metIds(idxClosestMet(j)),metIdxModelTemp, ...
                            'metabolite-metabolite', localityStruct, model,[],localityStruct.geneName{idxPselected(i)})];
                        outTableNode = [outTableNode; writeNode(metIdxModelTemp, 'metabolite', ...
                            localityStruct,corrStruct,metData,model,idxPselected(i))];
                        metIds = [metIds; metIdxModelTemp];
                    else
                        %                disp('screeeewed');
                        [d,path] = graphshortestpath(met2metMat, metIds(idxClosestMet),...
                            localityStruct.metIdxModel(idxDistSel(m)), 'Directed',false);
                        for p = 1:length(path)-1
                            outTableEdge = [outTableEdge; writeEdge(path(p), path(p+1),'metabolite-metabolite', ...
                                localityStruct, model,[],localityStruct.geneName{idxPselected(i)})];
                            metIds = [metIds; path(p+1)];
                            outTableNode = [outTableNode; writeNode(path(p+1), 'metabolite', ...
                                localityStruct,corrStruct,metData,model,idxPselected(i))];
                        end
                    end
                end
            end
        end
    end
end

xlswrite(['locality_edge_' localityStruct.parameters.modelType '.xls'], outTableEdge);
xlswrite(['locality_node_' localityStruct.parameters.modelType '.xls'] , outTableNode);
end

function outNode = writeNode(idx, nodeType, localityStruct, corrStruct, metData, model, geneIdxLS)

switch nodeType
    case 'gene'
        dIdx = localityStruct.geneIdxData(idx);
        outNode = [localityStruct.geneName(idx) nodeType localityStruct.geneName(idx) ...
            localityStruct.geneDescription(idx) num2cell(2-log10(localityStruct.p(idx))) ...
            num2cell(localityStruct.p(idx)) num2cell(corrStruct.age.rhoGeneAgeSpearman(dIdx))...
            num2cell(corrStruct.age.pGeneAgeSpearman(dIdx)) num2cell(1)];
    case 'reaction'
        switch upper(localityStruct.parameters.modelType)
            case 'RECONX'
                outNode = [{[model.rxns{idx} '_' localityStruct.geneName{geneIdxLS}]} nodeType model.rxns(idx) ...
                    model.rxnNames(idx) num2cell(0) num2cell(1) num2cell(0) num2cell(1) num2cell(0.5)];
            case 'KEGGS'
                outNode = [{[model.RP{idx} '_' localityStruct.geneName{geneIdxLS}]} nodeType model.RP(idx) ...
                    model.RP(idx) num2cell(0) num2cell(1) num2cell(0) num2cell(1) num2cell(0.5)];
        end
    case 'metabolite'
        idMetLS = find(localityStruct.metIdxModel == idx);
        idxMetData = localityStruct.metIdxData(idMetLS);
        dIdx = localityStruct.geneIdxData(geneIdxLS);
        switch upper(localityStruct.parameters.modelType)
            case 'RECONX'
                if ~isempty(idMetLS)
                    outNode = [{[model.mets{idx} '_' localityStruct.geneName{geneIdxLS}]} nodeType model.mets(idx) ...
                        model.metNames(idx) num2cell(corrStruct.rhoMetGeneSpearman(idxMetData,dIdx)) ...
                        num2cell(corrStruct.pMetGeneSpearman(idxMetData,dIdx)) ...
                        num2cell(corrStruct.age.rhoMetAgeSpearman(idxMetData)) ...
                        num2cell(corrStruct.age.pMetAgeSpearman(idxMetData)) num2cell(1)];
                else
                    outNode = [{[model.mets{idx} '_' localityStruct.geneName{geneIdxLS}]} nodeType model.mets(idx) ...
                        model.metNames(idx) num2cell(0) num2cell(1) num2cell(0) num2cell(1) num2cell(0)];
                end
            case 'KEGGS'
                if ~isempty(idMetLS)
                    outNode = [{[model.Compounds{idx} '_' localityStruct.geneName{geneIdxLS}]} nodeType model.CompoundsName(idx) ...
                        model.Compounds(idx) num2cell(corrStruct.rhoMetGeneSpearman(idxMetData,dIdx)) ...
                        num2cell(corrStruct.pMetGeneSpearman(idxMetData,dIdx)) ...
                        num2cell(corrStruct.age.rhoMetAgeSpearman(idxMetData)) ...
                        num2cell(corrStruct.age.pMetAgeSpearman(idxMetData)) num2cell(1)];
                else
                    outNode = [{[model.Compounds{idx} '_' localityStruct.geneName{geneIdxLS}]} nodeType model.CompoundsName(idx) ...
                        model.Compounds(idx) num2cell(0) num2cell(1) num2cell(0) num2cell(1) num2cell(0)];
                end
        end
end
end

function outEdge = writeEdge(idxN1,idxN2, edgeType, localityStruct, model, metData, geneAdd)
switch edgeType
    case 'gene-reaction'
         switch upper(localityStruct.parameters.modelType)
             case 'RECONX'
                 outEdge = [localityStruct.geneName(idxN1) {[localityStruct.geneName{idxN1} '-' model.rxns{idxN2} '_' localityStruct.geneName{idxN1}]}...
                     {[model.rxns{idxN2} '_' localityStruct.geneName{idxN1}]} edgeType];
             case 'KEGGS'
                 outEdge = [localityStruct.geneName(idxN1) {[localityStruct.geneName{idxN1} '-' model.RP{idxN2} '_' localityStruct.geneName{idxN1}]}...
                     {[model.RP{idxN2} '_' localityStruct.geneName{idxN1}]} edgeType];
         end
    case 'reaction-metabolite'
        switch upper(localityStruct.parameters.modelType)
            case 'RECONX'
                outEdge = [{[model.rxns{idxN1} '_' geneAdd]} ...
                    {[model.rxns{idxN1} '_' geneAdd '-' model.mets{idxN2} '_' geneAdd]} ...
                    {[model.mets{idxN2} '_' geneAdd]} edgeType];
            case 'KEGGS'
                outEdge = [{[model.RP{idxN1} '_' geneAdd]} ...
                    {[model.RP{idxN1} '_' geneAdd '-' model.Compounds{idxN2} '_' geneAdd]} ...
                    {[model.Compounds{idxN2} '_' geneAdd]} edgeType];
        end
    case 'metabolite-metabolite'
        switch upper(localityStruct.parameters.modelType)
            case 'RECONX'
                outEdge = [{[model.mets{idxN1} '_' geneAdd]} ...
                    {[model.mets{idxN1} '_' geneAdd '-' model.mets{idxN2} '_' geneAdd]} ...
                    {[model.mets{idxN2} '_' geneAdd]} edgeType];
            case 'KEGGS'
                outEdge = [{[model.Compounds{idxN1} '_' geneAdd]} ...
                    {[model.Compounds{idxN1} '_' geneAdd '-' model.Compounds{idxN2} '_' geneAdd]} ...
                    {[model.Compounds{idxN2} '_' geneAdd]} edgeType];
        end
end
    
end

