Permalink
Browse files

Added a PositionSpecificScoringMatrix class

  • Loading branch information...
1 parent dcf9797 commit ac5fdfff052372ae9091790826994b16dd84f0fc @mdehoon mdehoon committed Dec 11, 2012
Showing with 49 additions and 0 deletions.
  1. +49 −0 Bio/Motif/_Motif.py
View
@@ -137,6 +137,55 @@ def ic(self, background=None):
result+=self[a][i]*math.log(self[a][i],2)
return result
+ def make_pssm(self, background=None):
+ """
+ returns the Position-Specific Scoring Matrix.
+
+ The Position-Specific Scoring Matrix (PSSM) contains the log-odds
+ scores computed from the probability matrix and the background
+ probabilities. If the background is None, a uniform background
+ distribution is assumed.
+ """
+ return PositionSpecificScoringMatrix(self.alphabet, self, background)
+
+
+class PositionSpecificScoringMatrix(GenericPositionMatrix):
+
+ def __init__(self, alphabet, probabilities, background=None):
+ GenericPositionMatrix.__init__(self, alphabet, counts)
+ result = 0.0
+ if background is None:
+ background = {}
+ for letter in self.alphabet.letters:
+ background[letter] = 1.0
+ else:
+ background = dict(background)
+ total = sum(background.values())
+ for letter in self.alphabet.letters:
+ background[letter] /= total
+ for letter in self.alphabet.letters:
+ self[letter] = []
+ b = background[letter]
+ if b > 0:
+ for i in range(self.length):
+ p = probabilities[letter][i]
+ if p > 0:
+ logodds = math.log(p/b, 2)
+ self[letter].append(logodds)
+ else:
+ logodds = float("-inf")
+ self[letter].append(logodds)
+ else:
+ for i in range(self.length):
+ p = probabilities[letter][i]
+ if p > 0:
+ logodds = float("inf")
+ self[letter].append(logodds)
+ else:
+ logodds = float("nan")
+ self[letter].append(logodds)
+ self[letter] = tuple(self[letter])
+
class Motif(object):
"""

0 comments on commit ac5fdff

Please sign in to comment.