|
| 1 | +# vim: set expandtab ts=4 sw=4: |
| 2 | + |
| 3 | +# === UCSF ChimeraX Copyright === |
| 4 | +# Copyright 2022 Regents of the University of California. All rights reserved. |
| 5 | +# The ChimeraX application is provided pursuant to the ChimeraX license |
| 6 | +# agreement, which covers academic and commercial uses. For more details, see |
| 7 | +# <https://www.rbvi.ucsf.edu/chimerax/docs/licensing.html> |
| 8 | +# |
| 9 | +# This particular file is part of the ChimeraX library. You can also |
| 10 | +# redistribute and/or modify it under the terms of the GNU Lesser General |
| 11 | +# Public License version 2.1 as published by the Free Software Foundation. |
| 12 | +# For more details, see |
| 13 | +# <https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html> |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
| 16 | +# EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ADDITIONAL LIABILITY |
| 18 | +# LIMITATIONS ARE DESCRIBED IN THE GNU LESSER GENERAL PUBLIC LICENSE |
| 19 | +# VERSION 2.1 |
| 20 | +# |
| 21 | +# This notice must be embedded in or attached to all copies, including partial |
| 22 | +# copies, of the software or any revisions or derivations thereof. |
| 23 | +# === UCSF ChimeraX Copyright === |
| 24 | + |
| 25 | +class MatchDialog: |
| 26 | + def __init__(self, sv, tool_window): |
| 27 | + self.sv = sv |
| 28 | + self.tool_window = tool_window |
| 29 | + tool_window.help = "help:user/tools/sequenceviewer.html#match" |
| 30 | + |
| 31 | + from Qt.QtWidgets import QHBoxLayout, QVBoxLayout, QLabel, QGridLayout, QLineEdit, QCheckBox |
| 32 | + from Qt.QtGui import QDoubleValidator |
| 33 | + from Qt.QtCore import Qt |
| 34 | + layout = QVBoxLayout() |
| 35 | + #layout.setContentsMargins(0,0,0,0) |
| 36 | + layout.setSpacing(0) |
| 37 | + |
| 38 | + from chimerax.atomic.widgets import ChainMenuButton, ChainListWidget |
| 39 | + chains_layout = QGridLayout() |
| 40 | + chains_layout.setSpacing(3) |
| 41 | + chains_layout.addWidget(QLabel("Reference chain"), 0, 0, alignment=Qt.AlignCenter) |
| 42 | + chains_layout.addWidget(QLabel("Match chain(s)"), 0, 1, alignment=Qt.AlignCenter) |
| 43 | + self.ref_chain_menu = ChainMenuButton(sv.session, list_func=sv.alignment.associations.keys) |
| 44 | + chains_layout.addWidget(self.ref_chain_menu, 1, 0, alignment=Qt.AlignCenter) |
| 45 | + self.match_chain_list = ChainListWidget(sv.session, autoselect=ChainListWidget.AUTOSELECT_FIRST, |
| 46 | + list_func=sv.alignment.associations.keys, filter_func=lambda c, menu=self.ref_chain_menu: |
| 47 | + c.structure is not getattr(menu.value, 'structure', None)) |
| 48 | + chains_layout.addWidget(self.match_chain_list, 1, 1) |
| 49 | + self.ref_chain_menu.value_changed.connect(self.match_chain_list.refresh) |
| 50 | + chains_layout.setRowStretch(1, 1) |
| 51 | + layout.addLayout(chains_layout) |
| 52 | + |
| 53 | + cv_layout = QHBoxLayout() |
| 54 | + self.conserved_check_box = QCheckBox("Only match residues in columns with at least ") |
| 55 | + cv_layout.addWidget(self.conserved_check_box) |
| 56 | + self.conservation_value = cv = QLineEdit("80") |
| 57 | + cv.setAlignment(Qt.AlignCenter) |
| 58 | + cv.setMaximumWidth(4 * cv.fontMetrics().averageCharWidth()) |
| 59 | + cv.setValidator(QDoubleValidator(0.0, 100.0, -1)) |
| 60 | + cv_layout.addWidget(cv) |
| 61 | + cv_layout.addWidget(QLabel("% identity"), alignment=Qt.AlignLeft, stretch=1) |
| 62 | + layout.addLayout(cv_layout) |
| 63 | + |
| 64 | + it_layout = QHBoxLayout() |
| 65 | + self.iterate_check_box = QCheckBox("Iterate by pruning long atom pairs until no pair exceeds ") |
| 66 | + it_layout.addWidget(self.iterate_check_box) |
| 67 | + self.iteration_value = iv = QLineEdit("2.0") |
| 68 | + iv.setAlignment(Qt.AlignCenter) |
| 69 | + iv.setMaximumWidth(4 * iv.fontMetrics().averageCharWidth()) |
| 70 | + dv = QDoubleValidator() |
| 71 | + dv.setBottom(0.0) |
| 72 | + iv.setValidator(dv) |
| 73 | + it_layout.addWidget(iv) |
| 74 | + it_layout.addWidget(QLabel(" angstroms"), alignment=Qt.AlignLeft, stretch=1) |
| 75 | + layout.addLayout(it_layout) |
| 76 | + |
| 77 | + ur_layout = QHBoxLayout() |
| 78 | + self.use_region_check_box = QCheckBox("Match active region only") |
| 79 | + ur_layout.addWidget(self.use_region_check_box, alignment=Qt.AlignLeft, stretch=1) |
| 80 | + layout.addLayout(ur_layout) |
| 81 | + |
| 82 | + #cr_layout = QHBoxLayout() |
| 83 | + #self.create_region_check_box = QCheckBox("Create region showing matched residues") |
| 84 | + #cr_layout.addWidget(self.create_region_check_box, alignment=Qt.AlignLeft, stretch=1) |
| 85 | + #layout.addLayout(cr_layout) |
| 86 | + |
| 87 | + from Qt.QtWidgets import QDialogButtonBox as qbbox |
| 88 | + bbox = qbbox(qbbox.Ok | qbbox.Apply | qbbox.Close | qbbox.Help) |
| 89 | + bbox.accepted.connect(self.match) |
| 90 | + bbox.button(qbbox.Apply).clicked.connect(lambda f = self.match: f(apply=True)) |
| 91 | + hide_self = lambda *args, tw=tool_window: setattr(tool_window, 'shown', False) |
| 92 | + bbox.rejected.connect(hide_self) |
| 93 | + from chimerax.core.commands import run |
| 94 | + bbox.helpRequested.connect(lambda *, run=run, ses=self.sv.session, help=tool_window.help: |
| 95 | + run(ses, "help " + help)) |
| 96 | + layout.addWidget(bbox) |
| 97 | + |
| 98 | + tool_window.ui_area.setLayout(layout) |
| 99 | + |
| 100 | + def match(self, *, apply=False): |
| 101 | + from chimerax.core.errors import UserError |
| 102 | + ref = self.ref_chain_menu.value |
| 103 | + if ref is None: |
| 104 | + raise UserError("No reference chain chosen") |
| 105 | + used_chains = { ref.structure: ref } |
| 106 | + matches = self.match_chain_list.value |
| 107 | + if not matches: |
| 108 | + raise UserError("No match chains chosen") |
| 109 | + for match in matches: |
| 110 | + if match.structure in used_chains: |
| 111 | + self.tool_window.shown = True |
| 112 | + raise UserError("Cannot match multiple chains from the same structure (%s and %s)" |
| 113 | + % (used_chains[match.structure], match)) |
| 114 | + used_chains[match.structure] = match |
| 115 | + |
| 116 | + args = "" |
| 117 | + if self.conserved_check_box.isChecked(): |
| 118 | + if not self.conservation_value.hasAcceptableInput(): |
| 119 | + raise UserError("Percent identity value must be between 0 and 100") |
| 120 | + args += " conservation " + self.conservation_value.text() |
| 121 | + if self.iterate_check_box.isChecked(): |
| 122 | + if not self.iteration_value.hasAcceptableInput(): |
| 123 | + raise UserError("Iteration cutoff value must be 0 or more") |
| 124 | + args += " iterate " + self.iteration_value.text() |
| 125 | + else: |
| 126 | + args += " iterate none" |
| 127 | + if self.use_region_check_box.isChecked(): |
| 128 | + region = self.sv.active_region |
| 129 | + if region is None: |
| 130 | + raise UserError("No active region") |
| 131 | + cols = [] |
| 132 | + for block in region.blocks: |
| 133 | + line1, line2, pos1, pos2 = block |
| 134 | + cols.extend(list(range(pos1+1,pos2+2))) |
| 135 | + if not cols: |
| 136 | + raise UserError("Active region is empty") |
| 137 | + args += " columns %s" % ','.join(["%d" % col for col in cols]) |
| 138 | + |
| 139 | + if not apply: |
| 140 | + self.tool_window.shown = False |
| 141 | + from chimerax.core.commands import run, StringArg |
| 142 | + results = run(self.sv.session, "seq match %s %s to %s%s" |
| 143 | + % (StringArg.unparse(self.sv.alignment.ident), ref.atomspec, |
| 144 | + ','.join([c.atomspec for c in matches]), args)) |
| 145 | + |
| 146 | + if self.create_region_check_box.isChecked(): |
| 147 | + pass #TODO |
0 commit comments