stcrpy 1.0.3__py3-none-any.whl → 1.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,13 @@ The MHC class. This is similar to the Fab class.
7
7
 
8
8
  """
9
9
 
10
+ import warnings
11
+
12
+ from Bio import BiopythonWarning
13
+
10
14
  from .Entity import Entity
15
+ from .MHCchain import MHCchain
16
+ from .utils.region_definitions import IMGT_MH1_ABD, IMGT_MH2_ABD
11
17
 
12
18
 
13
19
  class MHC(Entity):
@@ -38,6 +44,23 @@ class MHC(Entity):
38
44
  def _add_tcr(self, tcr=None):
39
45
  self.tcr.append(tcr)
40
46
 
47
+ def copy(self, copy_siblings = True ):
48
+ """
49
+ Return a copy of the MHC object. This returns a shallow copy of the MHC object.
50
+ If the copy_siblings flag is set to True, the antigen and TCR objects will also be copied. Warning - if the copy_siblings flag is set to False, the antigen and TCR objects will not be copied, and the reference will still point to the same TCR and antigen objects as the original.
51
+
52
+ copy_siblings: Whether to copy sibling entities (ie. TCR and Antigen objects). Default True.
53
+
54
+ """
55
+ shallow = super().copy()
56
+ if copy_siblings:
57
+ shallow.antigen = [a.copy(copy_siblings=False) for a in self.get_antigen()]
58
+ shallow.tcr = [t.copy(copy_siblings=False) for t in self.get_TCR()]
59
+ for t in shallow.tcr:
60
+ t.MHC = [shallow if m.id == shallow.id else m.copy(copy_siblings=False) for m in t.get_MHC()]
61
+ t.antigen = [ag.copy(copy_siblings=False) if ag.id not in [a.id for a in shallow.antigen] else [a for a in shallow.antigen if a.id==ag.id][0] for ag in t.antigen]
62
+ return shallow
63
+
41
64
  def get_TCR(self):
42
65
  return self.tcr
43
66
 
@@ -78,6 +101,14 @@ class MHC(Entity):
78
101
  def get_allele_assignments(self):
79
102
  return {c.id: c.get_allele_assignments() for c in self.get_chains()}
80
103
 
104
+ def crop(self, *args, **kwargs):
105
+ """Raises NotImplementedError."""
106
+ raise NotImplementedError()
107
+
108
+ def standardise_chain_names():
109
+ """Raises NotImplementedError."""
110
+ raise NotImplementedError()
111
+
81
112
 
82
113
  class MH1(MHC):
83
114
  """
@@ -89,15 +120,15 @@ class MH1(MHC):
89
120
  if self.MHC_type == "MH1":
90
121
  return "<%s %s%s GA1/GA2=%s; B2M=%s>" % (
91
122
  self.MHC_type,
92
- self.MH1,
93
- self.B2M,
123
+ self.MH1 if self.MH1 else '',
124
+ self.B2M if self.B2M else '',
94
125
  self.MH1,
95
126
  self.B2M,
96
127
  )
97
128
  else:
98
129
  return "<GA1/GA2 %s%s GA1=%s; GA2=%s>" % (
99
- self.GA1,
100
- self.GA2,
130
+ self.GA1 if self.GA1 else '',
131
+ self.GA2 if self.GA1 else '',
101
132
  self.GA1,
102
133
  self.GA2,
103
134
  )
@@ -152,6 +183,60 @@ class MH1(MHC):
152
183
  if hasattr(self, "B2M"):
153
184
  return self.child_dict[self.B2M]
154
185
 
186
+ def crop(self, *, remove_het_atoms: bool = True) -> None:
187
+ """Crop to antigen binding domain.
188
+
189
+ This method mutates the MH1 object.
190
+
191
+ Args:
192
+ remove_het_atoms: remove het atoms from structure as well
193
+
194
+ """
195
+ alpha_chain = self.get_alpha()
196
+ new_alpha_chain = MHCchain(alpha_chain.id)
197
+
198
+ for residue in alpha_chain:
199
+ if residue.id[1] in IMGT_MH1_ABD or (not remove_het_atoms and residue.id[0] != ' '):
200
+ new_alpha_chain.add(residue.copy())
201
+
202
+ new_alpha_chain.analyse(alpha_chain.chain_type)
203
+
204
+ del self[alpha_chain.id]
205
+ self.add(new_alpha_chain)
206
+
207
+ if hasattr(self, 'B2M'):
208
+ del self[self.B2M]
209
+ self.B2M = None
210
+
211
+ def standardise_chain_names(self) -> None:
212
+ """Standardise MHC chain name to A and B2M chain name to B."""
213
+ new_id = []
214
+ new_child_dict = {}
215
+
216
+ for MH1_domain in set(['MH1', 'GA1', 'GA2']):
217
+ if hasattr(self, MH1_domain):
218
+ new_child_dict['A'] = self.child_dict[getattr(self, MH1_domain)]
219
+ setattr(self, MH1_domain, 'A')
220
+ new_id.append('A')
221
+ break
222
+
223
+ if hasattr(self, 'B2M'):
224
+ new_child_dict['B'] = self.child_dict[self.B2M]
225
+ self.B2M = 'B'
226
+ new_id.append('B')
227
+
228
+ with warnings.catch_warnings():
229
+ warnings.simplefilter('ignore', BiopythonWarning)
230
+
231
+ for chain_id, chain in new_child_dict.items():
232
+ chain.id = chain_id
233
+
234
+ self.child_dict = new_child_dict
235
+
236
+ with warnings.catch_warnings():
237
+ warnings.simplefilter('ignore', BiopythonWarning)
238
+ self.id = ''.join(new_id)
239
+
155
240
 
156
241
  class MH2(MHC):
157
242
  """
@@ -194,6 +279,60 @@ class MH2(MHC):
194
279
  if hasattr(self, "GB"):
195
280
  return self.child_dict[self.GB]
196
281
 
282
+ def crop(self, *, remove_het_atoms: bool = True) -> None:
283
+ """Crop to antigen binding domain.
284
+
285
+ This method mutates the MH2 object.
286
+
287
+ Args:
288
+ remove_het_atoms: remove het atoms from structure as well
289
+
290
+ """
291
+ new_child_dict = {}
292
+
293
+ for chain in self:
294
+ new_chain = MHCchain(chain.id)
295
+
296
+ for residue in chain:
297
+ if residue.id[1] in IMGT_MH2_ABD or (not remove_het_atoms and residue.id[0] != ' '):
298
+ new_chain.add(residue.copy())
299
+
300
+ new_chain.analyse(chain.chain_type)
301
+ new_child_dict[new_chain.id] = new_chain
302
+
303
+ for chain_id in new_child_dict:
304
+ del self[chain_id]
305
+
306
+ for new_chain in new_child_dict.values():
307
+ self.add(new_chain)
308
+
309
+ def standardise_chain_names(self) -> None:
310
+ """Standardise MHC chain 1 name to A and MHC chain 2 name to B."""
311
+ new_id = []
312
+ new_child_dict = {}
313
+
314
+ if hasattr(self, 'GA'):
315
+ new_child_dict['A'] = self.child_dict[self.GA]
316
+ self.GA = 'A'
317
+ new_id.append('A')
318
+
319
+ if hasattr(self, 'GB'):
320
+ new_child_dict['B'] = self.child_dict[self.GB]
321
+ self.GB = 'B'
322
+ new_id.append('B')
323
+
324
+ with warnings.catch_warnings():
325
+ warnings.simplefilter('ignore', BiopythonWarning)
326
+
327
+ for chain_id, chain in new_child_dict.items():
328
+ chain.id = chain_id
329
+
330
+ self.child_dict = new_child_dict
331
+
332
+ with warnings.catch_warnings():
333
+ warnings.simplefilter('ignore', BiopythonWarning)
334
+ self.id = ''.join(new_id)
335
+
197
336
 
198
337
  class CD1(MHC):
199
338
  """
@@ -253,6 +392,60 @@ class CD1(MHC):
253
392
  if hasattr(self, "B2M"):
254
393
  return self.child_dict[self.B2M]
255
394
 
395
+ def crop(self, *, remove_het_atoms: bool = True) -> None:
396
+ """Crop to antigen binding domain.
397
+
398
+ This method mutates the CD1 object.
399
+
400
+ Args:
401
+ remove_het_atoms: remove het atoms from structure as well
402
+
403
+ """
404
+ if hasattr(self, 'CD1'):
405
+ alpha_chain = self.child_dict[self.CD1]
406
+
407
+ new_alpha_chain = MHCchain(alpha_chain.id)
408
+
409
+ for residue in alpha_chain:
410
+ if residue.id[1] in IMGT_MH1_ABD or (not remove_het_atoms and residue.id[0] != ' '):
411
+ new_alpha_chain.add(residue.copy())
412
+
413
+ new_alpha_chain.analyse(alpha_chain.chain_type)
414
+
415
+ del self[alpha_chain.id]
416
+ self.add(new_alpha_chain)
417
+
418
+ if hasattr(self, 'B2M'):
419
+ del self[self.B2M]
420
+ self.B2M = None
421
+
422
+ def standardise_chain_names(self) -> None:
423
+ """Standardise CD1 chain name to A and B2M chain name to B."""
424
+ new_id = []
425
+ new_child_dict = {}
426
+
427
+ if hasattr(self, 'CD1'):
428
+ new_child_dict['A'] = self.child_dict[self.CD1]
429
+ self.CD1 = 'A'
430
+ new_id.append('A')
431
+
432
+ if hasattr(self, 'B2M'):
433
+ new_child_dict['B'] = self.child_dict[self.B2M]
434
+ self.B2M = 'B'
435
+ new_id.append('B')
436
+
437
+ with warnings.catch_warnings():
438
+ warnings.simplefilter('ignore', BiopythonWarning)
439
+
440
+ for chain_id, chain in new_child_dict.items():
441
+ chain.id = chain_id
442
+
443
+ self.child_dict = new_child_dict
444
+
445
+ with warnings.catch_warnings():
446
+ warnings.simplefilter('ignore', BiopythonWarning)
447
+ self.id = ''.join(new_id)
448
+
256
449
 
257
450
  class MR1(MHC):
258
451
  """
@@ -309,6 +502,60 @@ class MR1(MHC):
309
502
  if hasattr(self, "B2M"):
310
503
  return self.child_dict[self.B2M]
311
504
 
505
+ def crop(self, *, remove_het_atoms: bool = True) -> None:
506
+ """Crop to antigen binding domain.
507
+
508
+ This method mutates the MR1 object.
509
+
510
+ Args:
511
+ remove_het_atoms: remove het atoms from structure as well
512
+
513
+ """
514
+ if hasattr(self, 'MR1'):
515
+ alpha_chain = self.child_dict[self.CD1]
516
+
517
+ new_alpha_chain = MHCchain(alpha_chain.id)
518
+
519
+ for residue in alpha_chain:
520
+ if residue.id[1] in IMGT_MH1_ABD or (not remove_het_atoms and residue.id[0] != ' '):
521
+ new_alpha_chain.add(residue.copy())
522
+
523
+ new_alpha_chain.analyse(alpha_chain.chain_type)
524
+
525
+ del self[alpha_chain.id]
526
+ self.add(new_alpha_chain)
527
+
528
+ if hasattr(self, 'B2M'):
529
+ del self[self.B2M]
530
+ self.B2M = None
531
+
532
+ def standardise_chain_names(self) -> None:
533
+ """Standardise MR1 chain name to A and B2M chain name to B."""
534
+ new_id = []
535
+ new_child_dict = {}
536
+
537
+ if hasattr(self, 'MR1'):
538
+ new_child_dict['A'] = self.child_dict[self.MR1]
539
+ self.MR1 = 'A'
540
+ new_id.append('A')
541
+
542
+ if hasattr(self, 'B2M'):
543
+ new_child_dict['B'] = self.child_dict[self.B2M]
544
+ self.B2M = 'B'
545
+ new_id.append('B')
546
+
547
+ with warnings.catch_warnings():
548
+ warnings.simplefilter('ignore', BiopythonWarning)
549
+
550
+ for chain_id, chain in new_child_dict.items():
551
+ chain.id = chain_id
552
+
553
+ self.child_dict = new_child_dict
554
+
555
+ with warnings.catch_warnings():
556
+ warnings.simplefilter('ignore', BiopythonWarning)
557
+ self.id = ''.join(new_id)
558
+
312
559
 
313
560
  class scMH1(MHC):
314
561
  """
@@ -385,6 +632,48 @@ class scMH1(MHC):
385
632
  def get_B2M(self):
386
633
  return None
387
634
 
635
+ def crop(self, *, remove_het_atoms: bool = True) -> None:
636
+ """Crop to antigen binding domain.
637
+
638
+ This method mutates the scMH1 object.
639
+
640
+ Args:
641
+ remove_het_atoms: remove het atoms from structure as well
642
+
643
+ """
644
+ alpha_chain = self.get_alpha()
645
+ new_alpha_chain = MHCchain(alpha_chain.id)
646
+
647
+ for residue in alpha_chain:
648
+ if residue.id[1] in IMGT_MH1_ABD or (not remove_het_atoms and residue.id[0] != ' '):
649
+ new_alpha_chain.add(residue.copy())
650
+
651
+ new_alpha_chain.analyse(alpha_chain.chain_type)
652
+
653
+ del self[alpha_chain.id]
654
+ self.add(new_alpha_chain)
655
+
656
+ def standardise_chain_names(self) -> None:
657
+ """Standardise MHC chain name to A."""
658
+ new_child_dict = {}
659
+ for MH1_domain in set(['MH1', 'GA1', 'GA2']):
660
+ if hasattr(self, MH1_domain):
661
+ new_child_dict['A'] = self.child_dict[getattr(self, MH1_domain)]
662
+ setattr(self, MH1_domain, 'A')
663
+ break
664
+
665
+ with warnings.catch_warnings():
666
+ warnings.simplefilter('ignore', BiopythonWarning)
667
+
668
+ for chain_id, chain in new_child_dict.items():
669
+ chain.id = chain_id
670
+
671
+ self.child_dict = new_child_dict
672
+
673
+ with warnings.catch_warnings():
674
+ warnings.simplefilter('ignore', BiopythonWarning)
675
+ self.id = 'A'
676
+
388
677
 
389
678
  class scCD1(MHC):
390
679
  """
@@ -448,6 +737,48 @@ class scCD1(MHC):
448
737
  def get_B2M(self):
449
738
  return None
450
739
 
740
+ def crop(self, *, remove_het_atoms: bool = True) -> None:
741
+ """Crop to antigen binding domain.
742
+
743
+ This method mutates the CD1 object.
744
+
745
+ Args:
746
+ remove_het_atoms: remove het atoms from structure as well
747
+
748
+ """
749
+ if hasattr(self, 'CD1'):
750
+ alpha_chain = self.child_dict[self.CD1]
751
+
752
+ new_alpha_chain = MHCchain(alpha_chain.id)
753
+
754
+ for residue in alpha_chain:
755
+ if residue.id[1] in IMGT_MH1_ABD or (not remove_het_atoms and residue.id[0] != ' '):
756
+ new_alpha_chain.add(residue.copy())
757
+
758
+ new_alpha_chain.analyse(alpha_chain.chain_type)
759
+
760
+ del self[alpha_chain.id]
761
+ self.add(new_alpha_chain)
762
+
763
+ def standardise_chain_names(self) -> None:
764
+ """Standardise CD1 chain name to A."""
765
+ new_child_dict = {}
766
+ if hasattr(self, 'CD1'):
767
+ new_child_dict['A'] = self.child_dict[self.CD1]
768
+ self.CD1 = 'A'
769
+
770
+ with warnings.catch_warnings():
771
+ warnings.simplefilter('ignore', BiopythonWarning)
772
+
773
+ for chain_id, chain in new_child_dict.items():
774
+ chain.id = chain_id
775
+
776
+ self.child_dict = new_child_dict
777
+
778
+ with warnings.catch_warnings():
779
+ warnings.simplefilter('ignore', BiopythonWarning)
780
+ self.id = 'A'
781
+
451
782
 
452
783
  class scMH2(MHC):
453
784
  """
@@ -514,3 +845,57 @@ class scMH2(MHC):
514
845
  def get_GB(self):
515
846
  if hasattr(self, "GB"):
516
847
  return self.child_dict[self.GB]
848
+
849
+ def crop(self, *, remove_het_atoms: bool = True) -> None:
850
+ """Crop to antigen binding domain.
851
+
852
+ This method mutates the scMH2 object.
853
+
854
+ Args:
855
+ remove_het_atoms: remove het atoms from structure as well
856
+
857
+ """
858
+ new_child_dict = {}
859
+
860
+ for chain in self:
861
+ new_chain = MHCchain(chain.id)
862
+
863
+ for residue in chain:
864
+ if residue.id[1] in IMGT_MH2_ABD or (not remove_het_atoms and residue.id[0] != ' '):
865
+ new_chain.add(residue.copy())
866
+
867
+ new_chain.analyse(chain.chain_type)
868
+ new_child_dict[new_chain.id] = new_chain
869
+
870
+ for chain_id in new_child_dict:
871
+ del self[chain_id]
872
+
873
+ for new_chain in new_child_dict.values():
874
+ self.add(new_chain)
875
+
876
+ def standardise_chain_names(self) -> None:
877
+ """Standardise MHC chain 1 name to A or MHC chain 2 name to B."""
878
+ new_id = []
879
+ new_child_dict = {}
880
+
881
+ if hasattr(self, 'GA'):
882
+ new_child_dict['A'] = self.child_dict[self.GA]
883
+ self.GA = 'A'
884
+ new_id.append('A')
885
+
886
+ if hasattr(self, 'GB'):
887
+ new_child_dict['B'] = self.child_dict[self.GB]
888
+ self.GB = 'B'
889
+ new_id.append('B')
890
+
891
+ with warnings.catch_warnings():
892
+ warnings.simplefilter('ignore', BiopythonWarning)
893
+
894
+ for chain_id, chain in new_child_dict.items():
895
+ chain.id = chain_id
896
+
897
+ self.child_dict = new_child_dict
898
+
899
+ with warnings.catch_warnings():
900
+ warnings.simplefilter('ignore', BiopythonWarning)
901
+ self.id = ''.join(new_id)