biocrnpyler.core.OrderedPolymer
- class biocrnpyler.core.OrderedPolymer(parts, default_direction=None)[source]
Bases:
MonomerCollectionA polymer made up of OrderedMonomers with a specific order.
Represents a linear sequence of monomers where each monomer has a defined position and direction. This class extends
MonomerCollectionto provide ordered, directional polymer structures commonly used to represent DNA constructs, RNA sequences, and protein chains.- Parameters:
parts (
listortuple) –Sequence of parts to add to the polymer. Each element can be:
An
OrderedMonomerobject (uses existing direction)A list/tuple
[OrderedMonomer, direction]specifying monomer and its direction explicitly
default_direction (
strorint, optional) – Default direction for monomers when not explicitly specified. Common values include ‘forward’, ‘reverse’, 0, 1, or None.
- Attributes:
polymer (
tupleofOrderedMonomer) – Ordered tuple of monomers in this polymer. Alias formonomersproperty inherited fromMonomerCollection.default_direction (
str,int, orNone) – Default direction assigned to monomers lacking explicit direction.
See also
NamedPolymerAn OrderedPolymer with an associated name.
OrderedMonomerA unit that belongs to an OrderedPolymer.
MonomerCollectionBase class for monomer collections.
Notes
Directions indicate the orientation of monomers in the polymer:
‘forward’ or 0: Standard/positive orientation
‘reverse’ or 1: Inverted/negative orientation
None: No specified orientation
The polymer tuple is immutable, but monomers can be added via
insert,append, orreplacemethods. Direct assignment to positions uses__setitem__which callsreplace.All monomers are deep-copied when added to ensure the polymer maintains independent references. This prevents external modifications from affecting the polymer structure.
Examples
Create a polymer from monomers:
>>> mon1 = bcp.OrderedMonomer() >>> mon2 = bcp.OrderedMonomer() >>> polymer = bcp.OrderedPolymer( ... parts=[mon1, mon2], ... default_direction='forward' ... ) >>> len(polymer) 2
Create a polymer with explicit directions:
>>> polymer = bcp.OrderedPolymer( ... parts=[[mon1, 'forward'], [mon2, 'reverse']] ... ) >>> polymer[0].direction 'forward' >>> polymer[1].direction 'reverse'
Methods
Add a monomer to the end of the polymer.
Callback method invoked whenever the polymer structure changes.
Remove a monomer from the polymer at a specific position.
Invert a direction value.
Insert a monomer at a specific position in the polymer.
Replace a monomer at a specific position in the polymer.
Reverse the order and directions of all monomers in the polymer.
- __contains__(item)[source]
Check if a monomer is in the polymer.
- Parameters:
item (
OrderedMonomer) – The monomer to search for.- Returns:
True if the monomer is in the polymer, False otherwise.
- Return type:
bool
- __eq__(other)[source]
Check equality with another OrderedPolymer.
Two polymers are equal if they have the same length and each corresponding pair of monomers has the same direction, position, and type.
- Parameters:
other (
OrderedPolymer) – The polymer to compare with.- Returns:
True if polymers are equal, False otherwise.
- Return type:
bool
- __getitem__(ii)[source]
Get a monomer or slice of monomers from the polymer.
- Parameters:
ii (
intorslice) – Index or slice to retrieve from the polymer.- Returns:
The monomer at the given index, or a tuple of monomers for a slice.
- Return type:
OrderedMonomerortuple
- __len__()[source]
Return the number of monomers in the polymer.
- Returns:
The number of monomers in the polymer sequence.
- Return type:
int
- __setitem__(ii, val)[source]
Replace a monomer at a specific position.
- Parameters:
ii (
int) – Index at which to replace the monomer.val (
OrderedMonomer) – The new monomer to insert at the position.
Notes
Internally calls
replacewith the monomer’s existing direction.
- append(part, direction=None)[source]
Add a monomer to the end of the polymer.
Appends a copy of the given monomer to the end of the polymer sequence by calling
insertat the final position.- Parameters:
part (
OrderedMonomer) – The monomer to append. A copy of this monomer will be added.direction (
str,int, orNone, optional) – Direction for the appended monomer. If None, uses the monomer’s existing direction if available.
See also
insertInsert a monomer at a specific position.
Examples
>>> polymer = bcp.OrderedPolymer(parts=[]) >>> mon = bcp.OrderedMonomer() >>> polymer.append(mon, direction='forward') >>> len(polymer) 1
- changed()[source]
Callback method invoked whenever the polymer structure changes.
This method is called after operations that modify the polymer, such as
insert,replace,delpart, orreverse. Subclasses can override this to implement custom behavior when the polymer is modified.Notes
The base implementation does nothing. Override in subclasses to add functionality like name regeneration, validation, or notifications.
- delpart(position)[source]
Remove a monomer from the polymer at a specific position.
Removes the monomer at the given position, shifts all subsequent monomers to lower positions, and calls the
changedcallback.- Parameters:
position (
int) – Index of the monomer to remove. Must be a valid position in the polymer.
See also
Notes
The removed monomer’s
removemethod is called to clear its parent, position, and direction. If the polymer has anameattribute and amake_namemethod, the name is regenerated after deletion.
- direction_invert(dirname)[source]
Invert a direction value.
Converts a direction to its opposite orientation. Used during polymer reversal operations.
- Parameters:
dirname (
str,int, orNone) –The direction to invert. Supported values:
’forward’ \(\rightleftharpoons\) ‘reverse’
0 \(\rightleftharpoons\) 1
None -> None
- Returns:
The inverted direction. Returns the input unchanged if it cannot be inverted.
- Return type:
str,int, orNone- Warns:
UserWarning – If the direction value is not recognized.
Examples
>>> polymer = bcp.OrderedPolymer(parts=[]) >>> polymer.direction_invert('forward') 'reverse' >>> polymer.direction_invert(0) 1
- insert(position, part, direction=None)[source]
Insert a monomer at a specific position in the polymer.
Inserts a copy of the given monomer at the specified position, shifting all subsequent monomers to higher positions. Calls the
changedcallback after insertion.- Parameters:
position (
int) – Index at which to insert the monomer. Must be between 0 andlen(polymer)(inclusive).part (
OrderedMonomer) – The monomer to insert. A copy of this monomer will be added.direction (
str,int, orNone, optional) – Direction for the inserted monomer. If None, uses the monomer’s existing direction.
See also
Notes
The monomer is deep-copied before insertion to maintain independence from the original object.
- replace(position, part, direction=None)[source]
Replace a monomer at a specific position in the polymer.
Removes the monomer at the given position and inserts a copy of the new monomer in its place. Calls the
changedcallback after replacement.- Parameters:
position (
int) – Index of the monomer to replace. Must be a valid position in the polymer.part (
OrderedMonomer) – The monomer to insert. A copy of this monomer will be added.direction (
str,int, orNone, optional) – Direction for the new monomer. If None, uses the monomer’s existing direction.
Notes
The removed monomer’s
removemethod is called to clear its parent, position, and direction attributes.
- reverse()[source]
Reverse the order and directions of all monomers in the polymer.
Reverses the polymer sequence and inverts the direction of each monomer. Updates all monomer positions to reflect their new locations. Calls the
changedcallback after reversal.Notes
This operation modifies the polymer in place. All monomers have their directions inverted using
direction_invertand their positions updated to match the reversed sequence.Examples
>>> mon1 = bcp.OrderedMonomer() >>> mon2 = bcp.OrderedMonomer() >>> polymer = bcp.OrderedPolymer( ... parts=[[mon1, 'forward'], [mon2, 'reverse']] ... ) >>> polymer.reverse() >>> polymer[0].direction 'forward' >>> polymer[1].direction 'reverse'