
:>"^§%  ã               @   s‹   d  Z  d d l m Z d d l m Z d d l m Z d Z Gd d „  d e ƒ Z	 e	 ƒ  Z
 Gd d	 „  d	 e ƒ Z Gd
 d „  d e ƒ Z d S)zOutput of PDB files.é    )Ú
basestring)ÚStructureBuilder)Úatom_weightsz@%s%5i %-4s%c%3s %c%4i%c   %8.3f%8.3f%8.3f%s%6.2f      %4s%2s%2s
c               @   sR   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z d d	 „  Z d
 d „  Z d S)ÚSelectzãSelect everything for PDB output (for use as a base class).

    Default selection (everything) during writing - can be used as base class
    to implement selective output. This selects which entities will be written out.
    c             C   s   d S)z/Represent the output as a string for debugging.z<Select all>© )Úselfr   r   ú2/tmp/pip-build-ww9dw3qa/biopython/Bio/PDB/PDBIO.pyÚ__repr__   s    zSelect.__repr__c             C   s   d S)z*Overload this to reject models for output.é   r   )r   Úmodelr   r   r   Úaccept_model!   s    zSelect.accept_modelc             C   s   d S)z*Overload this to reject chains for output.r
   r   )r   Úchainr   r   r   Úaccept_chain%   s    zSelect.accept_chainc             C   s   d S)z,Overload this to reject residues for output.r
   r   )r   Úresiduer   r   r   Úaccept_residue)   s    zSelect.accept_residuec             C   s   d S)z)Overload this to reject atoms for output.r
   r   )r   Úatomr   r   r   Úaccept_atom-   s    zSelect.accept_atomN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r   r   r      s   r   c               @   s.   e  Z d  Z d Z d d „  Z d d „  Z d S)ÚStructureIOz8Base class to derive structure file format writers from.c             C   s   d S)zInitialise.Nr   )r   r   r   r   Ú__init__8   s    zStructureIO.__init__c             C   s«  | j  d k r | } n†t ƒ  } | j d ƒ | j d ƒ | j  d k ro | j j | j ƒ  ƒ | j |  _ n&| j d ƒ | j  d k r¨ | j d j | j ƒ  ƒ ní | j d ƒ | j  d k ry$ | j	 j
 } | | j d d _
 Wn t k
 rü Yn X| j d d j | j ƒ  ƒ nw | j d	 d d
 d ƒ y' | j	 j	 j
 } | | j d d _
 Wn t k
 roYn X| j d d j d j | j ƒ  ƒ | j } | |  _ d S)z7Check what the user is providing and build a structure.ÚSÚpdbú ÚMr   ÚCÚAÚRZDUMr
   N)Úlevelr   Zinit_structureZinit_segÚ	structureÚaddÚcopyZ
init_modelZ
init_chainÚparentÚidÚ	ExceptionZinit_residueZ
child_list)r   Z
pdb_objectr!   ZsbZ	parent_idr   r   r   Úset_structure<   s8    		!%	zStructureIO.set_structureN)r   r   r   r   r   r'   r   r   r   r   r   5   s   r   c               @   sI   e  Z d  Z d Z d d d „ Z d d d „ Z e d d	 d
 d „ Z d S)ÚPDBIOaÁ  Write a Structure object (or a subset of a Structure object) as a PDB file.

    Examples
    --------
    >>> from Bio.PDB import PDBParser
    >>> from Bio.PDB.PDBIO import PDBIO
    >>> parser = PDBParser()
    >>> structure = parser.get_structure("1a8o", "PDB/1A8O.pdb")
    >>> io=PDBIO()
    >>> io.set_structure(structure)
    >>> io.save("bio-pdb-pdbio-out.pdb")
    >>> import os
    >>> os.remove("bio-pdb-pdbio-out.pdb")  # tidy up


    r   c             C   s   | |  _  d S)z’Create the PDBIO object.

        :param use_model_flag: if 1, force use of the MODEL record in output.
        :type use_model_flag: int
        N)Úuse_model_flag)r   r)   r   r   r   r   v   s    zPDBIO.__init__z  c
             C   sÙ  | d k r d }
 n d }
 | j  rp | j  j ƒ  j ƒ  } | j ƒ  t k r^ t d | j  ƒ ‚ | j d ƒ } n d } | j ƒ  j ƒ  } t | ƒ d k  rÒ | d d	 … j	 ƒ  rÒ t | j ƒ  ƒ d k  rÒ d | } | j
 ƒ  } | j ƒ  \ } } } | j ƒ  } | j ƒ  } y d
 | } Wn t k
 rš| d k rzd } d d l } d d l m } | j d t | j ƒ  ƒ | ƒ n t d | | j ƒ  f ƒ ‚ Yn X|
 | | | | | | | | | | | | | | |	 f } t | S)z$Return an ATOM PDB string (PRIVATE).r   ZHETATMzATOM  zUnrecognised element %ré   z  é   Nr
   z%6.2fé   r   )ÚBiopythonWarningz-Missing occupancy in atom %s written as blankzInvalid occupancy %r in atom %rz      )ÚelementÚstripÚupperÚ
capitalizer   Ú
ValueErrorÚrjustÚget_fullnameÚlenÚisalphaZ
get_altlocZ	get_coordZget_bfactorZget_occupancyÚ	TypeErrorÚwarningsZBior-   ÚwarnÚreprZget_full_idÚ_ATOM_FORMAT_STRING)r   r   ÚhetfieldÚsegidÚatom_numberÚresnameÚresseqÚicodeÚchain_idZchargeZrecord_typer.   ÚnameZaltlocÚxÚyÚzZbfactorZ	occupancyZoccupancy_strr8   r-   Úargsr   r   r   Ú_get_atom_line€   s\    		@

	zPDBIO._get_atom_lineTFc             C   s:  |  j  } t | t ƒ r0 t | d ƒ } d } n | } d } t |  j ƒ d k sZ |  j rc d } n d } x§|  j j ƒ  D]–}	 | j |	 ƒ s‘ qy d }
 | s£ d } | r½ | j	 d |	 j
 ƒ x6|	 j ƒ  D](} | j | ƒ sâ qÊ | j ƒ  } d } xÕ | j ƒ  D]Ç } | j | ƒ sq| j ƒ  \ } } } | j ƒ  } | j ƒ  } x | j ƒ  D]q } | j | ƒ rSd } d }
 | r†| j ƒ  } | | | | | | | | | ƒ } | j	 | ƒ | sS| d 7} qSWqW| rÊ | j	 d | | | | | f ƒ qÊ W| ry |
 ry | j	 d ƒ qy W| r&| j	 d ƒ | r6| j ƒ  d S)	aC  Save structure to a file.

        :param file: output file
        :type file: string or filehandle

        :param select: selects which entities will be written.
        :type select: object

        Typically select is a subclass of L{Select}, it should
        have the following methods:

         - accept_model(model)
         - accept_chain(chain)
         - accept_residue(residue)
         - accept_atom(atom)

        These methods should return 1 if the entity is to be
        written out, 0 otherwise.

        Typically select is a subclass of L{Select}.
        Úwr
   r   zMODEL      %s
zQTER   %5i      %3s %c%4i%c                                                      
zENDMDL
zEND
N)rH   Ú
isinstancer   Úopenr5   r!   r)   Zget_listr   ÚwriteZ
serial_numr   Zget_idZget_unpacked_listr   Zget_resnameZ	get_segidr   Zget_serial_numberÚclose)r   ÚfileÚselectZ	write_endZpreserve_atom_numberingZget_atom_lineÚfpZ
close_fileZ
model_flagr   Zmodel_residues_writtenr>   r   rB   Zchain_residues_writtenr   r<   r@   rA   r?   r=   r   Úsr   r   r   ÚsaveÎ   sp    				z
PDBIO.saveN)r   r   r   r   r   rH   Ú_selectrR   r   r   r   r   r(   d   s   Dr(   N)r   Z	Bio._py3kr   ZBio.PDB.StructureBuilderr   ZBio.Data.IUPACDatar   r;   Úobjectr   rS   r   r(   r   r   r   r   Ú<module>   s   	/