/* * src/graphics-ligand-mesh-molecule.cc * * Copyright 2020 by Medical Research Council * Author: Paul Emsley * * This file is part of Coot * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 3 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copies of the GNU General Public License and * the GNU Lesser General Public License along with this program; if not, * write to the Free Software Foundation, Inc., 51 Franklin Street, * Fifth Floor, Boston, MA, 02110-1301, USA. * See http://www.gnu.org/licenses/ * */ #include // Do we need this block? // #include #ifdef USE_PYTHON #include "Python.h" #endif #include "graphics-info.h" // because gtk_gl_area_attach_buffers() /////////////////////////// #include "graphics-ligand-mesh-molecule.hh" #ifdef MAKE_ENHANCED_LIGAND_TOOLS #include "lidia-core/rdkit-interface.hh" #endif graphics_ligand_mesh_molecule_t::~graphics_ligand_mesh_molecule_t() {} // I used to understand polymorphism. Now this looks to me like a mystical incantation. // template lig_build::molecule_t::~molecule_t() {} bool graphics_ligand_mesh_molecule_t::setup_from(int imol_in, mmdb::Residue *residue_p, const std::string &alt_conf, coot::protein_geometry *geom_p) { bool status = false; imol = imol_in; #ifdef MAKE_ENHANCED_LIGAND_TOOLS imol = imol_in; if (residue_p) { try { std::string res_name = residue_p->GetResName(); std::pair p = geom_p->get_monomer_restraints_at_least_minimal(res_name, imol); if (! p.first) { std::cout << "DEBUG:: graphics_ligand_molecule: No restraints for \"" << res_name << "\"" << std::endl; } else { const coot::dictionary_residue_restraints_t &restraints = p.second; RDKit::RWMol rdkm = coot::rdkit_mol(residue_p, restraints, alt_conf); // std::cout << "--------------------- graphics-ligand-view setup_from() 1 " << std::endl; // coot::debug_rdkit_molecule(&rdkm); unsigned int n_atoms = rdkm.getNumAtoms(); if (n_atoms > 1) { // return a kekulize mol RDKit::RWMol rdk_mol_with_no_Hs = coot::remove_Hs_and_clean(rdkm); double weight_for_3d_distances = 0.005; int mol_2d_depict_conformer = coot::add_2d_conformer(&rdk_mol_with_no_Hs, weight_for_3d_distances); // why is there no connection between a lig_build molecule_t // and a rdkit molecule conformer? // For now hack around using a molfile molecule... // // I think I should have a rdkit_mol->lig_build::molecule_t converter // (for later). lig_build::molfile_molecule_t m = coot::make_molfile_molecule(rdk_mol_with_no_Hs, mol_2d_depict_conformer); if (false) { std::cout << "make_molfile_molecule() makes molecule: " << std::endl; m.debug(); } init_from_molfile_molecule(m); status = true; // OK, if we got to here... } } } catch (const std::runtime_error &coot_error) { std::cout << coot_error.what() << std::endl; } catch (const std::exception &rdkit_error) { std::cout << rdkit_error.what() << std::endl; } } #endif return status; } void graphics_ligand_mesh_molecule_t::init_from_molfile_molecule(const lig_build::molfile_molecule_t &mol_in) { atoms.clear(); bonds.clear(); scale_correction = mol_in.get_scale_correction(); // so that the median bond length is 1.0 scale_correction.second *= 1.0; // std::cout << "debug:: scale_correction " << scale_correction.first // << " " << scale_correction.second << std::endl; for (unsigned int iat=0; iat pt = fill_mesh_bonds(); std::vector triangle_vertices(3*pt.size()); for (unsigned int i=0; i graphics_ligand_mesh_molecule_t::fill_mesh_bonds() { // add to polygon_vertices - no longer do we make lines 20230617-PE auto gl_bond = [] (const graphics_ligand_mesh_bond &bond, const lig_build::pos_t &pos_1_raw, const lig_build::pos_t &pos_2_raw, bool shorten_first, bool shorten_second, std::vector &polygon_vertices) { lig_build::bond_t::bond_type_t bt = bond.get_bond_type(); double shorten_fraction = 0.8; lig_build::pos_t pos_1 = pos_1_raw; lig_build::pos_t pos_2 = pos_2_raw; // fraction_point() returns a point that is (say) 0.8 of the way // from p1 (first arg) to p2 (second arg). // if (shorten_first) pos_1 = lig_build::pos_t::fraction_point(pos_2_raw, pos_1_raw, shorten_fraction); if (shorten_second) pos_2 = lig_build::pos_t::fraction_point(pos_1_raw, pos_2_raw, shorten_fraction); switch (bt) { case lig_build::bond_t::SINGLE_BOND: // Add new cases, a bit of a hack of course. case lig_build::bond_t::SINGLE_OR_DOUBLE: case lig_build::bond_t::SINGLE_OR_AROMATIC: case lig_build::bond_t::AROMATIC_BOND: case lig_build::bond_t::DELOC_ONE_AND_A_HALF: case lig_build::bond_t::BOND_ANY: { // line_vertices.push_back(pos_t_to_glm(pos_1)); // line_vertices.push_back(pos_t_to_glm(pos_2)); // in the new system, a line is 2 triangles lig_build::pos_t buv = (pos_1-pos_2).unit_vector(); lig_build::pos_t buv_90 = buv.rotate(90); float thickness = 0.065; lig_build::pos_t p_1 = pos_1 + buv_90 * thickness; lig_build::pos_t p_2 = pos_1 - buv_90 * thickness; lig_build::pos_t p_3 = pos_2 + buv_90 * thickness; lig_build::pos_t p_4 = pos_2 - buv_90 * thickness; polygon_vertices.push_back(position_triple_t(p_1, p_2, p_3)); polygon_vertices.push_back(position_triple_t(p_2, p_4, p_3)); } break; case lig_build::bond_t::DOUBLE_BOND: case lig_build::bond_t::DOUBLE_OR_AROMATIC: { if (bond.have_centre_pos()) { lig_build::pos_t pos_1_local = pos_1; lig_build::pos_t pos_2_local = pos_2; double shorten_fraction_for_double = 0.82; if (shorten_first) pos_1 = lig_build::pos_t::fraction_point(pos_2_local, pos_1_local, shorten_fraction_for_double); if (shorten_second) pos_2 = lig_build::pos_t::fraction_point(pos_1_local, pos_2_local, shorten_fraction_for_double); std::pair p = bond.make_double_aromatic_short_stick(pos_1_local, pos_2_local, shorten_first, shorten_second); // line_vertices.push_back(pos_t_to_glm(pos_1)); // line_vertices.push_back(pos_t_to_glm(pos_2)); // line_vertices.push_back(pos_t_to_glm(p.first)); // line_vertices.push_back(pos_t_to_glm(p.second)); lig_build::pos_t buv = (pos_1-pos_2).unit_vector(); lig_build::pos_t buv_90 = buv.rotate(90); float thickness = 0.065; lig_build::pos_t p_1 = pos_1 + buv_90 * thickness; lig_build::pos_t p_2 = pos_1 - buv_90 * thickness; lig_build::pos_t p_3 = pos_2 + buv_90 * thickness; lig_build::pos_t p_4 = pos_2 - buv_90 * thickness; polygon_vertices.push_back(position_triple_t(p_1, p_2, p_3)); polygon_vertices.push_back(position_triple_t(p_2, p_4, p_3)); buv = (p.first-p.second).unit_vector(); buv_90 = buv.rotate(90); p_1 = p.first + buv_90 * thickness; p_2 = p.first - buv_90 * thickness; p_3 = p.second + buv_90 * thickness; p_4 = p.second - buv_90 * thickness; polygon_vertices.push_back(position_triple_t(p_1, p_2, p_3)); polygon_vertices.push_back(position_triple_t(p_2, p_4, p_3)); } else { std::pair, std::pair > p = bond.make_double_bond(pos_1, pos_2, shorten_first, shorten_second); // line_vertices.push_back(pos_t_to_glm(p.first.first)); // line_vertices.push_back(pos_t_to_glm(p.first.second)); // line_vertices.push_back(pos_t_to_glm(p.second.first)); // line_vertices.push_back(pos_t_to_glm(p.second.second)); lig_build::pos_t buv = (p.first.first-p.first.second).unit_vector(); lig_build::pos_t buv_90 = buv.rotate(90); float thickness = 0.065; lig_build::pos_t p_1 = p.first.first + buv_90 * thickness; lig_build::pos_t p_2 = p.first.first - buv_90 * thickness; lig_build::pos_t p_3 = p.first.second + buv_90 * thickness; lig_build::pos_t p_4 = p.first.second - buv_90 * thickness; polygon_vertices.push_back(position_triple_t(p_1, p_2, p_3)); polygon_vertices.push_back(position_triple_t(p_2, p_4, p_3)); buv = (p.first.first-p.first.second).unit_vector(); buv_90 = buv.rotate(90); p_1 = p.second.first + buv_90 * thickness; p_2 = p.second.first - buv_90 * thickness; p_3 = p.second.second + buv_90 * thickness; p_4 = p.second.second - buv_90 * thickness; polygon_vertices.push_back(position_triple_t(p_1, p_2, p_3)); polygon_vertices.push_back(position_triple_t(p_2, p_4, p_3)); } } break; case lig_build::bond_t::TRIPLE_BOND: { lig_build::pos_t buv = (pos_2-pos_1).unit_vector(); lig_build::pos_t buv_90 = buv.rotate(90); double small = 0.25; lig_build::pos_t p1 = pos_1 + buv_90 * small; lig_build::pos_t p2 = pos_2 + buv_90 * small; lig_build::pos_t p3 = pos_1; lig_build::pos_t p4 = pos_2; lig_build::pos_t p5 = pos_1 - buv_90 * small; lig_build::pos_t p6 = pos_2 - buv_90 * small; // line_vertices.push_back(pos_t_to_glm(p1)); // line_vertices.push_back(pos_t_to_glm(p2)); // line_vertices.push_back(pos_t_to_glm(p3)); // line_vertices.push_back(pos_t_to_glm(p4)); // line_vertices.push_back(pos_t_to_glm(p5)); // line_vertices.push_back(pos_t_to_glm(p6)); auto pairs = {std::make_pair(p1,p2), std::make_pair(p3,p4), std::make_pair(p5,p6)}; for (const auto &pair : pairs) { const auto &p1 = pair.first; const auto &p2 = pair.second; lig_build::pos_t buv2 = (p1-p2).unit_vector(); lig_build::pos_t buv2_90 = buv2.rotate(90); float thickness = 0.045; lig_build::pos_t p_1 = p1 + buv2_90 * thickness; lig_build::pos_t p_2 = p1 - buv2_90 * thickness; lig_build::pos_t p_3 = p2 + buv2_90 * thickness; lig_build::pos_t p_4 = p2 - buv2_90 * thickness; polygon_vertices.push_back(position_triple_t(p_1, p_2, p_3)); polygon_vertices.push_back(position_triple_t(p_2, p_4, p_3)); } } break; case lig_build::bond_t::IN_BOND: { // set of lines std::vector > vp = lig_build::pos_t::make_wedge_in_bond(pos_1, pos_2); if (vp.size()) { for (unsigned int i=0; i v = lig_build::pos_t::make_wedge_out_bond(pos_1, pos_2); if (v.size() == 4) { // make 2 triangle from this polygon_vertices.push_back(position_triple_t(v[0], v[1], v[2])); polygon_vertices.push_back(position_triple_t(v[2], v[3], v[0])); } } break; case lig_build::bond_t::BOND_UNDEFINED: break; } }; std::vector line_vertices; std::vector polygon_vertices; // for wedge bonds; for (unsigned int ib=0; ib (polygon_vertices); } void graphics_ligand_mesh_molecule_t::fill_mesh_atoms() { // this function should be called assign_atom_colours() we don't do anything with the mesh. // All complexities happen at draw time. bool background_black_flag = true; for (unsigned int iat=0; iat local_bonds = bonds_having_atom_with_atom_index(iat); bool gl_flag = true; lig_build::atom_id_info_t atom_id_info = make_atom_id_by_using_bonds(iat, ele, local_bonds, gl_flag); coot::colour_t col = atoms[iat].get_colour(background_black_flag); // using ele atoms[iat].colour = col; if (false) std::cout << "in fill_mesh_atoms() atom_index " << iat << " with charge " << atoms[iat].charge << " made atom_id_info:\n" << atom_id_info << " assign colour " << col << std::endl; } } } coot::colour_t graphics_ligand_mesh_atom::get_colour(bool dark_bg) const { coot::colour_t col; if (element == "Br") { col.col[0] = 0.66; col.col[1] = 0.2; col.col[2] = 0.2; } if (element == "I") { col.col[0] = 0.42; col.col[1] = 0.1; col.col[2] = 0.8; } if ((element == "F") || (element == "Cl")) { col.col[0] = 0.3; col.col[1] = 0.7; col.col[2] = 0.3; } if (element == "O") { col.col[0] = 0.9; col.col[1] = 0.3; col.col[2] = 0.3; } if (element == "P") { col.col[0] = 0.7; col.col[1] = 0.3; col.col[2] = 0.9; } if ((element == "S") || (element == "Se")) { col.col[0] = 0.76; col.col[1] = 0.76; col.col[2] = 0.2; } if (element == "N") { col.col[0] = 0.5; col.col[1] = 0.5; col.col[2] = 1.0; } return col; } void graphics_ligand_mesh_molecule_t::draw(Shader *shader_p, Shader *hud_text_shader_p, float widget_width, float widget_height, const std::map &ft_characters) { // this whole function needs to be rethought and replaced. auto pos_t_to_glm = [] (const lig_build::pos_t &p) { return glm::vec2(p.x, p.y); }; // draw "line" bonds and wedge bonds // // This argument order doesn't expand the ligand - it just shifts it mesh.draw(shader_p, widget_height, widget_width); // weird! // This argument expands the ligand // mesh.draw(shader_p, widget_width, widget_height); // draw atoms (text) // double window_rescale_factor_x = 900.0/static_cast(widget_width); double window_rescale_factor_y = 900.0/static_cast(widget_height); glm::vec2 position_on_canvas(-0.61, -0.61); bool gl_flag = true; for (unsigned int iat=0; iat local_bonds = bonds_having_atom_with_atom_index(iat); lig_build::atom_id_info_t atom_id_info = make_atom_id_by_using_bonds(iat, ele, local_bonds, gl_flag); for (unsigned int i=0; i(widget_width), sc * 900.0/static_cast(widget_height)); // std::cout << ".... debug:: " << window_rescale_factor_x << " " << window_rescale_factor_y << std::endl; glm::vec2 rejigged_pos(pos.x * window_rescale_factor_x, pos.y * window_rescale_factor_y); hud_texture_tmesh.set_position_and_scales(rejigged_pos + position_on_canvas, scales); if (false) std::cout << "debug;: graphics_ligand_mesh_molecule_t::draw() calling draw_label(): iat " << iat << " ioff " << i << " " << " \"" << offset.text << "\" subscript " << offset.subscript << " superscript " << offset.superscript << " length: " << offset.text.length() << " colour " << atom.colour << std::endl; glm::vec4 colour = atom.colour.to_glm(); hud_texture_tmesh.draw_label(label, colour, hud_text_shader_p, ft_characters); } } } }