
:>"^ä/  ã               @   sg   d  Z  d d l m Z Gd d „  d e j e ƒ Z Gd d „  d e ƒ Z d d d	 „ Z d
 d „  Z d S)zParsing TRANSFAC files.é    )Úmotifsc               @   sC   e  Z d  Z d Z d d d d d d d h Z d	 d
 d d h Z d S)ÚMotifaŠ  Store the information for one TRANSFAC motif.

    This class inherits from the Bio.motifs.Motif base class, as well
    as from a Python dictionary. All motif information found by the parser
    is stored as attributes of the base class when possible; see the
    Bio.motifs.Motif base class for a description of these attributes. All
    other information associated with the motif is stored as (key, value)
    pairs in the dictionary, where the key is the two-letter fields as found
    in the TRANSFAC file. References are an exception: These are stored in
    the .references attribute.

    These fields are commonly found in TRANSFAC files::

        AC:    Accession number
        AS:    Accession numbers, secondary
        BA:    Statistical basis
        BF:    Binding factors
        BS:    Factor binding sites underlying the matrix
               [sequence; SITE accession number; start position for matrix
               sequence; length of sequence used; number of gaps inserted;
               strand orientation.]
        CC:    Comments
        CO:    Copyright notice
        DE:    Short factor description
        DR:    External databases
               [database name: database accession number]
        DT:    Date created/updated
        HC:    Subfamilies
        HP:    Superfamilies
        ID:    Identifier
        NA:    Name of the binding factor
        OC:    Taxonomic classification
        OS:    Species/Taxon
        OV:    Older version
        PV:    Preferred version
        TY:    Type
        XX:    Empty line; these are not stored in the Record.

    References are stored in an .references attribute, which is a list of
    dictionaries with the following keys::

        RN:    Reference number
        RA:    Reference authors
        RL:    Reference data
        RT:    Reference title
        RX:    PubMed ID

    For more information, see the TRANSFAC documentation.
    ÚBFÚOVÚHPÚBSÚHCÚDTÚDRÚRXÚRAÚRTÚRLN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmultiple_value_keysÚreference_keys© r   r   ú8/tmp/pip-build-ww9dw3qa/biopython/Bio/motifs/transfac.pyr      s   1r   c               @   s.   e  Z d  Z d Z d d „  Z d d „  Z d S)ÚRecordzñStore the information in a TRANSFAC matrix table.

    The record inherits from a list containing the individual motifs.

    Attributes:
     - version - The version number, corresponding to the 'VV' field
       in the TRANSFAC file;

    c             C   s   d |  _  d S)zInitialize.N)Úversion)Úselfr   r   r   Ú__init__Q   s    zRecord.__init__c             C   s
   t  |  ƒ S)z'Turn the TRANSFAC matrix into a string.)Úwrite)r   r   r   r   Ú__str__U   s    zRecord.__str__N)r   r   r   r   r   r   r   r   r   r   r   F   s   	r   Tc             C   sþ  i  } g  } d } t  ƒ  } xÜ|  D]Ô} | j ƒ  } | s= q" | j d d ƒ } | d j ƒ  } | rŒ t | ƒ d k rŒ t d j | ƒ ƒ ‚ t | ƒ d k rÜ | d j ƒ  }	 | rÜ | j d ƒ d sÜ t d j | ƒ ƒ ‚ | d k rô |	 | _ nT| d" k rHi  } |	 j ƒ  d d … d d d d g k rFt d j | | ƒ ƒ ‚ d }
 x d D] } g  | | <qSWxÞ|  D]Ö} | j ƒ  } | j d d ƒ } | d j ƒ  } t | ƒ d k rò| d j ƒ  }	 | rò| j d ƒ d sòt d j | ƒ ƒ ‚ y t | ƒ } Wn t k
 rPYn X|
 d k rN| d k rN| rXt d j | ƒ ƒ ‚ n
 |
 d 7}
 | |
 k ryt d j | ƒ ƒ ‚ | rÐt | ƒ d k r©t d j | | ƒ ƒ ‚ t | ƒ d k rÐt d j | ƒ ƒ ‚ |	 j ƒ  d d … } t | ƒ d k rt d j | ƒ ƒ ‚ x4 t	 d | ƒ D]# \ } } | | j
 t | ƒ ƒ qWqnW| d k rWq" | d k r8|	 j d ƒ \ } } } | d d k r£t d j | | ƒ ƒ ‚ | d# d k rËt d j | | ƒ ƒ ‚ t | d d$ … ƒ } t | ƒ | d k rt d j | t | ƒ d | ƒ ƒ ‚ | |	 i } | j
 | ƒ q" | d k r—| d k	 rˆt d  d d! | ƒ } | j | ƒ | | _ | j
 | ƒ i  } g  } q" | t j k r³|	 | | <q" | t j k rì| | k rØg  | | <| | j
 |	 ƒ q" |	 | | <q" W| S)%z4Parse a transfac format handle into a Record object.Né   r   é   zHThe key value of a TRANSFAC motif line should have 2 characters: "{0:s}"z  zQA TRANSFAC motif line should have 2 spaces between key and value columns: "{0:s}"ZVVÚP0ÚPOé   ÚAÚCÚGÚTzGA TRANSFAC matrix "{0:s}" line should be followed by "A C G T": "{0:s}"ZACGTzgA TRANSFAC matrix should start with "01" as first row of the matrix, but this matrix uses "00": "{0:s}"zQThe TRANSFAC matrix row number does not match the position in the matrix: "{0:s}"z}A TRANSFAC matrix line should have a 2 digit key at the start of the line ("{0:02d}"), but this matrix uses "{0:d}": "{1:s}".z=A TRANSFAC matrix line should have a key and a value: "{0:s}"zWA TRANSFAC matrix line should have a value for each nucleotide (A, C, G and T): "{0:s}"ÚXXÚRNú;ú[zHThe index "{0:s}" in a TRANSFAC RN line should start with a "[": "{0:s}"ú]zFThe index "{0:s}" in a TRANSFAC RN line should end with a "]": "{0:s}"zoThe index "{0:d}" of the TRANSFAC RN line does not match the current number of seen references "{1:d}": "{2:s}"z//ÚalphabetÚcounts)r   r    éÿÿÿÿr-   )r   ÚstripÚsplitÚlenÚ
ValueErrorÚformatÚ	partitionr   ÚintÚzipÚappendÚfloatr   ÚupdateÚ
referencesr   r   )ÚhandleÚstrictÚannotationsr9   r,   ÚrecordÚlineÚ	key_valueÚkeyÚvalueÚlengthÚcÚiÚvaluesÚvÚindexÚ	separatorZ	accessionÚ	referenceÚmotifr   r   r   ÚreadZ   sÂ    			(			
									
rK   c                 s  g  } y |  j  } Wn t k
 r' Yn$ X| d k	 rK d | } | j | ƒ t j } d; } x¨|  D] ‰ g  } x^| D]V} d } x.| D]&}	 |	 d k r|ˆ j }
 |
 d k r± q‡ ˆ j } t ˆ j ƒ } d j	 d g | ƒ } | j | ƒ x„ t
 |
 ƒ D]v ‰  d j	 d g d d „  | Dƒ ƒ d } | t ˆ  d g ‡  ‡ f d  d †  | Dƒ | ˆ  g ƒ } | j | ƒ qù Wd! } nš y ˆ j |	 ƒ } Wn t k
 r©d } Yn X| d k	 r|	 | k róxK | D]# } d" |	 | f } | j | ƒ qÉWn d" |	 | f } | j | ƒ d! } |	 d k r‡ y ˆ j } Wn t k
 rCYq‡ Xd< } x` | D]X } xO | D]G }	 | j |	 ƒ } | d k r‚q^d" |	 | f } | j | ƒ d! } q^WqQWq‡ W| rt d( } | j | ƒ qt Wd) } | j | ƒ d* j	 | ƒ d* } | j | ƒ qa Wd+ j	 | ƒ } | S)=z7Write the representation of a motif in TRANSFAC format.NzVV  %s
XX
//
ÚACÚASÚIDr	   ÚCOÚNAÚDEÚTYÚOSÚOCr   r   r   r   ÚBAr   ÚCCr
   r   ÚPVFr   z      ú z%02.dc             S   s   g  |  ] } d  ‘ q S)z%6.20gr   )Ú.0Úlr   r   r   ú
<listcomp>  s   	 zwrite.<locals>.<listcomp>z      %sr   c                s!   g  |  ] } ˆ j  | ˆ  ‘ q Sr   )r,   )rY   rZ   )rD   rJ   r   r   r[     s   	 Tz%s  %sr'   r   r   r   r   r&   z//Ú
Ú ©rL   rM   ©rN   ©zDTrO   ©rP   ©rQ   ©rR   ©rS   rT   ©zHPzHC©zBF©zP0©rU   ©zBS©zCC©zDR©zOVrW   )r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   )zRNzRXzRAzRTzRL)r   ÚAttributeErrorr6   r   r   rB   Zdegenerate_consensusÚsortedr+   ÚjoinÚrangeÚtupleÚgetr9   )r   Úblocksr   Úblockr   ÚsectionsÚlinesÚsectionZblankr@   rB   ÚsequenceÚlettersr>   rA   rF   r9   ÚkeysrI   Útextr   )rD   rJ   r   r   Ð   s¤    
	              		$	r   N)	r   ZBior   r   ÚdictÚlistr   rK   r   r   r   r   r   Ú<module>   s
   :v