sync with trunk
[bioperl-network.git] / lib / Bio / Network / IO.pm
blobf9ca825a649591ee1979d634f2d1268368607944
1 # $Id$
3 # BioPerl module for Bio::Network::IO
5 # You may distribute this module under the same terms as perl itself
6 # POD documentation - main docs before the code
8 =head1 NAME
10 Bio::Network::IO - Class for reading and writing biological network data.
12 =head1 SYNOPSIS
14 This is a modules for reading and writing protein-protein interaction
15 and creating networks from this data.
17 # Read protein interaction data in some format
18 my $io = Bio::Network::IO->new(-file => 'bovine.xml',
19 -format => 'psi25' );
20 my $network = $io->next_network;
22 =head1 DESCRIPTION
24 This class is analagous to the SeqIO and AlignIO classes. To read in a
25 file of a particular format, file and format are given as key/value
26 pairs as arguments. The Bio::Network::IO checks that the appropriate
27 module is available and loads it.
29 At present only the DIP tab-delimited format and PSI XML format are
30 supported.
32 =head1 METHODS
34 The main methods are:
36 =head2 $net = $io-E<gt>next_network
38 The next_network method does not imply that multiple networks are
39 contained in a file, this is to maintain a consistent nomenclature
40 with Bioperl methods like $seqio-E<gt>next_seq and $alnio-E<gt>next_aln.
42 =head2 $io-E<gt>write_network($network)
44 UNIMPLEMENTED.
46 =head1 REQUIREMENTS
48 To read from PSI XML you will need the XML::Twig module,
49 available from CPAN.
51 =head1 FEEDBACK
53 =head2 Mailing Lists
55 User feedback is an integral part of the evolution of this and other
56 Bioperl modules. Send your comments and suggestions preferably to one
57 of the Bioperl mailing lists.
59 Your participation is much appreciated.
61 bioperl-l@bioperl.org - General discussion
62 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
64 =head2 Support
66 Please direct usage questions or support issues to the mailing list:
68 I<bioperl-l@bioperl.org>
70 rather than to the module maintainer directly. Many experienced and
71 reponsive experts will be able look at the problem and quickly
72 address it. Please include a thorough description of the problem
73 with code and data examples if at all possible.
75 =head2 Reporting Bugs
77 Report bugs to the Bioperl bug tracking system to help us keep track
78 the bugs and their resolution. Bug reports can be submitted via the
79 web:
81 http://bugzilla.open-bio.org/
83 =head1 AUTHORS
85 Brian Osborne bosborne at alum.mit.edu
86 Richard Adams richard.adams@ed.ac.uk
88 =cut
90 package Bio::Network::IO;
91 use strict;
92 use base 'Bio::Root::IO';
93 use vars qw(%DBNAMES);
95 # these values are used to standardize database names
96 %DBNAMES = (
97 DIP => "DIP", # found in DIP files
98 SWP => "UniProt", # found in DIP files
99 PIR => "PIR", # found in DIP files
100 GI => "GenBank" # found id DIP files
103 =head2 new
105 Name : new
106 Usage : $io = Bio::Network::IO->new(-file => 'myfile.xml',
107 -format => 'psi25');
108 Returns : A Bio::Network::IO stream initialised to the appropriate format.
109 Args : Named parameters:
110 -file => $filename
111 -format => format
112 -threshold => a confidence score for the interaction, optional
113 -source => optional database name (e.g. "intact")
114 -verbose => optional, set to 1 to get commentary
116 =cut
118 sub new {
119 my ($caller, @args) = @_;
120 my $class = ref($caller) || $caller;
121 if ($class =~ /Bio::Network::IO::(\S+)/){
122 my $self = $class->SUPER::new(@args);
123 $self->_initialize_io(@args);
124 return $self;
125 } else {
126 my %param = @args;
127 @param{ map { lc $_ } keys %param } = values %param;
128 if (!exists($param{'-format'})) {
129 Bio::Root::Root->throw("Must specify a valid format!");
131 my $format = $param{'-format'};
132 $format = "\L$format";
133 return undef unless ($class->_load_format_module($format));
134 return "Bio::Network::IO::$format"->new(@args);
138 =head2 next_network
140 Name : next_network
141 Usage : $gr = $io->next_network
142 Returns : A Bio::Network::ProteinNet object.
143 Args : None
145 =cut
147 sub next_network {
148 my ($self, $gr) = @_;
149 $self->throw("Sorry, you cannot read from a generic Bio::Network::IO object.");
152 =head2 write_network
154 Name : write_network
155 Usage : $gr = $io->write_network($net).
156 Args : A Bio::Network::ProteinNet object.
157 Returns : None
159 =cut
161 sub write_network {
162 my ($self, $gr) = @_;
163 $self->throw("Sorry, you can't write from a generic Bio::NetworkIO object.");
166 =head2 threshold
168 Name : get or set a threshold
169 Usage : $io->threshold($val)
170 Returns : The threshold
171 Args : A number or none
173 =cut
175 sub threshold {
176 my $self = shift;
177 $self->{_th} = @_ if @_;
178 return $self->{_th};
181 =head2 verbose
183 Name : get or set verbosity
184 Usage : $io->verbose(1)
185 Returns : The verbosity setting
186 Args : 1 or none
188 =cut
190 sub verbose {
191 my $self = shift;
192 $self->{_verbose} = @_ if @_;
193 return $self->{_verbose};
196 =head2 _load_format_module
198 Title : _load_format_module
199 Usage : INTERNAL Bio::Network::IO stuff
200 Function: Loads up (like use) a module at run time on demand
201 Returns :
202 Args :
204 =cut
206 sub _load_format_module {
207 my ($self, $format) = @_;
208 my $module = "Bio::Network::IO::" . $format;
209 my $ok;
211 eval {
212 $ok = $self->_load_module($module);
214 if ( $@ ) {
215 print STDERR <<END
216 $self: $format cannot be found
217 Exception $@
218 For more information about the Bio::Network::IO system please see the Bio:Network::IO docs.
222 return $ok;
225 =head2 _initialize_io
227 Title : _initialize_io
228 Usage : *INTERNAL Bio::Network::IO stuff*
229 Function:
230 Returns :
231 Args :
233 =cut
235 sub _initialize_io {
236 my ($self, @args) = @_;
237 $self->SUPER::_initialize_io(@args);
238 my ($th,$verbose) = $self->_rearrange( [qw(THRESHOLD VERBOSE)], @args);
239 $self->{'_th'} = $th;
240 $self->{'_verbose'} = $verbose;
241 return $self;
244 =head2 _get_standard_name
246 Title : _get_standard_name
247 Usage :
248 Function: Returns some standard name for a database, uses global
249 %DBNAMES
250 Returns :
251 Args :
253 =cut
255 sub _get_standard_name {
256 my ($self,$name) = @_;
257 $DBNAMES{$name};
262 __END__