1 """Parsing of GO annotation of TigrGeneIndices's TC sequence --yong27, 2005-05-03
2 """
3 import sys, unittest
4
5 class Go:
6 def __init__(self, aRecordLine):
7 words = aRecordLine.split()
8 self.id = words[0].split(':')[1]
9 self.proof = words[-1]
10 self.category = words[-2]
11 self.description = ' '.join(words[1:-2])
12
13 class TcGo:
14 def __init__(self, aTcId):
15 self.id = aTcId
16 self.gos = dict()
17
18 def addGo(self, aGo):
19 self.gos[aGo.id] = aGo
20
21 def getGo(self, aGoId):
22 return self.gos.get(aGoId)
23
24 class TcGoGenerator:
25 def __init__(self, aFile):
26 self.file = aFile
27 self.g = self.parse()
28
29 def parse(self):
30 strRecord = list()
31 for line in self.file:
32 if line[0]=='>' and strRecord:
33 yield '\n'.join(strRecord)
34 strRecord = list()
35 strRecord.append(line.strip())
36 else:
37 yield '\n'.join(strRecord)
38
39 def nextRecord(self):
40 return self.g.next()
41 def next(self):
42 lines = self.nextRecord().splitlines()
43 tcid = lines[0][1:]
44 tc = TcGo(tcid)
45 for line in lines[1:]:
46 tc.addGo(Go(line))
47 return tc
48 def __iter__(self):
49 return self
50
51 class GoTest(unittest.TestCase):
52 def setUp(self):
53 self.go = Go('GO:0003774 motor activity F GOA_SPTR|P42025~98.57~74')
54
55 def testGo(self):
56 self.assertEquals('0003774',self.go.id)
57 self.assertEquals('motor activity',self.go.description)
58 self.assertEquals('F',self.go.category)
59
60 def testTcGo(self):
61 tg = TcGo('TC271527')
62 tg.addGo(self.go)
63 self.assertEquals('0003774',tg.getGo('0003774').id)
64
65 class GeneratorTest(unittest.TestCase):
66 def setUp(self):
67 input = """\
68 >TC271527
69 GO:0003774 motor activity F GOA_SPTR|P42025~98.57~74
70 GO:0003779 actin binding F FB|FBgn0011745~89.57~74
71 >TC123456
72 GO:0015629 actin cytoskeleton C FB|FBgn0011745~89.57~74
73 GO:0005200 structural constituent of cytoskeleton F FB|FBgn0011745~89.57~74
74 >TC111111
75 GO:0015629 actin cytoskeleton C FB|FBgn0011745~89.57~74"""
76 self.it = TcGoGenerator(StringIO(input))
77
78 def testNextRecord(self):
79 expected = """\
80 >TC271527
81 GO:0003774 motor activity F GOA_SPTR|P42025~98.57~74
82 GO:0003779 actin binding F FB|FBgn0011745~89.57~74"""
83 self.assertEquals(expected, self.it.nextRecord())
84 expected2 = """\
85 >TC123456
86 GO:0015629 actin cytoskeleton C FB|FBgn0011745~89.57~74
87 GO:0005200 structural constituent of cytoskeleton F FB|FBgn0011745~89.57~74"""
88 self.assertEquals(expected2, self.it.nextRecord())
89 expected3 = """\
90 >TC111111
91 GO:0015629 actin cytoskeleton C FB|FBgn0011745~89.57~74"""
92 self.assertEquals(expected3, self.it.nextRecord())
93 self.assertRaises(StopIteration, self.it.nextRecord)
94
95 def testNextTc(self):
96 self.assertEquals('TC271527', self.it.next().id)
97 self.assertEquals('0015629', self.it.next().getGo('0015629').id)
98
99 if __name__=='__main__':
100 from cStringIO import StringIO
101 unittest.main()