Drkcore

17 02 2012 chemoinformatics perl Tweet

XML::Writerを使ってCytoscape用のXGMMLを出力する

Perlでコードを書いていてCytoscape用にファイルを出力したかったんだがGraph::GMLは読み込み専用だし、Graph::XGMMLはCytoscapeじゃ読み込めないし、、、

Graph::XGMMLのソースコードを読んでみたら、XML::Writerを使えばいいみたいなので書いた。

sub write_xgmml {
  open my $output, '>', 'output.xgmml';
  my $xml  = XML::Writer->new(OUTPUT=>$output);
  $xml->xmlDecl('UTF-8');
  $xml->startTag('graph',
                directed=>"1",
                label=>"Sample1",
                'xmlns:dc'=>"http://purl.org/dc/elements/1.1/",
                'xmlns:xlink'=>"http://www.w3.org/1999/xlink",
                'xmlns:rdf'=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                'xmlns:cy'=>"http://www.cytoscape.org",
                'xmlns'=>"http://www.cs.rpi.edu/XGMML"
               );

  my $i = 1;
  my %dict;

  #add node
  foreach my $key ( keys %$ids ){
    $dict{$key} = $i;
    $xml->startTag('node',
                  id => $i,
                  label => $key,
                 );
    $xml->emptyTag('att',
                  type => 'string',
                  name => 'canonicalName',
                  value => $key
                 );

    $xml->emptyTag('att',
                  type => 'real',
                  name => 'property',
                  value => $ids->{$key}->{'property'} ? $ids->{$key}->{'property'} : 0.0
                 );

    $xml->endTag('node');
    $i++;
  }

  #add edge
  for my $e (@$spt) {
    $xml->startTag('edge',
                  label  => "$e->[0] to $e->[1]",
                  source => $dict{$e->[0]},
                  target => $dict{$e->[1]}
                 );
    $xml->emptyTag('att',
                  type=>'string',
                  name=>'canonicalName',
                  value=> "$e->[0] to $e->[1]"
                 );
    $xml->endTag('edge');
  }

  $xml->endTag('graph');
  $xml->end;
  close($output);
}

基本的にstartTagとendTagで包んでemptyTagで属性を入れてくだけですね。あとCytoscapeのIDって整数じゃないとダメだったような気がするので、そうなるように書いたんだけど実際どうだったかは忘れた。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021