/* * lidia-core/svg-molecule.cc * * Copyright 2022 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 * 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/ */ #ifdef MAKE_ENHANCED_LIGAND_TOOLS #include "svg-molecule.hh" #include "rdkit-interface.hh" // put this into (new file) svg-molecule.cc when you're happy with it void svg_molecule_t::import_rdkit_mol(RDKit::ROMol *rdkm, int iconf) { // we don't fiddle with the hydrogens here, we just draw what we are given. // // So typically, user has called Chem.RemoveHs() or remove_non_polar_Hs() // and MolOps::Kekulize() and WedgeMolBonds() before calling this function. int n_conf = rdkm->getNumConformers(); if (iconf < n_conf) { const RDKit::PeriodicTable *tbl = RDKit::PeriodicTable::getTable(); RDKit::Conformer &conf = rdkm->getConformer(iconf); unsigned int n_mol_atoms = rdkm->getNumAtoms(); // determine the centre correction double sum_x = 0; double sum_y = 0; double min_y = 9e9; for (unsigned int iat=0; iat bond_lengths; for (unsigned int i=0; igetNumBonds(); i++) { const RDKit::Bond *bond_p = rdkm->getBondWithIdx(i); int idx_1 = bond_p->getBeginAtomIdx(); int idx_2 = bond_p->getEndAtomIdx(); if ( (*rdkm)[idx_1]->getAtomicNum() != 1) { if ( (*rdkm)[idx_2]->getAtomicNum() != 1) { RDGeom::Point3D &r_pos_1 = conf.getAtomPos(idx_1); RDGeom::Point3D &r_pos_2 = conf.getAtomPos(idx_2); clipper::Coord_orth p1(r_pos_1.x, r_pos_1.y, r_pos_1.z); clipper::Coord_orth p2(r_pos_2.x, r_pos_2.y, r_pos_2.z); double l = clipper::Coord_orth::length(p1, p2); bond_lengths.push_back(l); } } } if (bond_lengths.size() > 0) { std::sort(bond_lengths.begin(), bond_lengths.end()); int index = bond_lengths.size()/2; double bll = bond_lengths[index]; double scale = 1.0/bll; // scale_correction.first = 1; // scale_correction.second = scale; } for (unsigned int iat=0; iatgetProp("name", name); } catch (const KeyErrorException &kee) { // we don't need to see these. We get them when reading an mdl file // (for example). // std::cout << "caught no-name for atom exception in import_rdkit_mol(): " // << kee.what() << std::endl; } try { at_p->getProp("molFileAlias", name); } catch (const KeyErrorException &kee) { } // lig_build::pos_t pos = input_coords_to_canvas_coords(cp); lig_build::pos_t pos(r_pos.x, r_pos.y); int n = at_p->getAtomicNum(); std::string element = tbl->getElementSymbol(n); int charge = at_p->getFormalCharge(); if (false) std::cout << "found atom " << iat << " with element \"" << element << "\" with formal charge " << charge << std::endl; svg_atom_t mol_at(pos, element, charge); mol_at.charge = charge; if (! name.empty()) mol_at.atom_name = name; std::pair added = add_atom(mol_at); } unsigned int n_bonds = rdkm->getNumBonds(); // std::cout << "considering " << n_bonds << " bonds" << std::endl; for (unsigned int ib=0; ibgetBondWithIdx(ib); int idx_1 = bond_p->getBeginAtomIdx(); int idx_2 = bond_p->getEndAtomIdx(); lig_build::bond_t::bond_type_t bt = coot::convert_bond_type(bond_p->getBondType()); if (false) std::cout << "on import got bt " << bt << " from " << bond_p->getBondType() << std::endl; try { const svg_atom_t &cat1 = atoms[idx_1]; const svg_atom_t &cat2 = atoms[idx_2]; bool shorten_first = false; bool shorten_second = false; if ( (*rdkm)[idx_1]->getAtomicNum() != 6) { shorten_first = true; } else { // how many bonds does this atom have? // if (mol[idx_1].getNumBonds() == 1) // shorten_first = true; } if ( (*rdkm)[idx_2]->getAtomicNum() != 6) { shorten_second = true; } else { } // do we need to pass idx_1 and idx_2, neighbour info? // cairo_bond_t bond(idx_1, idx_2, cat1, cat2, shorten_first, shorten_second, // bt, empty, empty); svg_bond_t bond(idx_1, idx_2, bt); RDKit::Bond::BondDir bond_dir = bond_p->getBondDir(); if (false) std::cout << "bond " << ib << ": type " << bt << " between " << idx_1 << " at " << conf.getAtomPos(idx_1) << " and " << idx_2 << " at " << conf.getAtomPos(idx_2) << " dir " << bond_dir << std::endl; if (bond_dir != RDKit::Bond::NONE) { if (bond_dir == RDKit::Bond::BEGINWEDGE) { bond.set_bond_type(lig_build::bond_t::OUT_BOND); } if (bond_dir == RDKit::Bond::BEGINDASH) bond.set_bond_type(lig_build::bond_t::IN_BOND); } add_bond(bond); } catch (...) { std::cout << "WARNING:: problem. scrambled input molecule? numbers of atoms: "; std::cout << rdkm->getNumAtoms() << " vs " << get_number_of_atoms_including_hydrogens() << std::endl; } } assign_ring_centres(); } median_bond_length_ = median_bond_length(); } // This is for a double bond in a ring // // pos_1 and pos_2 are the coordinates of the atoms, not the pre-shorted // coordinates // std::string svg_bond_t::draw_double_in_ring_bond(const lig_build::pos_t &pos_1_in, const lig_build::pos_t &pos_2_in, const std::string &bond_colour, bool shorten_first, bool shorten_second, const lig_build::pos_t ¢re, double scale, bool dashed_inner) { std::string s; lig_build::pos_t pos_1 = pos_1_in; lig_build::pos_t pos_2 = pos_2_in; double shorten_fraction = 0.74; if (shorten_first) pos_1 = lig_build::pos_t::fraction_point(pos_2_in, pos_1_in, shorten_fraction); if (shorten_second) pos_2 = lig_build::pos_t::fraction_point(pos_1_in, pos_2_in, shorten_fraction); // outside "normal" bond // lig_build::pos_t p1 = svg_molecule_t::mol_coords_to_svg_coords(pos_1, centre, scale); // lig_build::pos_t p2 = svg_molecule_t::mol_coords_to_svg_coords(pos_2, centre, scale); lig_build::pos_t p1 = pos_1; lig_build::pos_t p2 = pos_2; // for inner bond std::pair p = make_double_aromatic_short_stick(pos_1_in, pos_2_in, shorten_first, shorten_second); // cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); // cairo_move_to(cr, p1.x, p1.y); // cairo_line_to(cr, p2.x, p2.y); // cairo_stroke(cr); s += make_bond_line_string(p1, p2, scale, bond_colour); // p1 = svg_molecule_t::mol_coords_to_svg_coords(p.first, centre, scale); // p2 = svg_molecule_t::mol_coords_to_svg_coords(p.second, centre, scale); p1 = p.first; p2 = p.second; // cairo_move_to(cr, p1.x, p1.y); // cairo_line_to(cr, p2.x, p2.y); // cairo_stroke(cr); if (dashed_inner) { s += make_dashed_bond_line_string(p1, p2, bond_colour); } else { s += make_bond_line_string(p1, p2, scale, bond_colour); } if (dashed_inner) { //cairo_set_dash(cr, NULL, 0, 0); // restore } // std::cout << "draw_double_in_ring_bond return s " << s << std::endl; return s; } // not in-ring // // shorten_first and shorten_second are set depending on the element of the atoms and number of bonds. std::string svg_bond_t::draw_double_bond(const lig_build::atom_t &at_1, const lig_build::atom_t &at_2, const std::string &bond_colour, bool shorten_first, bool shorten_second, const std::vector > &other_connections_to_first_atom, const std::vector > &other_connections_to_second_atom, const lig_build::pos_t ¢re, double scale) { std::string s; lig_build::pos_t pos_1 = at_1.atom_position; lig_build::pos_t pos_2 = at_2.atom_position; double shorten_fraction = 0.74; if (shorten_first) pos_1 = lig_build::pos_t::fraction_point(at_2.atom_position, at_1.atom_position, shorten_fraction); if (shorten_second) pos_2 = lig_build::pos_t::fraction_point(at_1.atom_position, at_2.atom_position, shorten_fraction); // the boring case (symmetrically, same-length both displaced) (e.g. N=N, C=C) occurs when: // not // at least one C atom with at least one bond and not one C atom with more than one bond. // bool do_boring_case = false; if ((other_connections_to_second_atom.size() == 0) && (other_connections_to_first_atom.size() == 0)) { do_boring_case = true; } unsigned int n_C = 0; if (at_1.element == "C") n_C++; if (at_2.element == "C") n_C++; if (n_C == 0) { do_boring_case = true; } else { // more than 1 Carbon if (n_C == 2) { if (other_connections_to_first_atom.size() == 0) if (other_connections_to_second_atom.size() == 0) do_boring_case = true; } if (n_C == 1) { if (at_1.element == "C") if (other_connections_to_first_atom.size() > 1) do_boring_case = true; if (at_2.element == "C") if (other_connections_to_second_atom.size() > 1) do_boring_case = true; } } if (do_boring_case) { // std::cout << "boring case" << std::endl; // boring case :-) std::pair, std::pair > p = make_double_bond(at_1.atom_position, at_2.atom_position, shorten_first, shorten_second); // lig_build::pos_t p1 = svg_molecule_t::mol_coords_to_svg_coords(p.first.first, centre, scale); // lig_build::pos_t p2 = svg_molecule_t::mol_coords_to_svg_coords(p.first.second, centre, scale); lig_build::pos_t p1 = p.first.first; lig_build::pos_t p2 = p.first.second; // cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); // cairo_move_to(cr, p1.x, p1.y); // cairo_line_to(cr, p2.x, p2.y); // cairo_stroke(cr); s += make_bond_line_string(p1, p2, scale, bond_colour); // p1 = svg_molecule_t::mol_coords_to_svg_coords(p.second.first, centre, scale); // p2 = svg_molecule_t::mol_coords_to_svg_coords(p.second.second, centre, scale); p1 = p.second.first; p2 = p.second.second; // cairo_move_to(cr, p1.x, p1.y); // cairo_line_to(cr, p2.x, p2.y); // cairo_stroke(cr); s += make_bond_line_string(p1, p2, scale, bond_colour); } else { // std::cout << "double bond elegant case" << std::endl; // elegant case, offset, shorten a bond std::pair, std::pair > bonds = make_double_bond(at_1.atom_position, at_2.atom_position, shorten_first, shorten_second, other_connections_to_first_atom, other_connections_to_second_atom); lig_build::pos_t p1 = bonds.first.first; lig_build::pos_t p2 = bonds.first.second; s += make_bond_line_string(p1, p2, scale, bond_colour); lig_build::pos_t p1s = bonds.second.first; lig_build::pos_t p2s = bonds.second.second; s += make_bond_line_string(p1s, p2s, scale, bond_colour); } return s; } // static lig_build::pos_t svg_molecule_t::mol_coords_to_svg_coords(const lig_build::pos_t &pos_1, const lig_build::pos_t ¢re, double scale) { lig_build::pos_t p1 = (pos_1 - centre) * scale; p1.y = -p1.y; // canvas is upside down c.f. normal/real-world/molecule coordinates p1 += lig_build::pos_t(0.5,0.5); // 20241116-PE removed this offset - why was it here? if (false) std::cout << "mol_coords_to_svg_coords: " << " scale " << scale << " input " << pos_1 << " centre " << centre << " output " << p1 << std::endl; return p1; } std::string svg_bond_t::make_bond_line_string(const lig_build::pos_t &p1, const lig_build::pos_t &p2, double sf, const std::string &bond_colour) const { std::string s; s += " \n"; return s; } std::string svg_bond_t::make_dashed_bond_line_string(const lig_build::pos_t &p1, const lig_build::pos_t &p2, const std::string &bond_colour) const { double sf = 1.0; std::string s; s += " \n"; return s; } std::string svg_bond_t::draw_bond(const svg_atom_t &at_1, const svg_atom_t &at_2, bool at_1_in_ring_flag, bool at_2_in_ring_flag, lig_build::bond_t::bond_type_t bt, const std::string &bond_colour, bool shorten_first, bool shorten_second, const std::vector > &other_connections_to_first_atom, const std::vector > &other_connections_to_second_atom, const lig_build::pos_t ¢re, double scale) { if (false) std::cout << "\ndebug:: in svg_bond_t::draw_bond() --- start --- " << " at_1 " << at_1.atom_id << " " << at_1.atom_position << " at_2 " << at_2.atom_id << " " << at_2.atom_position << " centre " << centre << std::endl; scale = 1.0; std::string s; lig_build::pos_t pos_1_in = at_1.atom_position; lig_build::pos_t pos_2_in = at_2.atom_position; lig_build::pos_t pos_1 = pos_1_in; lig_build::pos_t pos_2 = pos_2_in; // fraction_point() returns a point that is (say) 0.8 of the way // from p1 (first arg) to p2 (second arg). // double shorten_fraction = 0.74; if (shorten_first) pos_1 = lig_build::pos_t::fraction_point(pos_2_in, pos_1_in, shorten_fraction); if (shorten_second) pos_2 = lig_build::pos_t::fraction_point(pos_1_in, pos_2_in, shorten_fraction); if (false) std::cout << "------ draw_bond for bt " << bt << " between " << at_1 << " " << at_1.atom_name << " and " << at_2 << " " << at_2.atom_name << " and " << " c.f. " << " single-bond " << lig_build::bond_t::SINGLE_BOND << " single-or-double " << lig_build::bond_t::SINGLE_OR_DOUBLE << " single-or-aromatic " << lig_build::bond_t::SINGLE_OR_AROMATIC << " deloc-one-and-half " << lig_build::bond_t::DELOC_ONE_AND_A_HALF << " bond-any " << lig_build::bond_t::BOND_ANY << " in-bond " << lig_build::bond_t::IN_BOND << " out-bond " << lig_build::bond_t::OUT_BOND << std::endl; switch (bt) { case lig_build::bond_t::SINGLE_BOND: case lig_build::bond_t::SINGLE_OR_DOUBLE: case lig_build::bond_t::SINGLE_OR_AROMATIC: case lig_build::bond_t::DELOC_ONE_AND_A_HALF: case lig_build::bond_t::BOND_ANY: { // std::cout << "draw_bond single" << std::endl; // push down the decision making for the shortening bool at_1_is_singleton = false; bool at_2_is_singleton = false; if (other_connections_to_first_atom.size() == 0) at_1_is_singleton = true; if (other_connections_to_second_atom.size() == 0) at_2_is_singleton = true; std::pair bp = coords_for_single_bond(at_1, at_2, at_1_is_singleton, at_2_is_singleton); pos_1 = bp.first; pos_2 = bp.second; std::string bond_string = make_bond_line_string(pos_1, pos_2, scale, bond_colour); s += bond_string; } break; case lig_build::bond_t::DOUBLE_BOND: case lig_build::bond_t::DOUBLE_OR_AROMATIC: { if (have_centre_pos()) { std::string bond_colour_db = bond_colour; s += draw_double_in_ring_bond(pos_1_in, pos_2_in, bond_colour_db, shorten_first, shorten_second, centre, scale); } else { std::string bond_colour_db = bond_colour; s += draw_double_bond(at_1, at_2, bond_colour_db, shorten_first, shorten_second, other_connections_to_first_atom, other_connections_to_second_atom, centre, scale); } } break; // other bond types case lig_build::bond_t::AROMATIC_BOND: if (have_centre_pos()) { // std::cout << "draw aromatic bond " << std::endl; bool dashed_inner = true; s += draw_double_in_ring_bond(pos_1_in, pos_2_in, bond_colour, shorten_first, shorten_second, centre, scale, dashed_inner); } 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.15/scale; 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; lig_build::pos_t sc_p1 = p1; lig_build::pos_t sc_p2 = p2; lig_build::pos_t sc_p3 = p3; lig_build::pos_t sc_p4 = p4; lig_build::pos_t sc_p5 = p5; lig_build::pos_t sc_p6 = p6; std::string bond_string; bond_string = make_bond_line_string(sc_p1, sc_p2, scale, bond_colour); s += bond_string; bond_string = make_bond_line_string(sc_p3, sc_p4, scale, bond_colour); s += bond_string; bond_string = make_bond_line_string(sc_p5, sc_p6, scale, bond_colour); s += bond_string; } break; case IN_BOND: { // set of lines std::vector > vp = lig_build::pos_t::make_wedge_in_bond(pos_1, pos_2); if (vp.size()) { std::string ss = "\n"; s += ss; for (unsigned int i=0; i 0) { // don't make sheared or darted wedge bonds to atoms that are not Carbon // don't make sheared or darted wedge bond to a C that is tetrahedral bool draw_dart_or_wedge = false; if (at_2.element == "C") if (other_connections_to_second_atom.size() <= 2) draw_dart_or_wedge = true; if (draw_dart_or_wedge) { std::string ss = "\n"; std::string bond_string = draw_sheared_or_darted_wedge_bond(pos_1, pos_2, bond_colour, other_connections_to_second_atom, centre, scale); s += bond_string; done_darted = true; } } if (! done_darted) { // filled shape (normal wedge) std::vector v = lig_build::pos_t::make_wedge_out_bond(pos_1, pos_2); if (!v.empty()) { std::string bond_string = " \n"; s += bond_string; } } } break; case BOND_UNDEFINED: break; } return s; } std::string svg_bond_t::draw_sheared_or_darted_wedge_bond(const lig_build::pos_t &pos_1, const lig_build::pos_t &pos_2, const std::string &bond_colour, const std::vector > &other_connections_to_second_atom, const lig_build::pos_t ¢re, double scale) const { std::string s; s += "\n"; std::vector v = coords_for_sheared_or_darted_wedge_bond(pos_1, pos_2, other_connections_to_second_atom); // if there is a triple bond connected to the second atom, we want to draw an ordinary // bond between the atom postions as well as the wedge (because the wedge is shortened // for aesthetic reasons). // if (other_connections_to_second_atom.size() == 1) { const lig_build::pos_t &third_atom_pos = other_connections_to_second_atom[0].first.atom_position; const lig_build::bond_t &third_bond = other_connections_to_second_atom[0].second; if (third_bond.get_bond_type() == lig_build::bond_t::TRIPLE_BOND) { lig_build::pos_t p1 = svg_molecule_t::mol_coords_to_svg_coords(pos_1, centre, scale); lig_build::pos_t p2 = svg_molecule_t::mol_coords_to_svg_coords(pos_2, centre, scale); // cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); // cairo_move_to(cr, p1.x, p1.y); // cairo_line_to(cr, p2.x, p2.y); // cairo_stroke(cr); s += make_bond_line_string(p1, p2, scale, bond_colour); } } // std::cout << "in draw_sheared_or_darted_wedge_bond() v.size " << v.size() << std::endl; // // darts have 5 points, sheared bonds have 4 points. We don't want to stroke the // // darts (I think). // // // if (v.size() == 4) { // // stroked shape // // // lig_build::pos_t p = svg_molecule_t::mol_coords_to_svg_coords(v[0], centre, scale); // cairo_move_to(cr, p.x, p.y); // for (unsigned int i=1; i= 4) { s += " \n"; } return s; } // optional arg void svg_atom_t::set_colour(bool against_a_dark_background) { // the constructor always passes against_a_dark_background as false colour = "grey"; if (element == "C") colour = "#202020"; if (element == "O") colour = "red"; if (element == "N") colour = "blue"; if (element == "S") colour = "#bbbb00"; if (element == "F") colour = "green"; if (element == "Cl") colour = "green"; if (element == "Br") colour = "brown"; if (element == "I") colour = "purple"; if (element == "P") colour = "orange"; if (element == "Fe") colour = "brown"; if (element == "H") colour = "slategrey"; if (against_a_dark_background) { if (element == "C") colour = "#cccccc"; if (element == "N") colour = "#7070ff"; } } std::string svg_atom_t::make_text_item(const lig_build::atom_id_info_t &atom_id_info, const lig_build::pos_t ¢re, double scale, double median_bond_length) const { double sf = 1.0; std::string s; for (unsigned int i=0; i 0) { // cairo_text_extents_t te; // // we need to "centre" the first letter of the text, ie, the N, not the NH. // std::string t0(txt.substr(0,1)); // cairo_text_extents(cr, t0.c_str(), &te); // cairo_move_to(cr, p.x, p.y); // cairo_rel_move_to(cr, // - te.x_bearing - te.width / 2, // - te.y_bearing - te.height / 2); // if (false) // std::cout << "show text \"" << txt << "\" at " << p << " for t0 " // << t0 << " with move-rel " // << te.x_bearing << " - " << te.width << "/2 " // << te.y_bearing << " - " << te.height << "/2 " // << std::endl; // cairo_show_text(cr, txt.c_str()); // cairo_stroke(cr); // 20230215-PE updated // 20241117-PE updated again // 20250121-PE updated again double x_fudge = 0.0; double y_fudge = 0.20; // x_fudge += 0.45 * static_cast(i); // these were adjusted by eye, previously fs was just 0.8; double fs = 0.05; // 0.07 is too big // 20250121-PE was 0.04; // if (fs < 0.7) fs = 0.7; std::string fss = std::to_string(fs); std::string default_font_size = "\"" + fss + "em\""; std::string font_size = default_font_size; if (atom_id_info.offsets[i].superscript) font_size = "\"0.035em\""; if (atom_id_info.offsets[i].subscript) font_size = "\"0.035em\""; if (atom_id_info.offsets[i].subscript) x_fudge += 0.004; //needs checking if (txt == "-") font_size = "\"0.04em\""; if (txt == "-") x_fudge += 0.05; std::string atom_string; atom_string += " "; atom_string += txt; atom_string += "\n"; // std::cout << "atom_string: " << atom_string; s += atom_string; } else { std::cout << "oops empty text!" << std::endl; } } } return s; } std::string svg_molecule_t::render_to_svg_string(double sf, bool dark_background_flag, bool add_background_rect) { // 20241002-PE note sf was 400.0; svg_container_t s = make_svg(sf, dark_background_flag, add_background_rect); return s.compose(add_background_rect); } svg_container_t svg_molecule_t::make_svg(double sf, bool dark_background_flag, bool add_background_rect) { auto make_bond_comment = [] (unsigned int bond_idx, const svg_bond_t &bond) { std::string s("\n"); return s; }; sf = 1.0; std::cout << "debug:: in svg_molecule_t::make_svg() scale_factor is " << sf << std::endl; svg_container_t svg; std::string svg_header_1 = " max_x) max_x = pos.x; if (pos.y > max_y) max_y = pos.y; } // now adjust so that the labels can fit // a sugar is about 200x200 min_x -= 2.0; min_y -= 2.0; max_x += 2.0; max_y += 2.0; svg.set_bounds(min_x, min_y, max_x, max_y); // viewBox is min_x, min_y, width, height float width = max_x - min_x; float height = max_y - min_y; viewBox_string = "viewBox=" + std::string("\"") + std::to_string(min_x) + std::string(" ") + std::to_string(min_y) + std::string(" ") + std::to_string(width) + std::string(" ") + std::to_string(height) + std::string("\""); } // s += svg_header_1; // s += viewBox_string; // s += svg_header_2; // std::cout << "viewBox: " << viewBox_string << std::endl; // just testing that I can see something. No longer needed because I can // s += " \n"; // double scale = get_scale(); double scale = sf; scale = 5.0; lig_build::pos_t centre = get_ligand_centre(); // ---------------------- Bonds ------------------------------------- for (unsigned int ib=0; ib shorten = shorten_flags(ib); bool at_1_in_ring_flag = in_ring_p(idx_1); bool at_2_in_ring_flag = in_ring_p(idx_2); std::vector > other_connections_to_first_atom = make_other_connections_to_first_atom_info(ib); std::vector > other_connections_to_second_atom = make_other_connections_to_second_atom_info(ib); std::string bond_string = bonds[ib].draw_bond(atoms[idx_1], atoms[idx_2], at_1_in_ring_flag, at_2_in_ring_flag, bt, bond_colour, shorten.first, shorten.second, other_connections_to_first_atom, other_connections_to_second_atom, centre, scale); std::string bc = make_bond_comment(ib, bonds[ib]); // std::cout << "debug:: in svg_molecule_t::make_svg() bond_string is " << bond_string << std::endl; // I don't care about s now svg.add(bc); svg.add(bond_string); } } // ---------------------- Atoms ------------------------------------- double scale_for_atoms = 1.0; if (!atoms.empty()) svg.add(std::string("\n")); for (unsigned int iat=0; iat local_bonds = bonds_having_atom_with_atom_index(iat); bool gl_flag = false; if (ele != "C") { lig_build::atom_id_info_t atom_id_info = make_atom_id_by_using_bonds(iat, ele, local_bonds, gl_flag); atoms[iat].set_atom_id(atom_id_info.atom_id); // quick hack if (true) std::cout << "in render(): atom_index " << iat << " pos " << atoms[iat].atom_position << " with charge " << atoms[iat].charge << " made atom_id_info " << atom_id_info << std::endl; std::cout << "atom block: passing scale_for_atoms " << scale_for_atoms << std::endl; std::string a = atoms[iat].make_text_item(atom_id_info, centre, scale_for_atoms, median_bond_length_); svg.add(a); } else { // a super-atom carbon if (local_bonds.size() == 1) { // atoms[iat].set_colour(cr); // lig_build::atom_id_info_t atom_id_info = make_atom_id_by_using_bonds(iat, ele, local_bonds, gl_flag); // atoms[iat].make_text_item(cr, atom_id_info, centre, scale, median_bond_length_); lig_build::atom_id_info_t atom_id_info = make_atom_id_by_using_bonds(iat, "C", local_bonds, gl_flag); atoms[iat].set_atom_id(atom_id_info.atom_id); // quick hack std::string a = atoms[iat].make_text_item(atom_id_info, centre, scale_for_atoms, median_bond_length_); svg.add(a); } } } return svg; } double svg_molecule_t::get_scale() const { std::pair ext = ligand_extents(); double delta_x = ext.second.x - ext.first.x; double delta_y = ext.second.y - ext.first.y; double delta = (delta_x > delta_y) ? delta_x : delta_y; // The scale transforms from molecule coordinates to the 0->1 square // (centering is also applied of course) // What makes this tricky is that atom labels (e.g. "NH2") can go beyond // the limits of the atoms. So we need to make space for them by making // the molecules a bit smaller that exact fit to the box. How much smaller? // Not clear. A bit less than 0.75? BHH falls off the edge at 0.75 (with // scale 0.09). // So let's make the scale limit a bit smaller (was 0.09, now 0.089). // Also, we don't want massive "zoomed in" representation of tiny molecules // (e.g. ALM). double scale_lim = 0.089; // heuristic double scale = scale_lim; // we could be more clever here by looking at the atoms on the edge of the molecle // if they are non-carbon, reduce the scale a bit. 0.75 is a safe value that // allows non-C edge atom representation. if (delta > 1) scale = 0.74/delta; // was 0.75 (0.75 cuts atom labels on occassion e.g. 1386433) if (scale > scale_lim) scale = scale_lim; // std::cout << "get_scale() returns " << scale << std::endl; return scale; } #endif // MAKE_ENHANCED_LIGAND_TOOLS