#! /usr/bin/python -t # _*_ coding: iso-8859-1 _*_ # Last edited on 2009-07-13 23:11:43 by stolfi # Last edited on 2009-07-13 22:16:35 by stolfi def build_formula_carbon_suboxide_polymer(pat) : "Builds a formula for a linear carbon suboxide polymer described by {pat}.\n" \ "\n" \ " Pat is the alist of strings. Each string describes a C3O2 unit" \ " of the polymer, from left to right. Each unit can be:\n" \ "\n" \ " 'L{D}' = left terminator. \n" \ " '{N}{X}H{D}{Y}' = {N} copies of the hemimer. \n" \ " 'R{D}' = right terminator. \n" \ " '{N}g' = a gap of {N} half-bonds. \n" \ "\n" \ " The modifier {D} for a terminator 'L' or 'R' is either 'u' (ketene" \ " at top) or 'd' (ketene at bottom); for a hemimer sequence it applies" \ " to the first hemimer of the sequence, also 'u' (carbonyl on top) or 'd' (carbonyl at bottom). \n" \ "\n" \ " The modifiers {X} and {Y} for a hemimer sequence apply to the left" \ " side of the first unit and the right side of the last unit," \ " respectively. They can be either 's' (straight horizontal connections)" \ " or 'b' (connections bent to accept a terminator)." fm = MFormula(); sb = fm.bond_length(1.0); dX = 0.0; # Horizontal displacement for next unit. flip = 0; # Next unit must be flipped/rotated. Tang = carbon_suboxide_polymer_terminator_angle(); for i in range(pat) : item = pat[i]; # Code of next unit or uit sequence. # Extract and delete the repeater {N} if any: N = int(re.findall(r'^(\d+\)', item)); if N == None : N = 0; item = re.sub(r'^(\d+\)', '', item); # Operations: if item == 'g' : # Gap: dX = dX + 0.5*N*sb; elif re.match(r'^[LR]', item) : # Left/right terminator: # Extract the inversion flag {D}: D = re.findall(r'([ud])$', item) if D == None : D = 'u'; item = re.sub(r'([ud])$', '', item); if D == 'u' : Rinv = 0; elif D == 'd' : Rinv = 1; else : assert FALSE; if if (i == len(pat)-1) or (i == fu = build_formula_poly_carbon_suboxide_terminator(".",Rang,Rinv); fu = build_formula_poly_carbon_suboxide_monomer(); sb = fu.bond_length(1.0); # Relative length of single bonds. dx = 3.0*sb; # Assemble the three full units: k0 = fm.add_subformula(fu, 0, 0, [0*dx,0]); k1 = fm.add_subformula(fu, 0, 0, [1*dx,0]); k2 = fm.add_subformula(fu, 0, 0, [2*dx,0]); return fm;