function bg = drawNetwork(F, uncCutoff, knockdowns, overexpressions, h)

if nargin < 5
    h = gca;
    if nargin < 4
        overexpressions = {};
        if nargin < 3
            knockdowns = {};
        end
    end
end

metaData = { ...
    'krbEGF',               'EGFR', 'Activation', 'EGF';
    'kruEGF',               'EGFR', 'Activation', 'EGF';
    'krbNGF',               'NGFR', 'Activation', 'NGF';
    'kruNGF',               'NGFR', 'Activation', 'NGF';
    'kEGF',                 'SOS', 'Activation',  'EGFR';
    'KmEGF',                'SOS', 'Activation',  'EGFR';
    'kNGF',                 'SOS', 'Activation',  'NGFR';
    'KmNGF',                'SOS', 'Activation',  'NGFR';
    'kdSos',                'SOS', 'Deactivation',  'P90Rsk';
    'KmdSos',               'SOS', 'Deactivation',  'P90Rsk';
    'kSos',                 'RAS', 'Activation',  'SOS';
    'KmSos',                'RAS', 'Activation',  'SOS';
    'kRasGap',              'RAS', 'Deactivation',  'RasGAP';
    'KmRasGap',             'RAS', 'Deactivation',  'RasGAP';
    'kRasToRaf1',           'RAF1', 'Activation',  'RAS';
    'KmRasToRaf1',          'RAF1', 'Activation',  'RAS';
    'kpRaf1',               'MEK', 'Activation',  'RAF1';
    'KmpRaf1',              'MEK', 'Activation',  'RAF1';
    'kpBRaf',               'MEK', 'Activation',  'BRAF';
    'KmpBRaf',              'MEK', 'Activation',  'BRAF';
    'kdMek',                'ERK', 'Activation',  'MEK'; 
    'KmdMek',               'ERK', 'Activation',  'MEK'
    'kpMekCytoplasmic',     'MEK', 'Deactivation',  'PP2A';
    'KmpMekCytoplasmic',    'MEK', 'Deactivation',  'PP2A';
    'kdErk',                'ERK', 'Deactivation',  'PP2A';
    'KmdErk',               'ERK', 'Deactivation',  'PP2A';
    'kpP90Rsk',             'P90Rsk', 'Activation',  'ERK';
    'KmpP90Rsk',            'P90Rsk', 'Activation',  'ERK';
    'kPI3K',                'PI3K', 'Activation',  'EGFR';
    'KmPI3K',               'PI3K', 'Activation',  'EGFR';
    'kPI3KRas',             'PI3K', 'Activation',  'RAS';
    'KmPI3KRas',            'PI3K', 'Activation',  'RAS';
    'kAkt',                 'AKT', 'Activation',  'PI3K';
    'KmAkt',                'AKT', 'Activation',  'PI3K';
    'kdRaf1ByAkt',          'RAF1', 'Deactivation',  'AKT';
    'KmRaf1ByAkt',          'RAF1', 'Deactivation',  'AKT';
    'kC3GNGF',              'C3G', 'Activation',  'NGFR';
    'KmC3GNGF',             'C3G', 'Activation',  'NGFR';
    'kC3G',                 'RAP1', 'Activation',  'C3G';
    'KmC3G',                'RAP1', 'Activation',  'C3G';
    'kRapGap',              'RAP1', 'Deactivation',  'RAPGAP';
    'KmRapGap',             'RAP1', 'Deactivation',  'RAPGAP'
    'kRap1ToBRaf',          'BRAF', 'Activation',  'RAP1';
    'KmRap1ToBRaf',         'BRAF', 'Activation',  'RAP1';
    'kdRaf1',               'RAF1', 'Deactivation',  'Raf1PPTase';
    'KmdRaf1',              'RAF1', 'Deactivation',  'Raf1PPTase';
    'kdBRaf',               'BRAF', 'Deactivation',  'Raf1PPTase';
    'KmdBRaf',              'BRAF', 'Deactivation',  'Raf1PPTase'};

targets = {metaData{:,2}};
sense   = {metaData{:,3}};
enzymes = {metaData{:,4}};
allSpecies = unique({targets{:}, enzymes{:}});


% Build the graph
M = zeros(length(allSpecies));

for i = 1:length(targets)
    targetInd = strcmp(allSpecies, targets{i});
    enzInd    = strcmp(allSpecies, enzymes{i});
    switch(sense{i})
        case 'Activation',   edge = 1;
        case 'Deactivation', edge = -1;
        otherwise
            error('Unknown Connection Type');
    end

    M(enzInd, targetInd) = edge;
end

%% Construct the Biograph object
graph = biograph(M, allSpecies, 'EdgeType', 'curved');

bg  = view(graph); %Does a deep copy so modify the copy

%% Do the layout
vSpace = 30;
hSpace = 60;
layout = {...
'AKT',	     hSpace*0,	 vSpace*2;
'BRAF',	     hSpace*4,	 vSpace*1
'C3G',	     hSpace*4,	 vSpace*4
'EGF',	     hSpace*1,	 vSpace*6
'EGFR',	     hSpace*1,   vSpace*5
'ERK',	     hSpace*2,	 vSpace*0
'MEK',	     hSpace*2,	 vSpace*1
'NGF',	     hSpace*3,	 vSpace*6
'NGFR',	     hSpace*3,	 vSpace*5
'P90Rsk',	 hSpace*1,   vSpace*0
'PI3K',	     hSpace*0,	 vSpace*4
'PP2A',	     hSpace*3,	 vSpace*0.5
'RAF1',	     hSpace*2,	 vSpace*2
'RAP1',	     hSpace*4,	 vSpace*3
'RAPGAP',	 hSpace*3,	 vSpace*4
'RAS',	     hSpace*2,	 vSpace*3
'Raf1PPTase',hSpace*3,	 vSpace*2
'RasGAP',	 hSpace*3,	 vSpace*3
'SOS',	     hSpace*2,	 vSpace*4};
for i=1:length(layout)
    node=getnodesbyid(bg, layout{i, 1});
    set(node, 'Position', [layout{i,2}, layout{i,3}]);
end
dolayout(bg, 'Paths', true);

% Activating Edges Black Inhibiting Edges Red
edges = bg.edges;
for i = 1:length(edges)
    edge = edges(i);
    
    switch(edge.Weight)
        case 1
            edge.LineColor=[0 0 0];
        case -1
            edge.LineColor=[1 0 0];
        otherwise
            error('Uknown Edge Weight');
    end
   
    
end

% Compute parameter uncertainty
V = inv(posdef(F)); % Variance matrix
unc = sqrt(diag(V)); % Parameter uncertainties

% Note which parameters belong to which edges
for i = 1:length(metaData)
    from  = metaData{i,4};
    to    = metaData{i,2};
    
    edge = bg.getedgesbynodeid(from, to);
    edge.UserData = [edge.UserData, i];
end

% Change edge weights based on parameter uncertainties
for i = 1:numel(edges)
    edge = edges(i);
    data = edge.UserData;

    count = 0;
    for j = 1:length(data);
        if(unc(data(j)) <= uncCutoff)
            count = count + 1;
        end
    end
   
    switch(count)
        case 0, edge.LineWidth = 1.0;
        case 1, edge.LineWidth = 2.0;
        case 2, edge.LineWidth = 4.0;
        otherwise
            error('Should only have 0,1,2 identified params');
    end          
end

% Change node weights based on highlights
nodes = bg.nodes;
for i = 1:numel(nodes)
    node = nodes(i);
    name = node.id;
    
    if any(strcmpi(knockdowns, name))
%        node.LineWidth = 2;
        node.Color = [1, .25, .25];
    end
    
    if any(strcmpi(overexpressions, name))
%        node.LineWidth = 2;
        node.Color = [.25, 1, .25];
    end
end

bgc = get(bg.h, 'Children');
set(h, 'Visible','off');
copyobj(bgc, h);

biographFigure = get(bg.h, 'Parent');
close(biographFigure);

