~ubuntu-branches/ubuntu/raring/bioperl/raring

« back to all changes in this revision

Viewing changes to doc/Deobfuscator/lib/Deobfuscator.pm

  • Committer: Bazaar Package Importer
  • Author(s): Charles Plessy
  • Date: 2008-03-18 14:44:57 UTC
  • mfrom: (4 hardy)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20080318144457-1jjoztrvqwf0gruk
* debian/control:
  - Removed MIA Matt Hope (dopey) from the Uploaders field.
    Thank you for your work, Matt. I hope you are doing well.
  - Downgraded some recommended package to the 'Suggests' priority,
    according to the following discussion on Upstream's mail list.
    http://bioperl.org/pipermail/bioperl-l/2008-March/027379.html
    (Closes: #448890)
* debian/copyright converted to machine-readable format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Deobfuscator;
 
2
 
 
3
# module for retrieving method-specific documentation from a
 
4
# Berkeley database
 
5
#
 
6
# first version by Dave Messina (dmessina@watson.wustl.edu) at the
 
7
# Cold Spring Harbor Laboratory Advanced Bioinformatics Course
 
8
# Oct 12-25, 2005
 
9
 
 
10
# part of the Deobfuscator package
 
11
# by Laura Kavanaugh and Dave Messina
 
12
#
 
13
# cared for by Dave Messina <dave-pause@davemessina.net>
 
14
#
 
15
# POD documentation - main docs before the code
 
16
 
 
17
=head1 NAME
 
18
 
 
19
Deobfuscator - get BioPerl method and package information from a Berkeley DB
 
20
 
 
21
 
 
22
=head1 VERSION
 
23
 
 
24
This document describes Deobfuscator version 0.0.2
 
25
 
 
26
 
 
27
=head1 SYNOPSIS
 
28
 
 
29
    use Deobfuscator;
 
30
 
 
31
    # get all the methods available to objects belonging to a class
 
32
    # (including those inherited from parent classes)
 
33
    my $hashref = Deobfuscator::return_methods('Bio::SeqIO', 'Bio::AlignIO');
 
34
 
 
35
    # retrieve the return values for a method
 
36
    my $method_db_ref = Deobfuscator::open_db('methods.db');
 
37
    my $ret_vals = Deobfuscator::get_method_docs( $method_db_ref,
 
38
                                                  'Bio::SeqIO::next_seq',
 
39
                                                  'returns' );
 
40
    close_db($method_db_ref);
 
41
 
 
42
    # retrieve the synopsis documentation for a class
 
43
    my $pkg_db_ref = Deobfuscator::open_db('packages.db');
 
44
    my $synopsis = Deobfuscator::get_pkg_docs( $pkg_db_ref,
 
45
                                              'Bio::SeqIO',
 
46
                                              'synopsis' );
 
47
    close_db($pkg_db_ref);
 
48
 
 
49
 
 
50
=head1 DESCRIPTION
 
51
 
 
52
The Deobfuscator module contains functions which relate to retrieving
 
53
specific types of documentation about BioPerl packages and methods.
 
54
 
 
55
The deob_index.pl script reads through all of the BioPerl files, extracts
 
56
the documentation, and stores it in two BerkeleyDB databases. This module
 
57
is then used to query those databases for information about a given method
 
58
or package. (see the deob_index.pl documentation for more info.)
 
59
 
 
60
The types of information available for individual methods include: the
 
61
usage statement, the return values, the arguments to give to the method, the
 
62
description of the function, and an example of how to use the method.
 
63
 
 
64
The Deobfuscator module can be used also to retrieve the synopsis and
 
65
description documentation for a given class.
 
66
 
 
67
 
 
68
=head1 DIAGNOSTICS
 
69
 
 
70
=over
 
71
 
 
72
=item C<< error: couldn't eval $module >>
 
73
 
 
74
A package couldn't be loaded (eval'd), which would prevent us from determining
 
75
what its methods are.
 
76
 
 
77
=item C<< error: couldn't open $filename >>
 
78
 
 
79
One of the Berkeley databases couldn't be opened. Possible causes are:
 
80
deob_index.pl wasn't run and so the databases weren't created, or the database
 
81
files aren't in the correct place.
 
82
 
 
83
=item C<< error: couldn't close database >>
 
84
 
 
85
One of the Berkeley databases couldn't be closed. This might just be a 
 
86
transient filesystem error.
 
87
 
 
88
=back
 
89
 
 
90
 
 
91
=head1 CONFIGURATION AND ENVIRONMENT
 
92
 
 
93
This software requires:
 
94
 
 
95
=over
 
96
 
 
97
=item A working installation of the Berkeley DB
 
98
 
 
99
The Berkeley DB comes standard with most UNIX distributions, so you may 
 
100
already have it installed. See L<http://www.sleepycat.com> for more information.
 
101
 
 
102
=item BioPerl
 
103
 
 
104
Deobfuscator.pm recursively navigates a directory of BioPerl modules. Note
 
105
that the BioPerl module directory need not be "installed"; any old location
 
106
will do. See L<http://www.bioperl.org> for the latest version.
 
107
 
 
108
=back
 
109
 
 
110
 
 
111
=head1 DEPENDENCIES
 
112
 
 
113
L<version>, L<Class::Inspector>, L<DB_File>
 
114
 
 
115
 
 
116
=head1 INCOMPATIBILITIES
 
117
 
 
118
None reported.
 
119
 
 
120
 
 
121
=head1 BUGS AND LIMITATIONS
 
122
 
 
123
In the current implementation, Deobfuscator does not show internal or private
 
124
methods (i.e. those whose name begins with an underscore). This is simply an
 
125
option in the Class::Inspector->methods call, and so could be presented as an
 
126
option to the user (patches welcome).
 
127
 
 
128
No bugs have been reported.
 
129
 
 
130
 
 
131
=head1 FEEDBACK
 
132
 
 
133
=head2 Mailing Lists
 
134
 
 
135
User feedback is an integral part of the evolution of this and other
 
136
Bioperl modules. Send your comments and suggestions preferably to one
 
137
of the Bioperl mailing lists.  Your participation is much appreciated.
 
138
 
 
139
  bioperl-l@bioperl.org                  - General discussion
 
140
  http://www.bioperl.org/wiki/Mailing_lists   - About the mailing lists
 
141
 
 
142
=head2 Reporting Bugs
 
143
 
 
144
Report bugs to the Bioperl bug tracking system to help us keep track
 
145
the bugs and their resolution.  Bug reports can be submitted via the
 
146
web:
 
147
 
 
148
  http://bugzilla.open-bio.org/
 
149
 
 
150
 
 
151
=head1 SEE ALSO
 
152
 
 
153
L<deob_index.pl>
 
154
 
 
155
 
 
156
=head1 AUTHOR
 
157
 
 
158
Dave Messina C<< <dave-pause@davemessina.net> >>
 
159
 
 
160
 
 
161
=head1 CONTRIBUTORS
 
162
 
 
163
=over
 
164
 
 
165
=item Laura Kavanaugh
 
166
 
 
167
=item David Curiel
 
168
 
 
169
=back
 
170
 
 
171
 
 
172
=head1 ACKNOWLEDGMENTS
 
173
 
 
174
This software was developed originally at the Cold Spring Harbor Laboratory's
 
175
Advanced Bioinformatics Course between Oct 12-25, 2005. Many thanks to David
 
176
Curiel, who provided much-needed guidance and assistance on this project.
 
177
 
 
178
 
 
179
=head1 LICENSE AND COPYRIGHT
 
180
 
 
181
Copyright (C) 2005-6 Laura Kavanaugh and Dave Messina. All Rights Reserved.
 
182
 
 
183
This module is free software; you may redistribute it and/or modify it under the
 
184
same terms as Perl itself. See L<perlartistic>.
 
185
 
 
186
 
 
187
=head1 DISCLAIMER
 
188
 
 
189
This software is provided "as is" without warranty of any kind.
 
190
 
 
191
 
 
192
=head1 APPENDIX
 
193
 
 
194
The rest of the documentation details each of the functions.
 
195
Internal methods are preceded with a "_".
 
196
 
 
197
=cut
 
198
 
 
199
 
 
200
use version; $VERSION = qv('0.0.2');
 
201
use warnings;
 
202
use strict;
 
203
use Class::Inspector;
 
204
use DB_File;
 
205
 
 
206
use lib './lib';
 
207
 
 
208
 
 
209
=head2 return_methods
 
210
 
 
211
Title   : return_methods
 
212
Usage   : $methods_hashref = Deobfuscator::return_methods('Bio::AlignIO',
 
213
          'Bio::SeqIO');
 
214
Function: traverses the inheritance tree for a given class to determine
 
215
          the methods available to objects belonging to that class
 
216
 
 
217
Returns : a reference to a hash. The hash keys are fully-qualified class
 
218
          names, such as 'Bio::SeqIO'. The hash values are references to
 
219
          an array of hashes, where each array element is a reference to 
 
220
          a hash containing two key-value pairs, 'method' and 'class';
 
221
 
 
222
Args    : a list of fully-qualified class names
 
223
 
 
224
=cut
 
225
 
 
226
sub return_methods {
 
227
 
 
228
    my @input = @_;
 
229
 
 
230
    # key: full class name
 
231
    # value: a reference to an array of hashes
 
232
    #    where each array element is a pointer to a hash
 
233
    #    which contains two key: 'method' and 'class'
 
234
    my %methods_of;
 
235
 
 
236
 
 
237
    foreach my $class (@input) {
 
238
 
 
239
        # fancy eval so that we can loop through different modules
 
240
        _load_module($class);
 
241
 
 
242
        # methods returned from Class::Inspector as:
 
243
        # [
 
244
        #   [ 'Class::method1',   'Class',   'method1', \&Class::method1   ],
 
245
        #   [ 'Another::method2', 'Another', 'method2', \&Another::method2 ],
 
246
        #   [ 'Foo::bar',         'Foo',     'bar',     \&Foo::bar         ],
 
247
        # ]
 
248
        my $methods_aryref3
 
249
            = Class::Inspector->methods( $class, 'expanded', 'public' );
 
250
 
 
251
 
 
252
        for ( my $i = 0; $i < scalar @{$methods_aryref3}; $i++ ) {
 
253
            foreach my $meth ( $methods_aryref3->[$i] ) {
 
254
                my $method_name    = $meth->[2];
 
255
                my $inherited_from = $meth->[1];
 
256
                push @{$methods_of{$class}}, [$method_name, $inherited_from];
 
257
            }
 
258
 
 
259
        }
 
260
 
 
261
    }
 
262
    return \%methods_of;
 
263
}
 
264
 
 
265
=head2 print_methods
 
266
 
 
267
Title   : print_methods
 
268
Usage   : print_methods('Bio::AlignIO','Bio::SeqIO');
 
269
Function: traverses the inheritance tree for a given class to determine
 
270
           the methods available to objects belonging to that class, then
 
271
           pretty-prints the resulting information.
 
272
Returns : nothing. But it does print to the current filehandle (usually
 
273
           STDOUT).
 
274
Args    : a list of fully-qualified class names
 
275
 
 
276
=cut
 
277
 
 
278
sub print_methods {
 
279
 
 
280
    my @input = @_;
 
281
 
 
282
    foreach my $class (@input) {
 
283
 
 
284
        # fancy eval so that we can loop through different modules
 
285
        _load_module($class);
 
286
 
 
287
        # methods returned as
 
288
        # [
 
289
        #   [ 'Class::method1',   'Class',   'method1', \&Class::method1   ],
 
290
        #   [ 'Another::method2', 'Another', 'method2', \&Another::method2 ],
 
291
        #   [ 'Foo::bar',         'Foo',     'bar',     \&Foo::bar         ],
 
292
        # ]
 
293
        my $methods_aryref3
 
294
            = Class::Inspector->methods( $class, 'expanded', 'public' );
 
295
 
 
296
        print "methods for $class\n";
 
297
        print "=========================================\n";
 
298
 
 
299
        for ( my $i = 0; $i < scalar @{$methods_aryref3}; $i++ ) {
 
300
            print "method $i\n";
 
301
            foreach my $meth ( $methods_aryref3->[$i] ) {
 
302
                print "\t           class: $meth->[1]\n";
 
303
                print "\t          method: $meth->[2]\n";
 
304
            }
 
305
            print "--------------------------------------\n";
 
306
        }
 
307
 
 
308
    }
 
309
 
 
310
}
 
311
 
 
312
=head2 _load_module
 
313
 
 
314
Title   : _load_module
 
315
Usage   : * INTERNAL USE ONLY *
 
316
Function: attempts to load a module
 
317
Returns : nothing. But it does die upon failure to load.
 
318
Args    : a module name
 
319
 
 
320
=cut
 
321
 
 
322
sub _load_module {
 
323
    my $module = shift;
 
324
    eval "require $module";
 
325
    die "error! couldn't eval $module" if $@;
 
326
}
 
327
 
 
328
=head2 open_db
 
329
 
 
330
Title   : open_db
 
331
Usage   : open_db($filename)
 
332
Function: opens a Berkeley DB
 
333
Returns : a hashref tied to the DB
 
334
Args    : a filename as a scalar
 
335
 
 
336
=cut
 
337
 
 
338
sub open_db {
 
339
    my ($filename) = @_;
 
340
    
 
341
    my %hash;
 
342
    my $hashref = \%hash;
 
343
    
 
344
    tie %hash, "DB_File", $filename or die "error: couldn't open $filename: $!\n";
 
345
    
 
346
    return $hashref;
 
347
}
 
348
 
 
349
=head2 close_db
 
350
 
 
351
Title   : close_db
 
352
Usage   : closes a Berkeley DB
 
353
Function: closes a database
 
354
Returns : nothing.
 
355
Args    : a hashref to a tied Berkeley DB
 
356
 
 
357
=cut
 
358
 
 
359
sub close_db {
 
360
    my ($hashref) = @_;
 
361
    
 
362
    untie $hashref or die "error: couldn't close database: $!\n";
 
363
}
 
364
 
 
365
=head2 get_pkg_docs
 
366
 
 
367
Title   : get_pkg_docs
 
368
Usage   : get_pkg_docs($db_hashref, 'Class name', 'documentation type');
 
369
Function: returns a specified part of the documentation for a class
 
370
Returns : a string containing the desired documentation or ' ' if the
 
371
          documentation doesn't exist
 
372
Args    : - $db_hashref is the ref to the hash tied to the DB
 
373
          - Class name is of the form 'Bio::SeqIO'
 
374
          - documentation type is the subfield of the method's POD.
 
375
          The possible values of documentation type are:
 
376
          short_desc, synopsis, desc
 
377
 
 
378
=cut
 
379
 
 
380
sub get_pkg_docs {
 
381
    my ($db_hashref, $pkg_name, $info_type) = @_;
 
382
 
 
383
    # hash to store our hash value, now split out into its constituent parts
 
384
    my %record;
 
385
        
 
386
    my $rec_sep = 'DaVe-ReC-sEp';
 
387
 
 
388
    # if the method isn't in our db
 
389
    if ( ! exists($db_hashref->{$pkg_name}) ) {
 
390
        return 0;
 
391
    }
 
392
 
 
393
    # grab the constituent parts of the pkg record
 
394
    ( $record{'short_desc'}, $record{'synopsis'}, $record{'desc'} ) = 
 
395
        ( split $rec_sep, $db_hashref->{$pkg_name} );
 
396
 
 
397
    # return just the part that was asked for
 
398
    if ( exists($record{$info_type}) ) {
 
399
        return $record{$info_type};
 
400
    }
 
401
    else { return ' '; }
 
402
}
 
403
 
 
404
=head2 get_method_docs
 
405
 
 
406
Title   : get_method_docs
 
407
Usage   : get_method_docs($db_hashref, 'Class+method name', 'documentation type');
 
408
Example : get_method_docs($db_hashref, 'Bio::SeqIO::next_aln', 'args');
 
409
Function: returns a specified part of the documentation for a class's method
 
410
Returns : a string containing the desired documentation, or 0 if the
 
411
         desired documentation doesn't exist
 
412
Args    : - $db_hashref is the ref to the hash tied to the DB
 
413
          - Class+method name is of the form 'Bio::SeqIO::next_aln',
 
414
            where Bio::SeqIO is the class and next_aln is the method.
 
415
          - documentation type is the subfield of the method's POD.
 
416
            The possible values of documentation type are:
 
417
            title, usage, function, returns, args
 
418
 
 
419
=cut
 
420
 
 
421
sub get_method_docs {
 
422
    my ($db_hashref, $meth_name, $info_type) = @_;
 
423
 
 
424
    my %record;
 
425
    my $whole_record;
 
426
 
 
427
    my $rec_sep = 'DaVe-ReC-sEp';
 
428
 
 
429
    # if the method isn't in our db
 
430
    if ( !exists( $db_hashref->{$meth_name} ) ) {
 
431
        return 0;
 
432
    }
 
433
 
 
434
    # separate the sub-records using the record separator and field tag
 
435
    my @parts = split $rec_sep, $db_hashref->{$meth_name};
 
436
 
 
437
    # put individual info types into separate hash entries...
 
438
    foreach my $part (@parts) {
 
439
        if ($part =~ /^-(\w+)\|(.*)/) { $record{$1} = $2; }
 
440
    
 
441
    # ... and put the whole thing into one big string
 
442
        $whole_record .= "$part\n";
 
443
    }
 
444
 
 
445
    # return a specific part if that was asked for
 
446
    if ($info_type) {
 
447
        # return just the part that was asked for      
 
448
        if ( exists( $record{$info_type} ) ) {
 
449
 
 
450
                        # if there's really nothing in there, say so.
 
451
                        if ( ( $record{$info_type} =~ /^[\s\n]*$/)
 
452
                || ( $record{$info_type} eq '') ) { return 0; }
 
453
                        else { 
 
454
                                return $record{$info_type};
 
455
                        }
 
456
        }
 
457
        # or return everything
 
458
        else { return $whole_record; }
 
459
    }
 
460
    # otherwise return whole record
 
461
    else {
 
462
        return $whole_record;
 
463
    }
 
464
}
 
465
 
 
466
=head2 htmlify
 
467
 
 
468
Title   : htmlify
 
469
Usage   : htmlify($string);
 
470
Example : htmlify('this is a : doc);
 
471
Function: does some crude reformatting of POD method documentation by swapping
 
472
          isolated colons (':') into HTML <br> tags
 
473
Returns : a string
 
474
Args    : a string
 
475
 
 
476
=cut
 
477
 
 
478
sub htmlify {
 
479
        my ($string) = @_;
 
480
 
 
481
        # change isolated colons into <br> tags
 
482
        $string =~ s/\s:\s/ <br> /g;
 
483
 
 
484
        return $string;
 
485
}
 
486
 
 
487
1;
 
488
__END__