Debian Codegraph @

python/obitools3/parsers/embl_genbank_features.pyx

raw
1 #cython: language_level=3
2
3 '''
4 Created on June 12th 2018
5
6 @author: coissac/mercier
7 '''
8
9
10 import logging
11 import re
12 from itertools import chain
13 from obitools3.utils cimport str2bytes
14
15
16 _featureMatcher = re.compile(b'^(FT| ) [^ ].+\n((FT| ) .+\n)+',re.M)
17 _featureCleaner = re.compile(b'^FT',re.M)
18
19 def textFeatureIterator(fttable):
20 '''
21 Iterate through a textual description of a feature table in a genbank
22 or embl format. Return at each step a text representation of each individual
23 feature composing the table.
24
25 @param fttable: a string corresponding to the feature table of a genbank
26 or an embl entry
27
28 @type fttable: C{str}
29
30 @return: an iterator on str
31 @rtype: iterator
32
33 @see: L{ftParser}
34 '''
35 for m in _featureMatcher.finditer(fttable):
36 t = m.group()
37 t = _featureCleaner.sub(b' ',t)
38 yield t
39
40
41 _qualifierMatcher = re.compile(b'(?<=^ {21}/).+(\n {21}[^/].+)*',re.M)
42 _qualifierCleanner= re.compile(b"^ +",re.M)
43
44 def qualifierIterator(qualifiers):
45 '''
46 Parse a textual description of a feature in embl or genbank format
47 as returned by the textFeatureIterator iterator and iterate through
48 the key, value qualified defining this location.
49
50 @param qualifiers: substring containing qualifiers
51 @type qualifiers: str
52
53 @return: an iterator on tuple (key,value), where keys are C{str}
54 @rtype: iterator
55 '''
56 for m in _qualifierMatcher.finditer(qualifiers):
57 t = m.group()
58 t = _qualifierCleanner.sub(b'',t)
59 t = t.split(b'=',1)
60 if len(t)==1:
61 t = (t[0],None)
62 else:
63 if t[0]==b'translation':
64 value = t[1].replace(b'\n',b'')
65 else:
66 value = t[1].replace(b'\n',b' ')
67 try:
68 value = eval(value)
69 if type(value) == str:
70 value = str2bytes(value)
71 except:
72 pass
73 t = (t[0],value)
74 yield t
75
76
77 _ftmatcher = re.compile(b'(?<=^ {5})\S+')
78 _qualifiersMatcher = re.compile(b'^ +/.+',re.M+re.DOTALL)
79
80 def ftParser(feature):
81 fttype = _ftmatcher.search(feature).group()
82 qualifiers=_qualifiersMatcher.search(feature)
83 if qualifiers is not None:
84 qualifiers=qualifiers.group()
85 else:
86 qualifiers=b""
87 logging.debug("Qualifiers regex not matching on \n=====\n%s\n========" % feature)
88
89 return fttype,qualifiers
90
91
92 class Feature(dict):
93
94 def __init__(self,type):
95 self._fttype=type
96
97 def getFttype(self):
98 return self._fttype
99
100 def __str__(self):
101 return repr(self)
102
103 def __repr__(self):
104 return str((self.ftType, dict.__repr__(self)))
105
106 ftType = property(getFttype, None, None, "Feature type name")
107
108
109 def featureFactory(featureDescription):
110 fttype,qualifiers = ftParser(featureDescription)
111 feature = Feature(fttype)
112 feature.raw = featureDescription
113
114 for k,v in qualifierIterator(qualifiers):
115 feature.setdefault(k,[]).append(v)
116
117 return feature
118
119
120 def featureIterator(featureTable,skipError=False):
121 for tft in textFeatureIterator(featureTable):
122 try:
123 feature = featureFactory(tft)
124 except AssertionError,e:
125 logging.debug("Parsing error on feature :\n===============\n%s\n===============" % tft)
126 if not skipError:
127 raise e
128 logging.debug("\t===> Error skipped")
129 continue
130
131 yield feature
132
133
134 def extractTaxon(bytes text, dict tags):
135
136 s = next(featureIterator(text))
137 s = [s]
138
139 t = set(int(v[6:]) for v in chain(*tuple(f[b'db_xref'] for f in s if b'db_xref' in f))
140 if v[0:6]==b'taxon:')
141 if len(t)==1 :
142 taxid=t.pop()
143 if taxid >=0:
144 tags[b'TAXID']=taxid
145
146 t = set(chain(*tuple(f[b'organism'] for f in s if b'organism' in f)))
147 if len(t)==1:
148 tags[b'organism']=t.pop()