
    Ri_/                     d    d Z ddlmZ  G d dej                  e      Z G d de      Zd
dZd Zy	)zParsing TRANSFAC files.    )motifsc                   2     e Zd ZdZh dZh dZ fdZ xZ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BSCCDRDTHCHPOV>   RARLRTRXc                     	 t         |   |      }|S # t        $ r! t         t        j                  |   |      }Y |S w xY w)N)super__getitem__	TypeErrorr   r   )selfkeyvalue	__class__s      K/home/agent/.friday_env/lib/python3.12/site-packages/Bio/motifs/transfac.pyr   zMotif.__getitem__F   sI    	?G',E   	?&,,9#>E	?s    &>>)__name__
__module____qualname____doc__multiple_value_keysreference_keysr   __classcell__)r   s   @r   r   r      s#    0d K .N     r   c                       e Zd ZdZd Zd Zy)RecordzStore 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                     d| _         y)zInitialize the class.N)versionr   s    r   __init__zRecord.__init__Y   s	    r"   c                     t        |       S )z'Turn the TRANSFAC matrix into a string.)writer'   s    r   __str__zRecord.__str__]   s    T{r"   N)r   r   r   r   r(   r+    r"   r   r$   r$   N   s    r"   r$   c                    i }g }d}t               }| D ]  }|j                         }|s|j                  dd      }|d   j                         }|rt        |      dk7  rt	        d| d      t        |      dk(  r8|d   j                         }	|r#|j                  d      d   st	        d| d      |d	k(  r		|_        n|d
v ri }	j                         dd g dk7  rt	        d| d|       d}
dD ]  }g ||<   	 | D ]c  }|j                         }|j                  dd      }|d   j                         }t        |      dk(  r8|d   j                         }	|r#|j                  d      d   st	        d| d      	 t        |      }|
dk(  r|dk(  r|rt	        d|       |
dz  }
||
k7  rt	        d| d      |rCt        |      dk(  rt	        d|dd|dd|dd      t        |      dk7  rt	        d| d      |	j                         dd }t        |      dk7  rt	        d| d      t        d|      D ]"  \  }}||   j                  t        |             $ f |dk(  r]|dk(  r	j                  d      \  }}}|d   dk7  rt	        d| d | d      |d!   d"k7  rt	        d| d#| d      t        |dd!       }t        |      |dz
  k7  r$t	        d|dd$t        |      dz   dd|dd      ||	i}|j                  |       |d%k(  r>|6t        d|&      }|j                  |       ||_        |j                  |       i }g }H|t        j                  v r	|<   a|t        j                  v r||vrg ||<   ||   j                  	       	||<    |S # t        $ r Y  Sw xY w)'z4Parse a transfac format handle into a Record object.N   r      zAThe key value of a TRANSFAC motif line should have 2 characters:""  zKA TRANSFAC motif line should have 2 spaces between key and value columns: "VV)P0PO   )ACGTzA TRANSFAC matrix "z(" line should be followed by "A C G T": ACGTzaA TRANSFAC matrix should start with "01" as first row of the matrix, but this matrix uses "00": "zKThe TRANSFAC matrix row number does not match the position in the matrix: "zLA TRANSFAC matrix line should have a 2 digit key at the start of the line ("02dz"), but this matrix uses "dz": "sz".z7A TRANSFAC matrix line should have a key and a value: "zQA TRANSFAC matrix line should have a value for each nucleotide (A, C, G and T): "XXRN;[zThe index "z2" in a TRANSFAC RN line should start with a "[": "]z0" in a TRANSFAC RN line should end with a "]": "zP" of the TRANSFAC RN line does not match the current number of seen references "//)alphabetcounts)r$   stripsplitlen
ValueError	partitionr&   intzipappendfloatr   update
referencesr    r   )handlestrictannotationsrQ   rF   recordline	key_valuer   r   lengthcivaluesvindex	separator	accession	referencemotifs                       r   readrb   b   s   KJFXF x%zz|JJtQ'	l  "3x1} vQ   y>QaL&&(E~~d+A.$ 6$ 
 $;"FNL F{{}Ra $88 )# /..2V5  F q	 0/zz| JJtQ/	l((*y>Q&%aL..0E#~~d3A6",!DDH6!L# CA Q;16(KKO&R 
 aKF;$,,064  3x1}(??@g F667U$tAhbJ 
 9~*(((,vQ0  r*v;!#$99=aA   / /DAq1I$$U1X./_0/b 4<D[*///#*>'E9iQx3 !% )%%)F!-  RyC !% )%%)F!-  a$E:%!)+ !% +J!+A.d4(!= 
 eIi(D[!vf=[)#- e$KJE((("IcNE---+%#%C ##E*$Kqx%r MU " s   N//	N=<N=c                    g }	 | j                   }|d|z  }|j                  |       	 t        j                  }d}| D ]  }g }|D ]  }d}	|D ]  }
|
dk(  r|j
                  }|dk(  r|j                  }t        |j                        }dj                  dg|z         }|j                  |       t        |      D ]s  }dj                  d	g|D cg c]  }d
 c}z         dz   }|t        |dz   g|D cg c]  }|j                  |   |    c}z   ||   gz         z  }|j                  |       u d}	nR	 |j                  |
      }|>|
|v r |D ]  }|
 d| }|j                  |        n|
 d| }|j                  |       d}	|
dk(  s?	 |j                  }d}|D ]7  }|D ]0  }
|j                  |
      }||
 d| }|j                  |       d}	2 9  |	sd}|j                  |        d}|j                  |       dj                  |      dz   }|j                  |        dj                  |      }|S # t        $ r Y %w xY wc c}w c c}w # t        $ r d}Y %w xY w# t        $ r Y -w xY w)z7Write the representation of a motif in TRANSFAC format.NzVV  %s
XX
//
))ACAS)ID)r
   CO)NA)DE)TY)OSOC)r   r   )r   )r3   )BA)r   )r   )r	   )r   PVFr3   r   z       z%02.dz%6.20gz      %sr.   Tr1   rn   )r?   r   r   r   r   r>   rD   
 )r&   rN   AttributeErrorr   r   rX   degenerate_consensussortedrE   joinrangetuplerF   getrQ   )r   blocksr&   blockr   sectionsra   linessectionblankr   rX   sequencelettersrV   rZ   _r   r\   rQ   keysr`   texts                          r   r*   r*      s   F!.. 
   MM% 33H"  @ 9	#GE 4-$;"\\F{ $99H$U^^4G#==$')9:DLL&"6] 
+HHgYG1Lq(1L%LM()   $eUG;BCau||Aq1CD'{m,'  
 T*
+ !E% %		# ("55%* 3*-b} %T 23 '*U"UG#4D!LL. $$;-%*%5%5
  >)3 -I'+ -(1c(:#(=$,*-b'8 %T 2(,--[4-j T"s9	#v T		% 4'eA@D 776?DKE  ` 2M
 D * % $% * sA   I 	I2I4II.	III+	*I+	.	I;	:I;	N)T)	r   Bior   r   dictlistr$   rb   r*   r,   r"   r   <module>r      s9     >FLL$ >BT (Dgr"   