-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_expression.pl
381 lines (293 loc) · 9.97 KB
/
map_expression.pl
1
2
3
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
34
35
36
37
38
39
40
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/perl
#
#################################################################################
# map_expression.pl #
#################################################################################
#================================================================================
# Copyright (C) 2014 - Sergio CASTILLO
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#================================================================================
use warnings;
use strict;
use Data::Dumper;
use Color::Spectrum::Multi;
use Getopt::Long;
use CGI;
#================================================================================
# VARIABLES
#================================================================================
my $expression_file;
my $svg_filename;
my $debug;
my $help;
my $html;
my $color_number = 20;
# Default
GetOptions(
'colornum=i' => \$color_number,
'exp=s' => \$expression_file,
'svg=s' => \$svg_filename,
'html=s' => \$html,
'debug' => \$debug,
'help' => \$help
);
help() if ($help or !$expression_file or !$svg_filename);
#================================================================================
# MAIN LOOP
#================================================================================
my $exp_info = expression($expression_file);
my @sorted_nodes = sort {
$exp_info->{$a} <=> $exp_info->{$b}
} keys %{ $exp_info };
# Get values
my @values = map {
$exp_info->{$_};
} @sorted_nodes;
# Get intervals
my ($min, $max) = ($values[0], $values[-1]);
my $magic_number = ($max - $min) / $color_number;
my @intervals = ($min);
calc_intervals(
$min,
$magic_number,
$color_number,
\@intervals
);
my $int_values = join_intervals(\@intervals);
# Assign color to each interval
my ($int_2_colors, $colors) = int_to_colors($int_values);
if ($html) {
print_colors(
$int_2_colors,
$int_values,
$expression_file,
$html
);
}
# Assign a color to each node
my $nodes_2_color = data_to_color(
$exp_info,
$int_2_colors
);
# Print svg
change_svg(
$nodes_2_color,
$svg_filename,
$colors
);
if ($debug) {
print STDERR Dumper($exp_info);
print STDERR Dumper($int_2_colors);
print STDERR Dumper($nodes_2_color);
}
#================================================================================
# FUNCTIONS
#================================================================================
#--------------------------------------------------------------------------------
sub expression {
my $filename = shift;
my %exp_info = ();
open my $fh, '<', $filename
or die "Can't open $filename :$!\n";
while (<$fh>) {
chomp;
my ($node, $value) = split /\t/, $_;
my $rounded_val = sprintf("%.4f", $value);
$exp_info{$node} = $rounded_val;
}
return \%exp_info;
} # sub expression
#--------------------------------------------------------------------------------
sub calc_intervals {
my $min = shift;
my $magic_number = shift;
my $color_number = shift;
my $intervals = shift;
my $int_value = $min;
for my $i (1..$color_number) {
$int_value += $magic_number;
push @{ $intervals }, $int_value;
}
return;
}
#--------------------------------------------------------------------------------
sub join_intervals {
my $intervals = shift;
my %int_values = ();
for my $i (0..@{ $intervals } -2) {
$int_values{$intervals[$i] . "-" . $intervals[$i+1]} = $i;
}
return \%int_values;
}
#--------------------------------------------------------------------------------
sub int_to_colors {
my $intervals = shift;
my $number = keys %{ $intervals };
my %int_2_colors = ();
my $spect = Color::Spectrum::Multi->new();
my $colors_needed = $number -1;
my @colors = $spect->generate(
$colors_needed,
"#E6FFFF",
"#000099"
);
unshift @colors, "#FFFFFF"; # Add white to first interval
my @sorted_int = sort {
$intervals->{$a} <=> $intervals->{$b}
} keys %{ $intervals };
foreach my $i (0..$#colors) {
$int_2_colors{$sorted_int[$i]} = [$i, $colors[$i]];
}
return (\%int_2_colors, \@colors);
}
#--------------------------------------------------------------------------------
sub print_colors {
my $int_2_colors = shift;
my $intervals = shift;
my $filename = shift;
my $html_name = shift;
my $out_name = '';
if ($html_name !~ /\.html$/) {
$out_name = $html_name . '.html';
} else {
$out_name = $html_name;
}
open my $html_fh, '>', "$out_name"
or die "Can't write to $out_name : $!\n";
my $cgi = CGI->new;
print $html_fh $cgi->header,
$cgi->start_html("${filename}_colors"),
$cgi->h1('Your colors');
print $html_fh '<table border = "1" cellspacing="0" >';
my @sorted_int = sort {
$intervals->{$a} <=> $intervals->{$b}
} keys %{ $intervals };
foreach my $int (@sorted_int) {
print $html_fh '<tr><td bgcolor="',
$int_2_colors->{$int}->[1], '">',
$int_2_colors->{$int}->[1], '</td>',
"<td><td bgcolor=",
$int_2_colors->{$int}->[1], '>',
"$int<td>", "</tr>", "\n";
}
print $html_fh $cgi->end_html;
close $html_fh;
return;
} # sub print_colors
#--------------------------------------------------------------------------------
sub data_to_color {
my $data = shift;
my $int_2_colors = shift;
my %data_2_color = ();
my ($min_int, $max_int);
foreach my $node (keys %{ $data }) {
foreach my $interval (keys %{ $int_2_colors }) {
my ($min, $max) = split /\-/, $interval;
my $rounded_min = sprintf("%.4f", $min);
my $rounded_max = sprintf("%.4f", $max);
my $rounded_val = sprintf("%.4f", $data->{$node});
if ($rounded_val >= $rounded_min and $rounded_val <= $rounded_max) {
$data_2_color{$node} = $int_2_colors->{$interval}->[1]
unless exists $data_2_color{$node};
}# if
} # foreach interval
} # foreach node
return \%data_2_color;
} # sub data_2_color
sub missing_nodes {
# This will be needed when $range option is provided,
## Find missing nodes
#my @sorted = sort {
# $int_2_colors->{$a}[0] <=> $int_2_colors->{$b}[0]
#} keys %$int_2_colors;
#my ($min_int, $max_int) = ($sorted[0], $sorted[-1]);
#my @missing_nodes = grep {
# !exists $data_2_color{$_}
#} keys %{ $data };
}
#--------------------------------------------------------------------------------
sub change_svg {
my $nodes_2_color = shift;
my $svg_file = shift;
my $colors = shift;
open my $svg_fh, '<', $svg_file
or die "Can't asdf open $svg_file : $!\n";
local $/ = '<circle';
my $first =<$svg_fh>;
$first =~ s/<circle//;
$first =~ s/[\s\t]+$//;
print "$first\n"; # skip first line;
print_legend($colors);
while (<$svg_fh>) {
chomp;
if ($_ =~ m/class=\"(\w+)\"/g) {
my $node = $1;
if (exists $nodes_2_color->{$node}) {
my $color = $nodes_2_color->{$node};
$_ =~ s/fill=\"(.*?)\"/fill=\"$color\"/g;
#$_ =~ s/fill=\"#(\d+)\"/fill=\"$color\"/ge;
print "\t<circle$_\n";
} else {
print "\t<circle$_\n";# if gene
}
} # if node
} # while
return;
} # sub change_svg
#--------------------------------------------------------------------------------
sub print_legend {
my $colors = shift;
my $x = 70;
my $y = 50;
foreach my $color (@{ $colors }) {
print "<rect", "\n",
"style=\"fill:$color;fill-opacity:1;stroke-opacity:1;stroke:#000000\"", "\n",
"id=\"rect3099\"", "\n",
"width=\"250\"", "\n",
"height=\"60\"", "\n",
"x=\"$x\"", "\n",
"y=\"$y\" />", "\n";
$y += 62;
}
return;
}
#--------------------------------------------------------------------------------
sub help {
print STDERR << 'EOF';
||--------------------------------------------------------------------------------||
|| This script maps expression data onto an svg graph. ||
|| It will create an svg file with different colors depending ||
|| on the expression of each node. The output will be printed ||
|| to STDOUT. ||
|| ||
|| Usage: ||
|| perl map_expression.pl <options> > /path/to/output.svg ||
|| ||
|| Options: ||
|| ||
|| REQUIRED: ||
|| --exp <file> - dot file with expression data. ||
|| --svg <file> - svg file. ||
|| ||
|| OPTIONAL: ||
|| --cutoff <integer> - Everything below this value will be white. ||
|| --html <file> - Creates an html file with the gradient generated. ||
|| --colornum <integer> - Set number of colors. ||
|| ||
||--------------------------------------------------------------------------------||
EOF
exit(0);
} # sub help