-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhive.pl
executable file
·537 lines (518 loc) · 18.1 KB
/
hive.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#!/bin/env perl
=hey
Author: Shijian Sky Zhang
E-mail: zhangsjsky@pku.edu.cn
=cut
use 5.012;
use warnings;
use Getopt::Long;
use File::Basename;
sub usage{
my $scriptName = basename $0;
print <<HELP;
Usage: perl $scriptName INPUT
========== Options ==========
-d --dot FILE (Optional) The dot file, which is parsed by parsedot into segments.txt and links.txt.
A dot file can be generated by the makedot command.
If not specified, the segments.txt and links.txt must be pre-generated in the current directory.
The two files will be processed by linnet to generate the final hive plot.
--stack FILE (Optional) The stacked bar data file.
A stack file can be generated by the makestackeddata command
If not specified, the segments.txt and links.txt must be pre-generated in the current directory.
The two files will be processed by linnet to generate the final hive plot.
--dual Dual stack bar (for stack)
-n --norm DOU The max normalization for axis in stack plot
--sumnorm DOU The sum normalization for axis in stack plot
-c --cumul Is cumulated stack bar
-r --ribbon Draw in ribbon (auto set when -s)
-o --out STR Out-axis name[axisout]
--outCol STR Out-axis color[red]
--outNP STR Out-axis node position ([score], )
--outPS STR Out-axis node position sort ([num], )
--outNIP STR Out-axis node index position, i.e. place nodes on axes based on rank ordered, not absolute connectivity
--outSCH STR Hide out-axis self connection
--outSC Self connect nodes on out-axis (axis will be cloned and renamed with suffix _left/_right)
-a --all STR All-axis name[axisall]
--allCol STR All-axis color[yellow]
--allNP STR All-axis node position ([score], )
--allPS STR All-axis node position sort ([num], )
--allNIP STR All-axis node index position
--allSCH STR Hide all-axis self connection
--allSC Self connect nodes on out-axis (axis will be cloned and renamed with suffix _left/_right)
-i --in STR In-axis name[axisin]
--inCol STR In-axis color[green]
--inNP STR In-axis node position ([score], )
--inPS STR In-axis node position sort ([num], )
--inNIP STR In-axis node index position
--inSCH STR Hide in-axis self connection
--inSC Self connect nodes on out-axis (axis will be cloned and renamed with suffix _left/_right)
--scale INT Scale the axis length[image size]
--pixsize DOU [2.5]
--dot_example Print an short example of dot file
--stack_example Print an short example of stack file
--segment_example Print an short example of segment file
--link_example Print an short example of link file
-h --help Print this help information
HELP
exit(-1);
}
my ($dotFile, $oNodeIdxPos, $oSelfConnHide, $oSelfConn,
$aNodeIdxPos, $aSelfConnHide, $aSelfConn,
$iNodeIdxPos, $iSelfConnHide, $iSelfConn, $scaleNorm);
my ($stackFile, $dual, $norm, $sumnorm, $cumul);
my ($oName, $oCol, $oNodePos, $oNodePosSort, ) = ('axisout', 'red', 'score', 'num');
my ($aName, $aCol, $aNodePos, $aNodePosSort, ) = ('axisall', 'yellow', 'score', 'num');
my ($iName, $iCol, $iNodePos, $iNodePosSort, ) = ('axisin', 'green', 'score', 'num');
my ($ribbon, $pixSize) = ('no', 2.5);
GetOptions(
'd|dot=s' => \$dotFile,
'stack=s' => \$stackFile,
'dual' => \$dual,
'n|norm=s' => \$norm,
'sumnorm=s' => \$sumnorm,
'c|cumul' => \$cumul,
'r|ribbon' => \$ribbon,
'o|out=s' => \$oName,
'outCol=s' => \$oCol,
'outNP=s' => \$oNodePos,
'outPS=s' => \$oNodePosSort,
'outNIP' => \$oNodeIdxPos,
'outSCH' => \$oSelfConnHide,
'outSC' => \$oSelfConn,
'a|all=s' => \$aName,
'allCol=s' => \$aCol,
'allNP=s' => \$aNodePos,
'allPS=s' => \$aNodePosSort,
'allNIP' => \$aNodeIdxPos,
'allSCH' => \$aSelfConnHide,
'allSC' => \$aSelfConn,
'i|in=s' => \$iName,
'inCol=s' => \$iCol,
'inNP=s' => \$iNodePos,
'inPS=s' => \$iNodePosSort,
'inNIP' => \$iNodeIdxPos,
'inSCH' => \$iSelfConnHide,
'inSC' => \$iSelfConn,
's|scale=s' => \$scaleNorm,
'pixsize=s' => \$pixSize,
'h|help' => sub{usage()},
'dot_example' => sub{&show_dot()},
'stack_example' => sub{&show_stack},
'segment_example' => sub{&show_segment()},
'link_example' => sub{&show_link()}
) || usage();
if(defined $dotFile){
unless(-f 'parsedot.conf'){
open CONF, ">parsedot.conf" or die "Can't write to parsedot.conf";
say "Generating parsedot.conf...";
say CONF "<axes>";
if(defined $oSelfConn){
print CONF <<EOF;
<axis>
name = ${oName}_left
rule = !node_in && node_out
# rule = !node_out && node_neighbours_all_strict >=3 && node_neighbours_all_strict < 6 && node_in
color = $oCol
node_position = $oNodePos
node_position_sort = $oNodePosSort
self_connection_hide = no
multi_assign_ok = ${oName}_left,${oName}_right
self_connection = ${oName}_right
EOF
say CONF " node_index_position = yes" if defined $oNodeIdxPos;
say CONF " remap_connection = ${aName}_right=>${aName}_left" if defined $aSelfConn;
say CONF " remap_connection = ${iName}_left=>${iName}_right" if defined $iSelfConn;
print CONF <<EOF;
</axis>
<axis>
name = ${oName}_right
rule = !node_in && node_out
# rule = !node_out && node_neighbours_all_strict >=3 && node_neighbours_all_strict < 6 && node_in
color = $oCol
node_position = $oNodePos
node_position_sort = $oNodePosSort
self_connection_hide = no
multi_assign_ok = ${oName}_left,${oName}_right
self_connection = ${oName}_left
EOF
say CONF " node_index_position = yes" if defined $oNodeIdxPos;
say CONF " remap_connection = ${aName}_right=>${aName}_left" if defined $aSelfConn;
say CONF " remap_connection = ${iName}_left=>${iName}_right" if defined $iSelfConn;
say CONF " </axis>";
}else{
print CONF <<EOF;
<axis>
name = $oName
rule = !node_in && node_out
color = $oCol
node_position = $oNodePos
node_position_sort = $oNodePosSort
self_connection_hide = yes
EOF
say CONF " node_index_position = yes" if defined $oNodeIdxPos;
say CONF " remap_connection = ${aName}_right=>${aName}_left" if defined $aSelfConn;
say CONF " remap_connection = ${iName}_left=>${iName}_right" if defined $iSelfConn;
say CONF " </axis>";
}
if(defined $aSelfConn){
print CONF <<EOF;
<axis>
name = ${aName}_left
rule = node_in && node_out && node_in + node_out
color = $aCol
node_position = $aNodePos
node_position_sort = $aNodePosSort
self_connection_hide = no
multi_assign_ok = ${aName}_left,${aName}_right
self_connection = ${aName}_right
EOF
say CONF " node_index_position = yes" if defined $aNodeIdxPos;
say CONF " remap_connection = ${iName}_right=>${iName}_left" if defined $iSelfConn;
say CONF " remap_connection = ${oName}_left=>${oName}_right" if defined $oSelfConn;
print CONF <<EOF;
</axis>
<axis>
name = ${aName}_right
rule = node_in && node_out && node_in + node_out
color = $aCol
node_position = $aNodePos
node_position_sort = $aNodePosSort
self_connection_hide = no
multi_assign_ok = ${aName}_left,${aName}_right
self_connection = ${aName}_left
EOF
say CONF " node_index_position = yes" if defined $aNodeIdxPos;
say CONF " remap_connection = ${iName}_right=>${iName}_left" if defined $iSelfConn;
say CONF " remap_connection = ${oName}_left=>${oName}_right" if defined $oSelfConn;
say CONF " </axis>";
}else{
print CONF <<EOF;
<axis>
name = $aName
rule = node_in && node_out && node_in + node_out
color = $aCol
node_position = $aNodePos
node_position_sort = $aNodePosSort
self_connection_hide = yes
EOF
say CONF " node_index_position = yes" if defined $aNodeIdxPos;
say CONF " remap_connection = ${iName}_right=>${iName}_left" if defined $iSelfConn;
say CONF " remap_connection = ${oName}_left=>${oName}_right" if defined $oSelfConn;
say CONF " </axis>";
}
if(defined $iSelfConn){
print CONF <<EOF;
<axis>
name = ${iName}_left
rule = !node_out && node_in
color = $iCol
node_position = $iNodePos
node_position_sort = $iNodePosSort
self_connection_hide = no
multi_assign_ok = ${iName}_left,${iName}_right
self_connection = ${iName}_right
EOF
say CONF " node_index_position = yes" if defined $iNodeIdxPos;
say CONF " remap_connection = ${oName}_right=>${oName}_left" if defined $oSelfConn;
say CONF " remap_connection = ${aName}_left=>${aName}_right" if defined $aSelfConn;
print CONF <<EOF;
</axis>
<axis>
name = ${iName}_right
rule = !node_out && node_in
color = $iCol
node_position = $iNodePos
node_position_sort = $iNodePosSort
self_connection_hide = no
multi_assign_ok = ${iName}_left,${iName}_right
self_connection = ${iName}_left
EOF
say CONF " node_index_position = yes" if defined $iNodeIdxPos;
say CONF " remap_connection = ${oName}_right=>${oName}_left" if defined $oSelfConn;
say CONF " remap_connection = ${aName}_left=>${aName}_right" if defined $aSelfConn;
say CONF " </axis>";
}else{
print CONF <<EOF;
<axis>
name = $iName
color = $iCol
rule = !node_out && node_in
node_position = $iNodePos
node_position_sort = $iNodePosSort
self_connection_hide = yes
EOF
say CONF " node_index_position = yes" if defined $iNodeIdxPos;
say CONF " remap_connection = ${oName}_right=>${oName}_left" if defined $oSelfConn;
say CONF " remap_connection = ${aName}_left=>${aName}_right" if defined $aSelfConn;
say CONF " </axis>";
}
print CONF <<EOF;
</axes>
<links>
<link>
rule_node = node_out == 0 && node_in > 3
#rule_node1 =
color = green_a5
z = 15
thickness = 1
#show = no
</link>
<link>
rule_node = node_in > 3
color = red_a5
z = 10
thickness = 1
</link>
<link>
rule_node = node_all == 2
color = blue_a5
z = 20
thickness = 1
</link>
</links>
<files>
dir = .
segments = segments.txt
links = links.txt
</files>
EOF
}
say "Parsing dot file...";
`parsedot -conf parsedot.conf -dir . <$dotFile >parsedot.log 2>parsedot.err`;
}
if(defined $stackFile){
$ribbon = 'yes';
my $opt = "-dir .";
$opt .= " -dual" if defined $dual;
$opt .= " -norm $norm" if defined $norm;
$opt .= " -sumnorm $sumnorm" if defined $sumnorm;
$opt .= " -cumul" if defined $cumul;
say "Parsing stack file...";
`stackedbar $opt <$stackFile >stackedbar.log 2>stackedbar.err`;
}
unless(-f 'linnet.conf'){
open CONF, ">linnet.conf" or die "Can't write to linnet.conf";
say "Generating linnet.conf...";
say CONF <<EOF;
<colors>
<<include etc/colors.conf>>
<<include etc/brewer.conf>>
</colors>
<image>
size = 1000
dir = .
png = no
svg = yes
background = white
auto_alpha_steps = 10
</image>
<segments>
file = segments.txt
width = 5
spacing = __\$CONF{segments}{width}*3__
radius = __\$CONF{image}{size}/20__
</segments>
<links>
<link>
file = links.txt
ribbon = $ribbon
thickness = 1
color = black_a7
offset_start = __\$CONF{segments}{width}__
offset_end = __\$CONF{segments}{width}__
crest = __\$CONF{image}{size}/20__
bezier_radius_power = 2
</link>
</links>
EOF
if(defined $pixSize){
print CONF <<EOF;
<scale>
pixsize = $pixSize
</scale>
EOF
}
say CONF "<axes>";
if(defined $stackFile){
my $axisNum = `wc -l <segments.txt`;
my $stepAngle = 360/$axisNum;
my $angle = 0;
open SEG, "segments.txt" or die "Can't read segments.txt: $!";
my $i = 1;
while(<SEG>){
chomp;
my $axisID = (split)[0];
print CONF <<EOF;
<axis $i>
angle = $angle
scale = 1
reverse = no
segments = $axisID
</axis>
EOF
$i++;
$angle += $stepAngle;
}
}else{
$scaleNorm = '__$CONF{image}{size}__' unless defined $scaleNorm;
if(defined $oSelfConn){
print CONF <<EOF;
<axis aleft>
scale = 1
angle = 350
reverse = no
segments = ${oName}_left
EOF
say CONF " scale_norm = $scaleNorm";
print CONF <<EOF;
</axis>
<axis aright>
scale = 1
angle = 40
reverse = no
segments = ${oName}_right
EOF
say CONF " scale_norm = $scaleNorm";
say CONF " </axis>";
}else{
print CONF <<EOF;
<axis a>
scale = 1
angle = 0
reverse = no
segments = $oName
EOF
say CONF " scale_norm = $scaleNorm";
say CONF " </axis>";
}
if(defined $aSelfConn){
print CONF <<EOF;
<axis bleft>
scale = 1
angle = 110
reverse = no
segments = ${aName}_left
EOF
say CONF " scale_norm = $scaleNorm";
print CONF <<EOF;
</axis>
<axis bright>
scale = 1
angle = 160
reverse = no
segments = ${aName}_right
EOF
say CONF " scale_norm = $scaleNorm";
say CONF " </axis>";
}else{
print CONF <<EOF;
<axis b>
scale = 1
angle = 120
reverse = no
segments = $aName
EOF
say CONF " scale_norm = $scaleNorm";
say CONF " </axis>";
}
if(defined $iSelfConn){
print CONF <<EOF;
<axis cleft>
scale = 1
angle = 230
reverse = no
segments = ${iName}_left
EOF
say CONF " scale_norm = $scaleNorm";
print CONF <<EOF;
</axis>
<axis cright>
scale = 1
angle = 280
reverse = no
segments = ${iName}_right
EOF
say CONF " scale_norm = $scaleNorm";
say CONF " </axis>";
}else{
print CONF <<EOF;
<axis c>
scale = 1
angle = 240
reverse = no
segments = $iName
EOF
say CONF " scale_norm = $scaleNorm";
say CONF " </axis>";
}
}
say CONF "</axes>";
}
say "Drawing hive...";
`linnet -conf linnet.conf -file hiveplot.png >linnet.log 2>linnet.err`;
sub show_dot{
say <<EOF;
digraph example_5nodes_8edges {
node1 [color=bule];
node2;
node3;
node4;
node5;
node1 -> node2 [cost=4]
node1 -> node3
node1 -> node4
node2 -> node3
node2 -> node4
node2 -> node5
node3 -> node4
node3 -> node5
}
graph example_5nodes_8edges {
node1;
node2;
node3;
node4;
node5;
node1 -- node2
node1 -- node3
node1 -- node4
node2 -- node3
node2 -- node4
node2 -- node5
node3 -- node4
node3 -- node5
}
EOF
exit(-1);
}
sub show_stack{
say <<EOF;
0.090150989479609 0.35364988161776 0.25436193105378 0.326772487310053 0.0576129130845938 0.0103060975775797
0.422809756063918 0.228473162301084 0.165188211059863 0.0379145366331939 0.0217378734083639 0.359353362995354
0.0700319535741778 0.0624494686463672 0.162219210571154 0.151162753458981 0.512807674513482 0.264039283790673
0.368521319599996 0.0539913348125793 0.232805814876848 0.267989043737425 0.402303750528382 0.213584236305434
0.0484859812822996 0.301436152622209 0.185424832438355 0.216161178860347 0.00553778846517805 0.152717019330959
EOF
}
sub show_segment{
say <<EOF;
#axis_name axis_start axis_end ? axis_color
axisall_left 0 1 axisall_left yellow
axisall_right 0 1 axisall_right yellow
axisin 0 3 axisin green
axisout 0 3 axisout red
EOF
}
sub show_link{
say <<EOF;
#axis1_name axis1_start axis1_end axis2_name axis2_start axis2_end tags
axisout 3.000000 3.000000 axisall_left 0.000000 0.000000 # node1 node2
axisall_right 1.000000 1.000000 axisin 3.000000 3.000000 # node3 node4
axisout 3.000000 3.000000 axisall_left 1.000000 1.000000 # node1 node3
axisall_right 0.000000 0.000000 axisin 2.000000 2.000000 color=blue_a5,thickness=1,z=20 # node2 node5
axisout 3.000000 3.000000 axisin 3.000000 3.000000 # node1 node4
axisall_right 1.000000 1.000000 axisin 2.000000 2.000000 color=blue_a5,thickness=1,z=20 # node3 node5
axisall_right 0.000000 0.000000 axisin 3.000000 3.000000 # node2 node4
axisall_left 0.000000 0.000000 axisall_right 1.000000 1.000000 # node2 node3
EOF
}