
     i<\                         d Z ddlZddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZmZ  ej        e          Zdd
Zd Z G d de	          ZdS )a  Bond-Angle-Torsion coordinates analysis --- :mod:`MDAnalysis.analysis.bat`
===========================================================================

:Author: Soohaeng Yoo Willow and David Minh
:Year: 2020
:Copyright: Lesser GNU Public License, v2.1 or any higher version

.. versionadded:: 2.0.0

This module contains classes for interconverting between Cartesian and an
internal coordinate system, Bond-Angle-Torsion (BAT) coordinates
:footcite:p:`Chang2003`, for a given set of atoms or residues. This coordinate
system is designed to be complete, non-redundant, and minimize correlations
between degrees of freedom. Complete and non-redundant means that for N atoms
there will be 3N Cartesian coordinates and 3N BAT coordinates. Correlations are
minimized by using improper torsions, as described in :footcite:p:`Hikiri2016`.

More specifically, bond refers to the bond length, or distance between
a pair of bonded atoms. Angle refers to the bond angle, the angle between
a pair of bonds to a central atom. Torsion refers to the torsion angle.
For a set of four atoms a, b, c, and d, a torsion requires bonds between
a and b, b and c, and c and d. The torsion is the angle between a plane
containing atoms a, b, and c and another plane containing b, c, and d.
For a set of torsions that share atoms b and c, one torsion is defined as
the primary torsion. The others are defined as improper torsions, differences
between the raw torsion angle and the primary torsion. This definition reduces
the correlation between the torsion angles.

Each molecule also has six external coordinates that define its translation and
rotation in space. The three Cartesian coordinates of the first atom are the
molecule's translational degrees of freedom. Rotational degrees of freedom are
specified by the axis-angle convention. The rotation axis is a normalized vector
pointing from the first to second atom. It is described by the polar angle,
:math:`\phi`, and azimuthal angle, :math:`\theta`. :math:`\omega` is a third angle
that describes the rotation of the third atom about the axis.

This module was adapted from AlGDock :footcite:p:`Minh2020`.


See Also
--------
:class:`~MDAnalysis.analysis.dihedrals.Dihedral`
   class to calculate dihedral angles for a given set of atoms or residues
:func:`MDAnalysis.lib.distances.calc_dihedrals()`
   function to calculate dihedral angles from atom positions


Example applications
--------------------

The :class:`~MDAnalysis.analysis.bat.BAT` class defines bond-angle-torsion
coordinates based on the topology of an atom group and interconverts between
Cartesian and BAT coordinate systems.

For example, we can determine internal coordinates for residues 5-10
of adenylate kinase (AdK). The trajectory is included within the test data files::

   import MDAnalysis as mda
   from MDAnalysisTests.datafiles import PSF, DCD
   import numpy as np

   u = mda.Universe(PSF, DCD)

   # selection of atomgroups
   selected_residues = u.select_atoms("resid 5-10")

   from MDAnalysis.analysis.bat import BAT
   R = BAT(selected_residues)

   # Calculate BAT coordinates for a trajectory
   R.run()

After :meth:`R.run()<BAT.run>`, the coordinates can be accessed with
:attr:`R.results.bat<BAT.bat>`. The following code snippets assume that the
previous snippet has been executed.

Reconstruct Cartesian coordinates for the first frame::

   # Reconstruct Cartesian coordinates from BAT coordinates
   # of the first frame
   XYZ = R.Cartesian(R.results.bat[0,:])

   # The original and reconstructed Cartesian coordinates should all be close
   print(np.allclose(XYZ, selected_residues.positions, atol=1e-6))

Change a single torsion angle by :math:`\pi`::

   bat = R.results.bat[0,:]
   bat[bat.shape[0]-12] += np.pi
   XYZ = R.Cartesian(bat)

   # A good number of Cartesian coordinates should have been modified
   np.sum((XYZ - selected_residues.positions)>1E-5)

Store data to the disk and load it again::

   # BAT coordinates can be saved to disk in the numpy binary format
   R.save('test.npy')

   # The BAT coordinates in a new BAT instance can be loaded from disk
   # instead of using the run() method.
   Rnew = BAT(selected_residues, filename='test.npy')

   # The BAT coordinates before and after disk I/O should be close
   print(np.allclose(Rnew.results.bat, R.results.bat))


Analysis classes
----------------
.. autoclass:: BAT
    :members:
    :inherited-members:

    .. attribute:: results.bat

        Contains the time series of the Bond-Angle-Torsion coordinates as a
        (nframes, 3N) :class:`numpy.ndarray` array. Each row corresponds to
        a frame in the trajectory. In each column, the first six elements
        describe external degrees of freedom. The first three are the center
        of mass of the initial atom. The next three specify the  external angles
        according to the axis-angle convention: :math:`\phi`, the polar angle,
        :math:`\theta`, the azimuthal angle, and :math:`\omega`, a third angle
        that describes the rotation of the third atom about the axis. The next
        three degrees of freedom are internal degrees of freedom for the root
        atoms: :math:`r_{01}`, the distance between atoms 0 and 1,
        :math:`r_{12}`, the distance between atoms 1 and 2,
        and :math:`a_{012}`, the angle between the three atoms.
        The rest of the array consists of all the other bond distances,
        all the other bond angles, and then all the other torsion angles.


References
----------

.. footbibliography::

    N   )AnalysisBaseResultsGroup)
calc_bondscalc_anglescalc_dihedrals)
make_whole   )dueDoiFc                 (    t          | d |          S )aV  Sorts a list of atoms by mass and then by index

    The atom index is used as a tiebreaker so that the ordering is reproducible.

    Parameters
    ----------
    ag_o : list of Atoms
        List to sort
    reverse : bool
        Atoms will be in descending order

    Returns
    -------
    ag_n : list of Atoms
        Sorted list
    c                     | j         | j        fS N)massindex)as    a/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/analysis/bat.py<lambda>z%_sort_atoms_by_mass.<locals>.<lambda>   s    '8     )keyreversesorted)atomsr   s     r   _sort_atoms_by_massr      s    " %88'JJJJr   c                   	
 g }t          |           t                    t                    k     rAd}D ]
t          fd
j        D                       }|D ]	t          	fd
j        D                       }|D ]k}t          
fd|j        D                       }|D ]D}|                    t          j        	
||g                                         	           d}  |du r_t          d           t          d D                        t          d           t          d	 |D                        t          d
          t                    t                    k     A|S )a[  Constructs a list of torsion angles

    Parameters
    ----------
    root : AtomGroup
        First three atoms in the coordinate system
    atoms : AtomGroup
        Atoms that are allowed to be part of the torsion angle

    Returns
    -------
    torsions : list of AtomGroup
        list of AtomGroup objects that define torsion angles
    Fc              3   ,   K   | ]}|v |v
|V  d S r    ).0r   r   selected_atomss     r   	<genexpr>z!_find_torsions.<locals>.<genexpr>   sB       * *JJQn%<%< %<%<%<%<* *r   c              3   h   K   | ],}|k    r$t          |j                  d k    r|v r|v (|V  -dS )r   N)lenbonded_atoms)r   r   a0r   r    s     r   r!   z!_find_torsions.<locals>.<genexpr>   sc       . .RAN++a//en,, 
 -,,,. .r   c              3   8   K   | ]}|k    |v |v |V  d S r   r   )r   r   a1r   r    s     r   r!   z!_find_torsions.<locals>.<genexpr>   sJ       2 2GG!u**1;N;N ;N;N;N;N2 2r   TzSelected atoms:c                 "    g | ]}|j         d z   S r   r   r   r   s     r   
<listcomp>z"_find_torsions.<locals>.<listcomp>   s    777117Q;777r   zTorsions found:c                 <    g | ]}t          |j        d z             S r)   )listindices)r   ts     r   r,   z"_find_torsions.<locals>.<listcomp>   s&    99914	A&&999r   zAdditional torsions not found.)	r.   r#   r   r$   appendmda	AtomGroupprint
ValueError)rootr   torsionstorsionAddeda0_lista2_lista2a3_lista3r%   r'   r    s    `       @@@r   _find_torsionsr>      s    H$ZZN
n

E


*
*  "	 "	B) * * * * ** * *  G
    . . . . . . ._. . .   "  B 2 2 2 2 2 2 2!#2 2 2  G
 &   r2r26F(G(GHHH '--b111'+78 5  #$$$77777888#$$$99999:::=>>>U n

E


*
*V Or   c                        e Zd ZdZdZed             Z ej         e	d          dd          d fd		            Z
d
 Zd ZddZd Zd Zed             Zd Z xZS )BATa  Calculate BAT coordinates for the specified AtomGroup.

    Bond-Angle-Torsions (BAT) internal coordinates will be computed for
    the group of atoms and all frame in the trajectory belonging to `ag`.

    .. versionchanged:: 2.8.0
       Enabled **parallel execution** with the ``multiprocessing`` and ``dask``
       backends; use the new method :meth:`get_supported_backends` to see all
       supported backends.

    Tc                     dS )N)serialmultiprocessingdaskr   )clss    r   get_supported_backendszBAT.get_supported_backends  s    
 
r   z10.1002/jcc.26036z-Bond-Angle-Torsions Coordinate TransformationzMDAnalysis.analysis.bat.BAT)descriptionpathNc                      t          t                     j        |j        j        fi | | _        t           j        d          st          d          t           j        j	                  dk    rt          d          t          d  j        j        D             d          	d	         nvrt          d
          j        d	         } j        j        dk    r+t           fd|j        D             d          d	         }n)t           fd|j        D             d          d	         }t          j        ||g           _        t%           j         j                   _         fd j        D              _         fd j        D              _        d  j        D             fdt-          t                              D              _        t1          t3           j                             _        t          j        d  j        D                        _        t          j        d  j        D                        _        t          j        d  j        D                        _        t          j        d  j        D                        _        |                     |           dS dS )a  Parameters
        ----------
        ag : AtomGroup or Universe
            Group of atoms for which the BAT coordinates are calculated.
            `ag` must have a bonds attribute.
            If unavailable, bonds may be guessed using
            :meth:`AtomGroup.guess_bonds <MDAnalysis.core.groups.AtomGroup.guess_bonds>`.
            `ag` must only include one molecule.
            If a trajectory is associated with the atoms, then the computation
            iterates over the trajectory.
        initial_atom : :class:`Atom <MDAnalysis.core.groups.Atom>`
            The atom whose Cartesian coordinates define the translation
            of the molecule. If not specified, the heaviest terminal atom
            will be selected.
        filename : str
            Name of a numpy binary file containing a saved bat array.
            If filename is not ``None``, the data will be loaded from this file
            instead of being recalculated using the run() method.

        Raises
        ------
        AttributeError
            If `ag` does not contain a bonds attribute
        ValueError
            If `ag` contains more than one molecule

        bondsz AtomGroup has no attribute bondsr   z$AtomGroup has more than one moleculec                 B    g | ]}t          |j                  d k    |S r)   )r#   rJ   r+   s     r   r,   z BAT.__init__.<locals>.<listcomp>H  s*    <<<1#ag,,!*;*;Q*;*;*;r   T)r   Nr   z#Initial atom is not a terminal atom   c                 :    g | ]}|j         v r|k    r|v|S r   _ag)r   r   initial_atomselfterminal_atomss     r   r,   z BAT.__init__.<locals>.<listcomp>Y  sF       TXl**.00	  100r   c                 2    g | ]}|j         v |k    |S r   rN   )r   r   rP   rQ   s     r   r,   z BAT.__init__.<locals>.<listcomp>d  s8       TXA,=,= ,=,=,=r   c                 v    g | ]5}j         j        |j        k                                    d          d          6S r   rO   r/   r   nonzeror   r   rQ   s     r   r,   z BAT.__init__.<locals>.<listcomp>r  sH     
 
 
>?TX(1133A6q9
 
 
r   c                 ,    g | ]}fd |D             S )c                 v    g | ]5}j         j        |j        k                                    d          d          6S rU   rV   rX   s     r   r,   z+BAT.__init__.<locals>.<listcomp>.<listcomp>v  s;    FFFqdh!')2244Q7:FFFr   r   )r   r0   rQ   s     r   r,   z BAT.__init__.<locals>.<listcomp>u  s>     "
 "
 "
 GFFFAFFF"
 "
 "
r   c                 :    g | ]\  }}}}t          ||g          S r   r   )r   r%   r'   r;   r=   s        r   r,   z BAT.__init__.<locals>.<listcomp>|  s-    OOO,<RRvr2h''OOOr   c                 F    g | ]}                     |                   S r   r*   )r   nprior_atomss     r   r,   z BAT.__init__.<locals>.<listcomp>}  s8     )
 )
 )
23Kk!n--)
 )
 )
r   c                     g | ]
}|d          S rU   r   r   ags     r   r,   z BAT.__init__.<locals>.<listcomp>      "B"B"BR2a5"B"B"Br   c                     g | ]
}|d          S r)   r   r`   s     r   r,   z BAT.__init__.<locals>.<listcomp>  rb   r   c                     g | ]
}|d          S )r
   r   r`   s     r   r,   z BAT.__init__.<locals>.<listcomp>  rb   r   c                     g | ]
}|d          S )rL   r   r`   s     r   r,   z BAT.__init__.<locals>.<listcomp>  rb   r   ) superr@   __init__universe
trajectoryrO   hasattrAttributeErrorr#   	fragmentsr5   r   r   r$   n_atomsr2   r3   _rootr>   	_torsions_root_XYZ_inds_torsion_XYZ_indsrange_primary_torsion_indicesr.   set_unique_primary_torsion_indices_ag1_ag2_ag3_ag4load)
rQ   ra   rP   filenamekwargssecond_atom
third_atomr^   rR   	__class__s
   ` `    @@r   rg   zBAT.__init__  s_   B 	"c4!"+"8CCFCCC tx)) 	E !CDDDtx!""Q&&CDDD -<<<<<d
 
 
 )!,LL//BCCC #/2
 8q  ,     (5   	 	 	 	JJ -    (5  
    J ]L+z#JKK
 (
DH==
 
 
 
CG:
 
 
"
 "
 "
 "
^"
 "
 "
 POOOO)
 )
 )
 )
7<S=M=M7N7N)
 )
 )
% 04-..0
 0
, M"B"B4>"B"B"BCC	M"B"B4>"B"B"BCC	M"B"B4>"B"B"BCC	M"B"B4>"B"B"BCC	IIh  r   c                     t          j        | j        d| j        j        z  ft           j                  | j        _        d S )NrL   )dtype)npzerosn_framesrO   rm   float64resultsbatrQ   s    r   _preparezBAT._prepare  s;    8]A 001
 
 
r   c                    | j         j        | j         j        \  }}}nt          | j         d          \  }}}||z
  }||z
  }t	          j        t	          j        d||                    }t	          j        t	          j        d||                    }t	          j        t          dt          dt	          j        d||          t	          j        t	          j        d||          t	          j        d||          z            z                                }||z  }	t	          j
        |	d         |	d                   }
t	          j        |	d                   }t	          j        |
          }t	          j        |
          }t	          j        |          }t	          j        |          }t	          j        ||z  ||z  | g| |dg||z  ||z  |gg          }|                    ||z
            }t	          j
        |d         |d                   }t	          j        ||
|||||gf          }t!          | j        j        | j        j        | j        j        	          }t'          | j        j        | j        j        | j        j        | j        j        	          }t+          | j        j        | j        j        | j        j        | j        j        | j        j        	          }|| j                 }d
|| j        <   ||z  }|t          j        z   dt          j        z  z  t          j        z
  }t	          j        ||||f          | j        j        | j        d d f<   d S )NF)inplacei,i->g            ?r   r   r
   )box        )rn   
dimensions	positionsr	   r   sqrteinsumarccosmaxminarctan2cossinarraydotconcatenater   rv   rw   r   rx   r   ry   rs   ru   pir   r   _frame_index)rQ   p0p1p2v01v21r01r12a012ephithetacpspctstRzpos2omega
root_basedrJ   anglesr7   shifts                           r   _single_framezBAT._single_frame  s   
 : (:/LRRR%dj%@@@LRR2g2ggbic2233gbic2233 yIgsC00g	'344)GS#667  
 

 
 #Ij1qt$$	!A$VC[[VC[[VE]]VE]]X2grBw$sBlR"Wb2gr4JK
 
 vvb2g
47DG,,^R#ueS#t)L$MNN
 I!4$):N
 
 
 III	$	
 
 
 "IIII	$
 
 
 6769d23E%!be)4=1312
 2
*AAA-...r   c                 ~   t                               d           |                     | j        |||           t                               d           t	          j        |          | j        _        | j        j        j        | j	        d| j
        j        z  fk    rYd| j        j        j        d          d| j        j        j        d          d| j	        d| j
        j        z  f d	}t          |          | j        j        dd
df         | j        d         j        k                                    rt          d          | S )a  Loads the bat trajectory from a file in numpy binary format

        Parameters
        ----------
        filename : str
            name of numpy binary file
        start : int, optional
            start frame of analysis
        stop : int, optional
            stop frame of analysis
        step : int, optional
            number of frames to skip between each analysed frame

        See Also
        --------
        save: Saves the bat trajectory in a file in numpy binary format
        zChoosing frameszLoading filerL   z%Dimensions of array in loaded file, (r   ,r   z'), differ from required dimensions of ()NzXPosition of initial atom in file inconsistent with current trajectory in starting frame.)loggerinfo_setup_frames_trajectoryr   rz   r   r   shaper   rO   rm   r5   rn   positionany)rQ   r{   startstopsteperrmsgs         r   rz   zBAT.load  sN   $ 	%&&&4+UD$???N###78,, <!dmQ9I5I%JJJGL$*1-G G<#)!,G G #'-483C1C!DG G G  V$$$LQU#tz!}'==BBDD 	L   r   c                 D    t          j        || j        j                   dS )zSaves the bat trajectory in a file in numpy binary format

        See Also
        --------
        load: Loads the bat trajectory from a file in numpy binary format
        N)r   saver   r   )rQ   r{   s     r   r   zBAT.save  s!     	$,*+++++r   c           	      $   |dd         }|dd         \  }}}|dd         \  }}}| j         j        dz
  }	|d|	dz            }
||	dz   d|	z  dz            }t          j        |d|	z  dz   d                   }|| j                 }d|| j        <   ||z  }|t          j        z   dt          j        z  z  t          j        z
  }t          j        g d          }t          j        dd|g          }t          j        |t          j	        |          z  d||t          j
        |          z  z
  g          }t          j
        |          }t          j	        |          }t          j        || dg||dgg d	g          }|                    |          }t          j
        |          }t          j	        |          }t          j
        |          }t          j	        |          }t          j        ||z  | ||z  g||z  |||z  g| d|gg          }|                    |          }|                    |          }||z  }||z  }||z  }t          j        | j         j        df          }||| j        d         <   ||| j        d
         <   ||| j        d         <   t          | j        |
||          D ]b\  \  }}}}}}}||         }||         } ||         }t          j	        |          }!t          j
        |          }"t          j	        |          }#t          j
        |          }$||z
  }%|%t          j        t          j        d|%|%                    z  }%|| z
  }&|&t          j        t          j        d|&|&                    z  }&t          j        |&|%          }'t          j        d|%|&          }(t'          t          j        d|(|(z  z
            d          })|'|)z  }'t          j        |'|%          }*|||*|!z  |$z  |'|!z  |#z  z   |%|"z  z
  z  z   ||<   d|S )a<  Conversion of a single frame from BAT to Cartesian coordinates

        One application of this function is to determine the new
        Cartesian coordinates after modifying a specific torsion angle.

        Parameters
        ----------
        bat_frame : numpy.ndarray
            an array with dimensions (3N,) with external then internal
            degrees of freedom based on the root atoms, followed by the bond,
            angle, and (proper and improper) torsion coordinates.

        Returns
        -------
        XYZ : numpy.ndarray
            an array with dimensions (N,3) with Cartesian coordinates. The first
            dimension has the same ordering as the AtomGroup used to initialize
            the class. The molecule will be whole opposed to wrapped around a
            periodic boundary.
        NrL      	   r
   r   )r   r   r   r   )r   r   r   r   r   r   g|=)rO   rm   copydeepcopyrs   ru   r   r   r   r   r   r   r   rp   ziprq   r   r   crossr   )+rQ   	bat_frameoriginr   r   r   r   r   r   
n_torsionsrJ   r   r7   r   r   r   r   cosoRomegar   r   r   r   ReXYZr%   r'   r;   r=   angletorsionp3sn_angcs_angsn_torcs_torr   v32vpcssnvus+                                              r   	CartesianzBAT.Cartesian  s   , 2A2'!neU$QqS>c4X%)
!j1n,-:>A
NQ,>>?=1z>A+=+?+?!@AA6769d23E%!be)4= Xooo&&XsCo&&XsRVD\\)3cBF4LL6H0HIJJ VE]]VE]]BQ<"b!iii@AAZZ^^VC[[VC[[VE]]VE]]X2gsBG$rBwBG&<sArlK
 
 VVBZZVVBZZ
f
f
fh(!,--&(D"#&(D"#&(D"# 69"E686
 6
 	 	1RRc5' RBRBRBVE]]FVE]]FVG__FVG__Fr'C2729Wc377888Cr'C2729Wc377888C#s##B7C--BRWS27]++\::BbB"c""B3Vf$rF{V';;cFlJ CGG 
r   c                     | j         S )z=The atomgroup for which BAT are computed (read-only property)rN   r   s    r   r   z	BAT.atomsq  s     xr   c                 :    t          dt           j        i          S )Nr   )lookup)r   ndarray_vstackr   s    r   _get_aggregatorzBAT._get_aggregatorv  s    E<+F#GHHHHr   )NN)NNN)__name__
__module____qualname____doc__%_analysis_algorithm_is_parallelizableclassmethodrF   r   dciter   rg   r   r   rz   r   r   propertyr   r   __classcell__)r   s   @r   r@   r@     s/       
 
 -1)
 
 [
 SY  C*  
j  j  j  j  j  
j X
 
 

I
 I
 I
V' ' ' 'R, , ,a a aF   XI I I I I I Ir   r@   )F)r   loggingwarningsnumpyr   r   
MDAnalysisr2   baser   r   MDAnalysis.lib.distancesr   r   r   MDAnalysis.lib.mdamathr	   r   r   	getLoggerr   r   r   r>   r@   r   r   r   <module>r      s)  .H HR            , , , , , , , , L L L L L L L L L L - - - - - -        		8	$	$K K K K(< < <~sI sI sI sI sI, sI sI sI sI sIr   