
     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Z	ddl
Z	ddlZ	dZn# e$ r dZY nw xY wddlZddlmc mZ ddlmZmZ ddlmc mZ ddlmZ ddlmZ ddlmZ dd	lm Z  d
dl!m!Z!m"Z" ddl#m$Z$  ej%        d          Z&d+dZ'	 d+dZ(	 	 	 	 	 	 d,dZ) e!j*         e"d          dd          	 	 	 	 	 	 d-d            Z+ G d de$          Z, G d de$          Z- edd d!"          	 	 	 	 d.d&            Z.	 	 	 	 	 	 	 	 	 d/d)Z/d0d*Z0dS )1a1  Coordinate fitting and alignment --- :mod:`MDAnalysis.analysis.align`
=====================================================================

:Author: Oliver Beckstein, Joshua Adelman
:Year: 2010--2013
:Copyright: Lesser GNU Public License v2.1+

The module contains functions to fit a target structure to a reference
structure. They use the fast QCP algorithm to calculate the root mean
square distance (RMSD) between two coordinate sets [Theobald2005]_ and
the rotation matrix *R* that minimizes the RMSD [Liu2010]_. (Please
cite these references when using this module.).

Typically, one selects a group of atoms (such as the C-alphas),
calculates the RMSD and transformation matrix, and applys the
transformation to the current frame of a trajectory to obtain the
rotated structure. The :func:`alignto` and :class:`AlignTraj`
functions can be used to do this for individual frames and
trajectories respectively.

The :ref:`RMS-fitting-tutorial` shows how to do the individual steps
manually and explains the intermediate steps.

See Also
--------
:mod:`MDAnalysis.analysis.rms`
     contains functions to compute RMSD (when structural alignment is not
     required)
:mod:`MDAnalysis.lib.qcprot`
     implements the fast RMSD algorithm.


.. _RMS-fitting-tutorial:

RMS-fitting tutorial
--------------------

The example uses files provided as part of the MDAnalysis test suite
(in the variables :data:`~MDAnalysis.tests.datafiles.PSF`,
:data:`~MDAnalysis.tests.datafiles.DCD`, and
:data:`~MDAnalysis.tests.datafiles.PDB_small`). For all further
examples execute first ::

   >>> import MDAnalysis as mda
   >>> from MDAnalysis.analysis import align
   >>> from MDAnalysis.analysis.rms import rmsd
   >>> from MDAnalysis.tests.datafiles import PSF, DCD, PDB_small


In the simplest case, we can simply calculate the C-alpha RMSD between
two structures, using :func:`rmsd`::

   >>> ref = mda.Universe(PDB_small)
   >>> mobile = mda.Universe(PSF, DCD)
   >>> rmsd(mobile.select_atoms('name CA').positions, ref.select_atoms('name CA').positions)
   28.20178579474479

Note that in this example translations have not been removed. In order
to look at the pure rotation one needs to superimpose the centres of
mass (or geometry) first::

   >>> rmsd(mobile.select_atoms('name CA').positions, ref.select_atoms('name CA').positions, center=True)
   21.892591663632704

This has only done a translational superposition. If you want to also do a
rotational superposition use the superposition keyword. This will calculate a
minimized RMSD between the reference and mobile structure::

   >>> rmsd(mobile.select_atoms('name CA').positions, ref.select_atoms('name CA').positions, 
   ...      superposition=True)
   6.809396586471815

The rotation matrix that superimposes *mobile* on *ref* while
minimizing the CA-RMSD is obtained with the :func:`rotation_matrix`
function ::

   >>> mobile0 = mobile.select_atoms('name CA').positions - mobile.select_atoms('name CA').center_of_mass()
   >>> ref0 = ref.select_atoms('name CA').positions - ref.select_atoms('name CA').center_of_mass()
   >>> R, rmsd = align.rotation_matrix(mobile0, ref0)
   >>> rmsd
   6.809396586471805
   >>> R
   array([[ 0.14514539, -0.27259113,  0.95111876],
   ...    [ 0.88652593,  0.46267112, -0.00268642],
   ...    [-0.43932289,  0.84358136,  0.30881368]])

Putting all this together one can superimpose all of *mobile* onto *ref*::

   >>> mobile.atoms.translate(-mobile.select_atoms('name CA').center_of_mass())
   <AtomGroup with 3341 atoms>
   >>> mobile.atoms.rotate(R)
   <AtomGroup with 3341 atoms>
   >>> mobile.atoms.translate(ref.select_atoms('name CA').center_of_mass())
   <AtomGroup with 3341 atoms>
   >>> mobile.atoms.write("mobile_on_ref.pdb")


Common usage
------------

To **fit a single structure** with :func:`alignto`::

   >>> ref = mda.Universe(PSF, PDB_small)
   >>> mobile = mda.Universe(PSF, DCD)     # we use the first frame
   >>> align.alignto(mobile, ref, select="protein and name CA", weights="mass")
   (21.892591663632704, 6.809396586471809)

This will change *all* coordinates in *mobile* so that the protein
C-alpha atoms are optimally superimposed (translation and rotation).

To **fit a whole trajectory** to a reference structure with the
:class:`AlignTraj` class::

   >>> ref = mda.Universe(PSF, PDB_small)   # reference structure 1AKE
   >>> trj = mda.Universe(PSF, DCD)         # trajectory of change 1AKE->4AKE
   >>> alignment = align.AlignTraj(trj, ref, filename='rmsfit.dcd')
   >>> alignment.run()
   <MDAnalysis.analysis.align.AlignTraj object at ...> 

It is also possible to align two arbitrary structures by providing a
mapping between atoms based on a sequence alignment. This allows
fitting of structural homologs or wild type and mutant.

If a alignment was provided as "sequences.aln" one would first produce
the appropriate MDAnalysis selections with the :func:`fasta2select`
function and then feed the resulting dictionary to :class:`AlignTraj`::

   >>> seldict = align.fasta2select('sequences.aln') # doctest: +SKIP
   >>> alignment = align.AlignTraj(trj, ref, filename='rmsfit.dcd', select=seldict) # doctest: +SKIP
   >>> alignment.run() # doctest: +SKIP

(See the documentation of the functions for this advanced usage.)


Functions and Classes
---------------------

.. versionchanged:: 0.10.0
   Function :func:`~MDAnalysis.analysis.rms.rmsd` was removed from
   this module and is now exclusively accessible as
   :func:`~MDAnalysis.analysis.rms.rmsd`.

.. versionchanged:: 0.16.0
   Function :func:`~MDAnalysis.analysis.align.rms_fit_trj` deprecated
   in favor of :class:`AlignTraj` class.

.. versionchanged:: 0.17.0
   removed deprecated :func:`~MDAnalysis.analysis.align.rms_fit_trj`

.. autofunction:: alignto
.. autoclass:: AlignTraj
.. autoclass:: AverageStructure
.. autofunction:: rotation_matrix
.. autofunction:: iterative_average


Helper functions
----------------

The following functions are used by the other functions in this
module. They are probably of more interest to developers than to
normal users.

.. autofunction:: _fit_to
.. autofunction:: fasta2select
.. autofunction:: sequence_alignment
.. autofunction:: get_matching_atoms

    NTF)SelectionErrorSelectionWarning)MemoryReader)get_weights)	deprecate)ProgressBar   )dueDoi   )AnalysisBasezMDAnalysis.analysis.alignc                 Z   t          j        | t           j                  } t          j        |t           j                  }| j        |j        k    rt	          d          t          j        | |          r$|"t          j        dt           j                  dfS |j        d         }|5t          j        |t           j                  t          j        |          z  }t          j        dt           j                  }t          j
        | ||||          }|                    dd          |fS )a_  Returns the 3x3 rotation matrix `R` for RMSD fitting coordinate
    sets `a` and `b`.

    The rotation matrix `R` transforms vector `a` to overlap with
    vector `b` (i.e., `b` is the reference structure):

    .. math::
       \mathbf{b} = \mathsf{R} \cdot \mathbf{a}

    Parameters
    ----------
    a : array_like
        coordinates that are to be rotated ("mobile set"); array of N atoms
        of shape N*3 as generated by, e.g.,
        :attr:`MDAnalysis.core.groups.AtomGroup.positions`.
    b : array_like
        reference coordinates; array of N atoms of shape N*3 as generated by,
        e.g., :attr:`MDAnalysis.core.groups.AtomGroup.positions`.
    weights : array_like (optional)
        array of floats of size N for doing weighted RMSD fitting (e.g. the
        masses of the atoms)

    Returns
    -------
    R : ndarray
        rotation matrix
    rmsd : float
        RMSD between `a` and `b` before rotation
    ``(R, rmsd)`` rmsd and rotation matrix *R*

    Example
    -------
    `R` can be used as an argument for
    :meth:`MDAnalysis.core.groups.AtomGroup.rotate` to generate a rotated
    selection, e.g. ::

        >>> from MDAnalysisTests.datafiles import TPR, TRR
        >>> from MDAnalysis.analysis import align
        >>> A = mda.Universe(TPR,TRR)
        >>> B = A.copy()
        >>> R = rotation_matrix(A.select_atoms('backbone').positions,
        ...                     B.select_atoms('backbone').positions)[0]
        >>> A.atoms.rotate(R)
        <AtomGroup with 47681 atoms>
        >>> A.atoms.write("rotated.pdb")

    Notes
    -----
    The function does *not* shift the centers of mass or geometry;
    this needs to be done by the user.

    See Also
    --------
    MDAnalysis.analysis.rms.rmsd: Calculates the RMSD between *a* and *b*.
    alignto: A complete fit of two structures.
    AlignTraj: Fit a whole trajectory.
    dtypez 'a' and 'b' must have same shapeN   g        r   	   )npasarrayfloat64shape
ValueErrorallcloseeyemeanzerosqcpCalcRMSDRotationalMatrixreshape)abweightsNrotrmsds         c/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/analysis/align.pyrotation_matrixr&      s   v 	
1BJ'''A

1BJ'''Aw!';<<<	{1a 0W_varz***C//	
A*WBJ777"'':J:JJ
(1BJ
'
'
'C '1ag>>D;;q!d""    c                     t          | ||          \  }}|                    |            |                    |           |                    |           ||fS )ad  Perform an rmsd-fitting to determine rotation matrix and align atoms

    Parameters
    ----------
    mobile_coordinates : ndarray
        Coordinates of atoms to be aligned
    ref_coordinates : ndarray
        Coordinates of atoms to be fit against
    mobile_atoms : AtomGroup
        Atoms to be translated
    mobile_com: ndarray
        array of xyz coordinate of mobile center of mass
    ref_com : ndarray
        array of xyz coordinate of reference center of mass
    weights : array_like (optional)
       choose weights. With ``None`` weigh each atom equally. If a float array
       of the same length as `mobile_coordinates` is provided, use each element
       of the `array_like` as a weight for the corresponding atom in
       `mobile_coordinates`.

    Returns
    -------
    mobile_atoms : AtomGroup
        AtomGroup of translated and rotated atoms
    min_rmsd : float
        Minimum rmsd of coordinates

    Notes
    -----
    This function assumes that `mobile_coordinates` and `ref_coordinates` have
    already been shifted so that their centers of geometry (or centers of mass,
    depending on `weights`) coincide at the origin. `mobile_com` and `ref_com`
    are the centers *before* this shift.

    1. The rotation matrix :math:`\mathsf{R}` is determined with
       :func:`rotation_matrix` directly from `mobile_coordinates` and
       `ref_coordinates`.
    2. `mobile_atoms` :math:`X` is rotated according to the
       rotation matrix and the centers according to

       .. math::

           X' = \mathsf{R}(X - \bar{X}) + \bar{X}_{\text{ref}}

       where :math:`\bar{X}` is the center.

    r!   )r&   	translaterotate)mobile_coordinatesref_coordinatesmobile_atoms
mobile_comref_comr!   Rmin_rmsds           r%   _fit_tor3   5  so    n "OW  KAx J;'''7###!!r'   皙?c                    |dv r| j         }|j         }	n4t          j        |          } | j        |d          } |j        |d          }	t	          |	||||          \  }	}t          |	|          }|                    |          }
|	                    |          }|	j        |z
  }|j        |
z
  }t          j        |||          }|| j	        j         }nTt          |t                    r|                     |          }n)	 |j         }n # t          $ r d}t          |          dw xY wt          ||||
||          \  }}||fS )a  Perform a spatial superposition by minimizing the RMSD.

    Spatially align the group of atoms `mobile` to `reference` by
    doing a RMSD fit on `select` atoms.

    The superposition is done in the following way:

    1. A rotation matrix is computed that minimizes the RMSD between
       the coordinates of `mobile.select_atoms(sel1)` and
       `reference.select_atoms(sel2)`; before the rotation, `mobile` is
       translated so that its center of geometry (or center of mass)
       coincides with the one of `reference`. (See below for explanation of
       how *sel1* and *sel2* are derived from `select`.)

    2. All atoms in :class:`~MDAnalysis.core.universe.Universe` that
       contain `mobile` are shifted and rotated. (See below for how
       to change this behavior through the `subselection` keyword.)

    The `mobile` and `reference` atom groups can be constructed so that they
    already match atom by atom. In this case, `select` should be set to "all"
    (or ``None``) so that no further selections are applied to `mobile` and
    `reference`, therefore preserving the exact atom ordering (see
    :ref:`ordered-selections-label`).

    .. Warning:: The atom order for `mobile` and `reference` is *only*
       preserved when `select` is either "all" or ``None``. In any other case,
       a new selection will be made that will sort the resulting AtomGroup by
       index and therefore destroy the correspondence between the two groups.
       **It is safest not to mix ordered AtomGroups with selection strings.**

    Parameters
    ----------
    mobile : Universe or AtomGroup
       structure to be aligned, 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`
    select : str or dict or tuple (optional)
       The selection to operate on; can be one of:

       1. any valid selection string for
          :meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` that
          produces identical selections in `mobile` and `reference`; or

       2. a dictionary ``{'mobile': sel1, 'reference': sel2}`` where *sel1*
          and *sel2* are valid selection strings that are applied to
          `mobile` and `reference` respectively (the
          :func:`MDAnalysis.analysis.align.fasta2select` function returns such
          a dictionary based on a ClustalW_ or STAMP_ sequence alignment); or

       3. a tuple ``(sel1, sel2)``

       When using 2. or 3. with *sel1* and *sel2* then these selection strings
       are applied to `atomgroup` and `reference` respectively and should
       generate *groups of equivalent atoms*.  *sel1* and *sel2* can each also
       be a *list of selection strings* to generate a
       :class:`~MDAnalysis.core.groups.AtomGroup` with defined atom order as
       described under :ref:`ordered-selections-label`).
    match_atoms : bool (optional)
        Whether to match the mobile and reference atom-by-atom. Default ``True``.
    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
       `mobile` is provided, use each element of the `array_like` as a
       weight for the corresponding atom in `mobile`.
    tol_mass: float (optional)
       Reject match if the atomic masses for matched atoms differ by more than
       `tol_mass`, default [0.1]
    strict: bool (optional)
       ``True``
           Will raise :exc:`SelectionError` if a single atom does not
           match between the two selections.
       ``False`` [default]
           Will try to prepare a matching selection by dropping
           residues with non-matching atoms. See :func:`get_matching_atoms`
           for details.
    subselection : str or AtomGroup or None (optional)
       Apply the transformation only to this selection.

       ``None`` [default]
           Apply to ``mobile.universe.atoms`` (i.e., all atoms in the
           context of the selection from `mobile` such as the rest of a
           protein, ligands and the surrounding water)
       *selection-string*
           Apply to ``mobile.select_atoms(selection-string)``
       :class:`~MDAnalysis.core.groups.AtomGroup`
           Apply to the arbitrary group of atoms

    Returns
    -------
    old_rmsd : float
        RMSD before spatial alignment
    new_rmsd : float
        RMSD after spatial alignment

    See Also
    --------
    AlignTraj: More efficient method for RMSD-fitting trajectories.


    .. _ClustalW: http://www.clustal.org/
    .. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/

    .. versionchanged:: 1.0.0
       Added *match_atoms* keyword to toggle atom matching.

    .. versionchanged:: 0.8
       Added check that the two groups describe the same atoms including
       the new *tol_mass* keyword.

    .. versionchanged:: 0.10.0
       Uses :func:`get_matching_atoms` to work with incomplete selections
       and new `strict` keyword. The new default is to be lenient whereas
       the old behavior was the equivalent of ``strict = True``.

    .. versionchanged:: 0.16.0
       new general 'weights' kwarg replace `mass_weighted`, deprecated `mass_weighted`
    .. deprecated:: 0.16.0
       Instead of ``mass_weighted=True`` use new ``weights='mass'``

    .. versionchanged:: 0.17.0
       Deprecated keyword `mass_weighted` was removed.
    )allNmobile	referencetol_massstrictmatch_atomsNzIsubselection must be a selection string, an AtomGroup or Universe or Noner)   )atomsrmsprocess_selectionselect_atomsget_matching_atomsr   center	positionsr$   universe
isinstancestrAttributeError	TypeErrorr3   )r7   r8   selectr!   subselectionr:   r;   r<   r.   	ref_atomsr/   r0   r-   r,   old_rmsderrnew_rmsds                    r%   aligntorO   w  s   N  |O		&v..*v*F8,<=*I*F;,?@	0  I| )W--G$$W--Jw''G)G3O%/*<x*OWEEH,	L#	&	& +**<88	+'-LL 	+ 	+ 	+1  C..d*	+ %  L( Xs   >D D#z10.1021/acs.jpcb.7b11988z)Iterative Calculation of Opimal Referencez+MDAnalysis.analysis.align.iterative_average)descriptionpathr6   d   ư>c           
         |s| }t          j        |          }t          j         |j        |d                    }|d         d         }	t          |j        |          }t          j        }
t          t          |                    D ]}|
|k     r nt          | f||	dd|d|                                }t          j        |j        j        |j        j        |          }
|j        j        }|r/t"                              d| d	|
d
d|j        j        d
           d| d| d|j        j        d
}t"                              |           t)          |          t"                              d| d|j        j        d
           |S )a  Iteratively calculate an optimal reference that is also the average
    structure after an RMSD alignment.

    The optimal reference is defined as average
    structure of a trajectory, with the optimal reference used as input.
    This function computes the optimal reference by using a starting
    reference for the average structure, which is used as the reference
    to calculate the average structure again. This is repeated until the
    reference structure has converged. :footcite:p:`Linke2018`

    Parameters
    ----------
    mobile : mda.Universe
        Universe containing trajectory to be fitted to reference.
    reference : mda.Universe (optional)
        Universe containing the initial reference structure.
    select : str or tuple or dict (optional)
        Atom selection for fitting a substructue. Default is set to all.
        Can be tuple or dict to define different selection strings for
        mobile and target.
    weights : str, array_like (optional)
        Weights that can be used. If `None` use equal weights, if `'mass'`
        use masses of ref as weights or give an array of arbitrary weights.
    niter : int (optional)
        Maximum number of iterations.
    eps : float (optional)
        RMSD distance at which reference and average are assumed to be
        equal.
    verbose : bool (optional)
        Verbosity.
    **kwargs : dict (optional)
        AverageStructure kwargs.

    Returns
    -------
    avg_struc : AverageStructure
        AverageStructure result from the last iteration.

    Example
    -------
    `iterative_average` can be used to obtain a :class:`MDAnalysis.Universe`
    with the optimal reference structure.

    ::

        import MDAnalysis as mda
        from MDAnalysis.analysis import align
        from MDAnalysisTests.datafiles import PSF, DCD

        u = mda.Universe(PSF, DCD)
        av = align.iterative_average(u, u, verbose=True)

        averaged_universe = av.results.universe

    References
    ----------

    .. footbibliography::

    .. versionadded:: 2.8.0
    r8   r7   r   r6   )r7   r8   )r8   rI   r!   r)   ziterative_average(): i = z, rmsd-change = z.5fz, ave-rmsd = z)iterative_average(): Did not converge in z iterations to DRMSD < z. Final average RMSD = z*iterative_average(): Converged to DRMSD < )r>   r?   mdaMerger@   r   r=   r   infr   rangeAverageStructurerunr$   rC   resultsrD   loggerdebugerrorRuntimeErrorinfo)r7   r8   rI   r!   niterepsverbosekwargsref
sel_mobiledrmsdi	avg_strucerrmsgs                 r%   iterative_averagerk   8  s   X  	"6**F
)*I*F;,?@
A
AC!!$J#)W--GFEu&& # #3;;E$
(u==	
 

 
 
 #%% 	 I!2!<g
 
 
 ( 	LL;A ; ;!&.; ;'/4:; ;  AA A-0A A$-$5$:@A A 	
 	V6"""
KK	=S 	= 	= ) 1 6<	= 	=  
 r'   c                   `     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d fd	Zd	 Zd
 Zd Zed             Z	 xZ
S )	AlignTraja  RMS-align trajectory to a reference structure using a selection.

    Both the reference `reference` and the trajectory `mobile` must be
    :class:`MDAnalysis.Universe` instances. If they contain a trajectory then
    it is used. The output file format is determined by the file extension of
    `filename`. One can also use the same universe if one wants to fit to the
    current frame.

    .. versionchanged:: 1.0.0
       ``save()`` has now been removed, as an alternative use ``np.savetxt()``
       on :attr:`results.rmsd`.

    r6   Nrmsfit_r4   TFc                 b   t          j        |          } |j        |d          | _         |j        |d          | _        |st          |j        t                    r1|                                 d}t          
                    d           n|at          j                            |j        j                  d         }||z   }t          
                    d                    |                     t          j                            |          r|
st#          d           t%          t&          |           j        |j        fi | | j        st-          j        t,          j                   |j        | _        || _        | j        j        }t9          | j        | j        ||	|          \  | _        | _        |i }t;          j        | j        |fi || _        tA          | j        |          | _!        t          
                    d	                    tE          | j                                       dS )
a  Parameters
        ----------
        mobile : Universe
            Universe containing trajectory to be fitted to reference
        reference : Universe
            Universe containing trajectory frame to be used as reference
        select : str (optional)
            Set as default to all, is used for Universe.select_atoms to choose
            subdomain to be fitted against
        filename : str (optional)
            Provide a filename for results to be written to
        prefix : str (optional)
            Provide a string to prepend to filename for results to be written
            to
        weights : {"mass", ``None``} or array_like (optional)
            choose weights. With ``"mass"`` uses masses of `reference` as
            weights; with ``None`` weigh each atom equally. If a float array of
            the same length as the selection is provided, use each element of
            the `array_like` as a weight for the corresponding atom in the
            selection.
        tol_mass : float (optional)
            Tolerance given to `get_matching_atoms` to find appropriate atoms
        match_atoms : bool (optional)
            Whether to match the mobile and reference atom-by-atom. Default ``True``.
        strict : bool (optional)
            Force `get_matching_atoms` to fail if atoms can't be found using
            exact methods
        force : bool (optional)
            Force overwrite of filename for rmsd-fitting
        in_memory : bool (optional)
            *Permanently* switch `mobile` to an in-memory trajectory
            so that alignment can be done in-place, which can improve
            performance substantially in some cases. In this case, no file
            is written out (`filename` and `prefix` are ignored) and only
            the coordinates of `mobile` are *changed in memory*.
        verbose : bool (optional)
             Set logger to show more information and show detailed progress of
             the calculation if set to ``True``; the default is ``False``.
        writer_kwargs : dict (optional)
            kwarg dict to be passed to the constructed writer


        Attributes
        ----------
        reference_atoms : AtomGroup
            Atoms of the reference structure to be aligned against
        mobile_atoms : AtomGroup
            Atoms inside each trajectory frame to be rmsd_aligned
        results.rmsd : :class:`numpy.ndarray`
            Array of the rmsd values of the least rmsd between the mobile_atoms
            and reference_atoms after superposition and minimimization of rmsd

            .. versionadded:: 2.0.0

        rmsd : :class:`numpy.ndarray`
            Alias to the :attr:`results.rmsd` attribute.

            .. deprecated:: 2.0.0
               Will be removed in MDAnalysis 3.0.0. Please use
               :attr:`results.rmsd` instead.

        filename : str
            String reflecting the filename of the file where the aligned
            positions will be written to upon running RMSD alignment


        Notes
        -----
        - If set to ``verbose=False``, it is recommended to wrap the statement
          in a ``try ...  finally`` to guarantee restoring of the log level in
          the case of an exception.
        - The ``in_memory`` option changes the `mobile` universe to an
          in-memory representation (see :mod:`MDAnalysis.coordinates.memory`)
          for the remainder of the Python session. If ``mobile.trajectory`` is
          already a :class:`MemoryReader` then it is *always* treated as if
          ``in_memory`` had been set to ``True``.

        .. versionchanged:: 1.0.0
           Default ``filename`` has now been changed to the current directory.

        .. deprecated:: 0.19.1
           Default ``filename`` directory will change in 1.0 to the current directory.

        .. versionchanged:: 0.16.0
           new general ``weights`` kwarg replace ``mass_weights``

        .. deprecated:: 0.16.0
           Instead of ``mass_weighted=True`` use new ``weights='mass'``

        .. versionchanged:: 0.17.0
           removed deprecated `mass_weighted` keyword

        .. versionchanged:: 1.0.0
           Support for the ``start``, ``stop``, and ``step`` keywords has been
           removed. These should instead be passed to :meth:`AlignTraj.run`.

        .. versionchanged:: 2.0.0
           :attr:`rmsd` results are now stored in a
           :class:`MDAnalysis.analysis.base.Results` instance.

        .. versionchanged:: 2.8.0
           Added ``writer_kwargs`` kwarg dict to pass to the writer

        r8   r7   N3Moved mobile trajectory to in-memory representationr   z1filename of rms_align with no filename given: {0}z<Filename already exists in path and force is not set to Truer9   RMS-fitting on {0:d} atoms.)#r>   r?   r@   rK   r.   rE   
trajectoryr   transfer_to_memoryr\   r`   osrQ   splitfilenameformatexistsIOErrorsuperrm   __init___verboseloggingdisableWARNr=   r7   n_atomsrA   rU   Writer_writerr   _weightslen)selfr7   r8   rI   rv   prefixr!   r:   r<   r;   force	in_memorywriter_kwargsrd   fnnatoms	__class__s                   r%   r{   zAlignTraj.__init__  s   p &v..//1DE/F/1AB 	
6#4lCC 	%%'''HKKMNNNNW]]6#4#=>>qA!B;"F8,,  
 w~~h''     	(i'(9DDVDDD} 	*OGL))) l $,>N#-
 -
 -
))  M z$-II=II#DNG<<188T^9L9LMMNNNNNr'   c                     | j                             | j                  | _        | j         j        | j        z
  | _        t          j        | j        f          | j	        _
        d S N)rK   rB   r   _ref_comrC   _ref_coordinatesr   r   n_framesr[   r$   r   s    r%   _preparezAlignTraj._prepares  sN    --dm<< $ 84= HHdm%566r'   c                    | j         }| j                            | j                  }| j        j        |z
  }t          || j        | j        || j        | j                  \  }| j	        j
        |<   | j                            |           d S r   )_frame_indexr.   rB   r   rC   r3   r   r7   r   r[   r$   r   write)r   indexr/   r,   r.   s        r%   _single_framezAlignTraj._single_framez  s    !&--dm<<
!.8:E18!KMM2
 2
.dl'. 	<(((((r'   c                     | j                                          | j        s t          j        t          j                   d S d S r   )r   closer|   r}   r~   NOTSETr   s    r%   	_concludezAlignTraj._conclude  sB    } 	,OGN+++++	, 	,r'   c                 R    d}t          j        |t                     | j        j        S NzThe `rmsd` attribute was deprecated in MDAnalysis 2.0.0 and will be removed in MDAnalysis 3.0.0. Please use `results.rmsd` instead.warningswarnDeprecationWarningr[   r$   r   wmsgs     r%   r$   zAlignTraj.rmsd  ,    & 	
 	d.///|  r'   )
r6   Nrn   Nr4   TFTFN)__name__
__module____qualname____doc__r{   r   r   r   propertyr$   __classcell__r   s   @r%   rm   rm     s         $ kO kO kO kO kO kOZ7 7 7) ) ), , ,
 ! ! X! ! ! ! !r'   rm   c                        e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d fd	Zd	 Zd
 Zd Zed             Z	ed             Z
ed             Z xZS )rY   a>  RMS-align trajectory to a reference structure using a selection,
    and calculate the average coordinates of the trajectory.

    Both the reference `reference` and the trajectory `mobile` must be
    :class:`MDAnalysis.Universe` instances. If they contain a trajectory, then
    it is used. You can also use the same universe if you want to fit to the
    current frame.

    The output file format is determined by the file extension of
    `filename`.

    Example
    -------

    ::

        import MDAnalysis as mda
        from MDAnalysis.tests.datafiles import PSF, DCD
        from MDAnalysis.analysis import align

        u = mda.Universe(PSF, DCD)

        # align to the third frame and average structure
        av = align.AverageStructure(u, ref_frame=3).run()
        averaged_universe = av.results.universe

    Nr6   r4   TFr   c                 F   |
st          |j        t                    r0|                                 d}t                              d            t          t          |           j        |j        fi | | j	        st          j        t          j                   ||n|| _        t          j        |          } | j        j        |d          | _         |j        |d          | _        t'          | j                  t'          | j                  k    rSd                    | j        j        | j        j                  }t                              |           t/          |          t                              d                    t'          | j                                       |j        | _        || _        || _        t9          j        | j                  | j        _        t'          | j        j        j                  }tA          | j        | j        |||          \  | _        | _        t9          j!        | j        |          | _"        tG          | j        |          | _$        t                              d                    t'          | j                                       dS )	a  Parameters
        ----------
        mobile : Universe
            Universe containing trajectory to be fitted to reference
        reference : Universe (optional)
            Universe containing trajectory frame to be used as reference
        select : str (optional)
            Set as default to all, is used for Universe.select_atoms to choose
            subdomain to be fitted against
        filename : str (optional)
            Provide a filename for results to be written to
        weights : {"mass", ``None``} or array_like (optional)
            choose weights. With ``"mass"`` uses masses of `reference` as
            weights; with ``None`` weigh each atom equally. If a float array of
            the same length as the selection is provided, use each element of
            the `array_like` as a weight for the corresponding atom in the
            selection.
        tol_mass : float (optional)
            Tolerance given to `get_matching_atoms` to find appropriate atoms
        match_atoms : bool (optional)
            Whether to match the mobile and reference atom-by-atom. Default ``True``.
        strict : bool (optional)
            Force `get_matching_atoms` to fail if atoms can't be found using
            exact methods
        force : bool (optional)
            Force overwrite of filename for rmsd-fitting
        in_memory : bool (optional)
            *Permanently* switch `mobile` to an in-memory trajectory
            so that alignment can be done in-place, which can improve
            performance substantially in some cases. In this case, no file
            is written out (`filename` and `prefix` are ignored) and only
            the coordinates of `mobile` are *changed in memory*.
        ref_frame : int (optional)
            frame index to select frame from `reference`
        verbose : bool (optional)
            Set logger to show more information and show detailed progress of
            the calculation if set to ``True``; the default is ``False``.


        Attributes
        ----------
        reference_atoms : AtomGroup
            Atoms of the reference structure to be aligned against
        mobile_atoms : AtomGroup
            Atoms inside each trajectory frame to be rmsd_aligned
        results.universe : :class:`MDAnalysis.Universe`
            New Universe with average positions

            .. versionadded:: 2.0.0

        universe : :class:`MDAnalysis.Universe`
            Alias to the :attr:`results.universe` attribute.

            .. deprecated:: 2.0.0
               Will be removed in MDAnalysis 3.0.0. Please use
               :attr:`results.universe` instead.

        results.positions : np.ndarray(dtype=float)
            Average positions

            .. versionadded:: 2.0.0

        positions : np.ndarray(dtype=float)
            Alias to the :attr:`results.positions` attribute.

            .. deprecated:: 2.0.0
               Will be removed in MDAnalysis 3.0.0. Please use
               :attr:`results.positions` instead.

        results.rmsd : float
            Average RMSD per frame

            .. versionadded:: 2.0.0

        rmsd : float
            Alias to the :attr:`results.rmsd` attribute.

            .. deprecated:: 2.0.0
               Will be removed in MDAnalysis 3.0.0. Please use
               :attr:`results.rmsd` instead.

        filename : str
            String reflecting the filename of the file where the average
            structure is written


        Notes
        -----
        - If set to ``verbose=False``, it is recommended to wrap the statement
          in a ``try ...  finally`` to guarantee restoring of the log level in
          the case of an exception.
        - The ``in_memory`` option changes the `mobile` universe to an
          in-memory representation (see :mod:`MDAnalysis.coordinates.memory`)
          for the remainder of the Python session. If ``mobile.trajectory`` is
          already a :class:`MemoryReader` then it is *always* treated as if
          ``in_memory`` had been set to ``True``.


        .. versionadded:: 1.0.0
        .. versionchanged:: 2.0.0
           :attr:`universe`, :attr:`positions`, and :attr:`rmsd` are now
           stored in a :class:`MDAnalysis.analysis.base.Results` instance.
        Nrp   r8   r7   zkReference and trajectory atom selections do not contain the same number of atoms: N_ref={0:d}, N_traj={1:d}z RMS calculation for {0:d} atoms.r9   rq   )%rE   rr   r   rs   r\   r`   rz   rY   r{   r|   r}   r~   r   r8   r>   r?   r@   rK   r.   r   rw   r   	exceptionr   r=   r7   	ref_framerv   rU   rV   r[   rD   rA   r   r   r   r   )r   r7   r8   rI   rv   r!   r:   r<   r;   r   r   r   rd   rM   r   r   s                  r%   r{   zAverageStructure.__init__  sT   l  	O
6#4lCC 	O%%'''HKKMNNN 	/%%.v/@KKFKKK} 	*OGL)))&/&;&v..44f[6IJ/F/1ABt~#d&7"8"888,,2FN*D,=,E- -  S!!! %%%188T^9L9LMM	
 	
 	

 l"  #	$*; < <T\*011,>N#-
 -
 -
)) z$-88#DNG<<188T^9L9LMMNNNNNr'   c                    | j         j        j        j        j        }	 | j         j        j        | j                  | j                            | j                  | _	        | j        j
        | j	        z
  | _        | j        j
                                        | _        | j         j        j        |          n# | j         j        j        |          w xY wt          j        t!          | j                  df          | j        _
        d| j        _        d S )Nr   r   )r8   rD   rr   tsframer   rK   rB   r   r   rC   r   copy_ref_positionsr   r   r   r.   r[   r$   )r   current_frames     r%   r   zAverageStructure._preparec  s    /:=C	> N#.t~>> N11$-@@DM$(N$<t}$LD!"&.":"?"?"A"AD N#.}===DN#.}=== "$3t/@+A+A1*E!F!Fs   A<B1 1C
c           
      "   | j                             | j                  }| j         j        |z
  }| j        xj        t          || j        | j        || j	        | j                  d         z  c_        | j        xj        | j         j        z  c_        d S )Nr   )
r.   rB   r   rC   r[   r$   r3   r   r7   r   )r   r/   r,   s      r%   r   zAverageStructure._single_framev  s    &--dm<<
!.8:EW!KMM
 
  	 	$"3"==r'   c                    | j         xj        | j        z  c_        | j         xj        | j        z  c_        | j         j                            | j         j                            d                     | j                            | j         j        j	                   | j        
                                 | j        s t          j        t          j                   d S d S )N)r   r   )r[   rC   r   r$   rD   load_newr   r   r   r=   r   r|   r}   r~   r   r   s    r%   r   zAverageStructure._conclude  s    $-/T]*&&L"**:66	
 	
 	
 	4<06777} 	,OGN+++++	, 	,r'   c                 R    d}t          j        |t                     | j        j        S )NzThe `universe` attribute was deprecated in MDAnalysis 2.0.0 and will be removed in MDAnalysis 3.0.0. Please use `results.universe` instead.)r   r   r   r[   rD   r   s     r%   rD   zAverageStructure.universe  s,    * 	
 	d.///|$$r'   c                 R    d}t          j        |t                     | j        j        S )NzThe `positions` attribute was deprecated in MDAnalysis 2.0.0 and will be removed in MDAnalysis 3.0.0. Please use `results.positions` instead.)r   r   r   r[   rC   r   s     r%   rC   zAverageStructure.positions  s,    + 	
 	d.///|%%r'   c                 R    d}t          j        |t                     | j        j        S r   r   r   s     r%   r$   zAverageStructure.rmsd  r   r'   )
Nr6   NNr4   TFTFr   )r   r   r   r   r{   r   r   r   r   rD   rC   r$   r   r   s   @r%   rY   rY     s         > kO kO kO kO kO kOZ  &> > >	, 	, 	, % % X% & & X& ! ! X! ! ! ! !r'   rY   z2.4.0z3.0zeSee the documentation under Notes on how to directly useBio.Align.PairwiseAligner with ResidueGroups.)releaseremovemessager   皙c           
         t           sd}t          |          t          j                            d||||          }|                    |j                            d          | j                            d                    }|d         }	t          j	        dg d          }
 |
|	d         |	d	         |	j
        dt          |j        | j                            S )
aJ  Generate a global sequence alignment between two residue groups.

    The residues in `reference` and `mobile` will be globally aligned.
    The global alignment uses the Needleman-Wunsch algorithm as
    implemented in :mod:`Bio.Align.PairwiseAligner`. The parameters of the dynamic
    programming algorithm can be tuned with the keywords. The defaults
    should be suitable for two similar sequences. For sequences with
    low sequence identity, more specialized tools such as clustalw,
    muscle, tcoffee, or similar should be used.

    Parameters
    ----------
    mobile : AtomGroup
        Atom group to be aligned
    reference : AtomGroup
        Atom group to be aligned against
    match_score : float (optional), default 2
         score for matching residues, default 2
    mismatch_penalty : float (optional), default -1
         penalty for residues that do not match , default : -1
    gap_penalty : float (optional), default -2
         penalty for opening a gap; the high default value creates compact
         alignments for highly identical sequences but might not be suitable
         for sequences with low identity, default : -2
    gapextension_penalty : float (optional), default -0.1
         penalty for extending a gap, default: -0.1

    Returns
    -------
    alignment : tuple
        Tuple of top sequence matching output `('Sequence A', 'Sequence B', score,
        begin, end)`

    Raises
    ------
    ImportError
      If optional dependency Biopython is not available.

    Notes
    -----
    If you prefer to work directly with :mod:`Bio.Align` objects then you can
    run your alignment with :class:`Bio.Alig.PairwiseAligner` as ::

      import Bio.Align.PairwiseAligner

      aligner = Bio.Align.PairwiseAligner(
         mode="global",
         match_score=match_score,
         mismatch_score=mismatch_penalty,
         open_gap_score=gap_penalty,
         extend_gap_score=gapextension_penalty)
      aln = aligner.align(reference.residues.sequence(format="Seq"),
                          mobile.residues.sequence(format="Seq"))

      # choose top alignment with highest score
      topalignment = aln[0]

    The ``topalignment`` is a :class:`Bio.Align.PairwiseAlignment` instance
    that can be used in your bioinformatics workflows.

    See Also
    --------
    BioPython documentation for `PairwiseAligner`_. Alternatively, use
    :func:`fasta2select` with :program:`clustalw2` and the option
    ``is_aligned=False``.


    .. _`PairwiseAligner`:
       https://biopython.org/docs/latest/api/Bio.Align.html#Bio.Align.PairwiseAligner


    .. versionadded:: 0.10.0

    .. versionchanged:: 2.4.0
       Replace use of deprecated :func:`Bio.pairwise2.align.globalms` with
       :class:`Bio.Align.PairwiseAligner`.

    .. versionchanged:: 2.7.0
       Biopython is now an optional dependency which this method requires.

    zThe `sequence_alignment` method requires an installation of `Biopython`. Please install `Biopython` to use this method: https://biopython.org/wiki/Downloadglobal)modematch_scoremismatch_scoreopen_gap_scoreextend_gap_scoreSeq)rw   r   	Alignment)seqAseqBscorestartendr   )HAS_BIOPYTHONImportErrorBioAlignPairwiseAligneralignresiduessequencecollections
namedtupler   max
n_residues)r7   r8   r   mismatch_penaltygap_penaltygapextension_penaltyrj   aligneralntopalignmentAlignmentTuples              r%   sequence_alignmentr     s    ~  ": 	
 &!!!i'''"- (  G --##5#11   .. C
 q6L !+>>> N
 >QQ	I &"344  r'   r   	clustalw2c
                   !"# t           sd}
t          |
          |rvt                              d                    |                      t          |           5 }t          j                            |d          }ddd           n# 1 swxY w Y   n|Dt          j
                            |           \  }}t          j
                            |          dz   }|Dt          j
                            |          \  }}t          j
                            |          dz   }t          j        j                            |	| dd||	          }t                              d
t#                                 t                              dt%          |                     	  |            \  }}nG#  t                              dt#                                 t                              d            xY wt          |          5 }t          j                            |d          }ddd           n# 1 swxY w Y   t                              d                    |                     t                              d                    |                     t)          |          }|dk    rt+          d          ||g}||g}d}t-          |          D ]u\  }}||         Kt)          |j                  |j                            |          z
  }t3          j        d|dz             ||<   Xt3          j        ||                   ||<   vd t9          ||          D             }~~d } |||          "g }t;          |                                          D ]i!t?          |dd!f                   }||v r d#d|vr#dz  #n#dz  #d#z   dz   #|                     !"#fdt;          |          D                        jt3          j!        |          "                                }d#                    |d                   }d#                    |d                   } || d S )!a  Return selection strings that will select equivalent residues.

    The function aligns two sequences provided in a FASTA file and
    constructs MDAnalysis selection strings of the common atoms. When
    these two strings are applied to the two different proteins they
    will generate AtomGroups of the aligned residues.

    `fastafilename` contains the two un-aligned sequences in FASTA
    format. The reference is assumed to be the first sequence, the
    target the second. ClustalW_ produces a pairwise
    alignment (which is written to a file with suffix ``.aln``).  The
    output contains atom selection strings that select the same atoms
    in the two structures.

    Unless `ref_offset` and/or `target_offset` are specified, the resids
    in the structure are assumed to correspond to the positions in the
    un-aligned sequence, namely the first residue has resid == 1.

    In more complicated cases (e.g., when the resid numbering in the
    input structure has gaps due to missing parts), simply provide the
    sequence of resids as they appear in the topology in `ref_resids` or
    `target_resids`, e.g. ::

       target_resids = [a.resid for a in trj.select_atoms('name CA')]

    (This translation table *is* combined with any value for
    `ref_offset` or `target_offset`!)

    Parameters
    ----------
    fastafilename : str, path to filename
        FASTA file with first sequence as reference and
        second the one to be aligned (ORDER IS IMPORTANT!)
    is_aligned : bool (optional)
        ``False`` (default)
            run clustalw for sequence alignment;
        ``True``
            use the alignment in the file (e.g. from STAMP) [``False``]
    ref_offset : int (optional)
        add this number to the column number in the FASTA file
        to get the original residue number, default: 0
    target_offset : int (optional)
        add this number to the column number in the FASTA file
        to get the original residue number, default: 0
    ref_resids : str (optional)
        sequence of resids as they appear in the reference structure
    target_resids : str (optional)
        sequence of resids as they appear in the target
    alnfilename : str (optional)
        filename of ClustalW alignment (clustal format) that is
        produced by *clustalw* when *is_aligned* = ``False``.
        default ``None`` uses the name and path of *fastafilename* and
        substitutes the suffix with '.aln'.
    treefilename: str (optional)
        filename of ClustalW guide tree (Newick format);
        if default ``None``  the the filename is generated from *alnfilename*
        with the suffix '.dnd' instead of '.aln'
    clustalw : str (optional)
        path to the ClustalW (or ClustalW2) binary; only
        needed for `is_aligned` = ``False``, default: "ClustalW2"

    Returns
    -------
    select_dict : dict
        dictionary with 'reference' and 'mobile' selection string
        that can be used immediately in :class:`AlignTraj` as
        ``select=select_dict``.


    See Also
    --------
    :func:`sequence_alignment`, which does not require external
    programs.


    Raises
    ------
    ImportError
      If optional dependency Biopython is not available.


    .. _ClustalW: http://www.clustal.org/
    .. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/

    .. versionchanged:: 1.0.0
       Passing `alnfilename` or `treefilename` as `None` will create a file in
       the current working directory.
    .. versionchanged:: 2.7.0
       Biopython is now an optional dependency which this method requires.

    zThe `fasta2select` method requires an installation of `Biopython`. Please install `Biopython` to use this method: https://biopython.org/wiki/DownloadzUsing provided alignment {}fastaNz.alnz.dndproteinT)infiletyper   outfilenewtreez:Aligning sequences in %(fastafilename)r with %(clustalw)r.zClustalW commandline: %rzClustalW %(clustalw)r failedz=(You can get clustalw2 from http://www.clustal.org/clustal2/)clustalz'Using clustalw sequence alignment {0!r}z3ClustalW Newick guide tree was also produced: {0!r}r	   z5Only two sequences in the alignment can be processed.-r   c                     g | ]
\  }}||z   S  r   ).0residsoffsets      r%   
<listcomp>z fasta2select.<locals>.<listcomp>  s-       *FF  r'   c                    t          |           }t          j        ||                                 ft                    }t          |           D ]m\  }}d}||         t          j        t          j        t          j        t          |j
                            |k    dd                    dz
           ||ddf<   n|fd}|S )a%  Return a function that gives the resid for a position ipos in
        the nseq'th alignment.

        resid = resid_factory(alignment,seq2resids)
        r = resid(nseq,ipos)

        It is based on a look up table that translates position in the
        alignment to the residue number in the original
        sequence/structure.

        The first index of resid() is the alignmment number, the
        second the position in the alignment.

        seq2resids translates the residues in the sequence to resid
        numbers in the psf. In the simplest case this is a linear map
        but if whole parts such as loops are ommitted from the protein
        the seq2resids may have big gaps.

        Format: a tuple of two numpy arrays; the first array is for
        the reference, the second for the target, The index in each
        array gives the consecutive number of the amino acid in the
        sequence, the value the resid in the structure/psf.

        Note: assumes that alignments have same length and are padded if
        necessary.
        r   r   r   r   Nc                     || |f         S r   r   )nseqiposts      r%   residz2fasta2select.<locals>.resid_factory.<locals>.resid
  s    T4Z= r'   )r   r   r   get_alignment_lengthint	enumeratecumsumwherearraylistseq)	alignment
seq2residsr   r   iseqr   GAPr   s           r%   resid_factoryz#fasta2select.<locals>.resid_factory  s    8 9~~HdI::<<=SIII ++ 	 	GD!C#D)	"(28DKK#8#8C#?AFFGG!KAdAAAgJJ
 !" 	! 	! 	! 	! r'   zresid %iGz and ( backbone or name CB )z and backbonez( z )c                 .    g | ]} |          z  S r   r   )r   r  r   r   templates     r%   r   z fasta2select.<locals>.<listcomp>!  s*    NNN$EE$$5$55NNNr'   z or r   )r8   r7   )$r   r   r\   r`   rw   openr   AlignIOreadrt   rQ   splitextbasenamer   ApplicationsClustalwCommandliner]   varsrF   r   r   r   r   r  countr   aranger   ziprX   r   r  appendr   	transposejoin)$fastafilename
is_aligned
ref_residstarget_resids
ref_offsettarget_offset	verbosityalnfilenametreefilenameclustalwrj   r   r  filepathextrun_clustalwstdoutstderrr   r   orig_residsoffsetsr  r  r   lengthr  r  res_listalignedselref_selectiontarget_selectionr   r   r
  s$                                    @@@r%   fasta2selectr0  1  s   N  ": 	
 &!!! )
188GGHHH-   	9E((88I	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 G,,];;MHc'**844v=KG,,[99MHc7++H55>Ly-AA   B 
 
 	HFF	
 	
 	
 	/\1B1BCCC	)\^^NFFF	;TVVDDDKKO   + 	9#((i88I	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	95<<[II	
 	
 	
 	AHH 	
 	
 	
 y>>DqyyC
 
 	

 }-K=)G
CY'' > >at$ ZZ!%++c"2"22F "	!VaZ 8 8K "
;t+< = =K .1+w.G.G  J 	( ( (T M)Z00EH i446677 P PyD)**'>>g66HH'H(?T)NNNNNN%++NNNOOOO
(8


&
&
(
(CKKA''M{{3q6**&2BCCCs1   !BBBF( (AG,>!H++H/2H/c                 	   | j         |j         k    r|s+d}t                              |           t          |          | j        |j        k    rUd                    | j         |j         | j        |j                  }t                              |           t          |          d                    | j         |j                   }|r)t                              |           t          |          |dz  }t                              |           t          j        |t                     | j        |j        k    sJ t          j        d | j        D                       }t          j        d |j        D                       }t          j        ||z
            }	|	dk    t          j                  rWdfd
	}
 |
|           } |
|          }|j        j         |j        j         k    sJ t          j        | j                           }t                              d                                                                         t                              d                    | j        j        |                              t                              d                    |j        j        |                              |} |}~~| j         dk    s|j         dk    r+d}t                              |           t          |          |rt+          | d          rt+          |d          s9d}t                              |           t          j        |t                     nu	 t          j        | j        |j        z
            |k    }nX# t.          $ rK d                    | j         |j                   }t                              |           t          |          d	w xY wt          j        |          rt                              d           t1          | |         ||                   D ]m\  }}t                              d                    |j        |j        |j        |j        |j        |j        |j        |j        |j        |j        
  
                   nd                    |          }t                              |           t          |          | |fS )aj
  Return two atom groups with one-to-one matched atoms.

    The function takes two :class:`~MDAnalysis.core.groups.AtomGroup`
    instances `ag1` and `ag2` and returns two atom groups `g1` and `g2` that
    consist of atoms so that the mass of atom ``g1[0]`` is the same as the mass
    of atom ``g2[0]``, ``g1[1]`` and ``g2[1]`` etc.

    The current implementation is very simplistic and works on a per-residue basis:

    1. The two groups must contain the same number of residues.
    2. Any residues in each group that have differing number of atoms are discarded.
    3. The masses of corresponding atoms are compared. and if any masses differ
       by more than `tol_mass` the test is considered failed and a
       :exc:`SelectionError` is raised.

    The log file (see :func:`MDAnalysis.start_logging`) will contain detailed
    information about mismatches.

    Parameters
    ----------
    ag1 : AtomGroup
        First :class:`~MDAnalysis.core.groups.AtomGroup` instance that is
        compared
    ag2 : AtomGroup
        Second :class:`~MDAnalysis.core.groups.AtomGroup` instance that is
        compared
    tol_mass : float (optional)
         Reject if the atomic masses for matched atoms differ by more than
         `tol_mass` [0.1]
    strict : bool (optional)
        ``True``
            Will raise :exc:`SelectionError` if a single atom does not
            match between the two selections.
        ``False`` [default]
            Will try to prepare a matching selection by dropping
            residues with non-matching atoms. See :func:`get_matching_atoms`
            for details.
    match_atoms : bool (optional)
        ``True``
            Will attempt to match atoms based on mass
        ``False``
            Will not attempt to match atoms based on mass

    Returns
    -------
    (g1, g2) : tuple
        Tuple with :class:`~MDAnalysis.core.groups.AtomGroup`
        instances that match, atom by atom. The groups are either the
        original groups if all matched or slices of the original
        groups.

    Raises
    ------
    :exc:`SelectionError`
        Error raised if the number of residues does not match or if in the final
        matching masses differ by more than *tol*.

    Notes
    -----
    The algorithm could be improved by using e.g. the Needleman-Wunsch
    algorithm in :mod:`Bio.profile2` to align atoms in each residue (doing a
    global alignment is too expensive).

    .. versionadded:: 0.8

    .. versionchanged:: 0.10.0
       Renamed from :func:`check_same_atoms` to
       :func:`get_matching_atoms` and now returns matching atomgroups
       (possibly with residues removed)

    zMobile and reference atom selections do not contain the same number of atoms and atom matching is turned off. To match atoms based on residue and mass, try match_atoms=TruezReference and trajectory atom selections do not contain the same number of atoms: 
atoms:    N_ref={0}, N_traj={1}
and also not the same number of residues:
residues: N_ref={2}, N_traj={3}zrReference and trajectory atom selections do not contain the same number of atoms: 
atoms:    N_ref={0}, N_traj={1}zX
but we attempt to create a valid selection (use strict=True to disable this heuristic).)categoryc                 &    g | ]}|j         j        S r   r=   r   r   rs     r%   r   z&get_matching_atoms.<locals>.<listcomp>      AAAq17?AAAr'   c                 &    g | ]}|j         j        S r   r4  r5  s     r%   r   z&get_matching_atoms.<locals>.<listcomp>  r7  r'   r   Nc                     |t          j                  }| j        }|j        j        |         }|j        }t          j        ||          }||         S r   )r   logical_notr=   r   r   isin)g
match_maskaggoodr   ix_goodmismatch_masks         r%   get_atoms_byresz+get_matching_atoms.<locals>.get_atoms_byres  sR     %!#!>!>JW{)*5'&$//'{"r'   z7Removed {0} residues with non-matching numbers of atomsz!Removed residue ids: group 1: {0}z!Removed residue ids: group 2: {0}zFailed to automatically find matching atoms: created empty selections. Try to improve your selections for mobile and reference.massesz;Atoms could not be matched since they don't contain masses.z}Failed to find matching atoms: len(reference) = {}, len(mobile) = {} Try to improve your selections for mobile and reference.zAtoms: reference | trajectoryzY{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f}  |  {5!s:>4} {6:3d} {7!s:>3} {8!s:>3} {9:6.3f}z\Inconsistent selections, masses differ by more than {0}; mis-matching atoms are shown above.r   )r   r\   r^   r   r   rw   r`   r   r   r   r   r   r   absoluteanyr=   r  warningsumr]   r   hasattrrC  r   r  segidr   resnamenamemass)ag1ag2r:   r;   r<   rj   msgrsize1rsize2rsize_mismatchesrB  _ag1_ag2mismatch_resindexmass_mismatchesaratrA  s                    @r%   rA   rA   *  s   R {ck!! 	)<  LL    (((>S^++2
 fS[#+s~s~NN  LL    (((2 fS[#+..	 
  *S!!!$S))) AC KKM#(89999 ~////  AACLAAABBAACLAAABB;v77(1,6-   1	-
# 
# 
# 
# 
# 
# #?3''D"?3''D:%);;;;; !#	#. 9 9- HNNIPP!%%''   
 LL3::L'(9:   
 LL3::L'(9:    CCd {a3;!#3#3O  V$$$$V,,, .-
 sH%% )	-WS(-C-C )	-OCKKM#(899999
7K
SZ 7888C    7 7 7O&ck22  V$$$$V,,$67 vo&& - <===!#o"6O8LMM  FBLLszzHHJGGHHJGG    :&""  V$$$$V,,,8Os   (%N AO#r   )NNNr4   FT)Nr6   NrR   rS   F)r	   r   r   r   )	FNNr   r   r   NNr   )r4   FT)1r   os.pathrt   r   r}   r   numpyr   Bio.AlignIOr   	Bio.AlignBio.Align.Applicationsr   r   
MDAnalysisrU   MDAnalysis.lib.qcprotlibqcprotr   MDAnalysis.exceptionsr   r   MDAnalysis.analysis.rmsanalysisr>   MDAnalysis.coordinates.memoryr   MDAnalysis.lib.utilr   r   MDAnalysis.lib.logr   r
   r   baser   	getLoggerr\   r&   r3   rO   dciterk   rm   rY   r   r0  rA   r   r'   r%   <module>rk     sY  0h hR           !!!! MM    MMM
     # # # # # # # # # B B B B B B B B % % % % % % % % % 6 6 6 6 6 6 + + + + + + ) ) ) ) ) ) * * * * * *              		6	7	7R# R# R# R#v ?" ?" ?" ?"J ~ ~ ~ ~B C"##;	6   
w w w 
wt_! _! _! _! _! _! _! _!DQ! Q! Q! Q! Q!| Q! Q! Q!h 4   { { { {@ vD vD vD vDrk k k k k ks   ' 11