3 # BioPerl module for Bio::PopGen::PopStats
5 # Cared for by Jason Stajich <jason-at-bioperl.org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::PopGen::PopStats - A collection of methods for calculating
16 statistics about a population or sets of populations
20 use Bio::PopGen::PopStats;
21 my $stats = Bio::PopGen::PopStats->new(); # add -haploid => 1
22 # to process haploid data
26 Calculate various population structure statistics, most notably Wright's Fst.
32 User feedback is an integral part of the evolution of this and other
33 Bioperl modules. Send your comments and suggestions preferably to
34 the Bioperl mailing list. Your participation is much appreciated.
36 bioperl-l@bioperl.org - General discussion
37 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
41 Report bugs to the Bioperl bug tracking system to help us keep track
42 of the bugs and their resolution. Bug reports can be submitted via
45 http://bugzilla.open-bio.org/
47 =head1 AUTHOR - Jason Stajich
49 Email jason-at-bioperl.org
53 Matthew Hahn, matthew.hahn-at-duke.edu
57 The rest of the documentation details each of the object methods.
58 Internal methods are usually preceded with a _
63 # Let the code begin...
66 package Bio
::PopGen
::PopStats
;
69 # Object preamble - inherits from Bio::Root::Root
73 use base
qw(Bio::Root::Root);
78 Usage : my $obj = Bio::PopGen::PopStats->new();
79 Function: Builds a new Bio::PopGen::PopStats object
80 Returns : an instance of Bio::PopGen::PopStats
81 Args : -haploid => 1 (if want to use haploid calculations)
87 my($class,@args) = @_;
89 my $self = $class->SUPER::new
(@args);
90 my ($haploid) = $self->_rearrange([qw(HAPLOID)],@args);
91 if( $haploid ) { $self->haploid_status(1) }
98 Title : haploid_status
99 Usage : $obj->haploid_status($newval)
100 Function: Boolean value for whether or not to do haploid
101 or diploid calculations, where appropriate
103 Args : on set, new boolean value optional)
110 return $self->{'haploid_status'} = shift if @_;
111 return $self->{'haploid_status'};
115 # Implementation provided my Matthew Hahn, massaged by Jason Stajich
120 Usage : my $fst = $stats->Fst(\@populations,\@markernames)
121 Function: Calculate Wright's Fst based on a set of sub-populations
123 Returns : Fst value (a value between 0 and 1)
124 Args : Arrayref of populations to process
125 Arrayref of marker names to process
126 Note : Based on diploid method in Weir BS, Genetics Data Analysis II, 1996
131 #' make emacs happy here
133 my ($self,$populations,$markernames) = @_;
135 if( ! defined $populations ||
136 ref($populations) !~ /ARRAY/i ) {
137 $self->warn("Must provide a valid arrayref for populations");
139 } elsif( ! defined $markernames ||
140 ref($markernames) !~ /ARRAY/i ) {
141 $self->warn("Must provide a valid arrayref for marker names");
144 my $num_sub_pops = scalar @
$populations;
146 if( $num_sub_pops < 2 ) {
147 $self->warn("Must provide at least 2 populations for this test, you provided $num_sub_pops");
151 # This code assumes that pop 1 contains at least one of all the
152 # alleles - need to do some more work to insure that the complete
153 # set of alleles is seen.
155 my ($TS_sub1,$TS_sub2);
157 foreach my $marker ( @
$markernames ) {
158 # Get all the alleles from all the genotypes in all subpopulations
160 foreach my $allele ( map { $_->get_Alleles() }
161 map { $_->get_Genotypes($marker) } @
$populations ){
162 $allAlleles{$allele}++;
164 my @alleles = keys %allAlleles;
166 foreach my $allele_name ( @alleles ) {
167 my $avg_samp_size = 0; # n-bar
168 my $avg_allele_freq = 0; # p-tilda-A-dot
170 my $total_samples_squared = 0; #
171 my $sum_heterozygote = 0;
175 # Walk through each population, get the calculated allele frequencies
176 # for the marker, do some bookkeeping
179 foreach my $pop ( @
$populations ) {
180 my $s = $pop->get_number_individuals($marker);
182 $avg_samp_size += $s;
183 $total_samples_squared += $s**2;
185 my $markerobj = $pop->get_Marker($marker);
186 if( ! defined $markerobj ) {
187 $self->warn("Could not derive Marker for $marker ".
188 "from population ". $pop->name);
192 my $freq_homozygotes =
193 $pop->get_Frequency_Homozygotes($marker,$allele_name);
194 my %af = $markerobj->get_Allele_Frequencies();
195 my $all_freq = ( ($af{$allele_name} || 0));
197 $avg_allele_freq += $s * $all_freq;
198 $sum_heterozygote += (2 * $s)*( $all_freq - $freq_homozygotes);
200 push @marker_freqs, \
%af;
202 my $total_samples = $avg_samp_size; # sum of n over i sub-populations
203 $avg_samp_size /= $num_sub_pops;
204 $avg_allele_freq /= $total_samples;
207 my $adj_samp_size = ( 1/ ($num_sub_pops - 1)) *
208 ( $total_samples - ( $total_samples_squared/$total_samples));
210 my $variance = 0; # s-squared-sub-A
211 my $sum_variance = 0;
212 my $i = 0; # we have cached the marker info
213 foreach my $pop ( @
$populations ) {
214 my $s = $pop->get_number_individuals($marker);
215 my %af = %{$marker_freqs[$i++]};
216 $sum_variance += $s * (( ($af{$allele_name} || 0) -
217 $avg_allele_freq)**2);
219 $variance = ( 1 / (( $num_sub_pops-1)*$avg_samp_size))*$sum_variance;
222 my $freq_heterozygote = ($sum_heterozygote / $total_samples);
224 if( $self->haploid_status ) {
225 # Haploid calculations
227 my $T_sub1 = $variance -
228 ( ( 1/($avg_samp_size-1))*
229 ( ($avg_allele_freq*(1-$avg_allele_freq))-
230 ( (($num_sub_pops-1)/$num_sub_pops)*$variance)));
231 my $T_sub2 = ( (($adj_samp_size-1)/($avg_samp_size-1))*
232 $avg_allele_freq*(1-$avg_allele_freq) ) +
233 ( 1 + ( (($num_sub_pops-1)*
234 ($avg_samp_size-$adj_samp_size))/
235 ($avg_samp_size - 1))) *
236 ($variance/$num_sub_pops);
239 #to get total Fst from all alleles (if more than two) or all
240 #loci (if more than one), we need to calculate $T_sub1 and
241 #$T_sub2 for all alleles for all loci, sum, and then divide
247 my $S_sub1 = $variance - ( (1/($avg_samp_size-1))*
249 (1-$avg_allele_freq)) -
250 ((($num_sub_pops-1)/$num_sub_pops)*
251 $variance)-0.25*$freq_heterozygote ) );
252 my $S_sub2 = ($avg_allele_freq*(1-$avg_allele_freq)) -
253 ( ($avg_samp_size/($num_sub_pops*($avg_samp_size-1)))*
254 ( ((($num_sub_pops*($avg_samp_size- $adj_samp_size))/
255 $avg_samp_size)*$avg_allele_freq*
256 (1-$avg_allele_freq)) -
257 ( (1/$avg_samp_size)* (($avg_samp_size-1)+
260 $adj_samp_size) )*$variance ) -
261 ( (($num_sub_pops*($avg_samp_size-$adj_samp_size))/
262 (4*$avg_samp_size*$adj_samp_size))*
263 $freq_heterozygote ) ) );
265 my $S_sub3 = ($adj_samp_size/(2*$avg_samp_size))*
268 #Again, to get the average over many alleles or many loci,
269 #we will have to run the above for each and then sum the $S
270 #variables and recalculate the F statistics
276 # $Fst_diploid = $S_sub1/$S_sub2;
277 #my $Fit_diploid = 1 - ($S_sub3/$S_sub2);
278 #my $Fis_diploid = ($Fit_diploid-$Fst_diploid)/(1-$Fst_diploid);
279 $Fst = $TS_sub1 / $TS_sub2;