
    Ri|,                         d Z ddlZddlZddlmZ ddlmZ ddlmZ  G d dej                        Z G d d	e	      Z
d
 Zd Zd Zd Zd Zd Zd Zy)zJASPAR2014 module.    N)Align)motifs)Seqc                   d    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 d	dZed        Zed        Zd Zd Z	d Z
y)
Motifa  A subclass of Bio.motifs.Motif used to represent a JASPAR profile.

    Additional metadata information are stored if available. The metadata
    availability depends on the source of the JASPAR motif (a 'pfm' format
    file, a 'jaspar' format file or a JASPAR database).
    Nc                     t         j                  j                  | |||       || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        || _        || _        || _        || _        y)z"Construct a JASPAR Motif instance.N)r   r   __init__name	matrix_id
collectiontf_class	tf_familyspecies	tax_groupacc	data_typemedlinepazar_idcomment)selfr   r
   alphabet	alignmentcountsr   r   r   r   r   r   r   r   r   r   s                   R/home/agent/.friday_env/lib/python3.12/site-packages/Bio/motifs/jaspar/__init__.pyr	   zMotif.__init__   sr    & 	dHi@	"$ " ""     c                 6    t        | j                        \  }}|S )z!Return the JASPAR base matrix ID.split_jaspar_idr   )r   base_id__s      r   r   zMotif.base_id=   s     (7"r   c                 6    t        | j                        \  }}|S )z!Return the JASPAR matrix version.r   )r   r    versions      r   r"   zMotif.versionC   s     (7Wr   c                 |   d| j                    d}d| j                   d}dj                  ||g      }| j                  r#d| j                   d}dj                  ||g      }| j                  r#d| j                   d}dj                  ||g      }| j
                  r#d| j
                   d}dj                  ||g      }| j                  r2dd	j                  | j                         d}dj                  ||g      }| j                  r#d
| j                   d}dj                  ||g      }| j                  r#d| j                   d}	dj                  ||	g      }| j                  r#d| j                   d}
dj                  ||
g      }| j                  r#d| j                   d}dj                  ||g      }| j                  r#d| j                   d}dj                  ||g      }| j                  r#d| j                   d}dj                  ||g      }d| j                   d}dj                  ||g      }|S )zReturn a string representation of the JASPAR profile.

        We choose to provide only the filled metadata information.
        zTF name	
z
Matrix ID	 zCollection	z	TF class	z
TF family	zSpecies	,zTaxonomic group	z
Accession	zData type used	zMedline	z	PAZAR ID	z	Comments	zMatrix:
z

)r
   r   joinr   r   r   r   r   r   r   r   r   r   r   )r   tf_name_strmatrix_id_str
the_stringcollection_strtf_class_strtf_family_strspecies_strtax_group_stracc_strdata_type_strmedline_strpazar_id_strcomment_str
matrix_strs                  r   __str__zMotif.__str__I   s-   
 "$))B/%dnn%5R8WWk=9:
??+DOO+<B?N*n!=>J=='b9L*l!;<J>>)$..)9<M*m!<=J<<%chht||&<%=R@K*k!:;J>>//?rBM*m!<=J88#DHH:R0G*g!67J>>.t~~.>bAM*m!<=J<<%dll^26K*k!:;J=='b9L*l!;<J<<&t||nB7K*k!:;J T2
WWj*56
r   c                 6    | j                   j                         S )zvReturn the hash key corresponding to the JASPAR profile.

        :note: We assume the unicity of matrix IDs

        )r   __hash__r   s    r   r8   zMotif.__hash__s   s     ~~&&((r   c                 4    | j                   |j                   k(  S )z'Return True if matrix IDs are the same.r   )r   others     r   __eq__zMotif.__eq__{   s    ~~00r   )ACGTNNNNNNNNNNNN)__name__
__module____qualname____doc__r	   propertyr   r"   r6   r8   r=    r   r   r   r      sn     !"H  
  
(T)1r   r   c                   "    e Zd ZdZd Zd Zd Zy)RecordzaRepresent a list of jaspar motifs.

    Attributes:
     - version: The JASPAR version used

    c                     d| _         y)zInitialize the class.N)r"   r9   s    r   r	   zRecord.__init__   s	    r   c                 2    dj                  d | D              S )z,Return a string of all motifs in the Record.r$   c              3   2   K   | ]  }t        |        y wN)str).0	the_motifs     r   	<genexpr>z!Record.__str__.<locals>.<genexpr>   s     >IY>s   )r'   r9   s    r   r6   zRecord.__str__   s    yy>>>>r   c                 6    i }| D ]  }|||j                   <    |S )z8Return the list of matrices as a dictionary of matrices.r;   )r   dicmotifs      r   to_dictzRecord.to_dict   s)     	)E#(C 	)
r   N)r?   r@   rA   rB   r	   r6   rR   rD   r   r   rF   rF      s    ?r   rF   c                     |j                         }|dk(  rt        |       }|S |dk(  rt        |       }|S |dk(  rt        |       }|S t	        d|z        )zRead motif(s) from a file in one of several different JASPAR formats.

    Return the record of PFM(s).
    Call the appropriate routine based on the format passed.
    pfmsitesjasparUnknown JASPAR format %s)lower	_read_pfm_read_sites_read_jaspar
ValueError)handleformatrecords      r   readr`      sd     \\^F6"	7	V$	8	f%3f<==r   c                 J   d}g }|dk(  rR| d   }|j                   }|D ];  }||   D cg c]  }|d }}dj                  |       d}	|j                  |	       = n|dk(  r| D ]  }
|
j                   }	 |
j                  }d	| d|
j
                   d}	|j                  |	       |D ]>  }||   D cg c]  }|d }}| d
dj                  |       d}	|j                  |	       @  nt        d|z        dj                  |      }|S c c}w # t        $ r d}Y w xY wc c}w )z@Return the representation of motifs in "pfm" or "jaspar" format.r>   rT   r   z6.2f r$   rV   N>z [z]
rW   r%   )r   r'   appendr   AttributeErrorr
   r\   )r   r^   letterslinesrQ   r   lettervaluetermslinemr   texts                r   writern      sc   GEq	 	F28.Ad|_AEAhhuo&b)DLL	 
8	 	#AXXF!KK	 yk166("-DLL! #6<VnEUE$<EE CHHUO#4C8T"#	# 3f<== 775>DK- B " ! 	!
 Fs   D
3D/D DDc                 .   d}i }t        ||       D ]>  \  }}|j                         }|d   |k(  r|dd }|D cg c]  }t        |       c}||<   @ t        dd||      }d|j                  z  |_        t               }|j                  |       |S c c}w )z1Read the motif from a JASPAR .pfm file (PRIVATE).r>   r      N)r   r
   r   r   *)zipsplitfloatr   lengthmaskrF   rd   )	r]   r   r   rh   rk   wordsxrQ   r_   s	            r   rY   rY      s    HFHf- 3

8v!"IE,12q%(2v3 DthvNEu||#EJXF
MM%M 3s   Bc                    d}g }| D ]i  }|j                  d      s nVt        |       }d}|j                         D ]  }|j                         s||z  } t	        |      }|j                  |       k t        j                  |      }t        dd||      }d|j                  z  |_
        t               }|j                  |       |S )z1Read the motif from JASPAR .sites file (PRIVATE).r>   rc   r%   N)r   r
   r   r   rq   )
startswithnextstripisupperr   rd   r   	Alignmentr   ru   rv   rF   )	r]   r   	instancesrk   instancecr   rQ   r_   s	            r   rZ   rZ      s    HI #s# F| 	Ayy{A	 x="# 	*IDth)TEu||#EJXF
MM%Mr   c           	         d}i }t               }t        j                  d      }t        j                  d      }t        j                  d      }d}d}d}	g d}
| D ]g  }|j                         }|j	                  |      }|j	                  |      }|j	                  |      }|r7|j                  d      }|j                  d	      r|j                  d	      }}|}|rs|j                  dd
      \  }}|j                         }|D cg c]  }t        |       c}||<   |	dz  }	|	dk(  s|j                  t        ||||             d}d}i }d}	|s|j                  d      j                         }|D cg c]  }t        |       c}||
|	   <   |	dz  }	|	dk(  sB|j                  t        ||||             d}d}i }d}	j |S c c}w c c}w )at  Read motifs from a JASPAR formatted file (PRIVATE).

    Format is one or more records of the form, e.g.::

      - JASPAR 2010 matrix_only format::

                >MA0001.1 AGL3
                A  [ 0  3 79 40 66 48 65 11 65  0 ]
                C  [94 75  4  3  1  2  5  2  3  3 ]
                G  [ 1  0  3  4  1  0  5  3 28 88 ]
                T  [ 2 19 11 50 29 47 22 81  1  6 ]

      - JASPAR 2010-2014 PFMs format::

                >MA0001.1 AGL3
                0	3	79	40	66	48	65	11	65	0
                94	75	4	3	1	2	5	2	3	3
                1	0	3	4	1	0	5	3	28	88
                2	19	11	50	29	47	22	81	1	6

    r>   z^>\s*(\S+)(\s+(\S+))?z\s*([ACGT])\s*\[\s*(.*)\s*\]z
\s*(.+)\s*Nr   )ACGTrp            )r   r   )
rF   recompiler|   matchgrouprs   rt   rd   r   )r]   r   r   r_   head_patrow_pat_longrow_pat_short
identifierr
   	row_countnucleotidesrk   
head_matchrow_match_longrow_match_shortrh   
counts_strrw   rx   s                      r   r[   r[      s   , HFXFzz23H::=>LJJ}-MJDI&K !zz|^^D)
%++D1'--d3#))!,J"!''*!#1#7#71#= VZ$$&E0561eAh6F6NNIA~eJxPVWX!
	#))!,224E@E-F1eAh-FF;y)*NIA~eJxPVWX!
	C!F M' 7 .Gs   G5Gc                      j                   } j                  }d}t         j                        D ]  |t	         fd|D              z  } | j                  z  }t        j                  |      }|rt        |      }nt        j                  t        |      d      }t	        |j                               }i }|D ]  }||xx   |z  cc<   |||   z  ||<    |S )zCalculate pseudocounts.

    Computes the root square of the total number of sequences multiplied by
    the background nucleotide.
    r   c              3   B   K   | ]  }j                   |        y wrJ   )r   )rL   rh   irQ   s     r   rN   z)calculate_pseudocounts.<locals>.<genexpr>R  s     DU\\&)!,Ds   g      ?)r   
backgroundrangeru   summathsqrtdictfromkeyssortedvalues)	rQ   r   r   totalavg_nb_instancessq_nb_instancespseudocountsrh   r   s	   `       @r   calculate_pseudocountsr   E  s     ~~H!!J E5<<  ED8DDDE u||+ii 01O*%
]]6(#3S9

!!#$EL D6e#.F1CCVD r   c                 p    | j                  d      }d}d}t        |      dk(  r|d   }|d   }||fS | }||fS )zSplit a JASPAR matrix ID into its component.

    Components are base ID and version number, e.g. 'MA0047.2' is returned as
    ('MA0047', 2).
    .Nr   r   rp   )rs   len)idid_splitr   r"   s       r   r   r   f  sX     xx}HGG
8}1+1+ W Wr   )rB   r   r   Bior   r   Bio.Seqr   r   listrF   r`   rn   rY   rZ   r[   r   r   rD   r   r   <module>r      s_      	   l1FLL l1^T 0>(B(6FRBr   