| 1 | package Bio::DB::SeqFeature::Store::memory; | ||
| 2 | $Bio::DB::SeqFeature::Store::memory::VERSION = '1.7.5'; | ||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Bio::DB::SeqFeature::Store::memory -- In-memory implementation of Bio::DB::SeqFeature::Store | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Bio::DB::SeqFeature::Store; | ||
| 10 | |||
| 11 | # Open the sequence database | ||
| 12 | my $db = Bio::DB::SeqFeature::Store->new( -adaptor => 'memory', | ||
| 13 | -dsn => '/var/databases/test'); | ||
| 14 | # search... by id | ||
| 15 | my @features = $db->fetch_many(@list_of_ids); | ||
| 16 | |||
| 17 | # ...by name | ||
| 18 | @features = $db->get_features_by_name('ZK909'); | ||
| 19 | |||
| 20 | # ...by alias | ||
| 21 | @features = $db->get_features_by_alias('sma-3'); | ||
| 22 | |||
| 23 | # ...by type | ||
| 24 | @features = $db->get_features_by_type('gene'); | ||
| 25 | |||
| 26 | # ...by location | ||
| 27 | @features = $db->get_features_by_location(-seq_id=>'Chr1',-start=>4000,-end=>600000); | ||
| 28 | |||
| 29 | # ...by attribute | ||
| 30 | @features = $db->get_features_by_attribute({description => 'protein kinase'}) | ||
| 31 | |||
| 32 | # ...by the GFF "Note" field | ||
| 33 | @result_list = $db->search_notes('kinase'); | ||
| 34 | |||
| 35 | # ...by arbitrary combinations of selectors | ||
| 36 | @features = $db->features(-name => $name, | ||
| 37 | -type => $types, | ||
| 38 | -seq_id => $seqid, | ||
| 39 | -start => $start, | ||
| 40 | -end => $end, | ||
| 41 | -attributes => $attributes); | ||
| 42 | |||
| 43 | # ...using an iterator | ||
| 44 | my $iterator = $db->get_seq_stream(-name => $name, | ||
| 45 | -type => $types, | ||
| 46 | -seq_id => $seqid, | ||
| 47 | -start => $start, | ||
| 48 | -end => $end, | ||
| 49 | -attributes => $attributes); | ||
| 50 | |||
| 51 | while (my $feature = $iterator->next_seq) { | ||
| 52 | # do something with the feature | ||
| 53 | } | ||
| 54 | |||
| 55 | # ...limiting the search to a particular region | ||
| 56 | my $segment = $db->segment('Chr1',5000=>6000); | ||
| 57 | my @features = $segment->features(-type=>['mRNA','match']); | ||
| 58 | |||
| 59 | # getting & storing sequence information | ||
| 60 | # Warning: this returns a string, and not a PrimarySeq object | ||
| 61 | $db->insert_sequence('Chr1','GATCCCCCGGGATTCCAAAA...'); | ||
| 62 | my $sequence = $db->fetch_sequence('Chr1',5000=>6000); | ||
| 63 | |||
| 64 | # what feature types are defined in the database? | ||
| 65 | my @types = $db->types; | ||
| 66 | |||
| 67 | # create a new feature in the database | ||
| 68 | my $feature = $db->new_feature(-primary_tag => 'mRNA', | ||
| 69 | -seq_id => 'chr3', | ||
| 70 | -start => 10000, | ||
| 71 | -end => 11000); | ||
| 72 | |||
| 73 | =head1 DESCRIPTION | ||
| 74 | |||
| 75 | Bio::DB::SeqFeature::Store::memory is the in-memory adaptor for | ||
| 76 | Bio::DB::SeqFeature::Store. You will not create it directly, but | ||
| 77 | instead use Bio::DB::SeqFeature::Store-E<gt>new() to do so. | ||
| 78 | |||
| 79 | See L<Bio::DB::SeqFeature::Store> for complete usage instructions. | ||
| 80 | |||
| 81 | =head2 Using the memory adaptor | ||
| 82 | |||
| 83 | Before using the memory adaptor, populate a readable-directory on the | ||
| 84 | file system with annotation and/or sequence files. The annotation | ||
| 85 | files must be in GFF3 format, and sholud end in the extension .gff or | ||
| 86 | .gff3. They may be compressed with "compress", "gzip" or "bzip2" (in | ||
| 87 | which case the appropriate compression extension must be present as | ||
| 88 | well.) | ||
| 89 | |||
| 90 | You may include sequence data inline in the GFF3 files, or put the | ||
| 91 | sequence data in one or more separate FASTA-format files. These files | ||
| 92 | must end with .fa or .fasta and may be compressed. Because of the way | ||
| 93 | the adaptor works, you will get much better performance if you keep | ||
| 94 | the sequence data in separate FASTA files. | ||
| 95 | |||
| 96 | Initialize the database using the -dsn option. This should point to | ||
| 97 | the directory creating the annotation and sequence files, or to a | ||
| 98 | single GFF3 file. Examples: | ||
| 99 | |||
| 100 | |||
| 101 | # load all GFF3 and FASTA files located in /var/databases/test directory | ||
| 102 | $db = Bio::DB::SeqFeature::Store->new( -adaptor => 'memory', | ||
| 103 | -dsn => '/var/databases/test'); | ||
| 104 | |||
| 105 | |||
| 106 | # load the data in a single compressed GFF3 file located at | ||
| 107 | # /usr/annotations/worm.gf33.gz | ||
| 108 | $db = Bio::DB::SeqFeature::Store->new( -adaptor => 'memory', | ||
| 109 | -dsn => '/usr/annotations/worm.gff3.gz'); | ||
| 110 | |||
| 111 | For compatibility with the Bio::DB::GFF memory adaptor, -gff is | ||
| 112 | recognized as an alias for -dsn. | ||
| 113 | |||
| 114 | See L<Bio::DB::SeqFeature::Store> for all the access methods supported | ||
| 115 | by this adaptor. The various methods for storing and updating features | ||
| 116 | and sequences into the database are supported, including GFF3 loading | ||
| 117 | support, but since this is an in-memory adaptor all changes you make | ||
| 118 | will be lost when the script exits. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 123 | 1 | ▶ | use base 'Bio::DB::SeqFeature::Store'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 124 | 0 | ▶ | use Bio::DB::SeqFeature::Store::GFF3Loader; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 125 | use Bio::DB::GFF::Typename; | ||
| 126 | use Bio::DB::GFF::Util::Rearrange 'rearrange'; | ||
| 127 | use File::Temp 'tempdir'; | ||
| 128 | use IO::File; | ||
| 129 | use Bio::DB::Fasta; | ||
| 130 | use File::Glob ':glob'; | ||
| 131 | |||
| 132 | use constant BINSIZE => 10_000; | ||
| 133 | |||
| 134 | ### | ||
| 135 | # object initialization | ||
| 136 | # | ||
| 137 | sub init { | ||
| 138 | my ($self, $args) = @_; | ||
| 139 | $self->SUPER::init($args); | ||
| 140 | $self->{_data} = {}; | ||
| 141 | $self->{_children} = {}; | ||
| 142 | $self->{_index} = {}; | ||
| 143 | $self; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub post_init { | ||
| 147 | my $self = shift; | ||
| 148 | my ($file_or_dir) = rearrange([['DIR','DSN','FILE','GFF']],@_); | ||
| 149 | return unless $file_or_dir; | ||
| 150 | |||
| 151 | my $loader = Bio::DB::SeqFeature::Store::GFF3Loader->new(-store => $self, | ||
| 152 | -sf_class => $self->seqfeature_class, | ||
| 153 | -no_close_fasta => 1 | ||
| 154 | ) or $self->throw("Couldn't create GFF3Loader"); | ||
| 155 | my @argv; | ||
| 156 | if (-d $file_or_dir) { | ||
| 157 | @argv = ( | ||
| 158 | bsd_glob("$file_or_dir/*.size*"), | ||
| 159 | bsd_glob("$file_or_dir/*.gff"), bsd_glob("$file_or_dir/*.gff3"), | ||
| 160 | bsd_glob("$file_or_dir/*.gff.{gz,Z,bz2}"), bsd_glob("$file_or_dir/*.gff3.{gz,Z,bz2}") | ||
| 161 | ); | ||
| 162 | } else { | ||
| 163 | @argv = $file_or_dir; | ||
| 164 | } | ||
| 165 | local $self->{file_or_dir} = $file_or_dir; | ||
| 166 | $loader->load(@argv); | ||
| 167 | warn $@ if $@; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub commit { # reindex fasta files | ||
| 171 | my $self = shift; | ||
| 172 | |||
| 173 | my $db; | ||
| 174 | if (my $fh = $self->{fasta_fh}) { | ||
| 175 | $fh->close; | ||
| 176 | $db = Bio::DB::Fasta->new($self->{fasta_file}); | ||
| 177 | } elsif (exists $self->{file_or_dir} && -d $self->{file_or_dir}) { | ||
| 178 | $db = Bio::DB::Fasta->new($self->{file_or_dir}); | ||
| 179 | } | ||
| 180 | $self->{fasta_db} = $db if $db; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub can_store_parentage { 1 } | ||
| 184 | |||
| 185 | # return a hash ref in which each key is primary id | ||
| 186 | sub data { | ||
| 187 | shift->{_data}; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub _init_database { shift->init } | ||
| 191 | |||
| 192 | sub _store { | ||
| 193 | my $self = shift; | ||
| 194 | my $indexed = shift; | ||
| 195 | my @objs = @_; | ||
| 196 | my $data = $self->data; | ||
| 197 | my $count = 0; | ||
| 198 | for my $obj (@objs) { | ||
| 199 | # Add unique ID to feature if needed | ||
| 200 | my $primary_id = $self->_autoid($obj); | ||
| 201 | # Store feature (overwriting any existing feature with the same primary ID | ||
| 202 | # as required by Bio::DB::SF::Store) | ||
| 203 | $data->{$primary_id} = $obj; | ||
| 204 | if ($indexed) { | ||
| 205 | $self->{_index}{ids}{$primary_id} = undef; | ||
| 206 | $self->_update_indexes($obj); | ||
| 207 | } | ||
| 208 | $count++; | ||
| 209 | } | ||
| 210 | return $count; | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub _autoid { | ||
| 215 | # If a feature has no ID, assign it a unique ID | ||
| 216 | my ($self, $obj) = @_; | ||
| 217 | my $data = $self->data; | ||
| 218 | my $primary_id = $obj->primary_id; | ||
| 219 | if (not defined $primary_id) { | ||
| 220 | # Create a unique ID | ||
| 221 | $primary_id = 1 + scalar keys %{$data}; | ||
| 222 | while (exists $data->{$primary_id}) { | ||
| 223 | $primary_id++; | ||
| 224 | } | ||
| 225 | $obj->primary_id($primary_id); | ||
| 226 | } | ||
| 227 | return $primary_id; | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | sub _deleteid { | ||
| 232 | my ($self, $id) = @_; | ||
| 233 | if (exists $self->{_index}{ids}{$id}) { | ||
| 234 | # $indexed was true | ||
| 235 | $self->_update_indexes( $self->fetch($id), 1 ); | ||
| 236 | delete $self->{_index}{ids}{$id}; | ||
| 237 | } | ||
| 238 | delete $self->data->{$id}; | ||
| 239 | return 1; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub _fetch { | ||
| 243 | my ($self, $id) = @_; | ||
| 244 | return $self->data->{$id}; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub _add_SeqFeature { | ||
| 248 | my ($self, $parent, @children) = @_; | ||
| 249 | my $count = 0; | ||
| 250 | my $parent_id = ref $parent ? $parent->primary_id : $parent; | ||
| 251 | defined $parent_id or $self->throw("Parent $parent should have a primary ID"); | ||
| 252 | for my $child (@children) { | ||
| 253 | my $child_id = ref $child ? $child->primary_id : $child; | ||
| 254 | defined $child_id or $self->throw("Child $child should have a primary ID"); | ||
| 255 | $self->{_children}{$parent_id}{$child_id}++; | ||
| 256 | $count++; | ||
| 257 | } | ||
| 258 | return $count; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub _fetch_SeqFeatures { | ||
| 262 | my ($self, $parent, @types) = @_; | ||
| 263 | my $parent_id = $parent->primary_id; | ||
| 264 | defined $parent_id or $self->throw("Parent $parent should have a primary ID"); | ||
| 265 | my @children_ids = keys %{$self->{_children}{$parent_id}}; | ||
| 266 | my @children = map {$self->fetch($_)} @children_ids; | ||
| 267 | |||
| 268 | if (@types) { | ||
| 269 | my $data; | ||
| 270 | for my $c (@children) { | ||
| 271 | push @{$$data{$c->primary_tag}{$c->source_tag||''}}, $c; | ||
| 272 | } | ||
| 273 | @children = (); | ||
| 274 | for my $type (@types) { | ||
| 275 | $type .= ':' if (not $type =~ m/:/); | ||
| 276 | my ($primary_tag, undef, $source_tag) = ($type =~ m/^(.*?)(:(.*?))$/); | ||
| 277 | $source_tag ||= ''; | ||
| 278 | if ($source_tag eq '') { | ||
| 279 | for my $source (keys %{$$data{$primary_tag}}) { | ||
| 280 | if (exists $$data{$primary_tag}{$source_tag}) { | ||
| 281 | push @children, @{$$data{$primary_tag}{$source_tag}}; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } else { | ||
| 285 | if (exists $$data{$primary_tag}{$source_tag}) { | ||
| 286 | push @children, @{$$data{$primary_tag}{$source_tag}}; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | return @children; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub _update_indexes { | ||
| 296 | my ($self, $obj, $del) = @_; | ||
| 297 | defined (my $id = $obj->primary_id) or return; | ||
| 298 | $del ||= 0; | ||
| 299 | $self->_update_name_index($obj,$id, $del); | ||
| 300 | $self->_update_type_index($obj,$id, $del); | ||
| 301 | $self->_update_location_index($obj, $id, $del); | ||
| 302 | $self->_update_attribute_index($obj,$id, $del); | ||
| 303 | } | ||
| 304 | |||
| 305 | sub _update_name_index { | ||
| 306 | my ($self, $obj, $id, $del) = @_; | ||
| 307 | my ($names, $aliases) = $self->feature_names($obj); | ||
| 308 | foreach (@$names) { | ||
| 309 | if (not $del) { | ||
| 310 | $self->{_index}{name}{lc $_}{$id} = 1; | ||
| 311 | } else { | ||
| 312 | delete $self->{_index}{name}{lc $_}{$id}; | ||
| 313 | if (scalar keys %{ $self->{_index}{name}{lc $_} } == 0) { | ||
| 314 | delete $self->{_index}{name}{lc $_}; | ||
| 315 | } | ||
| 316 | }; | ||
| 317 | } | ||
| 318 | foreach (@$aliases) { | ||
| 319 | if (not $del) { | ||
| 320 | $self->{_index}{name}{lc $_}{$id} ||= 2; | ||
| 321 | } else { | ||
| 322 | delete $self->{_index}{name}{lc $_}{$id}; | ||
| 323 | if (scalar keys %{ $self->{_index}{name}{lc $_} } == 0) { | ||
| 324 | delete $self->{_index}{name}{lc $_}; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | sub _update_type_index { | ||
| 331 | my ($self, $obj, $id, $del) = @_; | ||
| 332 | my $primary_tag = lc($obj->primary_tag) || return; | ||
| 333 | my $source_tag = lc($obj->source_tag || ''); | ||
| 334 | if (not $del) { | ||
| 335 | $self->{_index}{type}{$primary_tag}{$source_tag}{$id} = undef; | ||
| 336 | } else { | ||
| 337 | delete $self->{_index}{type}{$primary_tag}{$source_tag}{$id}; | ||
| 338 | if ( scalar keys %{$self->{_index}{type}{$primary_tag}{$source_tag}} == 0 ) { | ||
| 339 | delete $self->{_index}{type}{$primary_tag}{$source_tag}; | ||
| 340 | if (scalar keys %{$self->{_index}{type}{$primary_tag}} == 0 ) { | ||
| 341 | delete $self->{_index}{type}{$primary_tag}; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | sub _update_location_index { | ||
| 348 | my ($self, $obj, $id, $del) = @_; | ||
| 349 | my $seq_id = $obj->seq_id || ''; | ||
| 350 | my $start = $obj->start || 0; | ||
| 351 | my $end = $obj->end || 0; | ||
| 352 | my $strand = $obj->strand; | ||
| 353 | my $bin_min = int $start/BINSIZE; | ||
| 354 | my $bin_max = int $end/BINSIZE; | ||
| 355 | for (my $bin = $bin_min; $bin <= $bin_max; $bin++ ) { | ||
| 356 | if (not $del) { | ||
| 357 | $self->{_index}{location}{lc $seq_id}{$bin}{$id} = undef; | ||
| 358 | } else { | ||
| 359 | delete $self->{_index}{location}{lc $seq_id}{$bin}{$id}; | ||
| 360 | if (scalar keys %{$self->{_index}{location}{lc $seq_id}{$bin}{$id}} == 0) { | ||
| 361 | delete $self->{_index}{location}{lc $seq_id}{$bin}{$id}; | ||
| 362 | } | ||
| 363 | if (scalar keys %{$self->{_index}{location}{lc $seq_id}{$bin}} == 0) { | ||
| 364 | delete $self->{_index}{location}{lc $seq_id}{$bin}; | ||
| 365 | } | ||
| 366 | if (scalar keys %{$self->{_index}{location}{lc $seq_id}} == 0) { | ||
| 367 | delete $self->{_index}{location}{lc $seq_id}; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | sub _update_attribute_index { | ||
| 374 | my ($self, $obj, $id, $del) = @_; | ||
| 375 | for my $tag ($obj->get_all_tags) { | ||
| 376 | for my $value ($obj->get_tag_values($tag)) { | ||
| 377 | if (not $del) { | ||
| 378 | $self->{_index}{attribute}{lc $tag}{lc $value}{$id} = undef; | ||
| 379 | } else { | ||
| 380 | delete $self->{_index}{attribute}{lc $tag}{lc $value}{$id}; | ||
| 381 | if ( scalar keys %{$self->{_index}{attribute}{lc $tag}{lc $value}} == 0) { | ||
| 382 | delete $self->{_index}{attribute}{lc $tag}{lc $value}; | ||
| 383 | } | ||
| 384 | if ( scalar keys %{$self->{_index}{attribute}{lc $tag}} == 0) { | ||
| 385 | delete $self->{_index}{attribute}{lc $tag}; | ||
| 386 | } | ||
| 387 | if ( scalar keys %{$self->{_index}{attribute}} == 0) { | ||
| 388 | delete $self->{_index}{attribute}; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | } | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | sub _features { | ||
| 396 | my $self = shift; | ||
| 397 | my ($seq_id,$start,$end,$strand, | ||
| 398 | $name,$class,$allow_aliases, | ||
| 399 | $types, | ||
| 400 | $attributes, | ||
| 401 | $range_type, | ||
| 402 | $iterator | ||
| 403 | ) = rearrange([['SEQID','SEQ_ID','REF'],'START',['STOP','END'],'STRAND', | ||
| 404 | 'NAME','CLASS','ALIASES', | ||
| 405 | ['TYPES','TYPE','PRIMARY_TAG'], | ||
| 406 | ['ATTRIBUTES','ATTRIBUTE'], | ||
| 407 | 'RANGE_TYPE', | ||
| 408 | 'ITERATOR', | ||
| 409 | ],@_); | ||
| 410 | |||
| 411 | my (@from,@where,@args,@group); | ||
| 412 | $range_type ||= 'overlaps'; | ||
| 413 | |||
| 414 | my @result; | ||
| 415 | unless (defined $name or defined $seq_id or defined $types or defined $attributes) { | ||
| 416 | @result = keys %{$self->{_index}{ids}}; | ||
| 417 | } | ||
| 418 | |||
| 419 | my %found = (); | ||
| 420 | my $result = 1; | ||
| 421 | |||
| 422 | if (defined($name)) { | ||
| 423 | # hacky backward compatibility workaround | ||
| 424 | undef $class if $class && $class eq 'Sequence'; | ||
| 425 | $name = "$class:$name" if defined $class && length $class > 0; | ||
| 426 | $result &&= $self->filter_by_name($name,$allow_aliases,\%found); | ||
| 427 | } | ||
| 428 | |||
| 429 | if (defined $seq_id) { | ||
| 430 | $result &&= $self->filter_by_location($seq_id,$start,$end,$strand,$range_type,\%found); | ||
| 431 | } | ||
| 432 | |||
| 433 | if (defined $types) { | ||
| 434 | $result &&= $self->filter_by_type($types,\%found); | ||
| 435 | } | ||
| 436 | |||
| 437 | if (defined $attributes) { | ||
| 438 | $result &&= $self->filter_by_attribute($attributes,\%found); | ||
| 439 | } | ||
| 440 | |||
| 441 | push @result,keys %found if $result; | ||
| 442 | return $iterator ? Bio::DB::SeqFeature::Store::memory::Iterator->new($self,\@result) | ||
| 443 | : map {$self->fetch($_)} @result; | ||
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | sub filter_by_type { | ||
| 448 | my ($self, $types_req, $filter) = @_; | ||
| 449 | my @types_req = ref $types_req eq 'ARRAY' ? @$types_req : $types_req; | ||
| 450 | |||
| 451 | my $types = $self->{_index}{type}; | ||
| 452 | my @types_found = $self->find_types(\@types_req); | ||
| 453 | |||
| 454 | my @results; | ||
| 455 | for my $type_found (@types_found) { | ||
| 456 | my ($primary_tag, undef, $source_tag) = ($type_found =~ m/^(.*?)(:(.*?))$/); | ||
| 457 | next unless exists $types->{$primary_tag}{$source_tag}; | ||
| 458 | push @results, keys %{$types->{$primary_tag}{$source_tag}}; | ||
| 459 | } | ||
| 460 | |||
| 461 | $self->update_filter($filter,\@results); | ||
| 462 | } | ||
| 463 | |||
| 464 | sub find_types { | ||
| 465 | my ($self, $types_req) = @_; | ||
| 466 | my @types_found; | ||
| 467 | |||
| 468 | my $types = $self->{_index}{type}; | ||
| 469 | |||
| 470 | for my $type_req (@$types_req) { | ||
| 471 | |||
| 472 | # Type is the primary tag and an optional source tag | ||
| 473 | my ($primary_tag, $source_tag); | ||
| 474 | if (ref $type_req && $type_req->isa('Bio::DB::GFF::Typename')) { | ||
| 475 | $primary_tag = $type_req->method; | ||
| 476 | $source_tag = $type_req->source; | ||
| 477 | } else { | ||
| 478 | ($primary_tag, undef, $source_tag) = ($type_req =~ m/^(.*?)(:(.*))?$/); | ||
| 479 | } | ||
| 480 | ($primary_tag, $source_tag) = (lc $primary_tag, lc($source_tag || '')); | ||
| 481 | |||
| 482 | next if not exists $$types{$primary_tag}; | ||
| 483 | |||
| 484 | if ($source_tag eq '') { | ||
| 485 | # Match all sources for this primary_tag | ||
| 486 | push @types_found, map {"$primary_tag:$_"} (keys %{ $$types{$primary_tag} }); | ||
| 487 | } else { | ||
| 488 | # Match only the requested source | ||
| 489 | push @types_found, "$primary_tag:$source_tag"; | ||
| 490 | } | ||
| 491 | |||
| 492 | } | ||
| 493 | return @types_found; | ||
| 494 | } | ||
| 495 | |||
| 496 | sub attributes { | ||
| 497 | my $self = shift; | ||
| 498 | return keys %{$self->{_index}{attribute}}; | ||
| 499 | } | ||
| 500 | |||
| 501 | sub filter_by_attribute { | ||
| 502 | my ($self, $attributes, $filter) = @_; | ||
| 503 | |||
| 504 | my $index = $self->{_index}{attribute}; | ||
| 505 | my $result; | ||
| 506 | |||
| 507 | for my $att_name (keys %$attributes) { | ||
| 508 | my @result; | ||
| 509 | my @matching_values; | ||
| 510 | my @search_terms = ref($attributes->{$att_name}) && ref($attributes->{$att_name}) eq 'ARRAY' | ||
| 511 | ? @{$attributes->{$att_name}} : $attributes->{$att_name}; | ||
| 512 | my @regexp_terms; | ||
| 513 | my @terms; | ||
| 514 | |||
| 515 | for my $v (@search_terms) { | ||
| 516 | if (my $regexp = $self->glob_match($v)) { | ||
| 517 | @regexp_terms = keys %{$index->{lc $att_name}} unless @regexp_terms; | ||
| 518 | push @terms,grep {/^$v$/i} @regexp_terms; | ||
| 519 | } else { | ||
| 520 | push @terms,lc $v; | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | for my $v (@terms) { | ||
| 525 | push @result,keys %{$index->{lc $att_name}{$v}}; | ||
| 526 | } | ||
| 527 | |||
| 528 | $result ||= $self->update_filter($filter,\@result); | ||
| 529 | } | ||
| 530 | |||
| 531 | $result; | ||
| 532 | } | ||
| 533 | |||
| 534 | sub filter_by_location { | ||
| 535 | my ($self, $seq_id, $start, $end, $strand, $range_type, $filter) = @_; | ||
| 536 | $strand ||= 0; | ||
| 537 | |||
| 538 | my $index = $self->{_index}{location}{lc $seq_id}; | ||
| 539 | my @bins; | ||
| 540 | |||
| 541 | if (!defined $start or !defined $end or $range_type eq 'contained_in') { | ||
| 542 | @bins = sort {$a<=>$b} keys %{$index}; | ||
| 543 | $start = $bins[0] * BINSIZE unless defined $start; | ||
| 544 | $end = (($bins[-1] + 1) * BINSIZE) - 1 unless defined $end; | ||
| 545 | } | ||
| 546 | my %seenit; | ||
| 547 | my $bin_min = int $start/BINSIZE; | ||
| 548 | my $bin_max = int $end/BINSIZE; | ||
| 549 | my @bins_in_range = $range_type eq 'contained_in' ? ($bins[0]..$bin_min,$bin_max..$bins[-1]) | ||
| 550 | : ($bin_min..$bin_max); | ||
| 551 | |||
| 552 | my @results; | ||
| 553 | for my $bin (@bins_in_range) { | ||
| 554 | next unless exists $index->{$bin}; | ||
| 555 | my @found = keys %{$index->{$bin}}; | ||
| 556 | for my $f (@found) { | ||
| 557 | next if $seenit{$f}++; | ||
| 558 | my $feature = $self->_fetch($f) or next; | ||
| 559 | next if $strand && $feature->strand != $strand; | ||
| 560 | |||
| 561 | if ($range_type eq 'overlaps') { | ||
| 562 | next unless $feature->end >= $start && $feature->start <= $end; | ||
| 563 | } | ||
| 564 | elsif ($range_type eq 'contains') { | ||
| 565 | next unless $feature->start >= $start && $feature->end <= $end; | ||
| 566 | } | ||
| 567 | elsif ($range_type eq 'contained_in') { | ||
| 568 | next unless $feature->start <= $start && $feature->end >= $end; | ||
| 569 | } | ||
| 570 | |||
| 571 | push @results,$f; | ||
| 572 | } | ||
| 573 | } | ||
| 574 | $self->update_filter($filter,\@results); | ||
| 575 | } | ||
| 576 | |||
| 577 | |||
| 578 | sub filter_by_name { | ||
| 579 | my ($self, $name, $allow_aliases, $filter) = @_; | ||
| 580 | |||
| 581 | my $index = $self->{_index}{name}; | ||
| 582 | |||
| 583 | my @names_to_fetch; | ||
| 584 | if (my $regexp = $self->glob_match($name)) { | ||
| 585 | @names_to_fetch = grep {/^$regexp$/i} keys %{$index}; | ||
| 586 | } else { | ||
| 587 | @names_to_fetch = lc $name; | ||
| 588 | } | ||
| 589 | |||
| 590 | my @results; | ||
| 591 | for my $n (@names_to_fetch) { | ||
| 592 | if ($allow_aliases) { | ||
| 593 | push @results,keys %{$index->{$n}}; | ||
| 594 | } else { | ||
| 595 | push @results,grep {$index->{$n}{$_} == 1} keys %{$index->{$n}}; | ||
| 596 | } | ||
| 597 | } | ||
| 598 | $self->update_filter($filter,\@results); | ||
| 599 | } | ||
| 600 | |||
| 601 | sub glob_match { | ||
| 602 | my ($self, $term) = @_; | ||
| 603 | return unless $term =~ /(?:^|[^\\])[*?]/; | ||
| 604 | $term =~ s/(^|[^\\])([+\[\]^{}\$|\(\).])/$1\\$2/g; | ||
| 605 | $term =~ s/(^|[^\\])\*/$1.*/g; | ||
| 606 | $term =~ s/(^|[^\\])\?/$1./g; | ||
| 607 | return $term; | ||
| 608 | } | ||
| 609 | |||
| 610 | |||
| 611 | sub update_filter { | ||
| 612 | my ($self, $filter, $results) = @_; | ||
| 613 | return unless @$results; | ||
| 614 | |||
| 615 | if (%$filter) { | ||
| 616 | my @filtered = grep {$filter->{$_}} @$results; | ||
| 617 | %$filter = map {$_=>1} @filtered; | ||
| 618 | } else { | ||
| 619 | %$filter = map {$_=>1} @$results; | ||
| 620 | } | ||
| 621 | |||
| 622 | } | ||
| 623 | |||
| 624 | sub _search_attributes { | ||
| 625 | my ($self, $search_string, $attribute_array, $limit) = @_; | ||
| 626 | |||
| 627 | $search_string =~ tr/*?//d; | ||
| 628 | |||
| 629 | my @words = map {quotemeta($_)} $search_string =~ /(\w+)/g; | ||
| 630 | my $search = join '|',@words; | ||
| 631 | |||
| 632 | my (%results,%notes); | ||
| 633 | |||
| 634 | my $index = $self->{_index}{attribute}; | ||
| 635 | for my $tag (@$attribute_array) { | ||
| 636 | my $attributes = $index->{lc $tag}; | ||
| 637 | for my $value (keys %{$attributes}) { | ||
| 638 | next unless $value =~ /$search/i; | ||
| 639 | my @ids = keys %{$attributes->{$value}}; | ||
| 640 | for my $w (@words) { | ||
| 641 | my @hits = $value =~ /($w)/ig or next; | ||
| 642 | $results{$_} += @hits foreach @ids; | ||
| 643 | } | ||
| 644 | $notes{$_} .= "$value " foreach @ids; | ||
| 645 | } | ||
| 646 | } | ||
| 647 | |||
| 648 | my @results; | ||
| 649 | for my $id (keys %results) { | ||
| 650 | my $hits = $results{$id}; | ||
| 651 | my $note = $notes{$id}; | ||
| 652 | $note =~ s/\s+$//; | ||
| 653 | my $relevance = 10 * $hits; | ||
| 654 | my $feature = $self->fetch($id); | ||
| 655 | my $name = $feature->display_name or next; | ||
| 656 | my $type = $feature->type; | ||
| 657 | push @results,[$name,$note,$relevance,$type,$id]; | ||
| 658 | } | ||
| 659 | |||
| 660 | return @results; | ||
| 661 | } | ||
| 662 | |||
| 663 | =head2 types | ||
| 664 | |||
| 665 | Title : types | ||
| 666 | Usage : @type_list = $db->types | ||
| 667 | Function: Get all the types in the database | ||
| 668 | Returns : array of Bio::DB::GFF::Typename objects (arrayref in scalar context) | ||
| 669 | Args : none | ||
| 670 | Status : public | ||
| 671 | |||
| 672 | =cut | ||
| 673 | |||
| 674 | sub types { | ||
| 675 | my $self = shift; | ||
| 676 | my @types; | ||
| 677 | for my $primary_tag ( keys %{$$self{_index}{type}} ) { | ||
| 678 | for my $source_tag ( keys %{$$self{_index}{type}{$primary_tag}} ) { | ||
| 679 | push @types, Bio::DB::GFF::Typename->new($primary_tag,$source_tag); | ||
| 680 | } | ||
| 681 | } | ||
| 682 | return @types; | ||
| 683 | } | ||
| 684 | |||
| 685 | # this is ugly | ||
| 686 | sub _insert_sequence { | ||
| 687 | my ($self, $seqid, $seq, $offset) = @_; | ||
| 688 | my $dna_fh = $self->private_fasta_file or return; | ||
| 689 | if ($offset == 0) { # start of the sequence | ||
| 690 | print $dna_fh ">$seqid\n"; | ||
| 691 | } | ||
| 692 | print $dna_fh $seq,"\n"; | ||
| 693 | } | ||
| 694 | |||
| 695 | sub _fetch_sequence { | ||
| 696 | my ($self, $seqid, $start, $end) = @_; | ||
| 697 | my $db = $self->{fasta_db} or return; | ||
| 698 | return $db->seq($seqid,$start,$end); | ||
| 699 | } | ||
| 700 | |||
| 701 | sub private_fasta_file { | ||
| 702 | my $self = shift; | ||
| 703 | return $self->{fasta_fh} if exists $self->{fasta_fh}; | ||
| 704 | my $dir = tempdir (CLEANUP => 1); | ||
| 705 | $self->{fasta_file} = "$dir/sequence.$$.fasta"; | ||
| 706 | return $self->{fasta_fh} = IO::File->new($self->{fasta_file},">"); | ||
| 707 | } | ||
| 708 | |||
| 709 | # summary support | ||
| 710 | sub coverage_array { | ||
| 711 | my $self = shift; | ||
| 712 | |||
| 713 | my ($seq_name,$start,$end,$types,$bins) = | ||
| 714 | rearrange([['SEQID','SEQ_ID','REF'],'START',['STOP','END'], | ||
| 715 | ['TYPES','TYPE','PRIMARY_TAG'],'BINS'],@_); | ||
| 716 | |||
| 717 | my @features = $self->_features(-seq_id=> $seq_name, | ||
| 718 | -start => $start, | ||
| 719 | -end => $end, | ||
| 720 | -types => $types); | ||
| 721 | |||
| 722 | my $binsize = ($end-$start+1)/$bins; | ||
| 723 | my $report_tag; | ||
| 724 | my @coverage_array = (0) x $bins; | ||
| 725 | |||
| 726 | for my $f (@features) { | ||
| 727 | $report_tag ||= $f->primary_tag; | ||
| 728 | my $fs = $f->start; | ||
| 729 | my $fe = $f->end; | ||
| 730 | my $start_bin = int(($fs-$start)/$binsize); | ||
| 731 | my $end_bin = int(($fe-$start)/$binsize); | ||
| 732 | $start_bin = 0 if $start_bin < 0; | ||
| 733 | $end_bin = $bins-1 if $end_bin >= $bins; | ||
| 734 | $coverage_array[$_]++ for ($start_bin..$end_bin); | ||
| 735 | } | ||
| 736 | return wantarray ? (\@coverage_array,$report_tag) : \@coverage_array; | ||
| 737 | } | ||
| 738 | |||
| 739 | sub _seq_ids { | ||
| 740 | my $self = shift; | ||
| 741 | |||
| 742 | if (my $fa = $self->{fasta_db}) { | ||
| 743 | if (my @s = eval {$fa->ids}) { | ||
| 744 | return @s; | ||
| 745 | } | ||
| 746 | } | ||
| 747 | |||
| 748 | my $l = $self->{_index}{location} or return; | ||
| 749 | return keys %$l; | ||
| 750 | } | ||
| 751 | |||
| 752 | package Bio::DB::SeqFeature::Store::memory::Iterator; | ||
| 753 | $Bio::DB::SeqFeature::Store::memory::Iterator::VERSION = '1.7.5'; | ||
| 754 | sub new { | ||
| 755 | my ($class, $store, $ids) = @_; | ||
| 756 | return bless {store => $store, | ||
| 757 | ids => $ids},ref($class) || $class; | ||
| 758 | } | ||
| 759 | |||
| 760 | sub next_seq { | ||
| 761 | my $self = shift; | ||
| 762 | my $store = $self->{store} or return; | ||
| 763 | my $id = shift @{$self->{ids}}; | ||
| 764 | defined $id or return; | ||
| 765 | return $store->fetch($id); | ||
| 766 | } | ||
| 767 | |||
| 768 | 1; | ||
| 769 | |||
| 770 | __END__ | ||