1 from obitools.location import Location,locationGenerator
2 import logging
3 import sys
4 import re
5
6
7
8
9 _featureMatcher = re.compile('^(FT| ) [^ ].+\n((FT| ) .+\n)+',re.M)
10 _featureCleaner = re.compile('^FT',re.M)
11
12
14 '''
15 Iterate through a textual description of a feature table in a genbank
16 or embl format. Return at each step a text representation of each individual
17 feature composing the table.
18
19 @param fttable: a string corresponding to the feature table of a genbank
20 or an embl entry
21
22 @type fttable: C{str}
23
24 @return: an iterator on str
25 @rtype: iterator
26
27 @see: L{ftParser}
28 '''
29 for m in _featureMatcher.finditer(fttable):
30 t = m.group()
31 t = _featureCleaner.sub(' ',t)
32 yield t
33
34 _qualifierMatcher = re.compile('(?<=^ {21}/).+(\n {21}[^/].+)*',re.M)
35 _qualifierCleanner= re.compile("^ +",re.M)
36
38 '''
39 Parse a textual description of a feature in embl or genbank format
40 as returned by the textFeatureIterator iterator and iterate through
41 the key, value qualified defining this location.
42
43 @param qualifiers: substring containing qualifiers
44 @type qualifiers: str
45
46 @return: an iterator on tuple (key,value), where keys are C{str}
47 @rtype: iterator
48 '''
49 for m in _qualifierMatcher.finditer(qualifiers):
50 t = m.group()
51 t = _qualifierCleanner.sub('',t)
52 t = t.split('=',1)
53 if len(t)==1:
54 t = (t[0],None)
55 else:
56 if t[0]=='translation':
57 value = t[1].replace('\n','')
58 else:
59 value = t[1].replace('\n',' ')
60 try:
61 value = eval(value)
62 except:
63 pass
64 t = (t[0],value)
65 yield t
66
67
68 _ftmatcher = re.compile('(?<=^ {5})\S+')
69 _locmatcher= re.compile('(?<=^.{21})[^/]+',re.DOTALL)
70 _cleanloc = re.compile('[\s\n]+')
71 _qualifiersMatcher = re.compile('^ +/.+',re.M+re.DOTALL)
72
85
86
89 self._fttype=type
90 self._loc=location
91
94
95
101
104
107
110
112 f = Feature(self._fttype,self._loc.simplify())
113 f.update(self)
114 return f
115
117 return str(self._loc)
118
121
124
127
130
133
134 ftType = property(getFttype, None, None, "Feature type name")
135
137 assert (self.getBegin() + s) > 0,"shift to large (%d)" % s
138 if s == 0:
139 return self
140 f = Feature(self._fttype,self._loc.shift(s))
141 f.update(self)
142 return f
143
144
147
150
151 begin = property(getBegin,None,None,"beginning position of the location")
152 end = property(getEnd,None,None,"ending position of the location")
153
154
165
178