1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
#
# Bio::MAGE::XMLReader
# a class for converting MAGE-ML into Perl objects
# originally written by Eric Deutsch. Converted into a class
# by Jason E. Stewart.
#
package Bio::MAGE::XML::Reader;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $DEBUG);
use Carp;
use XML::Xerces;
require Exporter;
use Data::Dumper;
use Benchmark;
use Bio::MAGE qw(:ALL);
use Bio::MAGE::Base;
use Carp;
=head1 NAME
Bio::MAGE::XML::Reader - a module for exporting MAGE-ML
=head1 SYNOPSIS
use Bio::MAGE::XML::Reader;
my $reader = Bio::MAGE::XML::Reader->new(handler=>$handler,
sax1=>$sax1,
verbose=>$verbose,
log_file=>\*STDERR,
);
# set the sax1 attribute
$reader->sax1($bool);
# get the current value
$value = $reader->sax1();
# set the content/document handler - this method is provided for completeness
# the value should be set in the call to the constructor to be effective
$reader->handler($HANDLER);
# get the current handler
$handler = $reader->handler();
# set the attribute
$reader->verbose($integer);
# get the current value
$value = $reader->verbose();
# set the attribute
$reader->log_file($filename);
# get the current value
$value = $reader->log_file();
# whether to read data cubes externally (default == FALSE)
$writer->external_data($bool);
my $fh = \*STDOUT;
my $mage = $reader->read($file_name);
=head1 DESCRIPTION
Methods for transforming information from a MAGE-OM objects into
MAGE-ML.
=cut
@ISA = qw(Bio::MAGE::Base Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT_OK = qw();
$DEBUG = 1;
###############################################################################
#
# Description : mageml_reader.pl is a MAGE-ML test reader.
# It reads in a MAGE-ML document, instantiating the objects for
# the # MAGE-OM class as they are read in. Lots of diagnostic
# information # is printed if --verbose is set. In a final step,
# a MAGE-ML document # is printed to STDOUT based on all the
# information read in. The # result should be (nearly) identical
# to the XML read in when # everything is working properly.
#
# Search for flags:
# - FIXME for known bugs/shortcomings
# - DUBIOUS for things that are probably okay but could lead
# to future problems.
#
###############################################################################
=head2 ATTRIBUTE METHODS
These methods have a polymorphic setter/getter method that sets an
attribute which affects the parsing of MAGE-ML. If given a value, the
method will save the value to the attribute, if invoked with no
argument it will return the current value of the attribute.
These attributes can all be set in the call to the constructor using
the named parameter style.
=over
=item sax1
This attribute determines whether a SAX1 parser and DocumentHandler or
a SAX2XMLReader and a ContentHandler will be used for parsing. The
default is to use a SAX2 parser.
=cut
sub sax1 {
my $self = shift;
if (@_) {
$self->{__SAX1} = shift;
}
return $self->{__SAX1};
}
###############################################################################
# count: setter/getter for the scalar to track counting output
###############################################################################
sub count {
my $self = shift;
if (scalar @_) {
$self->{__COUNT} = shift;
}
return $self->{__COUNT};
}
=item handler
If an application needs a custom handler it can set this attribute in
the call to the constructor. It is advised that the object use inherit
either from Bio::MAGE::XML::Handler::ContentHandler (if using SAX2) or
Bio::MAGE::DocumentHandler if using SAX1. In particular, whatever
class is used, it needs to implement the following methods:
=over
=item * verbose
called with the integer parameter that specifies the desired level of
output
=item * log_file
called with the file handle to which output should be sent
=item * init
called during the constructor for any needed work
=back
=cut
sub handler {
my $self = shift;
if (@_) {
$self->{__HANDLER} = shift;
}
return $self->{__HANDLER};
}
=head2 parser
Title : parser
Usage : $obj->parser($newval)
Function:
Example :
Returns : value of parser (a scalar)
Args : on set, new value (a scalar or undef, optional)
=cut
sub parser{
my $self = shift;
return $self->{__PARSE} = shift if @_;
return $self->{__PARSE};
}
=item verbose
This attribute determines the desired level of output during the
parse. The default is no output. A positive value increases the amount
of information.
=cut
sub verbose {
my $self = shift;
if (@_) {
$self->{__VERBOSE} = shift;
}
return $self->{__VERBOSE};
}
=item log_file
This attribute specifies a file handle to which parse output will be
directed. It is only needed if verbose is positive.
=cut
sub log_file {
my $self = shift;
if (@_) {
$self->{__LOG_FILE} = shift;
}
return $self->{__LOG_FILE};
}
=item external_data($bool)
If defined, this will cause all BioAssayData objects to read
themselves out using the DataExternal format.
B<Default Value:> false
=cut
sub external_data {
my $self = shift;
if (@_) {
$self->{__EXTERNAL_DATA} = shift;
}
return $self->{__EXTERNAL_DATA};
}
=item resolve_identifiers
This attribute specifies whether the reader should attempt to track
unhandled identifiers in the document, and then resolve them when
parsing is over. This can be a huge performance hit if you know that
all identifiers wil not resolve.
B<Default Value:> false
=cut
sub resolve_identifiers {
my $self = shift;
if (@_) {
$self->{__RESOLVE_IDENTIFIERS} = shift;
}
return $self->{__RESOLVE_IDENTIFIERS};
}
=pod
=back
=head2 INSTANCE METHODS
=over
=item $self->read($file_name)
This method will open the MAGE-ML file specified by $file_name and if
the C<handler> attribute is not set, it will create either a SAX2
parser or a SAX1 parser (depending on the value of the C <sax1>
attribute) and parse the file.
C<read()> can read from STDIN by specifying '-' as the filename. This
enables you to handle compressed XML files:
gzip -dc file.xml.gz | read.pl [options]
=cut
sub read {
my ($self,$file) = @_;
unless ($file eq '-') {
croak "File '$file' does not exist!\n"
unless (-f $file);
}
my $parser = $self->parser();
my $HANDLER = $self->handler();
$HANDLER->count($self->count)
if defined $self->count();
# my $LOG = $self->log_file();
my $LOG = new IO::File $self->log_file() , "w";
my $VERBOSE = $self->verbose();
#### Actually do the file parsing and loading
if ($file eq '-') {
$parser->parse (XML::Xerces::StdInInputSource->new());
} else {
my ($path) = $file =~ m|(.*/)|;
$HANDLER->dir($path)
if defined $path;
$parser->parse (XML::Xerces::LocalFileInputSource->new($file));
}
#### Try to process any remaining unhandled objects. These are
#### most likely to be references encountered before the
#### definition of that referenced object, but they might be dangling
#### references which are permitted with the hope that some other
#### entity can provide the needed information at some later time.
####
#### Deutsch says: I'm not really thrilled with this way of doing things.
#### It's a legacy from v1 of this code. Couldn't we just check before
#### instantiating an object to see if its identifier is already on the
#### unhandled list and if so, don't even bother calling new() but rather
#### flesh out the stub object into what it's really supposed to be?
#### Deutsch continues: Maybe that wouldn't be any easier... leave it
#### for now. DUBIOUS.
####
#### Will this even work if there are multiple unresolved references
#### of the same type? FIXME if not or remove this comment.
print $LOG <<LOG if ($VERBOSE);
-----------------------------------------------\
Looking for any unresolved references:
LOG
if ($self->resolve_identifiers) {
my $UNHANDLED = $HANDLER->unhandled();
foreach my $identifier (keys %{$UNHANDLED}) {
print $LOG "Looking for unhandled: $identifier\n"
if ($VERBOSE);
my $array_ref = $UNHANDLED->{$identifier};
#### Each item in unhandled is a three element array containing
#### the object, classname and method that needs to be called to
#### make the association
foreach my $obj_array_ref (@{$array_ref}) {
#### Obtain the object and method and classname
my ($attribute,$object,$class) = @{$obj_array_ref};
#### If there now is an object with this identifier, make the link
if (exists $HANDLER->id->{$class}->{$identifier}) {
print $LOG "There now is corresponding object: $identifier\n"
if ($VERBOSE);
no strict 'refs';
#### If the place where the reference is supposed to be is in fact
#### an array, this must be an array of references instead, so deal
#### with that. This may be a performance hit if there are thousands
#### of objects in the array, but it works for now. DUBIOUS
my $value = $object->get_slot($attribute);
if (ref($value) eq 'ARRAY') {
#### So loop of each element in the array
for (my $i=0;$i<scalar @{$value};$i++) {
#### When we find the identifier, make the link
if ($value->[$i]->getIdentifier() eq $identifier) {
$value->[$i] = $HANDLER->id->{$class}->{$identifier};
}
}
#### Otherwise it's just a single reference so make the link directly
} else {
$object->set_slot($attribute,$HANDLER->id->{$class}->{$identifier});
}
#### Delete the identifier from the unhandled list
delete $UNHANDLED->{$identifier};
#### Otherwise this identifier must not be in the document which
#### is allowed. It may mean that the data are just stored someplace
#### else, or that it could indicate a mistake.
} else {
print STDERR "WARNING: There is an unresolved ".
"$attribute '$identifier'\n" if ($VERBOSE);
}
}
}
}
#### If we're verbose mode, print $LOG out a good bit of information
#### about what's sitting in the HANDLER hash
if ($VERBOSE) {
print $LOG "\n-------------------------------------------------\n";
my ($key,$value);
my ($key2,$value2);
#### Print $LOG out all the items in the HANDLER hash
print $LOG "HANDLER:\n";
while (($key,$value) = each %{$HANDLER}) {
print $LOG "HANDLER->{$key} = $value:\n";
}
print $LOG "\n";
#### Loop over the various items in the HANDLER hash
#### and print $LOG out details about them
while (($key,$value) = each %{$HANDLER}) {
print $LOG "HANDLER->{$key}\n";
if ($key eq "__ID" or $key eq "__UNHANDLED") {
while (($key2,$value2) = each %{$HANDLER->{$key}}) {
print $LOG " $key2 = $value2\n";
}
} elsif ($key eq "__OBJ_STACK" or $key eq "__ASSN_STACK") {
foreach $key2 (@{$HANDLER->{$key}}) {
print $LOG " $key2\n";
}
} elsif ($key eq '__MAGE' || $key eq '__CLASS2FULLCLASS' || $key eq '__DIR' || $key eq '__READER') {
#### Skip those ones
#### __DIR and __READER must be an array reference but they are not (__DIR : scalar ; __READER : HASH ref)
} else {
foreach $key2 (@{$HANDLER->{$key}}) {
print $LOG " $key2\n";
}
}
}
}
#### Obtain the MAGE object from the HANDLER
my $mage = $HANDLER->MAGE();
#### If there was no MAGE object defined, die
unless ($mage) {
croak <<ERR;
ERROR: This MAGE-ML document has no top <MAGE-ML> tag!
This should never happen. complain to your MAGE-ML provider.
ERR
}
return $mage;
}
sub initialize {
my $self = shift;
my $HANDLER;
my $parser;
$self->verbose(0)
unless $self->verbose();
# Added by Mohammad on 19/11/03 shoja@ebi.ac.uk , Change begin
$self->external_data(0)
unless defined $self->external_data();
# Added by Mohammad on 19/11/03 shoja@ebi.ac.uk , Change end
if ($self->sax1) {
$parser = XML::Xerces::SAXParser->new();
$parser->setValidationScheme($XML::Xerces::SAXParser::Val_Always);
$parser->setDoNamespaces(0);
$parser->setDoSchema(0);
if (defined $self->handler()) {
$HANDLER = $self->handler();
} else {
$HANDLER = Bio::MAGE::XML::Handler::DocumentHandler->new();
$self->handler($HANDLER);
}
$parser->setDocumentHandler($HANDLER);
} else {
$parser = XML::Xerces::XMLReaderFactory::createXMLReader();
$parser->setFeature("http://xml.org/sax/features/namespaces", 0);
$parser->setFeature("http://xml.org/sax/features/validation", 1);
$parser->setFeature("http://apache.org/xml/features/validation/dynamic", 0);
if (defined $self->handler()) {
$HANDLER = $self->handler();
} else {
$HANDLER = Bio::MAGE::XML::Handler::ContentHandler->new();
$self->handler($HANDLER);
}
$parser->setContentHandler($HANDLER);
}
$self->resolve_identifiers(1)
unless defined $self->resolve_identifiers;
# this way the handler can access our attributes (verbose, log_file, etc)
$HANDLER->reader($self);
$HANDLER->init();
my $error_handler = XML::Xerces::PerlErrorHandler->new();
$parser->setErrorHandler($error_handler);
$self->parser($parser);
return 1;
}
=pod
=back
=cut
1;
|