#! /usr/bin/python -t # _*_ coding: iso-8859-1 _*_ # Last edited on 2013-02-20 01:06:02 by stolfilocal PROG_NAME = __file__[len(__file__)-3] PROG_DESC = "Outputs an SVG 'roadkill' diagram for said molecule" PROG_VERS = "1.0" PROG_COPYRIGHT = "Copyright © 2009-07-12 by the State University of Campinas (UNICAMP)" import sys import os import re import math; from math import sqrt,sin,cos,pi sys.path[1:0] = [ '.', '..', sys.path[0] + '/../lib', os.path.expandvars('${STOLFIHOME}/lib') ] # sys.stderr.write(re.sub('[,]', ',\n', "%s: path = %r\n" % (PROG_NAME, sys.path))); import rn import mformula; from mformula import MFormula import mformula_oxocarbon import mformula_svg; from mformula_svg import MFormula_SVG import mformula_args; import argparser PROG_HELP = \ PROG_NAME + " \\\n" \ + argparser.help_info_HELP + " \\\n" \ " > {FIGURE}.svg" PROG_INFO = \ "NAME\n" \ " " + PROG_NAME + " - " + PROG_DESC + ".\n" \ "\n" \ "DESCRIPTION\n" \ " " + PROG_HELP + ".\n" \ "\n" \ "DOCUMENTATION OPTIONS\n" \ + argparser.help_info_INFO + "\n" \ "\n" \ "AUTHOR\n" \ " Created 2009-07-12 by Jorge Stolfi, IC-UNICAMP.\n" \ "\n" \ "MODIFICATION HISTORY\n" \ " 2009-07-12 by J. Stolfi, IC-UNICAMP: created.\n" \ "\n" \ "WARRANTY\n" \ " " + argparser.help_info_NO_WARRANTY + "\n" \ "\n" \ "RIGHTS\n" \ " " + PROG_COPYRIGHT + ".\n" \ "\n" \ " " + argparser.help_info_STANDARD_RIGHTS style = mformula_args.parse(None); def build_formula() : ft = build_formula_third(); # The left third of the molecule. fm = MFormula(style); 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 build_formula_third() : "Builds the left 1/3 of the molecule, assumed centered at [0,0]." ft = MFormula(style); cb = ft.bond_length(1.5); # Length of aromatic bonds. db = ft.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; fm = build_formula(); svg = MFormula_SVG(fm,0); svg.standard_elem_style_1(); svg.output_figure();