通过读取Pos文件中节点的位置信息来更新GML中节点的位置,可以用于网络可视化的人工布局。
以下为脚本内容:
--------------------------------
use Data::Dumper;
my $a=shift;
my $b=shift;
open(A,$a)||die("Can not open the GML file!\n");
open(B,$b)||die("Can not open the Pos file!\n");
my $Creator="Cytoscape";
my $Version="1.0";
my %graph;
my %pos;
my @curNode;
push(@curNode,\%graph);
while(<A>){ #读入GML并将其放入一个hash中
if(/(\w+)\s\[/){
my %node=();
push(@{$curNode[-1]{$1}},\%node);#链接子节点
push(@curNode,\%node); #将子节点地址放入寻找路径
}else{
if(/([\w\d]+)\t(\S+)/){
$curNode[-1]{$1}=$2;
}
}
if(/\]\n/){pop @curNode;}#遇到末端 ],删除最后的路径,跳到上级路径
}
while(<B>){#读取list
chomp;
my @tmp=split(/\t/,$_);
if($tmp[1] and $tmp[2]){
$pos{$tmp[0]}{x}=$tmp[1];
$pos{$tmp[0]}{y}=$tmp[2];
}
}
update(\%graph);
outNode(\%graph);
sub update(\%,$){
my $data=shift;
my $nodeName=shift;
my $sItem;
if($nodeName eq "node"){
my $label=$data->{label};
$label=~s/^\"//;
$label=~s/\"$//;
my $x=$pos{$label}{x};
my $y=$pos{$label}{y};
if(defined $pos{$label}{x} and defined $pos{$label}{x} ){
$data->{graphics}->[0]->{x}=$x;
$data->{graphics}->[0]->{y}=$y;
}
}
foreach $sItem (keys %{$data}) {
if( ref($data->{$sItem}) eq "ARRAY"){
my $sY;
foreach $sY (@{$data->{$sItem}}) {
update($sY,$sItem);
}
}else{}
}
}
sub outNode(\%,$){
my $data=shift;
my $nodeName=shift;
my $depth=shift;
my $sItem;
print "\t" x $depth if($nodeName);
print "$nodeName\t[\n" if($nodeName);
foreach $sItem (keys %{$data}) {
if( ref($data->{$sItem}) eq "ARRAY"){
my $sY;
foreach $sY (@{$data->{$sItem}}) {
outNode($sY,$sItem,$depth+1);
}
}else{
print "\t" x $depth;
print "$sItem\t$data->{$sItem}\n";
}
}
print "\t" x $depth if($nodeName);
print "]\n" if($nodeName);
}
https://blog.sciencenet.cn/blog-662110-515792.html
下一篇:
使用Google AJAX Libraries API 加载JQuery