
     i7#                     l    d Z ddlZddlmZ ddlmZ ddZ G d	 d
e          Z	 G d de	          Z
dS )a  
Base classes for the selection writers
======================================

Specialized SelectionWriters are derived from
:class:`SelectionWriterBase`. Override the :meth:`~SelectionWriterBase._write_head`,
:meth:`~SelectionWriterBase._translate`, and :meth:`~SelectionWriterBase._write_tail`
methods.

.. autoclass:: SelectionWriterBase
   :members: __init__, write, _translate, _write_head, _write_tail, comment

.. autofunction:: join

    N   )util   )_SELECTION_WRITERS c                 d    d fd| dd         D              | d                   gz   S )zCreate a list from sequence.

    *string* is appended to each element but the last.

    *func* is applied to every element before appending *string*.
    Nc                     | S N )xs    d/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/selections/base.py<lambda>zjoin.<locals>.<lambda>6   s         c                 ,    g | ]} |          z   S r   r   ).0r   funcstrings     r   
<listcomp>zjoin.<locals>.<listcomp>7   s&    ///DDGGf///r   r   )seqr   r   s    ``r   joinr   .   sJ     |{/////c#2#h///44B==/AAr   c                       e Zd Zd ZdS )_Selectionmetac                     t                               t           |||           	 t          j        |d                   }|D ]#}||                                }| t
          |<   $d S # t          $ r Y d S w xY w)Nformat)type__init__r   
asiterableupperr   KeyError)clsnamebases	classdictfmtfs         r   r   z_Selectionmeta.__init__<   s    dD%333		,/)H"566C  , ,9GGII(+"1%%	, ,  	 	 	DD	s   A& &
A43A4N)__name__
__module____qualname__r   r   r   r   r   r   :   s#        , , , , ,r   r   c                   h    e Zd ZdZdZdZdZdZdZ	 ddZ	d Z
d Zdd	Zd
 Zd Zd Zd Zd Zd ZdS )SelectionWriterBasea'  Export a selection in MDAnalysis to a format usable in an external package.

    The :class:`SelectionWriterBase` writes a selection string to a file
    that can be used in another package such as `VMD`_, `PyMOL`_,
    `Gromacs`_ or `CHARMM`_. In this way, analysis and visualization
    can be done with the best or most convenient tools at hand.

    :class:`SelectionWriterBase` is a base class and child classes are
    derived with the appropriate customizations for the package file
    format.

    .. _VMD: http://www.ks.uiuc.edu/Research/vmd/
    .. _PyMol: http://www.pymol.org/
    .. _CHARMM:  http://www.charmm.org/
    .. _Gromacs: http://www.gromacs.org/

    .. versionchanged:: 0.11.0
       Can now also write to a :class:`~MDAnalysis.lib.util.NamedStream` instead
       of a normal file (using :class:`~MDAnalysis.lib.util.openany`).

    .. versionchanged:: 0.16.0
       Remove the `wa` mode. The file is now open when the instance is created
       and closed with the :meth:`close` method or when exiting the `with`
       statement.
    Nr      wc                    t          j        || j                  | _        |dvr"t          d                    |                    || _        |d         | _        ||dk     r| j        | _        n|du rd| _        n|| _        || _	        || _
        d| _        t          j        | j        | j                  | _        |                                  dS )aN  Set up for writing to *filename*.

        Parameters
        ----------
        filename:
            output file
        mode:
            create a new file ("w"), or append ("a") to existing file ["w"]
        numterms:
            number of individual index numbers per line for output
            formats that write multiple entries in one line. If set
            to 0 or ``False`` then no special formatting is done  [8]
        preamble:
            string that is written as a comment at the top of the file []
        kwargs:
            use as defaults for :meth:`write`
        )ext)ar-   z'mode must be one of 'w', 'a', not {0!r}r   NF)mode)r   filenamer/   
ValueErrorr   r1   _current_modedefault_numtermsnumtermspreamble	otherargsnumberanyopen_outfilewrite_preamble)selfr2   r1   r6   r7   kwargss         r   r   zSelectionWriterBase.__init__o   s    ( hDH===z!!9@@FF   	!!Wx!|| 1DMMDMM$DM T]9KLLLr   c                 .    | j         dS | j         |z  dz   S )a:  Return string *s* interpolated into the comment format string.

        If no :attr:`SelectionWriterBase.commentfmt` is defined (None) then the
        empty string is returned because presumably there is no way to enter
        comments into the file.

        A newline is appended to non-empty strings.
        Nr   
)
commentfmt)r=   ss     r   commentzSelectionWriterBase.comment   s#     ?"2"T))r   c                 |    | j         dS | j                            |                     | j                              dS )z-Write a header, depending on the file format.N)r7   r;   writerC   r=   s    r   r<   z"SelectionWriterBase.write_preamble   s9    = FDLL7788888r   c                    |j         }||j        |          n%	 |j        j        j        }n# t          $ r d}Y nw xY w|p| j                            dd          }|2|| xj        dz  c_        | j        } dj        d	i t                      }| 
                    |j                  }| j        pt          |j                  }| j        }	|                     |	|           t!          dt          |j                  |          D ]}
||
|
|z            }|	                    d                    |                     t          |          |k    r;|
|z   t          |j                  k    s |	                    d| j        z   dz              |	                    d           |                     |	           |	                    d           dS )
a  Write selection to the output file.

        Parameters
        ----------
        selection:
            a :class:`MDAnalysis.core.groups.AtomGroup`
        number:
            selection will be named "mdanalysis<number>"
            (``None`` auto increments between writes; useful
            when appending) [``None``]
        name:
            selection will be named *name* (instead of numbered) [``None``]
        frame:
            write selection of this frame (or the current one if
            ``None`` [``None``]
        Nr   r"   zmdanalysis{number:03d})r"   r    r@   r   )universe
trajectorytsframeAttributeErrorr8   getr9   r   vars
_translateatomsr6   lenr;   _write_headrangerE   r   continuation_write_tail)r=   	selectionr9   r"   rL   r1   uselection_termsstepoutiatomlines               r   rE   zSelectionWriterBase.write   s   " L-!   7t~))&$77<~q 2+2<<TVV<<D //)/::}4IO 4 4m4(((1c)/22D99 	: 	:E"554<#78DIIchhtnn%%%4yyD  Y_9M9M)M)M		# 11D8999			
 	
 	
 			$s   + ::c                 8    | j                                          dS )z9Close the file

        .. versionadded:: 0.16.0
        N)r;   closerF   s    r   r_   zSelectionWriterBase.close   s    
 	r   c                     t           )aX  Translate atoms into a list of native selection terms.

        - build list of ALL selection terms as if this was a single line, e.g.
          ``['index 12 |', 'index 22 |', 'index 33']``
        - only one term per atom!!
        - terms *must* be strings
        - something like::
             " ".join(terms)

          must work
        )NotImplementedError)r=   rQ   r>   s      r   rP   zSelectionWriterBase._translate   s
     "!r   c                     dS )z)Initial output to open file object *out*.Nr   r=   r[   r>   s      r   rS   zSelectionWriterBase._write_head       r   c                     dS )z&Last output to open file object *out*.Nr   rc   s      r   rV   zSelectionWriterBase._write_tail   rd   r   c                     | S r
   r   rF   s    r   	__enter__zSelectionWriterBase.__enter__   s    r   c                 .    |                                   d S r
   )r_   )r=   excs     r   __exit__zSelectionWriterBase.__exit__   s    

r   )r-   NN)NNNN)r'   r(   r)   __doc__r   r/   rU   rA   r5   r   rC   r<   rE   r_   rP   rS   rV   rg   rj   r   r   r   r+   r+   J   s         6 F
CLJ ;?' ' ' 'R* * *9 9 90 0 0 0d  " " "          r   r+   )	metaclass)r   N)rk   os.pathoslibr   r   r   r   r   r   r+   r   r   r   <module>rp      s   2                    	B 	B 	B 	B, , , , ,T , , , v v v v vN v v v v v vr   