| 1 | # | ||
| 2 | # BioPerl module for Bio::SeqIO::bsml | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Charles Tilford (tilfordc@bms.com) | ||
| 7 | # Copyright (C) Charles Tilford 2001 | ||
| 8 | # | ||
| 9 | # This library is free software; you can redistribute it and/or | ||
| 10 | # modify it under the terms of the GNU Lesser General Public | ||
| 11 | # License as published by the Free Software Foundation; either | ||
| 12 | # version 2.1 of the License, or (at your option) any later version. | ||
| 13 | # | ||
| 14 | # This library is distributed in the hope that it will be useful, | ||
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | # Lesser General Public License for more details. | ||
| 18 | # | ||
| 19 | # You should have received a copy of the GNU Lesser General Public | ||
| 20 | # License along with this library; if not, write to the Free Software | ||
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 22 | # Also at: http://www.gnu.org/copyleft/lesser.html | ||
| 23 | |||
| 24 | # Much of the basic documentation in this module has been | ||
| 25 | # cut-and-pasted from the embl.pm (Ewan Birney) SeqIO module. | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Bio::SeqIO::bsml - BSML sequence input/output stream | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | It is probably best not to use this object directly, but rather go | ||
| 34 | through the SeqIO handler system. To read a BSML file: | ||
| 35 | |||
| 36 | $stream = Bio::SeqIO->new( -file => $filename, -format => 'bsml'); | ||
| 37 | |||
| 38 | while ( my $bioSeqObj = $stream->next_seq() ) { | ||
| 39 | # do something with $bioSeqObj | ||
| 40 | } | ||
| 41 | |||
| 42 | To write a Seq object to the current file handle in BSML XML format: | ||
| 43 | |||
| 44 | $stream->write_seq( -seq => $seqObj); | ||
| 45 | |||
| 46 | If instead you would like a XML::DOM object containing the BSML, use: | ||
| 47 | |||
| 48 | my $newXmlObject = $stream->to_bsml( -seq => $seqObj); | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | This object can transform Bio::Seq objects to and from BSML (XML) | ||
| 53 | flatfiles. | ||
| 54 | |||
| 55 | =head1 FEEDBACK | ||
| 56 | |||
| 57 | =head2 Mailing Lists | ||
| 58 | |||
| 59 | User feedback is an integral part of the evolution of this and other | ||
| 60 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 61 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 62 | |||
| 63 | bioperl-l@bioperl.org - General discussion | ||
| 64 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 65 | |||
| 66 | =head2 Support | ||
| 67 | |||
| 68 | Please direct usage questions or support issues to the mailing list: | ||
| 69 | |||
| 70 | I<bioperl-l@bioperl.org> | ||
| 71 | |||
| 72 | rather than to the module maintainer directly. Many experienced and | ||
| 73 | reponsive experts will be able look at the problem and quickly | ||
| 74 | address it. Please include a thorough description of the problem | ||
| 75 | with code and data examples if at all possible. | ||
| 76 | |||
| 77 | =head2 Reporting Bugs | ||
| 78 | |||
| 79 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 80 | the bugs and their resolution. | ||
| 81 | Bug reports can be submitted via the web: | ||
| 82 | |||
| 83 | https://github.com/bioperl/bioperl-live/issues | ||
| 84 | |||
| 85 | =head2 Things Still to Do | ||
| 86 | |||
| 87 | =over | ||
| 88 | |||
| 89 | =item * | ||
| 90 | |||
| 91 | The module now uses the new Collection.pm system. However, | ||
| 92 | Annotations associated with a Feature object still seem to use the | ||
| 93 | old system, so parsing with the old methods are included.. | ||
| 94 | |||
| 95 | =item * | ||
| 96 | |||
| 97 | Generate Seq objects with no sequence data but an assigned | ||
| 98 | length. This appears to be an issue with Bio::Seq. It is possible | ||
| 99 | (and reasonable) to make a BSML document with features but no | ||
| 100 | sequence data. | ||
| 101 | |||
| 102 | =item * | ||
| 103 | |||
| 104 | Support C<Seq-data-import>. Do not know how commonly this is used. | ||
| 105 | |||
| 106 | =item * | ||
| 107 | |||
| 108 | Some features are awaiting implementation in later versions of | ||
| 109 | BSML. These include: | ||
| 110 | |||
| 111 | =over | ||
| 112 | |||
| 113 | =item * | ||
| 114 | |||
| 115 | Nested feature support | ||
| 116 | |||
| 117 | =item * | ||
| 118 | |||
| 119 | Complex feature (ie joins) | ||
| 120 | |||
| 121 | =item * | ||
| 122 | |||
| 123 | Unambiguity in strand (ie -1,0,1, not just 'complement' ) | ||
| 124 | |||
| 125 | =item * | ||
| 126 | |||
| 127 | More friendly dblink structures | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =item * | ||
| 132 | |||
| 133 | Location.pm (or RangeI::union?) appears to have a bug when 'expand' | ||
| 134 | is used. | ||
| 135 | |||
| 136 | =item * | ||
| 137 | |||
| 138 | More intelligent hunting for sequence and feature titles? It is not | ||
| 139 | terribly clear where the most appropriate field is located, better | ||
| 140 | grepping (eg looking for a reasonable count for spaces and numbers) | ||
| 141 | may allow for titles better than "AE008041". | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =head1 AUTHOR - Charles Tilford | ||
| 146 | |||
| 147 | Bristol-Myers Squibb Bioinformatics | ||
| 148 | |||
| 149 | Email tilfordc@bms.com | ||
| 150 | |||
| 151 | I have developed the BSML specific code for this package, but have used | ||
| 152 | code from other SeqIO packages for much of the nuts-and-bolts. In particular | ||
| 153 | I have used code from the embl.pm module either directly or as a framework | ||
| 154 | for many of the subroutines that are common to SeqIO modules. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | package Bio::SeqIO::bsml; | ||
| 159 | $Bio::SeqIO::bsml::VERSION = '1.7.8'; | ||
| 160 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 161 | |||
| 162 | 1 | ▶ | use Bio::SeqFeature::Generic; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 163 | 1 | ▶ | use Bio::Species; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 164 | 0 | ▶ | use XML::DOM; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 165 | use Bio::Seq::SeqFactory; | ||
| 166 | use Bio::Annotation::Collection; | ||
| 167 | use Bio::Annotation::Comment; | ||
| 168 | use Bio::Annotation::Reference; | ||
| 169 | use Bio::Annotation::DBLink; | ||
| 170 | |||
| 171 | use base qw(Bio::SeqIO); | ||
| 172 | |||
| 173 | my $idcounter = {}; # Used to generate unique id values | ||
| 174 | my $nvtoken = ": "; # The token used if a name/value pair has to be stuffed | ||
| 175 | # into a single line | ||
| 176 | |||
| 177 | =head1 METHODS | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | # LS: this seems to get overwritten on line 1317, generating a redefinition error. | ||
| 182 | # Dead code? | ||
| 183 | # CAT: This was inappropriately added in revision 1.10 - I added the check for | ||
| 184 | # existence of a sequence factory to the actual _initialize | ||
| 185 | # sub _initialize { | ||
| 186 | # my($self,@args) = @_; | ||
| 187 | # $self->SUPER::_initialize(@args); | ||
| 188 | # if( ! defined $self->sequence_factory ) { | ||
| 189 | # $self->sequence_factory(Bio::Seq::SeqFactory->new(-verbose => $self->verbose(), | ||
| 190 | # -type => 'Bio::Seq::RichSeq')); | ||
| 191 | # } | ||
| 192 | # } | ||
| 193 | |||
| 194 | =head2 next_seq | ||
| 195 | |||
| 196 | Title : next_seq | ||
| 197 | Usage : my $bioSeqObj = $stream->next_seq | ||
| 198 | Function: Retrieves the next sequence from a SeqIO::bsml stream. | ||
| 199 | Returns : A reference to a Bio::Seq::RichSeq object | ||
| 200 | Args : | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub next_seq { | ||
| 205 | my $self = shift; | ||
| 206 | my ($desc); | ||
| 207 | my $bioSeq = $self->sequence_factory->create(-verbose =>$self->verbose()); | ||
| 208 | |||
| 209 | unless (exists $self->{'domtree'}) { | ||
| 210 | $self->throw("A BSML document has not yet been parsed."); | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | my $dom = $self->{'domtree'}; | ||
| 214 | my $seqElements = $dom->getElementsByTagName ("Sequence"); | ||
| 215 | if ($self->{'current_node'} == $seqElements->getLength ) { | ||
| 216 | # There are no more <Sequence>s to process | ||
| 217 | return; | ||
| 218 | } | ||
| 219 | my $xmlSeq = $seqElements->item($self->{'current_node'}); | ||
| 220 | |||
| 221 | # Assume that title attribute contains the best display id | ||
| 222 | if (my $val = $xmlSeq->getAttribute( "title")) { | ||
| 223 | $bioSeq->display_id($val); | ||
| 224 | } | ||
| 225 | |||
| 226 | # Set the molecule type | ||
| 227 | if (my $val = $xmlSeq->getAttribute( "molecule" )) { | ||
| 228 | my %mol = ('dna' => 'DNA', 'rna' => 'RNA', 'aa' => 'protein'); | ||
| 229 | $bioSeq->molecule($mol{ lc($val) }); | ||
| 230 | } | ||
| 231 | |||
| 232 | # Set the accession number | ||
| 233 | if (my $val = $xmlSeq->getAttribute( "ic-acckey" )) { | ||
| 234 | $bioSeq->accession_number($val); | ||
| 235 | } | ||
| 236 | |||
| 237 | # Get the sequence data for the element | ||
| 238 | if (my $seqData = &FIRSTDATA($xmlSeq->getElementsByTagName("Seq-data") | ||
| 239 | ->item(0) ) ) { | ||
| 240 | # Sequence data exists, transfer to the Seq object | ||
| 241 | # Remove white space and CRs (not neccesary?) | ||
| 242 | $seqData =~ s/[\s\n\r]//g; | ||
| 243 | $bioSeq->seq($seqData); | ||
| 244 | } elsif (my $import = $xmlSeq->getElementsByTagName("Seq-dataimport") | ||
| 245 | ->item(0) ) { | ||
| 246 | #>>>> # What about <Seq-data-import> ?? | ||
| 247 | |||
| 248 | } elsif (my $val = $xmlSeq->getAttribute("length")) { | ||
| 249 | # No sequence defined, set the length directly | ||
| 250 | |||
| 251 | #>>>> # This does not appear to work - length is apparently calculated | ||
| 252 | # from the sequence. How to make a "virtual" sequence??? Such | ||
| 253 | # creatures are common in BSML... | ||
| 254 | $bioSeq->length($val); | ||
| 255 | } | ||
| 256 | |||
| 257 | my $species = Bio::Species->new(); | ||
| 258 | my @classification = (); | ||
| 259 | |||
| 260 | # Peruse the generic <Attributes> - those that are direct children of | ||
| 261 | # the <Sequence> or the <Feature-tables> element | ||
| 262 | # Sticky wicket here - data not controlled by schema, could be anything | ||
| 263 | my @seqDesc = (); | ||
| 264 | my %specs = ('common_name' => 'y', | ||
| 265 | 'genus' => 'y', | ||
| 266 | 'species' => 'y', | ||
| 267 | 'sub_species' => 'y', | ||
| 268 | ); | ||
| 269 | my %seqMap = ( | ||
| 270 | 'add_date' => [ qw(date date-created date-last-updated)], | ||
| 271 | 'keywords' => [ 'keyword', ], | ||
| 272 | 'seq_version' => [ 'version' ], | ||
| 273 | 'division' => [ 'division' ], | ||
| 274 | 'add_secondary_accession' => ['accession'], | ||
| 275 | 'pid' => ['pid'], | ||
| 276 | 'primary_id' => [ 'primary.id', 'primary_id' ], | ||
| 277 | ); | ||
| 278 | my @links; | ||
| 279 | my $floppies = &GETFLOPPIES($xmlSeq); | ||
| 280 | for my $attr (@{$floppies}) { | ||
| 281 | # Don't want to get attributes from <Feature> or <Table> elements yet | ||
| 282 | my $parent = $attr->getParentNode->getNodeName; | ||
| 283 | next unless($parent eq "Sequence" || $parent eq "Feature-tables"); | ||
| 284 | |||
| 285 | my ($name, $content) = &FLOPPYVALS($attr); | ||
| 286 | $name = lc($name); | ||
| 287 | if (exists $specs{$name}) { # It looks like part of species... | ||
| 288 | $species->$name($content); | ||
| 289 | next; | ||
| 290 | } | ||
| 291 | my $value = ""; | ||
| 292 | # Cycle through the Seq methods: | ||
| 293 | for my $method (keys %seqMap) { | ||
| 294 | # Cycle through potential matching attributes: | ||
| 295 | for my $match (@{$seqMap{$method}}) { | ||
| 296 | # If the <Attribute> name matches one of the keys, | ||
| 297 | # set $value, unless it has already been set | ||
| 298 | $value ||= $content if ($name =~ /$match/i); | ||
| 299 | } | ||
| 300 | if ($value ne "") { | ||
| 301 | |||
| 302 | if( $method eq 'seq_version'&& $value =~ /\S+\.(\d+)/ ) { | ||
| 303 | # hack for the fact that data in version is actually | ||
| 304 | # ACCESSION.VERSION | ||
| 305 | ($value) = $1; | ||
| 306 | } | ||
| 307 | $bioSeq->$method($value); | ||
| 308 | last; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | if( $name eq 'database-xref' ) { | ||
| 312 | my ($link_id,$link_db) = split(/:/,$value); | ||
| 313 | push @links, Bio::Annotation::DBLink->new(-primary_id => $link_id, | ||
| 314 | -database => $link_db); | ||
| 315 | } | ||
| 316 | next if ($value ne ""); | ||
| 317 | |||
| 318 | if ($name =~ /^species$/i) { # Uh, it's the species designation? | ||
| 319 | if ($content =~ / /) { | ||
| 320 | # Assume that a full species name has been provided | ||
| 321 | # This will screw up if the last word is the subspecies... | ||
| 322 | my @break = split " ", $content; | ||
| 323 | @classification = reverse @break; | ||
| 324 | } else { | ||
| 325 | $classification[0] = $content; | ||
| 326 | } | ||
| 327 | next; | ||
| 328 | } | ||
| 329 | if ($name =~ /sub[_ ]?species/i) { # Should be the subspecies... | ||
| 330 | $species->sub_species( $content ); | ||
| 331 | next; | ||
| 332 | } | ||
| 333 | if ($name =~ /classification/i) { # Should be species classification | ||
| 334 | # We will assume that there are spaces separating the terms: | ||
| 335 | my @bits = split " ", $content; | ||
| 336 | # Now make sure there is not other cruft as well (eg semi-colons) | ||
| 337 | for my $i (0..$#bits) { | ||
| 338 | $bits[$i] =~ /(\w+)/; | ||
| 339 | $bits[$i] = $1; | ||
| 340 | } | ||
| 341 | $species->classification( @bits ); | ||
| 342 | next; | ||
| 343 | } | ||
| 344 | if ($name =~ /comment/) { | ||
| 345 | my $com = Bio::Annotation::Comment->new('-text' => $content); | ||
| 346 | # $bioSeq->annotation->add_Comment($com); | ||
| 347 | $bioSeq->annotation->add_Annotation('comment', $com); | ||
| 348 | next; | ||
| 349 | } | ||
| 350 | # Description line - collect all descriptions for later assembly | ||
| 351 | if ($name =~ /descr/) { | ||
| 352 | push @seqDesc, $content; | ||
| 353 | next; | ||
| 354 | } | ||
| 355 | # Ok, we have no idea what this attribute is. Dump to SimpleValue | ||
| 356 | my $simp = Bio::Annotation::SimpleValue->new( -value => $content); | ||
| 357 | $bioSeq->annotation->add_Annotation($name, $simp); | ||
| 358 | } | ||
| 359 | unless ($#seqDesc < 0) { | ||
| 360 | $bioSeq->desc( join "; ", @seqDesc); | ||
| 361 | } | ||
| 362 | |||
| 363 | #>>>> This should be modified so that any IDREF associated with the | ||
| 364 | # <Reference> is then used to associate the reference with the | ||
| 365 | # appropriate Feature | ||
| 366 | |||
| 367 | # Extract out <Reference>s associated with the sequence | ||
| 368 | my @refs; | ||
| 369 | my %tags = ( | ||
| 370 | -title => "RefTitle", | ||
| 371 | -authors => "RefAuthors", | ||
| 372 | -location => "RefJournal", | ||
| 373 | ); | ||
| 374 | for my $ref ( $xmlSeq->getElementsByTagName ("Reference") ) { | ||
| 375 | my %refVals; | ||
| 376 | for my $tag (keys %tags) { | ||
| 377 | my $rt = &FIRSTDATA($ref->getElementsByTagName($tags{$tag})->item(0)); | ||
| 378 | next unless ($rt); | ||
| 379 | $rt =~ s/^[\s\r\n]+//; # Kill leading space | ||
| 380 | $rt =~ s/[\s\r\n]+$//; # Kill trailing space | ||
| 381 | $rt =~ s/[\s\r\n]+/ /; # Collapse internal space runs | ||
| 382 | $refVals{$tag} = $rt; | ||
| 383 | } | ||
| 384 | my $reference = Bio::Annotation::Reference->new( %refVals ); | ||
| 385 | |||
| 386 | # Pull out any <Reference> information hidden in <Attributes> | ||
| 387 | my %refMap = ( | ||
| 388 | comment => [ 'comment', 'remark' ], | ||
| 389 | medline => [ 'medline', ], | ||
| 390 | pubmed => [ 'pubmed' ], | ||
| 391 | start => [ 'start', 'begin' ], | ||
| 392 | end => [ 'stop', 'end' ], | ||
| 393 | ); | ||
| 394 | my @refCom = (); | ||
| 395 | my $floppies = &GETFLOPPIES($ref); | ||
| 396 | for my $attr (@{$floppies}) { | ||
| 397 | my ($name, $content) = &FLOPPYVALS($attr); | ||
| 398 | my $value = ""; | ||
| 399 | # Cycle through the Seq methods: | ||
| 400 | for my $method (keys %refMap) { | ||
| 401 | # Cycle through potential matching attributes: | ||
| 402 | for my $match (@{$refMap{$method}}) { | ||
| 403 | # If the <Attribute> name matches one of the keys, | ||
| 404 | # set $value, unless it has already been set | ||
| 405 | $value ||= $content if ($name =~ /$match/i); | ||
| 406 | } | ||
| 407 | if ($value ne "") { | ||
| 408 | my $str = '$reference->' . $method . "($value)"; | ||
| 409 | eval($str); | ||
| 410 | next; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | next if ($value ne ""); | ||
| 414 | # Don't know what the <Attribute> is, dump it to comments: | ||
| 415 | push @refCom, $name . $nvtoken . $content; | ||
| 416 | } | ||
| 417 | unless ($#refCom < 0) { | ||
| 418 | # Random stuff was found, tack it to the comment field | ||
| 419 | my $exist = $reference->comment; | ||
| 420 | $exist .= join ", ", @refCom; | ||
| 421 | $reference->comment($exist); | ||
| 422 | } | ||
| 423 | push @refs, $reference; | ||
| 424 | } | ||
| 425 | $bioSeq->annotation->add_Annotation('reference' => $_) for @refs; | ||
| 426 | my $ann_col = $bioSeq->annotation; | ||
| 427 | # Extract the <Feature>s for this <Sequence> | ||
| 428 | for my $feat ( $xmlSeq->getElementsByTagName("Feature") ) { | ||
| 429 | $bioSeq->add_SeqFeature( $self->_parse_bsml_feature($feat) ); | ||
| 430 | } | ||
| 431 | |||
| 432 | $species->classification( @classification ); | ||
| 433 | $bioSeq->species( $species ); | ||
| 434 | |||
| 435 | $bioSeq->annotation->add_Annotation('dblink' => $_) for @links; | ||
| 436 | |||
| 437 | $self->{'current_node'}++; | ||
| 438 | return $bioSeq; | ||
| 439 | } | ||
| 440 | #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 441 | # Get all the <Attribute> and <Qualifier> children for an object, and | ||
| 442 | # return them as an array reference | ||
| 443 | # ('floppy' since these elements have poor/no schema control) | ||
| 444 | sub GETFLOPPIES { | ||
| 445 | my $obj = shift; | ||
| 446 | |||
| 447 | my @floppies; | ||
| 448 | my $attributes = $obj->getElementsByTagName ("Attribute"); | ||
| 449 | for (my $i = 0; $i < $attributes->getLength; $i++) { | ||
| 450 | push @floppies, $attributes->item($i); | ||
| 451 | } | ||
| 452 | my $qualifiers = $obj->getElementsByTagName ("Qualifier"); | ||
| 453 | for (my $i = 0; $i < $qualifiers->getLength; $i++) { | ||
| 454 | push @floppies, $qualifiers->item($i); | ||
| 455 | } | ||
| 456 | return \@floppies; | ||
| 457 | } | ||
| 458 | #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 459 | # Given a DOM <Attribute> or <Qualifier> object, return the [name, value] pair | ||
| 460 | sub FLOPPYVALS { | ||
| 461 | my $obj = shift; | ||
| 462 | |||
| 463 | my ($name, $value); | ||
| 464 | if ($obj->getNodeName eq "Attribute") { | ||
| 465 | $name = $obj->getAttribute('name'); | ||
| 466 | $value = $obj->getAttribute('content'); | ||
| 467 | } elsif ($obj->getNodeName eq "Qualifier") { | ||
| 468 | # Wheras <Attribute>s require both 'name' and 'content' attributes, | ||
| 469 | # <Qualifier>s can technically have either blank (and sometimes do) | ||
| 470 | my $n = $obj->getAttribute('value-type'); | ||
| 471 | $name = $n if ($n ne ""); | ||
| 472 | my $v = $obj->getAttribute('value'); | ||
| 473 | $value = $v if ($v ne ""); | ||
| 474 | } | ||
| 475 | return ($name, $value); | ||
| 476 | } | ||
| 477 | #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 478 | # Returns the value of the first TEXT_NODE encountered below an element | ||
| 479 | # Rational - avoid grabbing a comment rather than the PCDATA. Not foolproof... | ||
| 480 | sub FIRSTDATA { | ||
| 481 | my $element = shift; | ||
| 482 | return unless ($element); | ||
| 483 | |||
| 484 | my $hopefuls = $element->getChildNodes; | ||
| 485 | my $data; | ||
| 486 | for (my $i = 0; $i < $hopefuls->getLength; $i++) { | ||
| 487 | if ($hopefuls->item($i)->getNodeType == | ||
| 488 | XML::DOM::Node::TEXT_NODE() ) { | ||
| 489 | $data = $hopefuls->item($i)->getNodeValue; | ||
| 490 | last; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | return $data; | ||
| 494 | } | ||
| 495 | #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 496 | # Just collapses whitespace runs in a string | ||
| 497 | sub STRIP { | ||
| 498 | my $string = shift; | ||
| 499 | $string =~ s/[\s\r\n]+/ /g; | ||
| 500 | return $string; | ||
| 501 | } | ||
| 502 | |||
| 503 | =head2 to_bsml | ||
| 504 | |||
| 505 | Title : to_bsml | ||
| 506 | Usage : my $domDoc = $obj->to_bsml(@args) | ||
| 507 | Function: Generates an XML structure for one or more Bio::Seq objects. | ||
| 508 | If $seqref is an array ref, the XML tree generated will include | ||
| 509 | all the sequences in the array. | ||
| 510 | Returns : A reference to the XML DOM::Document object generated / modified | ||
| 511 | Args : Argument array in form of -key => val. Recognized keys: | ||
| 512 | |||
| 513 | -seq A Bio::Seq reference, or an array reference of many of them | ||
| 514 | |||
| 515 | -xmldoc Specifies an existing XML DOM document to add the sequences | ||
| 516 | to. If included, then only data (no page formatting) will | ||
| 517 | be added. If not, a new XML::DOM::Document will be made, | ||
| 518 | and will be populated with both <Sequence> data, as well as | ||
| 519 | <Page> display elements. | ||
| 520 | |||
| 521 | -nodisp Do not generate <Display> elements, or any children | ||
| 522 | thereof, even if -xmldoc is not set. | ||
| 523 | |||
| 524 | -skipfeat If set to 'all', all <Feature>s will be skipped. If it is | ||
| 525 | a hash reference, any <Feature> with a class matching a key | ||
| 526 | in the hash will be skipped - for example, to skip 'source' | ||
| 527 | and 'score' features, use: | ||
| 528 | |||
| 529 | -skipfeat => { source => 'Y', score => 'Y' } | ||
| 530 | |||
| 531 | -skiptags As above: if set to 'all', no tags are included, and if a | ||
| 532 | hash reference, those specific tags will be ignored. | ||
| 533 | |||
| 534 | Skipping some or all tags and features can result in | ||
| 535 | noticeable speed improvements. | ||
| 536 | |||
| 537 | -nodata If true, then <Seq-data> will not be included. This may be | ||
| 538 | useful if you just want annotations and do not care about | ||
| 539 | the raw ACTG information. | ||
| 540 | |||
| 541 | -return Default is 'xml', which will return a reference to the BSML | ||
| 542 | XML object. If set to 'seq' will return an array ref of the | ||
| 543 | <Sequence> objects added (rather than the whole XML object) | ||
| 544 | |||
| 545 | -close Early BSML browsers will crash if an element *could* have | ||
| 546 | children but does not, and is closed as an empty element | ||
| 547 | e.g. <Styles/>. If -close is true, then such tags are given | ||
| 548 | a comment child to explicitly close them e.g. <Styles><!-- | ||
| 549 | --></Styles>. This is default true, set to "0" if you do | ||
| 550 | not want this behavior. | ||
| 551 | |||
| 552 | Examples : my $domObj = $stream->to_bsml( -seq => \@fourCoolSequenceObjects, | ||
| 553 | -skipfeat => { source => 1 }, | ||
| 554 | ); | ||
| 555 | |||
| 556 | # Or add sequences to an existing BSML document: | ||
| 557 | $stream->to_bsml(-seq => \@fourCoolSequenceObjects, | ||
| 558 | -skipfeat => { source => 1 }, | ||
| 559 | -xmldoc => $myBsmlDocumentInProgress, ); | ||
| 560 | |||
| 561 | =cut | ||
| 562 | |||
| 563 | sub to_bsml { | ||
| 564 | my $self = shift; | ||
| 565 | my $args = $self->_parseparams( -close => 1, | ||
| 566 | -return => 'xml', | ||
| 567 | @_); | ||
| 568 | $args->{NODISP} ||= $args->{NODISPLAY}; | ||
| 569 | my $seqref = $args->{SEQ}; | ||
| 570 | $seqref = (ref($seqref) eq 'ARRAY') ? $seqref : [ $seqref ]; | ||
| 571 | |||
| 572 | ############################# | ||
| 573 | # Basic BSML XML Components # | ||
| 574 | ############################# | ||
| 575 | |||
| 576 | my $xml; | ||
| 577 | my ($bsmlElem, $defsElem, $seqsElem, $dispElem); | ||
| 578 | if ($args->{XMLDOC}) { | ||
| 579 | # The user has provided an existing XML DOM object | ||
| 580 | $xml = $args->{XMLDOC}; | ||
| 581 | unless ($xml->isa("XML::DOM::Document")) { | ||
| 582 | $self->throw('SeqIO::bsml.pm error:\n'. | ||
| 583 | 'When calling ->to_bsml( { xmldoc => $myDoc }), $myDoc \n' . | ||
| 584 | 'should be an XML::DOM::Document object, or an object that\n'. | ||
| 585 | 'inherits from that class (like BsmlHelper.pm)'); | ||
| 586 | } | ||
| 587 | } else { | ||
| 588 | # The user has not provided a new document, make one from scratch | ||
| 589 | $xml = XML::DOM::Document->new(); | ||
| 590 | $xml->setXMLDecl( $xml->createXMLDecl("1.0") ); | ||
| 591 | my $url = "http://www.labbook.com/dtd/bsml2_2.dtd"; | ||
| 592 | my $doc = $xml->createDocumentType("Bsml",$url); | ||
| 593 | $xml->setDoctype($doc); | ||
| 594 | $bsmlElem = $self->_addel( $xml, 'Bsml'); | ||
| 595 | $defsElem = $self->_addel( $bsmlElem, 'Definitions'); | ||
| 596 | $seqsElem = $self->_addel( $defsElem, 'Sequences'); | ||
| 597 | unless ($args->{NODISP}) { | ||
| 598 | $dispElem = $self->_addel( $bsmlElem, 'Display'); | ||
| 599 | my $stylElem = $self->_addel( $dispElem, 'Styles'); | ||
| 600 | my $style = $self->_addel( $stylElem, 'Style', { | ||
| 601 | type => "text/css" }); | ||
| 602 | my $styleText = | ||
| 603 | qq(Interval-widget { display : "1"; }\n) . | ||
| 604 | qq(Feature { display-auto : "1"; }); | ||
| 605 | $style->appendChild( $xml->createTextNode($styleText) ); | ||
| 606 | } | ||
| 607 | } | ||
| 608 | |||
| 609 | # Establish fundamental BSML elements, if they do not already exist | ||
| 610 | $bsmlElem ||= $xml->getElementsByTagName("Bsml")->item(0); | ||
| 611 | $defsElem ||= $xml->getElementsByTagName("Definitions")->item(0); | ||
| 612 | $seqsElem ||= $xml->getElementsByTagName("Sequences")->item(0); | ||
| 613 | |||
| 614 | ############### | ||
| 615 | # <Sequences> # | ||
| 616 | ############### | ||
| 617 | |||
| 618 | # Map over Bio::Seq to BSML | ||
| 619 | my %mol = ('dna' => 'DNA', 'rna' => 'RNA', 'protein' => 'AA'); | ||
| 620 | my @xmlSequences; | ||
| 621 | |||
| 622 | for my $bioSeq (@{$seqref}) { | ||
| 623 | my $xmlSeq = $xml->createElement("Sequence"); | ||
| 624 | my $FTs = $xml->createElement("Feature-tables"); | ||
| 625 | |||
| 626 | # Array references to hold <Reference> objects: | ||
| 627 | my $seqRefs = []; my $featRefs = []; | ||
| 628 | # Array references to hold <Attribute> values (not objects): | ||
| 629 | my $seqDesc = []; | ||
| 630 | push @{$seqDesc}, | ||
| 631 | [ | ||
| 632 | "comment", | ||
| 633 | "This file generated to BSML 2.2 standards - " . | ||
| 634 | "joins will be collapsed to a single feature enclosing all members of the join" | ||
| 635 | ]; | ||
| 636 | push @{$seqDesc}, ["description" , eval{$bioSeq->desc}]; | ||
| 637 | for my $kwd ( eval{$bioSeq->get_keywords} ) { | ||
| 638 | push @{$seqDesc}, ["keyword" , $kwd]; | ||
| 639 | } | ||
| 640 | push @{$seqDesc}, ["keyword" , eval{$bioSeq->keywords}]; | ||
| 641 | push @{$seqDesc}, ["version" , eval{ | ||
| 642 | join(".", $bioSeq->accession_number, $bioSeq->seq_version); }]; | ||
| 643 | push @{$seqDesc}, ["division" , eval{$bioSeq->division}]; | ||
| 644 | push @{$seqDesc}, ["pid" , eval{$bioSeq->pid}]; | ||
| 645 | # push @{$seqDesc}, ["bio_object" , ref($bioSeq)]; | ||
| 646 | push @{$seqDesc}, ["primary_id" , eval{$bioSeq->primary_id}]; | ||
| 647 | for my $dt (eval{$bioSeq->get_dates()} ) { | ||
| 648 | push @{$seqDesc}, ["date" , $dt]; | ||
| 649 | } | ||
| 650 | for my $ac (eval{$bioSeq->get_secondary_accessions()} ) { | ||
| 651 | push @{$seqDesc}, ["secondary_accession" , $ac]; | ||
| 652 | } | ||
| 653 | |||
| 654 | # Determine the accession number and a unique identifier | ||
| 655 | my $acc = $bioSeq->accession_number eq "unknown" ? | ||
| 656 | "" : $bioSeq->accession_number; | ||
| 657 | my $id; | ||
| 658 | my $pi = $bioSeq->primary_id; | ||
| 659 | if ($pi && $pi !~ /Bio::/) { | ||
| 660 | # Not sure I understand what primary_id is... It sometimes | ||
| 661 | # is a string describing a reference to a BioSeq object... | ||
| 662 | $id = "SEQ" . $bioSeq->primary_id; | ||
| 663 | } else { | ||
| 664 | # Nothing useful found, make a new unique ID | ||
| 665 | $id = $acc || ("SEQ-io" . $idcounter->{Sequence}++); | ||
| 666 | } | ||
| 667 | # print "$id->",ref($bioSeq->primary_id),"\n"; | ||
| 668 | # An id field with spaces is interpreted as an idref - kill the spaces | ||
| 669 | $id =~ s/ /-/g; | ||
| 670 | # Map over <Sequence> attributes | ||
| 671 | my %attr = ( 'title' => $bioSeq->display_id, | ||
| 672 | 'length' => $bioSeq->length, | ||
| 673 | 'ic-acckey' => $acc, | ||
| 674 | 'id' => $id, | ||
| 675 | 'representation' => 'raw', | ||
| 676 | ); | ||
| 677 | $attr{molecule} = $mol{ lc($bioSeq->molecule) } if $bioSeq->can('molecule'); | ||
| 678 | |||
| 679 | |||
| 680 | for my $a (keys %attr) { | ||
| 681 | $xmlSeq->setAttribute($a, $attr{$a}) if (defined $attr{$a} && | ||
| 682 | $attr{$a} ne ""); | ||
| 683 | } | ||
| 684 | # Orphaned Attributes: | ||
| 685 | $xmlSeq->setAttribute('topology', 'circular') | ||
| 686 | if ($bioSeq->is_circular); | ||
| 687 | # <Sequence> strand, locus | ||
| 688 | |||
| 689 | $self->_add_page($xml, $xmlSeq) if ($dispElem); | ||
| 690 | ################ | ||
| 691 | # <Attributes> # | ||
| 692 | ################ | ||
| 693 | |||
| 694 | # Check for Bio::Annotations on the * <Sequence> *. | ||
| 695 | $self->_parse_annotation( -xml => $xml, -obj => $bioSeq, | ||
| 696 | -desc => $seqDesc, -refs => $seqRefs); | ||
| 697 | |||
| 698 | # Incorporate species data | ||
| 699 | if (ref($bioSeq->species) eq 'Bio::Species') { | ||
| 700 | # Need to peer into Bio::Species ... | ||
| 701 | my @specs = ('common_name', 'genus', 'species', 'sub_species'); | ||
| 702 | for my $sp (@specs) { | ||
| 703 | next unless (my $val = $bioSeq->species()->$sp()); | ||
| 704 | push @{$seqDesc}, [$sp , $val]; | ||
| 705 | } | ||
| 706 | push @{$seqDesc}, ['classification', | ||
| 707 | (join " ", $bioSeq->species->classification) ]; | ||
| 708 | # Species::binomial will return "genus species sub_species" ... | ||
| 709 | } elsif (my $val = $bioSeq->species) { | ||
| 710 | # Ok, no idea what it is, just dump it in there... | ||
| 711 | push @{$seqDesc}, ["species", $val]; | ||
| 712 | } | ||
| 713 | |||
| 714 | # Add the description <Attribute>s for the <Sequence> | ||
| 715 | for my $seqD (@{$seqDesc}) { | ||
| 716 | $self->_addel($xmlSeq, "Attribute", { | ||
| 717 | name => $seqD->[0], content => $seqD->[1]}) if ($seqD->[1]); | ||
| 718 | } | ||
| 719 | |||
| 720 | # If sequence references were added, make a Feature-table for them | ||
| 721 | unless ($#{$seqRefs} < 0) { | ||
| 722 | my $seqFT = $self->_addel($FTs, "Feature-table", { | ||
| 723 | title => "Sequence References", }); | ||
| 724 | for my $feat (@{$seqRefs}) { | ||
| 725 | $seqFT->appendChild($feat); | ||
| 726 | } | ||
| 727 | } | ||
| 728 | |||
| 729 | # This is the appropriate place to add <Feature-tables> | ||
| 730 | $xmlSeq->appendChild($FTs); | ||
| 731 | |||
| 732 | ############# | ||
| 733 | # <Feature> # | ||
| 734 | ############# | ||
| 735 | |||
| 736 | #>>>> # Perhaps it is better to loop through top_Seqfeatures?... | ||
| 737 | #>>>> # ...however, BSML does not have a hierarchy for Features | ||
| 738 | |||
| 739 | if (defined $args->{SKIPFEAT} && | ||
| 740 | $args->{SKIPFEAT} eq 'all') { | ||
| 741 | $args->{SKIPFEAT} = { all => 1}; | ||
| 742 | } else { $args->{SKIPFEAT} ||= {} } | ||
| 743 | for my $class (keys %{$args->{SKIPFEAT}}) { | ||
| 744 | $args->{SKIPFEAT}{lc($class)} = $args->{SKIPFEAT}{$class}; | ||
| 745 | } | ||
| 746 | # Loop through all the features | ||
| 747 | my @features = $bioSeq->all_SeqFeatures(); | ||
| 748 | if (@features && !$args->{SKIPFEAT}{all}) { | ||
| 749 | my $ft = $self->_addel($FTs, "Feature-table", { | ||
| 750 | title => "Features", }); | ||
| 751 | for my $bioFeat (@features ) { | ||
| 752 | my $featDesc = []; | ||
| 753 | my $class = lc($bioFeat->primary_tag); | ||
| 754 | # The user may have specified to ignore this type of feature | ||
| 755 | next if ($args->{SKIPFEAT}{$class}); | ||
| 756 | my $id = "FEAT-io" . $idcounter->{Feature}++; | ||
| 757 | my $xmlFeat = $self->_addel( $ft, 'Feature', { | ||
| 758 | 'id' => $id, | ||
| 759 | 'class' => $class , | ||
| 760 | 'value-type' => $bioFeat->source_tag }); | ||
| 761 | # Check for Bio::Annotations on the * <Feature> *. | ||
| 762 | $self->_parse_annotation( -xml => $xml, -obj => $bioFeat, | ||
| 763 | -desc => $featDesc, -id => $id, | ||
| 764 | -refs =>$featRefs, ); | ||
| 765 | # Add the description stuff for the <Feature> | ||
| 766 | for my $de (@{$featDesc}) { | ||
| 767 | $self->_addel($xmlFeat, "Attribute", { | ||
| 768 | name => $de->[0], content => $de->[1]}) if ($de->[1]); | ||
| 769 | } | ||
| 770 | $self->_parse_location($xml, $xmlFeat, $bioFeat); | ||
| 771 | |||
| 772 | # loop through the tags, add them as <Qualifiers> | ||
| 773 | next if (defined $args->{SKIPTAGS} && | ||
| 774 | $args->{SKIPTAGS} =~ /all/i); | ||
| 775 | # Tags can consume a lot of CPU cycles, and can often be | ||
| 776 | # rather non-informative, so -skiptags can allow one total or | ||
| 777 | # selective omission of tags. | ||
| 778 | for my $tag ($bioFeat->all_tags()) { | ||
| 779 | next if (exists $args->{SKIPTAGS}{$tag}); | ||
| 780 | for my $val ($bioFeat->each_tag_value($tag)) { | ||
| 781 | $self->_addel( $xmlFeat, 'Qualifier', { | ||
| 782 | 'value-type' => $tag , | ||
| 783 | 'value' => $val }); | ||
| 784 | } | ||
| 785 | } | ||
| 786 | } | ||
| 787 | } | ||
| 788 | |||
| 789 | ############## | ||
| 790 | # <Seq-data> # | ||
| 791 | ############## | ||
| 792 | |||
| 793 | # Add sequence data | ||
| 794 | if ( (my $data = $bioSeq->seq) && !$args->{NODATA} ) { | ||
| 795 | my $d = $self->_addel($xmlSeq, 'Seq-data'); | ||
| 796 | $d->appendChild( $xml->createTextNode($data) ); | ||
| 797 | } | ||
| 798 | |||
| 799 | # If references were added, make a Feature-table for them | ||
| 800 | unless ($#{$featRefs} < 0) { | ||
| 801 | my $seqFT = $self->_addel($FTs, "Feature-table", { | ||
| 802 | title => "Feature References", }); | ||
| 803 | for my $feat (@{$featRefs}) { | ||
| 804 | $seqFT->appendChild($feat); | ||
| 805 | } | ||
| 806 | } | ||
| 807 | |||
| 808 | # Place the completed <Sequence> tree as a child of <Sequences> | ||
| 809 | $seqsElem->appendChild($xmlSeq); | ||
| 810 | push @xmlSequences, $xmlSeq; | ||
| 811 | } | ||
| 812 | |||
| 813 | # Prevent browser crashes by explicitly closing empty elements: | ||
| 814 | if ($args->{CLOSE}) { | ||
| 815 | my @problemChild = ('Sequences', 'Sequence', 'Feature-tables', | ||
| 816 | 'Feature-table', 'Screen', 'View',); | ||
| 817 | for my $kid (@problemChild) { | ||
| 818 | for my $prob ($xml->getElementsByTagName($kid)) { | ||
| 819 | unless ($prob->hasChildNodes) { | ||
| 820 | $prob->appendChild( | ||
| 821 | $xml->createComment(" Must close <$kid> explicitly ")); | ||
| 822 | } | ||
| 823 | } | ||
| 824 | } | ||
| 825 | } | ||
| 826 | |||
| 827 | if (defined $args->{RETURN} && | ||
| 828 | $args->{RETURN} =~ /seq/i) { | ||
| 829 | return \@xmlSequences; | ||
| 830 | } else { | ||
| 831 | return $xml; | ||
| 832 | } | ||
| 833 | } | ||
| 834 | |||
| 835 | =head2 write_seq | ||
| 836 | |||
| 837 | Title : write_seq | ||
| 838 | Usage : $obj->write_seq(@args) | ||
| 839 | Function: Prints out an XML structure for one or more Bio::Seq objects. | ||
| 840 | If $seqref is an array ref, the XML tree generated will include | ||
| 841 | all the sequences in the array. This method is fairly simple, | ||
| 842 | most of the processing is performed within to_bsml. | ||
| 843 | Returns : A reference to the XML object generated / modified | ||
| 844 | Args : Argument array. Recognized keys: | ||
| 845 | |||
| 846 | -seq A Bio::Seq reference, or an array reference of many of them | ||
| 847 | |||
| 848 | Alternatively, the method may be called simply as... | ||
| 849 | |||
| 850 | $obj->write_seq( $bioseq ) | ||
| 851 | |||
| 852 | ... if only a single argument is passed, it is assumed that | ||
| 853 | it is the sequence object (can also be an array ref of | ||
| 854 | many Seq objects ) | ||
| 855 | |||
| 856 | -printmime If true prints "Content-type: $mimetype\n\n" at top of | ||
| 857 | document, where $mimetype is the value designated by this | ||
| 858 | key. For generic XML use text/xml, for BSML use text/x-bsml | ||
| 859 | |||
| 860 | -return This option will be suppressed, since the nature of this | ||
| 861 | method is to print out the XML document. If you wish to | ||
| 862 | retrieve the <Sequence> objects generated, use the to_bsml | ||
| 863 | method directly. | ||
| 864 | |||
| 865 | =cut | ||
| 866 | |||
| 867 | sub write_seq { | ||
| 868 | my $self = shift; | ||
| 869 | my $args = $self->_parseparams( @_); | ||
| 870 | if ($#_ == 0 ) { | ||
| 871 | # If only a single value is passed, assume it is the seq object | ||
| 872 | unshift @_, "-seq"; | ||
| 873 | } | ||
| 874 | # Build a BSML XML DOM object based on the sequence(s) | ||
| 875 | my $xml = $self->to_bsml( @_, | ||
| 876 | -return => undef ); | ||
| 877 | # Convert to a string | ||
| 878 | my $out = $xml->toString; | ||
| 879 | # Print after putting a return after each element - more readable | ||
| 880 | $out =~ s/>/>\n/g; | ||
| 881 | $self->_print("Content-type: " . $args->{PRINTMIME} . "\n\n") | ||
| 882 | if ($args->{PRINTMIME}); | ||
| 883 | $self->_print( $out ); | ||
| 884 | # Return the DOM tree in case the user wants to do something with it | ||
| 885 | |||
| 886 | $self->flush if $self->_flush_on_write && defined $self->_fh; | ||
| 887 | return $xml; | ||
| 888 | } | ||
| 889 | |||
| 890 | =head1 INTERNAL METHODS | ||
| 891 | |||
| 892 | The following methods are used for internal processing, and should probably | ||
| 893 | not be accessed by the user. | ||
| 894 | |||
| 895 | =head2 _parse_location | ||
| 896 | |||
| 897 | Title : _parse_location | ||
| 898 | Usage : $obj->_parse_location($xmlDocument, $parentElem, $SeqFeatureObj) | ||
| 899 | Function: Adds <Interval-loc> and <Site-loc> children to <$parentElem> based | ||
| 900 | on locations / sublocations found in $SeqFeatureObj. If | ||
| 901 | sublocations exist, the original location will be ignored. | ||
| 902 | Returns : An array ref containing the elements added to the parent. | ||
| 903 | These will have already been added to <$parentElem> | ||
| 904 | Args : 0 The DOM::Document being modified | ||
| 905 | 1 The DOM::Element parent that you want to add to | ||
| 906 | 2 Reference to the Bio::SeqFeature being analyzed | ||
| 907 | |||
| 908 | =cut | ||
| 909 | |||
| 910 | ############################### | ||
| 911 | # <Interval-loc> & <Site-loc> # | ||
| 912 | ############################### | ||
| 913 | |||
| 914 | sub _parse_location { | ||
| 915 | my $self = shift; | ||
| 916 | my ($xml, $xmlFeat, $bioFeat) = @_; | ||
| 917 | my $bioLoc = $bioFeat->location; | ||
| 918 | my @locations; | ||
| 919 | if (ref($bioLoc) =~ /Split/) { | ||
| 920 | @locations = $bioLoc->sub_Location; | ||
| 921 | # BSML 2.2 does not recognize / support joins. For this reason, | ||
| 922 | # we will just use the upper-level location. The line below can | ||
| 923 | # be deleted or commented out if/when BSML 3 supports complex | ||
| 924 | # interval deffinitions: | ||
| 925 | @locations = ($bioLoc); | ||
| 926 | } else { | ||
| 927 | @locations = ($bioLoc); | ||
| 928 | } | ||
| 929 | my @added = (); | ||
| 930 | |||
| 931 | # Add the site or interval positional information: | ||
| 932 | for my $loc (@locations) { | ||
| 933 | my ($start, $end) = ($loc->start, $loc->end); | ||
| 934 | my %locAttr; | ||
| 935 | # Strand information is not well described in BSML | ||
| 936 | $locAttr{complement} = 1 if ($loc->strand == -1); | ||
| 937 | if ($start ne "" && ($start == $end || $end eq "")) { | ||
| 938 | $locAttr{sitepos} = $start; | ||
| 939 | push @added, $self->_addel($xmlFeat,'Site-loc',\%locAttr); | ||
| 940 | } elsif ($start ne "" && $end ne "") { | ||
| 941 | if ($start > $end) { | ||
| 942 | # The feature is on the complementary strand | ||
| 943 | ($start, $end) = ($end, $start); | ||
| 944 | $locAttr{complement} = 1; | ||
| 945 | } | ||
| 946 | $locAttr{startpos} = $start; | ||
| 947 | $locAttr{endpos} = $end; | ||
| 948 | push @added, $self->_addel($xmlFeat,'Interval-loc',\%locAttr); | ||
| 949 | } else { | ||
| 950 | warn "Failure to parse SeqFeature location. Start = '$start' & End = '$end'"; | ||
| 951 | } | ||
| 952 | } | ||
| 953 | return \@added; | ||
| 954 | } | ||
| 955 | |||
| 956 | =head2 _parse_bsml_feature | ||
| 957 | |||
| 958 | Title : _parse_bsml_feature | ||
| 959 | Usage : $obj->_parse_bsml_feature($xmlFeature ) | ||
| 960 | Function: Will examine the <Feature> element provided by $xmlFeature and | ||
| 961 | return a generic seq feature. | ||
| 962 | Returns : Bio::SeqFeature::Generic | ||
| 963 | Args : 0 XML::DOM::Element <Feature> being analyzed. | ||
| 964 | |||
| 965 | =cut | ||
| 966 | |||
| 967 | sub _parse_bsml_feature { | ||
| 968 | my $self = shift; | ||
| 969 | my ($feat) = @_; | ||
| 970 | |||
| 971 | my $basegsf = Bio::SeqFeature::Generic->new(); | ||
| 972 | # score | ||
| 973 | # frame | ||
| 974 | # source_tag | ||
| 975 | |||
| 976 | # Use the class as the primary tag value, if it is present | ||
| 977 | if ( my $val = $feat->getAttribute("class") ) { | ||
| 978 | $basegsf->primary_tag($val); | ||
| 979 | } | ||
| 980 | |||
| 981 | # Positional information is in <Interval-loc>s or <Site-loc>s | ||
| 982 | # We need to grab these in order, to try to recreate joins... | ||
| 983 | my @locations = (); | ||
| 984 | for my $kid ($feat->getChildNodes) { | ||
| 985 | my $nodeName = $kid->getNodeName; | ||
| 986 | next unless ($nodeName eq "Interval-loc" || | ||
| 987 | $nodeName eq "Site-loc"); | ||
| 988 | push @locations, $kid; | ||
| 989 | } | ||
| 990 | if ($#locations == 0) { | ||
| 991 | # There is only one location specified | ||
| 992 | $self->_parse_bsml_location($locations[0], $basegsf); | ||
| 993 | } elsif ($#locations > 0) { | ||
| 994 | #>>>> # This is not working, I think the error is somewhere downstream | ||
| 995 | # of add_sub_SeqFeature, probably in RangeI::union ? | ||
| 996 | # The sub features are added fine, but the EXPANDed parent feature | ||
| 997 | # location has a messed up start - Bio::SeqFeature::Generic ref | ||
| 998 | # instead of an integer - and an incorrect end - the end of the first | ||
| 999 | # sub feature added, not of the union of all of them. | ||
| 1000 | |||
| 1001 | # Also, the SeqIO::genbank.pm output is odd - the sub features appear | ||
| 1002 | # to be listed with the *previous* feature, not this one. | ||
| 1003 | |||
| 1004 | for my $location (@locations) { | ||
| 1005 | my $subgsf = $self->_parse_bsml_location($location); | ||
| 1006 | # print "start ", $subgsf->start,"\n"; | ||
| 1007 | # print "end ", $subgsf->end,"\n"; | ||
| 1008 | $basegsf->add_sub_SeqFeature($subgsf, 'EXPAND'); | ||
| 1009 | } | ||
| 1010 | # print $feat->getAttribute('id'),"\n"; | ||
| 1011 | # print $basegsf->primary_tag,"\n"; | ||
| 1012 | |||
| 1013 | } else { | ||
| 1014 | # What to do if there are no locations? Nothing needed? | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | # Look at any <Attribute>s or <Qualifier>s that are present: | ||
| 1018 | my $floppies = &GETFLOPPIES($feat); | ||
| 1019 | for my $attr (@{$floppies}) { | ||
| 1020 | my ($name, $content) = &FLOPPYVALS($attr); | ||
| 1021 | # Don't know what the object is, dump it to a tag: | ||
| 1022 | $basegsf->add_tag_value(lc($name), $content); | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | # Mostly this helps with debugging, but may be of utility... | ||
| 1026 | # Add a tag holding the BSML id value | ||
| 1027 | if ( (my $val = $feat->getAttribute('id')) && | ||
| 1028 | !$basegsf->has_tag('bsml-id')) { | ||
| 1029 | # Decided that this got a little sloppy... | ||
| 1030 | # $basegsf->add_tag_value("bsml-id", $val); | ||
| 1031 | } | ||
| 1032 | return $basegsf; | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | =head2 _parse_bsml_location | ||
| 1036 | |||
| 1037 | Title : _parse_bsml_location | ||
| 1038 | Usage : $obj->_parse_bsml_feature( $intOrSiteLoc, $gsfObject ) | ||
| 1039 | Function: Will examine the <Interval-loc> or <Site-loc> element provided | ||
| 1040 | Returns : Bio::SeqFeature::Generic | ||
| 1041 | Args : 0 XML::DOM::Element <Interval/Site-loc> being analyzed. | ||
| 1042 | 1 Optional SeqFeature::Generic to use | ||
| 1043 | |||
| 1044 | =cut | ||
| 1045 | |||
| 1046 | sub _parse_bsml_location { | ||
| 1047 | my $self = shift; | ||
| 1048 | my ($loc, $gsf) = @_; | ||
| 1049 | |||
| 1050 | $gsf ||= Bio::SeqFeature::Generic->new(); | ||
| 1051 | my $type = $loc->getNodeName; | ||
| 1052 | my ($start, $end); | ||
| 1053 | if ($type eq 'Interval-loc') { | ||
| 1054 | $start = $loc->getAttribute('startpos'); | ||
| 1055 | $end = $loc->getAttribute('endpos'); | ||
| 1056 | } elsif ($type eq 'Site-loc') { | ||
| 1057 | $start = $end = $loc->getAttribute('sitepos'); | ||
| 1058 | } else { | ||
| 1059 | warn "Unknown location type '$type', could not make GSF\n"; | ||
| 1060 | return; | ||
| 1061 | } | ||
| 1062 | $gsf->start($start); | ||
| 1063 | $gsf->end($end); | ||
| 1064 | |||
| 1065 | # BSML does not have an explicit method to set undefined strand | ||
| 1066 | if (my $s = $loc->getAttribute("complement")) { | ||
| 1067 | if ($s) { | ||
| 1068 | $gsf->strand(-1); | ||
| 1069 | } else { | ||
| 1070 | $gsf->strand(1); | ||
| 1071 | } | ||
| 1072 | } else { | ||
| 1073 | # We're setting "strand nonspecific" here - bad idea? | ||
| 1074 | # In most cases the user likely meant it to be on the + strand | ||
| 1075 | $gsf->strand(0); | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | return $gsf; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | =head2 _parse_reference | ||
| 1082 | |||
| 1083 | Title : _parse_reference | ||
| 1084 | Usage : $obj->_parse_reference(@args ) | ||
| 1085 | Function: Makes a new <Reference> object from a ::Reference, which is | ||
| 1086 | then stored in an array provide by -refs. It will be | ||
| 1087 | appended to the XML tree later. | ||
| 1088 | Returns : | ||
| 1089 | Args : Argument array. Recognized keys: | ||
| 1090 | |||
| 1091 | -xml The DOM::Document being modified | ||
| 1092 | |||
| 1093 | -refobj The Annotation::Reference Object | ||
| 1094 | |||
| 1095 | -refs An array reference to hold the new <Reference> DOM object | ||
| 1096 | |||
| 1097 | -id Optional. If the XML id for the 'calling' element is | ||
| 1098 | provided, it will be placed in any <Reference> refs | ||
| 1099 | attribute. | ||
| 1100 | |||
| 1101 | =cut | ||
| 1102 | |||
| 1103 | sub _parse_reference { | ||
| 1104 | my $self = shift; | ||
| 1105 | my $args = $self->_parseparams( @_); | ||
| 1106 | my ($xml, $ref, $refRef) = ($args->{XML}, $args->{REFOBJ}, $args->{REFS}); | ||
| 1107 | |||
| 1108 | ############### | ||
| 1109 | # <Reference> # | ||
| 1110 | ############### | ||
| 1111 | |||
| 1112 | my $xmlRef = $xml->createElement("Reference"); | ||
| 1113 | #>> This may not be the right way to make a BSML dbxref... | ||
| 1114 | if (my $link = $ref->medline) { | ||
| 1115 | $xmlRef->setAttribute('dbxref', $link); | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | # Make attributes for some of the characteristics | ||
| 1119 | my %stuff = ( start => $ref->start, | ||
| 1120 | end => $ref->end, | ||
| 1121 | rp => $ref->rp, | ||
| 1122 | comment => $ref->comment, | ||
| 1123 | pubmed => $ref->pubmed, | ||
| 1124 | ); | ||
| 1125 | for my $s (keys %stuff) { | ||
| 1126 | $self->_addel($xmlRef, "Attribute", { | ||
| 1127 | name => $s, content => $stuff{$s} }) if ($stuff{$s}); | ||
| 1128 | } | ||
| 1129 | $xmlRef->setAttribute('refs', $args->{ID}) if ($args->{ID}); | ||
| 1130 | # Add the basic information | ||
| 1131 | # Should probably check for content before creation... | ||
| 1132 | $self->_addel($xmlRef, "RefAuthors")-> | ||
| 1133 | appendChild( $xml->createTextNode(&STRIP($ref->authors)) ); | ||
| 1134 | $self->_addel($xmlRef, "RefTitle")-> | ||
| 1135 | appendChild( $xml->createTextNode(&STRIP($ref->title)) ); | ||
| 1136 | $self->_addel($xmlRef, "RefJournal")-> | ||
| 1137 | appendChild( $xml->createTextNode(&STRIP($ref->location)) ); | ||
| 1138 | # References will be added later in a <Feature-Table> | ||
| 1139 | push @{$refRef}, $xmlRef; | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | =head2 _parse_annotation | ||
| 1143 | |||
| 1144 | Title : _parse_annotation | ||
| 1145 | Usage : $obj->_parse_annotation(@args ) | ||
| 1146 | Function: Will examine any Annotations found in -obj. Data found in | ||
| 1147 | ::Comment and ::DBLink structures, as well as Annotation | ||
| 1148 | description fields are stored in -desc for later | ||
| 1149 | generation of <Attribute>s. <Reference> objects are generated | ||
| 1150 | from ::References, and are stored in -refs - these will | ||
| 1151 | be appended to the XML tree later. | ||
| 1152 | Returns : | ||
| 1153 | Args : Argument array. Recognized keys: | ||
| 1154 | |||
| 1155 | -xml The DOM::Document being modified | ||
| 1156 | |||
| 1157 | -obj Reference to the Bio object being analyzed | ||
| 1158 | |||
| 1159 | -descr An array reference for holding description text items | ||
| 1160 | |||
| 1161 | -refs An array reference to hold <Reference> DOM objects | ||
| 1162 | |||
| 1163 | -id Optional. If the XML id for the 'calling' element is | ||
| 1164 | provided, it will be placed in any <Reference> refs | ||
| 1165 | attribute. | ||
| 1166 | |||
| 1167 | =cut | ||
| 1168 | |||
| 1169 | sub _parse_annotation { | ||
| 1170 | my $self = shift; | ||
| 1171 | my $args = $self->_parseparams( @_); | ||
| 1172 | my ($xml, $obj, $descRef, $refRef) = | ||
| 1173 | ( $args->{XML}, $args->{OBJ}, $args->{DESC}, $args->{REFS} ); | ||
| 1174 | # No good place to put any of this (except for references). Most stuff | ||
| 1175 | # just gets dumped to <Attribute>s | ||
| 1176 | my $ann = $obj->annotation; | ||
| 1177 | return unless ($ann); | ||
| 1178 | # use BMS::Branch; my $debug = BMS::Branch->new( ); warn "$obj :"; $debug->branch($ann); | ||
| 1179 | unless (ref($ann) =~ /Collection/) { | ||
| 1180 | # Old style annotation. It seems that Features still use this | ||
| 1181 | # form of object | ||
| 1182 | $self->_parse_annotation_old(@_); | ||
| 1183 | return; | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | for my $key ($ann->get_all_annotation_keys()) { | ||
| 1187 | for my $thing ($ann->get_Annotations($key)) { | ||
| 1188 | if ($key eq 'description') { | ||
| 1189 | push @{$descRef}, ["description" , $thing->value]; | ||
| 1190 | } elsif ($key eq 'comment') { | ||
| 1191 | push @{$descRef}, ["comment" , $thing->text]; | ||
| 1192 | } elsif ($key eq 'dblink') { | ||
| 1193 | # DBLinks get dumped to attributes, too | ||
| 1194 | push @{$descRef}, ["db_xref" , $thing->database . ":" | ||
| 1195 | . $thing->primary_id ]; | ||
| 1196 | if (my $com = $thing->comment) { | ||
| 1197 | push @{$descRef}, ["link" , $com->text ]; | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | } elsif ($key eq 'reference') { | ||
| 1201 | $self->_parse_reference( @_, -refobj => $thing ); | ||
| 1202 | } elsif (ref($thing) =~ /SimpleValue/) { | ||
| 1203 | push @{$descRef}, [$key , $thing->value]; | ||
| 1204 | } else { | ||
| 1205 | # What is this?? | ||
| 1206 | push @{$descRef}, ["error", "bsml.pm did not understand ". | ||
| 1207 | "'$key' = '$thing'" ]; | ||
| 1208 | } | ||
| 1209 | } | ||
| 1210 | } | ||
| 1211 | } | ||
| 1212 | |||
| 1213 | =head2 _parse_annotation_old | ||
| 1214 | |||
| 1215 | Title : _parse_annotation_old | ||
| 1216 | Usage : $obj->_parse_annotation_old(@args) | ||
| 1217 | Function: As above, but for the old Annotation system. | ||
| 1218 | Apparently needed because Features are still using the old-style | ||
| 1219 | annotations? | ||
| 1220 | Returns : | ||
| 1221 | Args : Argument array. Recognized keys: | ||
| 1222 | |||
| 1223 | -xml The DOM::Document being modified | ||
| 1224 | |||
| 1225 | -obj Reference to the Bio object being analyzed | ||
| 1226 | |||
| 1227 | -descr An array reference for holding description text items | ||
| 1228 | |||
| 1229 | -refs An array reference to hold <Reference> DOM objects | ||
| 1230 | |||
| 1231 | -id Optional. If the XML id for the 'calling' element is | ||
| 1232 | provided, it will be placed in any <Reference> refs | ||
| 1233 | attribute. | ||
| 1234 | |||
| 1235 | =cut | ||
| 1236 | |||
| 1237 | ############### | ||
| 1238 | # <Reference> # | ||
| 1239 | ############### | ||
| 1240 | |||
| 1241 | sub _parse_annotation_old { | ||
| 1242 | my $self = shift; | ||
| 1243 | my $args = $self->_parseparams( @_); | ||
| 1244 | my ($xml, $obj, $descRef, $refRef) = | ||
| 1245 | ( $args->{XML}, $args->{OBJ}, $args->{DESC}, $args->{REFS} ); | ||
| 1246 | # No good place to put any of this (except for references). Most stuff | ||
| 1247 | # just gets dumped to <Attribute>s | ||
| 1248 | if (my $ann = $obj->annotation) { | ||
| 1249 | push @{$descRef}, ["annotation", $ann->description]; | ||
| 1250 | for my $com ($ann->each_Comment) { | ||
| 1251 | push @{$descRef}, ["comment" , $com->text]; | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | # Gene names just get dumped to <Attribute name="gene"> | ||
| 1255 | for my $gene ($ann->each_gene_name) { | ||
| 1256 | push @{$descRef}, ["gene" , $gene]; | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | # DBLinks get dumped to attributes, too | ||
| 1260 | for my $link ($ann->each_DBLink) { | ||
| 1261 | push @{$descRef}, ["db_xref" , | ||
| 1262 | $link->database . ":" . $link->primary_id ]; | ||
| 1263 | if (my $com = $link->comment) { | ||
| 1264 | push @{$descRef}, ["link" , $com->text ]; | ||
| 1265 | } | ||
| 1266 | } | ||
| 1267 | |||
| 1268 | # References get produced and temporarily held | ||
| 1269 | for my $ref ($ann->each_Reference) { | ||
| 1270 | $self->_parse_reference( @_, -refobj => $ref ); | ||
| 1271 | } | ||
| 1272 | } | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | =head2 _add_page | ||
| 1276 | |||
| 1277 | Title : _add_page | ||
| 1278 | Usage : $obj->_add_page($xmlDocument, $xmlSequenceObject) | ||
| 1279 | Function: Adds a simple <Page> and <View> structure for a <Sequence> | ||
| 1280 | Returns : a reference to the newly created <Page> | ||
| 1281 | Args : 0 The DOM::Document being modified | ||
| 1282 | 1 Reference to the <Sequence> object | ||
| 1283 | |||
| 1284 | =cut | ||
| 1285 | |||
| 1286 | sub _add_page { | ||
| 1287 | my $self = shift; | ||
| 1288 | my ($xml, $seq) = @_; | ||
| 1289 | my $disp = $xml->getElementsByTagName("Display")->item(0); | ||
| 1290 | my $page = $self->_addel($disp, "Page"); | ||
| 1291 | my ($width, $height) = ( 7.8, 5.5); | ||
| 1292 | my $screen = $self->_addel($page, "Screen", { | ||
| 1293 | width => $width, height => $height, }); | ||
| 1294 | # $screen->appendChild($xml->createComment("Must close explicitly")); | ||
| 1295 | my $view = $self->_addel($page, "View", { | ||
| 1296 | seqref => $seq->getAttribute('id'), | ||
| 1297 | title => $seq->getAttribute('title'), | ||
| 1298 | title1 => "{NAME}", | ||
| 1299 | title2 => "{LENGTH} {UNIT}", | ||
| 1300 | }); | ||
| 1301 | $self->_addel($view, "View-line-widget", { | ||
| 1302 | shape => 'horizontal', | ||
| 1303 | hcenter => $width/2 + 0.7, | ||
| 1304 | 'linear-length' => $width - 2, | ||
| 1305 | }); | ||
| 1306 | $self->_addel($view, "View-axis-widget"); | ||
| 1307 | return $page; | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | |||
| 1311 | =head2 _addel | ||
| 1312 | |||
| 1313 | Title : _addel | ||
| 1314 | Usage : $obj->_addel($parentElem, 'ChildName', | ||
| 1315 | { anAttr => 'someValue', anotherAttr => 'aValue',}) | ||
| 1316 | Function: Add an element with attribute values to a DOM tree | ||
| 1317 | Returns : a reference to the newly added element | ||
| 1318 | Args : 0 The DOM::Element parent that you want to add to | ||
| 1319 | 1 The name of the new child element | ||
| 1320 | 2 Optional hash reference containing | ||
| 1321 | attribute name => attribute value assignments | ||
| 1322 | |||
| 1323 | =cut | ||
| 1324 | |||
| 1325 | sub _addel { | ||
| 1326 | my $self = shift; | ||
| 1327 | my ($root, $name, $attr) = @_; | ||
| 1328 | |||
| 1329 | # Find the DOM::Document for the parent | ||
| 1330 | my $doc = $root->getOwnerDocument || $root; | ||
| 1331 | my $elem = $doc->createElement($name); | ||
| 1332 | for my $a (keys %{$attr}) { | ||
| 1333 | $elem->setAttribute($a, $attr->{$a}); | ||
| 1334 | } | ||
| 1335 | $root->appendChild($elem); | ||
| 1336 | return $elem; | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | =head2 _show_dna | ||
| 1340 | |||
| 1341 | Title : _show_dna | ||
| 1342 | Usage : $obj->_show_dna($newval) | ||
| 1343 | Function: (cut-and-pasted directly from embl.pm) | ||
| 1344 | Returns : value of _show_dna | ||
| 1345 | Args : newvalue (optional) | ||
| 1346 | |||
| 1347 | =cut | ||
| 1348 | |||
| 1349 | sub _show_dna { | ||
| 1350 | my $obj = shift; | ||
| 1351 | if( @_ ) { | ||
| 1352 | my $value = shift; | ||
| 1353 | $obj->{'_show_dna'} = $value; | ||
| 1354 | } | ||
| 1355 | return $obj->{'_show_dna'}; | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | =head2 _initialize | ||
| 1359 | |||
| 1360 | Title : _initialize | ||
| 1361 | Usage : $dom = $obj->_initialize(@args) | ||
| 1362 | Function: Coppied from embl.pm, and augmented with initialization of the | ||
| 1363 | XML DOM tree | ||
| 1364 | Returns : | ||
| 1365 | Args : -file => the XML file to be parsed | ||
| 1366 | |||
| 1367 | =cut | ||
| 1368 | |||
| 1369 | sub _initialize { | ||
| 1370 | my($self,@args) = @_; | ||
| 1371 | |||
| 1372 | $self->SUPER::_initialize(@args); | ||
| 1373 | # hash for functions for decoding keys. | ||
| 1374 | $self->{'_func_ftunit_hash'} = {}; | ||
| 1375 | $self->_show_dna(1); # sets this to one by default. People can change it | ||
| 1376 | |||
| 1377 | my %param = @args; # From SeqIO.pm | ||
| 1378 | @param{ map { lc $_ } keys %param } = values %param; # lowercase keys | ||
| 1379 | if ( exists $param{-file} && $param{-file} !~ /^>/) { | ||
| 1380 | # Is it blasphemy to add your own keys to an object in another package? | ||
| 1381 | # domtree => the parsed DOM tree retruned by XML::DOM | ||
| 1382 | $self->{'domtree'} = $self->_parse_xml( $param{-file} ); | ||
| 1383 | # current_node => the <Sequence> node next in line for next_seq | ||
| 1384 | $self->{'current_node'} = 0; | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | $self->sequence_factory( Bio::Seq::SeqFactory->new | ||
| 1388 | ( -verbose => $self->verbose(), | ||
| 1389 | -type => 'Bio::Seq::RichSeq')) | ||
| 1390 | if( ! defined $self->sequence_factory ); | ||
| 1391 | } | ||
| 1392 | |||
| 1393 | |||
| 1394 | =head2 _parseparams | ||
| 1395 | |||
| 1396 | Title : _parseparams | ||
| 1397 | Usage : my $paramHash = $obj->_parseparams(@args) | ||
| 1398 | Function: Borrowed from Bio::Parse.pm, who borrowed it from CGI.pm | ||
| 1399 | Lincoln Stein -> Richard Resnick -> here | ||
| 1400 | Returns : A hash reference of the parameter keys (uppercase) pointing to | ||
| 1401 | their values. | ||
| 1402 | Args : An array of key, value pairs. Easiest to pass values as: | ||
| 1403 | -key1 => value1, -key2 => value2, etc | ||
| 1404 | Leading "-" are removed. | ||
| 1405 | |||
| 1406 | =cut | ||
| 1407 | |||
| 1408 | sub _parseparams { | ||
| 1409 | my $self = shift; | ||
| 1410 | my %hash = (); | ||
| 1411 | my @param = @_; | ||
| 1412 | |||
| 1413 | # Hacked out from Parse.pm | ||
| 1414 | # The next few lines strip out the '-' characters which | ||
| 1415 | # preceed the keys, and capitalizes them. | ||
| 1416 | for (my $i=0;$i<@param;$i+=2) { | ||
| 1417 | $param[$i]=~s/^\-//; | ||
| 1418 | $param[$i]=~tr/a-z/A-Z/; | ||
| 1419 | } | ||
| 1420 | pop @param if @param %2; # not an even multiple | ||
| 1421 | %hash = @param; | ||
| 1422 | return \%hash; | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | =head2 _parse_xml | ||
| 1426 | |||
| 1427 | Title : _parse_xml | ||
| 1428 | Usage : $dom = $obj->_parse_xml($filename) | ||
| 1429 | Function: uses XML::DOM to construct a DOM tree from the BSML document | ||
| 1430 | Returns : a reference to the parsed DOM tree | ||
| 1431 | Args : 0 Path to the XML file needing to be parsed | ||
| 1432 | |||
| 1433 | =cut | ||
| 1434 | |||
| 1435 | sub _parse_xml { | ||
| 1436 | my $self = shift; | ||
| 1437 | my $file = shift; | ||
| 1438 | |||
| 1439 | unless (-e $file) { | ||
| 1440 | $self->throw("Could not parse non-existant XML file '$file'."); | ||
| 1441 | return; | ||
| 1442 | } | ||
| 1443 | my $parser = XML::DOM::Parser->new(); | ||
| 1444 | my $doc = $parser->parsefile ($file); | ||
| 1445 | return $doc; | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | sub DESTROY { | ||
| 1449 | my $self = shift; | ||
| 1450 | # Reports off the net imply that DOM::Parser will memory leak if you | ||
| 1451 | # do not explicitly dispose of it: | ||
| 1452 | # http://aspn.activestate.com/ASPN/Mail/Message/perl-xml/788458 | ||
| 1453 | my $dom = $self->{'domtree'}; | ||
| 1454 | # For some reason the domtree can get undef-ed somewhere... | ||
| 1455 | $dom->dispose if ($dom); | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | |||
| 1459 | =head1 TESTING SCRIPT | ||
| 1460 | |||
| 1461 | The following script may be used to test the conversion process. You | ||
| 1462 | will need a file of the format you wish to test. The script will | ||
| 1463 | convert the file to BSML, store it in /tmp/bsmltemp, read that file | ||
| 1464 | into a new SeqIO stream, and write it back as the original | ||
| 1465 | format. Comparison of this second file to the original input file | ||
| 1466 | will allow you to track where data may be lost or corrupted. Note | ||
| 1467 | that you will need to specify $readfile and $readformat. | ||
| 1468 | |||
| 1469 | use Bio::SeqIO; | ||
| 1470 | # Tests preservation of details during round-trip conversion: | ||
| 1471 | # $readformat -> BSML -> $readformat | ||
| 1472 | my $tempspot = "/tmp/bsmltemp"; # temp folder to hold generated files | ||
| 1473 | my $readfile = "rps4y.embl"; # The name of the file you want to test | ||
| 1474 | my $readformat = "embl"; # The format of the file being tested | ||
| 1475 | |||
| 1476 | system "mkdir $tempspot" unless (-d $tempspot); | ||
| 1477 | # Make Seq object from the $readfile | ||
| 1478 | my $biostream = Bio::SeqIO->new( -file => "$readfile" ); | ||
| 1479 | my $seq = $biostream->next_seq(); | ||
| 1480 | |||
| 1481 | # Write BSML from SeqObject | ||
| 1482 | my $bsmlout = Bio::SeqIO->new(-format => 'bsml', | ||
| 1483 | -file => ">$tempspot/out.bsml"); | ||
| 1484 | warn "\nBSML written to $tempspot/out.bsml\n"; | ||
| 1485 | $bsmlout->write_seq($seq); | ||
| 1486 | # Need to kill object for following code to work... Why is this so? | ||
| 1487 | $bsmlout = ""; | ||
| 1488 | |||
| 1489 | # Make Seq object from BSML | ||
| 1490 | my $bsmlin = Bio::SeqIO->new(-file => "$tempspot/out.bsml", | ||
| 1491 | -format => 'bsml'); | ||
| 1492 | my $seq2 = $bsmlin->next_seq(); | ||
| 1493 | |||
| 1494 | # Write format back from Seq Object | ||
| 1495 | my $genout = Bio::SeqIO->new(-format => $readformat, | ||
| 1496 | -file => ">$tempspot/out.$readformat"); | ||
| 1497 | $genout->write_seq($seq2); | ||
| 1498 | warn "$readformat written to $tempspot/out.$readformat\n"; | ||
| 1499 | |||
| 1500 | # BEING LOST: | ||
| 1501 | # Join information (not possible in BSML 2.2) | ||
| 1502 | # Sequence type (??) | ||
| 1503 | |||
| 1504 | =cut | ||
| 1505 | |||
| 1506 | |||
| 1507 | 1; | ||