
     i5                         d Z ddlZddlZddlmZ ddlZddlmZm	Z	 ddl
mZmZ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 dd	lmZ  ej        d
          Z G d de          Z dS )a!  
PSF topology parser
===================

Reads a CHARMM/NAMD/XPLOR PSF_ file to build the system. The topology will
contain atom IDs, segids, residue IDs, residue names, atom names, atom types,
charges and masses. Bonds, angles, dihedrals and impropers are also read from
the file.

It reads both standard and extended ("EXT") PSF formats and can also parse NAMD
space-separated "PSF" file variants.

.. _PSF: http://www.charmm.org/documentation/c35b1/struct.html

Classes
-------

.. autoclass:: PSFParser
   :members:
   :inherited-members:

    N)ceil   )openanyatoi   )TopologyReaderBase	squash_bychange_squash)Atomids	Atomnames	AtomtypesMassesChargesResidsResnumsResnamesSegidsBondsAngles	Dihedrals	Impropers)TopologyzMDAnalysis.topology.PSFc                   .    e Zd ZdZdZd Zd Zd Zd ZdS )	PSFParsera  Read topology information from a CHARMM/NAMD/XPLOR PSF_ file.

    Creates a Topology with the following Attributes:
    - ids
    - names
    - types
    - masses
    - charges
    - resids
    - resnames
    - segids
    - bonds
    - angles
    - dihedrals
    - impropers

    .. _PSF: http://www.charmm.org/documentation/c35b1/struct.html


    .. versionchanged:: 2.8.0
       PSFParser now reads string resids and converts them to integers.
    PSFc           
         t          | j                  5 }t          |          }|                    d          sDd                    | j        |          }t
                              |           t          |          |dd                                         }d|v rd| _	        nd|v rd| _	        nd| _	        t          |           t          |                                          }|d	         d
k    sCd                    | j                  }t
                              |           t          |          t          t          |d                             D ]}t          |           t
                              d                    | j        | j	                             |                     |dd	d	| j        f          }t          ddd| j        fft"          ddd| j        fft$          ddd| j        fft&          ddd| j        fff}		 |	D ]F\  }
}t          |           |                     |
|                     ||                               Gn`# t*          $ rS t          t"          t$          t&          fD ]5}
t-          ||
j                  s|                     |
g                      6Y nw xY wddd           n# 1 swxY w Y   |S )zkParse PSF file into Topology

        Returns
        -------
        MDAnalysis *Topology* object
        r   z({0} is not valid PSF file (header = {1})   NNAMDEXTEXTENDEDSTANDARDr   z!NTITLEz{0} is not a valid PSF filer   zPSF file {0}: format {1}NATOMNBONDr      NTHETANPHINIMPHI)r   filenamenext
startswithformatloggererror
ValueErrorsplit_formatrangeintdebug
_parse_sec_parseatomsr   _parsesectionr   r   r   add_TopologyAttrStopIterationhasattrattrname)selfkwargspsffileheadererrheader_flagstitle_topsectionsattrinfos               g/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/topology/PSFParser.pyparsezPSFParser.parsed   s    T]## 4	7w']]F$$U++ &CJJM6  S!!! oo%!!"":++--L%%%,&&))MMMMM''))E!H	))3::4=IIS!!! oo%3uQx==))  WLL6$-66   //'GQ4;K+LMMC !Q(:;<(Aq$*<=>VQ4+=>?Xq!T-?@AH7"* O OJD$MMM((doogt.L.L)M)MNNNNO ! 7 7 7"FIyA 7 7D"366 7,,TT"XX6667 77a4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7 4	7l 
s8   G4K 
A	IK AJ1.K 0J11K  KKc                 J   |\  }}}}t          |          }|                                dk    r't          |          }|                                dk    '|                                }t          |d                   }|d                             d          }	|	|k    s?d                    ||	          }
t
                              |
           t          |
          t          t          ||z                      }t          j        t           |          } ||||          S )zzParse a single section of the PSF

        Returns
        -------
        A list of Attributes from this section
         r   r   z!:z"Expected section {0} but found {1})r)   stripr/   floatr+   r,   r-   r.   r2   r   	functoolspartial)r;   r=   section_infodesc	atoms_perper_line	parsefuncr>   num	sect_typer?   numlinespsffile_nexts                rG   r4   zPSFParser._parse_sec   s    0<,i9gllnn""']]F llnn""F1I1IOOD))	D  9@@i C LLS//!tC(N++,, (w77yy(;;;    c                    d d d d}|| j                  }d }t          j        |t          j                  }t          j        |t                    }t          j        |t          j                  }	t          j        |t                    }
t          j        |t                    }t          j        |t                    }t          j        |t          j                  }t          j        |t          j                  }t          |          D ]E}	  |            }nB# t          $ r5 | j	         d}t                              |           t          |          dw xY w	  | ||                    }n# t          $ ry |d	         } | ||                    }t                              d
           t                              d                    ||                                                     Y nw xY w|d         ||<   |d         ||<   |d         |	|<   |d         |
|<   |d         ||<   |d         ||<   |d         ||<   |d         ||<   Gt#          |          }t%          |          }t'          |          }t)          |          }t+          |          }t-          |	|
|f|	|
|f          \  }\  }}}t/          |          }t1          |                                          }t5          |          }t7          |          dd         \  }}t9          |          }t;          t=          |          t=          |          t=          |          |||||||||g	||          }|S )a  Parses atom section in a Charmm PSF file.

        Normal (standard) and extended (EXT) PSF format are
        supported. CHEQ is supported in the sense that CHEQ data is simply
        ignored.


        CHARMM Format from ``source/psffres.src``:

        CHEQ::
          II,LSEGID,LRESID,LRES,TYPE(I),IAC(I),CG(I),AMASS(I),IMOVE(I),ECH(I),EHA(I)

          standard format:
            (I8,1X,A4,1X,A4,1X,A4,1X,A4,1X,I4,1X,2G14.6,I8,2G14.6)
            (I8,1X,A4,1X,A4,1X,A4,1X,A4,1X,A4,1X,2G14.6,I8,2G14.6)  XPLOR
          expanded format EXT:
            (I10,1X,A8,1X,A8,1X,A8,1X,A8,1X,I4,1X,2G14.6,I8,2G14.6)
            (I10,1X,A8,1X,A8,1X,A8,1X,A8,1X,A4,1X,2G14.6,I8,2G14.6) XPLOR

        no CHEQ::
          II,LSEGID,LRESID,LRES,TYPE(I),IAC(I),CG(I),AMASS(I),IMOVE(I)

         standard format:
            (I8,1X,A4,1X,A4,1X,A4,1X,A4,1X,I4,1X,2G14.6,I8)
            (I8,1X,A4,1X,A4,1X,A4,1X,A4,1X,A4,1X,2G14.6,I8)  XPLOR
          expanded format EXT:
            (I10,1X,A8,1X,A8,1X,A8,1X,A8,1X,I4,1X,2G14.6,I8)
            (I10,1X,A8,1X,A8,1X,A8,1X,A8,1X,A4,1X,2G14.6,I8) XPLOR

        NAMD PSF

        space separated, see release notes for VMD 1.9.1, psfplugin at
        http://www.ks.uiuc.edu/Research/vmd/current/devel.html :

        psfplugin: Added more logic to the PSF plugin to determine cases where the
        CHARMM "EXTended" PSF format cannot accomodate long atom types, and we add
        a "NAMD" keyword to the PSF file flags line at the top of the file. Upon
        reading, if we detect the "NAMD" flag there, we know that it is possible
        to parse the file correctly using a simple space-delimited scanf() format
        string, and we use that strategy rather than holding to the inflexible
        column-based fields that are a necessity for compatibility with CHARMM,
        CNS, X-PLOR, and other formats. NAMD and the psfgen plugin already assume
        this sort of space-delimited formatting, but that's because they aren't
        expected to parse the PSF variants associated with the other programs. For
        the VMD PSF plugin, having the "NAMD" tag in the flags line makes it
        absolutely clear that we're dealing with a NAMD-specific file so we can
        take the same approach.

        c           
      *   | d d         | dd                                          pd| dd         | dd                                          | d	d
                                          | dd                                          | dd         | dd         fS )N   	      SYSTEM                     !   "   0   >   rK   ls    rG   <lambda>z'PSFParser._parseatoms.<locals>.<lambda>   s    "1"!B$+8"R%"R%  "R%  "R%  "R%"R%	# rX   c           
      *   | d d         | dd                                          pd| dd         | dd                                          | d	d
                                          | dd                                          | dd         | dd         fS )N
      ra   r^      rd   re   %   &   .   /   3   4   B   F   rj   rk   s    rG   rm   z'PSFParser._parseatoms.<locals>.<lambda>   s    #2#"R%  ,H"R%"R%  "R%  "R%  "R%"R%	# rX   c                 :    |                                  d d         S )Nr[   )r/   rk   s    rG   rm   z'PSFParser._parseatoms.<locals>.<lambda>
  s    aggiim rX   )r!   r    r   c                     t          | d                   dz
  | d         pdt          | d                   | d         | d         | d         t          | d                   t          | d	                   fS )
Nr   r   r^   r   r   r$            )r2   r   rL   )xs    rG   rm   z'PSFParser._parseatoms.<locals>.<lambda>  sb    !IIMaDH1JJaDaDaD!A$KK!A$KK	
 rX   )dtypez is not valid PSF fileNr   zWGuessing that this is actually a NAMD-type PSF file... continuing with fingers crossed!zFirst NAMD-type line: {0}: {1}r   r   r   r   r$   r|   r}   r~   )attrsatom_resindexresidue_segindex)r0   npzerosint32objectfloat32float64r1   r8   r(   r,   r-   r.   warningr3   r+   rstripr   r   r   r   r   r
   r   r   copyr   r	   r   r   len)r;   linesrQ   rV   atom_parsersatom_parserset_typeatomidssegidsresidsresnames	atomnames	atomtypeschargesmassesiliner?   valsresidx
new_residsnew_resnamesperres_segids
residueidsresiduenumsresiduenamessegidxperseg_segidsrC   s                                rG   r5   zPSFParser._parseatoms   s   h	 		 	 ,+/
 
2 #4<0	
 	
. (828444(&111("(3338HF333HXV444	HXV444	(82:666("*555x 	  	 A0uww  0 0 0>>>S!!! oo4/0xD 1 122    +62xD 1 1228  
 va//     aGAJQF1IQF1Iq'HQK7IaL7IaLaGAJQF1II '""i((	i((	'"" =JXv&6(B=
 =
99\= J''
joo//00-- !*- 8 8! <&&LL
OOKK
 !#!
 
 
& 
s   
D$$?E#'E<<B G?>G?c           
      ,   g }t          |          D ]}t          j         |                                                      dz
  }t          dt	          |          |          D ]/}|                    t          ||||z                                 0|S )Nr   r   )r1   r   int64r/   r   appendtuple)r;   r   rQ   rV   sectionr   fieldsjs           rG   r6   zPSFParser._parsesectiony  s    x 	A 	AAXeeggmmoo..2F1c&kk955 A AuVAI,=%>??@@@@ArX   N)	__name__
__module____qualname____doc__r+   rH   r4   r5   r6    rX   rG   r   r   J   sf         . F> > >@< < <8w w wr    rX   r   )!r   loggingrM   mathr   numpyr   lib.utilr   r   baser   r	   r
   core.topologyattrsr   r   r   r   r   r   r   r   r   r   r   r   r   core.topologyr   	getLoggerr,   r   r   rX   rG   <module>r      sv  0 ,                $ $ $ $ $ $ $ $ > > > > > > > > > >                              % $ $ $ $ $		4	5	5w w w w w" w w w w wrX   