#! /usr/bin/python3 -t PROG_DESC = "Outputs an SVG 'roadkill' diagram for said molecule" import math; from math import sqrt,sin,cos,pi import rn import sys, os, re import mformula import mformula_oxocarbon import mformula_svg def image_name(style): fname = __file__ imgname = re.sub(r'.*[/]', "", fname[0:-3]) + \ "_style" + style return imgname def image_descr(style): fname = __file__ imgname = re.sub(r'.*[/]', "", fname[0:-3]) descr = "Structural formula of " + re.sub(r'[_]', " ", imgname) descr += "\n" + \ "\n" + \ mformula_svg.style_descr(style) return descr def build_formula(svg): ft = build_formula_third(svg) # The left third of the molecule. fm = mformula.obj() k0 = fm.add_subformula(ft, 0, 30, [0,0]) k1 = fm.add_subformula(ft, 0, 150, [0,0]) k2 = fm.add_subformula(ft, 0, 270, [0,0]) fm.add_bond(k0+3,k1+0,1.5) fm.add_bond(k1+3,k2+0,1.5) fm.add_bond(k2+3,k0+0,1.5) return fm def image_name(style): fname = __file__ imgname = re.sub(r'.*[/]', "", fname[0:-3]) + \ "_style" + style return imgname def image_descr(style): fname = __file__ imgname = re.sub(r'.*[/]', "", fname[0:-3]) descr = "Structural formula of " + re.sub(r'[_]', " ", imgname) descr += "\n" + \ "\n" + \ mformula_svg.style_descr(style) return descr def build_formula_third(svg) : "Builds the left 1/3 of the molecule, assumed centered at [0,0]." ft = mformula.obj() cb = svg.rel_bond_length(1.5) # Length of aromatic bonds. db = svg.rel_bond_length(2.0) # Relative length of double bonds. # Assume that the benzene ring stays hexagonal: R = cb # Radius of benzene ring. c30 = cos(30*pi/180) s30 = sin(30*pi/180) # Compute bending of the benzene-butylene bonds so that single bonds have length 1: a0 = math.asin(0.50 - R*s30) # Lift angle of radial C-C bonds. sys.stderr.write("a0 = %.2f\n" % (a0*180/pi)) ca0 = cos(a0) sa0 = sin(a0) # Choose direction of C=O in carbonyls to bisect the corner andle: a2 = (a0 + pi/2.0)/2.0 sys.stderr.write("a2 = %.2f\n" % (a2*180/pi)) ca2 = cos(a2) sa2 = sin(a2) p0 = rn.scale(R, [-c30, +s30]) # Upper left carbon of benzene. p1 = rn.add(p0, [-ca0, +sa0]) # Upper left carbon of butylene. p2 = rn.add(p1, rn.scale(db, [-ca2, +sa2])) # Upper left oxygen of carboxyl. p3 = rn.scale(R, [-c30, -s30]) # Lower left carbon of benzene. p4 = rn.add(p3, [-ca0, -sa0]) # Lower left carbon of butylene. p5 = rn.add(p4, rn.scale(db, [-ca2, -sa2])) # Lower left oxygen of carboxyl. sys.stderr.write("p0--p1 = %7.5f\n" % rn.dist(p0,p1)) sys.stderr.write("p1--p1' = %7.5f\n" % (2*p1[1])) k0 = ft.add_atom("C", p0, 0,0) k1 = ft.add_atom("C", p1, 0,0) k2 = ft.add_atom("O", p2, 0,0) k3 = ft.add_atom("C", p3, 0,0) k4 = ft.add_atom("C", p4, 0,0) k5 = ft.add_atom("O", p5, 0,0) ft.add_bond(k0,k3,1.5) ft.add_bond(k0,k1,1.0) ft.add_bond(k1,k2,2.0) ft.add_bond(k3,k4,1.0) ft.add_bond(k4,k5,2.0) ft.add_bond(k1,k4,1.0) return ft