
     i(                     r    d Z ddlZddlmZ ddlmZmZ ddlm	Z	  G d d	e	          Z
 G d
 de	          ZdS )a8  Fitting transformations --- :mod:`MDAnalysis.transformations.fit`
=================================================================

Translate and/or rotates the coordinates of a given trajectory to align
a given AtomGroup to a reference structure.

.. autoclass:: fit_translation

.. autoclass:: fit_rot_trans

    N   )align)euler_from_matrixeuler_matrix   )TransformationBasec                   2     e Zd ZdZ	 	 	 	 d fd	Zd Z xZS )fit_translationa  Translates a given AtomGroup so that its center of geometry/mass matches
    the respective center of the given reference. A plane can be given by the
    user using the option `plane`, and will result in the removal of
    the translation motions of the AtomGroup over that particular plane.

    Example
    -------
    Removing the translations of a given AtomGroup `ag` on the XY plane by
    fitting its center of mass to the center of mass of a reference `ref`:

    .. code-block:: python

        ag = u.select_atoms("protein")
        ref = mda.Universe("reference.pdb")
        transform = mda.transformations.fit_translation(ag, ref, plane="xy",
                                                        weights="mass")
        u.trajectory.add_transformations(transform)

    Parameters
    ----------
    ag : Universe or AtomGroup
       structure to translate, a
       :class:`~MDAnalysis.core.groups.AtomGroup` or a whole
       :class:`~MDAnalysis.core.universe.Universe`
    reference : Universe or AtomGroup
       reference structure, a :class:`~MDAnalysis.core.groups.AtomGroup` or a
       whole :class:`~MDAnalysis.core.universe.Universe`
    plane: str, optional
        used to define the plane on which the translations will be removed.
        Defined as a string of the plane.
        Suported planes are yz, xz and xy planes.
    weights : {"mass", ``None``} or array_like, optional
       choose weights. With ``"mass"`` uses masses as weights; with ``None``
       weigh each atom equally. If a float array of the same length as
       `ag` is provided, use each element of the `array_like` as a
       weight for the corresponding atom in `ag`.

    Returns
    -------
    MDAnalysis.coordinates.timestep.Timestep


    .. versionchanged:: 2.0.0
       The transformation was changed from a function/closure to a class
       with ``__call__``.
    .. versionchanged:: 2.0.0
       The transformation was changed to inherit from the base class for
       limiting threads and checking if it can be used in parallel analysis.
    NTc                    t                                          ||           || _        || _        || _        || _        | j        Gdddd}	 || j                 | _        n-# t          t          f$ r t          | j         d          d w xY w	 | j        j	        j
        | j        j	        j
        k    r!| j         d| j         d}t          |          n0# t          $ r# | j         d	| j         d
}t          |          d w xY wt          j        | j        j	        | j        j	                  \  | _        | _        t          j        | j        j	        | j                  | _        | j                            | j                  | _        d S )Nmax_threadsparallelizabler   r   r   yzxzxy is not a valid plane and z" have mismatchednumber of residues or z is not validUniverse/AtomGroupweights)super__init__ag	referenceplaner   	TypeErrorKeyError
ValueErroratoms
n_residuesAttributeErrorr   get_matching_atomsrefmobileget_weightscenterref_com
selfr   r   r   r   r   r   axeserrmsg	__class__s
            h/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/transformations/fit.pyr   zfit_translation.__init__^   s    	#N 	 	
 	
 	
 "
:!1A..D!$*-

x(    z888 	3w}'4>+?+JJJw * *T^ * * * 
 !((( K  	3 	3 	37 & & & & &  !((d2	3 !& 8N $'-!
 !
$+ (NNNxt|44   A" "*BAC -Dc                     t          j        | j        j                            | j                  t           j                  }| j        |z
  }| j        
d|| j        <   |xj	        |z  c_	        |S )Nr   )
npasarrayr%   r    r'   r   float32r(   r   	positions)r*   ts
mobile_comvectors       r.   
_transformzfit_translation._transform   sf    ZK$$T\22BJ
 

 
*:!!"F4:
	    )NNNT__name__
__module____qualname____doc__r   r8   __classcell__r-   s   @r.   r
   r
   +   sf        0 0l ,5 ,5 ,5 ,5 ,5 ,5\	 	 	 	 	 	 	r9   r
   c                   2     e Zd ZdZ	 	 	 	 d fd	Zd Z xZS )fit_rot_transaf	  Perform a spatial superposition by minimizing the RMSD.

    Spatially align the group of atoms `ag` to `reference` by doing a RMSD
    fit.

    This fit works as a way to remove translations and rotations of a given
    AtomGroup in a trajectory. A plane can be given using the flag `plane`
    so that only translations and rotations in that particular plane are
    removed. This is useful for protein-membrane systems to where the membrane
    must remain in the same orientation.

    Note
    ----
    ``max_threads`` is set to 1 for this transformation
    with which it performs better.

    Example
    -------
    Removing the translations and rotations of a given AtomGroup `ag` on the XY plane
    by fitting it to a reference `ref`, using the masses as weights for the RMSD fit:

    .. code-block:: python

        ag = u.select_atoms("protein")
        ref = mda.Universe("reference.pdb")
        transform = mda.transformations.fit_rot_trans(ag, ref, plane="xy",
                                                      weights="mass")
        u.trajectory.add_transformations(transform)

    Parameters
    ----------
    ag : Universe or AtomGroup
       structure to translate and rotate, a
       :class:`~MDAnalysis.core.groups.AtomGroup` or a whole
       :class:`~MDAnalysis.core.universe.Universe`
    reference : Universe or AtomGroup
       reference structure, a :class:`~MDAnalysis.core.groups.AtomGroup` or a whole
       :class:`~MDAnalysis.core.universe.Universe`
    plane: str, optional
        used to define the plane on which the rotations and translations will be removed.
        Defined as a string of the plane. Supported planes are "yz", "xz" and "xy" planes.
    weights : {"mass", ``None``} or array_like, optional
       choose weights. With ``"mass"`` uses masses as weights; with ``None``
       weigh each atom equally. If a float array of the same length as
       `ag` is provided, use each element of the `array_like` as a
       weight for the corresponding atom in `ag`.

    Returns
    -------
    MDAnalysis.coordinates.timestep.Timestep


    .. versionchanged:: 2.0.0
       The transformation was changed from a function/closure to a class
       with ``__call__``.
    .. versionchanged:: 2.0.0
       The transformation was changed to inherit from the base class for
       limiting threads and checking if it can be used in parallel analysis.
    Nr   Tc                 T   t                                          ||           || _        || _        || _        || _        | j        Gdddd}	 || j                 | _        n-# t          t          f$ r t          | j         d          d w xY w	 | j        j	        j
        | j        j	        j
        k    r!| j         d| j         d}t          |          n0# t          $ r# | j         d	| j         d
}t          |          d w xY wt          j        | j        j	        | j        j	                  \  | _        | _        t          j        | j        j	        | j                  | _        | j                            | j                  | _        | j        j	        j        | j        z
  | _        d S )Nr   r   r   r   r   r   r   z# have mismatched number of residuesr   z  is not valid Universe/AtomGroupr   )r   r   r   r   r   r   r   r   r   r    r!   r"   r   r#   r$   r%   r&   r'   r(   r4   ref_coordinatesr)   s
            r.   r   zfit_rot_trans.__init__   s    	#N 	 	
 	
 	
 "
:!1A..D!$*-

x(    z888 	3w}'4>+?+JJJw * *T^ * * *  !((( K  	3 	3 	37 & & & & &  !((d2	3 !& 8N $'-!
 !
$+ (NNNxt|44#x~7$,Fr/   c                 r   | j         j                            | j                  }| j         j        j        |z
  }t          j        || j        | j                  \  }}| j        }| j	        	t          j        |t          j        d                              dd          f         }t          j        |t          j        d          f         }t          j        t!          |d          t          j                  }t%          d|j                  D ]}	|	| j	        k    r|| j	                 nd||	<    t)          |d         |d         |d         d          d dd df         }|| j	                 || j	        <   |j        |z
  |_        t          j        |j        |j                  |_        |j        |z   |_        |S )	Nr      r      sxyz)r+   r   r   )r%   r    r'   r   r4   r   rotation_matrixrD   r(   r   r1   r_zerosreshapec_r2   r   r3   rangesizer   dotT)
r*   r5   r6   mobile_coordinatesrotationdumpr7   matrix
euler_angsis
             r.   r8   zfit_rot_trans._transform  s   [&--dl;;
![.8:E. 4dl
 
 
$ :!U8RXa[[%8%8A%>%>>?FU628A;;./F!&v666
 J 1jo..  ./4:ooJtz**1 1 $1z!}jm&  qb"1"fH ",DJ!7F4:|j0vblHJ77|f,	r9   )NNr   Tr:   r@   s   @r.   rB   rB      sl        : :@ ,G ,G ,G ,G ,G ,G\      r9   rB   )r>   numpyr1   analysisr   lib.transformationsr   r   baser   r
   rB    r9   r.   <module>r]      s   .            A A A A A A A A $ $ $ $ $ $j j j j j( j j jZC C C C C& C C C C Cr9   