
Ĳ	Ic           @   s  d  Z  d d l m Z d d l Td d l m Z i d d 6d d 6d	 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d! d" 6d# d$ 6d% d& 6d' d( 6d) d* 6d+ d, 6d d- 6d! d. 6d d/ 6d d0 6d d1 6d d2 6d d3 6d d4 6d d5 6d d6 6d d7 6d d8 6d d9 6d d: 6d d; 6d# d< 6d d= 6d d> 6d d? 6d# d@ 6d dA 6d# dB 6d# dC 6d# dD 6d# dE 6d dF 6d dG 6d# dH 6d dI 6d# dJ 6d# dK 6d# dL 6d# dM 6d dN 6d# dO 6d# dP 6d# dQ 6d# dR 6d# dS 6d	 dT 6d dU 6d dV 6d dW 6d# dX 6d dY 6d' dZ 6d d[ 6d! d\ 6d d] 6d d^ 6d d_ 6d d` 6d da 6d	 db 6d dc 6d dd 6d de 6d df 6d) dg 6d dh 6d di 6d# dj 6d dk 6d dl 6d dm 6d+ dn 6d do 6d+ dp 6d dq 6d dr 6d! ds 6d! dt 6d du 6d	 dv 6d) dw 6d dx 6d dy 6d dz 6d d{ 6d d| 6d d} 6d) d~ 6d d 6d d 6d d 6d% d 6d! d 6d d 6d d 6d+ d 6d+ d 6d d 6d d 6d! d 6d! d 6d d 6d d 6d d 6d+ d 6d d 6d# d 6d d 6d d 6d d 6d# d 6d	 d 6d	 d 6d# d 6d d 6d d 6d d 6d+ d 6d# d 6d# d 6d# d 6d d 6d d 6d d 6d# d 6d d 6d# d 6d d 6d d 6d d 6d) d 6d d 6d d 6d d 6d) d 6d d 6d d 6d d 6d d 6d d 6d+ d 6d# d 6d d 6d d 6d# d 6d d 6d d 6Z d   Z d e f d     YZ d d d     YZ d d d     YZ	 d   Z
 d d d     YZ d d d     YZ d S(   s  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 (upto 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

to_one_letter_code -- 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.
i(   t   copy(   t   *(   t   Residuest   At   ALAt   Vt   VALt   Ft   PHEt   Pt   PROt   Mt   METt   It   ILEt   Lt   LEUt   Dt   ASPt   Et   GLUt   Kt   LYSt   Rt   ARGt   St   SERt   Tt   THRt   Yt   TYRt   Ht   HISt   Ct   CYSt   Nt   ASNt   Qt   GLNt   Wt   TRPt   Gt   GLYt   2ASt   3AHt   5HPt   ACLt   AIBt   ALMt   ALOt   ALYt   ARMt   ASAt   ASBt   ASKt   ASLt   ASQt   AYAt   BCSt   BHDt   BMTt   BNNt   BUCt   BUGt   C5Ct   C6Ct   CCSt   CEAt   CHGt   CLEt   CMEt   CSDt   CSOt   CSPt   CSSt   CSWt   CXMt   CY1t   CY3t   CYGt   CYMt   CYQt   DAHt   DALt   DARt   DASt   DCYt   DGLt   DGNt   DHAt   DHIt   DILt   DIVt   DLEt   DLYt   DNPt   DPNt   DPRt   DSNt   DSPt   DTHt   DTRt   DTYt   DVAt   EFCt   FLAt   FMEt   GGLt   GLZt   GMAt   GSCt   HACt   HARt   HICt   HIPt   HMRt   HPQt   HTRt   HYPt   IILt   IYRt   KCXt   LLPt   LLYt   LTRt   LYMt   LYZt   MAAt   MENt   MHSt   MISt   MLEt   MPQt   MSAt   MSEt   MVAt   NEMt   NEPt   NLEt   NLNt   NLPt   NMCt   OASt   OCSt   OMTt   PAQt   PCAt   PECt   PHIt   PHLt   PR3t   PRRt   PTRt   SACt   SARt   SCHt   SCSt   SCYt   SELt   SEPt   SETt   SHCt   SHRt   SOCt   STYt   SVAt   TIHt   TPLt   TPOt   TPQt   TRGt   TROt   TYBt   TYQt   TYSt   TYYt   AGMt   GL3t   SMCt   Bt   ASXt   CGUt   CSXt   Zt   GLXt   Xt   UNKc         C   s   |  d k r d S|  j    Sd S(   s   Convert RAF one-letter amino acid codes into IUPAC standard codes.
    
    Letters are uppercased, and "." ("Unknown") is converted to "X".
    t   .R   N(   t   upper(   t   one_letter_code(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   normalize_lettersI   s    t   SeqMapIndexc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   sI  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   s   t  j |   | |  _ t |  j  } z_ d } xR t r | j   } | sM Pn  | d d !} | d k rs | |  | <n  | j   } q1 WWd | j   Xd S(   sN   
        Arguments:
        
          filename  -- The file to index
        i    i   N(	   t   dictt   __init__t   filenamet   opent   Truet   readlinet   Nonet   tellt   close(   t   selfR   t   ft   positiont   linet   key(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR   ^   s    		 c         C   s\   t  j |  |  } t |  j  } z) | j |  | j   } t |  } Wd | j   X| S(   s'    Return an item from the indexed file. N(   R   t   __getitem__R   R   t   seekR   t   SeqMapR   (   R   R   R   R   R   t   record(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR   t   s    c         C   sK  t  |  t k r! t |  } n  | j } | j } | sB d	 } n  d
 } x | D] } | d } | d k s | d k s | d k s | d k r d } n  | | } |  | } d }	 t | j  }
 | d r t | j	 | d |   }	 n  | d rt | j	 | d |  d  }
 n  | |	 |
 !} | d
 k r9| } qO | | 7} qO W| S(   s   Get the sequence map for a collection of residues.

        residues -- A Residues instance, or a string that can be converted into
                    a Residues instance.
        t   _t    i    t   -t    i   i   (   R   R   R   (   (   R   R   R   N(
   t   typet
   StringTypeR   t   pdbidt	   fragmentsR   t   lent   rest   intt   index(   R   t   residuesR   t   fragst   seqMapt   fragt   chainidt   idt   smt   startt   end(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt	   getSeqMap   s0    		 	
0	


 
 #	(   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR   S   s   			R   c           B   se   e  Z d  Z d d  Z d   Z d d  Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z RS(   s  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().

    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
    c         C   sG   d |  _  d |  _ d |  _ d |  _ g  |  _ | rC |  j |  n  d  S(   NR   (   R   t   pdb_datestampt   versiont   flagsR   t   _process(   R   R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR      s    					c         C   s}  d } | j    } t |  | k  r7 t d |   n  | d d !|  _ | d d !} | d d !|  _ |  j d k r |  j d	 k r t d
 |  j   n  | d d !|  _ | d d !|  _ x t | t |  d  D] } | | | d !} t |  d k rt d | d   n  t   } | | _	 | d d !j
   | _ t | d d ! | _ t | d d ! | _ |  j j |  q Wd S(   s2   Parses a RAF record into a SeqMap object.
        i&   s   Incomplete header: i    i   i   i   i
   s   0.01s   0.02s   Incompatible RAF version: i   i   i   i   i   s   Corrupt Field: (t   )N(   t   rstripR   t
   ValueErrorR   R   R   R   t   ranget   ResR   t   stript   residR   t   atomt   seqresR   t   append(   R   R   t
   header_lenR   t   iR   t   r(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR      s*    		R   c         C   sk   xP t  d t |  j   D]6 } |  j | j | k r |  j | j | k r | Sq Wt d | |   d  S(   Ni    s   No such residue (   R   R   R   R   R   t   KeyError(   R   R   R   R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR      s    ,c         C   s#   t  |   } | j | | !| _ | S(   N(   R    R   (   R   R   t   jt   s(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   __getslice__   s    c         C   s   |  j  j |  d S(   s<   Append another Res object onto the list of residue mappings.N(   R   R   (   R   R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR      s    c         C   s   t  | t  s t d   n  |  j | j k r? t d   n  |  j | j k r` t d   n  |  j | j k r t d   n  |  j | j k r d |  _ n  |  j | j 7_ d S(   s  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.
        s'   Can only extend a SeqMap with a SeqMap.s,   Cannot add fragments from different proteinss   Incompatible rafss   Different pdb dates!R   N(   t
   isinstanceR   t	   TypeErrorR   R   R   R   R   (   R   t   other(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   extend   s    c         C   s   |  j  |  |  S(   N(   R  (   R   R  (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   __iadd__  s    c         C   s   t  |   } | j |  | S(   N(   R    R  (   R   R  R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   __add__  s    c         C   sg  i  } x] |  j  D]R } | j d k r+ q n  | j } | d k rI d } n  | j } | | | | f <q Wi  } x | j   D] } | j d  s | j d  ry | d d !} | d d !j   } | | f }	 |	 | k r4| |	 }
 |
 j } | d	 d
 !} | t k r1t | | k r.| j |  |
 | |	 <q.q1q4qy qy Wt	 |  t	 |  k rct
 d d   n  d S(   s$  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.

        pdb_handle -- A handle to the relevant PDB file.
        
        out_handle -- All output is written to this file like object.
        R   R   R   s   ATOM  t   HETATMi   i   i   i   i   s,   I could not find at least one ATOM or HETATMs8    record for each and every residue in this sequence map.N(   R   R   R   R   t
   xreadlinest
   startswithR   t   to_one_letter_codet   writeR   t   RuntimeError(   R   t
   pdb_handlet
   out_handlet   resSetR   R   R   t   resFoundR   R   R   t   atom_aat   resName(    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   getAtoms  s4    			
	N(   R   R   R   R   R   R   R   R   R   R  R  R  R  (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR      s   
	#					R   c           B   s   e  Z d  Z d   Z RS(   s    A single residue mapping from a RAF record.

    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(   NR   (   R   R   R   R   (   R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR   T  s    			(   R   R   R   R   (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR   I  s   
c         c   s    x |  D] } t  |  Vq Wd S(   s   Iterates over a RAF file, returning a SeqMap object for each line
    in the file.

    Arguments:
        
        handle -- file-like object.
    N(   R   (   t   handleR   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   parse[  s    t   Iteratorc           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   s   Iterates over a RAF file.
    c         C   s{   d d l  } | j d t  d d l m } m } t |  | k	 re t |  | k	 re t d   n  | |  _ | |  _	 d S(   s7  Create an object that iterates over a RAF file.

        handle -- file-like object.
        
        parser -- an optional Parser object to change the results into
                  another form.  If set to None, then the raw contents
                  of the file will be returned.
                  
        iNsM   Bio.SCOP.Raf.Iterator is deprecated. Please use Bio.SCOP.Raf.parse() instead.(   t   FileTypet   InstanceTypes,   I expected a file handle or file-like object(
   t   warningst   warnt   DeprecationWarningt   typesR  R  R   R  t   _handlet   _parser(   R   R  t   parserR  R  R  (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR   j  s    
$	c         C   sW   x1 |  j  j   } | s d S| d d k r Pq q W|  j d k	 rS |  j j |  S| S(   s   Retrieve the next record.i    t   #N(   R  R   R   R  R  (   R   R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   next}  s      c         C   s   t  |  j d   S(   N(   t   iterR   R   (   R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   __iter__  s    N(   R   R   R   R   R   R   R"  (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR  g  s   	
t   Parserc           B   s   e  Z d    Z d   Z RS(   c         C   s    d d  l  } | j d t  d  S(   Nis   Bio.SCOP.Raf.Parser is deprecated.
        Instead of

        parser = Raf.Parser()
        record = parser.parse(entry)

        please use

        record = Raf.SeqMap(entry)
        (   R  R  R  (   R   R  (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR     s    	c         C   s
   t  |  S(   s   Returns a SeqMap (   R   (   R   R   (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR    s    (   R   R   R   R  (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyR#    s   	N(    (    (    (    (   R   R    R  R   R	  R   R   R   R   R   R  R  R#  (    (    (    s   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/SCOP/Raf.pyt   <module>   sX   
################################
	
W	$