
     iz                     @   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dl	Z	ddl
Z
ddlmZ ddlmZ ddlZddlmZ ddlmZmZmZmZ  ej        d          Z eddd	
          d             Z G d de          Z G d de          Z G d de          ZdS )aW	  
Generation and Analysis of X3DNA helicoidal parameter profiles --- :mod:`MDAnalysis.analysis.legacy.x3dna`
==========================================================================================================

:Author: Elizabeth Denning
:Year: 2013-2014
:Copyright: Lesser GNU Public License v2.1+

.. versionadded:: 0.8
.. versionchanged:: 0.16.0
   This module is difficult to test due to restrictions on the X3DNA_ code. It
   is therefore considered unmaintained and legacy code. It was moved to the
   :mod:`MDAnalysis.analysis.legacy` package (see `issue 906`_)

.. _`issue 906`: https://github.com/MDAnalysis/mdanalysis/issues/906

With the help of this module, X3DNA_ can be run on frames in a trajectory. Data
can be combined and analyzed. X3DNA_ :footcite:p:`Lu2003,Lu2008` must be installed
separately.


.. rubric:: References

.. footbibliography::

.. _X3DNA: http://x3dna.org/


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

Single structure
~~~~~~~~~~~~~~~~

B-DNA structure::

   from MDAnalysis.analysis.x3dna import X3DNA, X3DNAtraj
   from MDAnalysis.tests.datafiles import PDB_X3DNA

   # set path to your x3dna binary in bashrc file
   H = X3DNA(PDB_X3DNA, executable="x3dna_ensemble analyze -b 355d.bps -p pdbfile")
   H.run()
   H.collect()
   H.plot()


Trajectory
~~~~~~~~~~

Analyzing a trajectory::

  u = MDAnalysis.Universe(psf, trajectory)
  H = X3DNAtraj(u, ...)
  H.run()
  H.plot()
  H.save()

The profiles are available as the attribute :attr:`X3DNAtraj.profiles`
(``H.profiles`` in the example) and are indexed by frame number but
can also be indexed by an arbitrary order parameter as shown in the
next example.



Analysis classes
----------------

.. autoclass:: X3DNA
   :members:
   :inherited-members:

   .. attribute:: profiles

      ``x3dna_ensemble analyze -b 355d.bps -p pdbfile attribute``:
      After running :meth:`X3DNA.collect`, this dict contains all the
      X3DNA profiles, indexed by the frame number. If only a single
      frame was analyzed then this will be ``X3DNA.profiles[0]``. Note
      that the order is random; one needs to sort the keys first.

.. autoclass:: X3DNAtraj
   :members:
   :inherited-members:

   .. attribute:: profiles

      After running :meth:`X3DNA.collect`, this dict contains all the
      X3DNA profiles, indexed by the frame number.



Utilities
---------

.. autoexception:: ApplicationError

    N)OrderedDict)ApplicationError)
asiterable	deprecaterealpathwhichzMDAnalysis.analysis.x3dna2.7.03.0.0LX3DNA module is deprecated and will be removed inMDAnalysis 3.0.0, see #3788releaseremovemessagec                    t          j        dt                     | j        du rg g g g g g g g g g g g f\  }}}}}}}}}	}
}}t	          t          |                     D ][}|                    |                                 |         j                   |                    |                                 |         j	                   |                    |                                 |         j
                   |                    |                                 |         j                   |                    |                                 |         j                   |	                    |                                 |         j                   |                    |                                 |         j                   |                    |                                 |         j                   |                    |                                 |         j                   |
                    |                                 |         j                   |                    |                                 |         j                   |                    |                                 |         j                   ]t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |	          t)          j        |
          t)          j        |          t)          j        |          f\  }}}}}}}}}	}
}}g g }}t	          t          |d                             D ]}|                    t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |	dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |
dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   g           |                    t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |	dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |
dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   g           n g g g g g g g g g g g g f\  }}}}
}}t	          t          |                     D ]/}|                    |                                 |         j                   |                    |                                 |         j                   |                    |                                 |         j                   |
                    |                                 |         j                   |                    |                                 |         j                   |                    |                                 |         j                   1t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |          t)          j        |	          t)          j        |
          t)          j        |          t)          j        |          f\  }}}}
}}g g }}t	          t          |d                             D ]}|                    t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |
dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   g           |                    t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   t)          j        |
dd|f                   t)          j        |dd|f                   t)          j        |dd|f                   g           t)          j        |          t)          j        |          }}||fS )a!  Get mean and standard deviation of helicoidal parameters from a saved `profile`.

    The `profile` should have been saved with :meth:`BaseX3DNA.save`. Then
    load it with ::

      profile = cPickle.load(open("x3dna.pickle"))
      h_mean, h_std = mean_std_from_x3dnaPickle(profile)

    Arguments
    ---------
    profile : dict
        A :attr:`X3DNA.profiles` dict with results from the
        :class:`X3DNA` analysis.

    Returns
    -------
    (list, list)
        The tuple contains two lists with the means and the standard deviations
        for the helicoidal parameters. The order for both lists is ``[shear,
        stretch, stagger, buckle, propeller, opening, shift, slide, rise, tilt,
        roll, twist]``.

    .. deprecated:: 2.7.0
        X3DNA will be removed in 3.0.0.
    zKX3DNA module is deprecated and will be removed in MDAnalysis 3.0, see #3788)categoryFr   N)warningswarnDeprecationWarningx3dna_paramrangelenappendvaluesShearStretchStaggerBuckle	PropellerOpeningRiseShiftSlideTiltRollTwistnparraymeanstd)profilebp_shear
bp_stretch
bp_staggerbp_risebp_shiftbp_slide	bp_bucklebp_propbp_openbp_tiltbp_rollbp_twistina_avgna_stdjs                    j/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/analysis/legacy/x3dna.pymean_std_from_x3dnaPickler<      sx	   D MU#    e## 2r2r2r2r2r2E	*j'8XyRY[bdkmts7||$$ 	7 	7AOOGNN,,Q/5666gnn..q19:::gnn..q19:::W^^--a07888NN7>>++A.8999NN7>>++A.6777NN7>>++A.3444OOGNN,,Q/5666OOGNN,,Q/5666NN7>>++A.3444NN7>>++A.3444OOGNN,,Q/56666 x))28J+?+?*AUAUWYW_`gWhWhHX 2 2BHY4G4GRYIZIZHWrx00"(72C2CRXhEWEWX	*j'8XyRY[bdkmt Rs8A;''((  	  	AMMGHQQQTN++GJqqq!t,--GJqqq!t,--GIaaadO,,GGAAAqDM**GGAAAqDM**GHQQQTN++GHQQQTN++GGAAAqDM**GGAAAqDM**GGAAAqDM**GHQQQTN++    MMF8AAAqD>**F:aaad+,,F:aaad+,,F9QQQT?++F7111a4=))F7111a4=))F8AAAqD>**F8AAAqD>**F7111a4=))F7111a4=))F7111a4=))F8AAAqD>**   # 	F CEb"bRTVXZ\^`bdfhjlnpBp?8Wgxs7||$$ 	7 	7ANN7>>++A.3444OOGNN,,Q/5666OOGNN,,Q/5666NN7>>++A.3444NN7>>++A.3444OOGNN,,Q/56666HXHZ  HZ  HWHXHXHYHWHWHWHWHXC
?8Wgx Rs8A;''(( 	 	AMMGHQQQTN++GHQQQTN++GGAAAqDM**GGAAAqDM**GGAAAqDM**GHQQQTN++	 	 	 MMF8AAAqD>**F8AAAqD>**F7111a4=))F7111a4=))F7111a4=))F8AAAqD>**	 	 	 	 Xf%%rx'7'7FF6>    c                   <    e Zd ZdZd
dZd Zd Zd Zd Zd Z	e	Z
d	S )	BaseX3DNAai  Baseclass for X3DNA_ analysis, providing plotting and utility functions.

    When methods return helicoidal basepair parameter as lists, then the order
    is always

    ====== ==============
    index  parameter
    ====== ==============
     0     shear
     1     stretch
     2     stagger
     3     buckle
     4     propeller
     5     opening
     6     shift
     7     slide
     8     rise
     9     tilt
    10     roll
    11     twist
    ====== ==============

    for each nucleic acid pair.

    .. deprecated:: 2.7.0
        X3DNA will be removed in 3.0.0.

    .. _X3DNA: http://x3dna.org

    x3dna.picklec                 l    ddl }|                    | j        t          |d          |j                   dS )zSave :attr:`profiles` as a Python pickle file *filename*.

        Load profiles dictionary with ::

           import cPickle
           profiles = cPickle.load(open(filename))

        r   Nwb)cPickledumpprofilesopenHIGHEST_PROTOCOL)selffilenamerC   s      r;   savezBaseX3DNA.saveB  sC     	M4$//1I	
 	
 	
 	
 	
r=   c                    g g g g g g g g g g g g f\  }}}}}}}}}	}
}}t          t          | j                            D ]}|                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j	                   |	                    | j                                        |         j
                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |
                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |	          t#          j        |
          t#          j        |          t#          j        |          f\  }}}}}}}}}	}
}}g g }}t          t          |d                             D ]}|                    t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |	dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |
dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   g           |                    t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |	dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |
dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   g           t#          j        |          t#          j        |          }}||fS )a  Returns the mean and standard deviation of base parameters.

        Returns
        -------
        (list, list)
            The tuple contains two lists with the means and the standard deviations
            for the helicoidal parameters. The order for both lists is ``[shear,
            stretch, stagger, buckle, propeller, opening, shift, slide, rise, tilt,
            roll, twist]``.
        r   N)r   r   rE   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   )rH   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   s                    r;   mean_stdzBaseX3DNA.mean_stdQ  s    2r2r2r2r2r2E	*j'8XyRY[bdkmts4=))** 	= 	=AOODM002215;<<<dm2244Q7?@@@dm2244Q7?@@@T]1133A6=>>>NN4=//11!4>???NN4=//11!4<===NN4=//11!49:::OODM002215;<<<OODM002215;<<<NN4=//11!49:::NN4=//11!49:::OODM002215;<<<< x))28J+?+?*AUAUWYW_`gWhWhHX 2 2BHY4G4GRYIZIZHWrx00"(72C2CRXhEWEWX	*j'8XyRY[bdkmt Rs8A;''((  	  	AMMGHQQQTN++GJqqq!t,--GJqqq!t,--GIaaadO,,GGAAAqDM**GGAAAqDM**GHQQQTN++GHQQQTN++GGAAAqDM**GGAAAqDM**GGAAAqDM**GHQQQTN++    MMF8AAAqD>**F:aaad+,,F:aaad+,,F9QQQT?++F7111a4=))F7111a4=))F8AAAqD>**F8AAAqD>**F7111a4=))F7111a4=))F7111a4=))F8AAAqD>**     &))28F+;+;v~r=   c                 
   g g g g g g g g g g g g f\  }}}}}}}}}	}
}}t          t          | j                            D ]}|                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j	                   |	                    | j                                        |         j
                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |
                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |	          t#          j        |
          t#          j        |          t#          j        |          f\  }}}}}}}}}	}
}}g }t          t          |d                             D ]r}|                    t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |d|                   t#          j        |dd|f                   t#          j        |	dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |
dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   g           tt#          j        |          }|S )a5  Returns the mean value for the base parameters.

        Returns
        -------
        list
            The list contains the means for the helicoidal parameters. The
            order is ``[shear, stretch, stagger, buckle, propeller, opening,
            shift, slide, rise, tilt, roll, twist]``.

        r   N)r   r   rE   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   )rH   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r:   s                   r;   r(   zBaseX3DNA.mean  s    2r2r2r2r2r2E	*j'8XyRY[bdkmts4=))** 	= 	=AOODM002215;<<<dm2244Q7?@@@dm2244Q7?@@@T]1133A6=>>>NN4=//11!4>???NN4=//11!4<===NN4=//11!49:::OODM002215;<<<OODM002215;<<<NN4=//11!49:::NN4=//11!49:::OODM002215;<<<< x))28J+?+?*AUAUWYW_`gWhWhHX 2 2BHY4G4GRYIZIZHWrx00"(72C2CRXhEWEWX	*j'8XyRY[bdkmt
 s8A;''(( 	 	AMMGHQQQTN++GJqqq!t,--GJqqq!t,--GIbqbM**GGAAAqDM**GGAAAqDM**GHQQQTN++GHQQQTN++GGAAAqDM**GGAAAqDM**GGAAAqDM**GHQQQTN++     &!!r=   c                 
   g g g g g g g g g g g g f\  }}}}}}}}}	}
}}t          t          | j                            D ]}|                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j	                   |	                    | j                                        |         j
                   |                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   |
                    | j                                        |         j                   |                    | j                                        |         j                   |                    | j                                        |         j                   t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |          t#          j        |	          t#          j        |
          t#          j        |          t#          j        |          f\  }}}}}}}}}	}
}}g }t          t          |d                             D ]r}|                    t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |d|                   t#          j        |dd|f                   t#          j        |	dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   t#          j        |
dd|f                   t#          j        |dd|f                   t#          j        |dd|f                   g           tt#          j        |          }|S )aK  Returns the standard deviation for the base parameters.

        Returns
        -------
        list
            The list contains the standard deviations for the helicoidal
            parameters. The order is ``[shear, stretch, stagger, buckle,
            propeller, opening, shift, slide, rise, tilt, roll, twist]``.

        r   N)r   r   rE   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r)   )rH   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r9   r:   s                   r;   r)   zBaseX3DNA.std  s    2r2r2r2r2r2E	*j'8XyRY[bdkmts4=))** 	= 	=AOODM002215;<<<dm2244Q7?@@@dm2244Q7?@@@T]1133A6=>>>NN4=//11!4>???NN4=//11!4<===NN4=//11!49:::OODM002215;<<<OODM002215;<<<NN4=//11!49:::NN4=//11!49:::OODM002215;<<<< x))28J+?+?*AUAUWYW_`gWhWhHX 2 2BHY4G4GRYIZIZHWrx00"(72C2CRXhEWEWX	*j'8XyRY[bdkmt
 s8A;''(( 	 	AMMF8AAAqD>**F:aaad+,,F:aaad+,,F9RaR=))F7111a4=))F7111a4=))F8AAAqD>**F8AAAqD>**F7111a4=))F7111a4=))F7111a4=))F8AAAqD>**     &!!r=   c                    |                                  \  }}t          t          |d                             D ]}|                    dt	          j        d                    }t          t          dt          |dd|f                   dz                       }|                    ||dd|f         |dd|f         d           |                    dt          |dd|f                   dz              |	                    d           | j
                                        d         j        j        |         }|d	v r)|                    d
                    |                     n(|                    d                    |                     |j                            d                    |                     |j                                         dS )a  Plot time-averaged base parameters for each basse pair in a 1D graph.

        One plot is produced for each parameter. It shows the the mean and
        standard deviation for each individual base pair. Each plot is saved to
        PNG file with name "<parameter_name>.png".

        Parameters
        ----------
        ax : matplotlib.pyplot.Axes (optional)
             Provide `ax` to have all plots plotted in the same axes.

        r   axo      Nz-o)yerrfmtzNucleic Acid Number)r   r   r   r    r!   r"   z{!s} ($\AA$)z{0!s} (deg)z	{0!s}.png)rL   r   r   poppltsubplotlisterrorbarset_xlim
set_xlabelrE   r   dtypenames
set_ylabelformatfiguresavefigclf)rH   kwargsr8   r9   krP   xparams           r;   plotzBaseX3DNA.plot	  s    s6!9~~&& 	 	AD#+c"2"233BU1c&A,//!34455AKK6!!!Q$<fQQQTlKEEEKK3vaaad|,,q0111MM0111M((**1-39!<E    o44U;;<<<<m22E;;<<<Ik00%99:::IMMOOOO'	 	r=   c              #   l   K   | j         dS t          | j                   D ]}|| j         |         fV  dS )zReturn an iterator over profiles sorted by frame/order parameter.

        The iterator produces tuples ``(q, profile)``. Typically, `q` is the
        frame number.
        N)rE   sorted)rH   qs     r;   sorted_profiles_iterzBaseX3DNA.sorted_profiles_iter-  sS       = F&& 	( 	(AdmA&'''''	( 	(r=   N)r@   )__name__
__module____qualname____doc__rJ   rL   r(   r)   rg   rk   __iter__ r=   r;   r?   r?   "  s         >
 
 
 
F F FP6 6 6p6 6 6p" " "H	( 	( 	( $HHHr=   r?   c                   R    e Zd ZdZ eddd          d             Zd Zd Zd	 Zd
S )X3DNAaq  Run X3DNA_ on a single frame or a DCD trajectory.

    Only a subset of all X3DNA control parameters is supported and can be set
    with keyword arguments. For further details on X3DNA_ see the `X3DNA docs`_.

    Running X3DNA with the :class:`X3DNA` class is a 3-step process:

     1. set up the class with all desired parameters
     2. run X3DNA with :meth:`X3DNA.run`
     3. collect the data from the output file with :meth:`X3DNA.collect`

    The class also provides some simple plotting functions of the collected
    data such as :meth:`X3DNA.plot` or :meth:`X3DNA.plot3D`.

    When methods return helicoidal basepair parameter as lists, then the order
    is always

    ====== ==============
    index  parameter
    ====== ==============
     0     shear
     1     stretch
     2     stagger
     3     buckle
     4     propeller
     5     opening
     6     shift
     7     slide
     8     rise
     9     tilt
    10     roll
    11     twist
    ====== ==============

    .. versionadded:: 0.8

    .. deprecated:: 2.7.0
        X3DNA will be removed in 3.0.0.

    .. _`X3DNA docs`: http://forum.x3dna.org/
    r	   r
   r   r   c                    g d| _         g | _        || _        t                              dt          |                      i | _        |                    dd          }|                    dd          | _        t          |          | j        d<   | j        d         t dj
        di t                      }t                              |           t                              d	t                                 t          t          j        |          t          j                            | j        d                   }|                    d
d          | _        | j        du rt'          j        d          | _        nt'          j        d          | _        | j                                        D ]=\  }}|t          |          't                              dt                                 >t1                      | _        dS )a  Set up parameters to run X3DNA_ on PDB *filename*.

        Parameters
        ----------
        filename : str
               The `filename` is used as input for X3DNA in the
               :program:`xdna_ensemble` command.  It specifies the name of a
               PDB coordinate file to be used. This must be in Brookhaven
               protein databank format or something closely approximating
               this.
        executable : str (optional)
               Path to the :program:`xdna_ensemble` executable directories
               (e.g. ``/opt/x3dna/2.1 and /opt/x3dna/2.1/bin``) must be set
               and then added to export in bashrc file. See X3DNA
               documentation for set-up instructions.
        x3dna_param : bool (optional)
               Determines whether base step or base pair parameters will be
               calculated. If ``True`` (default) then stacked *base step*
               parameters will be analyzed.  If ``False`` then stacked *base
               pair* parameters will be analyzed.
        logfile : str (optional)
               Write output from X3DNA to `logfile` (default: "bp_step.par")


        See Also
        --------
        :class:`X3DNAtraj`
        )zauxiliary.parzbestpairs.pdbzbp_order.datzbp_helical.parzcf_7methods.parzcol_chains.scrzcol_helices.scrzhel_regions.pdbzref_frames.datzhstacking.pdbzstacking.pdbz*Setting up X3DNA analysis for %(filename)r
executablexdna_ensembler   TNz*X3DNA binary {x3dna_exe_name!r} not found.zT%(x3dna_exe_name)r must be on the PATH or provided as keyword argument 'executable'.logfilezbp_step.parFz6x3dna_ensemble analyze -b 355d.bps --one %(filename)r z0find_pair -s %(filename)r stdout |analyze stdin z<Executable %(program)r not found, should have been %(path)r.rq   )	tempfilestempdirsrI   loggerinfovarsexerU   r   r   r_   fatalOSErrorerrnoENOENTospathdirnamerw   textwrapdedenttemplateitemserrorr   rE   )rH   rI   rc   x3dna_exe_nameerrmsg	x3dnapathprogramr   s           r;   __init__zX3DNA.__init__f  s   L
 
 
  @$t**MMM L/BB!::mT::$).$9$9!8O$,HAH  && F LL   LLf   %,///GOODH_$=>>	zz)];;u$$$OL DMM %OF DM
 "X^^-- 	 	MGT|uT{{2RFF  
 $r=   c                 2   |                     dd          }|                     d| j                  }t          |                                           }|                    |           |                     d| j                  }| j        |z  }|rXt          |d          5 }|                    |           ddd           n# 1 swxY w Y   t          
                    d|           t                              d|           t          
                    d| j        d	                    t          |d          5 }t          j        |gd
          }	ddd           n# 1 swxY w Y   t          |d          5 }|D ]2}
|
                                                    d          r	d|	_         n3ddd           n# 1 swxY w Y   |	j        dk    r!t                              d|	j        |           t                              dt                                 dS )zRun X3DNA on the input file.inpfileNoutfiler   wz(Wrote X3DNA input file %r for inspectionz4Starting X3DNA on %(filename)r (trajectory: %(dcd)r)z%srv   T)shellr)z*** ERROR ***ERROR   r   z#X3DNA Failure (%d). Check output %rz'X3DNA finished: output file %(outname)r)rU   rw   r|   copyupdater   r   rF   writerz   debugr{   r}   
subprocesscallstrip
startswith
returncode
bit_lengthr~   )rH   rc   inpnameoutname	x3dnaargsr   inpfoutputx3dnalines              r;   runz	X3DNA.run  s   **Y--**Y55JJOO%%	   jj0@AAmi' 	Ngs## q              LLCWMMMBI	
 	
 	
 	T48O4555'3 	76OSE666E	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7'3 	6  ::<<**+EFF '*E$E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 q  LL5   
 	=tvvFFFFFs6   CCC8EE #E 76F99F= F=c                    |                     d| j                  }|                     dd          }|                     dt          j        j                  }t
                              d|           d}d| j        v rddl} |j        | j                  }t          |          }|dk    rGt
          
                    d	| j                   t          d
                    | j                            t
                              d|| j                   t                      | _        t
                              d|||           d}g }	t          |d          5 }
d}|
D ]0}|                                }| j        du r|                    d          rBd}t
                              d           |                                }t+          d          }g }	x|rZt          |                                          dk    r2	 |                                \  }}}}}}}}}}}}}nO#  t
                              d||                                           t
                              d|            xY w|	                    t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          g           d}|                    d          rCd}t
                              d           |                                }t+          d          }g }	/|rt          |                                          dk    r	 |                                \  }}}}}}}nO#  t
                              d||                                           t
                              d|            xY w|	                    t5          |          t5          |          t5          |          t5          |          t5          |          t5          |          g           .d}2| j        du r#t6          j                            |	dd          }n"t6          j                            |	dd          }|| j        |<   t
                              d|t          |                     |t          j                            |dt?          |          z             }t          j         d           t          j        !                    |          st          j"        |           t          j                            |d                    ||                    }t7          j#        ||           t
                              d||           ddd           n# 1 swxY w Y   t          | j                  |k    r/t
                              dt          | j                             dS t
          $                    d t          | j                  |           dS )!a  Parse the output from a X3DNA run into numpy recarrays.

        Can deal with outputs containing multiple frames.

        The method saves the result as :attr:`X3DNA.profiles`, a dictionary
        indexed by the frame number. Each entry is a
        :class:`np.recarray`.

        If the keyword `outdir` is supplied (e.g. ".") then each profile is
        saved to a gzipped data file.

        Parameters
        ----------
        run : str, int (optional
              identifier, free form [1]
        outdir : str (optional)
              save output data under `outdir`/`run` if set to any other
              value but ``None`` [``None``]

        x3dnaoutr   rR   outdirz,Collecting X3DNA profiles for run with id %s*r   Nz'Glob pattern %r did not find any files.z*Glob pattern {0!r} did not find any files.z-Found %d input files based on glob pattern %sz)Run %s: Reading %d X3DNA profiles from %rr   Fz#        ShearTzStarted reading datazRun %d: Problem parsing line %rzCheck input file %r.z#      Shiftz#f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8,f8zOShear,Stretch,Stagger,Buckle,Propeller,Opening,Shift,Slide,Rise,Tilt,Roll,Twist)formatsr]   zf8,f8,f8,f8,f8,f8z Shift,Slide,Rise,Tilt,Roll,Twistz4Collected X3DNA profile for frame %d (%d datapoints)run_zrm -f tmp*.outzbp_step_{0!s}_{1:04d}.dat.gzz#Finished with frame %d, saved as %rz&Collected X3DNA profiles for %d framesz5Missing data: Found %d X3DNA profiles from %d frames.)%rU   rw   r   r   curdirrz   r{   rI   globr   r   
ValueErrorr_   r   rE   rF   rstripr   r   r   splitintr   critical	exceptionr   floatr&   recfromrecordsjoinstrsystemexistsmakedirssavetxtwarning)rH   rc   x3dna_outputr   r   lengthr   	filenamesx3dna_profile_norecordsr   	read_datar   fieldsSequencer   r   r   r   r   r   r!   r"   r    r#   r$   r%   frame_x3dna_outputrundirframe_x3dna_txts                                 r;   collectzX3DNA.collect  s   6 zz*dl;;jj""Hbgn55BCHHH$-KKK!	$-00I^^F{{=t}   !@GG   
 KK?   $7		
 	
 	
 ,$$ R	I i. i.{{}}#u,,'788 !$(	%;<<<!%+., ,( #%   /.tzz||,,11& %)JJLL!"$,$)$+$+$*$-$+$)$)$($($($)E	& &$E$'$(JJLL!" !" !"
 !' 0 0$:L!" !" !" !&#NN$)%LL$)'NN$)'NN$)&MM$))$4$4$)'NN$)%LL$)%LL$)$KK$)$KK$)$KK$)%LL!"    % ).I~66 !$(	%;<<<!%+., ,( #%   #.tzz||,,11& %)JJLL!"$,$)$)$($($($)E	& &$E$'$(JJLL!" !" !"
 !' 0 0$:L!" !" !" !&#NN$)%LL$)%LL$)$KK$)$KK$)$KK$)%LL!"	 	 	 % ).I5((%'V%7%7A7 &8 & &"" &(V%7%7/< &8 & &" /ADM*+LLF &''   !ffs3xx.?@@	*+++w~~f-- (K'''"$',,299- # # 
?,>???9$#  ]R	 R	 R	 R	 R	 R	 R	 R	 R	 R	 R	 R	 R	 R	 R	h t}''KK8#dm:L:L     NNGDM""    sG   B&W9>"H! W9!AI--EW93OW9APGW99W= W=c                     | j         D ]'}	 t          j        |           # t          $ r Y $w xY w| j        D ]}t          j        |d           d S )NT)ignore_errors)rx   r   unlinkr   ry   shutilrmtree)rH   r   ds      r;   __del__zX3DNA.__del__  sz     	 	A	!    	1 	1AM!400000	1 	1s    
--N)	rl   rm   rn   ro   r   r   r   r   r   rq   r=   r;   rs   rs   ;  s        ( (T Y*	  S& S& S&j!G !G !GF^ ^ ^@1 1 1 1 1r=   rs   c                   L    e Zd ZdZ eddd          d             Zd Zd Zd	S )
	X3DNAtraja  Analyze all frames in a trajectory.

    The :class:`X3DNA` class provides a direct interface to X3DNA. X3DNA itself
    has limited support for analysing trajectories but cannot deal with all the
    trajectory formats understood by MDAnalysis. This class can take any
    universe and feed it to X3DNA. By default it sequentially creates a PDB for
    each frame and runs X3DNA on the frame.


    .. deprecated:: 2.7.0
        X3DNA will be removed in 3.0.0.
    r	   r
   r   r   c                 0   || _         |                    dd          | _        |                    dd          | _        |                    dd          | _        |                    dd          | _        |                    dd          | _        || _        dS )	a
  Set up the class.

        Parameters
        ----------
        universe : Universe
               The input trajectory as part of a
               :class:`~MDAnalysis.core.universe.Universe`; the trajectory is
               converted to a sequence of PDB files and X3DNA is run on each
               individual file. (Use the `start`, `stop`, and `step` keywords
               to slice the trajectory.)
        selection : str (optional)
               MDAnalysis selection string (default: "nucleic") to select the
               atoms that should be analyzed.
        start : int (optional)
        stop : int (optional)
        step : int (optional)
               frame indices to slice the trajectory as
               ``universe.trajectory[start, stop, step]``; by default, the whole
               trajectory is analyzed.
        x3dna_param : bool (optional)
               indicates whether stacked bases or stacked base-pairs will be
               analyzed. ``True`` is bases and ``False`` is stacked base-pairs
               [Default is ``True``].
        kwargs : keyword arguments (optional)
               All other keywords are passed on to :class:`X3DNA` (see there
               for description).


        See Also
        --------
        :class:`X3DNA`

        	selectionnucleicr   TstartNstopstep)universerU   r   r   r   r   r   x3dna_kwargs)rH   r   rc   s      r;   r   zX3DNAtraj.__init__  s    T !K;;!::mT::ZZ..
JJvt,,	JJvt,,	"r=   c                     |                     d| j                  }|                     d| j                  }|                     d| j                  }|                     d| j                  }| j                                        }|                    |           t                      }| j	        
                    | j                  }| j	        j        |||         D ]}	t                              d|	j                   t!          j        d          \  }
}t%          j        |
           |                    |           t%          j        d                    |                     	 |                    |            | j        |fi |}	 t%          j        |           n:# t2          $ r Y n.w xY w# 	 t%          j        |           w # t2          $ r Y w w xY wxY wt5          |          d	k    rdd
                    t5          |          |                                |	          }t                              |           t;          j        |           |                                d         ||	j        <   || _         dS )zRun X3DNA on the whole trajectory and collect profiles.

        Keyword arguments `start`, `stop`, and `step` can be used to only
        analyse part of the trajectory. The defaults are the values provided to
        the class constructor.
        r   r   r   r   zX3DNA analysis frame %4d z.pdb)suffixzfind_pair {0!s} 355d.bpsrR   z6Got {0} profiles ({1}) --- should be 1 (time step {2})r   N)!rU   r   r   r   r   r   r   r   r   r   select_atomsr   
trajectoryrz   r{   frametempfilemkstempr   closer   r   r_   	run_x3dnar   r   r   keysr   r   r   r   rE   )rH   rc   r   r   r   r   x3dna_kwrE   r   tsfdpdbfilex3dna_profileserr_msgs                 r;   r   zX3DNAtraj.run  s`    

7DJ//zz&$),,zz&$),,jj0@AA$))++==-,,T^<<-*5d?; 	< 	<BKK3RX>>>"*&999KBHRLLLMM'"""I077@@AAAg&&&!/!D!D8!D!DIg&&&&   DIg&&&&   D>""a''RYY'')<)<)>)>  W%%%g&&&!/!6!6!8!8!;HRX sB   )#F2F""
F/.F/2G4G	G	
G	GG	Gc                     | j         |d<   t          |fi |}|                                 |                                 |j        S )z)Run X3DNA on a single PDB file `pdbfile`.r   )r   rs   r   r   rE   )rH   r   rc   Hs       r;   r   zX3DNAtraj.run_x3dna<  sF     $ 0}'$$V$$				zr=   N)rl   rm   rn   ro   r   r   r   r   rq   r=   r;   r   r     sx          Y*	  *# *# *#\&! &! &!P    r=   r   ) ro   r   r   loggingr   os.pathr   r   r   r   r   collectionsr   matplotlib.pyplotpyplotrV   numpyr&   
MDAnalysisr   MDAnalysis.lib.utilr   r   r   r   	getLoggerrz   r<   objectr?   rs   r   rq   r=   r;   <module>r      s  0_ _@    				          # # # # # #           ' ' ' ' ' ' F F F F F F F F F F F F		6	7	7 	&	  J J JZV$ V$ V$ V$ V$ V$ V$ V$rR1 R1 R1 R1 R1I R1 R1 R1jr r r r r	 r r r r rr=   