Skip to content

Commit 572cad8

Browse files
authored
Merge pull request #2222 from opencobra/develop
Develop
2 parents 14184be + b85d268 commit 572cad8

25 files changed

+637
-106
lines changed

external/visualization/distinguishable_colors/distinguishable_colors.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@
9090
lab = func(rgb);
9191
bglab = func(bg);
9292
else
93-
C = makecform('srgb2lab');
93+
C = makecform("srgb2lab");
94+
%C = makecform('srgb2cmyk');
9495
lab = applycform(rgb,C);
9596
bglab = applycform(bg,C);
9697
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Copyright (c) 2015, Jonathan C. Lansey
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in
12+
the documentation and/or other materials provided with the distribution
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
POSSIBILITY OF SUCH DAMAGE.
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
function lineStyles=linspecer(N,varargin)
2+
% function lineStyles = linspecer(N)
3+
% This function creates an Nx3 array of N [R B G] colors
4+
% These can be used to plot lots of lines with distinguishable and nice
5+
% looking colors.
6+
%
7+
% lineStyles = linspecer(N); makes N colors for you to use: lineStyles(ii,:)
8+
%
9+
% colormap(linspecer); set your colormap to have easily distinguishable
10+
% colors and a pleasing aesthetic
11+
%
12+
% lineStyles = linspecer(N,'qualitative'); forces the colors to all be distinguishable (up to 12)
13+
% lineStyles = linspecer(N,'sequential'); forces the colors to vary along a spectrum
14+
%
15+
% % Examples demonstrating the colors.
16+
%
17+
% LINE COLORS
18+
% N=6;
19+
% X = linspace(0,pi*3,1000);
20+
% Y = bsxfun(@(x,n)sin(x+2*n*pi/N), X.', 1:N);
21+
% C = linspecer(N);
22+
% axes('NextPlot','replacechildren', 'ColorOrder',C);
23+
% plot(X,Y,'linewidth',5)
24+
% ylim([-1.1 1.1]);
25+
%
26+
% SIMPLER LINE COLOR EXAMPLE
27+
% N = 6; X = linspace(0,pi*3,1000);
28+
% C = linspecer(N)
29+
% hold off;
30+
% for ii=1:N
31+
% Y = sin(X+2*ii*pi/N);
32+
% plot(X,Y,'color',C(ii,:),'linewidth',3);
33+
% hold on;
34+
% end
35+
%
36+
% COLORMAP EXAMPLE
37+
% A = rand(15);
38+
% figure; imagesc(A); % default colormap
39+
% figure; imagesc(A); colormap(linspecer); % linspecer colormap
40+
%
41+
% See also NDHIST, NHIST, PLOT, COLORMAP, 43700-cubehelix-colormaps
42+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43+
% by Jonathan Lansey, March 2009-2013 – Lansey at gmail.com %
44+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45+
%
46+
%% credits and where the function came from
47+
% The colors are largely taken from:
48+
% http://colorbrewer2.org and Cynthia Brewer, Mark Harrower and The Pennsylvania State University
49+
%
50+
%
51+
% She studied this from a phsychometric perspective and crafted the colors
52+
% beautifully.
53+
%
54+
% I made choices from the many there to decide the nicest once for plotting
55+
% lines in Matlab. I also made a small change to one of the colors I
56+
% thought was a bit too bright. In addition some interpolation is going on
57+
% for the sequential line styles.
58+
%
59+
%
60+
%%
61+
62+
%function lineStyles=linspecer(N,varargin)
63+
64+
if nargin==0 % return a colormap
65+
lineStyles = linspecer(128);
66+
return;
67+
end
68+
69+
if ischar(N)
70+
lineStyles = linspecer(128,N);
71+
return;
72+
end
73+
74+
if N<=0 % its empty, nothing else to do here
75+
lineStyles=[];
76+
return;
77+
end
78+
79+
% interperet varagin
80+
qualFlag = 0;
81+
colorblindFlag = 0;
82+
83+
if ~isempty(varargin)>0 % you set a parameter?
84+
switch lower(varargin{1})
85+
case {'qualitative','qua'}
86+
if N>12 % go home, you just can't get this.
87+
warning('qualitiative is not possible for greater than 12 items, please reconsider');
88+
else
89+
if N>9
90+
% warning(['Default may be nicer for ' num2str(N) ' for clearer colors use: whitebg(''black''); ']);
91+
end
92+
end
93+
qualFlag = 1;
94+
case {'sequential','seq'}
95+
lineStyles = colorm(N);
96+
return;
97+
case {'white','whitefade'}
98+
lineStyles = whiteFade(N);return;
99+
case 'red'
100+
lineStyles = whiteFade(N,'red');return;
101+
case 'blue'
102+
lineStyles = whiteFade(N,'blue');return;
103+
case 'green'
104+
lineStyles = whiteFade(N,'green');return;
105+
case {'gray','grey'}
106+
lineStyles = whiteFade(N,'gray');return;
107+
case {'colorblind'}
108+
colorblindFlag = 1;
109+
otherwise
110+
warning(['parameter ''' varargin{1} ''' not recognized']);
111+
end
112+
end
113+
% *.95
114+
% predefine some colormaps
115+
set3 = colorBrew2mat({[141, 211, 199];[ 255, 237, 111];[ 190, 186, 218];[ 251, 128, 114];[ 128, 177, 211];[ 253, 180, 98];[ 179, 222, 105];[ 188, 128, 189];[ 217, 217, 217];[ 204, 235, 197];[ 252, 205, 229];[ 255, 255, 179]}');
116+
set1JL = brighten(colorBrew2mat({[228, 26, 28];[ 55, 126, 184]; [ 77, 175, 74];[ 255, 127, 0];[ 255, 237, 111]*.85;[ 166, 86, 40];[ 247, 129, 191];[ 153, 153, 153];[ 152, 78, 163]}'));
117+
set1 = brighten(colorBrew2mat({[ 55, 126, 184]*.85;[228, 26, 28];[ 77, 175, 74];[ 255, 127, 0];[ 152, 78, 163]}),.8);
118+
119+
% colorblindSet = {[215,25,28];[253,174,97];[171,217,233];[44,123,182]};
120+
colorblindSet = {[215,25,28];[253,174,97];[171,217,233]*.8;[44,123,182]*.8};
121+
122+
set3 = dim(set3,.93);
123+
124+
if colorblindFlag
125+
switch N
126+
% sorry about this line folks. kind of legacy here because I used to
127+
% use individual 1x3 cells instead of nx3 arrays
128+
case 4
129+
lineStyles = colorBrew2mat(colorblindSet);
130+
otherwise
131+
colorblindFlag = false;
132+
warning('sorry unsupported colorblind set for this number, using regular types');
133+
end
134+
end
135+
if ~colorblindFlag
136+
switch N
137+
case 1
138+
lineStyles = { [ 55, 126, 184]/255};
139+
case {2, 3, 4, 5 }
140+
lineStyles = set1(1:N);
141+
case {6 , 7, 8, 9}
142+
lineStyles = set1JL(1:N)';
143+
case {10, 11, 12}
144+
if qualFlag % force qualitative graphs
145+
lineStyles = set3(1:N)';
146+
else % 10 is a good number to start with the sequential ones.
147+
lineStyles = cmap2linspecer(colorm(N));
148+
end
149+
otherwise % any old case where I need a quick job done.
150+
lineStyles = cmap2linspecer(colorm(N));
151+
end
152+
end
153+
lineStyles = cell2mat(lineStyles);
154+
155+
end
156+
157+
% extra functions
158+
function varIn = colorBrew2mat(varIn)
159+
for ii=1:length(varIn) % just divide by 255
160+
varIn{ii}=varIn{ii}/255;
161+
end
162+
end
163+
164+
function varIn = brighten(varIn,varargin) % increase the brightness
165+
166+
if isempty(varargin),
167+
frac = .9;
168+
else
169+
frac = varargin{1};
170+
end
171+
172+
for ii=1:length(varIn)
173+
varIn{ii}=varIn{ii}*frac+(1-frac);
174+
end
175+
end
176+
177+
function varIn = dim(varIn,f)
178+
for ii=1:length(varIn)
179+
varIn{ii} = f*varIn{ii};
180+
end
181+
end
182+
183+
function vOut = cmap2linspecer(vIn) % changes the format from a double array to a cell array with the right format
184+
vOut = cell(size(vIn,1),1);
185+
for ii=1:size(vIn,1)
186+
vOut{ii} = vIn(ii,:);
187+
end
188+
end
189+
%%
190+
% colorm returns a colormap which is really good for creating informative
191+
% heatmap style figures.
192+
% No particular color stands out and it doesn't do too badly for colorblind people either.
193+
% It works by interpolating the data from the
194+
% 'spectral' setting on http://colorbrewer2.org/ set to 11 colors
195+
% It is modified a little to make the brightest yellow a little less bright.
196+
function cmap = colorm(varargin)
197+
n = 100;
198+
if ~isempty(varargin)
199+
n = varargin{1};
200+
end
201+
202+
if n==1
203+
cmap = [0.2005 0.5593 0.7380];
204+
return;
205+
end
206+
if n==2
207+
cmap = [0.2005 0.5593 0.7380;
208+
0.9684 0.4799 0.2723];
209+
return;
210+
end
211+
212+
frac=.95; % Slight modification from colorbrewer here to make the yellows in the center just a bit darker
213+
cmapp = [158, 1, 66; 213, 62, 79; 244, 109, 67; 253, 174, 97; 254, 224, 139; 255*frac, 255*frac, 191*frac; 230, 245, 152; 171, 221, 164; 102, 194, 165; 50, 136, 189; 94, 79, 162];
214+
x = linspace(1,n,size(cmapp,1));
215+
xi = 1:n;
216+
cmap = zeros(n,3);
217+
for ii=1:3
218+
cmap(:,ii) = pchip(x,cmapp(:,ii),xi);
219+
end
220+
cmap = flipud(cmap/255);
221+
end
222+
223+
function cmap = whiteFade(varargin)
224+
n = 100;
225+
if nargin>0
226+
n = varargin{1};
227+
end
228+
229+
thisColor = 'blue';
230+
231+
if nargin>1
232+
thisColor = varargin{2};
233+
end
234+
switch thisColor
235+
case {'gray','grey'}
236+
cmapp = [255,255,255;240,240,240;217,217,217;189,189,189;150,150,150;115,115,115;82,82,82;37,37,37;0,0,0];
237+
case 'green'
238+
cmapp = [247,252,245;229,245,224;199,233,192;161,217,155;116,196,118;65,171,93;35,139,69;0,109,44;0,68,27];
239+
case 'blue'
240+
cmapp = [247,251,255;222,235,247;198,219,239;158,202,225;107,174,214;66,146,198;33,113,181;8,81,156;8,48,107];
241+
case 'red'
242+
cmapp = [255,245,240;254,224,210;252,187,161;252,146,114;251,106,74;239,59,44;203,24,29;165,15,21;103,0,13];
243+
otherwise
244+
warning(['sorry your color argument ' thisColor ' was not recognized']);
245+
end
246+
247+
cmap = interpomap(n,cmapp);
248+
end
249+
250+
% Eat a approximate colormap, then interpolate the rest of it up.
251+
function cmap = interpomap(n,cmapp)
252+
x = linspace(1,n,size(cmapp,1));
253+
xi = 1:n;
254+
cmap = zeros(n,3);
255+
for ii=1:3
256+
cmap(:,ii) = pchip(x,cmapp(:,ii),xi);
257+
end
258+
cmap = (cmap/255); % flipud??
259+
end
260+
261+
262+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function [geneList] = findGenesFromMets(model, metList, param)
2+
% find the set of genes that correspond to a set of metabolites
3+
%
4+
% USAGE:
5+
%
6+
% [geneList] = findGenesFromMets(model, metabolites)
7+
%
8+
% INPUTS:
9+
% model: COBRA model structure
10+
% metList: List of metabolite identifiers in a model to find the corresponding genes for
11+
%
12+
% OUTPUT:
13+
% geneList: List of genes corresponding to reactions
14+
%
15+
% .. Author: - Ronan Fleming (11/9/23)
16+
17+
if ~exist('param','var')
18+
param.minimal=0;
19+
end
20+
21+
[metInex,LOCB] = ismember(model.mets,metList);
22+
23+
missingMet = setdiff(metList,model.mets);
24+
if ~isempty(missingMet)
25+
for i = 1:length(missingMet)
26+
fprintf('%s\n',['The metabolite ', missingMet{i},' is not in your model!']);
27+
end
28+
end
29+
30+
%
31+
metList = model.mets(metInex);
32+
33+
if param.minimal==1
34+
[rxnList, ~] = findRxnsFromMets(model, metList,'minimalReactionN',1);
35+
else
36+
[rxnList, ~] = findRxnsFromMets(model, metList);
37+
end
38+
39+
[rxnIndex,LOCB] = ismember(model.rxns,rxnList);
40+
41+
%Create the rxnGeneMat field if not present
42+
if ~isfield(model,'rxnGeneMat')
43+
model = buildRxnGeneMat(model);
44+
end
45+
46+
colBool=true(size(model.rxnGeneMat,2),1);
47+
mode='inclusive';
48+
geneBool = getCorrespondingCols(model.rxnGeneMat, rxnIndex, colBool, mode);
49+
50+
% N = model.S~=0;
51+
% %zero out non-index metabolites and external reactions
52+
% if isfield(model,'SConsistentRxnBool')
53+
% N(~index,~model.SConsistentRxnBool)=0;
54+
% else
55+
% if isfield(model,'SIntRxnBool')
56+
% N(~index,~model.SIntRxnBool)=0;
57+
% end
58+
% end
59+
% %setup the problem for optimizeCardinality
60+
% problem.p = true(size(N,2),1);
61+
% problem.q = false(size(N,2),1);
62+
% problem.r = false(size(N,2),1);
63+
% problem.A = N;
64+
% problem.b = ones(size(N,1),1);
65+
% problem.b(~index)=0;
66+
% problem.csense(1:size(N,1),1) = 'G';
67+
% problem.lb = zeros(size(N,2),1);
68+
% problem.ub = 10*ones(size(N,2),1);
69+
% problem.c = zeros(size(N,2),1);
70+
% %find the minimal number of reactions corresponding to those metabolites
71+
% param.printLevel=1;
72+
% solution = optimizeCardinality(problem, param);
73+
% %reaction indices
74+
% totals = solution.x ~=0;
75+
76+
geneList = model.genes(geneBool);
77+
78+

0 commit comments

Comments
 (0)