forked from daisieh/phylogenomics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcirclegraphs.pl
335 lines (270 loc) · 10.1 KB
/
circlegraphs.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
use CircleGraph;
=pod
CircleGraph $circlegraph_obj draw_circle_graph ( String $datafile, CircleGraph $circlegraph_obj )
$datafile is a tab-delimited file:
- the first column contains positions around a circle,
scaled to the size of the last row's value
- each subsequent column contains the values to graph, one graph per column
- the first row contains the names of the columns
$circlegraph_obj is an optional parameter for an existing CircleGraph.
The function returns a CircleGraph object with the values plotted,
positions mapped around the edge, and a legend describing the graph.
=cut
sub draw_circle_graph {
my $datafile = shift;
my $circlegraph_obj = shift;
my $circle_size = shift;
unless ($circlegraph_obj) {
$circlegraph_obj = new CircleGraph();
}
open DATAFH, "<$datafile" or die "$datafile failed to open\n";
my $max_diffs = 0;
my $total_elems;
my @positions, @differences;
my $line = readline DATAFH;
$line =~ s/\n//;
my @labels = split /\t/, $line;
shift @labels;
my @graphs = ();
for (my $i=0; $i<@labels; $i++) {
my @curr_array = ();
push (@graphs, \@curr_array);
}
$line = readline DATAFH;
while ($line ne "") {
$line =~ s/\n//;
my @items = split ('\t', $line);
my $pos = shift @items;
$total_elems = push (@positions, $pos);
for (my $i=0; $i<@items; $i++) {
@items[$i] =~ s/\n//;
push (@{$graphs[$i]}, @items[$i]);
}
push (@differences, @items);
$line = readline DATAFH;
}
my @sorted = sort {$a <=> $b} @differences;
$max_diffs = @sorted[@sorted-1];
$max_diffs =~ s/\n//;
if ($max_diffs == 0) {
die "Couldn't draw graph: all values are 0.";
}
for(my $i=0; $i<@graphs; $i++) {
for (my $j=0; $j<@{$graphs[$i]}; $j++) {
@{$graphs[$i]}[$j] = (@{$graphs[$i]}[$j]/$max_diffs);
}
}
# draw background circles
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius);
$circlegraph_obj->draw_circle($circlegraph_obj->outer_radius);
# draw graphs
for (my $j=0; $j<@graphs; $j++) {
$circlegraph_obj->plot_line(\@positions, $graphs[$j], {color=>$j});
$circlegraph_obj->append_to_legend("@labels[$j]", "$j");
}
# draw labels around the edge
$circlegraph_obj->set_font("Helvetica", 6, "black");
if ($circle_size eq "") {
$circle_size = @positions[@positions-1];
}
for (my $i = 0; $i < $total_elems; $i++) {
my $angle = (@positions[$i]/$circle_size) * 360;
my $radius = $circlegraph_obj->outer_radius + 10;
my $label = @positions[$i];
if (($label % 1000)!=0) { $label = "-"; }
$circlegraph_obj->circle_label($angle, $radius, "$label");
}
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius, {'filled'=>1, 'color'=>"white"} );
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius);
$circlegraph_obj->append_to_legend("Maximum percent difference ($max_diffs) is scaled to 1");
return $circlegraph_obj;
}
=pod
CircleGraph $circlegraph_obj plots_around_circle ( String $datafile, CircleGraph $circlegraph_obj )
$datafile is a tab-delimited file:
- the first column contains positions around a circle
- each subsequent column contains the values to graph, one graph per column
- the first row contains the names of the columns
$circlegraph_obj is an optional parameter for an existing CircleGraph.
The function returns a CircleGraph object with the values plotted,
positions mapped around the edge, and a legend describing the graph.
=cut
sub plots_around_circle {
my $datafile = shift;
my $circlegraph_obj = shift;
my $circle_size = shift;
unless ($circlegraph_obj) {
$circlegraph_obj = new CircleGraph();
}
open DATAFH, "<$datafile" or die "$datafile failed to open\n";
my $max_diffs = 0;
my $total_elems;
my @positions, @differences;
my $line = readline DATAFH;
$line =~ s/\n//;
my @labels = split /\t/, $line;
shift @labels;
my @graphs = ();
for (my $i=0; $i<@labels; $i++) {
my @curr_array = ();
push (@graphs, \@curr_array);
}
$line = readline DATAFH;
while ($line ne "") {
$line =~ s/\n//;
my @items = split ('\t', $line);
my $pos = shift @items;
$total_elems = push (@positions, $pos);
for (my $i=0; $i<@items; $i++) {
@items[$i] =~ s/\n//;
push (@{$graphs[$i]}, @items[$i]);
}
push (@differences, @items);
$line = readline DATAFH;
}
my @sorted = sort {$a <=> $b} @differences;
$max_diffs = @sorted[@sorted-1];
$max_diffs =~ s/\n//;
if ($max_diffs == 0) {
die "Couldn't draw graph: all values are 0.";
}
for(my $i=0; $i<@graphs; $i++) {
for (my $j=0; $j<@{$graphs[$i]}; $j++) {
@{$graphs[$i]}[$j] = (@{$graphs[$i]}[$j]/$max_diffs);
}
}
# draw background circles
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius);
$circlegraph_obj->draw_circle($circlegraph_obj->outer_radius);
# draw graphs
for (my $j=0; $j<@graphs; $j++) {
$circlegraph_obj->plot_points(\@positions, $graphs[$j], {color=>$j,radius=>((($j+1)*15)-5),width=>10,angle=>0.3});
$circlegraph_obj->append_to_legend("@labels[$j]", "$j");
}
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius, {'filled'=>1, 'color'=>"white"} );
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius);
return $circlegraph_obj;
}
=pod
CircleGraph $circlegraph_obj draw_gene_map ( String $gene_file, CircleGraph $circlegraph_obj )
$gene_file is a tab-delimited file of gene locations, such as generated by get_locations_from_genbank_file()
$circlegraph_obj is an optional parameter for an existing CircleGraph.
The function returns a CircleGraph object with the gene locations plotted around the edge
of an inner circle and the names plotted inside the circle.
=cut
sub draw_gene_map {
my $gene_file = shift;
my $circlegraph_obj = shift;
my $params = shift;
unless ($circlegraph_obj) {
$circlegraph_obj = new CircleGraph();
}
my $direction = "IN";
my $width = 5;
my $color = "tardis";
if (ref($params) eq "HASH") {
if (exists $params->{"direction"}) {
$direction = $params->{"direction"};
}
if (exists $params->{"width"}) {
$width = $params->{"width"};
}
if (exists $params->{"color"}) {
$width = $params->{"color"};
}
}
open INPUTFILE, "<$gene_file" or die "$gene_file failed to open\n";
my @inputs = <INPUTFILE>;
close INPUTFILE;
while (@inputs[0] !~ /\t/) { #there's some sort of header
shift @inputs;
if (@inputs == 0) {
die "no data in $gene_file.\n";
}
}
(undef, undef, my $circle_size, undef) = split /\t/, pop @inputs;
$circle_size =~ s/\n//;
my @labels = ();
for (my $i = 0; $i < @inputs; $i++) {
my $line = @inputs[$i];
my ($label, $start, $stop, $value) = split /\t/, $line;
$value =~ s/\n//;
if ($value eq "") {
$value = 0;
}
my $start_angle = ($start/$circle_size) * 360;
my $stop_angle = ($stop/$circle_size) * 360;
my $radius = $circlegraph_obj->inner_radius;
if ($direction eq "OUT") {
$radius = $circlegraph_obj->outer_radius + $width;
}
$circlegraph_obj->draw_filled_arc ($radius, $start_angle, $stop_angle, {color=>$color});
# label this element
my $center_angle = ($start_angle + $stop_angle) / 2;
push @labels, "$label\t$center_angle";
}
if ($direction eq "OUT") {
$circlegraph_obj->draw_circle($circlegraph_obj->outer_radius, {filled => 1, color => "white"});
$circlegraph_obj->draw_circle($circlegraph_obj->outer_radius);
$circlegraph_obj->set_font("Helvetica", 8, "black");
foreach my $line (@labels) {
$line =~ /(.+?)\t(.+?)$/;
$circlegraph_obj->circle_label($2, $circlegraph_obj->outer_radius + 5 + $width, $1, "left");
}
} else {
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius - $width, {filled => 1, color => "white"});
$circlegraph_obj->draw_circle($circlegraph_obj->inner_radius);
$circlegraph_obj->set_font("Helvetica", 6, "black");
foreach my $line (@labels) {
$line =~ /(.+?)\t(.+?)$/;
$circlegraph_obj->circle_label($2, $circlegraph_obj->inner_radius - 5 - $width, $1, "right");
}
}
return $circlegraph_obj;
}
=pod
CircleGraph $circlegraph_obj draw_regions ( String $region_file, CircleGraph $circlegraph_obj, (optional) $color, (optional) $radius )
$region_file is a tab-delimited file of region locations, with the size of the circle as the last entry.
$circlegraph_obj is an optional parameter for an existing CircleGraph.
=cut
sub draw_regions {
my $region_file = shift;
my $circlegraph_obj = shift;
my $color = shift;
my $radius = shift;
unless ($circlegraph_obj) {
$circlegraph_obj = new CircleGraph();
}
if ($color eq "") {
$color = "tardis";
}
unless ($radius) {
$radius = $circlegraph_obj->inner_radius;
}
open INPUTFILE, "<$region_file" or die "$region_file failed to open\n";
my @inputs = <INPUTFILE>;
close INPUTFILE;
while (@inputs[0] !~ /\t/) { #there's some sort of header
shift @inputs;
if (@inputs == 0) {
die "no data in $region_file.\n";
}
}
(undef, undef, my $circle_size, undef) = split /\t/, pop @inputs;
$circle_size =~ s/\n//;
my @labels = ();
for (my $i = 0; $i < @inputs; $i++) {
my $line = @inputs[$i];
my ($label, $start, $stop, $value) = split /\t/, $line;
$value =~ s/\n//;
if ($value eq "") {
$value = 0;
}
my $start_angle = ($start/$circle_size) * 360;
my $stop_angle = ($stop/$circle_size) * 360;
$circlegraph_obj->draw_arc ($radius, $start_angle, $stop_angle, {color => "$color", width => 5});
}
return $circlegraph_obj;
}
# must return 1 for the file overall.
1;