Source code for schrodinger.ui.sequencealignment.contact_map
from past.utils import old_div
from schrodinger.Qt import QtCore
from schrodinger.Qt import QtGui
from schrodinger.Qt import QtWidgets
[docs]class ContactMap(QtWidgets.QDialog):
[docs] def __init__(self, parent, title="Contact Map"):
QtWidgets.QDialog.__init__(self, parent)
self.resize(500, 500)
self.setWindowTitle(title)
self.contact_map = None
self.map_image = None
self.map_layout = QtWidgets.QVBoxLayout(self)
self.map_frame = QtWidgets.QFrame(self)
self.map_frame.setLineWidth(1)
self.map_layout.addWidget(self.map_frame)
[docs] def paintEvent(self, event):
QtWidgets.QDialog.paintEvent(self, event)
painter = QtGui.QPainter(self)
if self.map_image:
img_width = self.map_image.width()
img_height = self.map_image.height()
frame_width = self.map_frame.width()
frame_height = self.map_frame.height()
if frame_width > frame_height:
yscale = old_div(float(frame_height), img_height)
xscale = old_div(float(frame_height), img_width)
else:
yscale = old_div(float(frame_width), img_height)
xscale = old_div(float(frame_width), img_width)
scaled_width = xscale * img_width - 50
scaled_height = yscale * img_height - 50
image = self.map_image.scaled(scaled_width, scaled_height)
image = image.mirrored(vertical=True)
xpos = self.map_frame.x() + 0.5 * (frame_width - scaled_width)
ypos = self.map_frame.y() + 0.5 * (frame_height - scaled_height)
painter.drawImage(xpos, ypos, image)
painter.setPen(QtCore.Qt.black)
font = painter.font()
font.setPointSize(8)
painter.setFont(font)
painter.drawRect(xpos, ypos, scaled_width, scaled_height)
hscale = old_div(float(scaled_width), img_width)
vscale = old_div(float(scaled_height), img_height)
pos = 0
while pos < img_width:
painter.drawLine(xpos + pos * hscale, ypos + scaled_height,
xpos + pos * hscale, ypos + scaled_height + 5)
if pos == 0:
tlabel = "1"
else:
tlabel = str(pos)
painter.drawText(xpos + pos * hscale - 2,
ypos + scaled_height + 20, tlabel)
pos += 10
pos = 0
while pos < img_height:
painter.drawLine(xpos - 5,
ypos + scaled_height - pos * vscale - 1, xpos,
ypos + scaled_height - pos * vscale - 1)
if pos == 0:
tlabel = "1"
else:
tlabel = str(pos)
painter.drawText(xpos - 60,
ypos + scaled_height - pos * vscale - 7, 50,
10, QtCore.Qt.AlignRight, tlabel)
pos += 10
[docs] def setMap(self, contact_map, width, height):
self.contact_map = contact_map
self.map_image = QtGui.QImage(width, height, QtGui.QImage.Format_RGB32)
min_val = max_val = contact_map[0]
for y in range(height):
for x in range(width):
if contact_map[y * width + x] < min_val:
min_val = contact_map[y * width + x]
if contact_map[y * width + x] > max_val:
max_val = contact_map[y * width + x]
if max_val != min_val:
irange = old_div(1.0, (max_val - min_val))
for y in range(height):
for x in range(width):
value = (contact_map[y * width + x] - min_val) * irange
# if value > 0.99:
# color = QtGui.QColor(255, 255, 255)
# else:
color = QtGui.QColor.fromHsvF(0.66 * (value), 0.75, 1.0)
self.map_image.setPixel(x, y, color.rgb())