#!perl
my
@all_from_fields
= (
'id'
,
'direction'
,
'transproton'
,
'proton'
);
my
@all_rel_fields
= (
'from_link'
,
'to_link'
, );
my
@all_to_fields
= (
'id'
,
'mod_date'
,
'name'
,
'msid'
,
'abbr'
,
'equation'
,
'reversibility'
);
my
%all_from_fields
=
map
{
$_
=> 1 }
@all_from_fields
;
my
%all_rel_fields
=
map
{
$_
=> 1 }
@all_rel_fields
;
my
%all_to_fields
=
map
{
$_
=> 1 }
@all_to_fields
;
my
@default_fields
= (
'from-link'
,
'to-link'
);
my
@from_fields
;
my
@rel_fields
;
my
@to_fields
;
my
$usage
=
"usage: get_relationship_Requires [-c column] [-a | -from field list -rel field list -to field list] < ids > extended.by.a.column(s)\n"
;
my
$column
;
my
$input_file
;
my
$a
;
my
$f
;
my
$r
;
my
$t
;
my
$h
;
my
$i
=
"-"
;
my
$geO
= Bio::KBase::CDMI::CDMIClient->new_get_entity_for_script(
"c=i"
=> \
$column
,
"h"
=> \
$h
,
"show-fields"
=> \
$h
,
"a"
=> \
$a
,
"from=s"
=> \
$f
,
"rel=s"
=> \
$r
,
"to=s"
=> \
$t
,
'i=s'
=> \
$i
);
if
(
$h
) {
print
STDERR
"from: "
,
join
(
","
,
@all_from_fields
),
"\n"
;
print
STDERR
"relation: "
,
join
(
","
,
@all_rel_fields
),
"\n"
;
print
STDERR
"to: "
,
join
(
","
,
@all_to_fields
),
"\n"
;
exit
0;
}
if
(
$a
&& (
$f
||
$r
||
$t
)) {
die
$usage
};
if
(
$a
) {
@from_fields
=
@all_from_fields
;
@rel_fields
=
@all_rel_fields
;
@to_fields
=
@all_to_fields
;
}
elsif
(
$f
||
$t
||
$r
) {
my
$err
= 0;
if
(
$f
) {
@from_fields
=
split
(
","
,
$f
);
$err
+= check_fields(\
@from_fields
,
%all_from_fields
);
}
if
(
$r
) {
@rel_fields
=
split
(
","
,
$r
);
$err
+= check_fields(\
@rel_fields
,
%all_rel_fields
);
}
if
(
$t
) {
@to_fields
=
split
(
","
,
$t
);
$err
+= check_fields(\
@to_fields
,
%all_to_fields
);
}
if
(
$err
) {
exit
1;}
}
else
{
@rel_fields
=
@default_fields
;
}
my
$ih
;
if
(
$input_file
)
{
open
$ih
,
"<"
,
$input_file
or
die
"Cannot open input file $input_file: $!"
;
}
else
{
$ih
= \
*STDIN
;
}
while
(
my
@tuples
= Bio::KBase::Utilities::ScriptThing::GetBatch(
$ih
,
undef
,
$column
)) {
my
@h
=
map
{
$_
->[0] }
@tuples
;
my
$h
=
$geO
->get_relationship_Requires(\
@h
, \
@from_fields
, \
@rel_fields
, \
@to_fields
);
my
%results
;
for
my
$result
(
@$h
) {
my
@from
;
my
@rel
;
my
@to
;
my
$from_id
;
my
$res
=
$result
->[0];
for
my
$key
(
@from_fields
) {
push
(
@from
,
$res
->{
$key
});
}
$res
=
$result
->[1];
$from_id
=
$res
->{
'from_link'
};
for
my
$key
(
@rel_fields
) {
push
(
@rel
,
$res
->{
$key
});
}
$res
=
$result
->[2];
for
my
$key
(
@to_fields
) {
push
(
@to
,
$res
->{
$key
});
}
if
(
$from_id
) {
push
@{
$results
{
$from_id
}}, [
@from
,
@rel
,
@to
];
}
}
for
my
$tuple
(
@tuples
)
{
my
(
$id
,
$line
) =
@$tuple
;
my
$resultsForId
=
$results
{
$id
};
if
(
$resultsForId
) {
for
my
$result
(
@$resultsForId
) {
print
join
(
"\t"
,
$line
,
@$result
) .
"\n"
;
}
}
}
}
sub
check_fields {
my
(
$fields
,
%all_fields
) =
@_
;
my
@err
;
for
my
$field
(
@$fields
) {
if
(!
$all_fields
{
$field
})
{
push
(
@err
,
$field
);
}
}
if
(
@err
) {
my
@f
=
keys
%all_fields
;
print
STDERR
"get_relationship_Requires: unknown fields @err. Valid fields are @f\n"
;
return
1;
}
return
0;
}