Molecules and Sites

Molecules are derived from chem2.Molecule and represent collections of sites. Sites are derived from chem2.Site and represent functional substructures of a molecule. A molecule may be related to multiple sites, but a site may be related only to one molecule. Both molecules and sites may be subclassed from their base classes indefinitely many times.

The Molecule object has a site attribute that holds the list of sites. Below is a summary of Molecule attributes and methods.

Attribute Setter Getter Unsetter
id set_id(id) get_id()
sites add_sites(*sites) get_sites(**kwargs) remove_sites(*sites)

The Site object has a molecule attribute that points to a single molecule. Below is a summary of Site attributes and methods relevant for this section.

Attribute Setter Getter Unsetter
id set_id(id) get_id()
molecule set_molecule(molecule) get_molecule() unset_molecule()

add_sites() and set_molecule() have the same functionality of setting up relations between sites and molecules. Similarly, remove_sites() and unset_molecule() have the same functionality of removing relations between sites and molecules.

get_sites() can be filtered on attribute and site type.

Site has the allowed_molecule_types class attribute that restricts which molecule types are allowed to have a particular type of site. This can be set when subclassing Site.

Class Attribute Default Description
allowed_molecule_types None Tuple of allowed Molecule classes

Creating types & instances


In [1]:
from wc_rules.chem2 import Molecule,Site

Below we create the molecule type hierarchy Molecule -> Receptor -> EGFR


In [2]:
class Receptor(Molecule):pass
class EGFR(Receptor):pass

Similarly, from Site, we create 2 different types of sites.


In [3]:
class LigandBinding(Site):pass
class Dimerize(Site):pass

We create one instance of EGFR molecule, one instance of LigandBinding site and one instance of Dimerize site.


In [4]:
Rec = EGFR().set_id('Rec')
L = LigandBinding().set_id('L')
D = Dimerize().set_id('D')

[Rec.get_id(), L.get_id(), D.get_id()]


Out[4]:
['Rec', 'L', 'D']

The molecule instance named Rec can be type-checked against the EGFR class, the Receptor class and the Molecule class.


In [5]:
isinstance(Rec,EGFR) and isinstance(Rec,Receptor) and isinstance(Rec,Molecule)


Out[5]:
True

Managing molecule-site relations

Function Molecule method Site method
Attach sites to a molecule add_sites(*sites) set_molecule(molecule)
Detach sites from a molecule remove_sites(*sites) unset_molecule(molecule)
Get/Filter the attached sites get_sites(**kwargs)
Get the attached molecule get_molecule()

In [6]:
# Adding sites with add_sites()
Rec.add_sites(L,D)
# Getting sites
Rec.get_sites()


Out[6]:
[<__main__.LigandBinding at 0x2279503fdd8>,
 <__main__.Dimerize at 0x2279503fe48>]

In [7]:
# Filter sites by id
Rec.get_sites(id='L')


Out[7]:
[<__main__.LigandBinding at 0x2279503fdd8>]

In [8]:
# Filter sites by site type
Rec.get_sites(site_type=LigandBinding)


Out[8]:
[<__main__.LigandBinding at 0x2279503fdd8>]

In [9]:
# Removing sites with remove_sites()
Rec.remove_sites(L,D)
Rec.get_sites()


Out[9]:
[]

In [10]:
# Adding sites with set_molecule()
L.set_molecule(Rec)
D.set_molecule(Rec)
Rec.get_sites()


Out[10]:
[<__main__.LigandBinding at 0x2279503fdd8>,
 <__main__.Dimerize at 0x2279503fe48>]

In [11]:
# Getting attached molecule
L.get_molecule()


Out[11]:
<__main__.EGFR at 0x2279503fda0>

In [12]:
# Removing sites with unset_molecule()
L.unset_molecule()
D.unset_molecule()
Rec.get_sites()


Out[12]:
[]

In [13]:
# Creating molecule/site instances and setting relations at the same time
Rec = EGFR(id='Rec').add_sites(
    LigandBinding(id='L'),
    Dimerize(id='D'),
)

Rec.get_sites()


Out[13]:
[<__main__.LigandBinding at 0x2279504f7b8>,
 <__main__.Dimerize at 0x2279504f7f0>]

In [14]:
# Defining site types that can only be attached to EGFR
class EGFRSite(Site):
    allowed_molecule_types = (EGFR,) 
    
class IGFR(Molecule):pass

x1 = EGFRSite().set_molecule( IGFR() )
x2 = EGFRSite().set_molecule( EGFR() )
for x in [x1,x2]:
    try:
        x.verify_allowed_molecule_type()
        print('Allowed')
    except:
        print('Not allowed')


Not allowed
Allowed