
     i,*                         d Z ddlZddlZddlmZmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&  ej'        d          Z(d	 Z) G d
 de          Z*dS )a  
ParmEd topology parser --- :mod:`MDAnalysis.converters.ParmEdParser`
====================================================================

Converts a `ParmEd <https://parmed.github.io/ParmEd/html>`_
:class:`parmed.structure.Structure` into a :class:`MDAnalysis.core.Topology`.


Example
-------

If you want to use an MDAnalysis-written ParmEd structure for simulation
in ParmEd, you need to first read your files with ParmEd to include the
necessary topology parameters. ::

    >>> import parmed as pmd
    >>> import MDAnalysis as mda
    >>> from MDAnalysis.tests.datafiles import PRM7_ala2, RST7_ala2
    >>> prm = pmd.load_file(PRM7_ala2, RST7_ala2)
    >>> prm
    <AmberParm 3026 atoms; 1003 residues; 3025 bonds; PBC (orthogonal); parametrized>

We can then convert this to an MDAnalysis structure, select only the
protein atoms, and then convert it back to ParmEd. ::

    >>> u = mda.Universe(prm)
    >>> u
    <Universe with 3026 atoms>
    >>> prot = u.select_atoms('protein')
    >>> prm_prot = prot.convert_to('PARMED')
    >>> prm_prot
    <Structure 23 atoms; 2 residues; 22 bonds; PBC (orthogonal); parametrized>

From here you can create an OpenMM simulation system and minimize the
energy. ::

    >>> import openmm as mm
    >>> import openmm.app as app
    >>> from parmed import unit as u
    >>> system = prm_prot.createSystem(nonbondedMethod=app.NoCutoff,
    ...                                constraints=app.HBonds,
    ...                                implicitSolvent=app.GBn2)
    >>> integrator = mm.LangevinIntegrator(
    ...                         300*u.kelvin,       # Temperature of heat bath
    ...                         1.0/u.picoseconds,  # Friction coefficient
    ...                         2.0*u.femtoseconds, # Time step
    ... )
    >>> sim = app.Simulation(prm_prot.topology, system, integrator)
    >>> sim.context.setPositions(prm_prot.positions)
    >>> sim.minimizeEnergy(maxIterations=500)

Now you can continue on and run a simulation, if you wish.

Classes
-------

.. autoclass:: ParmEdParser
   :members:
   :inherited-members:

.. versionchanged:: 2.0.0
   The ParmEdParser class was moved from :mod:`~MDAnalysis.topology` to
   :mod:`~MDAnalysis.converters`

    N   )TopologyReaderBasechange_squash)Z2SYMB)Atomids	AtomnamesAltLocsChainIDs	AtomtypesOccupanciesTempfactorsElementsMassesChargesResidsResnumsResnamesSegids	GBScreensSolventRadiiNonbondedIndicesRMinsEpsilonsRMin14s
Epsilon14sBondsUreyBradleysAngles	Dihedrals	ImpropersCMaps)Topologyz"MDAnalysis.converters.ParmEdParserc                 V    t          |           dk    r| d         S t          |           S )N   r   )lentuple)valuess    l/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/converters/ParmEdParser.pysquash_identicalr)      s)    
6{{aayV}}    c                   2    e Zd ZdZdZed             Zd ZdS )ParmEdParserz
    For ParmEd structures
    PARMEDc                 X    	 ddl }t          | |j                  S # t          $ r Y dS w xY w)zNCan this Parser read object *thing*?

        .. versionadded:: 1.0.0
        r   NF)parmed
isinstance	StructureImportError)thingpmds     r(   _format_hintzParmEdParser._format_hint   sK    	4     eS]333  	 	 	55	s    
))c                   9 | j         }g }g }g }g }g }g }g }	g }
g }g }g }g }g }g }g }g }g }g }g }g }|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                   !g }t-          |          }g }t/          ||          D ]K\  }}	 |                    t0          |                    '# t2          $ r |                    d           Y Hw xY w|t4          t6          f|t8          t:          j        f|t>          t:          j        f|t@          t6          f|tB          t6          f|tD          t:          j#        f|tH          t6          f|tJ          t6          f|tL          t:          j        f|tN          t:          j        f|tP          t:          j        f|tR          t:          j        f|tT          t:          j#        f|tV          t:          j        f|tX          t:          j        f|tZ          t:          j        f|t\          t:          j        ffD ]8\  }}}|                     |t;          j/        ||                               9t;          j/        |
t:          j#                  }
t;          j/        |	t6                    }	t;          j/        |t6                    }t;          j/        |t6                    }ta          |
|	||f|
|	||f          \  } \  }
}	}}t-          |
          }!|                    tc          |
                     |                    te          |
3                                                     |                    ti          |	                     ta          |f|f          \  }"\  }t-          |          }#|                    tk          |                     i }$g }%g }&i }'g }(i })g }*i }+g },i }-g }.i }/g }0|j6        D ]t}1|1j7        j8        |1j9        j8        f}2|2|$vr|1g|1j:        gf|$|2<   -|$|2         d                             |1           |$|2         d                             |1j:                   u	 t/          tw          |$<                                           \  }$}3t/          |3 \  }%}&tw          t{          t|          |%                    }%tw          t{          t|          |&                    }&n# t~          $ r	 g g g }&}%}$Y nw xY w|                    t          |$|%d|&                     |jA        d|'|(f|jB        d|)|*f|jC        d	|+|,f|jD        d	|-|.f|jE        d
|/|0ffD ]j\  }4}5}3}|4D ]`9d t          d|5dz             D             }6t          9fd|6D                       }2|2|3vr9g|3|2<   E|3|2                             9           ak|'t          f|)t          f|+t          f|-t          f|/t          ffD ]\  }7}	 t/          tw          |7<                                           \  }}n# t~          $ r g g }}Y nw xY wtw          t{          t|          |                    }|                     |||dd                     t          ||!|#|| |"          }8|8S )a^  Parse PARMED into Topology

        Returns
        -------
        MDAnalysis *Topology* object


        .. versionchanged:: 2.0.0
           Elements are no longer guessed, if the elements present in the
           parmed object are not recoginsed (usually given an atomic mass of 0)
           then they will be assigned an empty string.
         )dtyper   r$   F)typesguessedorderr            c                 8    g | ]}d                      |          S )zatom{})format).0is     r(   
<listcomp>z&ParmEdParser.parse.<locals>.<listcomp>H  s$    FFF++FFFr*   c              3   B   K   | ]}t          |          j        V  d S )N)getattridx)rA   aps     r(   	<genexpr>z%ParmEdParser.parse.<locals>.<genexpr>I  s.      ==!GAqMM-======r*   N)attrsatom_resindexresidue_segindex)Nfilenameatomsappendnamemasschargetypeatomic_numbernumberresiduechainsegidaltlocbfactor	occupancyscreensolvent_radiusnb_idxrminepsilonrmin_14
epsilon_14r%   zipr   KeyErrorr   objectr   npfloat32r   r   r   r   int32r
   r	   r   r   r   r   r   r   r   r   r   arrayr   r   r   copyr   r   bondsatom1rF   atom2r;   listitemsmapr)   
ValueErrorr   urey_bradleysangles	dihedrals	improperscmapsranger&   r   r   r   r    r!   r"   ):selfkwargs	structurenamesmasseschargesr9   atomic_numbersserialsresnamesresidschainidssegidsaltLocsbfactorsoccupanciesscreenssolvent_radiinonbonded_indicesrminsepsilonsrmin14s
epsilon14satomrJ   n_atomselementszrP   valsAttrr8   residx
n_residuessegidx
n_segmentsbond_values
bond_typesbond_orders	ub_valuesub_typesangle_valuesangle_typesdihedral_valuesdihedral_typesimproper_valuesimproper_typescmap_values
cmap_typesbondrF   r'   pmdlistnarN   dcttoprH   s:                                                            @r(   parsezParmEdParser.parse   si    M	 
O 	/ 	/DLL###MM$)$$$NN4;'''LL###!!$"4555NN4;'''OODL-...MM$,-...OODL.///MM$,,---NN4;'''OODL)))t~...NN4;'''  !4555$$T[111LL###OODL)))NN4<(((do....e**>511 	$ 	$GAt$q	**** $ $ $#####$
 Iv&VRZ(grz*Iv&x(grx(x(gv&{BJ/+rz2i,L"*5 0"(;E2:&x,grz*RZ0#"
 	< 	<D$& LLbht5999::;;;;&1118HF3338HF333&///7DXx0Xx08
 8
448V
 [[
VF^^$$$WV[[]]++,,,Xh''((()6)fY??	[[
VF^^$$$ 
	
O 	7 	7D:>4:>2C+%%%)FTZL#9C  C #**4000C #**4:6666	C"%tK,=,=,?,?'@'@"AK '*6l#Jc"2J??@@Js#3[AABBKK  	> 	> 	>35r2[KKK	> 	:uK  	
 	
 	
 $aH=q,< !_nE !_nE_aj9+
 	* 	*&GR  * *FFU1b1f5E5EFFF====u=====f$$#$#F3KK3K&&q))))* %6"i(i(% 
 	M 	MIC%!4		#4#45ee % % % "e% -u5566ELLd%dKKKLLLL #
 
 
 
s6    J##KK$+Z   Z32Z3++__('_(N)__name__
__module____qualname____doc__r@   staticmethodr5   r    r*   r(   r,   r,      sR          F
4 
4 \
4L L L L Lr*   r,   )+r   loggingnumpyrf   topology.baser   r   guesser.tablesr   core.topologyattrsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   core.topologyr"   	getLoggerloggerr)   r,   r   r*   r(   <module>r      s  0@ @B      = = = = = = = = # # # # # #                                                         : % $ $ $ $ $		?	@	@  ` ` ` ` `% ` ` ` ` `r*   