o
    RŀgM(                     @   sh   d Z ddlmZ ddlmZ ddlmZ dd ZG dd deZG d	d
 d
Z	G dd dZ
dd ZdS )a  ASTRAL RAF (Rapid Access Format) Sequence Maps.

The ASTRAL RAF Sequence Maps record the relationship between the PDB SEQRES
records (representing the sequence of the molecule used in an experiment) to
the ATOM records (representing the atoms experimentally observed).

This data is derived from the Protein Data Bank CIF files. Known errors in the
CIF files are corrected manually, with the original PDB file serving as the
final arbiter in case of discrepancies.

Residues are referenced by residue ID. This consists of a the PDB residue
sequence number (up to 4 digits) and an optional PDB insertion code (an
ascii alphabetic character, a-z, A-Z). e.g. "1", "10A", "1010b", "-1"

See "ASTRAL RAF Sequence Maps":http://astral.stanford.edu/raf.html

Dictionary `protein_letters_3to1_extended` provides a mapping from the
3-letter amino acid codes found in PDB files to 1-letter codes. The 3-letter
codes include chemically modified residues.
    )copy)protein_letters_3to1_extended)Residuesc                 C   s   | dkrdS |   S )zConvert RAF one-letter amino acid codes into IUPAC standard codes.

    Letters are uppercased, and "." ("Unknown") is converted to "X".
    .X)upper)one_letter_code r	   @/var/www/html/myenv/lib/python3.10/site-packages/Bio/SCOP/Raf.pynormalize_letters!   s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SeqMapIndexaI  An RAF file index.

    The RAF file itself is about 50 MB. This index provides rapid, random
    access of RAF records without having to load the entire file into memory.

    The index key is a concatenation of the  PDB ID and chain ID. e.g
    "2drcA", ``"155c_"``. RAF uses an underscore to indicate blank
    chain IDs.
    c                 C   sz   t |  || _t| j&}d}	 | }|sn|dd }|dur&|| |< | }qW d   dS 1 s6w   Y  dS )zfInitialize the RAF file index.

        Arguments:
         - filename  -- The file to index

        r   T   N)dict__init__filenameopenreadlinetell)selfr   fpositionlinekeyr	   r	   r
   r   7   s   
"zSeqMapIndex.__init__c                 C   sV   t | |}t| j}|| | }t|}W d   |S 1 s$w   Y  |S )z%Return an item from the indexed file.N)r   __getitem__r   r   seekr   SeqMap)r   r   r   r   r   recordr	   r	   r
   r   L   s   


zSeqMapIndex.__getitem__c                 C   s   t |tr	t|}|j}|j}|sd}d}|D ]J}|d }|dv r#d}|| }| | }d}	t|j}
|d r@t||d |}	|d rPt||d |d }
||	|
 }|du r]|}q||7 }q|S )zGet the sequence map for a collection of residues.

        Arguments:
         - residues -- A Residues instance, or a string that can be
           converted into a Residues instance.

        ))_ r   Nr   )r   - r   r         )	
isinstancestrr   pdbid	fragmentslenresintindex)r   residuesr%   fragsseqMapfragchainididsmstartendr	   r	   r
   	getSeqMapV   s0   


zSeqMapIndex.getSeqMapN)__name__
__module____qualname____doc__r   r   r4   r	   r	   r	   r
   r   ,   s
    

r   c                   @   s\   e Zd ZdZdddZdd Zddd	Zd
d Zdd Zdd Z	dd Z
dd Zdd ZdS )r   a  An ASTRAL RAF (Rapid Access Format) Sequence Map.

    This is a list like object; You can find the location of particular residues
    with index(), slice this SeqMap into fragments, and glue fragments back
    together with extend().

    Attributes:
     - pdbid -- The PDB 4 character ID
     - pdb_datestamp -- From the PDB file
     - version -- The RAF format version. e.g. 0.01
     - flags -- RAF flags. (See release notes for more information.)
     - res -- A list of Res objects, one for each residue in this sequence map

    Nc                 C   s4   d| _ d| _d| _d| _g | _|r| | dS dS zInitialize the class.r   N)r%   pdb_datestampversionflagsr(   _process)r   r   r	   r	   r
   r      s   zSeqMap.__init__c                 C   s  d}|  }t||k rtd| |dd | _|dd }|dd | _| jdkr7| jd	kr7td
| j |dd | _|dd | _t|t|dD ]?}|||d  }t|dkretd| d t }||_	|dd 
 |_t|dd |_t|dd |_| j| qMdS )z2Parse a RAF record into a SeqMap object (PRIVATE).&   zIncomplete header: r      r      
   z0.01z0.02zIncompatible RAF version:                zCorrupt Field: ()N)rstripr'   
ValueErrorr%   r;   r:   r<   rangeResr/   stripresidr   atomseqresr(   append)r   r   
header_lenr/   ir   rr	   r	   r
   r=      s,   zSeqMap._processr   c                 C   sL   t t| jD ]}| j| j|kr| j| j|kr|  S qtd| | )z?Return the index of the SeqMap for the given resid and chainid.zNo such residue )rJ   r'   r(   rM   r/   KeyError)r   rM   r/   rR   r	   r	   r
   r*      s
    zSeqMap.indexc                 C   s&   t |tstt| }|j| |_|S )z,Extract a single Res object from the SeqMap.)r#   sliceNotImplementedErrorr   r(   )r   r*   sr	   r	   r
   r      s
   
zSeqMap.__getitem__c                 C   s   | j | dS )z<Append another Res object onto the list of residue mappings.N)r(   rP   )r   r(   r	   r	   r
   rP      s   zSeqMap.appendc                 C   st   t |ts	td| j|jkrtd| j|jkrtd| j|jkr'td| j|jkr0d| _|  j|j7  _dS )a  Append another SeqMap onto the end of self.

        Both SeqMaps must have the same PDB ID, PDB datestamp and
        RAF version.  The RAF flags are erased if they are inconsistent. This
        may happen when fragments are taken from different chains.
        z'Can only extend a SeqMap with a SeqMap.z,Cannot add fragments from different proteinszIncompatible rafszDifferent pdb dates!r   N)r#   r   	TypeErrorr%   r;   r:   r<   r(   r   otherr	   r	   r
   extend   s   
zSeqMap.extendc                 C   s   |  | | S )z$In place addition of SeqMap objects.)r[   rY   r	   r	   r
   __iadd__   s   
zSeqMap.__iadd__c                 C   s   t | }|| |S )zAddition of SeqMap objects.)r   r[   )r   rZ   rW   r	   r	   r
   __add__   s   
zSeqMap.__add__c                 C   s   i }| j D ]}|jdkrq|j}|dkrd}|j}||||f< qi }|D ]=}|dra|dd }|dd  }||f}	|	|v ra||	 }
|
j}|dd	 }|tv rat| |kra|| |
||	< q$t|t|krnt	d
dS )a5  Extract all relevant ATOM and HETATOM records from a PDB file.

        The PDB file is scanned for ATOM and HETATOM records. If the
        chain ID, residue ID (seqNum and iCode), and residue type match
        a residue in this sequence map, then the record is echoed to the
        output handle.

        This is typically used to find the coordinates of a domain, or other
        residue subset.

        Arguments:
         - pdb_handle -- A handle to the relevant PDB file.
         - out_handle -- All output is written to this file like object.

        r   r   r    )zATOM  HETATMrD      rE      rC   zbCould not find at least one ATOM or HETATM record for each and every residue in this sequence map.N)
r(   rN   r/   rM   
startswithrL   r   writer'   RuntimeError)r   
pdb_handle
out_handleresSetrS   r/   rM   resFoundr   r   r(   atom_aaresNamer	   r	   r
   getAtoms   s:   



zSeqMap.getAtoms)N)r   )r5   r6   r7   r8   r   r=   r*   r   rP   r[   r\   r]   rj   r	   r	   r	   r
   r      s    


!r   c                   @   s   e Zd ZdZdd ZdS )rK   a  A single residue mapping from a RAF record.

    Attributes:
     - chainid -- A single character chain ID.
     - resid   -- The residue ID.
     - atom    -- amino acid one-letter code from ATOM records.
     - seqres  -- amino acid one-letter code from SEQRES records.

    c                 C   s   d| _ d| _d| _d| _dS r9   )r/   rM   rN   rO   )r   r	   r	   r
   r   /  s   
zRes.__init__N)r5   r6   r7   r8   r   r	   r	   r	   r
   rK   $  s    
rK   c                 c   s    | D ]}t |V  qdS )zuIterate over RAF file, giving a SeqMap object for each line.

    Arguments:
     - handle -- file-like object.

    N)r   )handler   r	   r	   r
   parse7  s   rl   N)r8   r   Bio.Data.PDBDatar   Bio.SCOP.Residuesr   r   r   r   r   rK   rl   r	   r	   r	   r
   <module>   s   U $