1 | import unittest |
---|
2 | import sys |
---|
3 | import bx.pwm.position_weight_matrix as pwm |
---|
4 | from StringIO import StringIO |
---|
5 | |
---|
6 | basicPwm = \ |
---|
7 | """>MA0101 c-REL REL |
---|
8 | 0 5 8 4 |
---|
9 | 0 1 15 1 |
---|
10 | 1 0 15 1 |
---|
11 | 5 1 9 2 |
---|
12 | 6 5 3 3 |
---|
13 | 5 1 1 10 |
---|
14 | 1 0 0 16 |
---|
15 | 2 0 0 15 |
---|
16 | 0 15 0 2 |
---|
17 | 1 16 0 0 |
---|
18 | """ |
---|
19 | |
---|
20 | transfacPwm = \ |
---|
21 | """ID TATA |
---|
22 | XX |
---|
23 | P0 A C G T |
---|
24 | 01 33 73 78 16 S |
---|
25 | 02 10 24 11 155 T |
---|
26 | 03 176 3 2 19 A |
---|
27 | 04 2 7 3 188 T |
---|
28 | 05 178 2 3 17 A |
---|
29 | 06 133 2 2 63 A |
---|
30 | 07 183 3 10 4 A |
---|
31 | 08 112 2 24 62 W |
---|
32 | 09 78 26 80 16 R |
---|
33 | 10 29 72 75 24 N |
---|
34 | 11 42 74 68 16 N |
---|
35 | 12 42 65 66 27 N |
---|
36 | 13 41 60 67 32 N |
---|
37 | 14 35 54 72 39 N |
---|
38 | 15 40 51 73 36 N |
---|
39 | XX |
---|
40 | """ |
---|
41 | |
---|
42 | background = { 'A':.28,'C':.21, 'G':.24, 'T':.27 } |
---|
43 | |
---|
44 | dSeq = "ACCGAGTTAGCGTAAA" |
---|
45 | dScoresExpected = "-15.3697 0.4240 -16.5309 0.4027" |
---|
46 | |
---|
47 | qSeq = [{'A':0.27,'C':0.34,'G':0.07,'T':0.32}, |
---|
48 | {'A':0.24,'C':0.32,'G':0.09,'T':0.35}, |
---|
49 | {'A':0.80,'C':0.11,'G':0.03,'T':0.06}, |
---|
50 | {'A':0.07,'C':0.22,'G':0.37,'T':0.34}, |
---|
51 | {'A':0.07,'C':0.44,'G':0.03,'T':0.46}, |
---|
52 | {'A':0.43,'C':0.04,'G':0.18,'T':0.35}, |
---|
53 | {'A':0.84,'C':0.14,'G':0.01,'T':0.01}, |
---|
54 | {'A':0.31,'C':0.52,'G':0.13,'T':0.04}, |
---|
55 | {'A':0.22,'C':0.22,'G':0.45,'T':0.11}, |
---|
56 | {'A':0.36,'C':0.15,'G':0.42,'T':0.07}, |
---|
57 | {'A':0.11,'C':0.78,'G':0.07,'T':0.04}, |
---|
58 | {'A':0.07,'C':0.16,'G':0.64,'T':0.13}, |
---|
59 | {'A':0.34,'C':0.59,'G':0.03,'T':0.04}, |
---|
60 | {'A':0.32,'C':0.15,'G':0.07,'T':0.46}, |
---|
61 | {'A':0.07,'C':0.03,'G':0.59,'T':0.31}] |
---|
62 | |
---|
63 | qScoresExpected = "4.1106 0.7810" |
---|
64 | |
---|
65 | class PWMTestCase (unittest.TestCase): |
---|
66 | |
---|
67 | def testReader(self): |
---|
68 | |
---|
69 | # test basic format: i.e. for jaspar |
---|
70 | wms = [wm for wm in pwm.Reader(StringIO(basicPwm),format="basic", \ |
---|
71 | background=background,score_correction=False)] |
---|
72 | assert len(wms) == 1 |
---|
73 | |
---|
74 | # test transfac format |
---|
75 | wms = [wm for wm in pwm.Reader(StringIO(transfacPwm),format="transfac", \ |
---|
76 | background=background,score_correction=False)] |
---|
77 | assert len(wms) == 1 |
---|
78 | |
---|
79 | wm = wms[0] |
---|
80 | dScores = wm.score_seq(dSeq) |
---|
81 | assert len(dScores) == 2 |
---|
82 | assert "%.4f %.4f %.4f %.4f" % (dScores[0][0],dScores[0][1],dScores[1][0],dScores[1][1]) == dScoresExpected |
---|
83 | |
---|
84 | qdSeq = [] |
---|
85 | for (ix,nt) in enumerate(dSeq): |
---|
86 | qdSeq.append(dict()) |
---|
87 | qdSeq[ix][nt] = 1.0 |
---|
88 | qScores = wm.score_seq(qdSeq) |
---|
89 | assert len(qScores) == 2 |
---|
90 | assert "%.4f %.4f %.4f %.4f" % (qScores[0][0],qScores[0][1],qScores[1][0],qScores[1][1]) == dScoresExpected |
---|
91 | |
---|
92 | qScores = wm.score_seq(qSeq) |
---|
93 | assert len(qScores) == 1 |
---|
94 | assert "%.4f %.4f" % (qScores[0][0],qScores[0][1]) == qScoresExpected |
---|
95 | |
---|
96 | test_classes = [PWMTestCase] |
---|
97 | suite = unittest.TestSuite ([unittest.makeSuite (c) for c in test_classes]) |
---|