-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequence.py
121 lines (88 loc) · 3.31 KB
/
Sequence.py
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
"""
Defines the Sequence object. Contains a nucleotide sequence plus information such as name,
a header string, accession number.
"""
from sequtil import wrap
__version__ = '$Id$'
class Seq(object):
"""
The Sequence class"""
def __init__( self, name, seq, hea=None, data=None, **kwargs):
"""
name name of sequene
seq sequence
[hea] header information
[data] a dict containing arbitrary data
!!!!! ALL INDEXING IN THIS CLASS IS 0-BASED - EXCEPT getSeq1() !!!!!
"""
self.name = name
self.seq = seq
if hea:
self.hea = hea
else:
self.hea = ''
if data:
self.data = data
else:
self.data = {}
self.keys = ['name','seq','hea']
for k,v in kwargs.items():
self.keys.append(k)
setattr(self, k, v)
def __repr__( self ):
"""Special method to return string representation of the
whole sequence object when `obj` or repr(obj) is called"""
return """>%s%s\n%s""" % (self.name, self.hea, wrap(self.seq, 60))
def __str__( self ):
"""Special method to return sequence string when str(obj)
is called"""
return self.seq
def __eq__( self, other ):
"""Special method executed when a sequence objects is compared
to self; returns 1 if sequences are idential (irrespective of case) and 0
otherwise; does not take into account name or other fields. If other
is a string, compares to self.seq"""
try:
return self.seq.upper() == other.seq.upper()
except AttributeError:
return self.seq.upper() == other.upper()
def __len__( self ):
"""Special method execued when len( obj ) is called; returns length of
sequence"""
return len( self.seq )
def __getitem__( self, index ):
"""Special method to obtain single elements from sequence string
by calling obj[key];
!!!!! INDEXES ARE 0-BASED !!!!!"""
return self.seq[index]
def __getslice__( self, start, end ):
"""Special method to obtain object slices as in obj[i:j]
!!!!! INDEXES ARE 0-BASED !!!!!
"""
return self.seq[start:end]
def __contains__( self, substring ):
"""Special method called when 'item in obj' is executed; returns 0 or 1
"""
found = self.seq.upper().find( substring.upper() )
if found == -1:
found = 0
else:
found = 1
return found
def __add__( self, seqObj ):
"""Special method to concatenate two sequence objects. All properties
of the leftmost Seq object are retained. """
newStr = self.seq + seqObj.getSeq()
return Seq( name=self.name, seq=newStr, data=self.data )
def __call__( self ):
"""Special method executed when obj() is called; returns the sequence
string"""
return self.seq
def getSeq1( self, start, end ):
"""Return sequence string from 1-based index |start|
to 1-based index |end|, both inclusive; only
positive indexes allowed"""
if start <= 0 or end <= 0:
raise IndexError, "Zero or negative index"
start = start - 1
return self.seq[ start:end ]