mojaie / MolecularGraph.jl

Graph-based molecule modeling toolkit for cheminformatics
MIT License
189 stars 27 forks source link

C visible, transparent atom labels #70

Open SimonEnsemble opened 2 years ago

SimonEnsemble commented 2 years ago

one thing I couldn't figure out: the atom indices are not centered with the circles behind them. there is an offset. how can I fix this? https://github.com/mojaie/MolecularGraph.jl/blob/master/src/draw/svg.jl#L334-L335 some change here?

here is my test for this:

push!(LOAD_PATH, "src/")

import Pkg
Pkg.activate(".")
Pkg.instantiate()
using MolecularGraph
DRAW_SETTING[:C_visible] = true

s = "CC(C)(C)N(NC(=O)C1=CC=C(Cl)C=C1)C(=O)C1=CC=CC=C1"
mol = smilestomol(s)

function viz_molecule(mol)
    canvas = SvgCanvas()
    draw2d!(canvas, mol)
    drawatomindex!(canvas, mol,
        bgcolor=MolecularGraph.Color(255, 200, 100))#, opacity=0.5)
    svg_string = tosvg(canvas, 300, 300)
    return svg_string
end
svg_string = viz_molecule(mol)
savesvg(svg_string, "a.svg")
run(`xdg-open a.svg`)
codecov-commenter commented 2 years ago

Codecov Report

Merging #70 (12679ba) into master (00c880e) will decrease coverage by 0.08%. The diff coverage is 10.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #70      +/-   ##
==========================================
- Coverage   76.14%   76.05%   -0.09%     
==========================================
  Files          56       56              
  Lines        4963     4970       +7     
==========================================
+ Hits         3779     3780       +1     
- Misses       1184     1190       +6     
Impacted Files Coverage Δ
src/draw/svg.jl 73.43% <0.00%> (-2.07%) :arrow_down:
src/draw/draw2d.jl 75.32% <50.00%> (ø)
src/draw/draw3d.jl 97.82% <0.00%> (+2.17%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 00c880e...12679ba. Read the comment docs.

mojaie commented 2 years ago

Thank you very much. That looks great! I'm not sure about the way to fit the anchor and the size of text and rect in SVG. This positioning system may depend on the font size and the number of characters and seems to be complicated... I'd be happy if you post it as a new issue.

mojaie commented 2 years ago

Or, bxy = svgcoords(pos - (size / 2, size / 2)) may work like setatomhighlight! below.

https://github.com/mojaie/MolecularGraph.jl/blob/12679baa5c15f740d3cf55323a5fbda1f5a78f10/src/draw/svg.jl#L345

The anchor seems to be OK. The centering should be,

  1. detect text width in px (it depends on SVG viewer setting, so we may need some JavaScript code to detect that.)
  2. adjust x value of text tag ((rect size - text width) / 2).
mojaie commented 2 years ago

transform="translate(xpos, ypos)" in g tag and text-anchor="middle" in text would be better.

From

<g>
 <rect x="186.73" y="152.98" width="10" height="10" rx="5.0" ry="5.0" fill="rgb(240, 240, 255)"></rect>
 <text x="186.73" y="162.98" font-size="10" fill="rgb(0, 0, 0)">5</text>
</g>

To

<g width="10" height="10" transform="translate(186.73, 152.98)">
 <rect width="10" height="10" rx="5.0" ry="5.0" fill="rgb(240, 240, 255)"></rect>
 <text x="5" y="10" font-size="10" fill="rgb(0, 0, 0)" text-anchor="middle">5</text>
</g>

text.x value is a half of g.width

g.width is still environment dependent. (font-size / 2 * word count) would be a good guess.

SimonEnsemble commented 2 years ago

based on here, felt like this would work, but it doesn't. :(

image

    elem = """<g transform="translate($(pos.x), $(pos.y))">
     <rect x="0" y="0" width="$(size)" height="$(size)" rx="$(size/2)" ry="$(size/2)" fill="$(bc)" opacity="$(opacity)" />
     <text x="$(size/2)" y="$(size/2)" font-size="$(size)" fill="$(c)" text-anchor="middle" alignment-baseline="middle">$(text) </text>
    </g>
    """
mojaie commented 2 years ago

The attribute name would be fill-opacity https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-opacity and text.y may be y="$(size)".