Permalink
Browse files

avoid NaNs in the calculation of the mean score (David Arenillas, Uni…

…versity of British Columbia)
  • Loading branch information...
1 parent 34daa48 commit 4ca03c336aa52878bab093a31f2f4c0cbb90a90d mdehoon committed Jun 24, 2013
Showing with 13 additions and 1 deletion.
  1. +13 −1 Bio/motifs/matrix.py
View
14 Bio/motifs/matrix.py
@@ -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:
@@ -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):
@@ -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
@@ -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

0 comments on commit 4ca03c3

Please sign in to comment.