Bio-Biblio

 view release on metacpan or  search on metacpan

lib/Bio/Biblio/IO.pm  view on Meta::CPAN

package Bio::Biblio::IO;
BEGIN {
  $Bio::Biblio::IO::AUTHORITY = 'cpan:BIOPERLML';
}
{
  $Bio::Biblio::IO::VERSION = '1.70';
}
use utf8;
use strict;
use warnings;
use Symbol;

use parent qw(Bio::Root::Root Bio::Root::IO);

# ABSTRACT: Handling the bibliographic references
# AUTHOR:   Martin Senger <senger@ebi.ac.uk>
# OWNER:    2002 European Bioinformatics Institute
# LICENSE:  Perl_5


my $entry = 0;


sub new {
    my ($caller, @args) = @_;
    my $class = ref ($caller) || $caller;

    # if $caller is an object, or if it is an underlying
    # 'real-work-doing' class (e.g. Bio::Biblio::IO::medlinexml) then
    # we want to call SUPER to create and bless an object
    if( $class =~ /Bio::Biblio::IO::(\S+)/ ) {
        my ($self) = $class->SUPER::new (@args);
        $self->_initialize (@args);
        return $self;

    # this is called only the first time when somebody calls: 'new
    # Bio::Biblio::IO (...)', and it actually loads a 'real-work-doing'
    # module and call this new() method again (unless the loaded
    # module has its own new() method)
    } else {
        my %param = @args;
        @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
        my $format = $param{'-format'} ||
            $class->_guess_format( $param{-file} || $ARGV[0] ) ||
                'medlinexml';
        $format = "\L$format";  # normalize capitalization to lower case

        # load module with the real implementation - as defined in $format
        return unless (&_load_format_module ($format));

        # this will call this same method new() - but rather its
        # upper (object) branche
        return "Bio::Biblio::IO::$format"->new(@args);
    }
}


sub newFh {
    my $class = shift;
    return unless my $self = $class->new(@_);
    return $self->fh;
}


sub fh {
    my $self = shift;
    my $class = ref($self) || $self;
    my $s = Symbol::gensym;
    tie $$s,$class,$self;
    return $s;
}

# _initialize is chained for all Bio::Biblio::IO classes


sub _initialize {
    my ($self, @args) = @_;
    # initialize the IO part
    $self->_initialize_io (@args);
}


sub next_bibref {
    my ($self) = shift;
    $self->throw ("Sorry, you cannot read from a generic Bio::Biblio::IO object.");
}

# -----------------------------------------------------------------------------


sub _load_format_module {
    my ($format) = @_;
    my ($module, $load, $m);

    $module = "_<Bio/Biblio/IO/$format.pm";
    $load = "Bio/Biblio/IO/$format.pm";

    return 1 if $main::{$module};
    eval {
        require $load;
    };
    if ( $@ ) {
        Bio::Root::Root->throw (<<END);
$load: $format cannot be found or loaded
Exception $@
For more information about the Biblio system please see the Bio::Biblio::IO docs.
END
        ;
        return;
    }
    return 1;
}


sub _guess_format {
    my $class = shift;
    return unless $_ = shift;
    return 'medlinexml'   if (/\.(xml|medlinexml)$/i);
    return;
}


sub DESTROY {
    my $self = shift;

    $self->close();
}


sub TIEHANDLE {
    my ($class,$val) = @_;
    return bless {'biblio' => $val}, $class;
}


sub READLINE {
    my $self = shift;
    return $self->{'biblio'}->next_bibref() unless wantarray;
    my (@list, $obj);
    push @list, $obj while $obj = $self->{'biblio'}->next_bibref();
    return @list;
}

1;

__END__
=pod

=encoding utf-8

=head1 NAME

Bio::Biblio::IO - Handling the bibliographic references

=head1 VERSION

version 1.70

=head1 SYNOPSIS

    use Bio::Biblio::IO;

    # getting citations from a file
    $in = Bio::Biblio::IO->new ('-file' => 'myfile.xml' ,
                                '-format' => 'medlinexml');
  # --- OR ---

    # getting citations from a string
    $in = Bio::Biblio::IO->new ('-data' => '<MedlineCitation>...</MedlineCitation>' ,
                                '-format' => 'medlinexml');
  #--- OR ---

    # getting citations from a string if IO::String is installed
    use IO::String;
    $in = Bio::Biblio::IO->new ('-fh' => IO::String->new ($citation),

lib/Bio/Biblio/IO.pm  view on Meta::CPAN

    $io = Bio::Biblio::IO->new('-file' => 'data/medline_data.xml');

Another result format available is for PUBMED citations (which is a
super-set of the MEDLINE citations having few more tags):

    $io = Bio::Biblio::IO->new('-format' => 'pubmedxml',
                               '-result' => 'pubmed2ref',
                               '-data'   => $citation);

Or, because C<pubmed2ref> is a default one for PUBMED citations, you can say just:

    $io = Bio::Biblio::IO->new('-format' => 'pubmedxml',
                               '-data'   => $citation);

Both C<medline2ref> and C<pubmed2ref> results are objects defined in
the directory C<Bio::Biblio>.

=head1 ATTRIBUTES

=head2 fh

=head1 METHODS

=head2 new

=head2 newFh

=head2 next_bibref

 Usage   : $citation = stream->next_bibref
 Function: Reads the next citation object from the stream and returns it.
 Returns : a Bio::Biblio::Ref citation object, or something else
           (depending on the '-result' argument given in the 'new()'
           method).
 Args    : none

=head2 DESTROY

=head2 TIEHANDLE

=head2 READLINE

=head1 INTERNAL METHODS

=head2 _initialize

=head2 _load_format_module

 Usage   : $class->_load_format_module ($format)
 Returns : 1 on success, undef on failure
 Args    : 'format' should contain the last part of the
           name of a module who does the real implementation

It does (in run-time) a similar thing as

   require Bio::Biblio::IO::$format

It throws an exception if it fails to find and load the module
(for example, because of the compilation errors in the module).

=head2 _guess_format

 Usage   : $class->_guess_format ($filename)
 Returns : string with a guessed format of the input data (e.g. 'medlinexml')
 Args    : a file name whose extension can help to guess its format

It makes an expert guess what kind of data are in the given file
(but be prepare that $filename may be empty).

=head1 SEE ALSO

=over 4

=item *

An example script F<eg/biblio-soap.pl>

It has many options and its own help.  The relevant options to this IO module
are C<-f> (specifying what file to read) and C<-O> (specifying what result
format to achieve).

=item *

OpenBQS home page

http://www.ebi.ac.uk/~senger/openbqs/

=item *

Comments to the Perl client

http://www.ebi.ac.uk/~senger/openbqs/Client_perl.html

=back

=head1 FEEDBACK

=head2 Mailing lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Support

Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>

rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.

=head2 Reporting bugs

Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via the
web:

  https://redmine.open-bio.org/projects/bioperl/

=head1 LEGAL

=head2 Authors



( run in 0.814 second using v1.01-cache-2.11-cpan-140bd7fdf52 )