~ubuntu-branches/ubuntu/lucid/bioperl/lucid

« back to all changes in this revision

Viewing changes to Bio/OntologyIO.pm

  • Committer: Bazaar Package Importer
  • Author(s): Matt Hope
  • Date: 2004-04-18 14:24:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040418142411-gr92uexquw4w8liq
Tags: 1.4-1
* New upstream release
* Examples and working code are installed by default to usr/bin,
  this has been moved to usr/share/doc/bioperl/bin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: OntologyIO.pm,v 1.7 2003/11/20 06:34:11 allenday Exp $
 
2
#
 
3
# BioPerl module for Bio::OntologyIO
 
4
#
 
5
# Cared for by Hilmar Lapp <hlapp at gmx.net>
 
6
#
 
7
# Copyright Hilmar Lapp
 
8
#
 
9
# You may distribute this module under the same terms as perl itself
 
10
 
 
11
#
 
12
# (c) Hilmar Lapp, hlapp at gmx.net, 2003.
 
13
# (c) GNF, Genomics Institute of the Novartis Research Foundation, 2003.
 
14
#
 
15
# You may distribute this module under the same terms as perl itself.
 
16
# Refer to the Perl Artistic License (see the license accompanying this
 
17
# software package, or see http://www.perl.com/language/misc/Artistic.html)
 
18
# for the terms under which you may use, modify, and redistribute this module.
 
19
 
20
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 
21
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 
22
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
23
#
 
24
 
 
25
# POD documentation - main docs before the code
 
26
 
 
27
=head1 NAME
 
28
 
 
29
Bio::OntologyIO - Parser factory for Ontology formats 
 
30
 
 
31
=head1 SYNOPSIS
 
32
 
 
33
    use Bio::OntologyIO;
 
34
 
 
35
    my $parser = Bio::OntologyIO->new(-format => "go",
 
36
                                      -file=> $file);
 
37
 
 
38
    while(my $ont = $parser->next_ontology()) {
 
39
         print "read ontology ",$ont->name()," with ",
 
40
               scalar($ont->get_root_terms)," root terms, and ",
 
41
               scalar($ont->get_leaf_terms)," leaf terms\n";
 
42
    }
 
43
 
 
44
=head1 DESCRIPTION
 
45
 
 
46
This is the parser factory for different ontology sources and
 
47
formats. Conceptually, it is very similar to L<Bio::SeqIO>, but the
 
48
difference is that the chunk of data returned as an object is an
 
49
entire ontology.
 
50
 
 
51
=head1 FEEDBACK
 
52
 
 
53
=head2 Mailing Lists
 
54
 
 
55
User feedback is an integral part of the evolution of this and other
 
56
Bioperl modules. Send your comments and suggestions preferably to
 
57
the Bioperl mailing list.  Your participation is much appreciated.
 
58
 
 
59
  bioperl-l@bioperl.org              - General discussion
 
60
  http://bioperl.org/MailList.shtml  - About the mailing lists
 
61
 
 
62
=head2 Reporting Bugs
 
63
 
 
64
Report bugs to the Bioperl bug tracking system to help us keep track
 
65
of the bugs and their resolution. Bug reports can be submitted via
 
66
the web:
 
67
 
 
68
  http://bugzilla.bioperl.org/
 
69
 
 
70
=head1 AUTHOR - Hilmar Lapp
 
71
 
 
72
Email hlapp at gmx.net
 
73
 
 
74
Describe contact details here
 
75
 
 
76
=head1 CONTRIBUTORS
 
77
 
 
78
Additional contributors names and emails here
 
79
 
 
80
=head1 APPENDIX
 
81
 
 
82
The rest of the documentation details each of the object methods.
 
83
Internal methods are usually preceded with a _
 
84
 
 
85
=cut
 
86
 
 
87
 
 
88
# Let the code begin...
 
89
 
 
90
 
 
91
package Bio::OntologyIO;
 
92
use vars qw(@ISA);
 
93
use strict;
 
94
 
 
95
# Object preamble - inherits from Bio::Root::Root
 
96
 
 
97
use Bio::Root::Root;
 
98
use Bio::Root::IO;
 
99
 
 
100
@ISA = qw(Bio::Root::Root Bio::Root::IO);
 
101
 
 
102
#
 
103
# Maps from format name to driver suitable for the format.
 
104
#
 
105
my %format_driver_map = (
 
106
                         "go"       => "goflat",
 
107
                         "so"       => "soflat",
 
108
                         "interpro" => "InterProParser",
 
109
                         "evoc"     => "simplehierarchy",
 
110
                         );
 
111
 
 
112
=head2 new
 
113
 
 
114
 Title   : new
 
115
 Usage   : my $parser = Bio::OntologyIO->new(-format => 'go', @args);
 
116
 Function: Returns a stream of ontologies opened on the specified input
 
117
           for the specified format.
 
118
 Returns : An ontology parser (an instance of Bio::OntologyIO) initialized
 
119
           for the specified format.
 
120
 Args    : Named parameters. Common parameters are
 
121
 
 
122
              -format    - the format of the input; supported right now are
 
123
                          'go' (synonymous with goflat), 'so' (synonymous
 
124
                          with soflat), and 'interpro'
 
125
              -file      - the file holding the data
 
126
              -fh        - the stream providing the data (-file and -fh are
 
127
                          mutually exclusive)
 
128
              -ontology_name - the name of the ontology
 
129
              -engine    - the L<Bio::Ontology::OntologyEngineI> object
 
130
                          to be reused (will be created otherwise); note
 
131
                          that every L<Bio::Ontology::OntologyI> will
 
132
                          qualify as well since that one inherits from the
 
133
                          former.
 
134
              -term_factory - the ontology term factory to use. Provide a
 
135
                          value only if you know what you are doing.
 
136
 
 
137
           DAG-Edit flat file parsers will usually also accept the
 
138
           following parameters.
 
139
 
 
140
              -defs_file - the name of the file holding the term
 
141
                          definitions
 
142
              -files     - an array ref holding the file names (for GO,
 
143
                          there will usually be 3 files: component.ontology,
 
144
                          function.ontology, process.ontology)
 
145
 
 
146
           Other parameters are specific to the parsers.
 
147
 
 
148
=cut
 
149
 
 
150
sub new {
 
151
    my ($caller,@args) = @_;
 
152
    my $class = ref($caller) || $caller;
 
153
    # or do we want to call SUPER on an object if $caller is an
 
154
    # object?
 
155
    if( $class =~ /Bio::OntologyIO::(\S+)/ ) {
 
156
        my ($self) = $class->SUPER::new(@args); 
 
157
        $self->_initialize(@args);
 
158
        return $self;
 
159
    } else { 
 
160
        my %param = @args;
 
161
        @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
 
162
        my $format = $class->_map_format($param{'-format'});
 
163
 
 
164
        # normalize capitalization
 
165
        return undef unless( $class->_load_format_module($format) );
 
166
        return "Bio::OntologyIO::$format"->new(@args);
 
167
    }
 
168
 
 
169
}
 
170
 
 
171
sub _initialize {
 
172
    my($self, @args) = @_;
 
173
 
 
174
    # initialize factories etc
 
175
    my ($eng,$fact,$ontname) =
 
176
        $self->_rearrange([qw(TERM_FACTORY)
 
177
                           ], @args);
 
178
    # term object factory
 
179
    $self->term_factory($fact) if $fact;
 
180
 
 
181
    # initialize the Bio::Root::IO part
 
182
    $self->_initialize_io(@args);
 
183
}
 
184
 
 
185
=head2 next_ontology
 
186
 
 
187
 Title   : next_ontology
 
188
 Usage   : $ont = $stream->next_ontology()
 
189
 Function: Reads the next ontology object from the stream and returns it.
 
190
 Returns : a L<Bio::Ontology::OntologyI> compliant object, or undef at the
 
191
           end of the stream
 
192
 Args    : none
 
193
 
 
194
 
 
195
=cut
 
196
 
 
197
sub next_ontology {
 
198
    shift->throw_not_implemented();
 
199
}
 
200
 
 
201
=head2 term_factory
 
202
 
 
203
 Title   : term_factory
 
204
 Usage   : $obj->term_factory($newval)
 
205
 Function: Get/set the ontology term factory to use.
 
206
 
 
207
           As a user of this module it is not necessary to call this
 
208
           method as there will be default. In order to change the
 
209
           default, the easiest way is to instantiate
 
210
           L<Bio::Ontology::TermFactory> with the proper -type
 
211
           argument. Most if not all parsers will actually use this
 
212
           very implementation, so even easier than the aforementioned
 
213
           way is to simply call
 
214
           $ontio->term_factory->type("Bio::Ontology::MyTerm").
 
215
 
 
216
 Example : 
 
217
 Returns : value of term_factory (a Bio::Factory::ObjectFactoryI object)
 
218
 Args    : on set, new value (a Bio::Factory::ObjectFactoryI object, optional)
 
219
 
 
220
 
 
221
=cut
 
222
 
 
223
sub term_factory{
 
224
    my $self = shift;
 
225
 
 
226
    return $self->{'term_factory'} = shift if @_;
 
227
    return $self->{'term_factory'};
 
228
}
 
229
 
 
230
=head1 Private Methods
 
231
 
 
232
  Some of these are actually 'protected' in OO speak, which means you
 
233
  may or will want to utilize them in a derived ontology parser, but
 
234
  you should not call them from outside.
 
235
 
 
236
=cut
 
237
 
 
238
=head2 _load_format_module
 
239
 
 
240
 Title   : _load_format_module
 
241
 Usage   : *INTERNAL OntologyIO stuff*
 
242
 Function: Loads up (like use) a module at run time on demand
 
243
 Example :
 
244
 Returns :
 
245
 Args    :
 
246
 
 
247
=cut
 
248
 
 
249
sub _load_format_module {
 
250
    my ($self, $format) = @_;
 
251
    my $module = "Bio::OntologyIO::" . $format;
 
252
    my $ok;
 
253
 
 
254
    eval {
 
255
        $ok = $self->_load_module($module);
 
256
    };
 
257
    if ( $@ ) {
 
258
        print STDERR <<END;
 
259
$self: $format cannot be found
 
260
Exception $@
 
261
For more information about the OntologyIO system please see the docs.
 
262
This includes ways of checking for formats at compile time, not run time
 
263
END
 
264
    }
 
265
    return $ok;
 
266
}
 
267
 
 
268
sub DESTROY {
 
269
    my $self = shift;
 
270
 
 
271
    $self->close();
 
272
}
 
273
 
 
274
sub _map_format {
 
275
    my $self = shift;
 
276
    my $format = shift;
 
277
    my $mod;
 
278
 
 
279
    if($format) {
 
280
        $mod = $format_driver_map{lc($format)};
 
281
        $mod = lc($format) unless $mod;
 
282
    } else {
 
283
        $self->throw("unable to guess ontology format, specify -format");
 
284
    }
 
285
    return $mod;
 
286
}
 
287
 
 
288
sub unescape {
 
289
  my( $self, $ref ) = @_;
 
290
  $ref =~ s/&lt\\;/\</g;
 
291
  $ref =~ s/&gt\\;/\>/g;
 
292
  $ref =~ s/&pct\\;/\%/g;
 
293
  $ref =~ s/\\n/\n/g;
 
294
  $ref =~ s/\\t/\t/g;
 
295
  return $ref;
 
296
}
 
297
 
 
298
1;