| 1 | # some of the following code was pillaged from the CPAN module | ||
| 2 | # XML::Handler::Subs | ||
| 3 | # | ||
| 4 | # Copyright (C) 1999 Ken MacLeod | ||
| 5 | # XML::Handler::XMLWriter is free software; you can redistribute it and/or | ||
| 6 | # modify it under the same terms as Perl itself. | ||
| 7 | |||
| 8 | # | ||
| 9 | # BioPerl module for Bio::SeqIO::game::gameSubs | ||
| 10 | # | ||
| 11 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 12 | # | ||
| 13 | # Cared for by Sheldon McKay <mckays@cshl.edu> | ||
| 14 | # | ||
| 15 | # You may distribute this module under the same terms as perl itself | ||
| 16 | # | ||
| 17 | |||
| 18 | # POD documentation - main docs before the code | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Bio::SeqIO::game::gameSubs -- a base class for game-XML parsing | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | Not used directly | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | A bag of tricks for game-XML parsing. The PerlSAX handler methods were | ||
| 31 | stolen from Chris Mungall's XML base class, which he stole from Ken MacLeod's | ||
| 32 | XML::Handler::Subs | ||
| 33 | |||
| 34 | =head1 FEEDBACK | ||
| 35 | |||
| 36 | =head2 Mailing Lists | ||
| 37 | |||
| 38 | User feedback is an integral part of the evolution of this | ||
| 39 | and other Bioperl modules. Send your comments and suggestions preferably | ||
| 40 | to one of the Bioperl mailing lists. | ||
| 41 | |||
| 42 | Your participation is much appreciated. | ||
| 43 | |||
| 44 | bioperl-l@bioperl.org - General discussion | ||
| 45 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 46 | |||
| 47 | =head2 Support | ||
| 48 | |||
| 49 | Please direct usage questions or support issues to the mailing list: | ||
| 50 | |||
| 51 | I<bioperl-l@bioperl.org> | ||
| 52 | |||
| 53 | rather than to the module maintainer directly. Many experienced and | ||
| 54 | reponsive experts will be able look at the problem and quickly | ||
| 55 | address it. Please include a thorough description of the problem | ||
| 56 | with code and data examples if at all possible. | ||
| 57 | |||
| 58 | =head2 Reporting Bugs | ||
| 59 | |||
| 60 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 61 | of the bugs and their resolution. Bug reports can be submitted via the | ||
| 62 | web: | ||
| 63 | |||
| 64 | https://github.com/bioperl/bioperl-live/issues | ||
| 65 | |||
| 66 | =head1 AUTHOR - Sheldon McKay | ||
| 67 | |||
| 68 | Email mckays@cshl.edu | ||
| 69 | |||
| 70 | =head1 APPENDIX | ||
| 71 | |||
| 72 | The rest of the documentation details each of the object | ||
| 73 | methods. Internal methods are usually preceded with a _ | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | package Bio::SeqIO::game::gameSubs; | ||
| 78 | $Bio::SeqIO::game::gameSubs::VERSION = '1.7.8'; | ||
| 79 | 0 | ▶ | use XML::Parser::PerlSAX; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 80 | use UNIVERSAL; | ||
| 81 | use strict; | ||
| 82 | |||
| 83 | use vars qw {}; | ||
| 84 | use base qw(Bio::Root::Root); | ||
| 85 | |||
| 86 | |||
| 87 | =head2 new | ||
| 88 | |||
| 89 | Title : new | ||
| 90 | Usage : not used directly | ||
| 91 | Returns : a gameHandler object | ||
| 92 | Args : an XML filename | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub new { | ||
| 97 | my $type = shift; | ||
| 98 | my $file = shift || ""; | ||
| 99 | my $self = (@_ == 1) ? { %{ (shift) } } : { @_ }; | ||
| 100 | if ($file) { | ||
| 101 | $self->{file} = $file; | ||
| 102 | } | ||
| 103 | |||
| 104 | return bless $self, $type; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | =head2 go | ||
| 109 | |||
| 110 | Title : go | ||
| 111 | Usage : not used directly | ||
| 112 | Function: starts PerlSAX XML parsing | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub go { | ||
| 117 | my $self = shift; | ||
| 118 | XML::Parser::PerlSAX->new->parse(Source => { SystemId => "$self->{file}" }, | ||
| 119 | Handler => $self); | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 start_document | ||
| 123 | |||
| 124 | Title : start_document | ||
| 125 | Usage : not used directly | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub start_document { | ||
| 130 | my ($self, $document) = @_; | ||
| 131 | |||
| 132 | $self->{Names} = []; | ||
| 133 | $self->{Nodes} = []; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | =head2 end_document | ||
| 139 | |||
| 140 | Title : end_document | ||
| 141 | Usage : not used directly | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub end_document { | ||
| 146 | my ($self, $document) = @_; | ||
| 147 | |||
| 148 | delete $self->{Names}; | ||
| 149 | delete $self->{Nodes}; | ||
| 150 | |||
| 151 | return(); | ||
| 152 | } | ||
| 153 | |||
| 154 | =head2 start_element | ||
| 155 | |||
| 156 | Title : start_element | ||
| 157 | Usage : not used directly | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub start_element { | ||
| 162 | my ($self, $element) = @_; | ||
| 163 | |||
| 164 | $element->{Children} = []; | ||
| 165 | |||
| 166 | $element->{Name} =~ tr/A-Z/a-z/; | ||
| 167 | push @{$self->{Names}}, $element->{Name}; | ||
| 168 | push @{$self->{Nodes}}, $element; | ||
| 169 | |||
| 170 | my $el_name = "s_" . $element->{Name}; | ||
| 171 | $el_name =~ s/[^a-zA-Z0-9_]/_/g; | ||
| 172 | if ($ENV{DEBUG_XML_SUBS}) { | ||
| 173 | print STDERR "xml_subs:$el_name\n"; | ||
| 174 | } | ||
| 175 | if ($self->can($el_name)) { | ||
| 176 | $self->$el_name($element); | ||
| 177 | return 1; | ||
| 178 | } | ||
| 179 | |||
| 180 | return 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | =head2 end_element | ||
| 184 | |||
| 185 | Title : end_element | ||
| 186 | Usage : not used directly | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub end_element { | ||
| 191 | my ($self, $element) = @_; | ||
| 192 | |||
| 193 | my $called_sub = 0; | ||
| 194 | |||
| 195 | $element->{Name} =~ tr/A-Z/a-z/; | ||
| 196 | |||
| 197 | my $el_name = "e_" . $element->{Name}; | ||
| 198 | $el_name =~ s/[^a-zA-Z0-9_]/_/g; | ||
| 199 | |||
| 200 | my $rval = 0; | ||
| 201 | if ($ENV{DEBUG_XML_SUBS}) { | ||
| 202 | print STDERR "xml_subs:$el_name\n"; | ||
| 203 | } | ||
| 204 | if ($self->can($ {el_name})) { | ||
| 205 | $rval = $self->$el_name($element) || 0; | ||
| 206 | $called_sub = 1; | ||
| 207 | } | ||
| 208 | my $curr_element = $self->{Nodes}->[$#{$self->{Nodes}}]; | ||
| 209 | |||
| 210 | pop @{$self->{Names}}; | ||
| 211 | pop @{$self->{Nodes}}; | ||
| 212 | |||
| 213 | if ($rval eq -1 || !$called_sub) { | ||
| 214 | if (@{$self->{Nodes}}) { | ||
| 215 | my $parent = $self->{Nodes}->[$#{$self->{Nodes}}]; | ||
| 216 | push(@{$parent->{Children}}, $curr_element); | ||
| 217 | $parent->{"_".$curr_element->{Name}} = $curr_element; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | return $called_sub; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | =head2 characters | ||
| 227 | |||
| 228 | Title : characters | ||
| 229 | Usage : not used directly | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | sub characters { | ||
| 234 | my ($self, $characters) = @_; | ||
| 235 | |||
| 236 | my $str = $self->strip_characters($characters->{Data}); | ||
| 237 | my $curr_element = $self->curr_element(); | ||
| 238 | $curr_element->{Characters} .= $str; | ||
| 239 | 0; | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 strip_characters | ||
| 243 | |||
| 244 | Title : strip_characters | ||
| 245 | Usage : not used directly | ||
| 246 | Function: cleans up XML element contents | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | sub strip_characters { | ||
| 251 | my ($self, $str) = @_; | ||
| 252 | $str =~ s/^[ \n\t]* *//g; | ||
| 253 | $str =~ s/ *[\n\t]*$//g; | ||
| 254 | $str; | ||
| 255 | } | ||
| 256 | |||
| 257 | =head2 curr_element | ||
| 258 | |||
| 259 | Title : curr_element | ||
| 260 | Usage : not used directly | ||
| 261 | Function: returns the currently open element | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub curr_element { | ||
| 266 | my $self = shift; | ||
| 267 | return $self->{Nodes}->[-1]; | ||
| 268 | } | ||
| 269 | |||
| 270 | =head2 flush | ||
| 271 | |||
| 272 | Title : flush | ||
| 273 | Usage : $self->flush($element) # or $element->flush | ||
| 274 | Function: prune a branch from the XML tree | ||
| 275 | Returns : true if successful | ||
| 276 | Args : an element object (optional) | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub flush { | ||
| 281 | my $self = shift; | ||
| 282 | my $victim = shift || $self->curr_element; | ||
| 283 | $victim = {}; | ||
| 284 | return 1; | ||
| 285 | } | ||
| 286 | |||
| 287 | # throw a non-fatal warning | ||
| 288 | |||
| 289 | =head2 complain | ||
| 290 | |||
| 291 | Title : complain | ||
| 292 | Usage : $self->complain("This is terrible; I am not happy") | ||
| 293 | Function: throw a non-fatal warning, formats message for pretty-printing | ||
| 294 | Returns : nothing | ||
| 295 | Args : a list of strings | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | sub complain { | ||
| 300 | my $self = shift; | ||
| 301 | return 0 unless $self->{verbose}; | ||
| 302 | my $msg = join '', @_; | ||
| 303 | $msg =~ s/\n/ /g; | ||
| 304 | my @msg = split /\s+/, $msg; | ||
| 305 | my $new_msg = ''; | ||
| 306 | |||
| 307 | for ( @msg ) { | ||
| 308 | my ($last_chunk) = $new_msg =~ /\n?(.+)$/; | ||
| 309 | my $l = $last_chunk ? length $last_chunk : 0; | ||
| 310 | if ( (length $_) + $l > 45 ) { | ||
| 311 | $new_msg .= "\n$_ "; | ||
| 312 | } | ||
| 313 | else { | ||
| 314 | $new_msg .= $_ . ' '; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | $self->warn($new_msg); | ||
| 319 | } | ||
| 320 | |||
| 321 | =head2 dbxref | ||
| 322 | |||
| 323 | Title : dbxref | ||
| 324 | Usage : $self->db_xref($el, $tags) | ||
| 325 | Function: an internal method to flatten dbxref elements | ||
| 326 | Returns : the db_xref (eg wormbase:C02D5.1) | ||
| 327 | Args : an element object (reqd) and a hash ref of tag/values (optional) | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | sub dbxref { | ||
| 332 | my ($self, $el, $tags) = @_; | ||
| 333 | $tags ||= $self->{curr_tags}; | ||
| 334 | my $db = $el->{_xref_db}->{Characters}; | ||
| 335 | my $acc = $el->{_unique_id} || | ||
| 336 | $el->{_db_xref_id} || | ||
| 337 | $el->{_xref_db_id}; | ||
| 338 | my $id = $acc->{Characters} or return 0; | ||
| 339 | $self->flush( $el ); | ||
| 340 | |||
| 341 | # capture both the database and accession number | ||
| 342 | $id= $id =~ /^\w+$/ ? "$db:$id" : $id; | ||
| 343 | $tags->{dbxref} ||= []; | ||
| 344 | push @{$tags->{dbxref}}, $id; | ||
| 345 | $id; | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | =head2 comment | ||
| 350 | |||
| 351 | Title : comment | ||
| 352 | Usage : $self->comment($comment_element) | ||
| 353 | Function: a method to flatten comment elements | ||
| 354 | Returns : a string | ||
| 355 | Args : an comment element (reqd) and a hash ref of tag/values (optional) | ||
| 356 | Note : The hope here is that we can unflatten structured comments | ||
| 357 | in game-derived annotations happen to make a return trip | ||
| 358 | |||
| 359 | =cut | ||
| 360 | |||
| 361 | sub comment { | ||
| 362 | my ($self, $el, $tags) = @_; | ||
| 363 | |||
| 364 | $tags ||= $self->{curr_tags}; | ||
| 365 | my $text = $el->{_text}->{Characters}; | ||
| 366 | my $pers = $el->{_person}->{Characters}; | ||
| 367 | my $date = $el->{_date}->{Characters}; | ||
| 368 | my $int = $el->{_internal}->{Characters}; | ||
| 369 | $self->flush( $el ); | ||
| 370 | |||
| 371 | my $comment = "person=$pers; " if $pers; | ||
| 372 | $comment .= "date=$date; " if $date; | ||
| 373 | $comment .= "internal=$int; " if $int; | ||
| 374 | $comment .= "text=$text" if $text; | ||
| 375 | |||
| 376 | $tags->{comment} ||= []; | ||
| 377 | push @{$tags->{comment}}, $comment; | ||
| 378 | $comment; | ||
| 379 | } | ||
| 380 | |||
| 381 | =head2 property | ||
| 382 | |||
| 383 | Title : property | ||
| 384 | Usage : $self->property($property_element) | ||
| 385 | Function: an internal method to flatten property elements | ||
| 386 | Returns : a hash reference | ||
| 387 | Args : an property/output element (reqd) and a hash ref of tag/values (optional) | ||
| 388 | Note: This method is aliased to 'output' to handle structurally identical output elements | ||
| 389 | |||
| 390 | =cut | ||
| 391 | |||
| 392 | *output = \&property; | ||
| 393 | sub property { | ||
| 394 | my ($self, $el, $tags) = @_; | ||
| 395 | |||
| 396 | $tags ||= $self->{curr_tags}; | ||
| 397 | my $key = $el->{_type}->{Characters}; | ||
| 398 | my $value = $el->{_value}->{Characters}; | ||
| 399 | $self->flush( $el ); | ||
| 400 | |||
| 401 | $tags->{$key} ||= []; | ||
| 402 | push @{$tags->{$key}}, $value; | ||
| 403 | $tags; | ||
| 404 | } | ||
| 405 | |||
| 406 | =head2 evidence | ||
| 407 | |||
| 408 | Title : evidence | ||
| 409 | Usage : $self->evidence($evidence_element) | ||
| 410 | Function: a method to flatten evidence elements | ||
| 411 | Returns : a string | ||
| 412 | Args : an evidence element | ||
| 413 | |||
| 414 | =cut | ||
| 415 | |||
| 416 | sub evidence { | ||
| 417 | my ($self, $el) = @_; | ||
| 418 | my $tags = $self->{curr_tags}; | ||
| 419 | my $text = $el->{Characters} or return 0; | ||
| 420 | my $type = $el->{Attributes}->{type}; | ||
| 421 | my $res = $el->{Attributes}->{result}; | ||
| 422 | $self->flush( $el ); | ||
| 423 | |||
| 424 | my $evidence = "type=$type; " if $type; | ||
| 425 | $evidence .= "result=$res; " if $res; | ||
| 426 | $evidence .= "evidence=$text"; | ||
| 427 | |||
| 428 | $tags->{evidence}||= []; | ||
| 429 | push @{$tags->{evidence}}, $evidence; | ||
| 430 | $evidence; | ||
| 431 | } | ||
| 432 | |||
| 433 | =head2 date | ||
| 434 | |||
| 435 | Title : date | ||
| 436 | Usage : $self->date($date_element) | ||
| 437 | Function: a method to flatten date elements | ||
| 438 | Returns : true if successful | ||
| 439 | Args : a date element | ||
| 440 | |||
| 441 | =cut | ||
| 442 | |||
| 443 | sub date { | ||
| 444 | my ($self, $el) = @_; | ||
| 445 | my $tags = $self->{curr_tags}; | ||
| 446 | my $date = $el->{Characters} or return 0; | ||
| 447 | my $stamp = $el->{Attributes}->{timestamp}; | ||
| 448 | $self->flush( $el ); | ||
| 449 | |||
| 450 | $tags->{date} ||= []; | ||
| 451 | push @{$tags->{date}}, $date; | ||
| 452 | $tags->{timestamp} ||= []; | ||
| 453 | push @{$tags->{timestamp}}, $stamp; | ||
| 454 | 1; | ||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 458 | =head2 protein_id | ||
| 459 | |||
| 460 | Title : protein_id | ||
| 461 | Usage : $pid = $self->protein_id($cds, $standard_name) | ||
| 462 | Function: a method to search for a protein name | ||
| 463 | Returns : a string | ||
| 464 | Args : the CDS object plus the transcript\'s 'standard_name' | ||
| 465 | |||
| 466 | =cut | ||
| 467 | |||
| 468 | sub protein_id { | ||
| 469 | my ($self, $cds, $sn) = @_; | ||
| 470 | my $psn; | ||
| 471 | if ( $cds->has_tag('protein_id') ) { | ||
| 472 | ($psn) = $cds->get_tag_values('protein_id'); | ||
| 473 | } | ||
| 474 | elsif ( $cds->has_tag('product') ) { | ||
| 475 | ($psn) = $cds->get_tag_values('product'); | ||
| 476 | $psn =~ s/.+?(\S+)$/$1/; | ||
| 477 | } | ||
| 478 | elsif ( $cds->has_tag('gene') ) { | ||
| 479 | ($psn) = $cds->get_tag_values('gene'); | ||
| 480 | } | ||
| 481 | elsif ( $sn ) { | ||
| 482 | $psn = $sn; | ||
| 483 | } | ||
| 484 | else { | ||
| 485 | $self->complain("Could not find an ID for the protein"); | ||
| 486 | return ''; | ||
| 487 | } | ||
| 488 | |||
| 489 | $psn =~ s/-R/-P/; | ||
| 490 | return $psn; | ||
| 491 | } | ||
| 492 | |||
| 493 | 1; | ||
| 494 | |||