generalize BlockGenerator to work with string keys
[pygr.git] / tests / sequence_test.py
bloba0667e52fa077f205c7ed821f1d05e6c411799fe
1 import unittest
2 from testlib import testutil, PygrTestProgram
3 from pygr import sequence
5 class Sequence_Test(unittest.TestCase):
6 'basic sequence class tests'
8 def setUp(self):
9 self.seq = sequence.Sequence('atttgactatgctccag', 'foo')
11 def test_length(self):
12 "Sequence lenght"
13 assert len(self.seq) == 17
15 def test_slice(self):
16 "Sequence slice"
17 assert str(self.seq[5:10]) == 'actat'
19 def test_slicerc(self):
20 "Sequence slice then reverse complement"
21 assert str(-(self.seq[5:10])) == 'atagt'
23 def test_rcslice(self):
24 "Sequence reverse complement then slice"
25 assert str((-self.seq)[5:10]) == 'gcata'
27 def test_truncate(self):
28 "Sequence truncate"
29 assert str(self.seq[-202020202:5]) == 'atttg'
30 assert self.seq[-202020202:5] == self.seq[0:5]
31 assert self.seq[-2020202:] == self.seq
32 assert str(self.seq[-202020202:-5]) == 'atttgactatgc'
33 assert str(self.seq[-5:2029]) == 'tccag'
34 assert str(self.seq[-5:]) == 'tccag'
35 try:
36 self.seq[999:10000]
37 raise ValueError('failed to trap out of bounds slice')
38 except IndexError:
39 pass
40 try:
41 self.seq[-10000:-3000]
42 raise ValueError('failed to trap out of bounds slice')
43 except IndexError:
44 pass
45 try:
46 self.seq[1000:]
47 raise ValueError('failed to trap out of bounds slice')
48 except IndexError:
49 pass
51 def test_rctruncate(self):
52 "Sequence reverse complement truncate"
53 seq= -self.seq
54 assert str(seq[-202020202:5]) == 'ctgga'
55 assert seq[-202020202:5] == seq[0:5]
56 assert seq[-2020202:] == seq
57 assert str(seq[-202020202:-5]) == 'ctggagcatagt'
58 assert str(seq[-5:2029]) == 'caaat'
59 assert str(seq[-5:]) == 'caaat'
60 try:
61 seq[999:10000]
62 raise ValueError('failed to trap out of bounds slice')
63 except IndexError:
64 pass
65 try:
66 seq[-10000:-3000]
67 raise ValueError('failed to trap out of bounds slice')
68 except IndexError:
69 pass
70 try:
71 seq[1000:]
72 raise ValueError('failed to trap out of bounds slice')
73 except IndexError:
74 pass
76 def test_join(self):
77 "Sequence join"
78 assert str(self.seq[5:15] * self.seq[8:]) == 'atgctcc'
80 def test_rcjoin(self):
81 "Sequence reverse complement join"
82 assert str((-(self.seq[5:10])) * ((-self.seq)[5:10])) == 'ata'
84 def test_seqtype(self):
85 "Sequence lenght"
86 assert self.seq.seqtype() == sequence.DNA_SEQTYPE
87 assert sequence.Sequence('auuugacuaugcuccag', 'foo').seqtype() == \
88 sequence.RNA_SEQTYPE
89 assert sequence.Sequence('kqwestvvarphal', 'foo').seqtype() == \
90 sequence.PROTEIN_SEQTYPE
92 # @CTB
93 '''
94 #from pygrdata_test import PygrSwissprotBase
95 class Blast_Test(PygrSwissprotBase):
96 'test basic blast functionality'
97 @skip_errors(OSError,KeyError)
98 def setup(self):
99 PygrSwissprotBase.setup(self)
100 import pygr.Data
101 self.sp = pygr.Data.Bio.Seq.Swissprot.sp42()
102 import os
103 blastIndexPath = os.path.join(os.path.dirname(self.sp.filepath),
104 'wikiwacky')
105 self.sp.formatdb(blastIndexPath)
106 def blast(self):
107 hbb = self.sp['HBB1_TORMA']
108 hits = self.sp.blast(hbb)
109 edges = hits[hbb].edges(maxgap=1,maxinsert=1,
110 minAlignSize=14,pIdentityMin=0.5)
111 for t in edges:
112 assert len(t[0])>=14, 'result shorter than minAlignSize!'
113 result = [(t[0],t[1],t[2].pIdentity()) for t in edges]
114 store = PygrDataTextFile(os.path.join('results', 'seqdb1.pickle'))
115 correct = store['hbb blast 1']
116 assert approximate_cmp(result,correct,.0001) == 0, 'blast results should match'
117 result = [(t[0],t[1],t[2].pIdentity()) for t in hits[hbb].generateSeqEnds()]
118 correct = store['hbb blast 2']
119 assert approximate_cmp(result,correct,.0001) == 0, 'blast results should match'
120 trypsin = self.sp['PRCA_ANASP']
121 try:
122 hits[trypsin]
123 raise ValueError('failed to catch bad alignment query')
124 except KeyError:
125 pass
126 class Blast_reindex_untest(Blast_Test):
127 'test building blast indexes under a different name'
128 @skip_errors(OSError,KeyError)
129 def setup(self):
130 PygrSwissprotBase.setup(self)
131 import pygr.Data
132 self.sp = pygr.Data.Bio.Seq.Swissprot.sp42()
133 import os
134 blastIndexPath = os.path.join(os.path.dirname(self.sp.filepath),'wikiwacky')
135 self.sp.formatdb()
136 #self.sp.formatdb(blastIndexPath) # FORCE IT TO STORE INDEX WITH DIFFERENT NAME
137 #print 'blastIndexPath is',self.sp.blastIndexPath
141 if __name__ == '__main__':
142 PygrTestProgram(verbosity=2)