Permalink
Browse files

Replacing set_mask by a property .mask

  • Loading branch information...
1 parent fc24967 commit 9d0ff1923aa22d263e4358ef2d27ccee89447886 mdehoon committed Dec 5, 2012
Showing with 65 additions and 30 deletions.
  1. +1 −1 Bio/Motif/AlignAce.py
  2. +1 −1 Bio/Motif/Jaspar.py
  3. +47 −12 Bio/Motif/_Motif.py
  4. +16 −16 Tests/test_Motif.py
View
@@ -49,7 +49,7 @@ def read(handle):
motif = Motif(IUPAC.unambiguous_dna, instances)
motif.score = float(line.split()[-1])
motif.number = number
- motif.set_mask(mask)
+ motif.mask = mask
record.motifs.append(motif)
elif len(line.split("\t"))==4:
seq = Seq(line.split("\t")[0],IUPAC.unambiguous_dna)
View
@@ -33,5 +33,5 @@ def read(handle, format):
motif = Motif(alphabet, instances=instances)
else:
raise ValueError("Unknown format %s" % format)
- motif.set_mask("*"*motif.length)
+ motif.mask = "*"*motif.length
return motif
View
@@ -32,7 +32,6 @@ class Motif(object):
A class representing sequence motifs.
"""
def __init__(self, alphabet=None, instances=None, counts=None):
- self.mask = []
self._pwm_is_current = False
self._pwm = []
self._log_odds_is_current = False
@@ -85,6 +84,10 @@ def __init__(self, alphabet=None, instances=None, counts=None):
if alphabet is None:
alphabet = IUPAC.unambiguous_dna
self.alphabet = alphabet
+ if self.length==None:
+ self.__mask = ()
+ else:
+ self.__mask = (1,) * self.length
self.background=dict((n, 1.0/len(self.alphabet.letters))
for n in self.alphabet.letters)
self.beta=1.0
@@ -143,15 +146,47 @@ def set_mask(self,mask):
The mask should be a string containing asterisks in the position of significant columns and spaces in other columns
"""
+ warnings.warn("""\
+This function is now obsolete, and will be deprecated and removed
+in a future release of Biopython. As a replacement, instead of
+>>> motif.set_mask(mask)
+please use
+>>> motif.mask = mask
+instead.
+""", PendingDeprecationWarning)
self._check_length(len(mask))
- self.mask=[]
+ self.__mask=[]
for char in mask:
if char=="*":
- self.mask.append(1)
+ self.__mask.append(1)
elif char==" ":
- self.mask.append(0)
+ self.__mask.append(0)
else:
raise ValueError("Mask should contain only '*' or ' ' and not a '%s'"%char)
+ self.__mask = tuple(self.__mask)
+
+ def __get_mask(self):
+ return self.__mask
+
+ def __set_mask(self, mask):
+ if len(mask)!=self.length:
+ raise ValueError("The length (%d) of the mask is inconsistent with the length (%d) of the motif", (len(mask), self.length))
+ if isinstance(mask, str):
+ self.__mask=[]
+ for char in mask:
+ if char=="*":
+ self.__mask.append(1)
+ elif char==" ":
+ self.__mask.append(0)
+ else:
+ raise ValueError("Mask should contain only '*' or ' ' and not a '%s'"%char)
+ self.__mask = tuple(self.__mask)
+ else:
+ self.__mask = tuple(int(bool(c)) for c in mask)
+
+ mask = property(__get_mask, __set_mask)
+ del __get_mask
+ del __set_mask
def pwm(self,laplace=True):
"""
@@ -274,7 +309,7 @@ def score_hit(self,sequence,position,normalized=0,masked=0):
score = 0.0
for pos in xrange(self.length):
a = sequence[position+pos]
- if not masked or self.mask[pos]:
+ if not masked or self.__mask[pos]:
try:
score += lo[pos][a]
except:
@@ -283,7 +318,7 @@ def score_hit(self,sequence,position,normalized=0,masked=0):
if not masked:
score/=self.length
else:
- score/=len([x for x in self.mask if x])
+ score/=len([x for x in self.__mask if x])
return score
def search_pwm(self,sequence,normalized=0,masked=0,threshold=0.0,both=True):
@@ -459,7 +494,7 @@ def __str__(self,masked=False):
if masked:
for i in xrange(self.length):
- if self.mask[i]:
+ if self.__mask[i]:
string += "*"
else:
string += " "
@@ -521,7 +556,7 @@ def reverse_complement(self):
"""
Gives the reverse complement of the motif
"""
- alphabet = self.alphabet
+ alphabet = IUPAC.unambiguous_dna
if self.instances is not None:
instances = []
for instance in self.instances:
@@ -540,7 +575,7 @@ def reverse_complement(self):
res.counts["G"].reverse()
res.counts["T"].reverse()
res.length=self.length
- res.mask = self.mask
+ res.__mask = self.__mask[::-1]
return res
def _from_jaspar_pfm(self,stream,make_instances=False):
@@ -567,7 +602,7 @@ def _from_vert_matrix(self,stream,letters=None,make_instances=False):
for k,v in zip(letters,rec):
self.counts[k].append(v)
self.length+=1
- self.set_mask("*"*self.length)
+ self.__mask = (1,) * self.length
if make_instances is True:
self.make_instances_from_counts()
return self
@@ -595,7 +630,7 @@ def _from_horiz_matrix(self,stream,letters=None,make_instances=False):
s = sum(self.counts[nuc][0] for nuc in letters)
l = len(self.counts[letters[0]])
self.length=l
- self.set_mask("*"*l)
+ self.__mask = (1,) * l
if make_instances is True:
self.make_instances_from_counts()
return self
@@ -677,7 +712,7 @@ def _from_jaspar_sites(self,stream):
self.instances.append(instance)
self._pwm_is_current = False
self._log_odds_is_current = False
- self.set_mask("*"*len(instance))
+ self.__mask = (1,) * length
return self
def __getitem__(self,index):
View
@@ -76,7 +76,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[0].instances[8]), "TCTACGATTGAG")
self.assertEqual(str(record.motifs[0].instances[9]), "TCAAAGATAGAG")
self.assertEqual(str(record.motifs[0].instances[10]), "TCTACGATTGAG")
- self.assertEqual(record.motifs[0].mask, [1,1,0,1,1,1,1,1,0,1,1,1])
+ self.assertEqual(record.motifs[0].mask, (1,1,0,1,1,1,1,1,0,1,1,1))
self.assertAlmostEqual(record.motifs[0].score, 57.9079)
self.assertEqual(record.motifs[1].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[1].instances), 22)
@@ -102,7 +102,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[1].instances[19]), "GGCGGGCCATCCCTGTATGAA")
self.assertEqual(str(record.motifs[1].instances[20]), "CTCCAGGTCGCATGGAGAGAG")
self.assertEqual(str(record.motifs[1].instances[21]), "CCTCGGATCGCTTGGGAAGAG")
- self.assertEqual(record.motifs[1].mask, [1,0,1,1,0,1,0,0,1,1,1,0,0,0,1,0,0,0,1,0,1])
+ self.assertEqual(record.motifs[1].mask, (1,0,1,1,0,1,0,0,1,1,1,0,0,0,1,0,0,0,1,0,1))
self.assertAlmostEqual(record.motifs[1].score, 19.6235)
self.assertEqual(record.motifs[2].alphabet, IUPAC.unambiguous_dna)
@@ -125,7 +125,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[2].instances[15]), "GACCTGGAGGCTTAGACTTGG")
self.assertEqual(str(record.motifs[2].instances[16]), "GCGCTCTTCCCAAGCGATCCG")
self.assertEqual(str(record.motifs[2].instances[17]), "GGGCCGTCAGCTCTCAAGTCT")
- self.assertEqual(record.motifs[2].mask, [1,0,1,1,0,1,0,0,0,1,1,0,0,0,1,0,1,0,0,1,1])
+ self.assertEqual(record.motifs[2].mask, (1,0,1,1,0,1,0,0,0,1,1,0,0,0,1,0,1,0,0,1,1))
self.assertAlmostEqual(record.motifs[2].score, 19.1804)
self.assertEqual(record.motifs[3].alphabet, IUPAC.unambiguous_dna)
@@ -146,7 +146,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[3].instances[13]), "GCGATCAGCTTGTGGGCGTGC")
self.assertEqual(str(record.motifs[3].instances[14]), "GACAAATCGGATACTGGGGCA")
self.assertEqual(str(record.motifs[3].instances[15]), "GCACTTAGCAGCGTATCGTTA")
- self.assertEqual(record.motifs[3].mask, [1,1,1,0,0,0,0,1,1,0,0,0,0,1,0,0,1,1,1,0,1])
+ self.assertEqual(record.motifs[3].mask, (1,1,1,0,0,0,0,1,1,0,0,0,0,1,0,0,1,1,1,0,1))
self.assertAlmostEqual(record.motifs[3].score, 18.0097)
self.assertEqual(record.motifs[4].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[4].instances), 15)
@@ -165,7 +165,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[4].instances[12]), "ATGCTTAGAGGTT")
self.assertEqual(str(record.motifs[4].instances[13]), "AGACTTGGGCGAT")
self.assertEqual(str(record.motifs[4].instances[14]), "CGACCTGGAGGCT")
- self.assertEqual(record.motifs[4].mask, [1,1,0,1,0,1,1,1,1,1,1,0,1])
+ self.assertEqual(record.motifs[4].mask, (1,1,0,1,0,1,1,1,1,1,1,0,1))
self.assertAlmostEqual(record.motifs[4].score, 16.8287)
self.assertEqual(record.motifs[5].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[5].instances), 18)
@@ -187,7 +187,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[5].instances[15]), "CTCTGCGTCGCATGGCGGCGTGG")
self.assertEqual(str(record.motifs[5].instances[16]), "GGAGGCTTAGACTTGGGCGATAC")
self.assertEqual(str(record.motifs[5].instances[17]), "GCATGGAGAGAGATCCGGAGGAG")
- self.assertEqual(record.motifs[5].mask, [1,0,1,0,1,1,0,0,0,1,0,0,0,0,1,0,1,1,0,0,1,0,1])
+ self.assertEqual(record.motifs[5].mask, (1,0,1,0,1,1,0,0,0,1,0,0,0,0,1,0,1,1,0,0,1,0,1))
self.assertAlmostEqual(record.motifs[5].score, 15.0441)
self.assertEqual(record.motifs[6].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[6].instances), 20)
@@ -211,7 +211,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[6].instances[17]), "GCTCGTCTATGGTCA")
self.assertEqual(str(record.motifs[6].instances[18]), "GCGCATGCTGGATCC")
self.assertEqual(str(record.motifs[6].instances[19]), "GGCCGTCAGCTCTCA")
- self.assertEqual(record.motifs[6].mask, [1,1,0,1,1,1,1,0,1,0,1,0,0,1,1])
+ self.assertEqual(record.motifs[6].mask, (1,1,0,1,1,1,1,0,1,0,1,0,0,1,1))
self.assertAlmostEqual(record.motifs[6].score, 13.3145)
self.assertEqual(record.motifs[7].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[7].instances), 20)
@@ -235,7 +235,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[7].instances[17]), "AGTCAATGACACGCGCCTGGG")
self.assertEqual(str(record.motifs[7].instances[18]), "GGTCATGGAATCTTATGTAGC")
self.assertEqual(str(record.motifs[7].instances[19]), "GTAGATAACAGAGGTCGGGGG")
- self.assertEqual(record.motifs[7].mask, [1,0,0,1,0,0,0,1,1,0,0,1,1,0,0,0,1,1,0,1,1])
+ self.assertEqual(record.motifs[7].mask, (1,0,0,1,0,0,0,1,1,0,0,1,1,0,0,0,1,1,0,1,1))
self.assertAlmostEqual(record.motifs[7].score, 11.6098)
self.assertEqual(record.motifs[8].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[8].instances), 14)
@@ -253,7 +253,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[8].instances[11]), "GAGATCCGGAGGAGG")
self.assertEqual(str(record.motifs[8].instances[12]), "GCGATCCGAGGGCCG")
self.assertEqual(str(record.motifs[8].instances[13]), "GAGTTCACATGGCTG")
- self.assertEqual(record.motifs[8].mask, [1,0,1,0,0,1,1,0,1,1,1,1,1,0,1])
+ self.assertEqual(record.motifs[8].mask, (1,0,1,0,0,1,1,0,1,1,1,1,1,0,1))
self.assertAlmostEqual(record.motifs[8].score, 11.2943)
self.assertEqual(record.motifs[9].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[9].instances), 18)
@@ -275,7 +275,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[9].instances[15]), "TCTAGGCGGGC")
self.assertEqual(str(record.motifs[9].instances[16]), "AGTATGCTTAG")
self.assertEqual(str(record.motifs[9].instances[17]), "TGGAGGCTTAG")
- self.assertEqual(record.motifs[9].mask, [1,1,1,1,0,1,1,1,1,1,1])
+ self.assertEqual(record.motifs[9].mask, (1,1,1,1,0,1,1,1,1,1,1))
self.assertAlmostEqual(record.motifs[9].score, 9.7924)
self.assertEqual(record.motifs[10].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[10].instances), 13)
@@ -292,7 +292,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[10].instances[10]), "ATCCTCTGCGTCGCATGGCGG")
self.assertEqual(str(record.motifs[10].instances[11]), "GACCATAGACGAGCATCAAAG")
self.assertEqual(str(record.motifs[10].instances[12]), "GGCCCTCGGATCGCTTGGGAA")
- self.assertEqual(record.motifs[10].mask, [1,0,1,1,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,1,1])
+ self.assertEqual(record.motifs[10].mask, (1,0,1,1,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,1,1))
self.assertAlmostEqual(record.motifs[10].score, 9.01393)
self.assertEqual(record.motifs[11].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[11].instances), 16)
@@ -312,7 +312,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[11].instances[13]), "GCGATCCGAG")
self.assertEqual(str(record.motifs[11].instances[14]), "AGTGCGCGTC")
self.assertEqual(str(record.motifs[11].instances[15]), "AGTGCCCGAG")
- self.assertEqual(record.motifs[11].mask, [1,1,1,1,1,1,1,1,1,1])
+ self.assertEqual(record.motifs[11].mask, (1,1,1,1,1,1,1,1,1,1))
self.assertAlmostEqual(record.motifs[11].score, 7.51121)
self.assertEqual(record.motifs[12].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[12].instances), 16)
@@ -332,7 +332,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[12].instances[13]), "GCACGTAGCTGGTAAATAGG")
self.assertEqual(str(record.motifs[12].instances[14]), "GCGGCGTGGATTTCATACAG")
self.assertEqual(str(record.motifs[12].instances[15]), "CCTGGAGGCTTAGACTTGGG")
- self.assertEqual(record.motifs[12].mask, [1,1,0,1,1,0,0,1,1,0,1,0,0,0,1,0,0,0,1,1])
+ self.assertEqual(record.motifs[12].mask, (1,1,0,1,1,0,0,1,1,0,1,0,0,0,1,0,0,0,1,1))
self.assertAlmostEqual(record.motifs[12].score, 5.63667)
self.assertEqual(record.motifs[13].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[13].instances), 15)
@@ -351,7 +351,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[13].instances[12]), "ACGCACGGGACTTCAACCAG")
self.assertEqual(str(record.motifs[13].instances[13]), "GCACGTAGCTGGTAAATAGG")
self.assertEqual(str(record.motifs[13].instances[14]), "ATCCTCTGCGTCGCATGGCG")
- self.assertEqual(record.motifs[13].mask, [1,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,0,0,1,1])
+ self.assertEqual(record.motifs[13].mask, (1,1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,0,0,1,1))
self.assertAlmostEqual(record.motifs[13].score, 3.89842)
self.assertEqual(record.motifs[14].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[14].instances), 14)
@@ -369,7 +369,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[14].instances[11]), "TACTCCGGGTAC")
self.assertEqual(str(record.motifs[14].instances[12]), "GACGCAGAGGAT")
self.assertEqual(str(record.motifs[14].instances[13]), "TAGGCGGGCCAT")
- self.assertEqual(record.motifs[14].mask, [1,1,1,1,1,0,1,1,1,0,1,1])
+ self.assertEqual(record.motifs[14].mask, (1,1,1,1,1,0,1,1,1,0,1,1))
self.assertAlmostEqual(record.motifs[14].score, 3.33444)
self.assertEqual(record.motifs[15].alphabet, IUPAC.unambiguous_dna)
self.assertEqual(len(record.motifs[15].instances), 21)
@@ -394,7 +394,7 @@ def test_alignace_parsing(self):
self.assertEqual(str(record.motifs[15].instances[18]), "AGGCTCGCACGTAGCTGG")
self.assertEqual(str(record.motifs[15].instances[19]), "CCACGCCGCCATGCGACG")
self.assertEqual(str(record.motifs[15].instances[20]), "AGCCTCCAGGTCGCATGG")
- self.assertEqual(record.motifs[15].mask, [1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,1])
+ self.assertEqual(record.motifs[15].mask, (1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,1))
self.assertAlmostEqual(record.motifs[15].score, 1.0395)
def test_pfm_parsing(self):

0 comments on commit 9d0ff19

Please sign in to comment.