forked from churchlab/vdj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
437 lines (346 loc) · 14.4 KB
/
__init__.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import string
import types
import xml.etree.cElementTree as ElementTree # for VDJXML parsing
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import generic_dna
from Bio.SeqRecord import SeqRecord
from Bio.SeqFeature import SeqFeature, FeatureLocation
# ===================
# = DATA STRUCTURES =
# ===================
class ImmuneChain(SeqRecord):
"""Data structure to represent an immune chain.
It extends a biopython SeqRecord object with some simpler interface for
common analyses.
"""
def __init__(self, *args, **kw):
"""Initialize ImmuneChain
This is performed either with a prebuilt SeqRecord object or as a
native SeqRecord object.
"""
if len(args) > 0 and isinstance(args[0],SeqRecord): # pre-built SeqRecord
self._init_with_SeqRecord(args[0])
elif kw.has_key('record'): # pre-built SeqRecord
self._init_with_SeqRecord(kw['record'])
else: # native SeqRecord init
SeqRecord.__init__(self,*args,**kw)
# precompute hash on features for performance
self._update_feature_dict()
# load `source` feature qualifiers into annotations and delete `source`
# feature, if it exists
self._process_source_feature()
# define a set for uniq tags
self._tags = set(self.annotations.setdefault('tags',[]))
# force uppercase sequence
# self.seq = self.seq.upper()
def _init_with_SeqRecord(self,record):
# Initialize self using existing SeqRecord object
SeqRecord.__init__(self, seq=record.seq, id=record.id,
name=record.name, description=record.description,
dbxrefs=record.dbxrefs, features=record.features,
annotations=record.annotations,
letter_annotations=record.letter_annotations)
def _update_feature_dict(self):
self._features = {}
for (i,feature) in enumerate(self.features):
self._features.setdefault(feature.type,[]).append(i)
def _process_source_feature(self):
if 'source' in self._features:
if len(self._features['source']) > 1:
raise ValueError, "Found more than one `source` feature in %s" % self.id
for (k,v) in self.features[self._features['source'][0]].qualifiers.iteritems():
if k.startswith('__letter_annotations__'):
self.letter_annotations['.'.join(k.split('.')[1:])] = ''.join(v[0].split())
continue
if k != 'tags' and isinstance(v,types.ListType) and len(v) == 1: v = v[0]
# HACK: To deal with feature qualifier values that break across lines, the
# SeqIO parser loads each line separately, joins them with \n, and then
# replaces the newlines with a space. This is performed in
# `_feed_feature_table` in Scanner.py. However, this inserts spurious spaces
# in sequences that are stored along with my SeqRecord, like alignment
# annotations, so here I delete this extra spaces. Here I manually check for
# specific qualifiers that may be problematic.
if k == 'gapped_reference' or \
k == 'gapped_query':
self.annotations[k] = v.translate(None,string.whitespace)
continue
# for everything else
self.annotations[k] = v
self.features.pop(self._features['source'][0])
self._features.pop('source')
def __getattribute__(self,name):
"""Look for attributes in annotations and features."""
try:
return object.__getattribute__(self,name)
except AttributeError:
pass
try:
return self.annotations[name]
except KeyError:
pass
try:
if len(self._features[name]) > 1: raise AttributeError, "%s is not a unique feature" % name
return self.features[self._features[name][0]]
except KeyError:
pass
raise AttributeError, "couldn't find %s" % name
# define interface to tags object
def add_tags(self,tags):
if isinstance(tags,types.StringTypes):
tags = [tags]
elif isinstance(tags,types.ListType):
tags = list(tags)
else:
raise TypeError, "value must be string type or list type"
tags = set(tags)
self._tags.update(tags)
self.annotations['tags'] = list(self._tags)
return self
def add_tag(self,tag):
return self.add_tags(tag)
def has_tags(self,tags):
if isinstance(tags,types.StringTypes):
tags = [tags]
elif isinstance(tags,types.ListType):
tags = list(tags)
else:
raise TypeError, "value must be string type or list type"
return set(tags) <= self._tags
def has_tag(self,tag):
return self.has_tags(tag)
def del_tags(self,tags):
if isinstance(tags,types.StringTypes):
tags = [tags]
elif isinstance(tags,types.ListType):
tags = list(tags)
else:
raise TypeError, "value must be string type or list type"
for tag in tags:
self._tags.remove(tag)
self.annotations['tags'] = list(self._tags)
return self
def del_tag(self,tag):
return self.del_tags(tag)
# define some functional interface:
@property
def junction(self):
return self.junction_nt
@property
def junction_nt(self):
return self.__getattribute__('CDR3-IMGT').extract(self.seq.tostring())
@property
def junction_aa(self):
return self.__getattribute__('CDR3-IMGT').qualifiers['translation'][0]
@property
def cdr3(self):
return len(self.junction)
@property
def v(self):
return self.__getattribute__('V-REGION').qualifiers['allele'][0]
@property
def v_seq(self):
return self.__getattribute__('V-REGION').extract(self.seq.tostring())
@property
def d(self):
# return self.features[self._features['D-REGION'][0]].qualifiers['allele']
return self.annotations['D-REGION']
@property
def j(self):
return self.__getattribute__('J-REGION').qualifiers['allele'][0]
@property
def j_seq(self):
return self.__getattribute__('J-REGION').extract(self.seq.tostring())
@property
def vj(self):
return '|'.join([self.v,self.j])
@property
def vdj(self):
return '|'.join([self.v,self.d,self.j])
@property
def num_mutations(self):
aln = self.letter_annotations['alignment']
return aln.count('S') + aln.count('I')
def format(self,*args,**kw):
"""Format SeqRecord using any supported format.
The only reason for redefining this is the hack related to storing
user-defined annotations in a source feature.
"""
self._update_feature_dict()
if 'source' in self._features:
# TODO: elim this whole if statement and only leave the else
raise ValueError, "I should never get here"
assert( len(self._features['source']) == 1 )
feature = self.features[ self._features['source'][0] ]
feature.qualifiers.update(self.annotations)
for (k,v) in self.letter_annotations.iteritems(): feature.qualifiers['__letter_annotations__.'+k] = v
else:
feature = SeqFeature( type='source',
location=FeatureLocation(0,len(self)),
qualifiers=self.annotations )
for (k,v) in self.letter_annotations.iteritems(): feature.qualifiers['__letter_annotations__.'+k] = v
self.features.append(feature)
output = SeqRecord.format(self,*args,**kw)
self.features.pop() # the last one, which is the 'source' feat I just added
# self._features.pop('source')
return output
def __len__(self):
return len(self.seq)
def __str__(self):
return self.__repr__()
def __repr__(self):
return self.format('imgt')
# ================
# = INPUT/OUTPUT =
# ================
def parse_imgt(inputfile):
"""Parser for VDJXML
Really just a wrapper around SeqIO that upgrades SeqRecord to ImmuneChain
"""
for record in SeqIO.parse(inputfile,'imgt'):
yield ImmuneChain(record)
def filter_parse_imgt(inputfile,predicate):
"""Parser that takes a predicate function"""
for record in SeqIO.parse(inputfile,'imgt'):
chain = ImmuneChain(record)
if predicate(chain):
yield chain
# ==========================================================================
# =============================
# = TO PARSE OLD VDJXML FILES =
# =============================
class ImmuneChainXML(object):
"""Data structure to represent an immune chain."""
def __init__(self,**kw):
"""Initialize ImmuneChain
seq is 5'->3'
"""
def kw_init(attrib):
if kw.has_key(attrib):
self.__setattr__(attrib,kw[attrib])
kw_init('seq')
kw_init('descr')
kw_init('locus')
kw_init('v')
kw_init('d')
kw_init('j')
kw_init('c')
kw_init('junction')
if kw.has_key('tags'):
tags = kw['tags']
if isinstance(tags,types.StringTypes): tags = [tags]
self.tags = set(tags)
else:
self.tags = set([])
def get_cdr3(self):
return len(self.junction)
def set_cdr3(self,value):
pass
cdr3 = property(fget=get_cdr3,fset=set_cdr3)
def get_vj(self):
return '|'.join([self.v,self.j])
def set_vj(self):
pass
vj = property(fget=get_vj,fset=set_vj)
def get_vdj(self):
return '|'.join([self.v,self.d,self.j])
def set_vdj(self):
pass
vdj = property(fget=get_vdj,fset=set_vdj)
def add_tags(self,tagset):
if isinstance(tagset,types.StringTypes): tagset = [tagset]
self.tags.update(tagset)
def add_tag(self,tag):
self.add_tags(tag)
def remove_tags(self,tagset):
if isinstance(tagset,types.StringTypes): tagset = [tagset]
for tag in tagset: self.tags.remove(tag)
def remove_tag(self,tag):
self.remove_tags(tag)
def has_tag(self,tag):
if tag in self.tags:
return True
else:
return False
def __len__(self):
return len(self.seq)
def __str__(self):
return self.__repr__()
def __repr__(self):
return self.get_XML()
def get_XML(self):
format_xml = lambda attrib,value: "\t<%(attrib)s>%(value)s</%(attrib)s>\n" % {'attrib':attrib,'value':value}
xmlstring = '<ImmuneChain>\n'
for (attrib,value) in self.__dict__.iteritems():
if attrib == 'tags':
for tag in self.tags:
xmlstring += format_xml('tag',tag)
else:
xmlstring += format_xml(attrib,value)
xmlstring += '</ImmuneChain>\n'
return xmlstring
class ParserVDJXML(object):
"""Parser for VDJXML"""
def __init__(self):
self.chain = None
def start_handler(self,elem):
if elem.tag == 'ImmuneChain':
self.chain = ImmuneChainXML()
def end_handler(self,elem):
if elem.tag == 'tag':
self.chain.add_tags(elem.text)
elif elem.tag == 'v_end_idx' or elem.tag == 'j_start_idx':
self.chain.__setattr__(elem.tag,int(elem.text))
else:
self.chain.__setattr__(elem.tag,elem.text)
def parse(self,inputfile):
for event, elem in ElementTree.iterparse(inputfile,events=('start','end')):
if event == 'start':
if elem.tag == 'root': # to ensure non-incorp of <root> obj in chain
pass
else:
self.start_handler(elem)
elif event == 'end':
if elem.tag == 'ImmuneChain':
yield self.chain
elif elem.tag == 'root': # to ensure clean exit at end of file
pass
else:
self.end_handler(elem)
class PredicateParserVDJXML(ParserVDJXML):
"""VDJXML Parser that takes a predicate function"""
def __init__(self,predicate):
ParserVDJXML.__init__(self)
self.predicate = predicate
def parse(self,inputfile):
for event, elem in ElementTree.iterparse(inputfile,events=('start','end')):
if event == 'start':
self.start_handler(elem)
elif event == 'end':
if elem.tag == 'ImmuneChain':
if self.predicate(self.chain) == True:
yield self.chain
else:
self.end_handler(elem)
def parse_VDJXML(inputfile):
vdjxmlparser = ParserVDJXML()
return vdjxmlparser.parse(inputfile)
def filter_parse_VDJXML(inputfile,predicate):
vdjxmlparser = PredicateParserVDJXML(predicate)
return vdjxmlparser.parse(inputfile)
def xml2imgt(chainXML):
seq = Seq(chainXML.seq,generic_dna)
chain = ImmuneChain(seq=seq,id=chainXML.descr,name=chainXML.descr,description=chainXML.descr)
chain.annotations['barcode'] = chainXML.barcode
chain.add_tags(list(chainXML.tags))
chain.annotations['clone'] = chainXML.clone
if "coding" not in chain.tags: raise ValueError, "I want coding chains only."
vfeature = SeqFeature(location=FeatureLocation(0,chainXML.v_end_idx),type='V-REGION',strand=1,qualifiers={"allele":[chainXML.v]})
jfeature = SeqFeature(location=FeatureLocation(chainXML.j_start_idx,len(seq)),type='J-REGION',strand=1,qualifiers={"allele":[chainXML.j]})
cdr3feature = SeqFeature(location=FeatureLocation(chainXML.v_end_idx+3,chainXML.j_start_idx-3),type='CDR3-IMGT',strand=1)
chain.features.append(vfeature)
chain.features.append(jfeature)
chain.features.append(cdr3feature)
chain._update_feature_dict()
chain._process_source_feature()
return chain