
     iq^                         d Z ddlZddlmZmZmZmZmZ ddl	m
Z
 ddZddZddZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Z	 ddZdS )a:  
Nucleic acid analysis --- :mod:`MDAnalysis.analysis.nuclinfo`
=============================================================

:Author: Elizabeth Denning
:Year: 2011
:Copyright: Lesser GNU Public License v2.1+

The module provides functions to analyze nucleic acid structures, in
particular

- backbone dihedrals,
- chi dihedrals,
- AS or CP phase angles,
- Watson-Crick N1-N3 distances, C2-O2 distances, N6-O4 distances, O6-N4 distances.

For applications of this kind of analysis see
:footcite:p:`Denning2011,Denning2012`.

All functions take a :class:`~MDAnalysis.core.universe.Universe` as an
argument together with further parameters that specify the base or bases in
question. Angles are in degrees. The functions use standard CHARMM names for
nucleic acids and atom names.


.. rubric:: References

.. footbibliography::

Distances
---------

.. autofunction:: wc_pair

.. autofunction:: minor_pair

.. autofunction:: major_pair


Phases
------

.. autofunction:: phase_cp

.. autofunction:: phase_as


Dihedral angles
---------------

.. autofunction:: tors

.. autofunction:: tors_alpha

.. autofunction:: tors_beta

.. autofunction:: tors_gamma

.. autofunction:: tors_delta

.. autofunction:: tors_eps

.. autofunction:: tors_zeta

.. autofunction:: tors_chi

.. autofunction:: hydroxyl

.. autofunction:: pseudo_dihe_baseflip

    N)pisincossqrtpow)mdamathSYSTEMc           
         |                      d                    |                    j        d         dv rd\  }}|                      d                    |                    j        d         dv rd\  }}|                      d                    ||||||                    }t          j        |d         j        |d         j        z
            }|S )	a  Watson-Crick basepair distance for residue `i` with residue `bp`.

    The distance of the nitrogen atoms in a Watson-Crick hydrogen bond is
    computed.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    i : int
        resid of the first base
    bp : int
        resid of the second base
    seg1 : str (optional)
        segment id for first base ["SYSTEM"]
    seg2 : str (optional)
        segment id for second base ["SYSTEM"]

    Returns
    -------
    float
        Watson-Crick base pair distance


    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6
     resid {0!s} r   DCDTUCTCYTTHYURA)N3N1DGDAAGADEGUA)r   r   ](segid {0!s} and resid {1!s} and name {2!s}) or (segid {3!s} and resid {4!s} and name {5!s})    select_atomsformatresnamesr   normposition)	universeibpseg1seg2a1a2wc_distwcs	            f/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/analysis/nuclinfo.pywc_pairr0   e   s    @ _33A6677@C 	H 	 	 B_33A6677@C H   B##	;;A6!Rr2<
 <
 G 
gaj)GAJ,??	@	@BI    c           
         |                      d                    |                    j        d         dv rd\  }}|                      d                    |                    j        d         dv rd\  }}|                      d                    ||||||                    }t          j        |d         j        |d         j        z
            }|S )	a  Minor-Groove basepair distance for residue `i` with residue `bp`.

    The distance of the nitrogen and oxygen atoms in a Minor-groove hydrogen
    bond is computed.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    i : int
        resid of the first base
    bp : int
        resid of the second base
    seg1 : str (optional)
        segment id for first base ["SYSTEM"]
    seg2 : str (optional)
        segment id for second base ["SYSTEM"]

    Returns
    -------
    float
        Minor groove base pair distance

    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6
    r   r   r   )O2C2r   )r4   r3   z\(segid {0!s} and resid {1!s} and name {2!s}) or (segid {3!s} and resid {4!s} and name {5!s})r   r    )	r&   r'   r(   r)   r*   r+   r,   	c2o2_distc2o2s	            r/   
minor_pairr7      s    > _33A6677@C 	H 	 	 B_33A6677@C H   B%%	::@&!Rr2;
 ;
 I <	!-	!0EEFFDKr1   c           
         |                      d                    |                    j        d         dv r@|                      d                    |                    j        d         dv rd\  }}nd\  }}|                      d                    |                    j        d         dv r@|                      d                    |                    j        d         dv rd	\  }}nd
\  }}|                      d                    ||||||                    }t          j        |d         j        |d         j        z
            }|S )a  Major-Groove basepair distance for residue `i` with residue `bp`.

    The distance of the nitrogen and oxygen atoms in a Major-groove hydrogen
    bond is computed.


    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    i : int
        resid of the first base
    bp : int
        resid of the second base
    seg1 : str (optional)
        segment id for first base ["SYSTEM"]
    seg2 : str (optional)
        segment id for second base ["SYSTEM"]

    Returns
    -------
    float
        Major groove base pair distance

    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6
    r   r   )r   r   r   r   r   r   )r   r   r   )N4O6)r:   r9   )r   r   r   r   r   r   r   r   )r   r   r   r   r   )O4N6)r<   r;   r   r   r    )	r&   r'   r(   r)   r*   r+   r,   no_distmajors	            r/   
major_pairr?      sp   @ _33A6677@C H     !7!7!:!:;;DQG L
 
 

  FBFB_33A6677@C 	H 	 	   !7!7!:!:;;DQG L
 
 
  FBFB##	;;A6!Rr2<
 <
 G L,wqz/BBCCELr1   c                 	   |                      d                    ||                    }|                      d                    ||                    }|                      d                    ||                    }|                      d                    ||                    }|                      d                    ||                    }|j        }|j        }	|j        }
|j        }|j        }||	z   |
z   |z   |z   dz  }||z
  }|	|z
  }|
|z
  }||z
  }||z
  }|t          dt          z  dz  d	z            z  |t          dt          z  d
z  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   }|t          dt          z  dz  d	z            z  |t          dt          z  d
z  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   }t          j        |d         |d                   }|t          t          |d         d          t          |d         d          z   t          |d         d          z             z  }t          j
        ||          }t          j
        ||          }t          j
        ||          }t          j
        ||          }t          j
        ||          }|t          dt          z  dz  d	z            z  |t          dt          z  d
z  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   dz  t          d          z  }|t          dt          z  dz  d	z            z  |t          dt          z  d
z  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   |t          dt          z  dz  d	z            z  z   t          d          z  }t          j        ||          t          dz  z   dz  t          z  }|dz  S )a  Pseudo-angle describing the phase of the ribose pucker for residue `i` using the CP method.

    The angle is computed by the positions of atoms in the ribose ring.


    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    float
        phase angle in degrees


    .. versionadded:: 0.7.6
     atom {0!s} {1!s} O4'  atom {0!s} {1!s} C1'  atom {0!s} {1!s} C2'  atom {0!s} {1!s} C3'  atom {0!s} {1!s} C4' g?                 @      ?       @      @      @r   r      g?     f@h  )r!   r"   	positionsr   r   r   npcrossr   r   dotarctan2)r&   segr'   atom1atom2atom3atom4atom5data1data2data3data4data5r0r1r2r3r4r5R1R2xnr1_dr2_dr3_dr4_dr5_dDr   	phase_angs                                  r/   phase_cprr   .  s   . !!":"A"A#q"I"IJJE!!":"A"A#q"I"IJJE!!":"A"A#q"I"IJJE!!":"A"A#q"I"IJJE!!":"A"A#q"I"IJJEOEOEOEOEOE
%-%
%
'%
/I	>B	B	B	B	B	B 
c!b&3,$%%	%AFSL3&'''	)AFSL3&'''	) AFSL3&'''	) AFSL3&'''		)  
c!b&3,$%%	%AFSL3&'''	)AFSL3&'''	) AFSL3&'''	) AFSL3&'''		)  	A1A	DQqT1AaD!,s1Q4||;<<<A6"a==D6"a==D6"a==D6"a==D6"a==D CBs*+++c!b&3,,---/c!b&3,,---/ c!b&3,,---/ c!b&3,,---	/ 	 y//	  
AFSL3&''	'#a"fslS()))	+#a"fslS()))	+ #a"fslS()))	+ #a"fslS()))		+
 	Y	A Aq!!R#X.%7"<Is?r1   c           	         |                      d                    ||          d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|j                                        }|j                                        }	|j                                        }
|j                                        }|j                                        }|t	          dt
          z  dz  dz            z  |	t	          dt
          z  d	z  dz            z  z   |
t	          dt
          z  d
z  dz            z  z   |t	          dt
          z  dz  dz            z  z   |t	          dt
          z  dz  dz            z  z   dz  dz  }|t          dt
          z  dz  dz            z  |	t          dt
          z  d	z  dz            z  z   |
t          dt
          z  d
z  dz            z  z   |t          dt
          z  dz  dz            z  z   |t          dt
          z  dz  dz            z  z   d
z  dz  }t          j        ||          dz  t
          z  }|dz  S )a  Pseudo-angle describing the phase of the ribose pucker for residue `i` using the AS method

    The angle is computed by the position vector of atoms in the ribose ring.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    float
        phase angle in degrees


    .. versionadded:: 0.7.6
    rB   rC   rD   rE   rA   rM   rG   rH   rI   rJ   rK   rL   g       rO   rP   )	r!   r"   dihedralvaluer   r   r   rR   rU   )r&   rV   r'   angle1angle2angle3angle4angle5r\   r]   r^   r_   r`   Br   rq   s                   r/   phase_asr|     s   , "" ''Q// ''Q// ''Q// ''Q//	 F "" ''Q// ''Q// ''Q// ''Q//	 F "" ''Q// ''Q// ''Q// ''Q//	 F "" ''Q// ''Q// ''Q// ''Q//	 F "" ''Q// ''Q// ''Q// ''Q//	 F O!!##EO!!##EO!!##EO!!##EO!!##E Sw/#5666s52:1C7888:s52:1C7888: s52:1C7888: s52:1C7888	: 	 	  Sw/#5666s52:1C7888:s52:1C7888: s52:1C7888: s52:1C7888	: 	 	  
1a  5(2-Is?r1   c           
      Z   |                      d                    ||dz
            d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|                      d                    ||          d                    ||          d                    ||          d                    ||dz                       }|                      d                    ||          d                    ||          d                    ||dz             d                    ||dz                       }|                      d	                    ||          d
                    ||          d                    ||          d                    ||                    }	t          |	          dk     rh|                      d	                    ||          d
                    ||          d                    ||          d                    ||                    }	|j                                        dz  }
|j                                        dz  }|j                                        dz  }|j                                        dz  }|j                                        dz  }|j                                        dz  }|	j                                        dz  }|
||||||gS )a  Calculation of nucleic backbone dihedral angles.

    The dihedral angles are alpha, beta, gamma, delta, epsilon, zeta, chi.

    The dihedral is computed based on position of atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    [alpha, beta, gamma, delta, epsilon, zeta, chi] : list of floats
        torsion angles in degrees

    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6

     atom {0!s} {1!s} O3' r    atom {0!s} {1!s} P   atom {0!s} {1!s} O5'  atom {0!s} {1!s} C5'  atom {0!s} {1!s} P    rE   rD   rA   rB    atom {0!s} {1!s} N9  atom {0!s} {1!s} C4  rM    atom {0!s} {1!s} N1  atom {0!s} {1!s} C2  rP   r!   r"   lenrt   ru   )r&   rV   r'   abgdezcalphabetagammadeltaepsilonzetachis                    r/   torsr     s   : 	 ''QU33&&sA.. ''Q// ''Q//		 	A 	!((a00 ''Q// ''Q// ''Q//		 	A 	 ''Q// ''Q// ''Q// ''Q//		 	A 	 ''Q// ''Q// ''Q// ''Q//		 	A 	 ''Q// ''Q// ''Q//!((a!e44		 	A 	 ''Q// ''Q//!((a!e44 ''QU33		 	A 	 ''Q// ''Q//&&sA.. ''Q//		 	A 1vvzz!!$++C33$++C33#**322$++C33	
 
 J$E:#DJ$EJ$Ej  3&G:#D
*



s
"C4wc::r1   c           	         |                      d                    ||dz
            d                    ||          d                    ||          d                    ||                    }|j                                        dz  }|S )a  alpha backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    alpha : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    r~   r   r   r   r   rP   r!   r"   rt   ru   )r&   rV   r'   r   r   s        r/   
tors_alphar   C  s    , 	 ''QU33&&sA.. ''Q// ''Q//		 	A J$ELr1   c           	         |                      d                    ||          d                    ||          d                    ||          d                    ||                    }|j                                        dz  }|S )a  beta  backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    beta : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    r   r   r   rE   rP   r   )r&   rV   r'   r   r   s        r/   	tors_betar   c  s    , 	!((a00 ''Q// ''Q// ''Q//		 	A :#DKr1   c           	         |                      d                    ||          d                    ||          d                    ||          d                    ||                    }|j                                        dz  }|S )a  Gamma backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    gamma : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    r   r   rE   rD   rP   r   )r&   rV   r'   r   r   s        r/   
tors_gammar         , 	 ''Q// ''Q// ''Q// ''Q//		 	A J$ELr1   c           	         |                      d                    ||          d                    ||          d                    ||          d                    ||                    }|j                                        dz  }|S )a  delta backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    delta : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    r   rE   rD   r~   rP   r   )r&   rV   r'   r   r   s        r/   
tors_deltar     r   r1   c           
         |                      d                    ||          d                    ||          d                    ||          d                    ||dz                       }|j                                        dz  }|S )a  Epsilon backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    epsilon : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    rE   rD   r~   r   r   rP   r   )r&   rV   r'   r   r   s        r/   tors_epsr     s    , 	 ''Q// ''Q// ''Q//!((a!e44		 	A j  3&GNr1   c           
         |                      d                    ||          d                    ||          d                    ||dz             d                    ||dz                       }|j                                        dz  }|S )a  Zeta backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    zeta : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    rD   r~   r   r   r   rP   r   )r&   rV   r'   r   r   s        r/   	tors_zetar     s    , 	 ''Q// ''Q//!((a!e44 ''QU33		 	A :#DKr1   c           	         |                      d                    ||          d                    ||          d                    ||          d                    ||                    }t          |          dk     rh|                      d                    ||          d                    ||          d                    ||          d                    ||                    }|j                                        dz  }|S )	a  chi nucleic acid dihedral

     The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    chi : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    rA   rB   r   r   rM   r   r   rP   r   )r&   rV   r'   r   r   s        r/   tors_chir     s    , 	 ''Q// ''Q//&&sA.. ''Q//		 	A 1vvzz!!$++C33$++C33#**322$++C33	
 
 *



s
"CJr1   c           	         |                      d                    ||          d                    ||          d                    ||          d                    ||                    }	 |j                                        dz  }n8# t          $ r+ d| dt          |j                   }t	          |          dw xY w|S )	aC  2-hydroxyl dihedral. Useful only for RNA calculations.

     .. Note:: This dihedral calculation will only work if using atom
               names as documented by charmm force field parameters,
               namely "C1', C2', O2', H2'".

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    hydroxyl_angle : float
        torsion angle in degrees


    .. versionadded:: 0.7.6

    zatom {0!s} {1!s} C1'zatom {0!s} {1!s} C2'zatom {0!s} {1!s} O2'zatom {0!s} {1!s} H2'rP   zResid z5 does not contain atoms C1', C2', O2', H2' but atoms N)r!   r"   rt   ru   
ValueErrorlistatoms)r&   rV   r'   hhydrerrmsgs         r/   hydroxylr   *  s    2 	%%c1--%%c1--%%c1--%%c1--		 	A+z!!C' + + +%Q % %!']]% % 	   d*+ Ks   *B 5B<c                    |                      d                    ||||                    }|                      d                    ||                    }|                      d                    ||                    }	|                      d                    ||                    }
|                                |	                                |
                                |                                g}t          j        |d         |d         z
  |d         |d         z
  |d         |d         z
            }t          j        |          dz  }|S )	a  pseudo dihedral for flipped bases. Useful only for nucleic acid base flipping

    The dihedral is computed based on position atoms for resid `i`

    .. Note:: This dihedral calculation will only work if using atom names as
              documented by charmm force field parameters.

    Parameters
    ----------
    universe : Universe
        :class:`~MDAnalysis.core.universe.Universe` containing the
        trajectory
    bp1 : int
        resid that base pairs with `bp2`
    bp2 : int
        resid below the base that flips
    i : int
        resid of the base that flips
    segid1 : str (optional)
        segid of resid base pairing with `bp2`
    segid2 : str (optional)
        segid, same as that of segid of flipping resid `i`
    segid3 : str (optional)
        segid of resid `i` that flips

    Returns
    -------
    float
          pseudo dihedral angle in degrees


    .. versionadded:: 0.8.0
    zd ( segid {0!s} and resid {1!s} and nucleicbase ) or ( segid {2!s} and resid {3!s} and nucleicbase ) z.(segid {0!s} and resid {1!s} and nucleicbase) z/(segid {0!s} and resid {1!s} and nucleicsugar) r   r   rF      rP   )r!   r"   center_of_massr   rt   rR   rad2deg)r&   bp1bp2r'   r)   r*   seg3bf1bf4bf2bf3ri   pseudos                r/   pseudo_dihe_baseflipr   U  sM   H 

	>>Df#tS?
 ?
 C 

8??aHH C 

9@@sKK C 

9@@qII C 			A adQqTk1Q4!A$;!qtDDFZ#%FMr1   )r	   r	   )r	   r	   r	   )__doc__numpyrR   mathr   r   r   r   r   MDAnalysis.libr   r0   r7   r?   rr   r|   r   r   r   r   r   r   r   r   r   r    r1   r/   <module>r      s|  0F FN     ( ( ( ( ( ( ( ( ( ( ( ( ( ( " " " " " "; ; ; ;|: : : :zK K K K\X X XvX X Xv\; \; \;~  @  @  @  @  @  @$ $ $N( ( (X ?G; ; ; ; ; ;r1   