| 1 | package Bio::Gonzales::GO::Util; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | use Graph; | ||
| 7 | use Graph::Directed; | ||
| 8 | use GO::Model::Graph; | ||
| 9 | use GO::Model::Relationship; | ||
| 10 | use GO::Model::GraphIterator; | ||
| 11 | use GO::Model::Term; | ||
| 12 | use Data::Dumper; | ||
| 13 | use List::MoreUtils qw/uniq/; | ||
| 14 | |||
| 15 | use GO::Parser; | ||
| 16 | |||
| 17 | use 5.010; | ||
| 18 | |||
| 19 | use base 'Exporter'; | ||
| 20 | our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); | ||
| 21 | our $VERSION = '0.058'; # VERSION | ||
| 22 | |||
| 23 | @EXPORT = qw(); | ||
| 24 | %EXPORT_TAGS = (); | ||
| 25 | @EXPORT_OK = qw(subgraph_leaves as_2d_lookup get_recursive_related_terms_by_types); | ||
| 26 | |||
| 27 | sub as_2d_lookup { | ||
| 28 | my $data = shift; | ||
| 29 | |||
| 30 | my %lookup; | ||
| 31 | while ( my ( $key, $elements ) = each %$data ) { | ||
| 32 | $lookup{$key} = { map { $_ => 1 } @$elements }; | ||
| 33 | } | ||
| 34 | |||
| 35 | return \%lookup; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub subgraph_leaves { | ||
| 39 | my ( $go_child_lookup, $go_terms ) = @_; | ||
| 40 | |||
| 41 | #for g in all go terms from the protein | ||
| 42 | # for h in all go terms from the protein | ||
| 43 | # if g == h then next | ||
| 44 | # if g is in childs(h) then h is no leaf | ||
| 45 | |||
| 46 | my @go_uniq = uniq @$go_terms; | ||
| 47 | my %is_leaf = map { $_ => 1 } @go_uniq; | ||
| 48 | for ( my $i = 0; $i < @go_uniq; $i++ ) { | ||
| 49 | my $g = $go_uniq[$i]; | ||
| 50 | for ( my $j = 0; $j < @go_uniq; $j++ ) { | ||
| 51 | next if ( $i == $j ); | ||
| 52 | my $h = $go_uniq[$j]; | ||
| 53 | |||
| 54 | next unless ( $is_leaf{$h} ); | ||
| 55 | |||
| 56 | if ( $go_child_lookup->{$h}{$g} ) { | ||
| 57 | $is_leaf{$h} = 0; | ||
| 58 | } | ||
| 59 | |||
| 60 | } | ||
| 61 | } | ||
| 62 | return [ grep { $is_leaf{$_} } keys %is_leaf ]; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub get_recursive_related_terms_by_types { | ||
| 66 | my ( $self, $relkind, $acc, $types, $refl ) = @_; | ||
| 67 | my %type_lookup = map { $_ => 1 } @$types; | ||
| 68 | |||
| 69 | # if a term object is specified instead of ascc no, use the acc no | ||
| 70 | if ( ref($acc) && $acc->isa("GO::Model::Term") ) { | ||
| 71 | $acc = $acc->acc; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $rels | ||
| 75 | = ( $relkind eq "child" ) | ||
| 76 | ? $self->get_child_relationships($acc) | ||
| 77 | : $self->get_parent_relationships($acc); | ||
| 78 | |||
| 79 | if ($types) { | ||
| 80 | @$rels = grep { $type_lookup{$_->type} } @$rels; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $relmethod = $relkind . "_acc"; | ||
| 84 | |||
| 85 | my @pterms = map { | ||
| 86 | my $term = $self->get_term( $_->$relmethod() ); | ||
| 87 | my $rps = get_recursive_related_terms_by_types($self, $relkind, $_->$relmethod(), $types ); | ||
| 88 | ( $term, @$rps ); | ||
| 89 | } @$rels; | ||
| 90 | if ($refl) { | ||
| 91 | @pterms = ( $self->get_term($acc), @pterms ); | ||
| 92 | } | ||
| 93 | return \@pterms; | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; |