biocrnpyler.core.OrderedMonomer
- class biocrnpyler.core.OrderedMonomer(direction=None, position=None, parent=None)[source]
Bases:
objectA 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, orNone, optional) – Directional orientation of the monomer in the polymer. Common values include ‘forward’, ‘reverse’, 0, 1, or None. Default is None.position (
intorNone, 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 (
MonomerCollectionorNone, optional) – Reference to the parentMonomerCollectionorOrderedPolymercontaining this monomer. Default is None.
- Attributes:
direction (
str,int, orNone) – Directional orientation of the monomer.position (
intorNone) – Position index within the parent polymer.parent (
MonomerCollectionorNone) – 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
OrderedPolymerA polymer made up of OrderedMonomers.
MonomerCollectionBase class for monomer collections.
Notes
OrderedMonomers are deep-copied when inserted into polymers to ensure independence. Use
get_orphanto create a copy without a parent reference, orget_removedto create a fully detached copy.The
is_polymer_componentflag andfind_polymer_componentmethod 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 the polymer component within this monomer or its species.
Create a copy of this monomer without a parent reference.
Create a fully detached copy of this monomer.
Insert this monomer into a polymer at a specific position.
Remove this monomer from its parent polymer.
Set the direction of the monomer and return self.
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:
OrderedMonomerorNone- 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:
See also
get_removedCreate a fully detached copy.
removeRemove 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:
See also
get_orphanCreate a copy without parent but with position and direction.
removeRemove 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, orNone, 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:
See also
get_removedCreate a fully detached copy of the monomer.
get_orphanCreate 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, orNone) – The direction to assign to this monomer.- Returns:
Returns self for method chaining.
- Return type:
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.