2 Chain mapping aims to identify a one-to-one relationship between chains in a
3 reference structure and a model.
11 from scipy.special
import factorial
12 from scipy.special
import binom
23 def _CSel(ent, cnames):
24 """ Returns view with specified chains
26 Ensures that quotation marks are around chain names to not confuse
27 OST query language with weird special characters.
29 query =
"cname=" +
','.join([mol.QueryQuoteName(cname)
for cname
in cnames])
30 return ent.Select(query)
33 """ Result object for the chain mapping functions in :class:`ChainMapper`
35 Constructor is directly called within the functions, no need to construct
36 such objects yourself.
38 def __init__(self, target, model, chem_groups, chem_mapping, mapping, alns,
50 """ Target/reference structure, i.e. :attr:`ChainMapper.target`
52 :type: :class:`ost.mol.EntityView`
58 """ Model structure that gets mapped onto :attr:`~target`
60 Underwent same processing as :attr:`ChainMapper.target`, i.e.
61 only contains peptide/nucleotide chains of sufficient size.
63 :type: :class:`ost.mol.EntityView`
69 """ Groups of chemically equivalent chains in :attr:`~target`
71 Same as :attr:`ChainMapper.chem_group`
73 :class:`list` of :class:`list` of :class:`str` (chain names)
79 """ Assigns chains in :attr:`~model` to :attr:`~chem_groups`.
81 :class:`list` of :class:`list` of :class:`str` (chain names)
87 """ Mapping of :attr:`~model` chains onto :attr:`~target`
89 Exact same shape as :attr:`~chem_groups` but containing the names of the
90 mapped chains in :attr:`~model`. May contain None for :attr:`~target`
91 chains that are not covered. No guarantee that all chains in
92 :attr:`~model` are mapped.
94 :class:`list` of :class:`list` of :class:`str` (chain names)
100 """ Alignments of mapped chains in :attr:`~target` and :attr:`~model`
102 Each alignment is accessible with ``alns[(t_chain,m_chain)]``. First
103 sequence is the sequence of :attr:`target` chain, second sequence the
104 one from :attr:`~model`. The respective :class:`ost.mol.EntityView` are
105 attached with :func:`ost.seq.ConstSequenceHandle.AttachView`.
107 :type: :class:`dict` with key: :class:`tuple` of :class:`str`, value:
108 :class:`ost.seq.AlignmentHandle`
114 """ Placeholder property without any guarantee of being set
116 Different scores get optimized in the various chain mapping algorithms.
117 Some of them may set their final optimal score in that property.
118 Consult the documentation of the respective chain mapping algorithm
119 for more information. Won't be in the return dict of
125 """ Returns flat mapping as :class:`dict` for all mapable chains
127 :param mdl_as_key: Default is target chain name as key and model chain
128 name as value. This can be reversed with this flag.
129 :returns: :class:`dict` with :class:`str` as key/value that describe
132 flat_mapping = dict()
133 for trg_chem_group, mdl_chem_group
in zip(self.
chem_groups,
135 for a,b
in zip(trg_chem_group, mdl_chem_group):
136 if a
is not None and b
is not None:
144 """ Returns JSON serializable summary of results
148 json_dict[
"mapping"] = self.
mapping
150 json_dict[
"alns"] = list()
151 for aln
in self.alns.values():
152 trg_seq = aln.GetSequence(0)
153 mdl_seq = aln.GetSequence(1)
154 aln_dict = {
"trg_ch": trg_seq.GetName(),
"trg_seq": str(trg_seq),
155 "mdl_ch": mdl_seq.GetName(),
"mdl_seq": str(mdl_seq)}
156 json_dict[
"alns"].append(aln_dict)
162 """ Result object for :func:`ChainMapper.GetRepr`
164 Constructor is directly called within the function, no need to construct
165 such objects yourself.
167 :param lDDT: lDDT for this mapping. Depends on how you call
168 :func:`ChainMapper.GetRepr` whether this is backbone only or
170 :type lDDT: :class:`float`
171 :param substructure: The full substructure for which we searched for a
173 :type substructure: :class:`ost.mol.EntityView`
174 :param ref_view: View pointing to the same underlying entity as
175 *substructure* but only contains the stuff that is mapped
176 :type ref_view: :class:`mol.EntityView`
177 :param mdl_view: The matching counterpart in model
178 :type mdl_view: :class:`mol.EntityView`
180 def __init__(self, lDDT, substructure, ref_view, mdl_view):
183 assert(len(ref_view.residues) == len(mdl_view.residues))
205 """ lDDT of representation result
207 Depends on how you call :func:`ChainMapper.GetRepr` whether this is
208 backbone only or full atom lDDT.
210 :type: :class:`float`
216 """ The full substructure for which we searched for a
219 :type: :class:`ost.mol.EntityView`
225 """ View which contains the mapped subset of :attr:`substructure`
227 :type: :class:`ost.mol.EntityView`
233 """ The :attr:`ref_view` representation in the model
235 :type: :class:`ost.mol.EntityView`
241 """ The reference residues
243 :type: class:`mol.ResidueViewList`
245 return self.ref_view.residues
249 """ The model residues
251 :type: :class:`mol.ResidueViewList`
253 return self.mdl_view.residues
257 """ A list of mapped residue whose names do not match (eg. ALA in the
258 reference and LEU in the model).
260 The mismatches are reported as a tuple of :class:`~ost.mol.ResidueView`
261 (reference, model), or as an empty list if all the residue names match.
272 """ Representative backbone positions for reference residues.
274 Thats CA positions for peptides and C3' positions for Nucleotides.
276 :type: :class:`geom.Vec3List`
284 """ Representative backbone positions for model residues.
286 Thats CA positions for peptides and C3' positions for Nucleotides.
288 :type: :class:`geom.Vec3List`
296 """ Representative backbone positions for reference residues.
298 Thats N, CA and C positions for peptides and O5', C5', C4', C3', O3'
299 positions for Nucleotides.
301 :type: :class:`geom.Vec3List`
309 """ Representative backbone positions for reference residues.
311 Thats N, CA and C positions for peptides and O5', C5', C4', C3', O3'
312 positions for Nucleotides.
314 :type: :class:`geom.Vec3List`
322 """ Transformation to superpose mdl residues onto ref residues
324 Superposition computed as minimal RMSD superposition on
325 :attr:`ref_bb_pos` and :attr:`mdl_bb_pos`. If number of positions is
326 smaller 3, the full_bb_pos equivalents are used instead.
328 :type: :class:`ost.geom.Mat4`
341 """ :attr:`mdl_bb_pos` with :attr:`transform applied`
343 :type: :class:`geom.Vec3List`
347 self._superposed_mdl_bb_pos.ApplyTransform(self.
transform)
352 """ RMSD between :attr:`ref_bb_pos` and :attr:`superposed_mdl_bb_pos`
354 :type: :class:`float`
362 """ GDT with one single threshold: 8.0
364 :type: :class:`float`
372 """ GDT with one single threshold: 4.0
374 :type: :class:`float`
382 """ GDT with one single threshold: 2.0
384 :type: :class:`float`
392 """ GDT with one single threshold: 1.0
394 :type: :class:`float`
402 """ query for mdl residues in OpenStructure query language
404 Repr can be selected as ``full_mdl.Select(ost_query)``
406 Returns invalid query if residue numbers have insertion codes.
413 chname = r.GetChain().GetName()
414 rnum = r.GetNumber().GetNum()
415 if chname
not in chain_rnums:
416 chain_rnums[chname] = list()
417 chain_rnums[chname].append(str(rnum))
418 chain_queries = list()
419 for k,v
in chain_rnums.items():
420 q = f
"(cname={mol.QueryQuoteName(k)} and "
421 q += f
"rnum={','.join(v)})"
422 chain_queries.append(q)
427 """ Returns JSON serializable summary of results
430 json_dict[
"lDDT"] = self.
lDDT
431 json_dict[
"ref_residues"] = [r.GetQualifiedName()
for r
in \
433 json_dict[
"mdl_residues"] = [r.GetQualifiedName()
for r
in \
435 json_dict[
"transform"] = list(self.transform.data)
436 json_dict[
"bb_rmsd"] = self.
bb_rmsd
437 json_dict[
"gdt_8"] = self.
gdt_8
438 json_dict[
"gdt_4"] = self.
gdt_4
439 json_dict[
"gdt_2"] = self.
gdt_2
440 json_dict[
"gdt_1"] = self.
gdt_1
446 """ Returns flat mapping of all chains in the representation
448 :param mdl_as_key: Default is target chain name as key and model chain
449 name as value. This can be reversed with this flag.
450 :returns: :class:`dict` with :class:`str` as key/value that describe
453 flat_mapping = dict()
456 flat_mapping[mdl_res.chain.name] = trg_res.chain.name
458 flat_mapping[trg_res.chain.name] = mdl_res.chain.name
461 def _GetFullBBPos(self, residues):
462 """ Helper to extract full backbone positions
464 exp_pep_atoms = [
"N",
"CA",
"C"]
465 exp_nuc_atoms = [
"\"O5'\"",
"\"C5'\"",
"\"C4'\"",
"\"C3'\"",
"\"O3'\""]
468 if r.GetChemType() == mol.ChemType.NUCLEOTIDES:
469 exp_atoms = exp_nuc_atoms
470 elif r.GetChemType() == mol.ChemType.AMINOACIDS:
471 exp_atoms = exp_pep_atoms
473 raise RuntimeError(
"Something terrible happened... RUN...")
474 for aname
in exp_atoms:
475 a = r.FindAtom(aname)
477 raise RuntimeError(
"Something terrible happened... "
479 bb_pos.append(a.GetPos())
482 def _GetBBPos(self, residues):
483 """ Helper to extract single representative position for each residue
487 at = r.FindAtom(
"CA")
489 at = r.FindAtom(
"C3'")
491 raise RuntimeError(
"Something terrible happened... RUN...")
492 bb_pos.append(at.GetPos())
495 def _GetInconsistentResidues(self, ref_residues, mdl_residues):
496 """ Helper to extract a list of inconsistent residues.
498 if len(ref_residues) != len(mdl_residues):
499 raise ValueError(
"Something terrible happened... Reference and "
500 "model lengths differ... RUN...")
501 inconsistent_residues = list()
502 for ref_residue, mdl_residue
in zip(ref_residues, mdl_residues):
503 if ref_residue.name != mdl_residue.name:
504 inconsistent_residues.append((ref_residue, mdl_residue))
505 return inconsistent_residues
509 """ Class to compute chain mappings
511 All algorithms are performed on processed structures which fulfill
512 criteria as given in constructor arguments (*min_pep_length*,
513 "min_nuc_length") and only contain residues which have all required backbone
514 atoms. for peptide residues thats N, CA, C and CB (no CB for GLY), for
515 nucleotide residues thats O5', C5', C4', C3' and O3'.
517 Chain mapping is a three step process:
519 * Group chemically identical chains in *target* using pairwise
520 alignments that are either computed with Needleman-Wunsch (NW) or
521 simply derived from residue numbers (*resnum_alignments* flag).
522 In case of NW, *pep_subst_mat*, *pep_gap_open* and *pep_gap_ext*
523 and their nucleotide equivalents are relevant. Two chains are
524 considered identical if they fulfill the thresholds given by
525 *pep_seqid_thr*, *pep_gap_thr*, their nucleotide equivalents
526 respectively. The grouping information is available as
527 attributes of this class.
529 * Map chains in an input model to these groups. Generating alignments
530 and the similarity criteria are the same as above. You can either
531 get the group mapping with :func:`GetChemMapping` or directly call
532 one of the full fletched one-to-one chain mapping functions which
533 execute that step internally.
535 * Obtain one-to-one mapping for chains in an input model and
536 *target* with one of the available mapping functions. Just to get an
537 idea of complexity. If *target* and *model* are octamers, there are
538 ``8! = 40320`` possible chain mappings.
540 :param target: Target structure onto which models are mapped.
541 Computations happen on a selection only containing
542 polypeptides and polynucleotides.
543 :type target: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
544 :param resnum_alignments: Use residue numbers instead of
545 Needleman-Wunsch to compute pairwise
546 alignments. Relevant for :attr:`~chem_groups`
547 and related attributes.
548 :type resnum_alignments: :class:`bool`
549 :param pep_seqid_thr: Threshold used to decide when two chains are
550 identical. 95 percent tolerates the few mutations
551 crystallographers like to do.
552 :type pep_seqid_thr: :class:`float`
553 :param pep_gap_thr: Additional threshold to avoid gappy alignments with
554 high seqid. By default this is disabled (set to 1.0).
555 This threshold checks for a maximum allowed fraction
556 of gaps in any of the two sequences after stripping
557 terminal gaps. The reason for not just normalizing
558 seqid by the longer sequence is that one sequence
559 might be a perfect subsequence of the other but only
561 :type pep_gap_thr: :class:`float`
562 :param nuc_seqid_thr: Nucleotide equivalent for *pep_seqid_thr*
563 :type nuc_seqid_thr: :class:`float`
564 :param nuc_gap_thr: Nucleotide equivalent for *nuc_gap_thr*
565 :type nuc_gap_thr: :class:`float`
566 :param pep_subst_mat: Substitution matrix to align peptide sequences,
567 irrelevant if *resnum_alignments* is True,
568 defaults to seq.alg.BLOSUM62
569 :type pep_subst_mat: :class:`ost.seq.alg.SubstWeightMatrix`
570 :param pep_gap_open: Gap open penalty to align peptide sequences,
571 irrelevant if *resnum_alignments* is True
572 :type pep_gap_open: :class:`int`
573 :param pep_gap_ext: Gap extension penalty to align peptide sequences,
574 irrelevant if *resnum_alignments* is True
575 :type pep_gap_ext: :class:`int`
576 :param nuc_subst_mat: Nucleotide equivalent for *pep_subst_mat*,
577 defaults to seq.alg.NUC44
578 :type nuc_subst_mat: :class:`ost.seq.alg.SubstWeightMatrix`
579 :param nuc_gap_open: Nucleotide equivalent for *pep_gap_open*
580 :type nuc_gap_open: :class:`int`
581 :param nuc_gap_ext: Nucleotide equivalent for *pep_gap_ext*
582 :type nuc_gap_ext: :class:`int`
583 :param min_pep_length: Minimal number of residues for a peptide chain to be
584 considered in target and in models.
585 :type min_pep_length: :class:`int`
586 :param min_nuc_length: Minimal number of residues for a nucleotide chain to be
587 considered in target and in models.
588 :type min_nuc_length: :class:`int`
589 :param n_max_naive: Max possible chain mappings that are enumerated in
590 :func:`~GetNaivelDDTMapping` /
591 :func:`~GetDecomposerlDDTMapping`. A
592 :class:`RuntimeError` is raised in case of bigger
594 :type n_max_naive: :class:`int`
596 def __init__(self, target, resnum_alignments=False,
597 pep_seqid_thr = 95., pep_gap_thr = 1.0,
598 nuc_seqid_thr = 95., nuc_gap_thr = 1.0,
599 pep_subst_mat = seq.alg.BLOSUM62, pep_gap_open = -11,
600 pep_gap_ext = -1, nuc_subst_mat = seq.alg.NUC44,
601 nuc_gap_open = -4, nuc_gap_ext = -4,
602 min_pep_length = 10, min_nuc_length = 4,
623 pep_subst_mat = pep_subst_mat,
624 pep_gap_open = pep_gap_open,
625 pep_gap_ext = pep_gap_ext,
626 nuc_subst_mat = nuc_subst_mat,
627 nuc_gap_open = nuc_gap_open,
628 nuc_gap_ext = nuc_gap_ext)
636 """Target structure that only contains peptides/nucleotides
638 Contains only residues that have the backbone representatives
639 (CA for peptide and C3' for nucleotides) to avoid ATOMSEQ alignment
640 inconsistencies when switching between all atom and backbone only
643 :type: :class:`ost.mol.EntityView`
649 """Sequences of peptide chains in :attr:`~target`
651 Respective :class:`EntityView` from *target* for each sequence s are
652 available as ``s.GetAttachedView()``
654 :type: :class:`ost.seq.SequenceList`
656 return self._polypep_seqs
660 """Sequences of nucleotide chains in :attr:`~target`
662 Respective :class:`EntityView` from *target* for each sequence s are
663 available as ``s.GetAttachedView()``
665 :type: :class:`ost.seq.SequenceList`
671 """Groups of chemically equivalent chains in :attr:`~target`
673 First chain in group is the one with longest sequence.
675 :getter: Computed on first use (cached)
676 :type: :class:`list` of :class:`list` of :class:`str` (chain names)
681 self._chem_groups.append([s.GetName()
for s
in a.sequences])
686 """MSA for each group in :attr:`~chem_groups`
688 Sequences in MSAs exhibit same order as in :attr:`~chem_groups` and
689 have the respective :class:`ost.mol.EntityView` from *target* attached.
691 :getter: Computed on first use (cached)
692 :type: :class:`ost.seq.AlignmentList`
707 """Reference (longest) sequence for each group in :attr:`~chem_groups`
709 Respective :class:`EntityView` from *target* for each sequence s are
710 available as ``s.GetAttachedView()``
712 :getter: Computed on first use (cached)
713 :type: :class:`ost.seq.SequenceList`
718 s = seq.CreateSequence(a.GetSequence(0).GetName(),
719 a.GetSequence(0).GetGaplessString())
720 s.AttachView(a.GetSequence(0).GetAttachedView())
721 self._chem_group_ref_seqs.AddSequence(s)
726 """ChemType of each group in :attr:`~chem_groups`
728 Specifying if groups are poly-peptides/nucleotides, i.e.
729 :class:`ost.mol.ChemType.AMINOACIDS` or
730 :class:`ost.mol.ChemType.NUCLEOTIDES`
732 :getter: Computed on first use (cached)
733 :type: :class:`list` of :class:`ost.mol.ChemType`
747 """Maps sequences in *model* to chem_groups of target
749 :param model: Model from which to extract sequences, a
750 selection that only includes peptides and nucleotides
751 is performed and returned along other results.
752 :type model: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
753 :returns: Tuple with two lists of length `len(self.chem_groups)` and
754 an :class:`ost.mol.EntityView` representing *model*:
755 1) Each element is a :class:`list` with mdl chain names that
756 map to the chem group at that position.
757 2) Each element is a :class:`ost.seq.AlignmentList` aligning
758 these mdl chain sequences to the chem group ref sequences.
759 3) A selection of *model* that only contains polypeptides and
760 polynucleotides whose ATOMSEQ exactly matches the sequence
761 info in the returned alignments.
765 alns = [seq.AlignmentList()
for x
in self.
chem_groups]
767 for s
in mdl_pep_seqs:
770 s, mol.ChemType.AMINOACIDS,
773 mapping[idx].append(s.GetName())
774 alns[idx].append(aln)
776 for s
in mdl_nuc_seqs:
779 s, mol.ChemType.NUCLEOTIDES,
782 mapping[idx].append(s.GetName())
783 alns[idx].append(aln)
785 return (mapping, alns, mdl)
789 thresholds=[0.5, 1.0, 2.0, 4.0], strategy=
"naive",
790 steep_opt_rate =
None, block_seed_size = 5,
791 block_blocks_per_chem_group = 5,
792 chem_mapping_result =
None):
793 """ Identify chain mapping by optimizing lDDT score
795 Maps *model* chain sequences to :attr:`~chem_groups` and find mapping
796 based on backbone only lDDT score (CA for amino acids C3' for
799 Either performs a naive search, i.e. enumerate all possible mappings or
800 executes a greedy strategy that tries to identify a (close to) optimal
801 mapping in an iterative way by starting from a start mapping (seed). In
802 each iteration, the one-to-one mapping that leads to highest increase
803 in number of conserved contacts is added with the additional requirement
804 that this added mapping must have non-zero interface counts towards the
805 already mapped chains. So basically we're "growing" the mapped structure
806 by only adding connected stuff.
808 The available strategies:
810 * **naive**: Enumerates all possible mappings and returns best
812 * **greedy_fast**: perform all vs. all single chain lDDTs within the
813 respective ref/mdl chem groups. The mapping with highest number of
814 conserved contacts is selected as seed for greedy extension
816 * **greedy_full**: try multiple seeds for greedy extension, i.e. try
817 all ref/mdl chain combinations within the respective chem groups and
818 retain the mapping leading to the best lDDT.
820 * **greedy_block**: try multiple seeds for greedy extension, i.e. try
821 all ref/mdl chain combinations within the respective chem groups and
822 extend them to *block_seed_size*. *block_blocks_per_chem_group*
823 for each chem group are selected for exhaustive extension.
825 Sets :attr:`MappingResult.opt_score` in case of no trivial one-to-one
828 :param model: Model to map
829 :type model: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
830 :param inclusion_radius: Inclusion radius for lDDT
831 :type inclusion_radius: :class:`float`
832 :param thresholds: Thresholds for lDDT
833 :type thresholds: :class:`list` of :class:`float`
834 :param strategy: Strategy to find mapping. Must be in ["naive",
835 "greedy_fast", "greedy_full", "greedy_block"]
836 :type strategy: :class:`str`
837 :param steep_opt_rate: Only relevant for greedy strategies.
838 If set, every *steep_opt_rate* mappings, a simple
839 optimization is executed with the goal of
840 avoiding local minima. The optimization
841 iteratively checks all possible swaps of mappings
842 within their respective chem groups and accepts
843 swaps that improve lDDT score. Iteration stops as
844 soon as no improvement can be achieved anymore.
845 :type steep_opt_rate: :class:`int`
846 :param block_seed_size: Param for *greedy_block* strategy - Initial seeds
847 are extended by that number of chains.
848 :type block_seed_size: :class:`int`
849 :param block_blocks_per_chem_group: Param for *greedy_block* strategy -
850 Number of blocks per chem group that
851 are extended in an initial search
852 for high scoring local solutions.
853 :type block_blocks_per_chem_group: :class:`int`
854 :param chem_mapping_result: Pro param. The result of
855 :func:`~GetChemMapping` where you provided
856 *model*. If set, *model* parameter is not
858 :type chem_mapping_result: :class:`tuple`
859 :returns: A :class:`MappingResult`
862 strategies = [
"naive",
"greedy_fast",
"greedy_full",
"greedy_block"]
863 if strategy
not in strategies:
864 raise RuntimeError(f
"Strategy must be in {strategies}")
866 if chem_mapping_result
is None:
869 chem_mapping, chem_group_alns, mdl = chem_mapping_result
877 one_to_one = _CheckOneToOneMapping(self.
chem_groups, chem_mapping)
878 if one_to_one
is not None:
880 for ref_group, mdl_group
in zip(self.
chem_groups, one_to_one):
881 for ref_ch, mdl_ch
in zip(ref_group, mdl_group):
882 if ref_ch
is not None and mdl_ch
is not None:
883 aln = ref_mdl_alns[(ref_ch, mdl_ch)]
884 aln.AttachView(0, _CSel(self.
target, [ref_ch]))
885 aln.AttachView(1, _CSel(mdl, [mdl_ch]))
886 alns[(ref_ch, mdl_ch)] = aln
893 if strategy ==
"naive":
894 mapping, opt_lddt = _lDDTNaive(self.
target, mdl, inclusion_radius,
896 chem_mapping, ref_mdl_alns,
901 chem_mapping, ref_mdl_alns,
902 inclusion_radius=inclusion_radius,
903 thresholds=thresholds,
904 steep_opt_rate=steep_opt_rate)
905 if strategy ==
"greedy_fast":
906 mapping = _lDDTGreedyFast(the_greed)
907 elif strategy ==
"greedy_full":
908 mapping = _lDDTGreedyFull(the_greed)
909 elif strategy ==
"greedy_block":
910 mapping = _lDDTGreedyBlock(the_greed, block_seed_size,
911 block_blocks_per_chem_group)
913 opt_lddt = the_greed.lDDT(self.
chem_groups, mapping)
916 for ref_group, mdl_group
in zip(self.
chem_groups, mapping):
917 for ref_ch, mdl_ch
in zip(ref_group, mdl_group):
918 if ref_ch
is not None and mdl_ch
is not None:
919 aln = ref_mdl_alns[(ref_ch, mdl_ch)]
920 aln.AttachView(0, _CSel(self.
target, [ref_ch]))
921 aln.AttachView(1, _CSel(mdl, [mdl_ch]))
922 alns[(ref_ch, mdl_ch)] = aln
925 mapping, alns, opt_score = opt_lddt)
929 block_seed_size = 5, block_blocks_per_chem_group = 5,
930 steep_opt_rate =
None, chem_mapping_result =
None,
931 greedy_prune_contact_map =
False):
932 """ Identify chain mapping based on QSScore
934 Scoring is based on CA/C3' positions which are present in all chains of
935 a :attr:`chem_groups` as well as the *model* chains which are mapped to
936 that respective chem group.
938 The following strategies are available:
940 * **naive**: Naively iterate all possible mappings and return best based
943 * **greedy_fast**: perform all vs. all single chain lDDTs within the
944 respective ref/mdl chem groups. The mapping with highest number of
945 conserved contacts is selected as seed for greedy extension.
946 Extension is based on QS-score.
948 * **greedy_full**: try multiple seeds for greedy extension, i.e. try
949 all ref/mdl chain combinations within the respective chem groups and
950 retain the mapping leading to the best QS-score.
952 * **greedy_block**: try multiple seeds for greedy extension, i.e. try
953 all ref/mdl chain combinations within the respective chem groups and
954 extend them to *block_seed_size*. *block_blocks_per_chem_group*
955 for each chem group are selected for exhaustive extension.
957 Sets :attr:`MappingResult.opt_score` in case of no trivial one-to-one
960 :param model: Model to map
961 :type model: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
962 :param contact_d: Max distance between two residues to be considered as
963 contact in qs scoring
964 :type contact_d: :class:`float`
965 :param strategy: Strategy for sampling, must be in ["naive"]
966 :type strategy: :class:`str`
967 :param chem_mapping_result: Pro param. The result of
968 :func:`~GetChemMapping` where you provided
969 *model*. If set, *model* parameter is not
971 :type chem_mapping_result: :class:`tuple`
972 :param greedy_prune_contact_map: Relevant for all strategies that use
973 greedy extensions. If True, only chains
974 with at least 3 contacts (8A CB
975 distance) towards already mapped chains
976 in trg/mdl are considered for
977 extension. All chains that give a
978 potential non-zero QS-score increase
979 are used otherwise (at least one
980 contact within 12A). The consequence
981 is reduced runtime and usually no
982 real reduction in accuracy.
983 :returns: A :class:`MappingResult`
986 strategies = [
"naive",
"greedy_fast",
"greedy_full",
"greedy_block"]
987 if strategy
not in strategies:
988 raise RuntimeError(f
"strategy must be {strategies}")
990 if chem_mapping_result
is None:
993 chem_mapping, chem_group_alns, mdl = chem_mapping_result
1000 one_to_one = _CheckOneToOneMapping(self.
chem_groups, chem_mapping)
1001 if one_to_one
is not None:
1003 for ref_group, mdl_group
in zip(self.
chem_groups, one_to_one):
1004 for ref_ch, mdl_ch
in zip(ref_group, mdl_group):
1005 if ref_ch
is not None and mdl_ch
is not None:
1006 aln = ref_mdl_alns[(ref_ch, mdl_ch)]
1007 aln.AttachView(0, _CSel(self.
target, [ref_ch]))
1008 aln.AttachView(1, _CSel(mdl, [mdl_ch]))
1009 alns[(ref_ch, mdl_ch)] = aln
1015 if strategy ==
"naive":
1016 mapping, opt_qsscore = _QSScoreNaive(self.
target, mdl,
1018 chem_mapping, ref_mdl_alns,
1024 chem_mapping, ref_mdl_alns,
1025 contact_d = contact_d,
1026 steep_opt_rate=steep_opt_rate,
1027 greedy_prune_contact_map = greedy_prune_contact_map)
1028 if strategy ==
"greedy_fast":
1029 mapping = _QSScoreGreedyFast(the_greed)
1030 elif strategy ==
"greedy_full":
1031 mapping = _QSScoreGreedyFull(the_greed)
1032 elif strategy ==
"greedy_block":
1033 mapping = _QSScoreGreedyBlock(the_greed, block_seed_size,
1034 block_blocks_per_chem_group)
1036 opt_qsscore = the_greed.Score(mapping, check=
False)
1039 for ref_group, mdl_group
in zip(self.
chem_groups, mapping):
1040 for ref_ch, mdl_ch
in zip(ref_group, mdl_group):
1041 if ref_ch
is not None and mdl_ch
is not None:
1042 aln = ref_mdl_alns[(ref_ch, mdl_ch)]
1043 aln.AttachView(0, _CSel(self.
target, [ref_ch]))
1044 aln.AttachView(1, _CSel(mdl, [mdl_ch]))
1045 alns[(ref_ch, mdl_ch)] = aln
1048 mapping, alns, opt_score = opt_qsscore)
1051 single_chain_gdtts_thresh=0.4, subsampling=
None,
1052 first_complete=
False, iterative_superposition=
False,
1053 chem_mapping_result =
None):
1054 """Identify chain mapping based on rigid superposition
1056 Superposition and scoring is based on CA/C3' positions which are present
1057 in all chains of a :attr:`chem_groups` as well as the *model*
1058 chains which are mapped to that respective chem group.
1060 Transformations to superpose *model* onto :attr:`ChainMapper.target`
1061 are estimated using all possible combinations of target and model chains
1062 within the same chem groups and build the basis for further extension.
1064 There are four extension strategies:
1066 * **greedy_single_gdtts**: Iteratively add the model/target chain pair
1067 that adds the most conserved contacts based on the GDT-TS metric
1068 (Number of CA/C3' atoms within [8, 4, 2, 1] Angstrom). The mapping
1069 with highest GDT-TS score is returned. However, that mapping is not
1070 guaranteed to be complete (see *single_chain_gdtts_thresh*).
1072 * **greedy_iterative_gdtts**: Same as greedy_single_gdtts except that
1073 the transformation gets updated with each added chain pair.
1075 * **greedy_single_rmsd**: Conceptually similar to greedy_single_gdtts
1076 but the added chain pairs are the ones with lowest RMSD.
1077 The mapping with lowest overall RMSD gets returned.
1078 *single_chain_gdtts_thresh* is only applied to derive the initial
1079 transformations. After that, the minimal RMSD chain pair gets
1080 iteratively added without applying any threshold.
1082 * **greedy_iterative_rmsd**: Same as greedy_single_rmsd exept that
1083 the transformation gets updated with each added chain pair.
1084 *single_chain_gdtts_thresh* is only applied to derive the initial
1085 transformations. After that, the minimal RMSD chain pair gets
1086 iteratively added without applying any threshold.
1088 :param model: Model to map
1089 :type model: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
1090 :param strategy: Strategy to extend mappings from initial transforms,
1091 see description above. Must be in ["greedy_single",
1092 "greedy_iterative", "greedy_iterative_rmsd"]
1093 :type strategy: :class:`str`
1094 :param single_chain_gdtts_thresh: Minimal GDT-TS score for model/target
1095 chain pair to be added to mapping.
1096 Mapping extension for a given
1097 transform stops when no pair fulfills
1098 this threshold, potentially leading to
1099 an incomplete mapping.
1100 :type single_chain_gdtts_thresh: :class:`float`
1101 :param subsampling: If given, only use an equally distributed subset
1102 of all CA/C3' positions for superposition/scoring.
1103 :type subsampling: :class:`int`
1104 :param first_complete: Avoid full enumeration and return first found
1105 mapping that covers all model chains or all
1106 target chains. Has no effect on
1107 greedy_iterative_rmsd strategy.
1108 :type first_complete: :class:`bool`
1109 :param iterative_superposition: Whether to compute inital
1110 transformations with
1111 :func:`ost.mol.alg.IterativeSuperposeSVD`
1113 :func:`ost.mol.alg.SuperposeSVD`
1114 :type iterative_superposition: :class:`bool`
1115 :param chem_mapping_result: Pro param. The result of
1116 :func:`~GetChemMapping` where you provided
1117 *model*. If set, *model* parameter is not
1119 :type chem_mapping_result: :class:`tuple`
1120 :returns: A :class:`MappingResult`
1123 strategies = [
"greedy_single_gdtts",
"greedy_iterative_gdtts",
1124 "greedy_single_rmsd",
"greedy_iterative_rmsd"]
1125 if strategy
not in strategies:
1126 raise RuntimeError(f
"strategy must be {strategies}")
1128 if chem_mapping_result
is None:
1131 chem_mapping, chem_group_alns, mdl = chem_mapping_result
1138 one_to_one = _CheckOneToOneMapping(self.
chem_groups, chem_mapping)
1139 if one_to_one
is not None:
1141 for ref_group, mdl_group
in zip(self.
chem_groups, one_to_one):
1142 for ref_ch, mdl_ch
in zip(ref_group, mdl_group):
1143 if ref_ch
is not None and mdl_ch
is not None:
1144 aln = ref_mdl_alns[(ref_ch, mdl_ch)]
1145 aln.AttachView(0, _CSel(self.
target, [ref_ch]))
1146 aln.AttachView(1, _CSel(mdl, [mdl_ch]))
1147 alns[(ref_ch, mdl_ch)] = aln
1151 trg_group_pos, mdl_group_pos = _GetRefPos(self.
target, mdl,
1154 max_pos = subsampling)
1158 initial_transforms = list()
1159 initial_mappings = list()
1160 for trg_pos, trg_chains, mdl_pos, mdl_chains
in zip(trg_group_pos,
1164 for t_pos, t
in zip(trg_pos, trg_chains):
1165 for m_pos, m
in zip(mdl_pos, mdl_chains):
1166 if len(t_pos) >= 3
and len(m_pos) >= 3:
1167 transform = _GetTransform(m_pos, t_pos,
1168 iterative_superposition)
1170 t_m_pos.ApplyTransform(transform)
1171 gdt = t_pos.GetGDTTS(t_m_pos)
1172 if gdt >= single_chain_gdtts_thresh:
1173 initial_transforms.append(transform)
1174 initial_mappings.append((t,m))
1176 if strategy ==
"greedy_single_gdtts":
1177 mapping = _SingleRigidGDTTS(initial_transforms, initial_mappings,
1179 trg_group_pos, mdl_group_pos,
1180 single_chain_gdtts_thresh,
1181 iterative_superposition, first_complete,
1182 len(self.target.chains),
1185 elif strategy ==
"greedy_iterative_gdtts":
1186 mapping = _IterativeRigidGDTTS(initial_transforms, initial_mappings,
1188 trg_group_pos, mdl_group_pos,
1189 single_chain_gdtts_thresh,
1190 iterative_superposition,
1192 len(self.target.chains),
1195 elif strategy ==
"greedy_single_rmsd":
1196 mapping = _SingleRigidRMSD(initial_transforms, initial_mappings,
1198 trg_group_pos, mdl_group_pos,
1199 iterative_superposition)
1202 elif strategy ==
"greedy_iterative_rmsd":
1203 mapping = _IterativeRigidRMSD(initial_transforms, initial_mappings,
1205 trg_group_pos, mdl_group_pos,
1206 iterative_superposition)
1209 final_mapping = list()
1211 mapped_mdl_chains = list()
1212 for ref_ch
in ref_chains:
1213 if ref_ch
in mapping:
1214 mapped_mdl_chains.append(mapping[ref_ch])
1216 mapped_mdl_chains.append(
None)
1217 final_mapping.append(mapped_mdl_chains)
1220 for ref_group, mdl_group
in zip(self.
chem_groups, final_mapping):
1221 for ref_ch, mdl_ch
in zip(ref_group, mdl_group):
1222 if ref_ch
is not None and mdl_ch
is not None:
1223 aln = ref_mdl_alns[(ref_ch, mdl_ch)]
1224 aln.AttachView(0, _CSel(self.
target, [ref_ch]))
1225 aln.AttachView(1, _CSel(mdl, [mdl_ch]))
1226 alns[(ref_ch, mdl_ch)] = aln
1229 final_mapping, alns)
1232 """ Convenience function to get mapping with currently preferred method
1234 If number of possible chain mappings is <= *n_max_naive*, a naive
1235 QS-score mapping is performed and optimal QS-score is guaranteed.
1236 For anything else, a QS-score mapping with the greedy_full strategy is
1237 performed (greedy_prune_contact_map = True). The default for
1238 *n_max_naive* of 40320 corresponds to an octamer (8!=40320). A
1239 structure with stoichiometry A6B2 would be 6!*2!=1440 etc.
1242 if _NMappingsWithin(self.
chem_groups, chem_mapping_res[0], n_max_naive):
1244 chem_mapping_result=chem_mapping_res)
1247 greedy_prune_contact_map=
True,
1248 chem_mapping_result=chem_mapping_res)
1250 def GetRepr(self, substructure, model, topn=1, inclusion_radius=15.0,
1251 thresholds=[0.5, 1.0, 2.0, 4.0], bb_only=
False,
1252 only_interchain=
False, chem_mapping_result =
None,
1253 global_mapping =
None):
1254 """ Identify *topn* representations of *substructure* in *model*
1256 *substructure* defines a subset of :attr:`~target` for which one
1257 wants the *topn* representations in *model*. Representations are scored
1260 :param substructure: A :class:`ost.mol.EntityView` which is a subset of
1261 :attr:`~target`. Should be selected with the
1262 OpenStructure query language. Example: if you're
1263 interested in residues with number 42,43 and 85 in
1265 ``substructure=mapper.target.Select("cname=A and rnum=42,43,85")``
1266 A :class:`RuntimeError` is raised if *substructure*
1267 does not refer to the same underlying
1268 :class:`ost.mol.EntityHandle` as :attr:`~target`.
1269 :type substructure: :class:`ost.mol.EntityView`
1270 :param model: Structure in which one wants to find representations for
1272 :type model: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
1273 :param topn: Max number of representations that are returned
1274 :type topn: :class:`int`
1275 :param inclusion_radius: Inclusion radius for lDDT
1276 :type inclusion_radius: :class:`float`
1277 :param thresholds: Thresholds for lDDT
1278 :type thresholds: :class:`list` of :class:`float`
1279 :param bb_only: Only consider backbone atoms in lDDT computation
1280 :type bb_only: :class:`bool`
1281 :param only_interchain: Only score interchain contacts in lDDT. Useful
1282 if you want to identify interface patches.
1283 :type only_interchain: :class:`bool`
1284 :param chem_mapping_result: Pro param. The result of
1285 :func:`~GetChemMapping` where you provided
1286 *model*. If set, *model* parameter is not
1288 :type chem_mapping_result: :class:`tuple`
1289 :param global_mapping: Pro param. Specify a global mapping result. This
1290 fully defines the desired representation in the
1291 model but extracts it and enriches it with all
1292 the nice attributes of :class:`ReprResult`.
1293 The target attribute in *global_mapping* must be
1294 of the same entity as self.target and the model
1295 attribute of *global_mapping* must be of the same
1297 :type global_mapping: :class:`MappingResult`
1298 :returns: :class:`list` of :class:`ReprResult`
1302 raise RuntimeError(
"topn must be >= 1")
1304 if global_mapping
is not None:
1306 if global_mapping.target.handle.GetHashCode() != \
1307 self.target.handle.GetHashCode():
1308 raise RuntimeError(
"global_mapping.target must be the same "
1309 "entity as self.target")
1310 if global_mapping.model.handle.GetHashCode() != \
1311 model.handle.GetHashCode():
1312 raise RuntimeError(
"global_mapping.model must be the same "
1313 "entity as model param")
1316 for r
in substructure.residues:
1317 ch_name = r.GetChain().GetName()
1318 rnum = r.GetNumber()
1319 target_r = self.target.FindResidue(ch_name, rnum)
1320 if not target_r.IsValid():
1321 raise RuntimeError(f
"substructure has residue "
1322 f
"{r.GetQualifiedName()} which is not in "
1324 if target_r.handle.GetHashCode() != r.handle.GetHashCode():
1325 raise RuntimeError(f
"substructure has residue "
1326 f
"{r.GetQualifiedName()} which has an "
1327 f
"equivalent in self.target but it does "
1328 f
"not refer to the same underlying "
1331 target_a = target_r.FindAtom(a.GetName())
1332 if not target_a.IsValid():
1333 raise RuntimeError(f
"substructure has atom "
1334 f
"{a.GetQualifiedName()} which is not "
1336 if a.handle.GetHashCode() != target_a.handle.GetHashCode():
1337 raise RuntimeError(f
"substructure has atom "
1338 f
"{a.GetQualifiedName()} which has an "
1339 f
"equivalent in self.target but it does "
1340 f
"not refer to the same underlying "
1344 ca = r.FindAtom(
"CA")
1345 c3 = r.FindAtom(
"C3'")
1347 if not ca.IsValid()
and not c3.IsValid():
1348 raise RuntimeError(
"All residues in substructure must contain "
1349 "a backbone atom named CA or C3\'")
1352 if chem_mapping_result
is None:
1355 chem_mapping, chem_group_alns, mdl = chem_mapping_result
1362 substructure_res_indices = dict()
1363 for ch
in substructure.chains:
1364 full_ch = self.target.FindChain(ch.GetName())
1365 idx = [full_ch.GetResidueIndex(r.GetNumber())
for r
in ch.residues]
1366 substructure_res_indices[ch.GetName()] = idx
1370 substructure_chem_groups = list()
1371 substructure_chem_mapping = list()
1373 chnames = set([ch.GetName()
for ch
in substructure.chains])
1374 for chem_group, mapping
in zip(self.
chem_groups, chem_mapping):
1375 substructure_chem_group = [ch
for ch
in chem_group
if ch
in chnames]
1376 if len(substructure_chem_group) > 0:
1377 substructure_chem_groups.append(substructure_chem_group)
1378 substructure_chem_mapping.append(mapping)
1381 n_mapped_mdl_chains = sum([len(m)
for m
in substructure_chem_mapping])
1382 if n_mapped_mdl_chains == 0:
1387 substructure_ref_mdl_alns = dict()
1389 for ch
in mdl.chains:
1390 mdl_views[ch.GetName()] = _CSel(mdl, [ch.GetName()])
1391 for chem_group, mapping
in zip(substructure_chem_groups,
1392 substructure_chem_mapping):
1393 for ref_ch
in chem_group:
1394 for mdl_ch
in mapping:
1395 full_aln = ref_mdl_alns[(ref_ch, mdl_ch)]
1396 ref_seq = full_aln.GetSequence(0)
1400 tmp = [
'-'] * len(full_aln)
1401 for idx
in substructure_res_indices[ref_ch]:
1402 idx_in_seq = ref_seq.GetPos(idx)
1403 tmp[idx_in_seq] = ref_seq[idx_in_seq]
1404 ref_seq = seq.CreateSequence(ref_ch,
''.join(tmp))
1405 ref_seq.AttachView(_CSel(substructure, [ref_ch]))
1406 mdl_seq = full_aln.GetSequence(1)
1407 mdl_seq = seq.CreateSequence(mdl_seq.GetName(),
1408 mdl_seq.GetString())
1409 mdl_seq.AttachView(mdl_views[mdl_ch])
1410 aln = seq.CreateAlignment()
1411 aln.AddSequence(ref_seq)
1412 aln.AddSequence(mdl_seq)
1413 substructure_ref_mdl_alns[(ref_ch, mdl_ch)] = aln
1416 inclusion_radius = inclusion_radius,
1418 scored_mappings = list()
1422 flat_mapping = global_mapping.GetFlatMapping()
1424 for chem_group, chem_mapping
in zip(substructure_chem_groups,
1425 substructure_chem_mapping):
1426 chem_group_mapping = list()
1427 for ch
in chem_group:
1428 if ch
in flat_mapping:
1429 mdl_ch = flat_mapping[ch]
1430 if mdl_ch
in chem_mapping:
1431 chem_group_mapping.append(mdl_ch)
1433 chem_group_mapping.append(
None)
1435 chem_group_mapping.append(
None)
1436 mapping.append(chem_group_mapping)
1437 mappings = [mapping]
1439 mappings = list(_ChainMappings(substructure_chem_groups,
1440 substructure_chem_mapping,
1443 for mapping
in mappings:
1445 lddt_chain_mapping = dict()
1448 for ref_chem_group, mdl_chem_group
in zip(substructure_chem_groups,
1450 for ref_ch, mdl_ch
in zip(ref_chem_group, mdl_chem_group):
1452 if mdl_ch
is not None:
1453 lddt_chain_mapping[mdl_ch] = ref_ch
1454 aln = substructure_ref_mdl_alns[(ref_ch, mdl_ch)]
1455 lddt_alns[mdl_ch] = aln
1456 tmp = [int(c[0] !=
'-' and c[1] !=
'-')
for c
in aln]
1457 n_res_aln += sum(tmp)
1462 lDDT, _ = lddt_scorer.lDDT(mdl, thresholds=thresholds,
1463 chain_mapping=lddt_chain_mapping,
1464 residue_mapping = lddt_alns,
1465 check_resnames =
False,
1466 no_intrachain = only_interchain)
1469 ost.LogVerbose(
"No valid contacts in the reference")
1474 if len(scored_mappings) == 0:
1475 scored_mappings.append((lDDT, mapping))
1476 elif len(scored_mappings) < topn:
1477 scored_mappings.append((lDDT, mapping))
1478 scored_mappings.sort(reverse=
True, key=
lambda x: x[0])
1479 elif lDDT > scored_mappings[-1][0]:
1480 scored_mappings.append((lDDT, mapping))
1481 scored_mappings.sort(reverse=
True, key=
lambda x: x[0])
1482 scored_mappings = scored_mappings[:topn]
1486 for scored_mapping
in scored_mappings:
1487 ref_view = substructure.handle.CreateEmptyView()
1488 mdl_view = mdl.handle.CreateEmptyView()
1489 for ref_ch_group, mdl_ch_group
in zip(substructure_chem_groups,
1491 for ref_ch, mdl_ch
in zip(ref_ch_group, mdl_ch_group):
1492 if ref_ch
is not None and mdl_ch
is not None:
1493 aln = substructure_ref_mdl_alns[(ref_ch, mdl_ch)]
1495 if col[0] !=
'-' and col[1] !=
'-':
1496 ref_view.AddResidue(col.GetResidue(0),
1497 mol.ViewAddFlag.INCLUDE_ALL)
1498 mdl_view.AddResidue(col.GetResidue(1),
1499 mol.ViewAddFlag.INCLUDE_ALL)
1500 results.append(
ReprResult(scored_mapping[0], substructure,
1501 ref_view, mdl_view))
1505 """ Returns number of possible mappings
1507 :param model: Model with chains that are mapped onto
1509 :type model: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
1515 """ Entity processing for chain mapping
1517 * Selects view containing peptide and nucleotide residues which have
1518 required backbone atoms present - for peptide residues thats
1519 N, CA, C and CB (no CB for GLY), for nucleotide residues thats
1520 O5', C5', C4', C3' and O3'.
1521 * filters view by chain lengths, see *min_pep_length* and
1522 *min_nuc_length* in constructor
1523 * Extracts atom sequences for each chain in that view
1524 * Attaches corresponding :class:`ost.mol.EntityView` to each sequence
1525 * If residue number alignments are used, strictly increasing residue
1526 numbers without insertion codes are ensured in each chain
1528 :param ent: Entity to process
1529 :type ent: :class:`ost.mol.EntityView`/:class:`ost.mol.EntityHandle`
1530 :returns: Tuple with 3 elements: 1) :class:`ost.mol.EntityView`
1531 containing peptide and nucleotide residues 2)
1532 :class:`ost.seq.SequenceList` containing ATOMSEQ sequences
1533 for each polypeptide chain in returned view, sequences have
1534 :class:`ost.mol.EntityView` of according chains attached
1535 3) same for polynucleotide chains
1537 view = ent.CreateEmptyView()
1538 exp_pep_atoms = [
"N",
"CA",
"C",
"CB"]
1539 exp_nuc_atoms = [
"\"O5'\"",
"\"C5'\"",
"\"C4'\"",
"\"C3'\"",
"\"O3'\""]
1540 pep_query =
"peptide=true and aname=" +
','.join(exp_pep_atoms)
1541 nuc_query =
"nucleotide=true and aname=" +
','.join(exp_nuc_atoms)
1543 pep_sel = ent.Select(pep_query)
1544 for r
in pep_sel.residues:
1545 if len(r.atoms) == 4:
1546 view.AddResidue(r.handle, mol.INCLUDE_ALL)
1547 elif r.name ==
"GLY" and len(r.atoms) == 3:
1548 atom_names = [a.GetName()
for a
in r.atoms]
1549 if sorted(atom_names) == [
"C",
"CA",
"N"]:
1550 view.AddResidue(r.handle, mol.INCLUDE_ALL)
1552 nuc_sel = ent.Select(nuc_query)
1553 for r
in nuc_sel.residues:
1554 if len(r.atoms) == 5:
1555 view.AddResidue(r.handle, mol.INCLUDE_ALL)
1557 polypep_seqs = seq.CreateSequenceList()
1558 polynuc_seqs = seq.CreateSequenceList()
1560 if len(view.residues) == 0:
1562 return (view, polypep_seqs, polynuc_seqs)
1564 for ch
in view.chains:
1565 n_res = len(ch.residues)
1566 n_pep = sum([r.IsPeptideLinking()
for r
in ch.residues])
1567 n_nuc = sum([r.IsNucleotideLinking()
for r
in ch.residues])
1570 if n_pep > 0
and n_nuc > 0:
1571 raise RuntimeError(f
"Must not mix peptide and nucleotide linking "
1572 f
"residues in same chain ({ch.GetName()})")
1574 if (n_pep + n_nuc) != n_res:
1575 raise RuntimeError(
"All residues must either be peptide_linking "
1576 "or nucleotide_linking")
1590 ins_codes = [r.GetNumber().GetInsCode()
for r
in ch.residues]
1591 if len(set(ins_codes)) != 1
or ins_codes[0] !=
'\0':
1592 raise RuntimeError(
"Residue numbers in input structures must not "
1593 "contain insertion codes")
1596 nums = [r.GetNumber().GetNum()
for r
in ch.residues]
1597 if not all(i < j
for i, j
in zip(nums, nums[1:])):
1598 raise RuntimeError(
"Residue numbers in input structures must be "
1599 "strictly increasing for each chain")
1601 s =
''.join([r.one_letter_code
for r
in ch.residues])
1602 s = seq.CreateSequence(ch.GetName(), s)
1603 s.AttachView(_CSel(view, [ch.GetName()]))
1605 polypep_seqs.AddSequence(s)
1606 elif n_nuc == n_res:
1607 polynuc_seqs.AddSequence(s)
1609 raise RuntimeError(
"This shouldnt happen")
1611 if len(polypep_seqs) == 0
and len(polynuc_seqs) == 0:
1612 raise RuntimeError(f
"No chain fulfilled minimum length requirement "
1613 f
"to be considered in chain mapping "
1614 f
"({self.min_pep_length} for peptide chains, "
1615 f
"{self.min_nuc_length} for nucleotide chains) "
1616 f
"- mapping failed")
1619 chain_names = [s.GetAttachedView().chains[0].name
for s
in polypep_seqs]
1620 chain_names += [s.GetAttachedView().chains[0].name
for s
in polynuc_seqs]
1621 view = _CSel(view, chain_names)
1623 return (view, polypep_seqs, polynuc_seqs)
1626 """ Access to internal sequence alignment functionality
1628 Alignment parameterization is setup at ChainMapper construction
1630 :param s1: First sequence to align - must have view attached in case
1631 of resnum_alignments
1632 :type s1: :class:`ost.seq.SequenceHandle`
1633 :param s2: Second sequence to align - must have view attached in case
1634 of resnum_alignments
1635 :type s2: :class:`ost.seq.SequenceHandle`
1636 :param stype: Type of sequences to align, must be in
1637 [:class:`ost.mol.ChemType.AMINOACIDS`,
1638 :class:`ost.mol.ChemType.NUCLEOTIDES`]
1639 :returns: Pairwise alignment of s1 and s2
1641 if stype
not in [mol.ChemType.AMINOACIDS, mol.ChemType.NUCLEOTIDES]:
1642 raise RuntimeError(
"stype must be ost.mol.ChemType.AMINOACIDS or "
1643 "ost.mol.ChemType.NUCLEOTIDES")
1644 return self.aligner.Align(s1, s2, chem_type = stype)
1650 def __init__(self, pep_subst_mat = seq.alg.BLOSUM62, pep_gap_open = -5,
1651 pep_gap_ext = -2, nuc_subst_mat = seq.alg.NUC44,
1652 nuc_gap_open = -4, nuc_gap_ext = -4, resnum_aln =
False):
1653 """ Helper class to compute alignments
1655 Sets default values for substitution matrix, gap open and gap extension
1656 penalties. They are only used in default mode (Needleman-Wunsch aln).
1657 If *resnum_aln* is True, only residue numbers of views that are attached
1658 to input sequences are considered.
1668 def Align(self, s1, s2, chem_type=None):
1672 if chem_type
is None:
1673 raise RuntimeError(
"Must specify chem_type for NW alignment")
1674 return self.
NWAlign(s1, s2, chem_type)
1677 """ Returns pairwise alignment using Needleman-Wunsch algorithm
1679 :param s1: First sequence to align
1680 :type s1: :class:`ost.seq.SequenceHandle`
1681 :param s2: Second sequence to align
1682 :type s2: :class:`ost.seq.SequenceHandle`
1683 :param chem_type: Must be in [:class:`ost.mol.ChemType.AMINOACIDS`,
1684 :class:`ost.mol.ChemType.NUCLEOTIDES`], determines
1685 substitution matrix and gap open/extension penalties
1686 :type chem_type: :class:`ost.mol.ChemType`
1687 :returns: Alignment with s1 as first and s2 as second sequence
1689 if chem_type == mol.ChemType.AMINOACIDS:
1693 elif chem_type == mol.ChemType.NUCLEOTIDES:
1698 raise RuntimeError(
"Invalid ChemType")
1702 """ Returns pairwise alignment using residue numbers of attached views
1704 Assumes that there are no insertion codes (alignment only on numerical
1705 component) and that resnums are strictly increasing (fast min/max
1706 identification). These requirements are assured if a structure has been
1707 processed by :class:`ChainMapper`.
1709 :param s1: First sequence to align, must have :class:`ost.mol.EntityView`
1711 :type s1: :class:`ost.seq.SequenceHandle`
1712 :param s2: Second sequence to align, must have :class:`ost.mol.EntityView`
1714 :type s2: :class:`ost.seq.SequenceHandle`
1716 assert(s1.HasAttachedView())
1717 assert(s2.HasAttachedView())
1718 v1 = s1.GetAttachedView()
1719 rnums1 = [r.GetNumber().GetNum()
for r
in v1.residues]
1720 v2 = s2.GetAttachedView()
1721 rnums2 = [r.GetNumber().GetNum()
for r
in v2.residues]
1723 min_num = min(rnums1[0], rnums2[0])
1724 max_num = max(rnums1[-1], rnums2[-1])
1725 aln_length = max_num - min_num + 1
1727 aln_s1 = [
'-'] * aln_length
1728 for r, rnum
in zip(v1.residues, rnums1):
1729 aln_s1[rnum-min_num] = r.one_letter_code
1731 aln_s2 = [
'-'] * aln_length
1732 for r, rnum
in zip(v2.residues, rnums2):
1733 aln_s2[rnum-min_num] = r.one_letter_code
1735 aln = seq.CreateAlignment()
1736 aln.AddSequence(seq.CreateSequence(s1.GetName(),
''.join(aln_s1)))
1737 aln.AddSequence(seq.CreateSequence(s2.GetName(),
''.join(aln_s2)))
1740 def _GetAlnPropsTwo(aln):
1741 """Returns basic properties of *aln* version two...
1743 :param aln: Alignment to compute properties
1744 :type aln: :class:`seq.AlignmentHandle`
1745 :returns: Tuple with 2 elements. 1) sequence identify in range [0, 100]
1746 considering aligned columns 2) Fraction of non-gap characters
1747 in first sequence that are covered by non-gap characters in
1750 assert(aln.GetCount() == 2)
1751 n_tot = sum([1
for col
in aln
if col[0] !=
'-'])
1752 n_aligned = sum([1
for col
in aln
if (col[0] !=
'-' and col[1] !=
'-')])
1753 return (seq.alg.SequenceIdentity(aln), float(n_aligned)/n_tot)
1755 def _GetAlnPropsOne(aln):
1757 """Returns basic properties of *aln* version one...
1759 :param aln: Alignment to compute properties
1760 :type aln: :class:`seq.AlignmentHandle`
1761 :returns: Tuple with 3 elements. 1) sequence identify in range [0, 100]
1762 considering aligned columns 2) Fraction of gaps between
1763 first and last aligned column in s1 3) same for s2.
1765 assert(aln.GetCount() == 2)
1766 n_gaps_1 = str(aln.GetSequence(0)).strip(
'-').count(
'-')
1767 n_gaps_2 = str(aln.GetSequence(1)).strip(
'-').count(
'-')
1768 gap_frac_1 = float(n_gaps_1)/len(aln.GetSequence(0).GetGaplessString())
1769 gap_frac_2 = float(n_gaps_2)/len(aln.GetSequence(1).GetGaplessString())
1770 return (seq.alg.SequenceIdentity(aln), gap_frac_1, gap_frac_2)
1772 def _GetChemGroupAlignments(pep_seqs, nuc_seqs, aligner, pep_seqid_thr=95.,
1773 pep_gap_thr=0.1, nuc_seqid_thr=95.,
1775 """Returns alignments with groups of chemically equivalent chains
1777 :param pep_seqs: List of polypeptide sequences
1778 :type pep_seqs: :class:`seq.SequenceList`
1779 :param nuc_seqs: List of polynucleotide sequences
1780 :type nuc_seqs: :class:`seq.SequenceList`
1781 :param aligner: Helper class to generate pairwise alignments
1782 :type aligner: :class:`_Aligner`
1783 :param pep_seqid_thr: Threshold used to decide when two peptide chains are
1784 identical. 95 percent tolerates the few mutations
1785 crystallographers like to do.
1786 :type pep_seqid_thr: :class:`float`
1787 :param pep_gap_thr: Additional threshold to avoid gappy alignments with high
1788 seqid. The reason for not just normalizing seqid by the
1789 longer sequence is that one sequence might be a perfect
1790 subsequence of the other but only cover half of it. This
1791 threshold checks for a maximum allowed fraction of gaps
1792 in any of the two sequences after stripping terminal gaps.
1793 :type pep_gap_thr: :class:`float`
1794 :param nuc_seqid_thr: Nucleotide equivalent of *pep_seqid_thr*
1795 :type nuc_seqid_thr: :class:`float`
1796 :param nuc_gap_thr: Nucleotide equivalent of *nuc_gap_thr*
1797 :type nuc_gap_thr: :class:`float`
1798 :returns: Tuple with first element being an AlignmentList. Each alignment
1799 represents a group of chemically equivalent chains and the first
1800 sequence is the longest. Second element is a list of equivalent
1801 length specifying the types of the groups. List elements are in
1802 [:class:`ost.ChemType.AMINOACIDS`,
1803 :class:`ost.ChemType.NUCLEOTIDES`]
1805 pep_groups = _GroupSequences(pep_seqs, pep_seqid_thr, pep_gap_thr, aligner,
1806 mol.ChemType.AMINOACIDS)
1807 nuc_groups = _GroupSequences(nuc_seqs, nuc_seqid_thr, nuc_gap_thr, aligner,
1808 mol.ChemType.NUCLEOTIDES)
1809 group_types = [mol.ChemType.AMINOACIDS] * len(pep_groups)
1810 group_types += [mol.ChemType.NUCLEOTIDES] * len(nuc_groups)
1812 groups.extend(nuc_groups)
1813 return (groups, group_types)
1815 def _GroupSequences(seqs, seqid_thr, gap_thr, aligner, chem_type):
1816 """Get list of alignments representing groups of equivalent sequences
1818 :param seqid_thr: Threshold used to decide when two chains are identical.
1819 :type seqid_thr: :class:`float`
1820 :param gap_thr: Additional threshold to avoid gappy alignments with high
1821 seqid. The reason for not just normalizing seqid by the
1822 longer sequence is that one sequence might be a perfect
1823 subsequence of the other but only cover half of it. This
1824 threshold checks for a maximum allowed fraction of gaps
1825 in any of the two sequences after stripping terminal gaps.
1826 :type gap_thr: :class:`float`
1827 :param aligner: Helper class to generate pairwise alignments
1828 :type aligner: :class:`_Aligner`
1829 :param chem_type: ChemType of seqs which is passed to *aligner*, must be in
1830 [:class:`ost.mol.ChemType.AMINOACIDS`,
1831 :class:`ost.mol.ChemType.NUCLEOTIDES`]
1832 :type chem_type: :class:`ost.mol.ChemType`
1833 :returns: A list of alignments, one alignment for each group
1834 with longest sequence (reference) as first sequence.
1835 :rtype: :class:`ost.seq.AlignmentList`
1838 for s_idx
in range(len(seqs)):
1839 matching_group =
None
1840 for g_idx
in range(len(groups)):
1841 for g_s_idx
in range(len(groups[g_idx])):
1842 aln = aligner.Align(seqs[s_idx], seqs[groups[g_idx][g_s_idx]],
1844 sid, frac_i, frac_j = _GetAlnPropsOne(aln)
1845 if sid >= seqid_thr
and frac_i < gap_thr
and frac_j < gap_thr:
1846 matching_group = g_idx
1848 if matching_group
is not None:
1851 if matching_group
is None:
1852 groups.append([s_idx])
1854 groups[matching_group].append(s_idx)
1857 sorted_groups = list()
1860 tmp = sorted([[len(seqs[i]), i]
for i
in g], reverse=
True)
1861 sorted_groups.append([x[1]
for x
in tmp])
1863 sorted_groups.append(g)
1867 aln_list = seq.AlignmentList()
1868 for g
in sorted_groups:
1871 aln_list.append(seq.CreateAlignment(seqs[g[0]]))
1874 alns = seq.AlignmentList()
1877 alns.append(aligner.Align(seqs[i], seqs[j], chem_type))
1879 aln_list.append(seq.alg.MergePairwiseAlignments(alns, seqs[i]))
1882 seq_dict = {s.GetName(): s
for s
in seqs}
1883 for aln_idx
in range(len(aln_list)):
1884 for aln_s_idx
in range(aln_list[aln_idx].GetCount()):
1885 s_name = aln_list[aln_idx].GetSequence(aln_s_idx).GetName()
1886 s = seq_dict[s_name]
1887 aln_list[aln_idx].AttachView(aln_s_idx, s.GetAttachedView())
1891 def _MapSequence(ref_seqs, ref_types, s, s_type, aligner):
1892 """Tries top map *s* onto any of the sequences in *ref_seqs*
1894 Computes alignments of *s* to each of the reference sequences of equal type
1895 and sorts them by seqid*fraction_covered (seqid: sequence identity of
1896 aligned columns in alignment, fraction_covered: Fraction of non-gap
1897 characters in reference sequence that are covered by non-gap characters in
1898 *s*). Best scoring mapping is returned.
1900 :param ref_seqs: Reference sequences
1901 :type ref_seqs: :class:`ost.seq.SequenceList`
1902 :param ref_types: Types of reference sequences, e.g.
1903 ost.mol.ChemType.AminoAcids
1904 :type ref_types: :class:`list` of :class:`ost.mol.ChemType`
1905 :param s: Sequence to map
1906 :type s: :class:`ost.seq.SequenceHandle`
1907 :param s_type: Type of *s*, only try mapping to sequences in *ref_seqs*
1908 with equal type as defined in *ref_types*
1909 :param aligner: Helper class to generate pairwise alignments
1910 :type aligner: :class:`_Aligner`
1911 :returns: Tuple with two elements. 1) index of sequence in *ref_seqs* to
1912 which *s* can be mapped 2) Pairwise sequence alignment with
1913 sequence from *ref_seqs* as first sequence. Both elements are
1914 None if no mapping can be found.
1915 :raises: :class:`RuntimeError` if mapping is ambiguous, i.e. *s*
1916 successfully maps to more than one sequence in *ref_seqs*
1918 scored_alns = list()
1919 for ref_idx, ref_seq
in enumerate(ref_seqs):
1920 if ref_types[ref_idx] == s_type:
1921 aln = aligner.Align(ref_seq, s, s_type)
1922 seqid, fraction_covered = _GetAlnPropsTwo(aln)
1923 score = seqid * fraction_covered
1924 scored_alns.append((score, ref_idx, aln))
1926 if len(scored_alns) == 0:
1929 scored_alns = sorted(scored_alns, key=
lambda x: x[0], reverse=
True)
1930 return (scored_alns[0][1], scored_alns[0][2])
1932 def _GetRefMdlAlns(ref_chem_groups, ref_chem_group_msas, mdl_chem_groups,
1933 mdl_chem_group_alns, pairs=
None):
1934 """ Get all possible ref/mdl chain alignments given chem group mapping
1936 :param ref_chem_groups: :attr:`ChainMapper.chem_groups`
1937 :type ref_chem_groups: :class:`list` of :class:`list` of :class:`str`
1938 :param ref_chem_group_msas: :attr:`ChainMapper.chem_group_alignments`
1939 :type ref_chem_group_msas: :class:`ost.seq.AlignmentList`
1940 :param mdl_chem_groups: Groups of model chains that are mapped to
1941 *ref_chem_groups*. Return value of
1942 :func:`ChainMapper.GetChemMapping`.
1943 :type mdl_chem_groups: :class:`list` of :class:`list` of :class:`str`
1944 :param mdl_chem_group_alns: A pairwise sequence alignment for every chain
1945 in *mdl_chem_groups* that aligns these sequences
1946 to the respective reference sequence.
1948 :func:`ChainMapper.GetChemMapping`.
1949 :type mdl_chem_group_alns: :class:`list` of :class:`ost.seq.AlignmentList`
1950 :param pairs: Pro param - restrict return dict to specified pairs. A set of
1951 tuples in form (<trg_ch>, <mdl_ch>)
1952 :type pairs: :class:`set`
1953 :returns: A dictionary holding all possible ref/mdl chain alignments. Keys
1954 in that dictionary are tuples of the form (ref_ch, mdl_ch) and
1955 values are the respective pairwise alignments with first sequence
1956 being from ref, the second from mdl.
1960 for alns
in mdl_chem_group_alns:
1962 mdl_chain_name = aln.GetSequence(1).GetName()
1963 mdl_alns[mdl_chain_name] = aln
1967 ref_mdl_alns = dict()
1968 for ref_chains, mdl_chains, ref_aln
in zip(ref_chem_groups, mdl_chem_groups,
1969 ref_chem_group_msas):
1970 for ref_ch
in ref_chains:
1971 for mdl_ch
in mdl_chains:
1972 if pairs
is not None and (ref_ch, mdl_ch)
not in pairs:
1976 aln_list = seq.AlignmentList()
1978 s1 = ref_aln.GetSequence(0)
1979 s2 = ref_aln.GetSequence(ref_chains.index(ref_ch))
1980 aln_list.append(seq.CreateAlignment(s1, s2))
1982 aln_list.append(mdl_alns[mdl_ch])
1984 ref_seq = seq.CreateSequence(s1.GetName(),
1985 s1.GetGaplessString())
1986 merged_aln = seq.alg.MergePairwiseAlignments(aln_list,
1993 s2 = merged_aln.GetSequence(1)
1994 s3 = merged_aln.GetSequence(2)
1997 for idx
in range(len(s2)):
1998 if s2[idx] !=
'-' or s3[idx] !=
'-':
2002 for idx
in reversed(range(len(s2))):
2003 if s2[idx] !=
'-' or s3[idx] !=
'-':
2006 s2 = seq.CreateSequence(s2.GetName(), s2[a: len(s2)-b])
2007 s3 = seq.CreateSequence(s3.GetName(), s3[a: len(s3)-b])
2008 ref_mdl_alns[(ref_ch, mdl_ch)] = seq.CreateAlignment(s2, s3)
2012 def _CheckOneToOneMapping(ref_chains, mdl_chains):
2013 """ Checks whether we already have a perfect one to one mapping
2015 That means each list in *ref_chains* has exactly one element and each
2016 list in *mdl_chains* has either one element (it's mapped) or is empty
2017 (ref chain has no mapped mdl chain). Returns None if no such mapping
2020 :param ref_chains: corresponds to :attr:`ChainMapper.chem_groups`
2021 :type ref_chains: :class:`list` of :class:`list` of :class:`str`
2022 :param mdl_chains: mdl chains mapped to chem groups in *ref_chains*, i.e.
2023 the return value of :func:`ChainMapper.GetChemMapping`
2024 :type mdl_chains: class:`list` of :class:`list` of :class:`str`
2025 :returns: A :class:`list` of :class:`list` if a one to one mapping is found,
2028 only_one_to_one =
True
2030 for ref, mdl
in zip(ref_chains, mdl_chains):
2031 if len(ref) == 1
and len(mdl) == 1:
2032 one_to_one.append(mdl)
2033 elif len(ref) == 1
and len(mdl) == 0:
2034 one_to_one.append([
None])
2036 only_one_to_one =
False
2045 def __init__(self, ref, mdl, ref_mdl_alns, inclusion_radius = 15.0,
2046 thresholds = [0.5, 1.0, 2.0, 4.0]):
2047 """ Compute backbone only lDDT scores for ref/mdl
2049 Uses the pairwise decomposable property of backbone only lDDT and
2050 implements a caching mechanism to efficiently enumerate different
2085 def _SetupScorer(self):
2086 for ch
in self.ref.chains:
2088 query = f
"{self.inclusion_radius} <> "
2089 query += f
"[cname={mol.QueryQuoteName(ch.GetName())}] "
2090 query += f
"and cname!={mol.QueryQuoteName(ch.GetName())}"
2091 for close_ch
in self.ref.Select(query).chains:
2092 k1 = (ch.GetName(), close_ch.GetName())
2093 k2 = (close_ch.GetName(), ch.GetName())
2096 dimer_ref = _CSel(self.
ref, [k1[0], k1[1]])
2101 self.ref_interfaces.append(k1)
2106 self.
n += s.GetNChainContacts(ch.GetName(),
2108 self.ref_chains.append(ch.GetName())
2111 self.
n += s.GetNChainContacts(close_ch.GetName(),
2113 self.ref_chains.append(close_ch.GetName())
2116 for ch
in self.ref.chains:
2118 single_chain_ref = _CSel(self.
ref, [ch.GetName()])
2122 self.ref_chains.append(ch.GetName())
2124 def lDDT(self, ref_chain_groups, mdl_chain_groups):
2127 for ref_chains, mdl_chains
in zip(ref_chain_groups, mdl_chain_groups):
2128 for ref_ch, mdl_ch
in zip(ref_chains, mdl_chains):
2129 flat_map[ref_ch] = mdl_ch
2139 if ref_ch
in flat_map
and flat_map[ref_ch]
is not None:
2140 conserved += self.
SCCounts(ref_ch, flat_map[ref_ch])
2144 if ref_ch1
in flat_map
and ref_ch2
in flat_map:
2145 mdl_ch1 = flat_map[ref_ch1]
2146 mdl_ch2 = flat_map[ref_ch2]
2147 if mdl_ch1
is not None and mdl_ch2
is not None:
2148 conserved += self.
IntCounts(ref_ch1, ref_ch2, mdl_ch1,
2151 return conserved / (len(self.
thresholds) * self.
n)
2157 mdl_sel = _CSel(self.
mdl, [mdl_ch])
2159 _,_,_,conserved,_,_,_ = s.lDDT(mdl_sel,
2160 residue_mapping=alns,
2161 return_dist_test=
True,
2163 chain_mapping={mdl_ch: ref_ch},
2164 check_resnames=
False)
2169 k1 = ((ref_ch1, ref_ch2),(mdl_ch1, mdl_ch2))
2170 k2 = ((ref_ch2, ref_ch1),(mdl_ch2, mdl_ch1))
2175 mdl_sel = _CSel(self.
mdl, [mdl_ch1, mdl_ch2])
2177 _,_,_,conserved,_,_,_ = s.lDDT(mdl_sel,
2178 residue_mapping=alns,
2179 return_dist_test=
True,
2181 chain_mapping={mdl_ch1: ref_ch1,
2183 check_resnames=
False)
2189 def __init__(self, ref, mdl, ref_chem_groups, mdl_chem_groups,
2190 ref_mdl_alns, inclusion_radius = 15.0,
2191 thresholds = [0.5, 1.0, 2.0, 4.0],
2192 steep_opt_rate =
None):
2193 """ Greedy extension of already existing but incomplete chain mappings
2195 super().
__init__(ref, mdl, ref_mdl_alns,
2196 inclusion_radius = inclusion_radius,
2197 thresholds = thresholds)
2200 for k
in self.interface_scorer.keys():
2204 assert(len(ref_chem_groups) == len(mdl_chem_groups))
2209 for g_idx, (ref_g, mdl_g)
in enumerate(zip(ref_chem_groups,
2220 for ch
in self.mdl.chains:
2221 ch_name = ch.GetName()
2223 query = f
"{d} <> [cname={mol.QueryQuoteName(ch_name)}]"
2224 query += f
" and cname !={mol.QueryQuoteName(ch_name)}"
2225 for close_ch
in self.mdl.Select(query).chains:
2231 if len(mapping) == 0:
2232 raise RuntimError(
"Mapping must contain a starting point")
2239 for ref_ch
in mapping.keys():
2240 map_targets.update(self.
neighbors[ref_ch])
2243 for ref_ch
in mapping.keys():
2244 map_targets.discard(ref_ch)
2246 if len(map_targets) == 0:
2250 free_mdl_chains = list()
2252 tmp = [x
for x
in chem_group
if x
not in mapping.values()]
2253 free_mdl_chains.append(set(tmp))
2256 newly_mapped_ref_chains = list()
2258 something_happened =
True
2259 while something_happened:
2260 something_happened=
False
2263 n_chains = len(newly_mapped_ref_chains)
2265 mapping = self.
_SteepOpt(mapping, newly_mapped_ref_chains)
2267 if max_ext
is not None and len(newly_mapped_ref_chains) >= max_ext:
2272 for ref_ch
in map_targets:
2274 for mdl_ch
in free_mdl_chains[chem_group_idx]:
2276 n_single = self.
SCCounts(ref_ch, mdl_ch)
2280 if neighbor
in mapping
and mapping[neighbor]
in \
2282 n_inter += self.
IntCounts(ref_ch, neighbor, mdl_ch,
2284 n = n_single + n_inter
2286 if n_inter > 0
and n > max_n:
2291 max_mapping = (ref_ch, mdl_ch)
2294 something_happened =
True
2296 mapping[max_mapping[0]] = max_mapping[1]
2300 for neighbor
in self.
neighbors[max_mapping[0]]:
2301 if neighbor
not in mapping:
2302 map_targets.add(neighbor)
2305 map_targets.remove(max_mapping[0])
2309 free_mdl_chains[chem_group_idx].remove(max_mapping[1])
2312 newly_mapped_ref_chains.append(max_mapping[0])
2316 def _SteepOpt(self, mapping, chains_to_optimize=None):
2319 if chains_to_optimize
is None:
2320 chains_to_optimize = mapping.keys()
2323 ref_chains = [x
for x
in chains_to_optimize
if mapping[x]
is not None]
2327 for ch
in ref_chains:
2329 if chem_group_idx
in tmp:
2330 tmp[chem_group_idx].append(ch)
2332 tmp[chem_group_idx] = [ch]
2333 chem_groups = list(tmp.values())
2338 something_happened =
True
2339 while something_happened:
2340 something_happened =
False
2341 for chem_group
in chem_groups:
2342 if something_happened:
2344 for ch1, ch2
in itertools.combinations(chem_group, 2):
2345 swapped_mapping = dict(mapping)
2346 swapped_mapping[ch1] = mapping[ch2]
2347 swapped_mapping[ch2] = mapping[ch1]
2349 if score > current_lddt:
2350 something_happened =
True
2351 mapping = swapped_mapping
2352 current_lddt = score
2358 def _lDDTNaive(trg, mdl, inclusion_radius, thresholds, chem_groups,
2359 chem_mapping, ref_mdl_alns, n_max_naive):
2360 """ Naively iterates all possible chain mappings and returns the best
2368 if _NMappingsWithin(chem_groups, chem_mapping, 24):
2371 for mapping
in _ChainMappings(chem_groups, chem_mapping, n_max_naive):
2373 lddt_chain_mapping = dict()
2375 for ref_chem_group, mdl_chem_group
in zip(chem_groups, mapping):
2376 for ref_ch, mdl_ch
in zip(ref_chem_group, mdl_chem_group):
2378 if mdl_ch
is not None:
2379 lddt_chain_mapping[mdl_ch] = ref_ch
2380 lddt_alns[mdl_ch] = ref_mdl_alns[(ref_ch, mdl_ch)]
2381 lDDT, _ = lddt_scorer.lDDT(mdl, thresholds=thresholds,
2382 chain_mapping=lddt_chain_mapping,
2383 residue_mapping = lddt_alns,
2384 check_resnames =
False)
2385 if lDDT > best_lddt:
2386 best_mapping = mapping
2392 inclusion_radius=inclusion_radius,
2393 thresholds = thresholds)
2394 for mapping
in _ChainMappings(chem_groups, chem_mapping, n_max_naive):
2395 lDDT = lddt_scorer.lDDT(chem_groups, mapping)
2396 if lDDT > best_lddt:
2397 best_mapping = mapping
2400 return (best_mapping, best_lddt)
2403 def _GetSeeds(ref_chem_groups, mdl_chem_groups, mapped_ref_chains = set(),
2404 mapped_mdl_chains = set()):
2406 for ref_chains, mdl_chains
in zip(ref_chem_groups,
2408 for ref_ch
in ref_chains:
2409 if ref_ch
not in mapped_ref_chains:
2410 for mdl_ch
in mdl_chains:
2411 if mdl_ch
not in mapped_mdl_chains:
2412 seeds.append((ref_ch, mdl_ch))
2416 def _lDDTGreedyFast(the_greed):
2418 something_happened =
True
2421 while something_happened:
2422 something_happened =
False
2423 seeds = _GetSeeds(the_greed.ref_chem_groups,
2424 the_greed.mdl_chem_groups,
2425 mapped_ref_chains = set(mapping.keys()),
2426 mapped_mdl_chains = set(mapping.values()))
2431 n = the_greed.SCCounts(seed[0], seed[1])
2438 mapping[best_seed[0]] = best_seed[1]
2439 mapping = the_greed.ExtendMapping(mapping)
2440 something_happened =
True
2443 final_mapping = list()
2444 for ref_chains
in the_greed.ref_chem_groups:
2445 mapped_mdl_chains = list()
2446 for ref_ch
in ref_chains:
2447 if ref_ch
in mapping:
2448 mapped_mdl_chains.append(mapping[ref_ch])
2450 mapped_mdl_chains.append(
None)
2451 final_mapping.append(mapped_mdl_chains)
2453 return final_mapping
2456 def _lDDTGreedyFull(the_greed):
2457 """ Uses each reference chain as starting point for expansion
2460 seeds = _GetSeeds(the_greed.ref_chem_groups, the_greed.mdl_chem_groups)
2461 best_overall_score = -1.0
2462 best_overall_mapping = dict()
2467 mapping = the_greed.ExtendMapping({seed[0]: seed[1]})
2470 something_happened =
True
2471 while something_happened:
2472 something_happened =
False
2473 remnant_seeds = _GetSeeds(the_greed.ref_chem_groups,
2474 the_greed.mdl_chem_groups,
2475 mapped_ref_chains = set(mapping.keys()),
2476 mapped_mdl_chains = set(mapping.values()))
2477 if len(remnant_seeds) > 0:
2481 for remnant_seed
in remnant_seeds:
2482 tmp_mapping = dict(mapping)
2483 tmp_mapping[remnant_seed[0]] = remnant_seed[1]
2484 tmp_mapping = the_greed.ExtendMapping(tmp_mapping)
2485 score = the_greed.lDDTFromFlatMap(tmp_mapping)
2486 if score > best_score:
2488 best_mapping = tmp_mapping
2489 if best_mapping
is not None:
2490 something_happened =
True
2491 mapping = best_mapping
2493 score = the_greed.lDDTFromFlatMap(mapping)
2494 if score > best_overall_score:
2495 best_overall_score = score
2496 best_overall_mapping = mapping
2498 mapping = best_overall_mapping
2501 final_mapping = list()
2502 for ref_chains
in the_greed.ref_chem_groups:
2503 mapped_mdl_chains = list()
2504 for ref_ch
in ref_chains:
2505 if ref_ch
in mapping:
2506 mapped_mdl_chains.append(mapping[ref_ch])
2508 mapped_mdl_chains.append(
None)
2509 final_mapping.append(mapped_mdl_chains)
2511 return final_mapping
2514 def _lDDTGreedyBlock(the_greed, seed_size, blocks_per_chem_group):
2515 """ try multiple seeds, i.e. try all ref/mdl chain combinations within the
2516 respective chem groups and compute single chain lDDTs. The
2517 *blocks_per_chem_group* best scoring ones are extend by *seed_size* chains
2518 and the best scoring one is exhaustively extended.
2521 if seed_size
is None or seed_size < 1:
2522 raise RuntimeError(f
"seed_size must be an int >= 1 (got {seed_size})")
2524 if blocks_per_chem_group
is None or blocks_per_chem_group < 1:
2525 raise RuntimeError(f
"blocks_per_chem_group must be an int >= 1 "
2526 f
"(got {blocks_per_chem_group})")
2528 max_ext = seed_size - 1
2530 ref_chem_groups = copy.deepcopy(the_greed.ref_chem_groups)
2531 mdl_chem_groups = copy.deepcopy(the_greed.mdl_chem_groups)
2534 something_happened =
True
2535 while something_happened:
2536 something_happened =
False
2537 starting_blocks = list()
2538 for ref_chains, mdl_chains
in zip(ref_chem_groups, mdl_chem_groups):
2539 if len(mdl_chains) == 0:
2541 ref_chains_copy = list(ref_chains)
2542 for i
in range(blocks_per_chem_group):
2543 if len(ref_chains_copy) == 0:
2546 for ref_ch
in ref_chains_copy:
2547 seeds += [(ref_ch, mdl_ch)
for mdl_ch
in mdl_chains]
2554 seed = dict(mapping)
2555 seed.update({s[0]: s[1]})
2556 seed = the_greed.ExtendMapping(seed, max_ext = max_ext)
2557 seed_lddt = the_greed.lDDTFromFlatMap(seed)
2558 if seed_lddt > best_score:
2559 best_score = seed_lddt
2562 if best_mapping !=
None:
2563 starting_blocks.append(best_mapping)
2564 if best_seed[0]
in ref_chains_copy:
2566 ref_chains_copy.remove(best_seed[0])
2571 for seed
in starting_blocks:
2572 seed = the_greed.ExtendMapping(seed)
2573 seed_lddt = the_greed.lDDTFromFlatMap(seed)
2574 if seed_lddt > best_lddt:
2575 best_lddt = seed_lddt
2578 if best_lddt == 0.0:
2581 something_happened =
True
2582 mapping.update(best_mapping)
2583 for ref_ch, mdl_ch
in best_mapping.items():
2584 for group_idx
in range(len(ref_chem_groups)):
2585 if ref_ch
in ref_chem_groups[group_idx]:
2586 ref_chem_groups[group_idx].remove(ref_ch)
2587 if mdl_ch
in mdl_chem_groups[group_idx]:
2588 mdl_chem_groups[group_idx].remove(mdl_ch)
2591 final_mapping = list()
2592 for ref_chains
in the_greed.ref_chem_groups:
2593 mapped_mdl_chains = list()
2594 for ref_ch
in ref_chains:
2595 if ref_ch
in mapping:
2596 mapped_mdl_chains.append(mapping[ref_ch])
2598 mapped_mdl_chains.append(
None)
2599 final_mapping.append(mapped_mdl_chains)
2601 return final_mapping
2605 def __init__(self, ref, mdl, ref_chem_groups, mdl_chem_groups,
2606 ref_mdl_alns, contact_d = 12.0,
2607 steep_opt_rate =
None, greedy_prune_contact_map=
False):
2608 """ Greedy extension of already existing but incomplete chain mappings
2610 super().
__init__(ref, ref_chem_groups, mdl, ref_mdl_alns,
2611 contact_d = contact_d)
2617 if greedy_prune_contact_map:
2618 self.
neighbors = {k: set()
for k
in self.qsent1.chain_names}
2619 for p
in self.qsent1.interacting_chains:
2620 if np.count_nonzero(self.qsent1.PairDist(p[0], p[1])<=8) >= 3:
2625 for p
in self.qsent2.interacting_chains:
2626 if np.count_nonzero(self.qsent2.PairDist(p[0], p[1])<=8) >= 3:
2632 self.
neighbors = {k: set()
for k
in self.qsent1.chain_names}
2633 for p
in self.qsent1.interacting_chains:
2637 self.
mdl_neighbors = {k: set()
for k
in self.qsent2.chain_names}
2638 for p
in self.qsent2.interacting_chains:
2642 assert(len(ref_chem_groups) == len(mdl_chem_groups))
2647 for g_idx, (ref_g, mdl_g)
in enumerate(zip(ref_chem_groups,
2659 for ch
in self.ref.chains:
2660 single_chain_ref = _CSel(self.
ref, [ch.GetName()])
2668 mdl_sel = _CSel(self.
mdl, [mdl_ch])
2670 _,_,_,conserved,_,_,_ = s.lDDT(mdl_sel,
2671 residue_mapping=alns,
2672 return_dist_test=
True,
2674 chain_mapping={mdl_ch: ref_ch},
2675 check_resnames=
False)
2681 if len(mapping) == 0:
2682 raise RuntimError(
"Mapping must contain a starting point")
2689 for ref_ch
in mapping.keys():
2690 map_targets.update(self.
neighbors[ref_ch])
2693 for ref_ch
in mapping.keys():
2694 map_targets.discard(ref_ch)
2696 if len(map_targets) == 0:
2700 free_mdl_chains = list()
2702 tmp = [x
for x
in chem_group
if x
not in mapping.values()]
2703 free_mdl_chains.append(set(tmp))
2706 newly_mapped_ref_chains = list()
2708 something_happened =
True
2709 while something_happened:
2710 something_happened=
False
2713 n_chains = len(newly_mapped_ref_chains)
2715 mapping = self.
_SteepOpt(mapping, newly_mapped_ref_chains)
2717 if max_ext
is not None and len(newly_mapped_ref_chains) >= max_ext:
2721 old_score = score_result.QS_global
2722 nominator = score_result.weighted_scores
2723 denominator = score_result.weight_sum + score_result.weight_extra_all
2727 for ref_ch
in map_targets:
2729 for mdl_ch
in free_mdl_chains[chem_group_idx]:
2732 nominator_diff = 0.0
2733 denominator_diff = 0.0
2735 if neighbor
in mapping
and mapping[neighbor]
in \
2739 int1 = (ref_ch, neighbor)
2740 int2 = (mdl_ch, mapping[neighbor])
2743 denominator_diff += b
2744 denominator_diff += d
2750 if nominator_diff > 0:
2753 new_nominator = nominator + nominator_diff
2754 new_denominator = denominator + denominator_diff
2756 if new_denominator != 0.0:
2757 new_score = new_nominator/new_denominator
2758 diff = new_score - old_score
2761 max_mapping = (ref_ch, mdl_ch)
2763 if max_mapping
is not None:
2764 something_happened =
True
2766 mapping[max_mapping[0]] = max_mapping[1]
2770 for neighbor
in self.
neighbors[max_mapping[0]]:
2771 if neighbor
not in mapping:
2772 map_targets.add(neighbor)
2775 map_targets.remove(max_mapping[0])
2779 free_mdl_chains[chem_group_idx].remove(max_mapping[1])
2782 newly_mapped_ref_chains.append(max_mapping[0])
2786 def _SteepOpt(self, mapping, chains_to_optimize=None):
2789 if chains_to_optimize
is None:
2790 chains_to_optimize = mapping.keys()
2793 ref_chains = [x
for x
in chains_to_optimize
if mapping[x]
is not None]
2797 for ch
in ref_chains:
2799 if chem_group_idx
in tmp:
2800 tmp[chem_group_idx].append(ch)
2802 tmp[chem_group_idx] = [ch]
2803 chem_groups = list(tmp.values())
2808 current_score = score_result.QS_global
2809 something_happened =
True
2810 while something_happened:
2811 something_happened =
False
2812 for chem_group
in chem_groups:
2813 if something_happened:
2815 for ch1, ch2
in itertools.combinations(chem_group, 2):
2816 swapped_mapping = dict(mapping)
2817 swapped_mapping[ch1] = mapping[ch2]
2818 swapped_mapping[ch2] = mapping[ch1]
2820 if score_result.QS_global > current_score:
2821 something_happened =
True
2822 mapping = swapped_mapping
2823 current_score = score_result.QS_global
2828 def _QSScoreNaive(trg, mdl, chem_groups, chem_mapping, ref_mdl_alns, contact_d,
2835 for mapping
in _ChainMappings(chem_groups, chem_mapping, n_max_naive):
2836 score_result = qs_scorer.Score(mapping, check=
False)
2837 if score_result.QS_global > best_score:
2838 best_mapping = mapping
2839 best_score = score_result.QS_global
2840 return (best_mapping, best_score)
2843 def _QSScoreGreedyFast(the_greed):
2845 something_happened =
True
2847 while something_happened:
2848 something_happened =
False
2852 seeds = _GetSeeds(the_greed.ref_chem_groups,
2853 the_greed.mdl_chem_groups,
2854 mapped_ref_chains = set(mapping.keys()),
2855 mapped_mdl_chains = set(mapping.values()))
2857 n = the_greed.SCCounts(seed[0], seed[1])
2864 mapping[best_seed[0]] = best_seed[1]
2865 mapping = the_greed.ExtendMapping(mapping)
2866 something_happened =
True
2869 final_mapping = list()
2870 for ref_chains
in the_greed.ref_chem_groups:
2871 mapped_mdl_chains = list()
2872 for ref_ch
in ref_chains:
2873 if ref_ch
in mapping:
2874 mapped_mdl_chains.append(mapping[ref_ch])
2876 mapped_mdl_chains.append(
None)
2877 final_mapping.append(mapped_mdl_chains)
2879 return final_mapping
2882 def _QSScoreGreedyFull(the_greed):
2883 """ Uses each reference chain as starting point for expansion
2886 seeds = _GetSeeds(the_greed.ref_chem_groups, the_greed.mdl_chem_groups)
2887 best_overall_score = -1.0
2888 best_overall_mapping = dict()
2893 mapping = the_greed.ExtendMapping({seed[0]: seed[1]})
2896 something_happened =
True
2897 while something_happened:
2898 something_happened =
False
2899 remnant_seeds = _GetSeeds(the_greed.ref_chem_groups,
2900 the_greed.mdl_chem_groups,
2901 mapped_ref_chains = set(mapping.keys()),
2902 mapped_mdl_chains = set(mapping.values()))
2903 if len(remnant_seeds) > 0:
2907 for remnant_seed
in remnant_seeds:
2908 tmp_mapping = dict(mapping)
2909 tmp_mapping[remnant_seed[0]] = remnant_seed[1]
2910 tmp_mapping = the_greed.ExtendMapping(tmp_mapping)
2911 score_result = the_greed.FromFlatMapping(tmp_mapping)
2912 if score_result.QS_global > best_score:
2913 best_score = score_result.QS_global
2914 best_mapping = tmp_mapping
2915 if best_mapping
is not None:
2916 something_happened =
True
2917 mapping = best_mapping
2919 score_result = the_greed.FromFlatMapping(mapping)
2920 if score_result.QS_global > best_overall_score:
2921 best_overall_score = score_result.QS_global
2922 best_overall_mapping = mapping
2924 mapping = best_overall_mapping
2927 final_mapping = list()
2928 for ref_chains
in the_greed.ref_chem_groups:
2929 mapped_mdl_chains = list()
2930 for ref_ch
in ref_chains:
2931 if ref_ch
in mapping:
2932 mapped_mdl_chains.append(mapping[ref_ch])
2934 mapped_mdl_chains.append(
None)
2935 final_mapping.append(mapped_mdl_chains)
2937 return final_mapping
2940 def _QSScoreGreedyBlock(the_greed, seed_size, blocks_per_chem_group):
2941 """ try multiple seeds, i.e. try all ref/mdl chain combinations within the
2942 respective chem groups and compute single chain lDDTs. The
2943 *blocks_per_chem_group* best scoring ones are extend by *seed_size* chains
2944 and the best scoring one with respect to QS score is exhaustively extended.
2947 if seed_size
is None or seed_size < 1:
2948 raise RuntimeError(f
"seed_size must be an int >= 1 (got {seed_size})")
2950 if blocks_per_chem_group
is None or blocks_per_chem_group < 1:
2951 raise RuntimeError(f
"blocks_per_chem_group must be an int >= 1 "
2952 f
"(got {blocks_per_chem_group})")
2954 max_ext = seed_size - 1
2956 ref_chem_groups = copy.deepcopy(the_greed.ref_chem_groups)
2957 mdl_chem_groups = copy.deepcopy(the_greed.mdl_chem_groups)
2961 something_happened =
True
2962 while something_happened:
2963 something_happened =
False
2964 starting_blocks = list()
2965 for ref_chains, mdl_chains
in zip(ref_chem_groups, mdl_chem_groups):
2966 if len(mdl_chains) == 0:
2968 ref_chains_copy = list(ref_chains)
2969 for i
in range(blocks_per_chem_group):
2970 if len(ref_chains_copy) == 0:
2973 for ref_ch
in ref_chains_copy:
2974 seeds += [(ref_ch, mdl_ch)
for mdl_ch
in mdl_chains]
2981 seed = dict(mapping)
2982 seed.update({s[0]: s[1]})
2983 seed = the_greed.ExtendMapping(seed, max_ext = max_ext)
2984 score_result = the_greed.FromFlatMapping(seed)
2985 if score_result.QS_global > best_score:
2986 best_score = score_result.QS_global
2989 if best_mapping !=
None:
2990 starting_blocks.append(best_mapping)
2991 if best_seed[0]
in ref_chains_copy:
2993 ref_chains_copy.remove(best_seed[0])
2998 for seed
in starting_blocks:
2999 seed = the_greed.ExtendMapping(seed)
3000 score_result = the_greed.FromFlatMapping(seed)
3001 if score_result.QS_global > best_score:
3002 best_score = score_result.QS_global
3005 if best_mapping
is not None and len(best_mapping) > len(mapping):
3008 something_happened =
True
3009 mapping.update(best_mapping)
3010 for ref_ch, mdl_ch
in best_mapping.items():
3011 for group_idx
in range(len(ref_chem_groups)):
3012 if ref_ch
in ref_chem_groups[group_idx]:
3013 ref_chem_groups[group_idx].remove(ref_ch)
3014 if mdl_ch
in mdl_chem_groups[group_idx]:
3015 mdl_chem_groups[group_idx].remove(mdl_ch)
3018 final_mapping = list()
3019 for ref_chains
in the_greed.ref_chem_groups:
3020 mapped_mdl_chains = list()
3021 for ref_ch
in ref_chains:
3022 if ref_ch
in mapping:
3023 mapped_mdl_chains.append(mapping[ref_ch])
3025 mapped_mdl_chains.append(
None)
3026 final_mapping.append(mapped_mdl_chains)
3028 return final_mapping
3031 def _SingleRigidGDTTS(initial_transforms, initial_mappings, chem_groups,
3032 chem_mapping, trg_group_pos, mdl_group_pos,
3033 single_chain_gdtts_thresh, iterative_superposition,
3034 first_complete, n_trg_chains, n_mdl_chains):
3035 """ Takes initial transforms and sequentially adds chain pairs with
3036 best scoring gdtts that fulfill single_chain_gdtts_thresh. The mapping
3037 from the transform that leads to best overall gdtts score is returned.
3038 Optionally, the first complete mapping, i.e. a mapping that covers all
3039 target chains or all model chains, is returned.
3041 best_mapping = dict()
3043 for transform
in initial_transforms:
3045 mapped_mdl_chains = set()
3048 for trg_chains, mdl_chains, trg_pos, mdl_pos,
in zip(chem_groups,
3053 if len(trg_pos) == 0
or len(mdl_pos) == 0:
3059 for m_pos
in mdl_pos:
3061 t_m_pos.ApplyTransform(transform)
3062 t_mdl_pos.append(t_m_pos)
3064 for t_pos, t
in zip(trg_pos, trg_chains):
3065 for t_m_pos, m
in zip(t_mdl_pos, mdl_chains):
3066 gdt = t_pos.GetGDTTS(t_m_pos)
3067 if gdt >= single_chain_gdtts_thresh:
3068 gdt_scores.append((gdt, (t,m)))
3070 n_gdt_contacts = 4 * len(trg_pos[0])
3071 gdt_scores.sort(reverse=
True)
3072 for item
in gdt_scores:
3074 if p[0]
not in mapping
and p[1]
not in mapped_mdl_chains:
3075 mapping[p[0]] = p[1]
3076 mapped_mdl_chains.add(p[1])
3077 gdt += (item[0] * n_gdt_contacts)
3081 best_mapping = mapping
3084 if n == n_mdl_chains
or n == n_trg_chains:
3090 def _IterativeRigidGDTTS(initial_transforms, initial_mappings, chem_groups,
3091 chem_mapping, trg_group_pos, mdl_group_pos,
3092 single_chain_gdtts_thresh, iterative_superposition,
3093 first_complete, n_trg_chains, n_mdl_chains):
3094 """ Takes initial transforms and sequentially adds chain pairs with
3095 best scoring gdtts that fulfill single_chain_gdtts_thresh. With each
3096 added chain pair, the transform gets updated. Thus the naming iterative.
3097 The mapping from the initial transform that leads to best overall gdtts
3098 score is returned. Optionally, the first complete mapping, i.e. a mapping
3099 that covers all target chains or all model chains, is returned.
3103 trg_pos_dict = dict()
3104 for trg_pos, trg_chains
in zip(trg_group_pos, chem_groups):
3105 for t_pos, t
in zip(trg_pos, trg_chains):
3106 trg_pos_dict[t] = t_pos
3107 mdl_pos_dict = dict()
3108 for mdl_pos, mdl_chains
in zip(mdl_group_pos, chem_mapping):
3109 for m_pos, m
in zip(mdl_pos, mdl_chains):
3110 mdl_pos_dict[m] = m_pos
3112 best_mapping = dict()
3114 for initial_transform, initial_mapping
in zip(initial_transforms,
3116 mapping = {initial_mapping[0]: initial_mapping[1]}
3117 transform =
geom.Mat4(initial_transform)
3118 mapped_trg_pos =
geom.Vec3List(trg_pos_dict[initial_mapping[0]])
3119 mapped_mdl_pos =
geom.Vec3List(mdl_pos_dict[initial_mapping[1]])
3123 trg_chain_groups = [set(group)
for group
in chem_groups]
3124 mdl_chain_groups = [set(group)
for group
in chem_mapping]
3127 for group
in trg_chain_groups:
3128 if initial_mapping[0]
in group:
3129 group.remove(initial_mapping[0])
3131 for group
in mdl_chain_groups:
3132 if initial_mapping[1]
in group:
3133 group.remove(initial_mapping[1])
3136 something_happened =
True
3137 while something_happened:
3139 something_happened=
False
3140 best_sc_mapping =
None
3141 best_sc_group_idx =
None
3144 for trg_chains, mdl_chains
in zip(trg_chain_groups, mdl_chain_groups):
3145 for t
in trg_chains:
3146 t_pos = trg_pos_dict[t]
3147 for m
in mdl_chains:
3148 m_pos = mdl_pos_dict[m]
3150 t_m_pos.ApplyTransform(transform)
3151 gdt = t_pos.GetGDTTS(t_m_pos)
3152 if gdt > single_chain_gdtts_thresh
and gdt > best_sc_gdt:
3154 best_sc_mapping = (t,m)
3155 best_sc_group_idx = group_idx
3158 if best_sc_mapping
is not None:
3159 something_happened =
True
3160 mapping[best_sc_mapping[0]] = best_sc_mapping[1]
3161 mapped_trg_pos.extend(trg_pos_dict[best_sc_mapping[0]])
3162 mapped_mdl_pos.extend(mdl_pos_dict[best_sc_mapping[1]])
3163 trg_chain_groups[best_sc_group_idx].remove(best_sc_mapping[0])
3164 mdl_chain_groups[best_sc_group_idx].remove(best_sc_mapping[1])
3166 transform = _GetTransform(mapped_mdl_pos, mapped_trg_pos,
3167 iterative_superposition)
3170 mapped_mdl_pos.ApplyTransform(transform)
3171 gdt = mapped_trg_pos.GetGDTTS(mapped_mdl_pos, norm=
False)
3175 best_mapping = mapping
3178 if n == n_mdl_chains
or n == n_trg_chains:
3183 def _SingleRigidRMSD(initial_transforms, initial_mappings, chem_groups,
3184 chem_mapping, trg_group_pos, mdl_group_pos,
3185 iterative_superposition):
3187 Takes initial transforms and sequentially adds chain pairs with lowest RMSD.
3188 The mapping from the transform that leads to lowest overall RMSD is
3191 best_mapping = dict()
3192 best_ssd = float(
"inf")
3196 for transform
in initial_transforms:
3198 mapped_mdl_chains = set()
3200 for trg_chains, mdl_chains, trg_pos, mdl_pos,
in zip(chem_groups,
3204 if len(trg_pos) == 0
or len(mdl_pos) == 0:
3208 for m_pos
in mdl_pos:
3210 t_m_pos.ApplyTransform(transform)
3211 t_mdl_pos.append(t_m_pos)
3212 for t_pos, t
in zip(trg_pos, trg_chains):
3213 for t_m_pos, m
in zip(t_mdl_pos, mdl_chains):
3214 ssd = t_pos.GetSummedSquaredDistances(t_m_pos)
3215 ssds.append((ssd, (t,m)))
3219 if p[0]
not in mapping
and p[1]
not in mapped_mdl_chains:
3220 mapping[p[0]] = p[1]
3221 mapped_mdl_chains.add(p[1])
3226 best_mapping = mapping
3230 def _IterativeRigidRMSD(initial_transforms, initial_mappings, chem_groups,
3231 chem_mapping, trg_group_pos, mdl_group_pos,
3232 iterative_superposition):
3233 """ Takes initial transforms and sequentially adds chain pairs with
3234 lowest RMSD. With each added chain pair, the transform gets updated.
3235 Thus the naming iterative. The mapping from the initial transform that
3236 leads to best overall RMSD score is returned.
3240 trg_pos_dict = dict()
3241 for trg_pos, trg_chains
in zip(trg_group_pos, chem_groups):
3242 for t_pos, t
in zip(trg_pos, trg_chains):
3243 trg_pos_dict[t] = t_pos
3244 mdl_pos_dict = dict()
3245 for mdl_pos, mdl_chains
in zip(mdl_group_pos, chem_mapping):
3246 for m_pos, m
in zip(mdl_pos, mdl_chains):
3247 mdl_pos_dict[m] = m_pos
3249 best_mapping = dict()
3250 best_rmsd = float(
"inf")
3251 for initial_transform, initial_mapping
in zip(initial_transforms,
3253 mapping = {initial_mapping[0]: initial_mapping[1]}
3254 transform =
geom.Mat4(initial_transform)
3255 mapped_trg_pos =
geom.Vec3List(trg_pos_dict[initial_mapping[0]])
3256 mapped_mdl_pos =
geom.Vec3List(mdl_pos_dict[initial_mapping[1]])
3260 trg_chain_groups = [set(group)
for group
in chem_groups]
3261 mdl_chain_groups = [set(group)
for group
in chem_mapping]
3264 for group
in trg_chain_groups:
3265 if initial_mapping[0]
in group:
3266 group.remove(initial_mapping[0])
3268 for group
in mdl_chain_groups:
3269 if initial_mapping[1]
in group:
3270 group.remove(initial_mapping[1])
3273 something_happened =
True
3274 while something_happened:
3276 something_happened=
False
3277 best_sc_mapping =
None
3278 best_sc_group_idx =
None
3279 best_sc_rmsd = float(
"inf")
3281 for trg_chains, mdl_chains
in zip(trg_chain_groups, mdl_chain_groups):
3282 for t
in trg_chains:
3283 t_pos = trg_pos_dict[t]
3284 for m
in mdl_chains:
3285 m_pos = mdl_pos_dict[m]
3287 t_m_pos.ApplyTransform(transform)
3288 rmsd = t_pos.GetRMSD(t_m_pos)
3289 if rmsd < best_sc_rmsd:
3291 best_sc_mapping = (t,m)
3292 best_sc_group_idx = group_idx
3295 if best_sc_mapping
is not None:
3296 something_happened =
True
3297 mapping[best_sc_mapping[0]] = best_sc_mapping[1]
3298 mapped_trg_pos.extend(trg_pos_dict[best_sc_mapping[0]])
3299 mapped_mdl_pos.extend(mdl_pos_dict[best_sc_mapping[1]])
3300 trg_chain_groups[best_sc_group_idx].remove(best_sc_mapping[0])
3301 mdl_chain_groups[best_sc_group_idx].remove(best_sc_mapping[1])
3303 transform = _GetTransform(mapped_mdl_pos, mapped_trg_pos,
3304 iterative_superposition)
3307 mapped_mdl_pos.ApplyTransform(transform)
3308 rmsd = mapped_trg_pos.GetRMSD(mapped_mdl_pos)
3310 if rmsd < best_rmsd:
3312 best_mapping = mapping
3317 def _GetRefPos(trg, mdl, trg_msas, mdl_alns, max_pos = None):
3318 """ Extracts reference positions which are present in trg and mdl
3323 bb_trg = trg.Select(
"aname=\"CA\",\"C3'\"")
3324 bb_mdl = mdl.Select(
"aname=\"CA\",\"C3'\"")
3328 for aln_list
in mdl_alns:
3329 if len(aln_list) > 0:
3330 tmp = aln_list[0].GetSequence(0)
3331 ref_seq = seq.CreateSequence(tmp.GetName(), tmp.GetGaplessString())
3332 mdl_msas.append(seq.alg.MergePairwiseAlignments(aln_list, ref_seq))
3334 mdl_msas.append(seq.CreateAlignment())
3339 for trg_msa, mdl_msa
in zip(trg_msas, mdl_msas):
3341 if mdl_msa.GetCount() > 0:
3343 assert(trg_msa.GetSequence(0).GetGaplessString() == \
3344 mdl_msa.GetSequence(0).GetGaplessString())
3353 trg_indices = _GetFullyCoveredIndices(trg_msa)
3354 mdl_indices = _GetFullyCoveredIndices(mdl_msa)
3357 indices = sorted(list(trg_indices.intersection(mdl_indices)))
3360 if max_pos
is not None and len(indices) > max_pos:
3361 step = int(len(indices)/max_pos)
3362 indices = [indices[i]
for i
in range(0, len(indices), step)]
3365 trg_indices = _RefIndicesToColumnIndices(trg_msa, indices)
3366 mdl_indices = _RefIndicesToColumnIndices(mdl_msa, indices)
3369 trg_pos.append(list())
3370 mdl_pos.append(list())
3371 for s_idx
in range(trg_msa.GetCount()):
3372 trg_pos[-1].append(_ExtractMSAPos(trg_msa, s_idx, trg_indices,
3375 for s_idx
in range(1, mdl_msa.GetCount()):
3376 mdl_pos[-1].append(_ExtractMSAPos(mdl_msa, s_idx, mdl_indices,
3379 return (trg_pos, mdl_pos)
3381 def _GetFullyCoveredIndices(msa):
3382 """ Helper for _GetRefPos
3384 Returns a set containing the indices relative to first sequence in msa which
3385 are fully covered in all other sequences
3396 if sum([1
for olc
in col
if olc !=
'-']) == col.GetRowCount():
3397 indices.add(ref_idx)
3402 def _RefIndicesToColumnIndices(msa, indices):
3403 """ Helper for _GetRefPos
3405 Returns a list of mapped indices. indices refer to non-gap one letter
3406 codes in the first msa sequence. The returnes mapped indices are translated
3407 to the according msa column indices
3411 for col_idx, col
in enumerate(msa):
3413 mapping[ref_idx] = col_idx
3415 return [mapping[i]
for i
in indices]
3417 def _ExtractMSAPos(msa, s_idx, indices, view):
3418 """ Helper for _GetRefPos
3420 Returns a geom.Vec3List containing positions refering to given msa sequence.
3421 => Chain with corresponding name is mapped onto sequence and the position of
3422 the first atom of each residue specified in indices is extracted.
3423 Indices refers to column indices in msa!
3425 s = msa.GetSequence(s_idx)
3426 s_v = _CSel(view, [s.GetName()])
3429 assert(len(s.GetGaplessString()) == len(s_v.residues))
3431 residue_idx = [s.GetResidueIndex(i)
for i
in indices]
3432 return geom.Vec3List([s_v.residues[i].atoms[0].pos
for i
in residue_idx])
3434 def _NChemGroupMappings(ref_chains, mdl_chains):
3435 """ Number of mappings within one chem group
3437 :param ref_chains: Reference chains
3438 :type ref_chains: :class:`list` of :class:`str`
3439 :param mdl_chains: Model chains that are mapped onto *ref_chains*
3440 :type mdl_chains: :class:`list` of :class:`str`
3441 :returns: Number of possible mappings of *mdl_chains* onto *ref_chains*
3443 n_ref = len(ref_chains)
3444 n_mdl = len(mdl_chains)
3446 return factorial(n_ref)
3448 n_choose_k = binom(n_ref, n_mdl)
3449 return n_choose_k * factorial(n_mdl)
3451 n_choose_k = binom(n_mdl, n_ref)
3452 return n_choose_k * factorial(n_ref)
3454 def _NMappings(ref_chains, mdl_chains):
3455 """ Number of mappings for a full chem mapping
3457 :param ref_chains: Chem groups of reference
3458 :type ref_chains: :class:`list` of :class:`list` of :class:`str`
3459 :param mdl_chains: Model chains that map onto those chem groups
3460 :type mdl_chains: :class:`list` of :class:`list` of :class:`str`
3461 :returns: Number of possible mappings of *mdl_chains* onto *ref_chains*
3463 assert(len(ref_chains) == len(mdl_chains))
3465 for a,b
in zip(ref_chains, mdl_chains):
3466 n *= _NChemGroupMappings(a,b)
3469 def _NMappingsWithin(ref_chains, mdl_chains, max_mappings):
3470 """ Check whether total number of mappings is smaller than given maximum
3472 In principle the same as :func:`_NMappings` but it stops as soon as the
3475 :param ref_chains: Chem groups of reference
3476 :type ref_chains: :class:`list` of :class:`list` of :class:`str`
3477 :param mdl_chains: Model chains that map onto those chem groups
3478 :type mdl_chains: :class:`list` of :class:`list` of :class:`str`
3479 :param max_mappings: Number of max allowed mappings
3480 :returns: Whether number of possible mappings of *mdl_chains* onto
3481 *ref_chains* is below or equal *max_mappings*.
3483 assert(len(ref_chains) == len(mdl_chains))
3485 for a,b
in zip(ref_chains, mdl_chains):
3486 n *= _NChemGroupMappings(a,b)
3487 if n > max_mappings:
3491 def _RefSmallerGenerator(ref_chains, mdl_chains):
3492 """ Returns all possible ways to map mdl_chains onto ref_chains
3494 Specific for the case where len(ref_chains) < len(mdl_chains)
3496 for c
in itertools.combinations(mdl_chains, len(ref_chains)):
3497 for p
in itertools.permutations(c):
3500 def _RefLargerGenerator(ref_chains, mdl_chains):
3501 """ Returns all possible ways to map mdl_chains onto ref_chains
3503 Specific for the case where len(ref_chains) > len(mdl_chains)
3504 Ref chains without mapped mdl chain are assigned None
3506 n_ref = len(ref_chains)
3507 n_mdl = len(mdl_chains)
3508 for c
in itertools.combinations(range(n_ref), n_mdl):
3509 for p
in itertools.permutations(mdl_chains):
3510 ret_list = [
None] * n_ref
3511 for idx, ch
in zip(c, p):
3515 def _RefEqualGenerator(ref_chains, mdl_chains):
3516 """ Returns all possible ways to map mdl_chains onto ref_chains
3518 Specific for the case where len(ref_chains) == len(mdl_chains)
3520 for p
in itertools.permutations(mdl_chains):
3523 def _ConcatIterators(iterators):
3524 for item
in itertools.product(*iterators):
3527 def _ChainMappings(ref_chains, mdl_chains, n_max=None):
3528 """Returns all possible ways to map *mdl_chains* onto fixed *ref_chains*
3530 :param ref_chains: List of list of chemically equivalent chains in reference
3531 :type ref_chains: :class:`list` of :class:`list`
3532 :param mdl_chains: Equally long list of list of chemically equivalent chains
3533 in model that map on those ref chains.
3534 :type mdl_chains: :class:`list` of :class:`list`
3535 :param n_max: Aborts and raises :class:`RuntimeError` if max number of
3536 mappings is above this threshold.
3537 :type n_max: :class:`int`
3538 :returns: Iterator over all possible mappings of *mdl_chains* onto fixed
3539 *ref_chains*. Potentially contains None as padding when number of
3540 model chains for a certain mapping is smaller than the according
3542 Example: _ChainMappings([['A', 'B', 'C'], ['D', 'E']],
3543 [['x', 'y'], ['i', 'j']])
3544 gives an iterator over: [[['x', 'y', None], ['i', 'j']],
3545 [['x', 'y', None], ['j', 'i']],
3546 [['y', 'x', None], ['i', 'j']],
3547 [['y', 'x', None], ['j', 'i']],
3548 [['x', None, 'y'], ['i', 'j']],
3549 [['x', None, 'y'], ['j', 'i']],
3550 [['y', None, 'x'], ['i', 'j']],
3551 [['y', None, 'x'], ['j', 'i']],
3552 [[None, 'x', 'y'], ['i', 'j']],
3553 [[None, 'x', 'y'], ['j', 'i']],
3554 [[None, 'y', 'x'], ['i', 'j']],
3555 [[None, 'y', 'x'], ['j', 'i']]]
3557 assert(len(ref_chains) == len(mdl_chains))
3559 if n_max
is not None:
3560 if not _NMappingsWithin(ref_chains, mdl_chains, n_max):
3561 raise RuntimeError(f
"Too many mappings. Max allowed: {n_max}")
3566 for ref, mdl
in zip(ref_chains, mdl_chains):
3568 raise RuntimeError(
"Expext at least one chain in ref chem group")
3569 if len(ref) == len(mdl):
3570 iterators.append(_RefEqualGenerator(ref, mdl))
3571 elif len(ref) < len(mdl):
3572 iterators.append(_RefSmallerGenerator(ref, mdl))
3574 iterators.append(_RefLargerGenerator(ref, mdl))
3576 return _ConcatIterators(iterators)
3579 def _GetTransform(pos_one, pos_two, iterative):
3580 """ Computes minimal RMSD superposition for pos_one onto pos_two
3582 :param pos_one: Positions that should be superposed onto *pos_two*
3583 :type pos_one: :class:`geom.Vec3List`
3584 :param pos_two: Reference positions
3585 :type pos_two: :class:`geom.Vec3List`
3586 :iterative: Whether iterative superposition should be used. Iterative
3587 potentially raises, uses standard superposition as fallback.
3588 :type iterative: :class:`bool`
3589 :returns: Transformation matrix to superpose *pos_one* onto *pos_two*
3590 :rtype: :class:`geom.Mat4`
3595 res = mol.alg.IterativeSuperposeSVD(pos_one, pos_two)
3599 res = mol.alg.SuperposeSVD(pos_one, pos_two)
3600 return res.transformation
3603 __all__ = (
'ChainMapper',
'ReprResult',
'MappingResult')
def superposed_mdl_bb_pos
def _MappedInterfaceScores
def inconsistent_residues
def _GetInconsistentResidues
def chem_group_alignments