
This is the Title of the Book, eMatter Edition
Copyright © 2012 O’Reilly & Associates, Inc. All rights reserved.
Amino Acid Similarity
|
57
A random DNA source has an entropy of:
- ( (0.25)(-2) + (0.25)(-2) + (0.25)(-2) + (0.25)(-2) ) = 2 bits
However, a DNA source that emits 90 percent A or T and 10 percent G or C has an
entropy of:
- ( 2(0.45)(-1.15) + 2(0.05)(-4.32) ) = 1.47 bits
In these examples, you’ve been given the frequency distribution as some kind of
truth. But it’s rare to know such things a priori, and the parameters must be esti-
mated from actual data. You may find the following Perl program informative and
entertaining. It calculates the entropy of any file.
# Shannon Entropy Calculator
my %Count; # stores the counts of each symbol
my $total = 0; # total symbols counted
while (<>) { # read lines of input
foreach my $char (split(//, $_)) { # split the line into characters
$Count{$char}++; # add one to this character count
$total++; # add one to total counts
}
}
my $H = 0; # H is the entropy
foreach my $char (keys %Count) { # iterate through characters
my $p = $Count{$char}/$total; # probability of character
$H += $p * log($p); # p * log(p)
}
$H = -$H/log(2); # negate sum, convert base e to base 2
print "H = $H bits\n"; # output
Amino Acid Similarity
Molecular biologists usually think of amino acid similarity in terms of chemical simi-
larity (see Table 2-1). Figure 4-1 depicts a rough