def get_formula_dict( self): """returns formula as dictionary that can be passed to functions in periodic_table""" if self.group_type == "builtin": return PT.formula_dict( GT.groups_table[ self.symbol.lower()]['composition']) elif self.group_graph: form = self.group_graph.get_formula_dict() if 'H' in form: if form['H'] > self.occupied_valency: form['H'] -= self.occupied_valency else: del form['H'] return form else: return PT.formula_dict( self.symbol)
def set_name( self, name, interpret=1, occupied_valency=None): if occupied_valency == None: occupied_valency = self.occupied_valency if occupied_valency == 1 and (name.lower() in GT.groups_table): # name is a known group self.symbol = GT.groups_table[ name.lower()]['name'] self.group_type = "builtin" return True # try interpret the formula lf = oasa.linear_formula.linear_formula( name, start_valency=occupied_valency) if not lf.molecule: # it is possible the text goes the other way lf = oasa.linear_formula.linear_formula( name, end_valency=occupied_valency) if lf.molecule: self.group_graph = lf.molecule if lf.first_atom: self.connecting_atom = lf.first_atom elif lf.last_atom: self.connecting_atom = lf.last_atom else: self.connecting_atom = lf.molecule.vertices[0] self.symbol = name self.group_type = "implicit" self.group_graph.paper = self.paper return True # try chain if re.compile( "^[cC][0-9]*[hH][0-9]*$").match( name): form = PT.formula_dict( name.upper()) if occupied_valency == 1 and form.is_saturated_alkyl_chain(): self.symbol = str( form) self.group_type = "chain" return True return False
def get_formula_dict(self): """returns formula as dictionary that can be passed to functions in periodic_table""" if self.group_type == "builtin": return PT.formula_dict( GT.groups_table[self.symbol.lower()]['composition']) elif self.group_graph: form = self.group_graph.get_formula_dict() if 'H' in form: if form['H'] > self.occupied_valency: form['H'] -= self.occupied_valency else: del form['H'] return form else: return PT.formula_dict(self.symbol)
def get_formula_dict( self): """returns formula as dictionary that can be passed to functions in periodic_table""" ret = PT.formula_dict( self.symbol) if self.free_valency > 0: ret['H'] = self.free_valency return ret
def set_name(self, name, interpret=1, occupied_valency=None): if occupied_valency is None: occupied_valency = self.occupied_valency if occupied_valency == 1 and (name.lower() in GT.groups_table): # name is a known group self.symbol = GT.groups_table[name.lower()]['name'] self.group_type = "builtin" return True # try interpret the formula lf = oasa.linear_formula.linear_formula(name, start_valency=occupied_valency) if not lf.molecule: # it is possible the text goes the other way lf = oasa.linear_formula.linear_formula( name, end_valency=occupied_valency) if lf.molecule: self.group_graph = lf.molecule if lf.first_atom: self.connecting_atom = lf.first_atom elif lf.last_atom: self.connecting_atom = lf.last_atom else: self.connecting_atom = lf.molecule.vertices[0] self.symbol = name self.group_type = "implicit" self.group_graph.paper = self.paper return True # try chain if re.compile("^[cC][0-9]*[hH][0-9]*$").match(name): form = PT.formula_dict(name.upper()) if occupied_valency == 1 and form.is_saturated_alkyl_chain(): self.symbol = str(form) self.group_type = "chain" return True return False
def expand( self): """expands the group and returns list of atoms that new drawing afterwords""" if self.group_type == "builtin": names = Store.gm.get_template_names() if self.symbol in names: a2 = self.neighbors[0] x1, y1 = a2.get_xy() x2, y2 = self.get_xy() self.group_graph = Store.gm.get_transformed_template( names.index( self.symbol), (x1,y1,x2,y2), type='atom1') replacement = self.group_graph.next_to_t_atom else: print "unknown group %s" % a.symbol return None elif self.group_type == "chain": self.group_graph = self.molecule.create_graph() p = PT.formula_dict( self.symbol) n = p['C'] last = None for i in range( n): v = self.group_graph.add_vertex() v.x, v.y = None, None if last: self.group_graph.add_edge( last, v) last = v replacement = self.group_graph.vertices[0] replacement.x = self.x replacement.y = self.y elif self.group_type == "implicit": if not self.group_graph: self.set_name( self.symbol, occupied_valency=self.occupied_valency) for v in self.group_graph.vertices: v.x, v.y = None, None v.show = v.symbol != 'C' assert self.connecting_atom != None replacement = self.connecting_atom replacement.x = self.x replacement.y = self.y self.molecule.eat_molecule( self.group_graph) self.molecule.move_bonds_between_atoms( self, replacement) self.molecule.delete_vertex( self) if self.occupied_valency: oasa.coords_generator.calculate_coords( self.molecule, bond_length=-1) else: # if the group is the only vertex of the molecule we must set the bond_length explicitly # and the move the whole molecule replacement.x = None replacement.y = None x, y = self.x, self.y oasa.coords_generator.calculate_coords( self.molecule, bond_length=Screen.any_to_px( self.paper.standard.bond_length)) dx = x - replacement.x dy = y - replacement.y [a.move( dx, dy) for a in self.group_graph.vertices] return self.group_graph.vertices
from oasa.periodic_table import formula_dict mols, unique = App.paper.selected_to_unique_top_levels() colors = ["#cc0000","#00ff00","#0000ff","#ff00ff","#00ffff","#ff5500"] for mol in mols: if mol.object_type == "molecule": current_selected_bonds = set(mol.bonds) & set(App.paper.selected_bonds) if current_selected_bonds: for b in current_selected_bonds: mol.temporarily_disconnect_edge(b) fragments = list(mol.get_connected_components()) mol.reconnect_temporarily_disconnected_edges() for i, fragment in enumerate(fragments): color = colors[i%len(colors)] frag_bonds = mol.vertex_subgraph_to_edge_subgraph(fragment) - current_selected_bonds for x in fragment | frag_bonds: x.line_color = color x.redraw() formula = sum([atom.get_formula_dict() for atom in fragment], formula_dict()) text = "%s: %.8f" % (formula.get_html_repr_as_string(), formula.get_exact_molecular_mass()) text_obj = App.paper.new_text(0, 0, text) text_obj.line_color = color text_obj.draw() App.paper.place_next_to_bbox("b", "r", 10+20*i, text_obj, mol.bbox())
def get_formula_dict(self): """returns a formula dict as defined in the periodic_table.py::formula_dict""" comp = PT.formula_dict() for a in self.atoms: comp += a.get_formula_dict() return comp
def get_formula_dict( self): """returns a formula dict as defined in the periodic_table.py::formula_dict""" comp = PT.formula_dict() for a in self.atoms: comp += a.get_formula_dict() return comp
def get_formula_dict( self): """returns formula as dictionary that can be passed to functions in periodic_table""" return PT.formula_dict()
def expand(self): """expands the group and returns list of atoms that new drawing afterwords""" if self.group_type == "builtin": names = Store.gm.get_template_names() if self.symbol in names: a2 = self.neighbors[0] x1, y1 = a2.get_xy() x2, y2 = self.get_xy() self.group_graph = Store.gm.get_transformed_template( names.index(self.symbol), (x1, y1, x2, y2), type='atom1') replacement = self.group_graph.next_to_t_atom else: print("unknown group %s" % a.symbol) return None elif self.group_type == "chain": self.group_graph = self.molecule.create_graph() p = PT.formula_dict(self.symbol) n = p['C'] last = None for i in range(n): v = self.group_graph.add_vertex() v.x, v.y = None, None if last: self.group_graph.add_edge(last, v) last = v replacement = self.group_graph.vertices[0] replacement.x = self.x replacement.y = self.y elif self.group_type == "implicit": if not self.group_graph: self.set_name(self.symbol, occupied_valency=self.occupied_valency) for v in self.group_graph.vertices: v.x, v.y = None, None v.show = v.symbol != 'C' assert self.connecting_atom is not None replacement = self.connecting_atom replacement.x = self.x replacement.y = self.y self.molecule.eat_molecule(self.group_graph) self.molecule.move_bonds_between_atoms(self, replacement) self.molecule.delete_vertex(self) if self.occupied_valency: oasa.coords_generator.calculate_coords(self.molecule, bond_length=-1) else: # if the group is the only vertex of the molecule we must set the bond_length explicitly # and the move the whole molecule replacement.x = None replacement.y = None x, y = self.x, self.y oasa.coords_generator.calculate_coords( self.molecule, bond_length=Screen.any_to_px(self.paper.standard.bond_length)) dx = x - replacement.x dy = y - replacement.y [a.move(dx, dy) for a in self.group_graph.vertices] return self.group_graph.vertices