From 31c3a281835a59b44e5675f1b19f7cac66dfb822 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Tue, 13 Sep 2022 08:34:13 +0200
Subject: [PATCH] chain mapping: guarantee that proper EntityViews are attached

---
 modules/mol/alg/pymod/chain_mapping.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/modules/mol/alg/pymod/chain_mapping.py b/modules/mol/alg/pymod/chain_mapping.py
index 286b440aa..a7b2f824f 100644
--- a/modules/mol/alg/pymod/chain_mapping.py
+++ b/modules/mol/alg/pymod/chain_mapping.py
@@ -1089,6 +1089,9 @@ class ChainMapper:
         # strip the reference sequence in alignments to only contain
         # sequence from substructure
         substructure_ref_mdl_alns = dict()
+        mdl_views = dict()
+        for ch in mdl.chains:
+            mdl_views[ch.GetName()] = mdl.Select(f"cname={ch.GetName()}")
         for chem_group, mapping in zip(substructure_chem_groups,
                                        substructure_chem_mapping):
             for ref_ch in chem_group:
@@ -1104,9 +1107,13 @@ class ChainMapper:
                         tmp[idx_in_seq] = ref_seq[idx_in_seq]
                     ref_seq = seq.CreateSequence(ref_ch, ''.join(tmp))
                     ref_seq.AttachView(substructure.Select(f"cname={ref_ch}"))
+                    mdl_seq = full_aln.GetSequence(1)
+                    mdl_seq = seq.CreateSequence(mdl_seq.GetName(),
+                                                 mdl_seq.GetString())
+                    mdl_seq.AttachView(mdl_views[mdl_ch])
                     aln = seq.CreateAlignment()
                     aln.AddSequence(ref_seq)
-                    aln.AddSequence(full_aln.GetSequence(1))
+                    aln.AddSequence(mdl_seq)
                     substructure_ref_mdl_alns[(ref_ch, mdl_ch)] = aln
 
         lddt_scorer = lddt.lDDTScorer(substructure,
-- 
GitLab