| 1 | # TFBS module for TFBS::SiteSet |
| 2 | # |
| 3 | # Copyright Boris Lenhard |
| 4 | # |
| 5 | # You may distribute this module under the same terms as perl itself |
| 6 | # |
| 7 | |
| 8 | # POD |
| 9 | |
| 10 | =head1 NAME |
| 11 | |
| 12 | TFBS::SiteSet - a set of TFBS::Site objects |
| 13 | |
| 14 | =head1 SYNOPSIS |
| 15 | |
| 16 | my $site_set = TFBS::SiteSet->new(@list_of_site_objects); |
| 17 | |
| 18 | # add a TFBS::Site object to set: |
| 19 | |
| 20 | $site_set->add_site($site_obj); |
| 21 | |
| 22 | # append another TFBS::SiteSet contents: |
| 23 | |
| 24 | $site_pair_set->add_site_set($site_obj); |
| 25 | |
| 26 | # create an iterator: |
| 27 | |
| 28 | my $it = $site_set->Iterator(-sort_by => 'start'); |
| 29 | |
| 30 | |
| 31 | |
| 32 | =head1 DESCRIPTION |
| 33 | |
| 34 | TFBS::SiteSet is an aggregate class that contains a collection |
| 35 | of TFBS::Site objects. It can be created anew and filled with |
| 36 | TFBS::Site object. It is also returned by search_seq() method call |
| 37 | of some TFBS::PatternI subclasses (e.g. TFBS::Matrix::PWM). |
| 38 | |
| 39 | =head1 FEEDBACK |
| 40 | |
| 41 | Please send bug reports and other comments to the author. |
| 42 | |
| 43 | =head1 AUTHOR - Boris Lenhard |
| 44 | |
| 45 | Boris Lenhard E<lt>[email protected]<gt> |
| 46 | |
| 47 | =head1 APPENDIX |
| 48 | |
| 49 | The rest of the documentation details each of the object |
| 50 | methods. Internal methods are preceded with an underscore. |
| 51 | |
| 52 | =cut |
| 53 | |
| 54 | |
| 55 | |
| 56 | # The code begins HERE: |
| 57 | |
| 58 | |
| 59 | package TFBS::SiteSet; |
| 60 | |
| 61 | use vars qw(@ISA $AUTOLOAD); |
| 62 | use TFBS::Site; |
| 63 | use TFBS::_Iterator::_SiteSetIterator; |
| 64 | use strict; |
| 65 | @ISA = qw(Bio::Root::Root); |
| 66 | |
| 67 | sub new { |
| 68 | my ($class, @data) = @_; |
| 69 | my $self = bless {}, ref($class) || $class; |
| 70 | $self->{_site_array_ref} = []; |
| 71 | @data = @{$class->{site_list}} if !@data && ref($class); |
| 72 | $self->add_site(@data); |
| 73 | return $self; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | =head2 add_site |
| 79 | |
| 80 | Title : add_site |
| 81 | Usage : $siteset->add_site($site_object) |
| 82 | $siteset->add_site(@list_of_site_objects) |
| 83 | Function: adds TFBS::Site objects to an existing TFBS::SiteSet object |
| 84 | Returns : $sitepair object (usually ignored) |
| 85 | Args : A list of TFBS::Site objects to add |
| 86 | |
| 87 | =cut |
| 88 | |
| 89 | |
| 90 | |
| 91 | sub add_site { |
| 92 | my ($self, @site_list) = @_; |
| 93 | foreach my $site (@site_list) { |
| 94 | ref($site) =~ /TFBS::Site*/ |
| 95 | or $self->throw("Attempted to add an element ". |
| 96 | "of a wrong type."); |
| 97 | push @{$self->{_site_array_ref}}, $site; |
| 98 | } |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | =head2 add_site_set |
| 104 | |
| 105 | Title : add_site_set |
| 106 | Usage : $siteset->add_site_set($site_set_object) |
| 107 | $siteset->add_site(@list_of_site_set_objects) |
| 108 | Function: adds the contents of other TFBS::SiteSet objects |
| 109 | to an existing TFBS::SiteSet object |
| 110 | Returns : $siteset object (usually ignored) |
| 111 | Args : A list of TFBS::SiteSet objects whose contents should be |
| 112 | added to $siteset |
| 113 | |
| 114 | =cut |
| 115 | |
| 116 | |
| 117 | sub add_siteset { |
| 118 | my ($self, @sitesets) = @_; |
| 119 | foreach my $siteset (@sitesets) { |
| 120 | ref($siteset) =~ /TFBS::Site.*Set/ |
| 121 | or $self->throw("Attempted to add an element ". |
| 122 | "that is not a TFBS::SiteSet object."); |
| 123 | push @{$self->{_site_array_ref}}, |
| 124 | @{ $siteset->{_site_array_ref} }; |
| 125 | } |
| 126 | return $self; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | =head2 size |
| 131 | |
| 132 | Title : size |
| 133 | Usage : my $size = $siteset->size() |
| 134 | Function: returns a number of TFBS::Site objects contained in the set |
| 135 | Returns : a scalar (integer) |
| 136 | Args : none |
| 137 | |
| 138 | =cut |
| 139 | |
| 140 | |
| 141 | |
| 142 | sub size { |
| 143 | scalar @{ $_[0]->{_site_array_ref} }; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | =head2 Iterator |
| 149 | |
| 150 | Title : Iterator |
| 151 | Usage : my $siteset_iterator = |
| 152 | $siteset->Iterator(-sort_by =>'start'); |
| 153 | while (my $site = $siteset_iterator->next) { |
| 154 | # do whatever you want with individual matrix objects |
| 155 | } |
| 156 | Function: Returns an iterator object that can be used to go through |
| 157 | all members of the set (TFBS::Site objects) |
| 158 | Returns : an iterator object (currently undocumentened in TFBS - |
| 159 | but understands the 'next' method) |
| 160 | Args : -sort_by # optional - currently it accepts |
| 161 | # (default sort order in parenthetse) |
| 162 | # 'name' (pattern name, alphabetically) |
| 163 | # 'ID' (pattern/matrix ID, alphabetically) |
| 164 | # 'start' (site start in sequence, |
| 165 | # numerically,increasing order) |
| 166 | # 'end' (site end in sequence, |
| 167 | # numerically, increasing order) |
| 168 | # 'score' (numerically, decreasing order) |
| 169 | |
| 170 | -reverse # optional - reverses the default sorting order if true |
| 171 | |
| 172 | =cut |
| 173 | |
| 174 | |
| 175 | |
| 176 | sub Iterator { |
| 177 | |
| 178 | my ($self, %args) = @_; |
| 179 | return TFBS::_Iterator::_SiteSetIterator->new($self->{_site_array_ref}, |
| 180 | $args{'-sort_by'}, |
| 181 | $args{'-reverse'} |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | sub all_sites { |
| 187 | my ($self,%args) = @_; |
| 188 | return @{$self->{_site_array_ref}} if @{$self->{_site_array_ref}}; |
| 189 | |
| 190 | } |
| 191 | |
| 192 | |
| 193 | =head2 GFF |
| 194 | |
| 195 | Title : GFF |
| 196 | Usage : print $siteset->GFF(); |
| 197 | : print $siteset->GFF($gff_formatter) |
| 198 | Function: returns a "standard" multiline GFF string |
| 199 | Returns : a string (multiline, newline terminated) |
| 200 | Args : a $gff_formatter function reference (optional) |
| 201 | |
| 202 | =cut |
| 203 | |
| 204 | |
| 205 | sub GFF { |
| 206 | my ($self, %args) = @_; |
| 207 | my $site_iterator = $self->Iterator(-sort_by=>'start'); |
| 208 | my $gff_string = ""; |
| 209 | while (my $site = $site_iterator->next()) { |
| 210 | $gff_string .= $site->GFF(%args)."\n"; |
| 211 | } |
| 212 | |
| 213 | return $gff_string; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | ######################################################## |
| 219 | # OBSOLETE METHODS |
| 220 | ######################################################## |
| 221 | |
| 222 | sub old_GFF { |
| 223 | eval "require GFF::GeneFeatureSet;"; |
| 224 | if ($@) { print STDERR "Failed to load GFF modules, stopped"; return; } |
| 225 | my ($self) = @_; |
| 226 | my $site_iterator = $self->Iterator(-sort_by=>'start'); |
| 227 | my $GFFset = GFF::GeneFeatureSet->new(2); |
| 228 | while (my $site = $site_iterator->next()) { |
| 229 | $GFFset->addGeneFeature($site->GFF()); |
| 230 | } |
| 231 | return $GFFset; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | ############################################################## |
| 237 | # PRIVATE AND AUTOMATIC METHODS |
| 238 | ############################################################## |
| 239 | |
| 240 | |
| 241 | |
| 242 | sub AUTOLOAD { |
| 243 | my ($self) = @_; |
| 244 | my %discontinued = (sort => 1, |
| 245 | sort_by_name => 1, |
| 246 | sort_reversed => 1, |
| 247 | reverse => 1, |
| 248 | next_site => 1, |
| 249 | reset => 1 |
| 250 | ); |
| 251 | $AUTOLOAD =~ /.+::(\w+)/; |
| 252 | if ($discontinued{$1}) { |
| 253 | $self->_no_more($1); |
| 254 | } |
| 255 | else { |
| 256 | $self->throw("$1: no such method"); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | sub _no_more { |
| 261 | $_[0]->throw("Method '$_[1]' is no longer available in ". |
| 262 | ref($_[0]).". Use the 'Iterator' method instead."); |
| 263 | } |
| 264 | |
| 265 | 1; |
| 266 |