graph_color_s.m 7.36 KB
Newer Older
Michaela Olson's avatar
Michaela Olson committed
1
function  graph_color_s(fig)
2
3
% used to color graph using the same color scheme each time
% apply to existing figure handle 
Michaela Olson's avatar
Michaela Olson committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

    %now to the scatter plot colors
    ca = get(fig,'CurrentAxes');
    
    %make so that it works with scatter and gscatter
    graph_class = class(get(ca,'Children'));
  
    scat = get(ca,'Children'); %array of all scatters in graph
    
    %if a gscatter object, need to change marker styles
    if strcmp(graph_class,'matlab.graphics.chart.primitive.Line') 
        for row = 1:size(scat,1)
            set(scat(row,1),'Marker','o');
            set(scat(row,1),'MarkerSize',6);
            ecolor = get(scat(row,1),'MarkerEdgeColor');
            set(scat(row,1),'MarkerFaceColor',ecolor);
            set(scat(row,1),'MarkerEdgeColor','none');
        end
    end
    
    %give a grey background color to graph
    set(ca,'Color',[0.8, 0.8, 0.8]);
    
    % array of marker options to rotate through
    markers = ['o','d','s','>','p','<','v'];
    
    
    % sort drug by MoA
    RNAP = ["RifT","RIF","RNAP"];
    efflux = ["efflux","Ver","Thi"];
34
    lipid = ["PZA","CCCP","Gra","Mon","Nig","Nis","BDQ","Clz"];
Michaela Olson's avatar
Michaela Olson committed
35
    protein = ["Kan","Amk","Cam","AZT","Cla","Dox","Gent","Strep","Tet","Tig","Lin","protein"];
36
37
38
    dna = ["Lev","Mox","MIT","Olf","dna","MOX"];
    cell_wall = ["Mer","Amp","Ctax","INH","ETA","IMI", ...
        "Van","Pre","Cyc","Del","EMB","cell_wall","Cer","THL"];
Michaela Olson's avatar
Michaela Olson committed
39
    controls = ["MeOH","EtOH","NaOH","water","DMSO","Untreated","control"];
40
    unknowns = ["Unknown2019", "Unknown2239","Unknown3285"];
Michaela Olson's avatar
Michaela Olson committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    extras = ["INH_control","INH_control_3x"];
    
    %Set up arrays of similar colors for different MoA
    RNAPcol = color_maker(RNAP, [255 255 0], [0 0 50]);
    effluxcol = color_maker(efflux,[100 19 38],[0 50 50]);
    lipidcol = color_maker(lipid,[0 83 166],[8 13 35]);
    proteincol = color_maker(protein,[55 92 0],[10 20 4]);
    dnacol = color_maker(dna,[255 234 196],[0,-38,-62]);
    cell_wallcol = color_maker(cell_wall,[52 0 109],[9 8 14]);
    controlscol = color_maker(controls,[33 33 33],[25 25 25]);
    unknownscol = color_maker(unknowns,[150 20 170],[20 40 -20]);
    extrascol = color_maker(extras,[255 255 255],[0 -15 -50]);

    %failsafe if a drug shows up that is not listed
    nogroup = 0;
    ct = 0;
    
    %going through each item in the scatter/line array
    for row = 1:size(scat,1)
        name = get(scat(row,1),'DisplayName');

        
        % want to see if it is dose response data--if so, chop off ending
        % so that it can sort into an exisitng category
        find_underscore_w_dose = '([_][0-9])\w+';
        % that regexp will look to see if there is an underscore followed
        % by a number, indicating a dose         
        % if this is not empty, then it is dose data
        is_dose = ~isempty(regexp(name,find_underscore_w_dose));
        
        if is_dose
            underscore_loc = regexp(name,find_underscore_w_dose);
            % find what the dose is for coloring purposes
            dose = name(underscore_loc+1:end);
            % want to extract before that underscore and continue with that
            name = name(1:underscore_loc-1);
            % we also want to add a border or something to differentiate
            % plotting 3x and 025x, etc. 
            if strcmp(dose,'025x')
                set(scat(row,1),'MarkerEdgeColor',[.12, .12, .12])
            elseif strcmp(dose,'05x')
                set(scat(row,1),'MarkerEdgeColor',[.5, .5, .5])
            elseif strcmp(dose,'075x')
                set(scat(row,1),'MarkerEdgeColor',[1 .75, .6])
            elseif strcmp(dose,'1x')
                set(scat(row,1),'MarkerEdgeColor',[1, 1, 1])
            end
            % no border for 3x
            
        end
        
        % ... if they belong to a group, use the correct color array to
        % change the color/marker
        if ismember(name,RNAP) 
            ind = find(strcmpi(RNAP,name));
            graph_helper(scat(row,1), RNAPcol, ind, markers)
            
        elseif ismember(name,efflux)
            ind = find(strcmpi(efflux,name));
            graph_helper(scat(row,1), effluxcol, ind, markers)
            
        elseif ismember(name, lipid)           
            ind = find(strcmpi(lipid,name));
            graph_helper(scat(row,1), lipidcol,ind,markers)
            
        elseif ismember(name, protein)
            ind = find(strcmpi(protein,name));
            graph_helper(scat(row,1), proteincol,ind,markers)
            
        elseif ismember(name, dna)
            ind = find(strcmp(dna,name));
            graph_helper(scat(row,1), dnacol,ind,markers)
            
        elseif ismember(name, cell_wall)
            ind = find(strcmpi(cell_wall,name));
            graph_helper(scat(row,1), cell_wallcol,ind,markers)
            
        elseif ismember(name, controls)
            ind = find(strcmpi(controls,name));
            graph_helper(scat(row,1), controlscol,ind,markers)
            set(scat(row,1),'MarkerEdgeColor',[.09, .09, .09])
        elseif ismember(name,unknowns)
            ind = find(strcmpi(unknowns,name));
            graph_helper(scat(row,1),unknownscol,ind,markers)

        elseif ismember(name,extras)
            ind = find(strcmpi(extras,name));
            graph_helper(scat(row,1),extrascol,ind,markers)
            set(scat(row,1),'MarkerEdgeColor',[.09, .09, .09])
            
        else 
             set(scat(row,1),'MarkerFaceColor',[1, 1, 1])
             nogroup = nogroup+1;
             disp(name)
             set(scat(row,1),'Marker',markers(nogroup));
        end
        
  
    end
    
   
end



function graph_helper(scat, rgb, ind,markers)
% helper function for graph_color_s
% inputs: scat: a line or scatter object
% rgb: array of rgb values from 0-255
% ind: the index of where we currently are in the rgb array
% rgb(ind,1) corresponds to the r value at this index
% markers: array of markers to swap through

    % obtain correct rgb values
    r = rgb(ind,1);
    g = rgb(ind,2);
    b = rgb(ind,3);
 
    % will cause to loop through markers array when it gets too big
    while ind > size(markers,2)
        ind = ind - size(markers,2);
    end
    
    
    % switch from 0 - 255 scale to 0 - 1 scale and set value
    set(scat,'MarkerFaceColor',[r/255, g/255, b/255])
    
    % p is smaller than the rest by deault, need to make it bigger
    % size works slightly differently for scatter vs line objects
    % need to check the class before changing the size
    if strcmp(markers(ind),'p')
        classy = class(scat);
        if strcmp(classy,'matlab.graphics.chart.primitive.Scatter')
            set(scat,'SizeData',60)
        else
            set(scat,'MarkerSize',10);
        end
    end
    
    %looping through markers array
    set(scat,'Marker',markers(ind));
    
end


function color_ary = color_maker(ary, rgb, rgbadd)
% helper function for graph_color_s
    a = [];
    r = rgb(1);
    g = rgb(2);
    b = rgb(3);
    
    radd = rgbadd(1);
    gadd = rgbadd(2);
    badd = rgbadd(3);
    
    for i = 1:size(ary,2)

        r = r + radd;
        if r > 255 
            r = 255;
        elseif r < 0
            r = 0;
        end
        
        g = g + gadd;
        if g > 255 
            g = 255;
        elseif g < 0
            g = 0;
        end
        
        b = b + badd;
        if b > 255 
            b = 255;
        elseif b < 0
            b = 0;
        end
        a = [a; [r g b]];
        
    end
    
    color_ary = a;
end