use
Carp
qw(confess cluck)
;
@ISA
=
qw(GO::Model::Root Exporter)
;
sub
_valid_params {
return
qw(id code seq_acc xref seq_xref_list pub_xref_list)
;
}
sub
seq_acc {
my
$self
=
shift
;
if
(
@_
) {
my
$acc
=
shift
;
if
(
ref
(
$acc
)) {
if
(
ref
(
$acc
) eq
"ARRAY"
) {
foreach
(
@$acc
) {
$self
->add_seq_xref(
$_
);
}
}
else
{
if
(UNIVERSAL::isa(
$acc
,
"GO::Model::Xref"
)) {
$self
->add_seq_xref(
$acc
)
}
else
{
confess(
"$acc is not a valid argument for $self -> seq_acc()"
);
}
}
}
else
{
my
@accs
=
split
(/\;/,
$acc
);
foreach
my
$acc
(
@accs
) {
$self
->add_seq_xref(
$acc
);
}
}
}
return
join
(
"; "
,
map
{
$_
->as_str} @{
$self
->seq_xref_list || []});
}
sub
add_seq_xref {
my
$self
=
shift
;
my
$xref
=
shift
;
if
(
ref
(
$xref
)) {
if
(UNIVERSAL::isa(
$xref
,
"GO::Model::Xref"
)) {
$self
->{seq_xref_list} = []
unless
$self
->{seq_xref_list};
push
(@{
$self
->{seq_xref_list}},
$xref
);
}
else
{
confess(
"$xref is not a valid argument for $self -> add_seq_xref()"
);
}
}
else
{
if
(
$xref
=~ /\s*(\S+?):(\S+)/) {
my
(
$db
,
$acc
) = ($1, $2);
$acc
=~ s/ *$//;
$xref
=
GO::Model::Xref->new({
xref_dbname
=>
$db
,
xref_key
=>
$acc
});
}
else
{
$xref
=
GO::Model::Xref->new({
xref_dbname
=>
"UNKNOWN"
,
xref_key
=>
"$xref"
});
}
confess(
"Assertion error"
)
unless
$xref
->isa(
"GO::Model::Xref"
);
$self
->add_seq_xref(
$xref
);
}
}
sub
add_pub_xref {
my
$self
=
shift
;
my
$xref
=
shift
;
if
(
ref
(
$xref
)) {
if
(UNIVERSAL::isa(
$xref
,
"GO::Model::Xref"
)) {
$self
->{pub_xref_list} = []
unless
$self
->{pub_xref_list};
push
(@{
$self
->{pub_xref_list}},
$xref
);
}
else
{
confess(
"$xref is not a valid argument for $self -> add_pub_xref()"
);
}
}
else
{
if
(
$xref
=~ /\s*(\S+?):(\S+)/) {
my
(
$db
,
$acc
) = ($1, $2);
$acc
=~ s/ *$//;
$xref
=
GO::Model::Xref->new({
xref_dbname
=>
$db
,
xref_key
=>
$acc
});
}
else
{
$xref
=
GO::Model::Xref->new({
xref_dbname
=>
"UNKNOWN"
,
xref_key
=>
"$xref"
});
}
confess(
"Assertion error"
)
unless
$xref
->isa(
"GO::Model::Xref"
);
$self
->add_pub_xref(
$xref
);
}
}
sub
xref_list {
my
$self
=
shift
;
if
(
@_
) {
confess(
"get only"
);
}
my
@x
= @{
$self
->pub_xref_list || []};
push
(
@x
, @{
$self
->seq_xref_list || []});
return
\
@x
;
}
sub
xref {
my
$self
=
shift
;
if
(
@_
) {
$self
->pub_xref_list([
@_
]);
}
$self
->pub_xref_list &&
$self
->pub_xref_list->[0];
}
sub
valid_codes {
qw(IMP IGI IPI ISS IDA IEP IEA TAS NAS ND NR)
;
}
sub
_initialize
{
my
$self
=
shift
;
my
$paramh
=
shift
;
if
(!
ref
(
$paramh
)) {
confess(
"init param must be hash"
);
}
if
(
$paramh
->{reference}) {
my
(
$db
,
@keyparts
) =
split
(/:/,
$paramh
->{reference});
my
$key
=
join
(
":"
,
@keyparts
);
if
(!
$key
) {
$key
=
$db
;
$db
=
"U"
;
}
else
{
(
$db
) =~
tr
/A-Z/a-z/;
}
my
$xref
=
GO::Model::Xref->new({
xref_key
=>
$key
,
xref_dbname
=>
$db
});
$self
->xref(
$xref
);
delete
$paramh
->{reference};
}
$self
->SUPER::_initialize(
$paramh
);
}
sub
to_idl_struct {
my
$self
=
shift
;
if
(!
$self
->xref) {
confess(
"$self has no xref"
);
}
return
{
code
=>
$self
->code,
seq_acc
=>
$self
->seq_acc,
dbxref
=>
$self
->xref->to_idl_struct,
};
}
sub
from_idl {
my
$class
=
shift
;
my
$h
=
shift
;
$h
->{dbxref} = GO::Model::Xref->from_idl(
$h
->{dbxref});
return
$class
->new(
$h
);
}
sub
graphify {
my
$self
=
shift
;
my
(
$ref
,
$subg
,
$opts
) =
rearrange([
qw(ref graph opts)
],
@_
);
$opts
= {}
unless
$opts
;
$subg
=
$self
->apph->create_graph_obj
unless
$subg
;
my
$acc
=
sprintf
(
"%s"
,
$self
);
my
$t
=
$self
->apph->create_term_obj({
name
=>
$acc
,
acc
=>
$acc
});
$subg
->add_node(
$t
);
$subg
->add_arc(
$t
,
$ref
,
"hasEvidence"
)
if
$ref
;
foreach
my
$xr
(@{
$self
->xref_list || []}) {
$xr
->apph(
$self
->apph);
$xr
->graphify(
$t
,
$subg
);
}
my
$code
=
$self
->code;
my
$cn
=
$subg
->get_node(
$code
);
if
(!
$cn
) {
$cn
=
$self
->apph->create_term_obj({
name
=>
$code
,
acc
=>
$code
});
$subg
->add_node(
$cn
);
}
$subg
->add_arc(
$cn
,
$t
,
"hasCode"
);
$subg
;
}
1;