Source code for schrodinger.application.desmond.report_helper

import io
from past.utils import old_div

import numpy
import PIL

import schrodinger.ui.qt.structure2d as structure2d
from schrodinger.Qt import QtCore
from schrodinger.Qt import QtGui
from schrodinger.Qt.QtCore import QBuffer
from schrodinger.Qt.QtCore import QIODevice
from schrodinger.Qt.QtCore import Qt
from schrodinger.Qt.QtGui import QColor
from schrodinger.Qt.QtGui import QImage
from schrodinger.Qt.QtGui import QPainter
from schrodinger.Qt.QtGui import QPicture
from schrodinger.utils import qapplication

# Import everything from rl_helper for backward compatibility, since rl_helper
# was spun off from report_helper
from .rl_helper import reportlab  # noqa: F401
from .rl_helper import rlcolors  # noqa: F401
from .rl_helper import rlcanvas  # noqa: F401
from .rl_helper import platypus  # noqa: F401
from .rl_helper import inch  # noqa: F401
from .rl_helper import styles  # noqa: F401
from .rl_helper import HeaderStyle  # noqa: F401
from .rl_helper import ParaStyle  # noqa: F401
from .rl_helper import gray  # noqa: F401
from .rl_helper import colors  # noqa: F401
from .rl_helper import blackColorMap  # noqa: F401
from .rl_helper import new_page  # noqa: F401
from .rl_helper import add_vtable  # noqa: F401
from .rl_helper import add_table  # noqa: F401
from .rl_helper import add_and_parse_SMILES  # noqa: F401
from .rl_helper import add_spacer  # noqa: F401
from .rl_helper import get_image  # noqa: F401
from .rl_helper import report_add_ms_logo  # noqa: F401
from .rl_helper import report_add_logo  # noqa: F401
from .rl_helper import header  # noqa: F401
from .rl_helper import get_pargph  # noqa: F401
from .rl_helper import pargph  # noqa: F401
from .rl_helper import aspectScale  # noqa: F401
from .rl_helper import NumberedCanvas  # noqa: F401


[docs]def replica_name(n): if n < 26: return chr(ord('A') + n) else: return replica_name(old_div(n, 26) - 1) + replica_name(n % 26)
[docs]def change_plot_colors(axis, spines=True, ticks=True, labels=True, polar=False, label_size=8): gr = '#888888' if ticks: axis.tick_params(axis='x', colors=gr, labelsize=label_size) axis.tick_params(axis='y', colors=gr, labelsize=label_size) if spines: types = ['top', 'bottom', 'right', 'left'] if polar: types = ['polar'] for s in types: axis.spines[s].set_color(gr) if labels: axis.yaxis.label.set_color(gr) axis.xaxis.label.set_color(gr)
[docs]def load_gui(): return qapplication.get_application()
[docs]def generate_ligand_2d_placeholder(filename, natoms): qpic = QPicture() painter = QPainter(qpic) txt = "Molecule too big\n to render in 2D." brect = painter.drawText(0, 0, 200, 200, QtCore.Qt.AlignCenter, txt) painter.end() qpic.setBoundingRect(brect) # Convert the QPicture into QImage: img = QImage(QtCore.QSize(200, 200), QImage.Format_ARGB32) img.fill(0) # Initialize with zeros to overwrite garbage values painter2 = QtGui.QPainter() painter2.begin(img) painter2.drawPicture(0, 0, qpic) painter2.end() img.save(filename)
[docs]def generate_aligned_2d_ligand_pair(fn_list, ct1, ct2, aligned_core): """ Given two ligands, try to generate a 2d-plot where they're aligned. """ from .depictor import Depictor from .depictor import get_aligned_chmmol_from_ct chmmol0, chmmol1 = get_aligned_chmmol_from_ct(ct1, ct2, aligned_core[0], aligned_core[1], [], []) chmmol_list = [chmmol0, chmmol1] depictor = Depictor() depictor.setCoorGenMode(False) for fn, lig in zip(fn_list, chmmol_list): depictor.mol2image(lig, fn)
[docs]def generate_ligand_2d_image(filename, ligand_st=None, scene=None, crop=True, ret_size=False): ''' given a scene, or a ligand st, output the 2d image''' # if ligand is greater than 100 heavy atoms just return a # placeholder if ligand_st: atom_heavy = len( [atom for atom in ligand_st.atom if atom.atomic_number != 1]) else: return if atom_heavy > 100: generate_ligand_2d_placeholder(filename, ligand_st.atom_total) if ret_size: return (200, 200) return if scene is None: ligand_pic = structure2d.structure_item() ligand_pic.set_structure(ligand_st) ligand_pic.generate_picture() scene = structure2d.structure_scene() scene.addItem(ligand_pic) scene.setBackgroundBrush(Qt.white) img1 = QImage(1600, 1600, QImage.Format_ARGB32_Premultiplied) p = QPainter(img1) scene.render(p) p.end() if crop: img1 = crop_image(img1) img1.save(filename) if ret_size: return img1.size
[docs]def crop_image(image_in): ''' get rid of the white background rgb=[255,255,255]''' #convert image to an NP array if isinstance(image_in, QImage): image_in = convertQimageToImage(image_in) image_data = numpy.asarray(image_in) # get the smallest value from RGB image_data_bw = image_data.min(axis=2) # get non-white rows and columns non_empty_columns = numpy.where(image_data_bw.min(axis=0) != 255)[0] # see if clear backckground works better non_clear_columns = numpy.where(image_data_bw.min(axis=0) != 0)[0] if len(non_clear_columns) < len(non_empty_columns): non_empty_columns = non_clear_columns non_empty_rows = numpy.where(image_data_bw.min(axis=1) != 255)[0] if len(non_empty_columns) == 0 and len(non_empty_rows): return image_in cropBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns)) image_data_new = image_data[cropBox[0]:cropBox[1] + 1, cropBox[2]:cropBox[3] + 1, :] image_out = PIL.Image.fromarray(image_data_new) return image_out
[docs]def convertQimageToImage(qimg): ''' converts QImage object to PIL Image object, in order to crop''' buffer = QBuffer() buffer.open(QIODevice.ReadWrite) qimg.save(buffer, "PNG") strio = io.BytesIO() strio.write(buffer.data()) buffer.close() strio.seek(0) img = PIL.Image.open(strio) return img
[docs]def get_qcolor(hex_color): qc = QColor() qc.setNamedColor(hex_color) return qc
[docs]def save_2d_annotated_img(structure_item, filename, crop=True, ret_size=False): #img1 = QtGui.QImage(1600, 1600, QtGui.QImage.Format_ARGB32_Premultiplied) img1 = QtGui.QImage(1200, 1200, QtGui.QImage.Format_ARGB32) img1.fill(QtCore.Qt.white) scene = structure2d.structure_scene() scene.addItem(structure_item) p = QtGui.QPainter(img1) scene.render(p) p.end() if crop: img1 = crop_image(img1) img1.save(filename) if ret_size: return convertQimageToImage(img1).size