
     i*                         d Z dZd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 d
dlZ ej        d          Z G d de          ZdS )a*  Gromacs portable run input TPR format parser
============================================

The :mod:`~MDAnalysis.topology.TPRParser` module allows reading of a
Gromacs_ portable run input file (a `TPR file`_). Because
the file format of the TPR file is changing rapidly, not all versions
are currently supported. The known working versions and the
approximate Gromacs release numbers are listed in the table
:ref:`TPR format versions <TPR-format-table>`.

.. _`TPR-format-table`:

.. table:: TPR format versions and generations read by :func:`MDAnalysis.topology.TPRParser.parse`.

   ========== ============== ==================== =====
   TPX format TPX generation Gromacs release      read
   ========== ============== ==================== =====
   ??         ??             3.3, 3.3.1           no

   58         17             4.0, 4.0.2, 4.0.3,   yes
                             4.0.4, 4.0.5, 4.0.6,
                             4.0.7

   73         23             4.5.0, 4.5.1, 4.5.2, yes
                             4.5.3, 4.5.4, 4.5.5

   83         24             4.6, 4.6.1           yes

   100        26             5.0, 5.0.1, 5.0.2,   yes
                             5.0.3,5.0.4, 5.0.5

   103        26             5.1                  yes

   110        26             2016                 yes
   112        26             2018                 yes
   116        26             2019                 yes
   119        27             2020[*]_             yes
   122        28             2021                 yes
   127        28             2022                 yes
   129        28             2023                 yes
   133        28             2024.1               yes
   134        28             2024.4               yes
   137        28             2025.0               yes
   ========== ============== ==================== =====

.. [*] Files generated by the beta versions of Gromacs 2020 are NOT supported.
   See `Issue 2428`_ for more details.

For further discussion and notes see `Issue 2`_. Please *open a new issue* in
the `Issue Tracker`_ when a new or different TPR file format version should be
supported.

Bonded interactions available in Gromacs are described in table 5.5 of the
`Gromacs manual`_. The following ones are used to build the topology (see
`Issue 463`_):

* bonds: regular bonds (type 1), G96 bonds (type 2), Morse (type 3),
  cubic bonds (type 4), connections (type 5), harmonic potentials (type 6),
  FENE bonds (type 7), restraint potentials (type 10),
  tabulated potential with exclusion/connection (type 8),
  tabulated potential without exclusion/connection (type 9), constraints with
  exclusion/connection (type 1), constraints without exclusion/connection (type
  2), SETTLE constraints
* angles: regular angles (type 1), G96 angles (type 2), cross bond-bond
  (type3), cross-bond-angle (type 4), Urey-Bradley (type 5), quartic angles
  (type 6), restricted bending potential (type 10), tabulated angles (type 8)
* dihedrals: proper dihedrals (type 1 and type 9), Ryckaert-Bellemans dihedrals
  (type 3), Fourier dihedrals (type 5), restricted dihedrals (type 10),
  combined bending-torsion potentials (type 11), tabulated dihedral (type 8)
* impropers: improper dihedrals (type 2), periodic improper dihedrals (type 4)


Classes
-------

.. autoclass:: TPRParser
   :members:
   :inherited-members:

See Also
--------
:mod:`MDAnalysis.topology.tpr`


Development notes
-----------------

The TPR reader is a pure-python implementation of a basic TPR
parser. Currently the following sections of the topology are parsed:

* Atoms: number, name, type, resname, resid, segid, chainID, mass, charge, element
  [residue, segment, radius, bfactor, resnum, moltype]
* Bonds
* Angles
* Dihedrals
* Impropers

This tpr parser is written according to the following files

- :file:`{gromacs_dir}/src/kernel/gmxdump.c`
- :file:`{gromacs_dir}/src/gmxlib/tpxio.c` (the most important one)
- :file:`{gromacs_dir}/src/gmxlib/gmxfio_rw.c`
- :file:`{gromacs_dir}/src/gmxlib/gmxfio_xdr.c`
- :file:`{gromacs_dir}/include/gmxfiofio.h`

or their equivalent in more recent versions of Gromacs.

The function :func:`read_tpxheader` is based on the
`TPRReaderDevelopment`_ notes.  Functions with names starting with
``read_`` or ``do_`` are trying to be similar to those in
:file:`gmxdump.c` or :file:`tpxio.c`, those with ``extract_`` are new.

Versions prior to Gromacs 4.0.x are not supported.

.. Links
.. _Gromacs: http://www.gromacs.org
.. _`Gromacs manual`: http://manual.gromacs.org/documentation/5.1/manual-5.1.pdf
.. _TPR file: http://manual.gromacs.org/current/online/tpr.html
.. _`Issue Tracker`: https://github.com/MDAnalysis/mdanalysis/issues
.. _`Issue 2`: https://github.com/MDAnalysis/mdanalysis/issues/2
.. _`Issue 463`: https://github.com/MDAnalysis/mdanalysis/pull/463
.. _TPRReaderDevelopment: https://github.com/MDAnalysis/mdanalysis/wiki/TPRReaderDevelopment
.. _`Issue 2428`: https://github.com/MDAnalysis/mdanalysis/issues/2428


.. versionchanged:: 2.0.0
   The `elements` topology attribute is now exposed if at least one atom has
   a valid element symbol. In that case, atoms for which the element is not
   recognized have their element attribute set to an empty string. If none of
   the elements are recognized, then the `elements` attribute is not set in the
   topology.

.. versionchanged:: 2.7.0
   If the TPR molblock is named "Protein_chain_XXX" then we assume that XXX is 
   describing the chain of a protein (in the sense of the PDB chainID) and set
   the topology attribute `chainID` to "XXX". In all other cases, the chainID
   remains the full molblock name. The `segID` is never changed.
z	Zhuyi XuezGNU Public Licence, v2   )openany   )utils)setting)TopologyReaderBase)Resnums    NzMDAnalysis.topology.TPRparserc                   $    e Zd ZdZdZddZd ZdS )	TPRParserzRead topology information from a Gromacs_ `TPR file`_.

    .. _Gromacs: http://www.gromacs.org
    .. _TPR file: http://manual.gromacs.org/current/online/tpr.html
    TPRTc                    t          | j        d          5 }|                                }ddd           n# 1 swxY w Y   t          j        |          }	 t          j        |          }nH# t          t          f$ r4 | j         d}t          	                    |           t          |          w xY w|                     |           |j        t          j        k    r|j        dk    rt!          |                                          |                                z
  }|d|j        z  k    r+d}t          	                    |           t          |          t          j                            |          }|j        }	|j        rt          j        ||j                   |	dk    r5|j        d	k     rt          j        ||	           t          j        ||	           |j        r#t          j        ||j        ||j        
          }
n3| j         d}t          	                    |           t          |          |
                    t=          |
j        j         !                                                     |
S )a  Parse a Gromacs TPR file into a MDAnalysis internal topology structure.

        Parameters
        ----------
        tpr_resid_from_one: bool (optional)
            Toggle whether to index resids from 1 or 0 from TPR files.
            TPR files index resids from 0 by default, even though GRO and ITP
            files index from 1.

        Returns
        -------
        structure : dict


        .. versionchanged:: 1.1.0
            Added the ``tpr_resid_from_one`` keyword to control if
            resids are indexed from 0 or 1. Default ``False``.
        .. versionchanged:: 2.0.0
            Changed to ``tpr_resid_from_one=True`` by default.
        rb)modeNz*: Invalid tpr file or cannot be recognized      zHTPR files produced with beta versions of gromacs 2020 are not supported.r	   E   )tpr_resid_from_one	precisionz: No topology found in tpr file)"r   filenameread	tpr_utilsTPXUnpackerread_tpxheaderEOFError
ValueErrorloggercriticalIOError_log_headerfverStpxv_AddSizeFieldfgenlen
get_bufferget_positionsizeOfTprBodyTPXUnpacker2020from_unpackerngtcbBoxextract_box_infondo_realbTopdo_mtopr   add_TopologyAttrr   residsvaluescopy)selfr   kwargsinfiletprfdatathmsgactual_body_size
state_ngtctpr_tops              g/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/topology/TPRParser.pyparsezTPRParser.parse   s   * T]... 	!&;;==D	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!$T**	)$//BB*% 	 	 	]NNNCOOC   #,,	
 	 7a)))bgmm"4??#4#4558I8I8K8KK1r'7#777)  $$$cll",::4@@DW
7 	6&tRW555>>w||"4444j   7 
	'#5,	  GG ]CCCCOOC   #,,  )>)C)C)E)E!F!FGGGs   8<<A- -AB2c                    t                               d|j                    t                               d|j                    t                               d|j                    t                               d|j                    t                               d|j                    t                               d|j                    t                               d|j                    t                               d|j	                    t                               d	|j
                    t                               d
|j                    t                               d|j                    t                               d|j                    t                               d|j                    t                               d|j                    t                               d|j                    d S )NzGromacs version   : ztpx version       : ztpx generation    : ztpx precision     : ztpx file_tag      : ztpx natoms        : ztpx ngtc          : ztpx fep_state     : ztpx lambda        : ztpx bIr (input record): ztpx bTop         : ztpx bX           : ztpx bV           : ztpx bF           : ztpx bBox         : )r   infover_strr    r#   r   file_tagnatomsr*   	fep_statelambdebugbIrr.   bXbVbFr+   )r4   r9   s     r>   r   zTPRParser._log_header	  s   72:77888427445554274455592<99:::82;88999629667774274455592<99:::42744555888999427445552252233322522333225223334274455555    N)T)__name__
__module____qualname____doc__formatr?   r    rL   r>   r   r      sM          FH H H HT6 6 6 6 6rL   r   )rP   
__author____copyright__lib.utilr   tprr   r   r   r!   baser   core.topologyattrsr   logging	getLoggerr   r   rR   rL   r>   <module>r[      s   :I IT 
(       # # # # # #       $ $ $ $ $ $ ( ( ( ( ( ( 		:	;	;b6 b6 b6 b6 b6" b6 b6 b6 b6 b6rL   