Skip to content

Commit

Permalink
avoid NaNs in the calculation of the mean score (David Arenillas, Uni…
Browse files Browse the repository at this point in the history
…versity of British Columbia)
  • Loading branch information
mdehoon committed Jun 24, 2013
1 parent 34daa48 commit 4ca03c3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Bio/motifs/matrix.py
Expand Up @@ -10,7 +10,7 @@
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

#Hack for Python 2.5, isnan was new in Python 2.6
#Hack for Python 2.5, isnan and isinf were new in Python 2.6
try:
from math import isnan as _isnan
except ImportError:
Expand All @@ -19,6 +19,14 @@ def _isnan(value):
if str(value).lower() == "nan":
return True
return value != value
try:
from math import isinf as _isinf
except ImportError:
def _isinf(value):
#This is tricky due to cross platform float differences
if str(value).lower().endswith("inf"):
return True
return False

class GenericPositionMatrix(dict):

Expand Down Expand Up @@ -426,6 +434,8 @@ def mean(self, background=None):
logodds = self[letter,i]
if _isnan(logodds):
continue
if _isinf(logodds) and logodds < 0:
continue
b = background[letter]
p = b * math.pow(2,logodds)
sx += p * logodds
Expand All @@ -448,6 +458,8 @@ def std(self, background=None):
logodds = self[letter,i]
if _isnan(logodds):
continue
if _isinf(logodds) and logodds < 0:
continue
b = background[letter]
p = b * math.pow(2,logodds)
sx += p*logodds
Expand Down

0 comments on commit 4ca03c3

Please sign in to comment.