Collection.pm 5.36 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
4
Copyright [2016-2018] EMBL-European Bioinformatics Institute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

Roy Storey's avatar
Roy Storey committed
20 21 22 23 24 25 26 27
=pod

=head1 NAME

Bio::EnsEMBL::Hive::Utils::Collection - A collection object

=cut

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package Bio::EnsEMBL::Hive::Utils::Collection;

use strict;
use warnings;


sub new {
    my $class = shift @_;

    my $self = bless {}, $class;

    $self->listref( shift @_ || [] );

    return $self;
}


sub listref {
    my $self = shift @_;

    if(@_) {
        $self->{'_listref'} = shift @_;
    }
    return $self->{'_listref'};
}


sub list {
    my $self = shift @_;

    return @{ $self->listref };
}


62 63 64 65 66 67 68
sub add {
    my $self = shift @_;

    push @{ $self->listref }, @_;
}


69
sub find_one_by {
70 71 72 73
    my ($self, %method_to_filter_value) = @_;

    ELEMENT: foreach my $element (@{ $self->listref }) {
        keys %method_to_filter_value;   # sic! This is to "rewind" the each% operator to the beginning each time
74 75 76
        while(my ($filter_name, $filter_value) = each %method_to_filter_value) {
            my $actual_value = (ref($element) eq 'HASH') ? $element->{$filter_name} : $element->$filter_name();
            next ELEMENT unless( defined($actual_value)   # either both defined and equal or neither defined
77 78 79 80
                                    ? defined($filter_value) && ( (ref($filter_value) eq 'CODE')
                                                                    ? &$filter_value( $actual_value )
                                                                    : ( $filter_value eq $actual_value )
                                                                )
81 82
                                    : !defined($filter_value)
                               );
83 84 85
        }
        return $element;
    }
86 87

    return undef;   # have to be explicit here to avoid surprises
88 89 90 91
}


sub find_all_by {
92
    my ($self, %method_to_filter_value) = @_;
93

94 95 96 97
    my @filtered_elements = ();

    ELEMENT: foreach my $element (@{ $self->listref }) {
        keys %method_to_filter_value;   # sic! This is to "rewind" the each% operator to the beginning each time
98 99 100
        while(my ($filter_name, $filter_value) = each %method_to_filter_value) {
            my $actual_value = (ref($element) eq 'HASH') ? $element->{$filter_name} : $element->$filter_name();
            next ELEMENT unless( defined($actual_value)   # either both defined and equal or neither defined
101 102 103 104
                                    ? defined($filter_value) && ( (ref($filter_value) eq 'CODE')
                                                                    ? &$filter_value( $actual_value )
                                                                    : ( $filter_value eq $actual_value )
                                                                )
105 106
                                    : !defined($filter_value)
                               );
107 108 109
        }
        push @filtered_elements, $element;
    }
110

111
    return \@filtered_elements;
112 113 114
}


115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
sub _find_all_by_subpattern {    # subpatterns can be combined into full patterns using +-,
    my ($self, $pattern) = @_;

    my $filtered_elements;

    if( $pattern=~/^\d+$/ ) {

        $filtered_elements = $self->find_all_by( 'dbID', $pattern );

    } elsif( $pattern=~/^(\d+)\.\.(\d+)$/ ) {

        $filtered_elements = $self->find_all_by( 'dbID', sub { return $1<=$_[0] && $_[0]<=$2; } );

    } elsif( $pattern=~/^(\d+)\.\.$/ ) {

        $filtered_elements = $self->find_all_by( 'dbID', sub { return $1<=$_[0]; } );

    } elsif( $pattern=~/^\.\.(\d+)$/ ) {

        $filtered_elements = $self->find_all_by( 'dbID', sub { return $_[0]<=$1; } );

    } elsif( $pattern=~/^\w+$/) {

        $filtered_elements = $self->find_all_by( 'name', $pattern );

    } elsif( $pattern=~/^[\w\%]+$/) {

        $pattern=~s/\%/.*/g;
        $filtered_elements = $self->find_all_by( 'name', sub { return $_[0]=~/^${pattern}$/; } );
    }

    return $filtered_elements;
}


sub find_all_by_pattern {
    my ($self, $pattern) = @_;

    if( not defined($pattern) ) {

        return [ $self->list ];

    } else {

        my @syll = split(/([+\-,])/, $pattern);

        my %uniq = map { ("$_" => $_) } @{ $self->_find_all_by_subpattern( shift @syll ) };   # initialize with the first syllable

        while(@syll) {
            my $operation   = shift @syll;
            my $subpattern  = shift @syll;

            foreach my $element (@{ $self->_find_all_by_subpattern( $subpattern ) }) {
                if($operation eq '-') {
                    delete $uniq{ "$element" };
                } elsif ($operation eq '+' or $operation eq ',') {
                    $uniq{ "$element" } = $element;
                } else {
                    throw( "Complex pattern '$pattern' not recognized" );
                }
            }
        }

        return [ values %uniq ];
    }
}


183 184 185 186 187 188 189
sub DESTROY {
    my $self = shift @_;

    $self->listref( [] );
}

1;