biocrnpyler.core.OrderedMonomer

class biocrnpyler.core.OrderedMonomer(direction=None, position=None, parent=None)[source]

Bases: object

A unit that belongs to an OrderedPolymer.

Represents a single monomer unit within a polymer structure. Each monomer tracks its position in the polymer, its directional orientation, and maintains a reference to its parent polymer. This class is used as a base for representing DNA parts, RNA components, amino acids, and other polymer building blocks.

Parameters:
  • direction (str, int, or None, optional) – Directional orientation of the monomer in the polymer. Common values include ‘forward’, ‘reverse’, 0, 1, or None. Default is None.

  • position (int or None, optional) – Index position of the monomer within its parent polymer. Must be non-None if the monomer belongs to a polymer. Default is None.

  • parent (MonomerCollection or None, optional) – Reference to the parent MonomerCollection or OrderedPolymer containing this monomer. Default is None.

Attributes:
  • direction (str, int, or None) – Directional orientation of the monomer.

  • position (int or None) – Position index within the parent polymer.

  • parent (MonomerCollection or None) – Reference to the parent collection or polymer.

  • is_polymer_component (bool) – Flag indicating whether this monomer is part of a polymer structure. Set to True when inserted into a polymer.

See also

OrderedPolymer

A polymer made up of OrderedMonomers.

MonomerCollection

Base class for monomer collections.

Notes

OrderedMonomers are deep-copied when inserted into polymers to ensure independence. Use get_orphan to create a copy without a parent reference, or get_removed to create a fully detached copy.

The is_polymer_component flag and find_polymer_component method support scenarios where monomers may be nested within complex species.

Examples

Create a standalone monomer:

>>> monomer = bcp.OrderedMonomer(direction='forward')
>>> monomer.direction
'forward'
>>> monomer.parent is None
True

Add a monomer to a polymer:

>>> polymer = bcp.OrderedPolymer(parts=[])
>>> monomer = bcp.OrderedMonomer()
>>> polymer.append(monomer, direction='forward')
>>> polymer[0].position
0
>>> polymer[0].parent is polymer
True

Methods

find_polymer_component

Find the polymer component within this monomer or its species.

get_orphan

Create a copy of this monomer without a parent reference.

get_removed

Create a fully detached copy of this monomer.

monomer_insert

Insert this monomer into a polymer at a specific position.

remove

Remove this monomer from its parent polymer.

set_dir

Set the direction of the monomer and return self.

subhash

Compute hash contribution from monomer properties.

__eq__(other)[source]

Check equality with another OrderedMonomer.

Two monomers are equal if they have the same direction, position, and parent.

Parameters:

other (OrderedMonomer) – The monomer to compare with.

Returns:

True if monomers are equal, False otherwise.

Return type:

bool

find_polymer_component()[source]

Find the polymer component within this monomer or its species.

Searches this monomer and, if it is a ComplexSpecies, its constituent species to find which one is marked as a polymer component.

Returns:

The monomer that is part of a polymer structure, or None if no polymer component is found.

Return type:

OrderedMonomer or None

Raises:

ValueError – If multiple species are marked as polymer components in the same location.

Notes

This method is primarily used internally to handle complex species that may contain monomers as part of larger structures.

get_orphan()[source]

Create a copy of this monomer without a parent reference.

Returns a copy that retains position and direction but has no parent polymer. Useful for temporarily working with monomers outside their polymer context.

Returns:

A copy of this monomer with parent set to None but position and direction preserved.

Return type:

OrderedMonomer

See also

get_removed

Create a fully detached copy.

remove

Remove this monomer from its parent in place.

Notes

This is a shallow copy of the monomer object itself, though the parent reference is explicitly cleared.

get_removed()[source]

Create a fully detached copy of this monomer.

Returns a copy with all polymer-related attributes (parent, position, direction) cleared. Also removes ‘forward’ and ‘reverse’ attributes if present.

Returns:

A copy of this monomer with no parent, position, or direction, and with directional attributes removed.

Return type:

OrderedMonomer

See also

get_orphan

Create a copy without parent but with position and direction.

remove

Remove this monomer from its parent in place.

Notes

This method is useful for creating completely independent copies of monomers that can be reused in different contexts without any polymer associations.

monomer_insert(parent: OrderedPolymer, position: int, direction=None)[source]

Insert this monomer into a polymer at a specific position.

Sets the monomer’s parent, position, and direction attributes to reflect its insertion into the polymer. Marks the monomer (or its polymer component if it is a complex species) as a polymer component.

Parameters:
  • parent (OrderedPolymer) – The polymer to insert this monomer into.

  • position (int) – The position index where this monomer is being inserted.

  • direction (str, int, or None, optional) – The direction for this monomer. If None, uses the monomer’s existing direction.

Raises:

ValueError – If position is None, or if parent is None.

remove()[source]

Remove this monomer from its parent polymer.

Clears the monomer’s parent, position, and direction attributes, effectively detaching it from any polymer structure.

Returns:

Returns self for method chaining.

Return type:

OrderedMonomer

See also

get_removed

Create a fully detached copy of the monomer.

get_orphan

Create a copy with parent removed but position and direction preserved.

set_dir(direction)[source]

Set the direction of the monomer and return self.

Convenience method for setting direction in a fluent interface style.

Parameters:

direction (str, int, or None) – The direction to assign to this monomer.

Returns:

Returns self for method chaining.

Return type:

OrderedMonomer

Examples

>>> monomer = bcp.OrderedMonomer().set_dir('forward')
>>> monomer.direction
'forward'
subhash()[source]

Compute hash contribution from monomer properties.

Computes a hash value based on the monomer’s position, direction, and name (if present), excluding the parent reference.

Returns:

Hash value based on monomer-specific properties.

Return type:

int

Notes

This method is used by __hash__ to compute the monomer’s hash contribution. It excludes the parent to avoid circular dependencies in hash computation.