
     i,                         d 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	m
Z
  G d d	e
j                  Z G d
 de
j                  ZdS )a  CRD structure files in MDAnalysis --- :mod:`MDAnalysis.coordinates.CRD`
===========================================================================

Read and write coordinates in CHARMM CARD coordinate format (suffix
"crd"). The CHARMM "extended format" is handled automatically.

    N   )NoDataError)util   )basec                   ,    e Zd ZdZdZdddZd Zd ZdS )	CRDReaderzCRD reader that implements the standard and extended CRD coordinate formats

    .. versionchanged:: 0.11.0
       Now returns a ValueError instead of FormatError.
       Frames now 0-based instead of 1-based.
    CRDNAngstromtimelengthc           	      `   g }t          j        | j                  5 }d}d}t          |          D ]k\  }}|                                                    d          s|                                dk    rF|                                }t          |          dk    r"t          |d                   }|d         dk    }	 |rQ|	                    t          j        |dd	                                         dd
         t                               nP|	                    t          j        |dd                                         dd
         t                               5# t          $ r+ d| d|                                 }t          |          d w xY w	 d d d            n# 1 swxY w Y   t          |          | _         | j        j        t          j        |          fi | j        | _        d| j        _        | j        |k    rt          d| j        | j        |fz            d S )NFr   * r   EXT-   d      dtype   2   zCheck CRD format at line z: zUFound %d coordinates in %r but the header claims that there should be %d coordinates.)r   openanyfilename	enumeratestrip
startswithsplitlenintappendnparrayfloat	Exceptionrstrip
ValueErrorn_atoms	_Timestepfrom_coordinates
_ts_kwargstsframe)	selfcoords_listcrdfileextendednatomslinenumlinefieldserrmsgs	            d/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/coordinates/CRD.py_read_first_framezCRDReader._read_first_frame4   s    \$-(( 	7GHF!*7!3!3 7 7::<<**3// 4::<<23E3Ev;;!## ^^F%bzU2H7 #**HT"S&\%7%7%9%9!A#%>eLLL    $**HT"R%[%6%6%8%81%=UKKK   ! 7 7 7+G + +;;==+ +  %V,,$67'7	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7: ;''1$.1H[!!
 
%)_
 
 
 <6!!,<78   "!s+   B#F* B#E%#F*%5FF**F.1F.c                     t          |fi |S )zReturns a CRDWriter for *filename*.

        Parameters
        ----------
        filename: str
            filename of the output CRD file

        Returns
        -------
        :class:`CRDWriter`

        )	CRDWriterr0   r   kwargss      r9   WriterzCRDReader.Writerl   s     ,,V,,,    )__name__
__module____qualname____doc__formatunitsr:   r?    r@   r9   r	   r	   )   sQ          FZ00E6 6 6p- - - - -r@   r	   c                   >    e Zd ZdZdZdddZdddd	d
dZd ZddZdS )r<   a  CRD writer that implements the CHARMM CRD coordinate format.

    It automatically writes the CHARMM EXT extended format if there
    are more than 99,999 atoms.

    Requires the following attributes to be present:
    - resids
    - resnames
    - names
    - chainIDs
    - tempfactors

    .. versionchanged:: 0.11.0
       Frames now 0-based instead of 1-based
    .. versionchanged:: 2.2.0
       CRD extended format can now be explicitly requested with the
       `extended` keyword
    .. versionchanged:: 2.6.0
       Files are now written in `wt` mode, and keep extensions, allowing
       for files to be written under compressed formats
    r
   Nr   r   z{serial:10d}{totRes:10d}  {resname:<8.8s}  {name:<8.8s}{pos[0]:20.10f}{pos[1]:20.10f}{pos[2]:20.10f}  {chainID:<8.8s}  {resSeq:<8d}{tempfactor:20.10f}
z{0:10d} EXT
z{serial:5d}{totRes:5d} {resname:<4.4s} {name:<4.4s}{pos[0]:10.5f}{pos[1]:10.5f}{pos[2]:10.5f} {chainID:<4.4s} {resSeq:<4d}{tempfactor:10.5f}
z* FRAME {frame} FROM {where}
z{0:5d}
)ATOM_EXTNUMATOMS_EXTATOMTITLENUMATOMSc                     t          j        |dd          | _        d| _        |                    dd          | _        dS )a4  
        Parameters
        ----------
        filename : str or :class:`~MDAnalysis.lib.util.NamedStream`
             name of the output file or a stream

        extended : bool (optional)
             By default, noextended CRD format is used [``False``].
             However, extended CRD format can be forced by
             specifying `extended` ``=True``. Note that the extended format
             is *always* used if the number of atoms exceeds 99,999, regardless
             of the setting of `extended`.

             .. versionadded:: 2.2.0
        crdT)extkeepNr3   F)r   r   rO   popr3   r=   s      r9   __init__zCRDWriter.__init__   s>    " hEEEE 

:u55r@   c                    	 |j         }n # t          $ r d}t          |          dw xY w||j        |          n%	 |j        j        j        }n# t          $ r d}Y nw xY w|j        }|j        }t          |          }| j	        s|dk    r| j
        d         }d}	d}
d}n| j
        d         }d	}	d
}
d	}i }g }dt          j        d          fdt          j        |t                    fdt          j        d          fdt          j        d          ffD ]J\  }}	 t!          ||          ||<   # t"          t          f$ r |||<   |                    |           Y Gw xY w	 |j        |d<   nf# t"          t          f$ rR 	 |j        |d<   nC# t"          t          f$ r/ t          j        d          |d<   |                    |           Y nw xY wY nw xY w|r;t+          j        d                    d                    |                               t3          j        | j        d          5 }|                    | j
        d                             ||j        j                             |                    d           | j	        s|dk    r4|                    | j
        d                             |                     n3|                    | j
        d                             |                     d}|d         }t;          t=          |          ||d         |d         |d         |d         |d                   D ]\  }}}}}}}|dk    s||         ||dz
           k    r|dz  }t3          j        |dz   |	          }t3          j        ||
          }t3          j        ||          }|                    |                    ||||||||                     	 ddd           dS # 1 swxY w Y   dS ) a1  Write selection at current trajectory frame to file.

        Parameters
        ----------
        selection : AtomGroup
             group of atoms to be written
        frame : int (optional)
             Move the trajectory to frame `frame`; by default, write
             the current frame.

        z-Input obj is neither an AtomGroup or UniverseNr   i rI   
      rK         resnames)UNKresidsr   names)Xtempfactors)g        chainIDs)r   zlSupplied AtomGroup was missing the following attributes: {miss}. These will be written with default values. z, )misswtrL   )r/   wherez*
rJ   rM   r   )serialtotResresnamenameposchainIDresSeq
tempfactor) universeAttributeError	TypeError
trajectoryr.   r/   atoms	positionsr!   r3   fmt	itertoolscycler$   onesr"   getattrr   r#   r_   segidswarningswarnrE   joinr   r   r   writeziprangeltruncate_int)r0   	selectionr/   ur8   ro   coorr*   at_fmt
serial_len	resid_len
totres_lenattrsmissing_topologyattrdefaultrO   current_residr[   irg   re   rf   rh   residrj   rc   s                              r9   rz   zCRDWriter.write   s   	."AA 	. 	. 	.DFF##-	. L-!    O 	 e** = 		GeOOXj)FJIJJXf%FJIJ 223rwwc2223iof--.IOF334
 	. 	.MD'.%eT22d0 . . .%d ''-----.	. %E*^, 	. 	. 	..$)Lj!!0 . . .$-OE$:$:j! ''-----.		.  	M6tyy)9::6;;   \$-.. /	#II!((q|'< )    
 IIe } @%		$(>299'BBCCCC		$(:.55g>>??? M8_FEHgj!gj!hm$F F  A3wz Avv&)va!e}"<"<!Q&M +AE:>>*5)<< $ 2=* M M		MM%, '! '$#- " 	 	   #'/	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	st   
 'A AAD+++EE
E( (G:
FG=GGGG
G%F0O##O'*O')N)	rA   rB   rC   rD   rE   rF   rq   rS   rz   rG   r@   r9   r<   r<   |   s         , FZ00EA (? 2# C(6 6 6.} } } } } }r@   r<   )rD   rr   numpyr$   rw   
exceptionsr   libr   r   r   SingleFrameReaderBaser	   
WriterBaser<   rG   r@   r9   <module>r      s   2           $ $ $ $ $ $            P- P- P- P- P-* P- P- P-fB B B B B B B B B Br@   