sync w/ main trunk
[bioperl-live.git] / Bio / PopGen / Marker.pm
blob1f1dcc67a61234c19de4fd38aebbfda7ecc919b2
1 # $Id$
3 # BioPerl module for Bio::PopGen::Marker
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Jason Stajich <jason@bioperl.org>
9 # Copyright Jason Stajich
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::PopGen::Marker - A genetic marker which one uses to generate genotypes
19 =head1 SYNOPSIS
21 my $name = $marker->name(); # marker name
22 my $description = $marker->description(); # description
23 my $type = $marker->type(); # coded type of the marker
24 my $unique_id = $marker->unique_id; # optional unique ID
25 my @alleles = $marker->get_Alleles(); # the known alleles
26 my %allele_freqs = $marker->get_Allele_Frequencies(); # keys are marker names
27 # vals are frequencies
28 # may change to handle multiple populations
30 =head1 DESCRIPTION
32 This object will not contain genotype information pertaining to an
33 individual, but rather population level statistics and descriptive
34 information about a marker.
36 =head1 FEEDBACK
38 =head2 Mailing Lists
40 User feedback is an integral part of the evolution of this and other
41 Bioperl modules. Send your comments and suggestions preferably to
42 the Bioperl mailing list. Your participation is much appreciated.
44 bioperl-l@bioperl.org - General discussion
45 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
47 =head2 Support
49 Please direct usage questions or support issues to the mailing list:
51 L<bioperl-l@bioperl.org>
53 rather than to the module maintainer directly. Many experienced and
54 reponsive experts will be able look at the problem and quickly
55 address it. Please include a thorough description of the problem
56 with code and data examples if at all possible.
58 =head2 Reporting Bugs
60 Report bugs to the Bioperl bug tracking system to help us keep track
61 of the bugs and their resolution. Bug reports can be submitted via
62 the web:
64 http://bugzilla.open-bio.org/
66 =head1 AUTHOR - Jason Stajich
68 Email jason-at-bioperl.org
70 =head1 CONTRIBUTORS
72 Matthew Hahn, matthew.hahn-at-duke.edu
74 =head1 APPENDIX
76 The rest of the documentation details each of the object methods.
77 Internal methods are usually preceded with a _
79 =cut
82 # Let the code begin...
85 package Bio::PopGen::Marker;
86 use strict;
88 # Object preamble - inherits from Bio::Root::Root
91 use vars qw($UniqueCounter);
93 $UniqueCounter = 0;
95 use base qw(Bio::Root::Root Bio::PopGen::MarkerI);
97 =head2 new
99 Title : new
100 Usage : my $obj = Bio::PopGen::Marker->new();
101 Function: Builds a new Bio::PopGen::Marker object
102 Returns : an instance of Bio::PopGen::Marker
103 Args : -name => [string] marker name
104 -description => [string] marker description
105 -type => [string] marker type
106 -unique_id => [string/int] unique id
107 -allele_freq => [hash ref] allele frequencies
109 =cut
111 sub new {
112 my($class,@args) = @_;
114 my $self = $class->SUPER::new(@args);
115 my ($name,$desc,$type,$uid,$af) = $self->_rearrange([qw(NAME
116 DESCRIPTION
117 TYPE
118 UNIQUE_ID
119 ALLELE_FREQ)],@args);
120 $self->{'_allele_freqs'} = {};
121 if( ! defined $uid ) {
122 $uid = $UniqueCounter++;
124 if( defined $name) {
125 $self->name($name);
126 } else {
127 $self->throw("Must provide a name when initializing a Marker");
129 defined $desc && $self->description($desc);
130 defined $type && $self->type($type);
131 $self->unique_id($uid);
132 if( defined $af) {
133 if( ref($af) !~ /HASH/i ) {
134 $self->warn("Must provide valid Hash reference for allele_freq method");
135 } else {
136 foreach my $allele ( keys %$af ) {
137 $self->add_Allele_Frequency($allele, $af->{$allele});
141 return $self;
144 =head2 name
146 Title : name
147 Usage : my $name = $marker->name();
148 Function: Get the name of the marker
149 Returns : string representing the name of the marker
150 Args : [optional] name
153 =cut
155 sub name{
156 my $self = shift;
158 return $self->{'_name'} = shift if @_;
159 return $self->{'_name'};
163 =head2 description
165 Title : description
166 Usage : my $desc = $marker->description
167 Function: Get the marker description free text
168 Returns : string
169 Args : [optional] string
172 =cut
174 sub description{
175 my $self = shift;
177 return $self->{'_description'} = shift if @_;
178 return $self->{'_description'};
181 =head2 type
183 Title : type
184 Usage : my $type = $marker->type;
185 Function: Get coded string for marker type
186 Returns : string
187 Args : [optional] string
190 =cut
192 sub type{
193 my $self = shift;
195 return $self->{'_type'} = shift if @_;
196 return $self->{'_type'};
200 =head2 unique_id
202 Title : unique_id
203 Usage : my $id = $marker->unique_id;
204 Function: Get the unique marker ID
205 Returns : unique ID string
206 Args : [optional ] string
209 =cut
211 sub unique_id{
212 my $self = shift;
214 return $self->{'_uniqueid'} = shift if @_;
215 return $self->{'_uniqueid'};
218 =head2 get_Alleles
220 Title : get_Alleles
221 Usage : my @alleles = $marker->get_Alleles();
222 Function: Get the available marker alleles
223 Returns : Array of strings
224 Args : none
226 =cut
228 sub get_Alleles{
229 my $self = shift;
230 my (@numeric,@alpha);
232 for ( keys %{$self->{'_allele_freqs'}} ) {
233 if( /[^\d\.\-e]/ ) { push @alpha, $_ }
234 else { push @numeric, $_ }
236 @numeric = sort { $b <=> $a } @numeric;
237 @alpha = sort { $b cmp $a } @alpha;
238 return @numeric,@alpha;
242 =head2 get_Allele_Frequencies
244 Title : get_Allele_Frequencies
245 Usage : my %allele_freqs = $marker->get_Allele_Frequencies;
246 Function: Get the alleles and their frequency (set relative to
247 a given population - you may want to create different
248 markers with the same name for different populations
249 with this current implementation
250 Returns : Associative array where keys are the names of the alleles
251 Args : none
254 =cut
256 sub get_Allele_Frequencies{
257 return %{$_[0]->{'_allele_freqs'}};
260 =head2 add_Allele_Frequency
262 Title : add_Allele_Frequency
263 Usage : $marker->add_Allele_Frequency($allele,$freq)
264 Function: Adds an allele frequency
265 Returns : None
266 Args : $allele - allele name
267 $freq - frequency value
270 =cut
272 sub add_Allele_Frequency{
273 my ($self,$allele,$freq) = @_;
274 $self->{'_allele_freqs'}->{$allele} = $freq;
277 =head2 reset_alleles
279 Title : reset_alleles
280 Usage : $marker->reset_alleles();
281 Function: Reset the alleles for a marker
282 Returns : None
283 Args : None
286 =cut
288 sub reset_alleles{
289 my ($self) = @_;
290 $self->{'_allele_freqs'} = {};