o
    Rŀg<                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 dZdZd	ZG d
d dZe ZG dd dZG dd deZdS )zOutput of PDB files.    N)BiopythonWarning)atom_weights)PDBIOException)StructureBuilderz@%s%5i %-4s%c%3s %c%4i%c   %8.3f%8.3f%8.3f%s%6.2f      %4s%2s%2s
z<%s%5i %-4s%c%3s %c%4i%c   %8.3f%8.3f%8.3f %7s  %6s      %2s
zQTER   %5i      %3s %c%4i%c                                                      
c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )SelectzSelect 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      dS )z/Represent the output as a string for debugging.z<Select all> selfr   r   A/var/www/html/myenv/lib/python3.10/site-packages/Bio/PDB/PDBIO.py__repr__#      zSelect.__repr__c                 C   r   )z*Overload this to reject models for output.   r   )r
   modelr   r   r   accept_model'   r   zSelect.accept_modelc                 C   r   )z*Overload this to reject chains for output.r   r   )r
   chainr   r   r   accept_chain+   r   zSelect.accept_chainc                 C   r   )z,Overload this to reject residues for output.r   r   )r
   residuer   r   r   accept_residue/   r   zSelect.accept_residuec                 C   r   )z)Overload this to reject atoms for output.r   r   )r
   atomr   r   r   accept_atom3   r   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   r   )zInitialise.Nr   r	   r   r   r   __init__>   s    zStructureIO.__init__c                 C   s@  |j dkr|}nt }|d |d |j dkr'|j|  |j| _nq|d |j dkr<|jd |  n\d}|| |j dkrh|j	d	ur[|j	j
}||jd | _
|}|jd | |  n0|d
ddd |jd | jd |  z|j	j	j
}W n	 ty   Y n	w ||jd | _
|j}|| _d	S )z7Check what the user is providing and build a structure.Spdb Mr   CARNDUMr   )levelr   init_structureinit_seg	structureaddcopy
init_model
init_chainparentidinit_residue
child_listAttributeError)r
   
pdb_objectr(   sbchain_idog_chain_idr   r   r   set_structureA   s:   










zStructureIO.set_structureN)r   r   r   r   r   r6   r   r   r   r   r   ;   s    r   c                   @   sD   e Zd ZdZdddZ	dddZedd
dZeddfddZ	d	S )PDBIOa  Write a Structure object (or a subset of a Structure object) as a PDB or PQR 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   Fc                 C   s   || _ || _dS )zCreate the PDBIO object.

        :param use_model_flag: if 1, force use of the MODEL record in output.
        :type use_model_flag: int
        :param is_pqr: if True, build PQR file. Otherwise build PDB file.
        :type is_pqr: Boolean
        N)use_model_flagis_pqr)r
   r8   r9   r   r   r   r      s   
zPDBIO.__init__  c
                 C   sj  |dkrd}
nd}
zt |}W n ty   t|dw |dkr*td| d|jrL|j  }| tvrF|dkrFtd	|j |d
}nd}|j }t	|dk rm|dd 
 rmt	| d
k rmd| }|j}|j\}}}| js|j}z|jd}W n' ttfy   |jdu rd}td|jdt n	td|jdY nw |
|||||||||||||||	f}t| S z|jd}W n' ttfy   |jdu rd}td|j dt n	td|jdY nw z|jd}W n) ttfy    |jdu rd}td|j dt n	td|j dY nw |
|||||||||||||f}t| S )z$Return an ATOM PDB string (PRIVATE).r   HETATMzATOM  z is not a number.Atom serial numbers must be numerical If you are converting from an mmCIF structure, try using preserve_atom_numbering=Falsei zAtom serial number ('') exceeds PDB format limit.XzUnrecognised element    r:      Nr   z6.2fz      zMissing occupancy in atom z written as blankzInvalid occupancy value: z7.4fz       zMissing PQR charge in atom zInvalid PQR charge value: z6.4fzMissing radius in atom zInvalid radius value: )int
ValueErrorelementstripupper
capitalizer   rjustfullnamelenisalphaaltloccoordr9   bfactor	occupancy	TypeErrorwarningswarnfull_idr   _ATOM_FORMAT_STRING
pqr_chargeradius_PQR_ATOM_FORMAT_STRING)r
   r   hetfieldsegidatom_numberresnameresseqicoder4   chargerecord_typerB   namerJ   xyzrL   rM   argsrS   rT   r   r   r   _get_atom_line   s   	

,



zPDBIO._get_atom_lineNc              
   C   sl   |r#z|    t| j W dS  ty" } zW Y d}~dS d}~ww |dur2| | |   dS td)zRevert data written to file by removing the file or truncating it.

        This method is used when the writer throws an exception, to avoid
        writing incomplete files that might confuse users or workflows.
        Nz6One of 'truncate_to' or 'delete_file' must be provided)closeosremover^   OSErrorseektruncate	Exception)handletruncate_todelete_fileerrr   r   r   _revert_write#  s   
zPDBIO._revert_writeTc                 C   s  t |trt|d}n| }|}| j}t| jdks| jr!d}nd}| j D ] }	|	|	s1q(d}
|s7d}|rC|
d|	j d |	 D ]}||sOqG|j}t|dkrtt |tre| j|dd n| j||d td	| d
d}| D ]}||sqz|j\}}}|j}|j}|jd }|dkrt |tr| j|dd n| j||d td| d
| D ]U}||sqd}d}
|r|j}z|||||||||}W n- ty } z t |tr| j|dd n| j||d td|j d| |d}~ww |
| |d7 }qqz|r|
t|||||f  qG|r)|
r)|
d q(|r2|
d t |tr>|  dS 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      
T)rm   )rl   zChain id ('r<   i'  zResidue number ('zError when writing atom z: NzENDMDL
zEND   
)
isinstancestropentellrc   rH   r(   r8   get_listr   write
serial_numr   r.   ro   r   get_unpacked_listr   rY   rW   r   serial_numberrj   rQ   _TER_FORMAT_STRINGrd   )r
   fileselect	write_endpreserve_atom_numberingfhandlefd_positionget_atom_line
model_flagr   model_residues_writtenrX   r   r4   chain_residues_writtenr   rV   rZ   r[   rY   rW   residr   srn   r   r   r   save>  s   













"

z
PDBIO.save)r   F)r:   )NF)
r   r   r   r   r   rc   staticmethodro   _selectr   r   r   r   r   r7   t   s    

 r7   )r   re   rO   Bior   Bio.Data.IUPACDatar   Bio.PDB.PDBExceptionsr   Bio.PDB.StructureBuilderr   rR   rU   r{   r   r   r   r7   r   r   r   r   <module>   s"   9