Chemistry.base package

Submodules

Chemistry.base.compounds module

This module provides the underlying framework for the package. It provides the base Compound class that is used throughout to represent molecules, as well as the abstract class _CompoundWrapper that is used throughout whenever the Compound class is wrapped (such as by Reactant). These classes are fundamental to the rest of the program.

class Chemistry.base.compounds.Compound(atoms, bonds, other_info={})[source]

Bases: networkx.classes.graph.Graph

A molecule stored in all its glory.

Implemented as a graph (subclassing networkx.Graph).

Parameters:

atoms : dict

A dictionary storing all the atoms of a molecule. Should be presented in a form like

{‘a1’: ‘H’, ‘a2’: ‘O’, ‘a3’: ‘H’}.

Accepted format is “a#” as a key and the atom’s atomic symbol as the value.

bonds : dict

A dictionary storing all the bonds of a molecule. Should be presented in a form like

`{‘b1’: (‘a1’, ‘a2’, {‘order’: 1, ‘chirality’: None}),

‘b2’: (‘a2’, ‘a3’, {‘order’: 1, ‘chirality’: None})}`.

Accepted format is “b#” as a key and a three-item tuple as the value. The items at indices 0 and 1 should be the keys of the two atoms being bonded (order doesn’t matter here) and the item at index 2 should be a dictionary of relevant information. Information that is not provided will be assigned reasonable default values. The chirality is from index 0 to index 1 of the tuple.

other_info : dict, optional.

A dictionary that stores all other relevant information about a molecule. Things like molecular charge, pka, the name/id of the molecule, etc. If no information is provided the constructor will attempt to ascertain any information it needs.

Attributes

name

Methods

add_cycle(nodes, **attr) Add a cycle.
add_edge(u, v[, attr_dict]) Add an edge between u and v.
add_edges_from(ebunch[, attr_dict]) Add all the edges in ebunch.
add_node(n[, attr_dict]) Add a single node n and update node attributes.
add_nodes_from(nodes, **attr) Add multiple nodes.
add_path(nodes, **attr) Add a path.
add_star(nodes, **attr) Add a star.
add_weighted_edges_from(ebunch[, weight]) Add all the edges in ebunch as weighted edges with specified weights.
adjacency_iter() Return an iterator of (node, adjacency dict) tuples for all nodes.
adjacency_list() Return an adjacency list representation of the graph.
clear() Remove all nodes and edges from the graph.
copy() Return a copy of the graph.
degree([nbunch, weight]) Return the degree of a node or nodes.
degree_iter([nbunch, weight]) Return an iterator for (node, degree).
edge_matcher(edge1, edge2) Helper function to check for isomorphic graphs
edges([nbunch, data]) Return a list of edges.
edges_iter([nbunch, data]) Return an iterator over the edges.
get_edge_data(u, v[, default]) Return the attribute dictionary associated with edge (u,v).
has_edge(u, v) Return True if the edge (u,v) is in the graph.
has_node(n) Return True if the graph contains the node n.
is_directed() Return True if graph is directed, False otherwise.
is_isomorphic(other) Determines whether or not a molecule is isomorphically equivalent to another.
is_multigraph() Return True if graph is a multigraph, False otherwise.
json_serialize(obj[, as_str]) Serializes an object for json, used for __str__ and __repr__
nbunch_iter([nbunch]) Return an iterator of nodes contained in nbunch that are also in the graph.
neighbors(n) Return a list of the nodes connected to the node n.
neighbors_iter(n) Return an iterator over all neighbors of node n.
node_matcher(node1, node2) Helper function to check for isomorphic graphs
nodes([data]) Return a list of the nodes in the graph.
nodes_iter([data]) Return an iterator over the nodes.
nodes_with_selfloops() Return a list of nodes with self loops.
number_of_edges([u, v]) Return the number of edges between two nodes.
number_of_nodes() Return the number of nodes in the graph.
number_of_selfloops() Return the number of selfloop edges.
order() Return the number of nodes in the graph.
remove_edge(u, v) Remove the edge between u and v.
remove_edges_from(ebunch) Remove all edges specified in ebunch.
remove_node(n) Remove node n.
remove_nodes_from(nodes) Remove multiple nodes.
selfloop_edges([data]) Return a list of selfloop edges.
size([weight]) Return the number of edges.
subgraph(nbunch) Return the subgraph induced on nodes in nbunch.
to_directed() Return a directed representation of the graph.
to_undirected() Return an undirected copy of the graph.
classmethod edge_matcher(edge1, edge2)[source]

Helper function to check for isomorphic graphs

Parameters:

edge1 : Object

The first edge evaluated when checking graph isomorphism

edge2 : Object

The second edge evalutated when checking graph isomorphism

is_isomorphic(other)[source]

Determines whether or not a molecule is isomorphically equivalent to another.

Parameters:

other : Compound, _CompoundWrapper

The Compound that is being check for isomorphism.

classmethod json_serialize(obj, as_str=False)[source]

Serializes an object for json, used for __str__ and __repr__

Parameters:

obj : Object

The object to be serialized. Can be anything, although this function is largely intended to work with Compound objects

as_str : bool, optional

Specifies whether repr or str will be used to generate the strings

classmethod node_matcher(node1, node2)[source]

Helper function to check for isomorphic graphs

Parameters:

node1 : Object

The first node evaluated when checking graph isomorphism

node2 : Object

The second node evaluated when checking graph isomorphism

Returns:

bool

Whether or not the nodes can be considered equivalent

Chemistry.base.periodic_table module

This module stores all of the data about each element in the periodic table. This includes atomic mass, radius, electronegativity, etc.

Chemistry.base.periodic_table.get_element(symbol='C')[source]

Function that returns the appropriate data for a given element.

Parameters:

symbol : string

The symbol of the atom being returned. Defaults to ‘C’ for Carbon.

Returns:

table : dict

A dictionary storing all of the relevant information about an element.

Chemistry.base.products module

This module provides the necessary classes and helper functions to create and manipulate the products of chemical reactions.

class Chemistry.base.products.EquilibriumProducts(reactants, products)[source]

Bases: object

EquilibriumProducts object - much the same as a Products object but for reversible reactions.

Parameters:

reactants : tuple

Tuple of the reactants.

products : Products

Products object.

Attributes

products The products of the reaction; that is the things resulting from
reactants The reactants in the reaction; the things that were there initially
products[source]

The products of the reaction; that is the things resulting from the interaction of the reactants

reactants[source]

The reactants in the reaction; the things that were there initially and that remain there after the reaction

class Chemistry.base.products.Product(comp, percentage)[source]

Bases: Chemistry.base.compounds._CompoundWrapper

The base Product object. Represents a compound that results from a chemical reaction.

Parameters:

comp : Compound

The compound that is being considered a product.

percentage : float

What percent of the total products is represented by this compound.

Attributes

compound The underlying compound object that is being wrapped.
class Chemistry.base.products.Products(maj, min_)[source]

Bases: object

A Products object - represents a collection of Product objects because a reaction usually has more than one.

Parameters:

maj : tuple

Tuple of 0 or more Product objects. Represents the major products.

min_ : tuple

Tuple of 0 or more Product objects. Represents the minor products.

Attributes

———

major

minor

Attributes

major The major products of a reaction.
minor The minor products of a reaction.
major[source]

The major products of a reaction.

Expected to be a collection of Product objects. Raises a TypeError if given things that are not NoneType (those are skipped) or Product objects

minor[source]

The minor products of a reaction.

Expected to be a collection of Product objects. Raises a TypeError if given things that are not NoneType (those are skipped) or Product objects

Chemistry.base.reactants module

This module provides the classes used to form various reactants in a chemical reaction.

class Chemistry.base.reactants.Acid(compound, acidic_point, pka)[source]

Bases: Chemistry.base.reactants.Reactant

A subclass of Reactant, represents acidic compounds in a reaction.

Parameters:

compound : Compound

The molecule being treated as an acid.

acidic_point : string

The key of the ‘acidic point’ of the molecule, or the most acidic H+.

pka : float

The pKa of the aforementioned most acidic H+.

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.

Methods

to_conjugate_base() Transforms the current acid into its conjugate base.
to_conjugate_base()[source]

Transforms the current acid into its conjugate base.

class Chemistry.base.reactants.Base(compound, basic_point, pka)[source]

Bases: Chemistry.base.reactants.Reactant

A subclass of Reactant, represents basic compounds in a reaction.

Parameters:

compound : Compound

The molecule being treated as a base.

basic_point : string

The key of the ‘basic point’ of the molecule, or the location that is most accepting of H+.

pka : float

The pKa of the conjugate acid.

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.

Methods

to_conjugate_acid() Transforms the current base into its conjugate acid.
to_conjugate_acid()[source]

Transforms the current base into its conjugate acid. Is side-effect free; all changes happen on a copy of this base.

Returns:

Acid

The conjugate acid of the base.

class Chemistry.base.reactants.BronstedAcid(compound, acidic_point, pka)[source]

Bases: Chemistry.base.reactants.Acid

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.

Methods

to_conjugate_base() Transforms the current acid into its conjugate base.
class Chemistry.base.reactants.BronstedBase(compound, basic_point, pka)[source]

Bases: Chemistry.base.reactants.Base

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.

Methods

to_conjugate_acid() Transforms the current base into its conjugate acid.
class Chemistry.base.reactants.Electrophile(compound)[source]

Bases: Chemistry.base.reactants.Reactant

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.
class Chemistry.base.reactants.LewisAcid(compound, acidic_point, pka)[source]

Bases: Chemistry.base.reactants.Acid

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.

Methods

to_conjugate_base() Transforms the current acid into its conjugate base.
class Chemistry.base.reactants.LewisBase(compound, basic_point, pka)[source]

Bases: Chemistry.base.reactants.Base

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.

Methods

to_conjugate_acid() Transforms the current base into its conjugate acid.
class Chemistry.base.reactants.Nucleophile(compound)[source]

Bases: Chemistry.base.reactants.Reactant

Attributes

compound The underlying compound object that is being wrapped.
pka The pka of a molecule.
class Chemistry.base.reactants.Reactant(compound)[source]

Bases: Chemistry.base.compounds._CompoundWrapper

The base Reactant object. All subclasses of this are things that are commonly found in a reaction.

Parameters:

compound : Compound

The molecule being considered as a reactant.

Attributes

pka The pka of a molecule.
pka[source]

The pka of a molecule. If the wrapped molecule already has a known pka then the value passed by the constructor is checked against that.

Module contents

This package provides the underlying framework necessary for chemical reactions. All reactions rely on the infrastructure provided within.