def place_molecules(self): last_anchor_x = 80 last_anchor_y = 200 # data for rescaling if self._reactions: for react in self._reactions: reactants, products = react # these are ids plus_objs = [] for part in (reactants, products): for id in part: m = self._mol_ids[id] last_anchor_x = self._scale_and_move_molecule( m, last_anchor_x, last_anchor_y) last_anchor_x += self._xshift if id != part[-1]: # we add some pluses p = plus(self.paper, xy=(last_anchor_x, last_anchor_y)) plus_objs.append(p) self.molecules.append(p) bbox = p.bbox() dx = (bbox[0] - bbox[2]) p.move(dx / 2, 0) last_anchor_x += self._xshift + dx if part == reactants: arr = arrow(self.paper) # first point arr.create_new_point(last_anchor_x, last_anchor_y) last_anchor_x += self.paper.any_to_px( self.paper.standard.arrow_length) # second point arr.create_new_point(last_anchor_x, last_anchor_y) last_anchor_x += self._xshift # adding the reaction information into the arrow.reaction arr.reaction.reactants.extend( [self._mol_ids[m] for m in reactants]) arr.reaction.products.extend( [self._mol_ids[m] for m in products]) arr.pluses = plus_objs self.molecules.append(arr) else: for m in self.molecules: last_anchor_x = self._scale_and_move_molecule( m, last_anchor_x, last_anchor_y) last_anchor_x += self._xshift
def place_molecules(self): last_anchor_x = 80 last_anchor_y = 200 # data for rescaling if self._reactions: for react in self._reactions: reactants, products = react # these are ids plus_objs = [] for part in (reactants, products): for id in part: m = self._mol_ids[id] last_anchor_x = self._scale_and_move_molecule(m, last_anchor_x, last_anchor_y) last_anchor_x += self._xshift if id != part[-1]: # we add some pluses p = plus(self.paper, xy=(last_anchor_x, last_anchor_y)) plus_objs.append(p) self.molecules.append(p) bbox = p.bbox() dx = bbox[0] - bbox[2] p.move(dx / 2, 0) last_anchor_x += self._xshift + dx if part == reactants: arr = arrow(self.paper) # first point arr.create_new_point(last_anchor_x, last_anchor_y) last_anchor_x += self.paper.any_to_px(self.paper.standard.arrow_length) # second point arr.create_new_point(last_anchor_x, last_anchor_y) last_anchor_x += self._xshift # adding the reaction information into the arrow.reaction arr.reaction.reactants.extend([self._mol_ids[m] for m in reactants]) arr.reaction.products.extend([self._mol_ids[m] for m in products]) arr.pluses = plus_objs self.molecules.append(arr) else: for m in self.molecules: last_anchor_x = self._scale_and_move_molecule(m, last_anchor_x, last_anchor_y) last_anchor_x += self._xshift
def get_molecules( self, file_name): doc = dom.parse( file_name) molecules = [] # read colors colors=[] for elem7 in doc.getElementsByTagName("color"): red=(float(elem7.getAttribute("r"))*255) green=(float(elem7.getAttribute("g"))*255) blue=(float(elem7.getAttribute("b"))*255) colors.append("#%02x%02x%02x" % (red,green,blue)) # read fonts fonts={} for elem8 in doc.getElementsByTagName("font"): family=str(elem8.getAttribute("name")) fonts[int(elem8.getAttribute("id"))]=family # read molecules for elem1 in doc.getElementsByTagName("fragment"): if elem1.parentNode.nodeName=="page": mol = molecule( paper=self.paper) atom_id_to_atom = {} atom_id_to_text = {} for elem2 in elem1.childNodes: # atom if elem2.nodeName=="n": font = "" Size = 12 text = "C" color1="#000000" for elem3 in elem2.childNodes: if elem3.nodeName=="t": if elem3.hasAttribute("color"): color1=colors[int(elem3.getAttribute("color"))-2] text = "" for elem4 in elem3.childNodes: if elem4.nodeName=="s": if elem3.hasAttribute("color"): color1=colors[int(elem3.getAttribute("color"))-2] for Id, Font in fonts.items(): if Id==int(elem4.getAttribute("font")): font=Font Size= int(elem4.getAttribute("size")) text += dom_ext.getAllTextFromElement( elem4).strip() position = elem2.getAttribute("p").split() assert len( position) == 2 # we must postpone symbol assignment until we know the valency of the atoms atom_id_to_text[ elem2.getAttribute('id')] = text atom = mol.create_vertex() atom.line_color = color1 atom.font_family = font atom.font_size = Size atom.x = float( position[0]) atom.y = float( position[1]) mol.add_vertex( atom) atom_id_to_atom[ elem2.getAttribute('id')] = atom # bond #{"v BKChemu bond.type":"v ChemDraw hodnota atributu Display elementu b"} bondType2={"WedgeBegin":"w", "WedgedHashBegin":"h", "Wavy":"a", "Bold":"b", "Dash":"d" } if elem2.nodeName=="b": if elem2.hasAttribute("color"): color2 = colors[(int(elem2.getAttribute("color"))-2)] else: color2="#000000" order = 1 if elem2.hasAttribute("Order"): order = int( elem2.getAttribute("Order")) bond = mol.create_edge() if elem2.hasAttribute("Display"): display = elem2.getAttribute("Display").strip() for bondC, bondB in bondType2.items(): if bondC ==display: bond.type = bondB bond.line_color = color2 bond.order = order atom1 = atom_id_to_atom[ elem2.getAttribute("B")] atom2 = atom_id_to_atom[ elem2.getAttribute("E")] mol.add_edge( atom1, atom2, bond) # here we reassign the symbols for id, atom in atom_id_to_atom.items(): text = atom_id_to_text[ id] v = mol.create_vertex_according_to_text( atom, text) atom.copy_settings( v) mol.replace_vertices( atom, v) atom.delete() # finally we add the molecule to the list of molecules for output molecules.append( mol) # read texts textik={2:"i", 1:"b", 32:"sub", 64:"sup"} for elem5 in doc.getElementsByTagName("t"): if elem5.parentNode.nodeName=="page": position = map( float, elem5.getAttribute("p").split()) assert len( position) == 2 celyText="" for elem51 in elem5.childNodes: if elem51.nodeName=="s": for elem52 in elem51.childNodes: if isinstance( elem52, dom.Text): rodice=[] text100=elem52.data if elem51.hasAttribute("face"): Face01=int(elem51.getAttribute("face")) for face, parent in textik.items(): for i in range(9): if not Face01&2**i==0: if face==Face01&2**i: rodice.append(parent) for rodic in rodice: text100 = "<%s>%s</%s>" % (rodic,text100,rodic) celyText += text100 if elem5.hasAttribute("color"): color3=colors[(int(elem5.getAttribute("color"))-2)] else: color3="#000000" font_id = elem51.getAttribute("font") if font_id != "": font=fonts[int(font_id)] #text = dom_ext.getAllTextFromElement(elem51) #print celyText text = celyText t = text_class( self.paper, position, text=text) t.line_color = color3 #print elem51 if elem51.hasAttribute("size"): t.font_size = int( elem51.getAttribute("size")) if font: t.font_family = font molecules.append(t) # read graphics - plus for elem6 in doc.getElementsByTagName("graphic"): if elem6.getAttribute("GraphicType")=="Symbol" and elem6.getAttribute("SymbolType")=="Plus": position = map( float, elem6.getAttribute("BoundingBox").split()) position2=[position[0],position[1]] assert len(position2) == 2 if elem6.hasAttribute("color"): color4=colors[(int(elem6.getAttribute("color"))-2)] else: color4="#000000" pl = plus(self.paper, position2) pl.line_color = color4 molecules.append(pl) sipka=[] #for elem71 in doc.getElementsByTagName("graphic"): #if elem71.getAttribute("GraphicType")=="Line": for elem7 in doc.getElementsByTagName("arrow"): sipka.insert(0,elem7.getAttribute('Head3D') ) sipka.insert(1,elem7.getAttribute('Tail3D') ) if elem7.hasAttribute("color"): sipka.insert(0,colors[(int(elem7.getAttribute("color"))-2)]) point1 = map( float, sipka[1].split()) point2 = map( float, sipka[2].split()) arr = arrow( self.paper, points=[point2[0:2],point1[0:2]], fill=sipka[0]) arr.line_color=sipka[0] molecules.append( arr) sipka=[] return molecules
def get_molecules(self, file_name): doc = dom.parse(file_name) molecules = [] # read colors colors = [] for elem7 in doc.getElementsByTagName("color"): red = (float(elem7.getAttribute("r")) * 255) green = (float(elem7.getAttribute("g")) * 255) blue = (float(elem7.getAttribute("b")) * 255) colors.append("#%02x%02x%02x" % (red, green, blue)) # read fonts fonts = {} for elem8 in doc.getElementsByTagName("font"): family = str(elem8.getAttribute("name")) fonts[int(elem8.getAttribute("id"))] = family # read molecules for elem1 in doc.getElementsByTagName("fragment"): if elem1.parentNode.nodeName == "page": mol = molecule(paper=self.paper) atom_id_to_atom = {} atom_id_to_text = {} for elem2 in elem1.childNodes: # atom if elem2.nodeName == "n": font = "" Size = 12 text = "C" color1 = "#000000" for elem3 in elem2.childNodes: if elem3.nodeName == "t": if elem3.hasAttribute("color"): color1 = colors[ int(elem3.getAttribute("color")) - 2] text = "" for elem4 in elem3.childNodes: if elem4.nodeName == "s": if elem3.hasAttribute("color"): color1 = colors[int( elem3.getAttribute("color")) - 2] for Id, Font in fonts.items(): if Id == int( elem4.getAttribute( "font")): font = Font Size = int(elem4.getAttribute("size")) text += dom_ext.getAllTextFromElement( elem4).strip() position = elem2.getAttribute("p").split() assert len(position) == 2 # we must postpone symbol assignment until we know the valency of the atoms atom_id_to_text[elem2.getAttribute('id')] = text atom = mol.create_vertex() atom.line_color = color1 atom.font_family = font atom.font_size = Size atom.x = float(position[0]) atom.y = float(position[1]) mol.add_vertex(atom) atom_id_to_atom[elem2.getAttribute('id')] = atom # bond #{"v BKChemu bond.type":"v ChemDraw hodnota atributu Display elementu b"} bondType2 = { "WedgeBegin": "w", "WedgedHashBegin": "h", "Wavy": "a", "Bold": "b", "Dash": "d" } if elem2.nodeName == "b": if elem2.hasAttribute("color"): color2 = colors[(int(elem2.getAttribute("color")) - 2)] else: color2 = "#000000" order = 1 if elem2.hasAttribute("Order"): order = int(elem2.getAttribute("Order")) bond = mol.create_edge() if elem2.hasAttribute("Display"): display = elem2.getAttribute("Display").strip() for bondC, bondB in bondType2.items(): if bondC == display: bond.type = bondB bond.line_color = color2 bond.order = order atom1 = atom_id_to_atom[elem2.getAttribute("B")] atom2 = atom_id_to_atom[elem2.getAttribute("E")] mol.add_edge(atom1, atom2, bond) # here we reassign the symbols for id, atom in atom_id_to_atom.items(): text = atom_id_to_text[id] v = mol.create_vertex_according_to_text(atom, text) atom.copy_settings(v) mol.replace_vertices(atom, v) atom.delete() # finally we add the molecule to the list of molecules for output molecules.append(mol) # read texts textik = {2: "i", 1: "b", 32: "sub", 64: "sup"} for elem5 in doc.getElementsByTagName("t"): if elem5.parentNode.nodeName == "page": position = map(float, elem5.getAttribute("p").split()) assert len(position) == 2 celyText = "" for elem51 in elem5.childNodes: if elem51.nodeName == "s": for elem52 in elem51.childNodes: if isinstance(elem52, dom.Text): rodice = [] text100 = elem52.data if elem51.hasAttribute("face"): Face01 = int(elem51.getAttribute("face")) for face, parent in textik.items(): for i in range(9): if not Face01 & 2**i == 0: if face == Face01 & 2**i: rodice.append(parent) for rodic in rodice: text100 = "<%s>%s</%s>" % (rodic, text100, rodic) celyText += text100 if elem5.hasAttribute("color"): color3 = colors[(int(elem5.getAttribute("color")) - 2)] else: color3 = "#000000" font_id = elem51.getAttribute("font") if font_id != "": font = fonts[int(font_id)] #text = dom_ext.getAllTextFromElement(elem51) #print celyText text = celyText t = text_class(self.paper, position, text=text) t.line_color = color3 #print elem51 if elem51.hasAttribute("size"): t.font_size = int(elem51.getAttribute("size")) if font: t.font_family = font molecules.append(t) # read graphics - plus for elem6 in doc.getElementsByTagName("graphic"): if elem6.getAttribute( "GraphicType") == "Symbol" and elem6.getAttribute( "SymbolType") == "Plus": position = map(float, elem6.getAttribute("BoundingBox").split()) position2 = [position[0], position[1]] assert len(position2) == 2 if elem6.hasAttribute("color"): color4 = colors[(int(elem6.getAttribute("color")) - 2)] else: color4 = "#000000" pl = plus(self.paper, position2) pl.line_color = color4 molecules.append(pl) sipka = [] #for elem71 in doc.getElementsByTagName("graphic"): #if elem71.getAttribute("GraphicType")=="Line": for elem7 in doc.getElementsByTagName("arrow"): sipka.insert(0, elem7.getAttribute('Head3D')) sipka.insert(1, elem7.getAttribute('Tail3D')) if elem7.hasAttribute("color"): sipka.insert(0, colors[(int(elem7.getAttribute("color")) - 2)]) point1 = map(float, sipka[1].split()) point2 = map(float, sipka[2].split()) arr = arrow(self.paper, points=[point2[0:2], point1[0:2]], fill=sipka[0]) arr.line_color = sipka[0] molecules.append(arr) sipka = [] return molecules