| 1 | package TFBS::Tools::SetOperationsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/; |
| 2 | use strictscip-perl cpan TFBS v0.5.0 strict/; |
| 3 | |
| 4 | use Bio::Root::Rootscip-perl cpan TFBS v0.5.0 Bio/Root/Root/; |
| 5 | use varsscip-perl cpan TFBS v0.5.0 vars/ qw'@ISA'; |
| 6 | @ISAscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@ISA`. = qw'Bio::Root::Root'; |
| 7 | |
| 8 | |
| 9 | sub newscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/new(). { |
| 10 | my ($caller, @args) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 11 | my $self = blessscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/bless(). {}, refscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/ref(). $caller || $caller; |
| 12 | my ($index_by, $strict, $output_type, $pairs) = |
| 13 | $self->_rearrangescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_rearrange().([qw'INDEX_BY STRICT OUTPUT_TYPE PAIRS'], @args); |
| 14 | $self->index_byscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/index_by().($index_by); |
| 15 | $self->strictscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/strict().($strict); |
| 16 | $self->output_typescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/output_type().($output_type); |
| 17 | $self->pairsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/pairs().($pairs); |
| 18 | return $self; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | sub unionscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/union(). { |
| 23 | my ($self, @sets) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 24 | my %union_index = |
| 25 | mapscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/map(). {$self->_indexscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index().($_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/$_.)} $self->_sets_to_arrayrefsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_sets_to_arrayrefs().(@sets); |
| 26 | $self->_outputscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_output().(\%union_index); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | sub intersectionscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/intersection(). { |
| 31 | my ($self, @sets) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 32 | |
| 33 | my @set_arrayrefs = $self->_sets_to_arrayrefsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_sets_to_arrayrefs().(@sets); |
| 34 | #this would be faster, but we might want to retain the exact objects |
| 35 | # that were present in |
| 36 | #my @set_arrayrefs = sort {@$a <=> @$b} $self->_sets_to_arrayrefs(@sets); |
| 37 | |
| 38 | my %intersection_index = $self->_indexscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index().(shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift(). @set_arrayrefs); |
| 39 | foreach my $set_arrayref (@set_arrayrefs) { |
| 40 | my %curr_set_index = $self->_indexscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index().($set_arrayref); |
| 41 | my @help_array = %curr_set_index; |
| 42 | |
| 43 | foreach my $key (keysscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/keys(). %intersection_index) { |
| 44 | |
| 45 | if (!existsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/exists(). $curr_set_index{$key}) { |
| 46 | deletescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/delete(). $intersection_index{$key} ; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | $self->_outputscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_output().(\%intersection_index); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | sub differencescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/difference(). { |
| 55 | # pairs only for now |
| 56 | my ($self, @sets) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 57 | my ($set1, $set2) = $self->_sets_to_arrayrefsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_sets_to_arrayrefs().(@sets); |
| 58 | if (!definedscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/defined(). $set2) { |
| 59 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/throw(). ("'difference' needs exactly two sets as arguments"); |
| 60 | } |
| 61 | my %diff_index1 = $self->_indexscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index().($set1); |
| 62 | my %diff_index2 = $self->_indexscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index().($set2); |
| 63 | foreach my $key (keysscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/keys(). %diff_index1) { |
| 64 | if (existsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/exists(). $diff_index2{$key}) { |
| 65 | deletescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/delete(). $diff_index1{$key}; |
| 66 | deletescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/delete(). $diff_index2{$key}; |
| 67 | } |
| 68 | } |
| 69 | wantarrayscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/wantarray(). ? ($self->_outputscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_output().(\%diff_index1), $self->_outputscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_output().(\%diff_index2)) |
| 70 | : $self->_outputscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_output().(\%diff_index1); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | sub index_byscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/index_by(). { |
| 75 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift().; |
| 76 | |
| 77 | # By default, we are dealing with Bio::SeqFeatureI objects |
| 78 | my @DEFAULTS = qw(primary_tag source_tag start end score strand); |
| 79 | if (@_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.) { |
| 80 | if(!definedscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/defined(). $_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.[0]) { |
| 81 | $self->{_index_by} = \@DEFAULTS; |
| 82 | } |
| 83 | elsif (refscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/ref().($_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.[0]) eq "ARRAY") { |
| 84 | $self->{_index_by} = $_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.[0]; |
| 85 | } |
| 86 | else { |
| 87 | $self->{_index_by} = [@_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.]; |
| 88 | } |
| 89 | } |
| 90 | return @{$self->{_index_by}}; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | sub strictscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/strict(). { |
| 95 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift().; |
| 96 | if (@_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.) { |
| 97 | if ($self->{_strict} = shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift().) { |
| 98 | $self->{_index_fn} = \&_index_strictscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index_strict().; |
| 99 | } |
| 100 | else { |
| 101 | $self->{_index_fn} = \&_index_by_annotationscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index_by_annotation().; |
| 102 | } |
| 103 | } |
| 104 | return $self->{_strict}; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | sub output_typescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/output_type(). { |
| 109 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift().; |
| 110 | if (@_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.) { |
| 111 | unless ($self->{_output_type} = shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift().) { |
| 112 | $self->{_output_type} = "arrayref" |
| 113 | } |
| 114 | } |
| 115 | return $self->{_output_type}; |
| 116 | } |
| 117 | |
| 118 | sub pairsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/pairs(). { |
| 119 | my $self = shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift().; |
| 120 | if (@_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.) { |
| 121 | if ($self->{_pairs} = shiftscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/shift(). and !$self->strictscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/strict().) { |
| 122 | $self->{_index_fn} = \&_index_by_pair_annotationscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index_by_pair_annotation().; |
| 123 | } |
| 124 | } |
| 125 | return $self->{_pairs}; |
| 126 | } |
| 127 | |
| 128 | sub _indexscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index(). { |
| 129 | my ($self) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 130 | $self->{_index_fn}->(scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`->()`().@_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.)scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`->()`().; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | sub _index_strictscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index_strict(). { |
| 135 | my ($self, $set_arrayref) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 136 | my %index_hash = (mapscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/map(). {$_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/$_., $_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/$_.} @$set_arrayref); |
| 137 | return %index_hash; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | sub _index_by_pair_annotationscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index_by_pair_annotation(). { |
| 142 | my ($self, $set_arrayref) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 143 | my %index_hash; |
| 144 | foreach my $member (@$set_arrayref) { |
| 145 | my @index_elements = ($self->_get_index_elementsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_get_index_elements().($member->feature1scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/feature1().), |
| 146 | $self->_get_index_elementsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_get_index_elements().($member->feature2scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/feature2().)); |
| 147 | $index_hash{joinscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/join().("::", @index_elements)} = $member; |
| 148 | } |
| 149 | return %index_hash; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | sub _index_by_annotationscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_index_by_annotation(). { |
| 154 | my ($self, $set_arrayref) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 155 | my %index_hash; |
| 156 | foreach my $member (@$set_arrayref) { |
| 157 | my @index_elements = $self->_get_index_elementsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_get_index_elements().($member); |
| 158 | $index_hash{joinscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/join().("::", @index_elements)} = $member; |
| 159 | } |
| 160 | return %index_hash; |
| 161 | } |
| 162 | |
| 163 | sub _get_index_elementsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_get_index_elements(). { |
| 164 | my ($self, $set_member) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 165 | my @index_elements; |
| 166 | foreach my $method ($self->index_byscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/index_by().) { |
| 167 | if (refscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/ref().($method) eq 'CODE') { |
| 168 | pushscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/push(). @index_elements, $method->(scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`->()`().$set_member)scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`->()`().; |
| 169 | } |
| 170 | else { |
| 171 | eval { pushscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/push(). @index_elements, $set_member->$methodscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/$method().; }; |
| 172 | if ($@scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`$@`.) { |
| 173 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/throw().(sprintfscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/sprintf().("Could not use '%s' for indexing a %s object. The original error was:\n", |
| 174 | $method, refscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/ref().($set_member)).$@scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`$@`.) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return @index_elements; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | sub _sets_to_arrayrefsscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_sets_to_arrayrefs(). { |
| 183 | my ($self, @sets) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 184 | my @set_arrayrefs; |
| 185 | foreach my $set (@sets) { |
| 186 | if (refscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/ref().($set) eq "ARRAY") { |
| 187 | pushscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/push(). @set_arrayrefs, $set; |
| 188 | } |
| 189 | elsif(refscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/ref().($set) and $set->canscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/can().("Iterator")) { |
| 190 | my @set_elements; |
| 191 | my $it = $set->Iteratorscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/Iterator().; |
| 192 | while (my $set_el = $it->nextscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/next().) { pushscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/push(). @set_elements, $set_el } |
| 193 | pushscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/push(). @set_arrayrefs, \@set_elements; |
| 194 | } |
| 195 | else { |
| 196 | $self->throw("Set must be an aray reference or have an ". |
| 197 | "Iterator method. Got ".(ref($set or $set)). "instead."); |
| 198 | } |
| 199 | } |
| 200 | return @set_arrayrefs; |
| 201 | } |
| 202 | |
| 203 | sub _outputscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/_output(). { |
| 204 | my ($self, $hashref) = @_scip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/`@_`.; |
| 205 | if ($self->output_typescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/output_type(). eq "arrayref") { |
| 206 | return [valuesscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/values(). %$hashref]; |
| 207 | } |
| 208 | elsif ($self->output_typescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/output_type(). eq "array") { |
| 209 | return %$hashref; |
| 210 | } |
| 211 | elsif ($self->output_typescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/output_type(). eq "matrix_set") { |
| 212 | my $setobj = TFBS::MatrixSet->newscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/new().; |
| 213 | $setobj->add_Matrixscip-perl cpan TFBS v0.5.0 TFBS/MatrixSet/add_Matrix().(valuesscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/values(). %$hashref); |
| 214 | return $setobj; |
| 215 | } |
| 216 | elsif ($self->output_typescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/output_type(). eq "site_set") { |
| 217 | my $setobj = TFBS::SiteSet->newscip-perl cpan TFBS v0.5.0 TFBS/SiteSet/new().; |
| 218 | $setobj->add_sitescip-perl cpan TFBS v0.5.0 TFBS/SiteSet/add_site().(valuesscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/values(). %$hashref); |
| 219 | return $setobj; |
| 220 | } |
| 221 | else { |
| 222 | $self->throwscip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/throw().($self->output_typescip-perl cpan TFBS v0.5.0 TFBS/Tools/SetOperations/output_type().." is not a supported output type"); |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | 1; |