
     iI                         d Z ddlZddlmZmZ ddl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Zd
Zn# e$ r dZY nw xY w G d dej                  ZdS )a
  
TNG trajectory files --- :mod:`MDAnalysis.coordinates.TNG`
==========================================================


The TNG format :footcite:p:`Lundborg2014` is a format used in `GROMACS`_ for
storage of trajectory and topology information. The TNG format allows a wide
range of compression algorithms and unlike the compressed XTC format can also
store velocities and forces in addition to positions.

The classes in this module are based on the `pytng`_ package for reading TNG
files. The reader is directed to the `pytng documentation`_ for further reading
about how pytng works under the hood. Pytng is an optional dependency and must
be installed to use this Reader. Use of the reader without pytng installed will
raise an `ImportError`. The variable `HAS_PYTNG` indicates whether this module
has pytng availble.

In addition to particle-dependent trajectory information like positions,
forces and velocities, the TNG format can store trajectory metadata and
other arbitrary time dependent data. Additional information can range from
the virial and pressure components to the molecular topology of the system.
This is enabled by a block based system in which binary flags indicate the
presence or absence of various data blocks. The structure of a TNG file is
provided  in the `TNG specification`_. The TNG paper :footcite:p:`Lundborg2014` and
the `pytng documentation`_ are also good resources. The user is encouraged to
read the full list of `TNG blocks`_ to understand the full power of the TNG
format.



Current Limitations
-------------------
Currently there is only a Reader for TNG files and no Writer. This will depend
on upstream changes in pytng. Additionally, it is not currently possible to
read the molecular topology from a TNG file.

There are also some limitations to reading TNG files with pytng.
Currently we do not allow data to be read *off stride*. In essence this
means that all of the critical trajectory data (positions, box, velocities
(if present), forces (if present)) must share the same stride in trajectory
integrator steps. These critical blocks in the TNG file are henceforth called
*special blocks*. Optional blocks (all blocks that are not special blocks)
will not be read if they do not share an integrator step with, or are not
divisible by the shared integrator step of the special blocks.


References
----------

.. footbibliography::


.. Links

.. _GROMACS:
    https://www.gromacs.org/
.. _pytng:
    https://github.com/MDAnalysis/pytng
.. _pytng documentation:
    https://www.mdanalysis.org/pytng/
.. _TNG blocks:
    https://www.mdanalysis.org/pytng/documentation_pages/Blocks.html
.. _TNG specification:
    https://gitlab.com/hugomacdermott/tng/-/blob/master/Trajectoryformatspecification.mk

    N)ListOptional)base)Timestep)triclinic_box)store_init_arguments   )DoidueTFc                       e Zd ZdZdZdddddZdZd	Zd
ZdZ	eeee	gZ
 ej         ed          de           ed)dedef fd                        Zd Zd Zededefd            Zedefd            Zedee         fd            Zedee         fd            Zedee         fd            Zd ZdedefdZdedefdZ d*d!e!e         defd"Z"d#d$d!edefd%Z#d& Z$d' Z%d( Z& xZ'S )+	TNGReadera  Reader for the TNG format

    The TNG format :footcite:p:`Lundborg2014` is a format used in `GROMACS`_ for
    storage of trajectory and topology information. This reader is are based on
    the `pytng`_ package for reading TNG files. The reader is directed to the
    `pytng documentation`_ and `TNG specification`_ for further reading.

    The TNG format allows a wide range of compression
    algorithms and unlike the compressed XTC format can also store velocities
    and forces in addition to positions.

    The contents of the *special blocks* (positions, box, velocities, forces)
    are read into the timestep if present. Additional blocks are read into the
    `ts.data` dictionary if they are available at the current frame.

    .. versionadded:: 2.4.0
    TNGsecondnmznm/pszkJ/(mol*nm))timelengthvelocityforceTNG_TRAJ_BOX_SHAPETNG_TRAJ_POSITIONSTNG_TRAJ_VELOCITIESTNG_TRAJ_FORCESz10.1002/jcc.23495zThe TNG paper)descriptionpathTfilenameconvert_unitsc                     t           st          d           t          t                     j        |fi | | _        | _        t          j         j        d           _	         j	        j
         _
         j	        j         _        t           j	        j                                                   _         j	        j         _         j	        j         _         j	        j         _        d  j        D              _          j         j
        fi  j         _         j         j        v  _         j        r9d j         j        <   t;          j        dt:          j                   j        _          j!         j        v  _"         j"        r8d j         j!        <    j	        #                     j!                   j        _$         j%         j        v  _&         j&        r8d j         j%        <    j	        #                     j%                   j        _'         j(         j        v  _)         j)        r8d j         j(        <    j	        #                     j(                   j        _*         fd j        D              _+         ,                                 d _-         j	        #                     j                   _.        d	 _-         /                                 d
S )zInitialize a TNG trajectory

        Parameters
        ----------
        filename : str
            filename of the trajectory
        convert_units : bool (optional)
            convert into MDAnalysis units

        1TNGReader: To read TNG files please install pytngrc                     i | ]}|d S )F ).0ks     d/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/coordinates/TNG.py
<dictcomp>z&TNGReader.__init__.<locals>.<dictcomp>   s    &N&N&NAq%&N&N&N    T   )dtypec                 &    g | ]}|j         v|S r!   )_special_blocks)r"   blockselfs     r$   
<listcomp>z&TNGReader.__init__.<locals>.<listcomp>   s1     #
 #
 #
D000 000r&   r   N)0	HAS_PYTNGImportErrorsuperr   __init__r   r   pytngTNGFileIterator_file_iteratorn_atomsn_steps_n_stepslist	block_idskeys_block_names_block_dictionaryblock_strides_block_stridesn_data_frames_data_framesr*   _special_block_present	_Timestep
_ts_kwargsts_box_blockname_has_boxnpzerosfloat32
dimensions_positions_blockname_has_positions make_ndarray_for_block_from_name	positions_velocities_blockname_has_velocities
velocities_forces_blockname_has_forcesforces_additional_blocks_check_strides_and_frames_frame	_box_temp_read_next_timestep)r,   r   r   kwargs	__class__s   `   r$   r2   zTNGReader.__init__   s     	C   	(i';;F;;; *#3DM3GG*2+3 !!4!>!C!C!E!EFF!%!4!>"1? /=&N&N9M&N&N&N# $.AAAA +t/@@= 	??CD'(;<!#!2:!>!>!>DG"74;LL 	EID'(AB#DD-  G  $9T=NN 	FJD'(BC#DD.  G  1T5FF 	BFD'(>?#DD*  GN#
 #
 #
 #
*#
 #
 #

 	&&((( ,MM
 
   """""r&   c                 |   g g | j         D ]F}| j        |         }                    |           | j        |         }                    |           Gt	          fdD                       }t	          fdD                       }|r|st          d          d         | _        d         dz   | _        g | _        | j	        D ]r}| j        |         }|| j        k    r>|| j        z  rt          j        d| d           =| j                            |           X| j                            |           sdS )	z
        Check that the strides and frame numbers of the blocks in the TNG
        file match up so that the file can be iterated over retrieving data at
        each integrator step
        c              3   0   K   | ]}|d          k    V  dS r   Nr!   )r"   vstridess     r$   	<genexpr>z6TNGReader._check_strides_and_frames.<locals>.<genexpr>   s+      ::Qgaj::::::r&   c              3   0   K   | ]}|d          k    V  dS r_   r!   )r"   r`   r@   s     r$   rb   z6TNGReader._check_strides_and_frames.<locals>.<genexpr>  s-      EE!]1--EEEEEEr&   z<Strides of TNG special blocks not equal, file cannot be readr      zTNG additional block zm does not match strides of other blocks and is not divisible by the global stride_length. It will not be readN)special_blocksr?   appendrA   allIOError_global_stride	_n_frames_additional_blocks_to_readrV   warningswarn)	r,   r+   striden_data_frame
strides_eq	frames_eq
stride_addr@   ra   s	          @@r$   rW   z#TNGReader._check_strides_and_frames   s    ( 	/ 	/E(/FNN6""",U3L  ....::::':::::
EEEE}EEEEE	 	I 	N   &aj&q)A-*,', 	> 	>E,U3JT000 33 
M/ / / /    3::    /66u====	> 	>r&   c                 8    | j                                          dS )zclose the readerN)r5   _closer,   s    r$   closezTNGReader.close  s    ""$$$$$r&   returnc                     t           st          d          t          j        | d          5 }|j        }ddd           n# 1 swxY w Y   |S )a  parse the number of atoms in the TNG trajectory file

        Parameters
        ----------
        filename : str
            The name of the TNG file

        Returns
        -------
        n_atoms : int
            The number of atoms in the TNG file

        r   r   N)r/   r0   r3   r4   r6   )r   tngr6   s      r$   parse_n_atomszTNGReader.parse_n_atoms"  s      	C   "8S11 	"SkG	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   A  AAc                     | j         S )znumber of frames in trajectory

        Returns
        -------
        n_frames : int
            The number of data containing frames in the trajectory
        )rj   ru   s    r$   n_frameszTNGReader.n_frames9  s     ~r&   c                     | j         S )zlist of the blocks that are in the file

        Returns
        -------

        blocks : list
            The block names present in the TNG trajectory
        )r<   ru   s    r$   blockszTNGReader.blocksD  s       r&   c                 H    d | j                                         D             S )zlist of the special blocks that are in the file

        Returns
        -------

        special_blocks : list
            The special block names (positions, box, velocities, forces)
            present in the TNG trajectory
        c                     g | ]	\  }}||
S r!   r!   )r"   r#   r`   s      r$   r-   z,TNGReader.special_blocks.<locals>.<listcomp>[  s!    GGGdaQGGGGr&   )rB   itemsru   s    r$   re   zTNGReader.special_blocksP  s(     HGd9??AAGGGGr&   c                     | j         S )a|  list of the additional (non-special) blocks that are being read from
        the trajectory. This may be exclude some blocks present in the file if
        they do not fall on the same trajectory stride as the positions and
        velocities.

        Returns
        -------

        additional_blocks : list
            The additional block names in the TNG trajectory
        )rk   ru   s    r$   additional_blockszTNGReader.additional_blocks]  s     ..r&   c                     d| j         _        d| _        | j                                         | j                            | j        d           dS )zreopen the trajectoryr   r.   r   N)rE   framerX   r5   rt   _openr   ru   s    r$   _reopenzTNGReader._reopenl  sH    ""$$$!!$-55555r&   r   c                     || j         z  S )a  Convert a frame number to an integrator step

        Parameters
        ----------
        frame : int
            The frame number

        Returns
        -------
        integrators_step :  int
            The integrator step of the frame in the TNG file

        )ri   )r,   r   s     r$   _frame_to_stepzTNGReader._frame_to_steps  s     t***r&   ic                 B    |dz
  | _         |                                 }|S )zread frame i

        Parameters
        ----------
        i : int
            The trajectory frame to be read

        Returns
        -------
        ts : Timestep
            Data from frame i encapsulated in an MDA `:class:Timestep`
        rd   )rX   rZ   )r,   r   rE   s      r$   _read_framezTNGReader._read_frame  s%     !e%%''	r&   NrE   c                    | j         | j        dz
  k    rt          d          || j        }|                     | j         dz             }| j                            |          }| xj         dz  c_         |                     ||          }|S )a	  Read next frame into a timestep

        Parameters
        ----------
        ts : Timestep
            The timestep to read the data into

        Returns
        -------
        ts :  Timestep
            The timestep filled with data from the next step
        rd   z"trying to go over trajectory limit)rX   r|   rh   rE   r   r5   	read_step_frame_to_ts)r,   rE   stepiterator_steps       r$   rZ   zTNGReader._read_next_timestep  s     ;$-!+++>???:B""4;?33+55d;;q}b11	r&   	curr_stepzpytng.TNGCurrentIntegratorStepc                    | j         |_        |                                }| j        r|                     |          }||_        |j        |j        d<   | j        r|	                    | j
                   t          | j
                            dd           |_        |j        st          d          | j        r"|                     |j        dd                    | j        rQ|                    |j                   |j        st          d          | j        r|                     |j                   | j        rQ|                    |j                   |j        st          d          | j        r|                     |j                   | j        rV|                    |j                   |j        st          d          | j        r|                     | j        j                   | j        D ]v}| j        |         }|| j        z  s]| j         !                    |          }|"                    | j#        |         |          |j        |<   |j        st          d| d	          w|S )
a;  convert a TNGCurrentIteratorStep to an MDA Timestep

        Parameters
        ----------
        curr_step : pytng.TNGCurrentIntegratorStep
            The current timestep in the TNG trajectory

        ts : Timestep
            The current timestep in the MDA trajectory

        Returns
        -------
        ts: Timestep
            The current timestep in the MDA trajectory with data from TNG
            trajectory integrator step

        Raises
        ------
        IOError
            The reading of one of the attributes from the TNG file failed
        r      z Failed to read box from TNG fileNz&Failed to read positions from TNG filez'Failed to read velocities from TNG filez#Failed to read forces from TNG filez Failed to read additional block z from TNG file)$rX   r   get_timer   convert_time_from_nativer   r   datarG   get_boxrY   r   reshaperK   read_successrh   convert_pos_from_nativerM   get_positionsrO   rQ   get_velocitiesrR   convert_velocities_from_nativerT   
get_forcesrU   convert_forces_from_nativerE   rk   r?   ri   r5   rN   get_blockidr=   )r,   r   rE   r   r+   add_block_stride
block_datas          r$   r   zTNGReader._frame_to_ts  s   2 ;!!## 	70066D#.= 	@dn---)4>+A+A!Q+G+GHBM) B@AAA! @,,R]2A2->??? 	;##BL111) HFGGG! ;,,R\::: 	C$$R]333) IGHHH! C33BMBBB 	@  +++) ECDDD! @//???4 	 	E#259$t':: 'HHOO  "+!6!6*51:" " !- !P5PPP   	r&   c                 >    | j                                         }|d= |S )zMake a dictionary of the class state to pickle Reader instance.

        Must be done manually as pytng uses a non-trivial`__cinit__`.
        r5   )__dict__copy)r,   states     r$   __getstate__zTNGReader.__getstate__  s%    
 ""$$"#r&   c                     || _         t          j        | j        d          | _        |                     | j                  }| j                            |          }dS )zWRestore class from `state` dictionary in unpickling of Reader
        instance
        r   N)r   r3   r4   r   r5   r   rX   r   )r,   r   r   _s       r$   __setstate__zTNGReader.__setstate__  sS     #3DM3GG ""4;//))$//r&   c                      t          d          )zWriter for TNG files

        Raises
        ------
        NotImplementedError
            Currently there is not writer for TNG files pending implementation
            upstream in pytng.
        z*There is currently no writer for TNG files)NotImplementedErrorru   s    r$   WriterzTNGReader.Writer  s     ""NOOOr&   )T)N)(__name__
__module____qualname____doc__formatunitsrF   rL   rP   rS   r*   r   dciter
   r   strboolr2   rW   rv   staticmethodintrz   propertyr|   r   r~   re   r   r   r   r   r   r   rZ   r   r   r   r   __classcell__)r\   s   @r$   r   r   p   s        $ F 	 E *N/1)	O SY  oH   S# S# S#T S# S# S# S# S#  S#j,> ,> ,>\% % %      \, #    X 	!S	 	! 	! 	! X	! 
HS	 
H 
H 
H X
H /49 / / / X/6 6 6+C +C + + + + S X    " hx&8 H    0I9I?GI	I I I IV  0 0 0"	P 	P 	P 	P 	P 	P 	Pr&   r   )r   rl   typingr   r   numpyrH   MDAnalysis.coordinatesr   MDAnalysis.coordinates.timestepr   MDAnalysis.lib.mdamathr   MDAnalysis.lib.utilr   r   r
   r3   r/   r0   
ReaderBaser   r!   r&   r$   <module>r      s(  0A AF  ! ! ! ! ! ! ! !     ' ' ' ' ' ' 4 4 4 4 4 4 0 0 0 0 0 0 4 4 4 4 4 4        LLL II    IIIkP kP kP kP kP kP kP kP kP kPs   ; AA