BEGIN{
$FAC
= Bio::Seq::SeqFactory->new(
-type
=>
'Bio::Seq::RichSeq'
);
}
sub
next_network {
my
$self
=
shift
;
my
%seen_nodes
= ();
my
$graph
= new Bio::Graph::ProteinGraph();
while
(
my
$l
=
$self
->_readline() ) {
my
(
$edge_id
,
$n1
,
$s1
,
$p1
,
$g1
,
$n2
,
$s2
,
$p2
,
$g2
,
$score
) =
$l
=~/^DIP:(\d+E)\t+
(DIP\S+)\t+
(SWP\S+)?\t*
(PIR\S+)?\t*
(GI\S+)\t+
(DIP\S+)\t+
(SWP\S+)?\t*
(PIR\S+)?\t*
(GI\S+)\t*
(\d\.\d+)?
/x;
if
(
defined
(
$self
->{
'_th'
}) &&
defined
(
$score
)) {
next
unless
$score
>=
$self
->{
'_th'
};
}
my
(
$node1
,
$node2
);
if
(!
exists
(
$seen_nodes
{
$n1
}) ) {
my
$acc
=
$s1
||
$p1
||
$g1
;
my
$ac
=
$self
->_add_db_links(
$acc
,
$s1
,
$p1
,
$n1
,
$g1
);
$node1
=
$FAC
->create(
-accession_number
=>
$acc
,
-primary_id
=>
$g1
,
-display_id
=>
$acc
,
-annotation
=>
$ac
,
);
for
my
$n
(
$g1
,
$p1
,
$s1
,
$n1
) {
$seen_nodes
{
$n
} =
$node1
if
$n
;
}
}
else
{
$node1
=
$seen_nodes
{
$n1
};
}
if
(!
exists
(
$seen_nodes
{
$n2
}) ) {
my
$acc
=
$s2
||
$p2
||
$g2
;
my
$ac
=
$self
->_add_db_links(
$acc
,
$s2
,
$p2
,
$n2
,
$g2
);
$node2
=
$FAC
->create(
-accession_number
=>
$acc
,
-primary_id
=>
$g2
,
-display_id
=>
$acc
,
-annotation
=>
$ac
,
);
for
my
$n
(
$g2
,
$p2
,
$s2
,
$n2
) {
$seen_nodes
{
$n
} =
$node2
if
$n
;
}
}
else
{
$node2
=
$seen_nodes
{
$n2
};
}
$graph
->add_edge(Bio::Graph::Edge->new(
-nodes
=> [
$node1
,
$node2
],
-weight
=>
$score
,
-id
=>
$edge_id
),
);
}
$graph
->{
'_id_map'
} = \
%seen_nodes
;
return
$graph
;
}
sub
write_network {
my
(
$self
,
$gr
) =
@_
;
if
(!
$gr
|| !
$gr
->isa(
'Bio::Graph::ProteinGraph'
)) {
$self
->throw(
"I need a Bio::Graph::ProteinGraph, not a ["
.
ref
(
$gr
) .
"]"
);
}
my
@edges
=
$gr
->edges();
for
my
$edge
(
@edges
) {
my
$str
=
"DIP:"
.
$edge
->object_id().
"\t"
;
my
@nodes
=
$edge
->nodes();
for
my
$n
(
@nodes
){
my
%ids
=
$gr
->_get_ids_by_db(
$n
);
for
my
$db
(
qw(DIP SWP PIR GI Ref-Seq RefSeq psixml ens)
){
if
(
exists
(
$ids
{
$db
})){
$str
.=
"$db:$ids{$db}\t"
;
}
else
{
$str
.=
"\t"
;
}
}
}
$str
=~ s/\t$//;
if
(
defined
(
$edge
->weight)) {
$str
.=
"\t"
.
$edge
->weight.
"\n"
;
}
else
{
$str
.=
"\n"
;
}
$self
->_print(
$str
);
}
$self
->flush();
}
sub
_add_db_links {
my
(
$self
,
$acc
,
$s1
,
$p1
,
$n1
,
$g1
) =
@_
;
my
%ids
;
$ids
{
'PIR'
} =
$p1
if
$p1
;
$ids
{
'SWP'
} =
$s1
if
$s1
;
$ids
{
'DIP'
} =
$n1
if
$n1
;
$ids
{
'GI'
} =
$g1
if
$g1
;
my
$ac
= Bio::Annotation::Collection->new();
for
my
$db
(
keys
%ids
) {
my
$an
= Bio::Annotation::DBLink->new(
-database
=>
$db
,
-primary_id
=>
$ids
{
$db
},
);
$ac
->add_Annotation(
'dblink'
,
$an
);
}
return
$ac
;
}
1;