| 1 | package Bio::DB::SeqFeature::Store::GFF2Loader; | ||
| 2 | $Bio::DB::SeqFeature::Store::GFF2Loader::VERSION = '1.7.5'; | ||
| 3 | # $Id: GFF2Loader.pm 11755 2007-11-08 02:19:29Z cjfields $ | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Bio::DB::SeqFeature::Store::GFF2Loader -- GFF2 file loader for Bio::DB::SeqFeature::Store | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Bio::DB::SeqFeature::Store; | ||
| 12 | use Bio::DB::SeqFeature::Store::GFF2Loader; | ||
| 13 | |||
| 14 | # Open the sequence database | ||
| 15 | my $db = Bio::DB::SeqFeature::Store->new( -adaptor => 'DBI::mysql', | ||
| 16 | -dsn => 'dbi:mysql:test', | ||
| 17 | -write => 1 ); | ||
| 18 | |||
| 19 | my $loader = Bio::DB::SeqFeature::Store::GFF2Loader->new(-store => $db, | ||
| 20 | -verbose => 1, | ||
| 21 | -fast => 1); | ||
| 22 | |||
| 23 | $loader->load('./my_genome.gff'); | ||
| 24 | |||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | The Bio::DB::SeqFeature::Store::GFF2Loader object parsers GFF2-format | ||
| 29 | sequence annotation files and loads Bio::DB::SeqFeature::Store | ||
| 30 | databases. For certain combinations of SeqFeature classes and | ||
| 31 | SeqFeature::Store databases it features a "fast load" mode which will | ||
| 32 | greatly accelerate the loading of GFF2 databases by a factor of 5-10. | ||
| 33 | |||
| 34 | The GFF2 file format has been extended very slightly to accommodate | ||
| 35 | Bio::DB::SeqFeature::Store. First, the loader recognizes is a new | ||
| 36 | directive: | ||
| 37 | |||
| 38 | # #index-subfeatures [0|1] | ||
| 39 | |||
| 40 | Note that you can place a space between the two #'s in order to | ||
| 41 | prevent GFF2 validators from complaining. | ||
| 42 | |||
| 43 | If this is true, then subfeatures are indexed (the default) so that | ||
| 44 | they can be retrieved with a query. See L<Bio::DB::SeqFeature::Store> | ||
| 45 | for an explanation of this. If false, then subfeatures can only be | ||
| 46 | accessed through their parent feature. The default is to index all | ||
| 47 | subfeatures. | ||
| 48 | |||
| 49 | Second, the loader recognizes a new attribute tag called index, which | ||
| 50 | if present, controls indexing of the current feature. Example: | ||
| 51 | |||
| 52 | ctg123 . TF_binding_site 1000 1012 . + . ID=tfbs00001;index=1 | ||
| 53 | |||
| 54 | You can use this to turn indexing on and off, overriding the default | ||
| 55 | for a particular feature. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | |||
| 60 | # load utility - incrementally load the store based on GFF2 file | ||
| 61 | # | ||
| 62 | # two modes: | ||
| 63 | # slow mode -- features can occur in any order in the GFF2 file | ||
| 64 | # fast mode -- all features with same ID must be contiguous in GFF2 file | ||
| 65 | |||
| 66 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 67 | 1 | ▶ | use Carp 'croak'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 68 | 1 | ▶ | use Bio::DB::GFF::Util::Rearrange; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 69 | 1 | ▶ | use Text::ParseWords 'quotewords'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 70 | 1 | ▶ | use base 'Bio::DB::SeqFeature::Store::GFF3Loader'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 71 | |||
| 72 | my %Special_attributes =( | ||
| 73 | Gap => 1, Target => 1, | ||
| 74 | Parent => 1, Name => 1, | ||
| 75 | Alias => 1, ID => 1, | ||
| 76 | index => 1, Index => 1, | ||
| 77 | ); | ||
| 78 | |||
| 79 | =head2 new | ||
| 80 | |||
| 81 | Title : new | ||
| 82 | Usage : $loader = Bio::DB::SeqFeature::Store::GFF2Loader->new(@options) | ||
| 83 | Function: create a new parser | ||
| 84 | Returns : a Bio::DB::SeqFeature::Store::GFF2Loader gff2 parser and loader | ||
| 85 | Args : several - see below | ||
| 86 | Status : public | ||
| 87 | |||
| 88 | This method creates a new GFF2 loader and establishes its connection | ||
| 89 | with a Bio::DB::SeqFeature::Store database. Arguments are -name=E<gt>$value | ||
| 90 | pairs as described in this table: | ||
| 91 | |||
| 92 | Name Value | ||
| 93 | ---- ----- | ||
| 94 | |||
| 95 | -store A writable Bio::DB::SeqFeature::Store database handle. | ||
| 96 | |||
| 97 | -seqfeature_class The name of the type of Bio::SeqFeatureI object to create | ||
| 98 | and store in the database (Bio::DB::SeqFeature by default) | ||
| 99 | |||
| 100 | -sf_class A shorter alias for -seqfeature_class | ||
| 101 | |||
| 102 | -verbose Send progress information to standard error. | ||
| 103 | |||
| 104 | -fast If true, activate fast loading (see below) | ||
| 105 | |||
| 106 | -chunk_size Set the storage chunk size for nucleotide/protein sequences | ||
| 107 | (default 2000 bytes) | ||
| 108 | |||
| 109 | -tmp Indicate a temporary directory to use when loading non-normalized | ||
| 110 | features. | ||
| 111 | |||
| 112 | When you call new(), a connection to a Bio::DB::SeqFeature::Store | ||
| 113 | database should already have been established and the database | ||
| 114 | initialized (if appropriate). | ||
| 115 | |||
| 116 | Some combinations of Bio::SeqFeatures and Bio::DB::SeqFeature::Store | ||
| 117 | databases support a fast loading mode. Currently the only reliable | ||
| 118 | implementation of fast loading is the combination of DBI::mysql with | ||
| 119 | Bio::DB::SeqFeature. The other important restriction on fast loading | ||
| 120 | is the requirement that a feature that contains subfeatures must occur | ||
| 121 | in the GFF2 file before any of its subfeatures. Otherwise the | ||
| 122 | subfeatures that occurred before the parent feature will not be | ||
| 123 | attached to the parent correctly. This restriction does not apply to | ||
| 124 | normal (slow) loading. | ||
| 125 | |||
| 126 | If you use an unnormalized feature class, such as | ||
| 127 | Bio::SeqFeature::Generic, then the loader needs to create a temporary | ||
| 128 | database in which to cache features until all their parts and subparts | ||
| 129 | have been seen. This temporary databases uses the "berkeleydb" adaptor. The | ||
| 130 | -tmp option specifies the directory in which that database will be | ||
| 131 | created. If not present, it defaults to the system default tmp | ||
| 132 | directory specified by File::Spec-E<gt>tmpdir(). | ||
| 133 | |||
| 134 | The -chunk_size option allows you to tune the representation of | ||
| 135 | DNA/Protein sequence in the Store database. By default, sequences are | ||
| 136 | split into 2000 base/residue chunks and then reassembled as | ||
| 137 | needed. This avoids the problem of pulling a whole chromosome into | ||
| 138 | memory in order to fetch a short subsequence from somewhere in the | ||
| 139 | middle. Depending on your usage patterns, you may wish to tune this | ||
| 140 | parameter using a chunk size that is larger or smaller than the | ||
| 141 | default. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | # sub new { } inherited | ||
| 146 | |||
| 147 | =head2 load | ||
| 148 | |||
| 149 | Title : load | ||
| 150 | Usage : $count = $loader->load(@ARGV) | ||
| 151 | Function: load the indicated files or filehandles | ||
| 152 | Returns : number of feature lines loaded | ||
| 153 | Args : list of files or filehandles | ||
| 154 | Status : public | ||
| 155 | |||
| 156 | Once the loader is created, invoke its load() method with a list of | ||
| 157 | GFF2 or FASTA file paths or previously-opened filehandles in order to | ||
| 158 | load them into the database. Compressed files ending with .gz, .Z and | ||
| 159 | .bz2 are automatically recognized and uncompressed on the fly. Paths | ||
| 160 | beginning with http: or ftp: are treated as URLs and opened using the | ||
| 161 | LWP GET program (which must be on your path). | ||
| 162 | |||
| 163 | FASTA files are recognized by their initial "E<gt>" character. Do not feed | ||
| 164 | the loader a file that is neither GFF2 nor FASTA; I don't know what | ||
| 165 | will happen, but it will probably not be what you expect. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | # sub load { } inherited | ||
| 170 | |||
| 171 | =head2 accessors | ||
| 172 | |||
| 173 | The following read-only accessors return values passed or created during new(): | ||
| 174 | |||
| 175 | store() the long-term Bio::DB::SeqFeature::Store object | ||
| 176 | |||
| 177 | tmp_store() the temporary Bio::DB::SeqFeature::Store object used | ||
| 178 | during loading | ||
| 179 | |||
| 180 | sfclass() the Bio::SeqFeatureI class | ||
| 181 | |||
| 182 | fast() whether fast loading is active | ||
| 183 | |||
| 184 | seq_chunk_size() the sequence chunk size | ||
| 185 | |||
| 186 | verbose() verbose progress messages | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | # sub store inherited | ||
| 191 | # sub tmp_store inherited | ||
| 192 | # sub sfclass inherited | ||
| 193 | # sub fast inherited | ||
| 194 | # sub seq_chunk_size inherited | ||
| 195 | # sub verbose inherited | ||
| 196 | |||
| 197 | =head2 Internal Methods | ||
| 198 | |||
| 199 | The following methods are used internally and may be overridden by | ||
| 200 | subclasses. | ||
| 201 | |||
| 202 | =over 4 | ||
| 203 | |||
| 204 | =item default_seqfeature_class | ||
| 205 | |||
| 206 | $class = $loader->default_seqfeature_class | ||
| 207 | |||
| 208 | Return the default SeqFeatureI class (Bio::DB::SeqFeature). | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | # sub default_seqfeature_class { } inherited | ||
| 213 | |||
| 214 | =item subfeatures_normalized | ||
| 215 | |||
| 216 | $flag = $loader->subfeatures_normalized([$new_flag]) | ||
| 217 | |||
| 218 | Get or set a flag that indicates that the subfeatures are | ||
| 219 | normalized. This is deduced from the SeqFeature class information. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | # sub subfeatures_normalized { } inherited | ||
| 224 | |||
| 225 | =item subfeatures_in_table | ||
| 226 | |||
| 227 | $flag = $loader->subfeatures_in_table([$new_flag]) | ||
| 228 | |||
| 229 | Get or set a flag that indicates that feature/subfeature relationships | ||
| 230 | are stored in a table. This is deduced from the SeqFeature class and | ||
| 231 | Store information. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | # sub subfeatures_in_table { } inherited | ||
| 236 | |||
| 237 | =item load_fh | ||
| 238 | |||
| 239 | $count = $loader->load_fh($filehandle) | ||
| 240 | |||
| 241 | Load the GFF2 data at the other end of the filehandle and return true | ||
| 242 | if successful. Internally, load_fh() invokes: | ||
| 243 | |||
| 244 | start_load(); | ||
| 245 | do_load($filehandle); | ||
| 246 | finish_load(); | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | # sub load_fh { } inherited | ||
| 251 | |||
| 252 | =item start_load, finish_load | ||
| 253 | |||
| 254 | These methods are called at the start and end of a filehandle load. | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | # sub create_load_data { } #inherited | ||
| 259 | |||
| 260 | # sub finish_load { } #inherite | ||
| 261 | |||
| 262 | =item do_load | ||
| 263 | |||
| 264 | $count = $loader->do_load($fh) | ||
| 265 | |||
| 266 | This is called by load_fh() to load the GFF2 file's filehandle and | ||
| 267 | return the number of lines loaded. | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | # sub do_load { } inherited | ||
| 272 | |||
| 273 | =item load_line | ||
| 274 | |||
| 275 | $loader->load_line($data); | ||
| 276 | |||
| 277 | Load a line of a GFF2 file. You must bracket this with calls to | ||
| 278 | start_load() and finish_load()! | ||
| 279 | |||
| 280 | $loader->start_load(); | ||
| 281 | $loader->load_line($_) while <FH>; | ||
| 282 | $loader->finish_load(); | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | # sub load_line { } # inherited | ||
| 287 | |||
| 288 | =item handle_meta | ||
| 289 | |||
| 290 | $loader->handle_meta($meta_directive) | ||
| 291 | |||
| 292 | This method is called to handle meta-directives such as | ||
| 293 | ##sequence-region. The method will receive the directive with the | ||
| 294 | initial ## stripped off. | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | # sub handle_meta {} # inherited | ||
| 299 | |||
| 300 | =item handle_feature | ||
| 301 | |||
| 302 | $loader->handle_feature($gff2_line) | ||
| 303 | |||
| 304 | This method is called to process a single GFF2 line. It manipulates | ||
| 305 | information stored a data structure called $self-E<gt>{load_data}. | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | # sub handle_feature { } # inherited | ||
| 310 | |||
| 311 | =item store_current_feature | ||
| 312 | |||
| 313 | $loader->store_current_feature() | ||
| 314 | |||
| 315 | This method is called to store the currently active feature in the | ||
| 316 | database. It uses a data structure stored in $self-E<gt>{load_data}. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | # sub store_current_feature { } inherited | ||
| 321 | |||
| 322 | =item build_object_tree | ||
| 323 | |||
| 324 | $loader->build_object_tree() | ||
| 325 | |||
| 326 | This method gathers together features and subfeatures and builds the graph that connects them. | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | # sub build_object_tree { } # inherited | ||
| 331 | |||
| 332 | =item build_object_tree_in_tables | ||
| 333 | |||
| 334 | $loader->build_object_tree_in_tables() | ||
| 335 | |||
| 336 | This method gathers together features and subfeatures and builds the | ||
| 337 | graph that connects them, assuming that parent/child relationships | ||
| 338 | will be stored in a database table. | ||
| 339 | |||
| 340 | =cut | ||
| 341 | |||
| 342 | # sub build_object_tree_in_tables { } # inherited | ||
| 343 | |||
| 344 | =item build_object_tree_in_features | ||
| 345 | |||
| 346 | $loader->build_object_tree_in_features() | ||
| 347 | |||
| 348 | This method gathers together features and subfeatures and builds the | ||
| 349 | graph that connects them, assuming that parent/child relationships are | ||
| 350 | stored in the seqfeature objects themselves. | ||
| 351 | |||
| 352 | =cut | ||
| 353 | |||
| 354 | # sub build_object_tree_in_features { } # inherited | ||
| 355 | |||
| 356 | =item attach_children | ||
| 357 | |||
| 358 | $loader->attach_children($store,$load_data,$load_id,$feature) | ||
| 359 | |||
| 360 | This recursively adds children to features and their subfeatures. It | ||
| 361 | is called when subfeatures are directly contained within other | ||
| 362 | features, rather than stored in a relational table. | ||
| 363 | |||
| 364 | =cut | ||
| 365 | |||
| 366 | # sub attach_children { } # inherited | ||
| 367 | |||
| 368 | =item fetch | ||
| 369 | |||
| 370 | my $feature = $loader->fetch($load_id) | ||
| 371 | |||
| 372 | Given a load ID (from the ID= attribute) this method returns the | ||
| 373 | feature from the temporary database or the permanent one, depending on | ||
| 374 | where it is stored. | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | # sub fetch { } # inherited | ||
| 379 | |||
| 380 | =item add_segment | ||
| 381 | |||
| 382 | $loader->add_segment($parent,$child) | ||
| 383 | |||
| 384 | This method is used to add a split location to the parent. | ||
| 385 | |||
| 386 | =cut | ||
| 387 | |||
| 388 | # sub add_segment { } # inherited | ||
| 389 | |||
| 390 | =item parse_attributes | ||
| 391 | |||
| 392 | ($reserved,$unreserved) = $loader->parse_attributes($attribute_line) | ||
| 393 | |||
| 394 | This method parses the information contained in the $attribute_line | ||
| 395 | into two hashrefs, one containing the values of reserved attribute | ||
| 396 | tags (e.g. ID) and the other containing the values of unreserved ones. | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | sub parse_attributes { # overridden | ||
| 401 | my $self = shift; | ||
| 402 | my $att = shift; | ||
| 403 | my @groups = quotewords('\s*;\s*',0,$att); | ||
| 404 | my (%reserved,%unreserved); | ||
| 405 | |||
| 406 | my $found_name; | ||
| 407 | |||
| 408 | for (@groups) { | ||
| 409 | my ($tag,$value); | ||
| 410 | if (/^(\S+)\s+(.+)/) { # Tag value pair | ||
| 411 | ($tag,$value) = ($1,$2); | ||
| 412 | } else { | ||
| 413 | $tag = 'Note'; | ||
| 414 | $value = $_; | ||
| 415 | } | ||
| 416 | if ($tag eq 'Target') { | ||
| 417 | my ($target,$start,$end) = split /\s+/,$value; | ||
| 418 | push @{$reserved{ID}},$target; | ||
| 419 | $found_name++; | ||
| 420 | if ($start <= $end) { $value .= ' +' } | ||
| 421 | else { $value .= ' -' } | ||
| 422 | } | ||
| 423 | |||
| 424 | if (!$found_name++) { | ||
| 425 | push @{$reserved{Alias}},$value; | ||
| 426 | $value = "$tag:$value"; | ||
| 427 | push @{$reserved{ID}},$value; | ||
| 428 | $tag = 'Name'; | ||
| 429 | } | ||
| 430 | |||
| 431 | if ($Special_attributes{$tag}) { # reserved attribute | ||
| 432 | push @{$reserved{$tag}},$value; | ||
| 433 | } else { | ||
| 434 | push @{$unreserved{$tag}},$value; | ||
| 435 | } | ||
| 436 | } | ||
| 437 | return (\%reserved,\%unreserved); | ||
| 438 | } | ||
| 439 | |||
| 440 | =item start_or_finish_sequence | ||
| 441 | |||
| 442 | $loader->start_or_finish_sequence('Chr9') | ||
| 443 | |||
| 444 | This method is called at the beginning and end of a fasta section. | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | # sub start_or_finish_sequence { } inherited | ||
| 449 | |||
| 450 | =item load_sequence | ||
| 451 | |||
| 452 | $loader->load_sequence('gatttcccaaa') | ||
| 453 | |||
| 454 | This method is called to load some amount of sequence after | ||
| 455 | start_or_finish_sequence() is first called. | ||
| 456 | |||
| 457 | =cut | ||
| 458 | |||
| 459 | # sub load_sequence { } inherited | ||
| 460 | |||
| 461 | =item open_fh | ||
| 462 | |||
| 463 | my $io_file = $loader->open_fh($filehandle_or_path) | ||
| 464 | |||
| 465 | This method opens up the indicated file or pipe, using some | ||
| 466 | intelligence to recognized compressed files and URLs and doing the | ||
| 467 | right thing. | ||
| 468 | |||
| 469 | =cut | ||
| 470 | |||
| 471 | # sub open_fh { } inherited | ||
| 472 | |||
| 473 | # sub msg { } inherited | ||
| 474 | |||
| 475 | =item time | ||
| 476 | |||
| 477 | my $time = $loader->time | ||
| 478 | |||
| 479 | This method returns the current time in seconds, using Time::HiRes if available. | ||
| 480 | |||
| 481 | =cut | ||
| 482 | |||
| 483 | # sub time { } inherited | ||
| 484 | |||
| 485 | =item unescape | ||
| 486 | |||
| 487 | my $unescaped = GFF2Loader::unescape($escaped) | ||
| 488 | |||
| 489 | This is an internal utility. It is the same as CGI::Util::unescape, | ||
| 490 | but doesn't change pluses into spaces and ignores unicode escapes. | ||
| 491 | |||
| 492 | =cut | ||
| 493 | |||
| 494 | # sub unescape { } inherited | ||
| 495 | |||
| 496 | 1; | ||
| 497 | __END__ | ||