biocrnpyler.core.NamedPolymer
- class biocrnpyler.core.NamedPolymer(parts, name, default_direction=None, circular=False)[source]
Bases:
OrderedPolymerAn OrderedPolymer with an associated name and circularity flag.
Extends
OrderedPolymerto include a name identifier and optional circular topology flag. Commonly used to represent named biological constructs like plasmids, chromosomes, or specific DNA/RNA sequences.- Parameters:
parts (
listortuple) – Sequence of parts to add to the polymer. SeeOrderedPolymerfor format details.name (
str) – Name identifier for the polymer.default_direction (
str,int, orNone, optional) – Default direction for monomers when not explicitly specified.circular (
bool, defaultFalse) – If True, indicates the polymer has circular topology (e.g., a plasmid). If False, the polymer is linear.
- Attributes:
name (
str) – Name identifier for the polymer.circular (
bool) – Flag indicating circular (True) or linear (False) topology.polymer (
tupleofOrderedMonomer) – Ordered tuple of monomers in this polymer (inherited).default_direction (
str,int, orNone) – Default direction for monomers (inherited).
See also
OrderedPolymerBase class for ordered polymer structures.
OrderedMonomerA unit that belongs to an OrderedPolymer.
Notes
The
circularattribute is primarily informational and does not automatically enforce circular topology constraints in polymer operations. Subclasses or external code must handle circular semantics as needed.Examples
Create a linear named polymer:
>>> mon1 = bcp.OrderedMonomer() >>> mon2 = bcp.OrderedMonomer() >>> polymer = bcp.NamedPolymer( ... parts=[mon1, mon2], ... name='my_construct', ... default_direction='forward' ... ) >>> polymer.name 'my_construct' >>> polymer.circular False
Create a circular polymer (plasmid):
>>> plasmid = bcp.NamedPolymer( ... parts=[mon1, mon2], ... name='pUC19', ... circular=True ... ) >>> plasmid.circular True
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'