ó
{™xTc           @   se  d  d l  m Z d  d l m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l	 Z
 d  d l Z d „  Z d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d  e j d! e j e j Bƒ d" d# d$ d% „  ƒ Z d& „  Z d' „  Z  e d  e j d( e j e j Bƒ d) e ƒ Z! e d  e j d* e j e j Bƒ d) e ƒ Z" e d  e j d+ e j e j Bƒ d) e ƒ Z# e d  e j d, e j e j Bƒ d) e ƒ Z$ e d  e j d- e j e j Bƒ d) e ƒ Z% e d  e j d. e j e j Bƒ d) e ƒ Z& e j d/ e j e j Be j' Bƒ Z( d0 „  Z) i e" d1 6e" d2 6e" d3 6e# d4 6e! d5 6e! d6 6Z* d7 „  Z+ d8 „  Z, d9 „  Z- d: „  Z. e/ d; „ Z0 d^ e/ d e/ d< „ Z2 d= e f d> „  ƒ  YZ3 d? „  Z4 d@ e3 f dA „  ƒ  YZ5 e j dB e j e j Bƒ Z6 e j dC e j ƒ Z7 e j dD ƒ Z8 dE „  Z9 dF „  Z: e5 d  e j dG e j e j Bƒ d) e ƒ Z; e5 d  e j dH e j e j Bƒ d) e ƒ Z< e5 d  e j dI e j e j Bƒ d) e ƒ Z= i e; d4 6e9 dJ 6e9 dK 6e< d1 6e= dL 6e= d2 6e: dM 6Z> dN „  Z? dO „  Z@ dP „  ZA dQ „  ZB d dR „ ZC dS e f dT „  ƒ  YZD dU e f dV „  ƒ  YZE dW eE f dX „  ƒ  YZF dY eE f dZ „  ƒ  YZG d[ „  ZH d d\ „ ZI d] „  ZJ d S(_   iÿÿÿÿ(   t   division(   t   Abort(   t
   group_argsNc         C   s   t  j d | |  ƒ S(   Ns
   [^
]{1,%d}(   t   ret   findall(   t   textt   width(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   wrap   s    t   ebi_pdb_headerc           B   sP   e  Z d  Z e j d e j ƒ Z d Z d „  Z d „  Z	 d „  Z
 e e
 ƒ Z
 RS(   s   
  EBI-style parsed header
  s;   
    ^ > \s* PDB \s* :
    ( [^_]+ ) _
    ( \w* ) \s*
    s   >PDB:XXXX_Xc         C   s   | |  _  | |  _ d  S(   N(   t
   identifiert   chain(   t   selfR	   R
   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   __init__!   s    	c         C   s   d |  j  |  j f S(   Ns
   >PDB:%s_%s(   R	   R
   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   __str__'   s    c         C   sd   |  j  j | ƒ } | s3 t |  j t | ƒ f ‚ n  | j ƒ  \ } } |  d | j ƒ  d | j ƒ  ƒ S(   NR	   R
   (   t   regext   searcht
   ValueErrort   formatt   strt   groupst   upper(   t   clst   datat   matchR	   R
   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   parse,   s
    (   t   __name__t
   __module__t   __doc__R   t   compilet   VERBOSER   R   R   R   R   t   classmethod(    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR      s   			
t   generic_sequencec           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s   
  Sequence
  c         C   s   | |  _  | |  _ d  S(   N(   t   headert   body(   R   R    R!   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   >   s    	c         C   s-   d j  t |  j ƒ |  g t |  j | ƒ ƒ S(   Ns   
(   t   joinR   R    R   R!   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   D   s    c         C   s"   | j  d t |  j ƒ ƒ |  _ d  S(   NR   (   R   R   R    (   R   R    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   reinterpret_headerK   s    c         C   s   t  |  j ƒ S(   N(   t   lenR!   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   __len__P   s    c         C   s   |  j  d ƒ S(   NiF   (   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   U   s    (   R   R   R   R   R   R#   R%   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   9   s   				t   sequencec           B   sG   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s   
  Sequence
  t    c         C   s>   | |  _  d j g  | D] } | j ƒ  s | ^ q ƒ |  _ d  S(   NR'   (   t   nameR"   t   isspaceR&   (   R   R&   R(   t   char(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   `   s    	c         C   s+   d j  d |  j |  g t |  j | ƒ ƒ S(   Ns   
s   >%s(   R"   R(   R   R&   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   h   s    c         C   s   |  j  d ƒ S(   NiF   (   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   p   s    c         C   s   t  | t ƒ o |  j | j k S(   N(   t
   isinstanceR&   (   R   t   other(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   __eq__u   s    c         C   s   |  j  | ƒ S(   N(   R-   (   R   R,   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   __ne__z   s    c         C   s   t  |  j ƒ S(   N(   R$   R&   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR%      s    (	   R   R   R   R   R   R   R-   R.   R%   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR&   [   s   				t   fasta_sequencec           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   s   
  Fasta sequence
  R'   c         C   s&   t  t |  ƒ j | | ƒ | |  _ d  S(   N(   t   superR/   R   t   description(   R   R&   R(   R1   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   ‰   s    c         C   s4   d j  d |  j |  j f |  g t |  j | ƒ ƒ S(   Ns   
s   >%s %s(   R"   R(   R1   R   R&   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   �   s    (   R   R   R   R   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR/   „   s   t   pir_sequencec           B   s)   e  Z d  Z d d d d „ Z d „  Z RS(   s   
  Pir sequence
  R'   t   P1c         C   s/   t  t |  ƒ j | | ƒ | |  _ | |  _ d  S(   N(   R0   R2   R   t   typeR1   (   R   R&   R(   R4   R1   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   œ   s    	c         C   sc   d |  j  |  j f |  d |  j |  d d j g  t |  j | d ƒ D] } d | ^ qG ƒ d S(   Ns   >%s;%ss   
i   s     t   *(   R4   R(   R1   R"   R   R&   (   R   R   t   line(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   £   s    (   R   R   R   R   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR2   —   s   t   pdb_sequencec           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   s   
  PDB sequence
  R'   c         C   s&   t  t |  ƒ j | | ƒ | |  _ d  S(   N(   R0   R7   R   R
   (   R   R&   R(   R
   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   ´   s    c         C   sP   d |  j  |  j f |  d d j g  t |  j | d ƒ D] } d | ^ q8 ƒ S(   Ns
   >PDB:%s_%ss   
i   s     (   R(   R
   R"   R   R&   (   R   R   R6   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   º   s    !(   R   R   R   R   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR7   ¯   s   t   midlinec           B   s8   e  Z d  Z d d d d d „ Z d d „ Z d „  Z RS(	   s   
  Alignment midline
  R5   t   :t   .t    c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t	   identicalt	   conservedt   semit   differ(   R   R<   R=   R>   R?   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   Ê   s    			t   -c         C   st   g  } t  | ƒ d k rg xL t | Œ  D]; } | | k rP | j |  j | ƒ ƒ q% | j |  j ƒ q% Wn  d j | ƒ S(   Ni    R'   (   R$   t   zipt   appendt   conservation_codeR?   R"   (   R   t
   alignmentst   gapt   resultt   equi(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   compareÒ   s    c         C   s/   x% | D] } | d | k r |  j  Sq W|  j S(   Ni    (   R?   R<   (   R   t   residuest   r(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRC   Þ   s     (   R   R   R   R   RH   RC   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR8   Å   s   t	   alignmentc           B   s¡   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s   
  Alignment
  R@   c         C   sS   t  | ƒ t  | ƒ k r$ t d ‚ n  |  j d | ƒ | |  _ | |  _ | |  _ d  S(   Ns4   'alignments' and 'names' do not have the same lengthRD   (   R$   R   t   _set_alignmentst   namesRD   RE   (   R   RD   RM   RE   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   ë   s    			c         C   s@   d } x3 t  |  j Œ  D]" } |  j | k r | d 7} q q W| S(   Ni    i   (   RA   RD   RE   (   R   RF   RG   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   aligned_positions_countú   s
    c         C   s„   d } t  |  j ƒ d k r€ xb t |  j Œ  D]N } |  j | k rF q+ n  x0 | d D] } | | d k rQ PqQ qQ W| d 7} q+ Wn  | S(   Ni    i   (   R$   RD   RA   RE   (   R   RF   RG   t   p(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   identity_count  s      c         C   sV   |  j  s d St |  j  d ƒ } t g  |  j  D] } | | j |  j ƒ ^ q- ƒ } | S(   Ng        i    (   RD   R$   t   mint   countRE   (   R   t   alignment_lengtht   seqt   shortest_sequence_length(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   shortest_seqlen  s    	/c         C   sv   |  j  s d St |  j  d ƒ } t g  |  j  D] } | | j |  j ƒ ^ q- ƒ } | d k rb d St |  j ƒ  ƒ | S(   Ng      ð?i    (   RD   R$   RQ   RR   RE   t   floatRP   (   R   RS   RT   RU   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   identity_fraction  s    	/c         C   sE   g  |  j  D]7 } d j g  | D] } | |  j k r | ^ q ƒ ^ q
 S(   NR'   (   RD   R"   RE   (   R   RT   t   c(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   sequence_strings/  s    c         C   s   |  j  r t |  j  d ƒ Sd S(   Ni    (   RD   R$   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   length5  s    	c         C   s   t  |  j ƒ S(   N(   R$   RD   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   multiplicity<  s    c         C   sG   d j  g  t |  j |  j ƒ D]$ \ } } t | | ƒ j | ƒ ^ q ƒ S(   Ns   

(   R"   RA   RD   RM   R&   R   (   R   R   RT   R(   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   A  s    	c         C   s   |  j  d | ƒ S(   NR   (   R   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   simple_formatG  s    c      	   C   sÊ  t  | ƒ |  j ƒ  k r$ t d ‚ n  g  } g  } g  } g  } xî t |  j | ƒ D]Ú \ } } d j g  | D] } | |  j k rh | ^ qh ƒ }	 | j |	 ƒ }
 |
 d k r³ t d ‚ n  d |
 k sÅ t ‚ | j	 | |
  ƒ | j	 |  j |
 ƒ |
 t  |	 ƒ } | j	 | | ƒ | j	 |  j t  | ƒ | ƒ qO Wg  } x€ t
 |  j ƒ D]o \ } } | j	 t t j | |  | | g | | d ƒ | t t j | |  | | g | | d ƒ ƒ qCW|  j d | ƒ d  S(   Ns#   Inconsistent extension sequence setR'   iÿÿÿÿs!   Alignment does not match sequencei    i   RD   (   R$   R\   R   RA   RD   R"   RE   t   findt   AssertionErrorRB   t	   enumeratet   reducet   operatort   addRL   (   R   t	   sequencest   pre_alist   pre_gapst	   post_alist	   post_gapst   at   sRY   t   partialt   post   endt   alist   index(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   extendL  s0    1"1*c         K   sQ   i |  j  d 6|  j d 6|  j d 6} | j |  j ƒ  ƒ | j | ƒ |  j | �  S(   NRD   RM   RE   (   RD   RM   RE   t   updatet   extrat	   __class__(   R   t   kwdt   keywords(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   copyw  s    

c         C   s   i  S(   N(    (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRr   ƒ  s    c         C   sˆ   | d k  s |  j  ƒ  | k r* t d ‚ n  |  j | g |  j |  |  j | d |  _ |  j | g |  j |  |  j | d |  _ d  S(   Ni    s   Sequence not found in alignmenti   (   R\   t
   IndexErrorRM   RD   (   R   Ro   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   assign_as_targetˆ  s    -c         C   sJ   x: | d D]. } t  | d ƒ t  | ƒ k r t d ‚ q q W| |  _ d  S(   Ni   i    s(   'alignments' do not have the same length(   R$   R   RD   (   R   RD   t   o(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRL   “  s    c         C   s   |  j  d ƒ S(   NiF   (   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   �  s    (   R   R   R   R   RN   RP   RV   RX   RZ   R[   R\   R   R]   Rp   Rv   Rr   Rx   RL   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRK   æ   s"   										+				
t   fasta_alignmentc           B   s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s   
  Fasta alignment
  R@   c         C   sM   t  | ƒ t  | ƒ k r$ t d ‚ n  t t |  ƒ j | | | ƒ | |  _ d  S(   Ns7   Inconsistent 'alignments' and 'descriptions' attributes(   R$   R   R0   Rz   R   t   descriptions(   R   RD   RM   R{   RE   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   §  s
    	c         C   sS   d j  g  t |  j |  j |  j ƒ D]* \ } } } t | | | ƒ j | ƒ ^ q" ƒ S(   Ns   

(   R"   RA   RD   RM   R{   R/   R   (   R   R   R&   R(   R1   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   ³  s    c         C   s   |  j  d | ƒ S(   NR   (   R   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR]   ¼  s    c         C   s   i |  j  d 6S(   NR{   (   R{   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRr   Á  s    c         C   s   |  j  d ƒ S(   NiF   (   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   È  s    (   R   R   R   R   R   R]   Rr   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRz   ¢  s   				t   pir_alignmentc           B   s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s   
  Pir alignment
  R@   c         C   sn   t  | ƒ t  | ƒ k s0 t  | ƒ t  | ƒ k r< t d ‚ n  t t |  ƒ j | | | ƒ | |  _ | |  _ d  S(   Ns@   Inconsistent 'alignments', 'types' and 'descriptions' attributes(   R$   R   R0   R|   R   t   typesR{   (   R   RD   RM   R}   R{   RE   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   Ò  s    		c         C   s_   d j  g  t |  j |  j |  j |  j ƒ D]0 \ } } } } t | | | | ƒ j | ƒ ^ q( ƒ S(   Ns   

(   R"   RA   RD   RM   R}   R{   R2   R   (   R   R   R&   R(   R4   R1   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   à  s    c         C   s   |  j  d | ƒ S(   NR   (   R   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR]   é  s    c         C   s   i |  j  d 6|  j d 6S(   NR{   R}   (   R{   R}   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRr   î  s    
c         C   s   |  j  d ƒ S(   NiF   (   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   ö  s    (   R   R   R   R   R   R]   Rr   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR|   Í  s   				t   clustal_alignmentc           B   sY   e  Z d  Z d d d „ Z d „  Z d
 d
 d „ Z d „  Z d „  Z d „  Z	 d	 „  Z
 RS(   s   
  Clustal alignment
  s   CLUSTAL 2.0.9R@   c         C   s)   t  t |  ƒ j | | | ƒ | |  _ d  S(   N(   R0   R~   R   t   program(   R   RD   RM   R   RE   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR      s    c         C   s`   d } g  } xM t  | | ƒ D]< } | t | ƒ | j |  j ƒ 7} | j | | | f ƒ q W| S(   Ni    (   R   R$   RR   RE   RB   (   R   t   captionRK   t	   aln_widtht   running_totalt   aln_infoR6   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   make_aln_info  s     c            sj  | s* t  ƒ  j d |  j d |  j ƒ } n$ t | ƒ |  j ƒ  k rN t d ‚ n  ˆ d  k ri |  j ƒ  ‰ n  g  t	 |  j
 |  j ƒ D]7 \ } } |  j d | j | ƒ |  d | d | ƒ ^ q } | j g  t | | ƒ D] } d | | d f ^ qÒ ƒ ‡  ‡ f d	 †  }	 d
 |  j d j g  t	 | Œ  D]A }
 d j g  |
 D]% \ } } ‰  d | | |	 ƒ  f ^ q4ƒ ^ q!ƒ S(   NRD   RE   s   Incorrect midline lengthR€   RK   R�   R;   R'   c              s!   ˆ  r d t  ˆ  ƒ j ˆ ƒ Sd S(   NR;   R'   (   R   t   rjust(    (   t   numt   number_width(    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   fmt_num/  s     s    %s multiple sequence alignment

s   

s   
s   %s %s%s(   R8   RH   RD   RE   R$   R[   R   t   Nonet   length_digitsRA   RM   R„   t   ljustRB   R   R   R"   (   R   R�   t   caption_widthR‡   t   middle_lineR(   RK   t	   aln_infosR6   Rˆ   t   zipped_infost   capt   ali(    (   R†   R‡   sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR     s$    	P-
c         C   s)   d d  l  } t | j |  j ƒ  ƒ ƒ d S(   Niÿÿÿÿi   (   t   matht   intt   log10R[   (   R   R’   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRŠ   @  s    c         C   sd   t  d | ƒ } t |  j ƒ  | | ƒ } t | | d | d d ƒ } |  j d | d | d | ƒ S(   Ngš™™™™™é?i   R�   RŒ   R‡   (   R“   RQ   RŠ   t   maxR   (   R   R   R�   t	   num_widthRŒ   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR]   F  s    	c         C   s   i |  j  d 6S(   NR   (   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRr   R  s    c         C   s   |  j  d d ƒ S(   Ni<   i   (   R   (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   Y  s    N(   R   R   R   R   R„   R‰   R   RŠ   R]   Rr   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR~   û  s   	/			t   generic_sequence_parserc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s%   
  General-purpose sequence parser
  c         C   s   | |  _  | |  _ d  S(   N(   R   R4   (   R   R   R4   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   d  s    	c         K   sü   g  } g  } xÍ t  rÛ |  j j | ƒ } | r× | j | j d ƒ ƒ } | d k sW t ‚ | |  | | t | j d ƒ ƒ } } | r¢ | j ƒ  r¢ | j | ƒ n  | j |  j	 t
 | j ƒ  | j ƒ  j ƒ  ƒ �  ƒ q Pq W| rò | j | ƒ n  | | f S(   Ni    (   t   TrueR   R   R^   t   groupR_   R$   R)   RB   R4   t   dictt   itemst	   groupdict(   R   R   t   kwargst   objectst   non_compliantR   t   it   unknown(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   j  s     	(/c         K   s   |  j  | | � S(   N(   R   (   R   R   R�   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   __call__‡  s    (   R   R   R   R   R   R¢   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR—   _  s   		t   SequenceFormatc           B   s   e  Z d  Z d „  Z RS(   s(   
  Bundle of independent parsing data
  c         C   s   | |  _  | |  _ | |  _ d  S(   N(   R   t   expectedt   create(   R   R   R¤   R¥   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   ‘  s    		(   R   R   R   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR£   Œ  s   R   s:   
    ^
    ( > [^\n]* ) \n
    ( [^>^*]* )
    \*?\s*
    R¤   s   >Header
SEQUENCER¥   c         C   s   t  d |  d d | ƒ S(   NR    i    R!   (   R   (   t   headersR!   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   <lambda>£  s   
c         c   s–   d } xP | j  |  ƒ D]? } | j ƒ  } | j ƒ  | | |  | | !f f V| j ƒ  } q W|  | j ƒ  } | rŒ d | t |  ƒ | f f Vn  t ‚ d S(   s   
  Partition sequence files
  i    N(    (   t   finditert   startR   Rm   t   stripR$   t   StopIteration(   R   R   t   positionR   t   currentt	   remaining(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   partition_stringª  s    !c   	      c   s´   t  d |  d | j ƒ } x• | D]� \ } \ } } } | j ƒ  r_ t d | | | j | f ‚ n  d j g  | d D] } | j ƒ  sp | ^ qp ƒ } | j d | d  d | ƒ Vq Wd S(	   s,   
  Generic purpose sequence header parser
  R   R   s;   Uninterpretable block from %s to %s:
Expected: %s
Found: %sR'   iÿÿÿÿR¦   R!   N(   R¯   R   Rª   R   R¤   R"   R)   R¥   (	   R   R   t	   partitionR   t   n_startt   n_endt   n_dataRY   R!   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   parse_sequence_str¾  s    2sR   
    ^ > \s*
    (?P<name> [^\n]* ) \n
    (?P<sequence> [^>^*]* )
    \*?\s*
    R4   sz   
    ^ > \s*
    (?P<name> [^\s^:]+ ) [ :]? [ \t]*
    (?P<description> [^\n]* ) \n
    (?P<sequence> [^>]* )
    \s*
    s�   
    ^ >
    (?P<type> [PFDRN][13LC] ) ;
    (?P<name> \S* ) \n
    (?P<description> [^\n]* ) \n
    (?P<sequence> [^>^\*]* )
    \* \s*
    sq   
    ^ >
    (?P<name> [^\n]* ) \n
    (?P<description> [^\n]* ) \n
    (?P<sequence> [^>^\*]* )
    \*? \s*
    s8   
    (?P<sequence> [^>^*]*? )
    \*?\s*\n(?:\n|\Z)
    sq   
    ^ > \s* PDB \s* :
    (?P<name> [^_]+ ) _
    (?P<chain> \w* ) \s* \n
    (?P<sequence> [^>]* )
    \s*
    s,   
  \A \s*
  > ( [^\n]* ) \n
  ( .* )
  \Z
  c         C   sP   t  j |  ƒ } | s" g  |  g f S| j ƒ  \ } } t j d | d | j ƒ  ƒ S(   s"   
  Tom's format sequence parser
  R   R(   (   t   _tf_split_regexR   R   t   lineseparated_sequence_parseR   Rª   (   R   R   R(   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   tf_sequence_parse0  s    	s   .fastas   .fas   .faas   .pirs   .seqs   .datc         C   s%   t  j j |  ƒ \ } } t j | ƒ S(   N(   t   ost   patht   splitextt   _implemented_sequence_parserst   get(   t	   file_nameR(   t	   extension(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   sequence_parser_forK  s    c         C   s   t  j |  ƒ S(   N(   R»   R¼   (   R¾   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   sequence_parser_for_extensionR  s    c           C   s
   t  j ƒ  S(   N(   R»   t   keys(    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   known_sequence_formatsW  s    c         C   s}   t  t t t t t t g } g  } xF | D]> } | d |  ƒ \ } } | sS | | f S| j | | f ƒ q( Wt | d d „  ƒS(   NR   t   keyc         S   s   t  |  d ƒ S(   Ni   (   R$   (   RO   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR§   s  s    (	   t   fasta_sequence_parset   dbfetch_sequence_parset   pir_sequence_parset   tolerant_pir_sequence_parset   seq_sequence_parseR¶   R·   RB   RQ   (   R   t   parserst   resultsRO   t   seqst   junk(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   parse_sequence\  s    	
c         C   s¦  t  |  ƒ } t |  d ƒ j ƒ  } d  } | d  k	 ryJ | j | ƒ \ } } t | ƒ d k sc t ‚ | d j d k s| t ‚ Wn t k
 r’ } qX| rt	 j
 j t	 j
 j |  ƒ ƒ d } xN t | ƒ D]= \ }	 }
 |
 j d k rÈ d | |	 d f |
 _ |
 j GHqÈ qÈ Wn  | | f Sn  xt j t j t j t g D]õ } | | k	 r5yG | | ƒ \ } } t | ƒ d k stt ‚ | d j d k s�t ‚ Wn t k
 r£} q*X| rt	 j
 j t	 j
 j |  ƒ ƒ d } xN t | ƒ D]= \ }	 }
 |
 j d k rÙd | |	 d f |
 _ |
 j GHqÙqÙWn  | | f Sq5q5Wt j d d | ƒ } t j d | ƒ d  k r¢t | ƒ }
 | r•t	 j
 j t	 j
 j |  ƒ ƒ d |
 _ n  |
 g g  f Sd S(	   NRJ   i    R'   s   %s_%di   s   \ss   [^a-zA-Z\*](   NN(   R¿   t   opent   readR‰   R   R$   R_   R&   t	   ExceptionR¸   R¹   Rº   t   basenameR`   R(   RÄ   RÆ   RÈ   R·   R   t   subR   (   R½   t   assign_name_if_not_definedt   format_parserR   t
   seq_objectRž   RŸ   t   et	   base_namet   kRT   t   other_parser(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   any_sequence_formatv  sP    ""(c         C   sÚ  t  |  ƒ d k s* t  | ƒ d k s* t ‚ t  |  ƒ d k r] t  | ƒ d k r] | r] |  d St j ƒ  } xæ |  D]Þ } t | ƒ \ } }	 | d  k rô d d d d | }
 t | d ƒ rå |
 d d	 7}
 | |
 ƒ sp t ƒ  ‚ qp qp qô t |
 ƒ ‚ n  xW | D]O } t	 | j
 ƒ } | d
 k r%d } n  | j d | ƒ | j d | j ƒ qû Wqp Wt  | ƒ d k rªxC t | ƒ D]2 \ } } | j d | d ƒ | j d | ƒ qqWn  t | d ƒ } | j | j ƒ  ƒ | j ƒ  | S(   Ni    i   s0   The file '%s' could not be parsed as one of the sA   standard formats.  This could either be a problem with the file, s@   a non-standard format, or a bug in our code.  For further help, s6   please email the developers at help@phenix-online.org.R¢   s8     (This is not a fatal error, but the combined sequence s   file will be incomplete.)R'   t   nones   > %s
s   %s
s   > seq%d
t   w(   R$   R_   t	   cStringIOt   StringIORÚ   R‰   t   hasattrR   t   RuntimeErrorR   R(   t   writeR&   R`   RÎ   t   getvaluet   close(   t
   file_namest   output_fileRd   t   include_non_compliantt   call_back_on_errort   force_new_filet   seq_outt   seq_fileRž   RŸ   t   msgt
   seq_recordR(   RØ   RT   t   f(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   merge_sequence_files£  s<    *+	
t   generic_alignment_parserc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s&   
  General purpose alignment parser
  c         C   s
   d  | f S(   N(   R‰   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   failÎ  s    c         C   s)   g  |  j  j | ƒ D] } | j ƒ  ^ q S(   N(   R   R¨   Rœ   (   R   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   extractÓ  s    c         C   s–   d | k r d | k s t  ‚ g  | d D]4 } d j g  | D] } | j ƒ  s< | ^ q< ƒ ^ q) } t | ƒ sy d  | f S| | d <|  j | �  d f S(   NRD   RM   R'   (   R_   R"   R)   t   check_alignments_are_validR‰   R4   (   R   t	   data_dictR   t   ali_strR*   RD   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   assess_parsing_resultsØ  s    B

c         C   s   |  j  | ƒ S(   N(   R   (   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR¢   ë  s    (   R   R   R   Rð   Rñ   Rõ   R¢   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRï   É  s
   			c         C   sI   |  s
 t  St |  d ƒ } x( |  d D] } | t | ƒ k r% t Sq% Wt  S(   Ni    i   (   R˜   R$   t   False(   RD   t   firstR6   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRò   ð  s    t   sequential_alignment_parserc           B   s    e  Z d  Z d „  Z d „  Z RS(   s/   
  Specific for sequential format alignments
  c         C   s   | |  _  | |  _ d  S(   N(   R   R4   (   R   R   R4   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR     s    	c         C   s‡   |  j  | ƒ } | r) | r) |  j | ƒ St g  |  j j j ƒ  D]) } | g  | D] } | | ^ qO f ^ q? ƒ } |  j d | d | ƒ S(   NRó   R   (   Rñ   Rð   Rš   R   t
   groupindexRÁ   Rõ   (   R   R   R   R(   t   infot   preprocessed_data(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   	  s    B	(   R   R   R   R   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRø   þ  s   	sZ   
  ^
  (?P<name> [\S]* ) \s+
  (?P<alignment> [A-Z\-]* )
  (?P<number> \s+ \d+ )? \s* $
  s   ^ \s* [:.* ]+ \s* $s#   \A(.*) multiple sequence alignment$c   
   	   C   s­  |  j  ƒ  } | s d	 |  f Sd t | ƒ k  s4 t ‚ t j | d ƒ } | sW d	 |  f S| j d ƒ } i  } g  } xÚ | d D]Î } | j ƒ  s• q} n  t j | ƒ } | sÉ t	 j | ƒ r¿ q} n  d	 |  f S| j
 ƒ  } | d } | | k r| | k sý t ‚ g  | | <| j | ƒ n  | | j g  | d D] }	 |	 j ƒ  s,|	 ^ q,ƒ q} Wt d | j ƒ  ƒ snd	 |  f St d | d g  | D] } d j | | ƒ ^ q�d | ƒ d f S(
   s%   
  Specific for Clustal alignments
  i    i   R(   RK   RD   RM   R'   R   N(   t
   splitlinesR‰   R$   R_   t   CLUSTAL_HEADERR   R™   Rª   t   CLUSTAL_BODYt   CLUSTAL_MIDLINERœ   RB   Rp   R)   Rò   t   valuesR~   R"   (
   R   t   linesR   R   t   data_forRM   R6   Rú   R(   RY   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   clustal_alignment_parse%  s@    




8
&c         C   s>   y t  d |  ƒ } Wn t k
 r- d  |  f SX| j ƒ  d f S(   Nt   outputR'   (   t   hhalign_parserR   R‰   RK   (   R   t   hhalign(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   hhalign_alignment_parse^  s
    s�   
    ^ >
    (?P<types> [PFDRN][13LC] ) ;
    (?P<names> \S+ ) \n
    (?P<descriptions> [^\n]* ) \n
    (?P<alignments> [^>]* )
    \* \s*
    sl   
    ^ >
    (?P<names> \S+ ) \s+
    (?P<descriptions> [^\n]* ) \n
    (?P<alignments> [^>]* )
    \s*
    sU   
    ^ > \s*
    (?P<names> [^\n]* ) \n
    (?P<alignments> [^>^*]* )
    \*?\s*
    s   .alns   .clustals   .alis   .hhrc         C   s%   t  j j |  ƒ \ } } t j | ƒ S(   N(   R¸   R¹   Rº   t   _implemented_alignment_parsersR¼   (   R½   R(   R¾   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   alignment_parser_forœ  s    c         C   s   t  j |  ƒ S(   N(   R  R¼   (   R¾   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   alignment_parser_for_extension£  s    c           C   s
   t  j ƒ  S(   N(   R  RÁ   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   known_alignment_formats¨  s    c         C   sQ  t  j j |  ƒ \ } } t |  ƒ j ƒ  } d  } | d k r¶ | t k r¶ t j | ƒ } y/ | | ƒ d } t | j	 ƒ d k s… t
 ‚ Wn& t k
 rœ ‚  q¶ t k
 r® } q¶ X| Sn  xˆ t t t t g D]t } | | k rá qÉ n  y/ | | ƒ d } t | j	 ƒ d k st
 ‚ Wn& t k
 r&‚  qÉ t k
 r8} qÉ X| SqÉ Wt d ƒ ‚ d  S(   Ns   .hhri    s.   Not a recognizeable sequence alignment format!(   R¸   R¹   Rº   RÎ   RÏ   R‰   R  R¼   R$   RM   R_   t   KeyboardInterruptRÐ   t   pir_alignment_parseR  t   fasta_alignment_parset   ali_alignment_parseRà   (   R½   t   baset   extR   t   parser1t   alnRÖ   t   parser(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   any_alignment_file¬  s4      	  c         C   sà   d  } | d k r t d |  ƒ S| d  k	 rr t d | ƒ } | d |  ƒ d } | } | rr | j ƒ  d k rr | Sn  x[ t t t t g D]G } | | k r� q… n  | d |  ƒ d } | r… | j ƒ  d k r… | Sq… Wt d ƒ ‚ d  S(   Ns   .hhrR  R¾   R   i    s.   Not a recognizeable sequence alignment format!(	   R‰   R  R
  R\   R  R  R  R  Rà   (   R   R¾   t   triedR  R  (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   any_alignment_stringÇ  s"    	t   homology_search_hitc           B   s   e  Z d  Z d „  Z RS(   s"   
  A hit from a homology search
  c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R	   R
   t
   annotationRK   (   R   R	   R
   R  RK   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR   è  s    			(   R   R   R   R   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR  ã  s   t   hhpred_parserc           B   sl   e  Z d  Z e j d e j e j Bƒ Z e j d e j ƒ Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z RS(   s#   
  Parses .hhr files from HHPred
  sw   
    ( Query .*? ) ( \n | \r\n | \r ) \2
    ( \s+ No \s+ Hit .*? ) \2 \2
    ( .*? )
    (?= (?:Done!) | (?:\Z) )
    sf  
    \A
    Query \s+ ( \S .* ) (?: \n | \r\n | \r )
    Match_columns \s+ ( \d+ ) (?: \n | \r\n | \r )
    No_of_seqs \s+ ( \d + ) \s out \s of \s ( \d+ ) (?: \n | \r\n | \r )
    Neff \s+ (\S[^\n^\r]*) (?: \n | \r\n | \r )
    Searched_HMMs \s+ ( \d+ ) (?: \n | \r\n | \r )
    Date \s+ (\S[^\n^\r]*) (?: \n | \r\n | \r )
    Command \s+ (\S.*)
    \Z
    c         C   s`   |  j  j | ƒ } | s$ t d ‚ n  | j ƒ  \ } } } } |  j d | ƒ |  j d | ƒ d  S(   Ns   Incorrect file formatR    t   hits(   t   SPLITR   R   R   t   process_headert   process_hits(   R   R  t   resR    t   linefeedt   summaryR  (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR     s    c         C   sÍ   |  j  j | ƒ } | s$ t d ‚ n  | j d ƒ |  _ t | j d ƒ ƒ |  _ t | j d ƒ ƒ t | j d ƒ ƒ f |  _ | j d ƒ |  _ t | j d ƒ ƒ |  _	 | j d ƒ |  _
 | j d	 ƒ |  _ d  S(
   Ns   Incorrect header formati   i   i   i   i   i   i   i   (   t   HEADERR   R   R™   t   queryR“   t   match_columnst   no_of_sequencest   nefft   searched_hmmst   datet   command(   R   R    R  (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR    s    -c         C   sõ   |  j  ƒ  d } x² |  j j | ƒ D]ž } | j ƒ  | k r‹ | | j ƒ  k  sS t ‚ | | | j ƒ  !j ƒ  } | r‹ t d t | ƒ ‚ q‹ n  | j ƒ  } |  j	 d | ƒ |  j
 d | j ƒ  d ƒ q# W| | j ƒ  } | rñ t d t | ƒ ‚ n  d  S(   Ni    s   Uninterpretable block: %sR   t   blockss   Uninterpretable tail: %s(   t   setup_data_arrayst   HITSR¨   R©   R_   Rª   R   t   reprRm   t   add_match_to_hit_header_resultst   process_blocksRœ   (   R   R  R©   t   mR¡   R®   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR  +  s    
c         C   s7  g  } d } x› |  j  j | ƒ D]‡ } | j ƒ  | k r‡ | | j ƒ  k  sO t ‚ | | | j ƒ  !j ƒ  } | r‡ t d t | ƒ ‚ q‡ n  | j ƒ  } | j | j	 ƒ  ƒ q W| | j ƒ  } | rÖ t d t | ƒ ‚ n  | sè t d ‚ n  t
 g  | d D]" } t | d ƒ t | ƒ k ^ qö ƒ s't ‚ |  j d | ƒ S(   Ni    s   Uninterpretable: %ss   Empty homology blocki   t   matches(   t   BLOCKSR¨   R©   R_   Rª   R   R-  Rm   RB   R   t   allR$   t   merge_and_process_block_hits(   R   R*  R1  R©   R   R¡   R®   Ri   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR/  D  s"    ?c         C   s–   xz t  | d | d  ƒ D]a \ } } | | d k r@ t d ‚ n  t g  | d D] } | d | k ^ qN ƒ s t d ‚ q q W| d | d | d f S(   Ni   iÿÿÿÿs   Incorrect sequence indicesi    (   RA   R   R3  (   R   t   startst   endst   othersRj   RÖ   Ry   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   merge_sequence_numbersb  s    $-(   R   R   R   R   R   R   t   DOTALLR  R"  R   R  R  R/  R8  (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR  ð  s   				t   hhsearch_parserc           B   ss   e  Z d  Z e j d e j e j Be j Bƒ Z e j d e j ƒ Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z RS(   s"   
  Specific for hhsearch output
  s¨  
    No \s ( \d+) \s* (?: \n | \r\n | \r )
    >( [\w]{4} ) _  ( [\w]? ) \s ( [^\n]* )(?: \n | \r\n | \r )
    Probab = ( [+-]? \d+ \. \d* ) \s+
    E-value = ( \d+ \.? \d* )( e[+-]? \d+ )? \s+
    Score = ( \d+\.\d+ ) \s+
    Aligned_cols = ( \d+ ) \s+
    Identities = ( \d+ ) % \s+
    Similarity = ( -? \d+ \. \d+ ) \s+
    Sum_probs = ( \d+ \. \d+ ) (?: \n | \r\n | \r )
    (?P<blocks> .*? )(?= (?:^No) | (?:\Z) )
    sÀ  
    Q \s+ ss_pred \s+ ( [\w-]+ ) \s* (?: \n | \r\n | \r )
    Q \s+ [\w:\.|-]* \s+ ( \d+ ) \s+ ( [\w-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    Q \s+ Consensus \s+ ( \d+ ) \s+ ( [\w~-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    \s* ( [ \.\-+|=]* ) (?: \n | \r\n | \r )
    T \s+ Consensus \s+ ( \d+ ) \s+ ( [\w~-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    T \s+ \w+ \s+ ( \d+ ) \s+ ( [\w-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    (?: T \s+ ss_dssp \s+ ( [\w-]+ ) \s* (?: \n | \r\n | \r ) )?
    (?: T \s+ ss_pred \s+ ( [\w-]+ ) \s* (?: \n | \r\n | \r ) )?
    (?: Confidence \s+ ( [\w ]+ ) \s* (?: \n | \r\n | \r ))?
    c         C   så   g  |  _  g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _	 g  |  _
 g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ d  S(   N(   t   indicest   pdbst   chainst   annotationst   probabst   e_valuest   scorest   aligned_colst
   identitiest   similaritiest	   sum_probst   query_startst
   query_endst   query_otherst   query_alignmentst   query_consensit   query_ss_predst   midlinest
   hit_startst   hit_endst
   hit_otherst   hit_alignmentst   hit_consensit   hit_ss_predst   hit_ss_dssps(   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR+  ‘  s2    																								c         C   s”  |  j  |  |  _  |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j	 |  |  _	 |  j
 |  |  _
 |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ |  j |  |  _ d  S(   N(   R;  R<  R=  R>  R?  R@  RA  RB  RC  RD  RE  RF  RG  RH  RI  RJ  RK  RL  RM  RN  RO  RP  RQ  RR  RS  (   R   t	   max_count(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   restrict±  s2    c         C   sx  |  j  j t | j d ƒ ƒ ƒ |  j j | j d ƒ j ƒ  ƒ |  j j | j d ƒ ƒ |  j j | j d ƒ ƒ |  j j t	 | j d ƒ ƒ ƒ | j d ƒ } | j d ƒ rÃ | | j d ƒ 7} n  |  j
 j t	 | ƒ ƒ |  j j t	 | j d ƒ ƒ ƒ |  j j t | j d	 ƒ ƒ ƒ |  j j t	 | j d
 ƒ ƒ ƒ |  j j t	 | j d ƒ ƒ ƒ |  j j t	 | j d ƒ ƒ ƒ d  S(   Ni   i   i   i   i   i   i   i   i	   i
   i   i   (   R;  RB   R“   R™   R<  R   R=  R>  R?  RW   R@  RA  RB  RC  RD  RE  (   R   R   t   number(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR.  Ñ  s     c      
   C   sù  t  | Œ  } t | ƒ d k s$ t ‚ | d | d | d | d | d | d | d g } | d	 d t | d	 ƒ k r­ t g  | d D] } d
 t | ƒ ^ q‡ ƒ | d	 <n  | d d t | d ƒ k rÿ t g  | d D] } d
 t | ƒ ^ qÙ ƒ | d <n  g  } xš t t  | Œ  ƒ D]† \ } } t | d ƒ } t g  | d D] }	 | t |	 ƒ k ^ qBƒ sot d ‚ n  | j d
 | t | d | ƒ | d | ƒ qWg  | D] }
 t	 t
 j |
 ƒ ^ q©} t | ƒ t | ƒ k såt ‚ t	 t
 j | ƒ } |  j d g  | d D] } t | ƒ ^ qd g  | d D] } t | ƒ ^ q.d g  | d D] } t | ƒ ^ qQƒ } |  j d g  | d D] } t | ƒ ^ q€d g  | d D] } t | ƒ ^ q£d g  | d D] } t | ƒ ^ qÆƒ } |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j | ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d	 ƒ |  j j | d ƒ d  S(   Ni   i    i   i   i   i   i   i   i   R;   i   s   Incorrect alignmentsi	   R5  R6  i   R7  i   i
   i   i   (   N(   N(   RA   R$   R_   R‰   t   tupleR`   R3  R   RB   Ra   Rb   Rc   R8  R“   RF  RG  RH  RK  RI  RJ  RL  RM  RN  RO  RQ  RP  RS  RR  (   R   R1  R   Rd   t   dRL  Ro   Rn   RR   RY   Ri   t   mergedR8   t	   q_indicest	   t_indices(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR4  â  sN    744/-%	##&	##&c      
   c   sœ   t  |  j |  j |  j |  j |  j ƒ } xn | D]f \ } } } } } t d d d | | f g d | | g d d ƒ } t d | d | d	 | d
 | ƒ Vq. Wd  S(   NRM   t   targets   %s_%sRD   R   t   HHPredR	   R
   R  RK   (   RA   R<  R=  R>  RI  RP  R~   R  (   R   R   t   pdbR
   R  R#  t   hitRK   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR  %  s     	(   R   R   R   R   R   R   R9  t	   MULTILINER,  R2  R+  RU  R.  R4  R  (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR:  n  s   	 	 		CR  c           B   sj   e  Z d  Z e j d e j e j Be j Bƒ Z e j d e j ƒ Z	 d „  Z
 d „  Z d „  Z d „  Z RS(   s!   
  Specific for hhalign output
  sK  
    No \s ( \d+) \s* (?: \n | \r\n | \r )
    > \s* ( [^\n]* ) (?: \n | \r\n | \r )
    Probab = ( [+-]? \d+ \. \d* ) \s+
    E-value = ( \d+ \.? \d* )( e[+-]? \d+ )? \s+
    Score = ( \d+\.\d+ ) \s+
    Aligned_columns = ( \d+ ) \s+
    Identities = ( \d+ ) % (?: \n | \r\n | \r )
    (?P<blocks> .*? )(?= (?:^No) | (?:\Z) )
    s±  
    Q \s+ ss_pred \s+ ( [\w-]+ ) \s* (?: \n | \r\n | \r )
    Q \s+ ss_conf \s+ ( [\w-]+ ) \s* (?: \n | \r\n | \r )
    Q \s+ [\w:\.]+ \s+ ( \d+ ) \s+ ( [\w-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    Q \s+ Consensus \s+ ( \d+ ) \s+ ( [\w~-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    \s+ ( [ \.\-+|=]+ ) (?: \n | \r\n | \r )
    T \s+ Consensus \s+ ( \d+ ) \s+ ( [\w~-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    T \s+ [\w\.]+ \s+ ( \d+ ) \s+ ( [\w-]+ ) \s+ ( \d+ ) \s+ \( ( \d+ ) \) \s* (?: \n | \r\n | \r )
    T \s+ ss_pred \s+ ( [\w-]+ ) \s* (?: \n | \r\n | \r )
    T \s+ ss_conf \s+ ( [\w-]+ ) \s* (?: \n | \r\n | \r )
    c         C   sÊ   g  |  _  g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _	 g  |  _
 g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ d  S(   N(   R;  R>  R?  R@  RA  RB  RC  RF  RG  RH  RI  RJ  RK  t   query_ss_confsRL  RM  RN  RO  RP  RQ  RR  t   hit_ss_confs(   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR+  _  s,    																					c         C   s  |  j  j t | j d ƒ ƒ ƒ |  j j | j d ƒ ƒ |  j j t | j d ƒ ƒ ƒ | j d ƒ } | j d ƒ r‹ | | j d ƒ 7} n  |  j j t | ƒ ƒ |  j j t | j d ƒ ƒ ƒ |  j	 j t | j d ƒ ƒ ƒ |  j
 j t | j d ƒ ƒ ƒ d  S(	   Ni   i   i   i   i   i   i   i   (   R;  RB   R“   R™   R>  R?  RW   R@  RA  RB  RC  (   R   R   RV  (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR.  |  s    c      
   C   sp  t  | Œ  } t | ƒ d k s$ t ‚ | d | d | d | d | d | d | d | d	 g } g  } xš t t  | Œ  ƒ D]† \ } } t | d ƒ } t g  | d D] } | t | ƒ k ^ q¥ ƒ sÒ t d
 ‚ n  | j d | t | d | ƒ | d | ƒ q{ Wg  | D] }	 t t j	 |	 ƒ ^ q}
 t | ƒ t | ƒ k sHt ‚ t t j	 | ƒ } |  j
 d g  | d D] } t | ƒ ^ qnd g  | d D] } t | ƒ ^ q‘d g  | d D] } t | ƒ ^ q´ƒ } |  j
 d g  | d D] } t | ƒ ^ qãd g  | d D] } t | ƒ ^ qd g  | d D] } t | ƒ ^ q)ƒ } |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j |
 d ƒ |  j j |
 d ƒ |  j j |
 d ƒ |  j j |
 d ƒ |  j j | ƒ |  j j | d ƒ |  j j | d ƒ |  j j | d ƒ |  j j |
 d ƒ |  j j |
 d ƒ |  j j |
 d ƒ |  j j |
 d ƒ d  S(   Ni   i    i   i   i   i   i   i   i   s   Incorrect alignmentsR;   i
   R5  i   R6  i   R7  i   i   i   i   i   (   RA   R$   R_   R`   R3  R   RB   Ra   Rb   Rc   R8  R“   RF  RG  RH  RK  Ra  RI  RJ  RL  RM  RN  RO  RQ  RP  RR  Rb  (   R   R1  R   Rd   RL  Ro   Rn   RR   RY   Ri   RY  R8   RX  RZ  R[  (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR4  Š  sJ    "/-%	##&	##&c         C   sU   t  |  j ƒ d k s t ‚ t d d |  j d g d |  j d |  j d g d d ƒ S(   Ni   RM   R\  i    RD   R   R]  (   R$   R>  R_   R~   RI  RP  (   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRK   É  s
    (   R   R   R   R   R   R   R9  R`  R,  R2  R+  R.  R4  RK   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyR  >  s   
			?c         C   sv   t  |  ƒ j ƒ  } xQ t t g D]C } y | | ƒ } Wn& t k
 rK ‚  q t k
 r] } q X| Sq Wt d ƒ ‚ d  S(   Ns$   Not an HHpred/HHalign/HHsearch file!(   RÎ   RÏ   R  R:  R  RÐ   Rà   (   R½   R   R  RO   RÖ   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   any_hh_fileÒ  s     c         C   s  | d  k r t j } n  y5 t |  ƒ \ } } | d  k rL t d |  ƒ ‚ n  Wn$ t k
 rs } | t | ƒ IJd  SXt | ƒ d k r¸ | d IJx% | D] } | d t | ƒ IJq— Wn  d } } x7 | D]/ } t | j	 ƒ \ }	 }
 | |	 7} | |
 7} qÉ Wt
 d | d | ƒ Sd  S(   Ns   Could not parse %si    s/   Warning: non-compliant entries in sequence files     t
   n_residuest   n_bases(   R‰   t   syst   stdoutRÚ   R   RÐ   R   R$   t   composition_from_sequenceR&   R   (   R½   t   logRê   RŸ   RÖ   t   ncRd  Re  t	   seq_entryt	   n_res_seqt
   n_base_seq(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   composition_from_sequence_fileÞ  s&    


c         C   sµ   |  j  ƒ  } d } } t | ƒ | j d ƒ } | j d ƒ | j d ƒ | j d ƒ | j d ƒ | j d ƒ } | t | d ƒ k r› | t | ƒ 7} n | t | ƒ 7} | | f S(	   Ni    t   Xt   At   Ut   Tt   Gt   CgÍÌÌÌÌÌì?(   R   R$   RR   R“   (   R&   RT   Rd  Re  t   n_validt   n_na(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyRh  ô  s    
C(    (K   t
   __future__R    t   libtbx.utilsR   t   libtbxR   RÝ   R   Rb   t   os.pathR¸   Rf  R   t   objectR   R   R&   R/   R2   R7   R8   RK   Rz   R|   R~   R—   R£   R   R`  R   t   st_separated_fastaR¯   R´   RÈ   RÄ   RÆ   RÇ   R¶   RÅ   R9  Rµ   R·   R»   R¿   RÀ   RÂ   RÍ   Rö   RÚ   R‰   Rî   Rï   Rò   Rø   Rþ   Rÿ   Rý   R  R  R  R  R  R  R	  R
  R  R  R  R  R  R:  R  Rc  Rn  Rh  (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/iotbx/bioinformatics/__init__.pyt   <module>   sö   	(")!¼+.d-									
				-$'		9				
				~Ð”	