Visualize coarse-grained systems#

On this page you can find various examples how to colorize and visualize a coarse-grained (CG) system in SAMSON.

Colorize by group names#

The example below demonstrates how to colorize groups based on their names using a color palette.

Colorize by group names#
names = ['DAPC', 'DAPS', 'DFPC', 'DFPS', 
         'DIPC', 'DIPS', 'DPPC', 'DPPS', 
         'DLPC', 'DLPS', 'DYPC', 'DYPS', 
         'DOPE']

# get all structural groups in the active document
indexer = SAMSON.getNodes('node.type sg')

# make the operation undoable
SAMSON.beginHolding("Colorize nodes")

for node in indexer:
    if len(node.name):
        # get the first part of the name (name before the ID)
        name = node.name.split(' ')[0]

        if name in names:
            # colorize the node acording to its index in the list

            # get the index of the name in the list
            i = names.index(name)
            # get the color 'intensity' value, which should be in the [0, 1] range
            h = 1 - i / (len(names))
            # get RGB color from the palette
            color = SBPaletteDefaultPalette.sequentialHCLViridis.getColor(h)
            # set the color of the node
            node.setColor(color)

# stop holding the undoable operation
SAMSON.endHolding()

You can get a list of all structural group names (without IDs) in the document using the following code:

Get structural group names#
def get_structural_group_names():
    names = set()
    # get all structural groups in the active document
    indexer = SAMSON.getNodes('node.type sg')
    for node in indexer:
        if len(node.name):
            # get the first part of the name (name before the ID)
            names.add(node.name.split(' ')[0])

    return names

names = list(get_structural_group_names())
print(names)

Colorize coarse-grained atoms by name#

The example below demnonstrates how to colorize coarse-grained atoms by their name.

Colorize coarse-grained atoms by name#
atom_name_to_color = {
    'NH3' : SBElement.getColorCPK(SBElement.Nitrogen), # the CPK color for Nitrogen
    'NC3' : SBElement.getColorCPK(SBElement.N),        # the CPK color for Nitrogen
    'TAP' : SBElement.getColorCPK(SBElement.N),        # the CPK color for Nitrogen
    'PO4' : SBElement.getColorCPK(SBElement.P),        # the CPK color for Phosphorus
    'GL1' : SBColor.yellow,
    'GL2' : SBColor.yellow,
    'C1A' : SBColor.yellow,
    'C2A' : SBColor.yellow,
    'C3A' : SBColor.yellow,
    'C4A' : SBColor.yellow,
    'D1A' : SBColor.yellow,
    'D2A' : SBColor.yellow,
    'D3A' : SBColor.yellow,
    'D4A' : SBColor.yellow,
    'C1B' : SBColor.yellow,
    'C2B' : SBColor.yellow,
    'C3B' : SBColor.yellow,
    'C4B' : SBColor.yellow,
    'D1B' : SBColor.yellow,
    'D2B' : SBColor.yellow,
    'D3B' : SBColor.yellow,
    'D4B' : SBColor.yellow,
}
atom_name_to_radius = {
    'NH3' : SBQuantity.angstrom(1.5),
    'NC3' : SBQuantity.angstrom(1.5),
    'TAP' : SBQuantity.angstrom(1.5),
    'PO4' : SBQuantity.angstrom(1.5)
}

# get all coarse-grained atoms in the active document
indexer = SAMSON.getNodes('node.type atom and atom.symbol CG')

# make the operation undoable
SAMSON.beginHolding("Colorize nodes")

for node in indexer:
    if node.name in atom_name_to_color:
        # set the CG color for the atom
        node.setCoarseGrainedColor(atom_name_to_color[node.name])

    if node.name in atom_name_to_radius:
        # set the CG radius for the atom
        node.setCoarseGrainedRadius(atom_name_to_radius[node.name])

# stop holding the undoable operation
SAMSON.endHolding()

Colorize coarse-grained atoms by type#

The example below demnonstrates how to colorize coarse-grained atoms by their CG type using a color palette.

Colorize coarse-grained atoms by type#
def get_coarse_grained_types(atom_indexer):
    cg_types = set()
    for node in atom_indexer:
        if node.type == SBNode.Atom:
            cg_types.add(node.getCoarseGrainedType())

    return cg_types

# get all coarse-grained atoms in the active document
indexer = SAMSON.getNodes('node.type atom and atom.symbol CG')

cg_types = list(get_coarse_grained_types(atom_indexer = indexer))
print(f"Coarse-grained types in the document: {cg_types}")

# make the operation undoable
SAMSON.beginHolding("Colorize nodes")

for node in indexer:
    cg_type = node.getCoarseGrainedType()
    if cg_type in cg_types:
        # colorize the node acording to its index in the list

        # get the index of the CG type in the list
        i = cg_types.index(cg_type)
        # get the color 'intensity' value, which should be in the [0, 1] range
        h = 1 - i / (len(cg_types))
        # get RGB color from the palette
        color = SBPaletteDefaultPalette.sequentialHCLViridis.getColor(h)
        # set the CG color of the atom
        node.setCoarseGrainedColor(color)

# stop holding the undoable operation
SAMSON.endHolding()

You can also set specific colors per CG type. For example, for Martini 2 types:

Colorize coarse-grained atoms by Martini 2 type#

# The beads types for Martini 2 and some arbitrary colors for them
cg_types = {
'C1'   : SBElement.getColorCPK(SBElement.C),
'C2'   : SBElement.getColorCPK(SBElement.C),
'C3'   : SBElement.getColorCPK(SBElement.C),
'C4'   : SBElement.getColorCPK(SBElement.C),
'C5'   : SBElement.getColorCPK(SBElement.C),
'Nda'  : SBColor.magenta,
'Nd'   : SBColor.magenta,
'Na'   : SBColor.magenta,
'N0'   : SBColor.magenta,
'P1'   : SBColor.green,
'P2'   : SBColor.green,
'P3'   : SBColor.green,
'P4'   : SBColor.green,
'P5'   : SBColor.green,
'Qda+' : SBColor.blue,
'Qd+'  : SBColor.blue,
'Qa+'  : SBColor.blue,
'Q0+'  : SBColor.blue,
'Qda-' : SBColor.red,
'Qd-'  : SBColor.red,
'Qa-'  : SBColor.red,
'Q0-'  : SBColor.red,
}

# get all coarse-grained atoms in the active document
indexer = SAMSON.getNodes('node.type atom and atom.symbol CG')

# make the operation undoable
SAMSON.beginHolding("Colorize nodes")

for node in indexer:
    cg_type = node.getCoarseGrainedType()
    
    # take the partial charge into account for Qda, Qa, Qd, Q0 types
    if cg_type.startswith('Q'):
        if node.partialCharge < 0: cg_type += '-'
        elif node.partialCharge > 0: cg_type += '+'

    if cg_type in cg_types:
        # set the CG color of the atom
        node.setCoarseGrainedColor(cg_types[cg_type])

# stop holding the undoable operation
SAMSON.endHolding()