# hplattice.Chain¶

class hplattice.Chain.Chain(hpstring, initial_vec)[source]

The 2D HP lattice chain. The chain is defined by monomers (beads), which can be either Hydrophobic (H) or Polar (P). By convention, the first monomer in the chain is fixed at the origin on a two-dimensional square lattice.

Adjacent monomers are connected by bonds. The directionality of the bonds is stored in Vectors, and each bond-vector is stored as an integer: 0 (up), 1 (left), 2 (down) or 3 (right). A chain with $$N$$ monomers has $$N-1$$ vectors.

In addition to the vector representation of the chain, Chain objects also store the 2D coordinates of each monomer. The set of coordinates is stored in a Coords object.

Parameters: hpstring (str) – String that specifies H or P for each monomer. Example: PHPPHP initial_vec (list) – List that specifies the direction of each bond. Example: [0,0,1,2,1], which would correspond to up, up, left, down, left.
class Coords(num_monomers=0)[source]

Coords is the set of monomer coordinates for a chain.

Parameters: num_monomers (int) – The number of monomers in the chain.
as_npy_array()[source]

Convert Coords object to numpy.ndarray

Returns: array of coordinates numpy.ndarray
copy()[source]
Returns: a copy of this object Coords
distance_between_pts(idx1, idx2)[source]

Compute the cartesian distance between two monomers.

Parameters: idx1 (int) – the index of a monomer idx2 (int) – the index of a monomer
get(idx=None)[source]

Get the coordinates of a monomer.

Parameters: idx (int) – the index of a monomer
grow()[source]

Append another monomer to C-terminus of the chain, one space above the current C-terminal monomer.

is_viable()[source]

Check for steric overlap and chain crossovers, neither of which are allowed.

Returns: True if no problems are found with the chain. bool
pop()[source]

Remove the last monomer from the C-terminus of the chain.

rotate_down_to_left(idx)[source]

Decrements the x-coord and increments the y-coord of a monomer.

Parameters: idx (int) – the index of a monomer
rotate_right_to_down(idx)[source]

Decrements the x-coord and decrements the y-coord of a monomer.

Parameters: idx (int) – the index of a monomer
rotate_up_to_right(idx)[source]

Increments the x-coord and decrements the y-coord of a monomer.

Parameters: idx (int) – the index of a monomer
set(coords, idx=None)[source]

Change the coordinates for all monomers or one monomer if idx is defined.

Parameters: coords (numpy.ndarray) – 2D array of coordinates idx (int) – (optional), the index of a monomer
vec2coords(vec)[source]

Convert a Vectors object into a set of coordinates, and replace the currently stored coordinates with the result.

Parameters: vec (Vectors) – convert these vectors into a set of coordinates
class Chain.Vectors(vec_list=None)[source]

Vectors stores the directionality of the bonds between monomers in a Chain. Each bond-vector is stored as an integer: 0 (up), 1 (left), 2 (down) or 3 (right). A chain with $$N$$ monomers has $$N-1$$ vectors.

Parameters: vec_list (list) – List that specifies the direction of each bond. Example: [0,0,1,2,1], which would correspond to up, up, left, down, left.
as_npy_array()[source]

Convert Vectors object to numpy.ndarray

Returns: array of vector values numpy.ndarray
copy()[source]
Returns: copy of this object Vectors
get(idx)[source]

Get one of the vectors.

Parameters: idx (int) – the index of a vector
grow()[source]

Append another vector to C-terminus of the chain, pointing up.

increment(idx)[source]

Rotate a vector clockwise.

Parameters: idx (int) – the index of a vector
pop()[source]

Remove the last vector from the C-terminus of the chain.

set(vec)[source]

Change all of the vectors.

Parameters: vec (numpy.ndarray) – array of vector values
set_idx(idx, value)[source]

Change one of the vectors.

Parameters: idx (int) – the index of the vector to change value (int) – the value to change the vector to
Chain.contactstate()[source]

Find all contacts between pairs of H monomers that are separated by at least three positions along the chain, i.e. will not include (i, i+1) and (i, i+2) pairs.

Returns: list of (idx1,idx2) contacts (tuples) list
Chain.do_crankshaft(vecindex)[source]

Swap the values of a pair of chain vectors that are next-nearest neighbors.

Parameters: vecindex (int) – swap vecindex with vecindex+2
Chain.do_rigid_rot(vecindex, direction)[source]

Rotate a chain vector clockwise or counterclockwise. The possible clockwise rotations are:

0 –> 1

1 –> 2

2 –> 3

3 –> 0

Parameters: vecindex (int) – a valid vector index direction (int) – 1 for clockwise, -1 for counterclockwise

Swap the values of a pair of neighboring chain vectors.

Parameters: vecindex (int) – swap vecindex with vecindex+1
Chain.energy(epsilon=0.0)[source]

Compute energy of chain, based on hydrophobic contacts.

Parameters: epsilon (float) – the energy of one hydrophobic contact. the total energy of the chain. float
Chain.get_coord_array()[source]
Returns: the coordinates of the chain numpy.ndarray
Chain.get_hp_string()[source]
Returns: example HPHPHPPPHHP string
Chain.get_vec_length()[source]
Returns: The number of vectors in the chain, which should be one less than the number of monomers. int
Chain.grow()[source]

Add a new monomer to C-terminus of the chain.

Chain.hpstr2bin()[source]

Convert a string of type HPHPHPPPHHP to a list of 1s and 0s.

Chain.is_first_vec_one()[source]
Returns: True if the first vector points to the right bool
Chain.is_viable()[source]

Check for steric overlap and chain crossovers, neither of which are allowed.

Returns: True if no problems are found with the chain. bool
Chain.nextviable()[source]

Check for steric overlap and chain crossovers in the not-yet-accepted next configuration of the chain. This is usually called before accepting a monte carlo move to make sure that the chain doesn’t end up in a disallowed state.

Returns: True if no problems are found with the chain. bool
Chain.nonsym()[source]

Many of the conformations are related by rotations and reflections. We define a “non-symmetric” conformation to have the first direction 0 and the first turn be a 1 (right turn)

Returns: 1 if the chain is non-symmetric, 0 otherwise. int
Chain.shift()[source]

Shifts the chain vector to the ‘next’ list, according to an enumeration scheme where the most distal chain vector is incremented 0->1->2->3. After 3, the most distal vector element is removed, and the next most distal element is incremented. If there are multiple “3” vectors, this process is done recursively.

Example:

[0,0,0,0] –> [0,0,0,1]

[0,0,1,2] –> [0,0,1,3]

[0,1,0,3] –> [0,1,1]

[0,3,3,3] –> [1]

This operation is very useful for enumerating the full space of chain conformations.

Returns: 1 if no more shifts are possible and 0 otherwise. int
Chain.update_chain()[source]

Accept recent chain move. This is usually called after a trial monte carlo move to accept the chain perturbation.

Chain.vec2coords()[source]

Convert a Vectors object into a set of coordinates, and replace the currently stored coordinates with the result.

Parameters: vec (Vectors) – generate coordinates from current vectors