
     i@                          d Z ddlZdZdZdZdZdej        ded	ej        fd
Z	dej        dedefdZ
dej        d	ej        fdZeedfdej        dededed	ej        f
dZdej        d	ej        fdZdS )a  
A re-implementation of DSSP algorithm :footcite:p:`Kabsch1983`, taken from
*pydssp* v.0.9.0 (https://github.com/ShintaroMinami/PyDSSP) by Shintaro Minami,
distributed under MIT license.

Current implementation doesn't use `einops` as a dependency, instead directly
using `numpy` operations for axis rearrangement. However, this implementation
does not allow for batch computation, in contrast with `pydssp`, since it's
designed to be used in per-frame manner in protein trajectories.
    Ng/$?iL  g            ?awindowreturnc                 B    t          t          | |d          |d          S )a  Performs array upsampling with given window along given axis.

    Example
    -------
    .. code-block:: python
        hbmap = np.arange(4*4).reshape(4,4)
        print(hbmap)
        # [[ 0  1  2  3]
        #  [ 4  5  6  7]
        #  [ 8  9 10 11]
        #  [12 13 14 15]]

        print(_upsample(hbmap))
        # [[[[ 0  1  2]
        #    [ 4  5  6]
        #    [ 8  9 10]]

        #   [[ 1  2  3]
        #    [ 5  6  7]
        #    [ 9 10 11]]]


        #  [[[ 4  5  6]
        #    [ 8  9 10]
        #    [12 13 14]]

        #   [[ 5  6  7]
        #    [ 9 10 11]
        #    [13 14 15]]]]

    Parameters
    ----------
    a : np.ndarray
        input array
    window : int
        upsample window

    Returns
    -------
    np.ndarray
        unfolded array
    )_unfold)r   r   s     o/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/analysis/dssp/pydssp_numpy.py	_upsampler      s#    V 71fb))62666    axisc                     t          j        |          dddf         t          j        | j        |         |z
  dz             dddf         z   }t          j        | ||          }t          j        ||dz
  d          S )z'Helper function for 2D array upsamplingN   r   )nparangeshapetakemoveaxis)r   r   r   idxunfoldeds        r
   r	   r	   B   s     		&!!!T'"
)AGDMF*Q.
/
/aaa
8	9  wq#D)))H;x2...r   coordc                 r   | dddf         | dddf         z
  }|t           j                            |dd          z  }| dddf         | dddf         z
  }|t           j                            |dd          z  }||z   }|t           j                            |dd          z  }| dddf         d|z  z   S )	a  Fills in hydrogen atoms positions if they are abscent, under the
    assumption that C-N-H and H-N-CA angles are perfect 120 degrees,
    and N-H bond length is 1.01 A.

    Parameters
    ----------
    coord : np.ndarray
        input coordinates in Angstrom, shape (n_atoms, 4, 3),
        where second axes corresponds to (N, CA, C, O) atom coordinates

    Returns
    -------
    np.ndarray
        coordinates of additional hydrogens, shape (n_atoms-1, 3)

    .. versionadded:: 2.8.0
    r   Nr   r      T)r   keepdimsg)\(?)r   linalgnorm)r   vec_cnvec_canvec_nhs       r
   _get_hydrogen_atom_positionr"   L   s    : 122q5\E#2#q&M)FbinnV"tnDDDF ABBElU122q5\)G	wR$GGGGgFbinnV"tnDDDF Q<$-''r   Fcutoffmarginreturn_ec                    | j         \  }}}|dv s
J d            |dk    rt          |           }n0|dk    r| dddf         }| ddddf         } nt          d          | dddf         | ddd	f         | ddd
f         }
}	}|dz
  }t          j        |	|ddf          }t          j        |
|ddf          }t          j        |dd|f                              ||d
          }t          j        |dd|f                              ||d
          }t          j                            ||z
  d          }t          j                            ||z
  d          }t          j                            ||z
  d          }t          j                            ||z
  d          }t          j        t          d|z  d|z  z   d|z  z
  d|z  z
  z  t          z  ddgddgg          }|r|S t          j        |t                     }|t          j        t          j        |dz
  t                    d           z  }|t          j        t          j        |d	z
  t                    d           z  }t          j        ||z
  |z
  | |          }t          j        ||z  t          j        z  d	z            dz   d	z  }||z  }|S )a  Returns hydrogen bond map

    Parameters
    ----------
    coord : np.ndarray
        input coordinates in either (n, 4, 3) or (n, 5, 3) shape
        (without or with hydrogens). If hydrogens are not present, then
        ideal positions (see :func:_get_hydrogen_atom_positions) are used.
    cutoff : float, optional
        cutoff, by default DEFAULT_CUTOFF
    margin : float, optional
        margin, by default DEFAULT_MARGIN
    return_e : bool, optional
        if to return energy instead of hbond map, by default False

    Returns
    -------
    np.ndarray
        output hbond map or energy depending on return_e param


    .. versionadded:: 2.8.0
    )      z8Number of atoms should be 4 (N,CA,C,O) or 5 (N,CA,C,O,H)r'   r(   r   Nr   r   r      r   r   )dtype)kr   )a_mina_max)r   r"   
ValueErrorr   tilereshaper   r   pad
CONST_Q1Q2CONST_Feyebooldiagonesclipsinpi)r   r#   r$   r%   n_atomsn_atom_types_h_1n_1c_0o_0ncmapomapnmaphmapd_ond_chd_ohd_cne
local_mask	hbond_maps                          r
   get_hbond_maprN   w   s   :  %{G\1     B  
 q)%00			ABBElaaa!eF
 
 	
 !""a%L%"a.%"a.cC!A73Aq	""D73Aq	""D73Aq	""**1a33D73Aq	""**1a33D9>>$+B>//D9>>$+B>//D9>>$+B>//D9>>$+B>//D 	:d
"S4Z/#*<	>
	 Q!Q		 	A   &----J27277Q;d;;;rBBBBBJ27277Q;d;;;rBBBBBJ!+F7&IIII	F*RU2Q677#=BIJ&Ir   c                 p   t          |           }t          j        |dd          }t          j        |d          dk    }t          j        |d          dk    }t          j        |d          dk    }t          j        |dd         |d	d         z  d	dgg          }t          j        |dd         |d	d         z  d	dgg          }t          j        |dd         |d	d         z  d	dgg          }|t          j        |d	d
          z   t          j        |dd
          z   t          j        |dd
          z   }|t          j        |dd
           z  | z  }|t          j        |dd
           z  | z  }|t          j        |d	d
          z   t          j        |dd
          z   }	|t          j        |d	d
          z   t          j        |dd
          z   t          j        |dd
          z   t          j        |dd
          z   }
t          |d          dk    }t          j        |d
d	          }|ddddd
d	f         |ddddd	df         z  |ddddd
d	f         |ddddd	df         z  z   }t          j        |d	d	gd	d	gg          }|ddddd	d	f         |ddddd	d	f         z  |ddddd
df         |ddddd
df         z  z   }t          j        |d	d	gd	d	gg          }||z                       d          dk    }|	|z   |
z   dk    }|}| | z  }t          j        |||gd          }|S )ai  Assigns secondary structure for a given coordinate array,
    either with or without assigned hydrogens

    Parameters
    ----------
    coord : np.ndarray
        input coordinates in either (n, 4, 3) or (n, 5, 3) shape,
        without or with hydrogens, respectively. Second dimension `k` represents
        (N, CA, C, O) atoms coordinates (if k=4), or (N, CA, C, O, H) coordinates
        (when k=5).

    Returns
    -------
    np.ndarray
        output (n,) array with one-hot labels in C3 notation ('-', 'H', 'E'),
        representing loop, helix and sheet, respectively.


    .. versionadded:: 2.8.0
    r   r   r)   )offsetg        r'   r(   Nr   r   r   r   )	rN   r   swapaxesdiagonalr1   rollr   sumstack)r   hbmapturn3turn4turn5h3h4h5helix4helix3helix5	unfoldmapunfoldmap_revp_bridgea_bridgeladderhelixstrandlooponehots                       r
   assignri      s   , %  EKr2&&E Ka(((3.EKa(((3.EKa(((3.E 
crc
U122Y&!Q	1	1B	crc
U122Y&!Q	1	1B	crc
U122Y&!Q	1	1B "'"a###bgb!Q&7&77"'"a:K:KKF	rwvr1%%%	%	/B	rwvr1%%%	%	/B"'"a###bgb!Q&7&77F

'"a

	
'"a

	 '"a

	 '"a

		  %##c)IK	1a00M!!!QQQ1*%aaaAqj(AAaaaAqj!IaaaAqj$99H vh!Q!Q 011H!!!QQQ1*%aaaAqj(AA!!!QQQ1*aaaAqj 99H vh!Q!Q 011H !&&r**S0F f_v%,EF6VGDXtUF+"555FMr   )__doc__numpyr   r2   r3   DEFAULT_CUTOFFDEFAULT_MARGINndarrayintr   r	   r"   floatr5   rN   ri    r   r
   <module>rr      sJ  	 	    

+7 +7S +7RZ +7 +7 +7 +7\/rz /3 /c / / / /((rz ((bj (( (( (( ((Z #"	S S:SS S 	S
 ZS S S SlG"* G G G G G G Gr   