File: test_CAPS.py

package info (click to toggle)
python-biopython 1.45-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 18,192 kB
  • ctags: 12,310
  • sloc: python: 83,505; xml: 13,834; ansic: 7,015; cpp: 1,855; sql: 1,144; makefile: 179
file content (172 lines) | stat: -rw-r--r-- 4,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from Bio import CAPS
import unittest
from Bio.Restriction import *
from Bio.Fasta import FastaAlign
from StringIO import StringIO

import sys
if sys.platform=="win32":
    from tempfile import mktemp
    import os
else :
    from tempfile import NamedTemporaryFile

def createAlignment(alignment):
  """Create a FastaAlignment from an alignment string"""
  alignment = alignment[alignment.find(">"):]

  if sys.platform=="win32":
      # On windows we cannot (re)open the tempfile to read it
      # while it is still open for writing.  And once closed,
      # it gets automatically deleted.
      #
      # This is a crude and potentially "unsafe" work around
      # (as in theory another program could use the same
      # filename in the instant before we open it)
      tmp_name = mktemp()
      tmp = open(tmp_name, "w")
      tmp.write(alignment)
      tmp.close()
  else :
      tmp = NamedTemporaryFile(bufsize=0)
      tmp_name = tmp.name
      tmp.write(alignment)
      #Don't close the file yet...
      tmp.flush()

  #import os
  #assert os.path.isfile(tmp_name), 'Missing temp file "%s"' % tmp_name

  align = FastaAlign.parse_file(tmp.name)

  if sys.platform=="win32":
      #We must remove the temp file ourselves.
      os.remove(tmp.name)
  else :
      #This will delete the temp file
      tmp.close()

  return align

class UnevenAlignment(unittest.TestCase):
  alignment = """
>foo1
aaaaaaaaaaaaaa
>foo2
aaaaaaaa
>foo3
aaaaaaaaaaaaaa
"""

  def setUp(self):
    self.align = createAlignment(self.alignment)

  def test(self):
    self.assertRaises(CAPS.AlignmentHasDifferentLengthsError, CAPS.CAPSMap, self.align)

class FastaAlignmentTest(unittest.TestCase):

  alignment = """"""
  
  enzymes = []
  
  def setUp(self):

    align = createAlignment(self.alignment)
    self.map = CAPS.CAPSMap(align, self.enzymes)

class NonExample(FastaAlignmentTest):

  alignment = """
>Sequence1
aaaaaaaaaaaaaaaaaaaa
>Sequence2
aaaaaaaaaaaaaaaaaaaa
"""
  
  def testNoCAPS(self):
    self.assertEqual(self.map.dcuts, [])

class ResultChecker(FastaAlignmentTest):
  """This class builds an alignment and then checks the results
  
  subclass it and expose fields:

  alignment - Text representation of the alignment to analyze
  enzymes - The enzymes to analyze this with

  
  """

  def check(self):
    self.assertEqual(len(self.map.dcuts), len(self.results))

    for i in range(0,len(self.results)):
      self.assertEqual(self.results[i][0], self.map.dcuts[i].enzyme)
      self.assertEqual(self.results[i][1], self.map.dcuts[i].start)
      self.assertEqual(self.results[i][2], self.map.dcuts[i].cuts_in)
      self.assertEqual(self.results[i][3], self.map.dcuts[i].blocked_in)

class Example1(ResultChecker):

  alignment = """
>0
AAAagaattcTAGATATACCAAACCAGAGAAAACAAATACATAATCGGAGAAATACAGAT
AGAGAGCGAGAGAGATCGACGGCGAAGCTCTTTACCCGGAAACCATTGAAATCGGACGGT
TTAGTGAAAATGGAGGATCAAGTagAtTTTGGGTTCCGTCCGAACGACGAGGAGCTCGTT
GGTCACTATCTCCGTAACAAAATCGAAGGAAACACTAGCCGCGACGTTGAAGTAGCCATC
AGCGAGGTCAACATCTGTAGCTACGATCCTTGGAACTTGCGCTGTAAGTTCCGAATTTTC
>1
AAAagaTttcTAGATATACCAAACCAGAGAAAACAAATACATAATCGGAGAAATACAGAT
AGAGAGCGAGAGAGATCGACGGCGAAGCTCTTTACCCGGAAACCATTGAAATCGGACGGT
TTAGTGAAAATGGAGGATCAAGTagctTTTGGGTTCCGTCCGAACGACGAGGAGCTCGTT
GGTCACTATCTCCGTAACAAAATCGAAGGAAACACTAGCCGCGACGTTGAAGTAGCCATC
AGCGAGGTCAACATCTGTAGCTACGATCCTTGGAACTTGCGCTGTAAGTTCCGAATTTTC
>2
AAAagaTttcTAGATATACCAAACCAGAGAAAACAAATACATAATCGGAGAAATACAGAT
AGAGAGCGAGAGAGATCGACGGCGAAGCTCTTTACCCGGAAACCATTGAAATCGGACGGT
TTAGTGAAAATGGAGGATCAAGTagctTTTGGGTTCCGTCCGAACGACGAGGAGCTCGTT
GGTCACTATCTCCGTAACAAAATCGAAGGAAACACTAGCCGCGACGTTGAAGTAGCCATC
AGCGAGGTCAACATCTGTAGCTACGATCCTTGGAACTTGCGCTGTAAGTTCCGAATTTTC
"""

  enzymes = [EcoRI, AluI]

  results = []
  results.append([EcoRI, 5, [0], [1,2]])
  results.append([AluI, 144, [1,2], [0]])
  
  def test(self):
    self.check()


class TrivialExample(FastaAlignmentTest):

  alignment = """
>1
gaattc
>2
gaactc
"""

  enzymes = [EcoRI]

  def testCAPS(self):
    self.assertEqual(len(self.map.dcuts), 1)
    self.assertEqual(self.map.dcuts[0].enzyme, EcoRI)
    self.assertEqual(self.map.dcuts[0].start, 1)
    self.assertEqual(self.map.dcuts[0].cuts_in, [0])
    self.assertEqual(self.map.dcuts[0].blocked_in, [1])
    
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(UnevenAlignment("test"))
  suite.addTest(TrivialExample("testCAPS"))
  suite.addTest(Example1("test"))

  return suite


if __name__ == "__main__":
  runner = unittest.TextTestRunner()
  runner.run(test_suite())