o
    RŀgSE                     @   sT   d Z ddlZddlZddlZddlmZ G dd dejZe	fddZ
d
dd	ZdS )zSubstitution matrices.    N)	as_handlec                       s  e Zd ZdZdddef fdd	Zdd Zdd Zd	d
 Zdd Z	dd Z
d8ddZd8ddZ fddZdd Zdd Zd8ddZedd Zdd Zd8dd Zd!d" Zd#d$ Zd%d& Zd8d'd(Zd)d* Zd+d, Zd-d. Zd/d0 Zd9d2d3Zd4d5 Zd6d7 Z  Z S ):Arrayz8numpy array subclass indexed by integers and by letters.Nc                    s8  t |tr|durtd|durtdg }d}|D ]u}t |tr8|du r*d}n|dkr2td|| qt |tr|du rFt|}n
|t|krPtd|dkrkt |ts]tdt|dkred}|| q|d	kr|D ]}t |ts|tdt|dkrd}|| qqqtd
| qtt|}|rd	|}nt|}t|}|dkr|f}	n|d	kr||f}	ntdt
 | |	|}
|dkrt|D ]\}}||d|
|< qn'|d	krt|D ]\}}t|D ]\}}||f}||d}||
||f< qq||
_|
S |du rtj}nt |ttfstdt|}|du r>|du r-d}n|dvr8td| |f| }	n?|du ri|j}	t|	}|dkrPn-|d	krc|	d |	d krbtdntd| |f| }	|j|	kr}td|	|jf t
 | |	|}
|du rd|
dd< n||
dd< ||
_|
S )zCreate a new Array instance.Nz)alphabet should be None if data is a dictz%dims should be None if data is a dictT   zinconsistent dimensions in datazexpected stringF   zFdata array should be 1- or 2- dimensional (found %d dimensions) in key zdata is an empty dictionaryg        z&alphabet should be a string or a tuple)r   r   z dims should be 1 or 2 (found %s)r   zdata array is not squarez@data array should be 1- or 2- dimensional (found %d dimensions) z=data shape has inconsistent shape (expected (%s), found (%s)))
isinstancedict
ValueErrorstrappendtuplelensortedsetjoinsuper__new__	enumerateget	_alphabetstringascii_uppercaseshape)clsalphabetdimsdatadtypesingle_letterskeyletternr   objii1letter1i2letter2value	__class__ \/var/www/html/myenv/lib/python3.10/site-packages/Bio/Align/substitution_matrices/__init__.pyr      s   















zArray.__new__c                 C   s   |d u rd S t |dd | _d S )Nr   )getattrr   )selfr"   r+   r+   r,   __array_finalize__|   s   zArray.__array_finalize__c              	   C   s   t |tr3g }|D ]#}t |tr'z| j|}W n ty&   td| d w || q	t|}|S t |trPz	| j|}W |S  tyO   td| d w |S )Nz'%s')r   r   r
   r   indexr	   
IndexErrorr   )r.   r   indicesr0   r+   r+   r,   _convert_key   s(   


zArray._convert_keyc                 C   s   |  |}tj| |}|jdkrN| jdkr!|j| jkr tdnL| jdkrM| jd }|jd |kr:|jd dkr:n3|jd dkrI|jd |krIn$tdn|jdkrd|jd | jd krc| j| |_n	|jdkrm|	 S |
tS )Nr   zRequesting truncated arrayr   r   )r3   npndarray__getitem__ndimr   r1   r   r   itemviewr   )r.   r   r(   lengthr+   r+   r,   r6      s,   







zArray.__getitem__c                 C   s   |  |}tj| || d S N)r3   r4   r5   __setitem__r.   r   r(   r+   r+   r,   r<      s   
zArray.__setitem__c                 C   s   ||   v S r;   )keys)r.   r   r+   r+   r,   __contains__   s   zArray.__contains__c                 C   sF   |\}}}| j }|D ]}t|tr|j |krtdq
tj| ||S )Nalphabets are inconsistent)r   r   r   r	   r4   r5   __array_prepare__)r.   out_arrcontextufuncinputsr#   r   argr+   r+   r,   rA      s   


zArray.__array_prepare__c                 C   s$   t |dkr
|d S tj| ||S )Nr   r   )r   r4   r5   __array_wrap__)r.   rB   rC   r+   r+   r,   rG      s   zArray.__array_wrap__c                    sz  g }| j }|D ]}t|tr!|j|krtd||tj q|| q|	dd }|rZg }	|D ]}t|trM|j|krCtd|	|tj q3|	| q3t
|	|d< nd|j }t j||g|R i |}
|
tu rstS |dkryd S |jdkr|
f}
g }t|
|D ](\}}|jdkr|}n|d u rt|t}| j |_ n|}| j |_ || qt|dkr|d S |S )Nr@   outr;   atr   r   )r   r   r   r   r	   r   r9   r4   r5   popr   noutr   __array_ufunc__NotImplementedzipr7   asarrayr   )r.   rD   methodrE   kwargsargsr   rF   outputsout_argsraw_resultsresults
raw_resultoutputresultr)   r+   r,   rL      sJ   







zArray.__array_ufunc__c                 C   sL   dd l }t| }||}| j}t| j}| j}t||d |f}tj	||fS Nr   )
pickler4   arraydumpsr   r   r   r   r   r   )r.   r[   valuesstater   r   r   	argumentsr+   r+   r,   
__reduce__   s   


zArray.__reduce__c                 C   s&   dd l }||| d d d d f< d S rZ   )r[   loads)r.   r_   r[   r+   r+   r,   __setstate__   s   zArray.__setstate__c                 C   s   t j| |}| j|_|S )zTranspose the array.)r4   r5   	transposer   )r.   axesotherr+   r+   r,   rd     s   zArray.transposec                 C   s   | j S )zReturn the alphabet property.)r   r.   r+   r+   r,   r     s   zArray.alphabetc                 C   s   t | j| d}|S )z&Create and return a copy of the array.)r   r   )r   r   )r.   rf   r+   r+   r,   copy  s   z
Array.copyc                 C   s"   z| | W S  t y   | Y S w )z=Return the value of the key if found; return value otherwise.)r1   r=   r+   r+   r,   r     s
   
z	Array.getc           	      c   s    t | j}|dkr"t| jD ]\}}tj| |}||fV  qdS |dkrNt| jD ] \}}t| jD ]\}}||f}tj| ||f}||fV  q4q+dS td| j )z7Return an iterator  of (key, value) pairs in the array.r   r   array has unexpected shape %sN)r   r   r   r   r4   r5   r6   RuntimeError)	r.   r   r0   r   r(   r$   c1r&   c2r+   r+   r,   items  s    
zArray.itemsc                    sL   t | j}| j |dkrt S |dkrt fdd D S td| j )z7Return a tuple with the keys associated with the array.r   r   c                 3   s"    | ]} D ]}||fV  qqd S r;   r+   ).0rl   rk   r   r+   r,   	<genexpr>/  s     zArray.keys.<locals>.<genexpr>ri   )r   r   r   r   rj   )r.   r   r+   ro   r,   r>   (  s   
z
Array.keysc                    s\   t j}j}|dkrtS |dkr'j\ }t fddt|D S tdj )z3Return a tuple with the values stored in the array.r   r   c                 3   s0    | ]}t  D ]}tj||fV  qqd S r;   )ranger4   r5   r6   )rn   r&   r$   n1r.   r+   r,   rp   ;  s    zArray.values.<locals>.<genexpr>ri   )r   r   r   r   rq   rj   )r.   r   r   n2r+   rr   r,   r^   3  s   

zArray.valuesc                 K   sn   |dur*z|  }W n ty   |D ]\}}|| |< qY nw |D ]}|| | |< q!|D ]}|| | |< q,dS )z,Update the array from dict/iterable E and F.N)r>   AttributeError)r.   EFr   r   r(   r+   r+   r,   updateC  s   
zArray.updatec           	   	   C   s   g }g }t |D ] \}}z| j|}W n	 ty   Y qw || || qt| j}t||d}tj	|g|  }tj	|g|  }tj
| |||< |S )zFSubset the array by selecting the letters from the specified alphabet.)r   )r   r   r0   r	   r   r   r   r   r4   ix_r5   r6   )	r.   r   iijjr#   r   jr   ar+   r+   r,   selectQ  s    

zArray.selectc                 C   s   | j }t|}d g| }g }z| j}W n	 ty   Y nw |D ]}d| }|| qd}t|D ]\}	}
| |
 }|| }t|}||krG|}|||	< q1dt| d }t||D ]\}}|| }|| d }|| qYd|}|S )N#  %s
r    %s
r   )	r   r   headerru   r   r   r
   rN   r   )r.   fmtr   r!   wordslinesr   linemaxwidthr#   r   r(   wordwidthfmt2r    textr+   r+   r,   
_format_1Dc  s6   



zArray._format_1Dc                    s  | j }t|  fddt D }g }z| j}W n	 ty"   Y nw |D ]}d| }|| q%tdd |D }dt| d }d| }t|D ]R\}	}
d	}t|D ] \}}||
f}| | }|| }t|}||krn|}||| |	< qTd
t| d }||
 }||7 }t|D ]\}}|| |	 }|| || |	< qqJ|	 d }|| t
||D ]\}}|| }|d| d }|| qd|}|S )Nc                    s   g | ]}d g  qS r;   r+   )rn   _r!   r+   r,   
<listcomp>  s    z$Array._format_2D.<locals>.<listcomp>r   c                 s   s    | ]}t |V  qd S r;   )r   )rn   cr+   r+   r,   rp     s    z#Array._format_2D.<locals>.<genexpr>%r    r   r   r   r   )r   r   rq   r   ru   r   maxr
   r   rstriprN   r   )r.   r   r   r   r   r   r   keywidthkeyfmtr|   rl   r   r#   rk   r   r(   r   r   r   r    rowr   r+   r   r,   
_format_2D  sP   


zArray._format_2Dc                 C   s
   |  |S r;   format)r.   r   r+   r+   r,   
__format__  s   
zArray.__format__r   c                 C   s\   |dkrt | jt jrd}nd}t| j}|dkr| |S |dkr(| |S td| )zReturn a string representation of the array.

        The argument ``fmt`` specifies the number format to be used.
        By default, the number format is "%i" if the array contains integer
        numbers, and "%.1f" otherwise.

        r   z%iz%.1fr   r   zArray has unexpected rank %d)	r4   
issubdtyper   integerr   r   r   r   rj   )r.   r   r!   r+   r+   r,   r     s   


zArray.formatc                 C   s   |   S r;   r   rg   r+   r+   r,   __str__  s   zArray.__str__c                 C   sD   t j| }| j}t|tr |dsJ |d d d| j  }|S )N)z,
         alphabet='%s'))r4   r5   __repr__r   r   r
   endswith)r.   r   r   r+   r+   r,   r     s   
zArray.__repr__r;   )r   )!__name__
__module____qualname____doc__floatr   r/   r3   r6   r<   r?   rA   rG   rL   ra   rc   rd   propertyr   rh   r   rm   r>   r^   rx   r~   r   r   r   r   r   r   __classcell__r+   r+   r)   r,   r      s:    g


1



)
r   c                 C   s  t | }| }W d   n1 sw   Y  g }t|D ]\}}|ds) n||dd   qdd ||d D }t|d t|d   krRdkr~n n*dd |D }|D ]}	t|	dkrkt|} nq]d	|}t	|d|d
}
|

| nG|d}|D ]}	t|	dkrt|} nqd	|}t	|d|d
}
t||D ]\}}||dksJ t||D ]\}}t||
||f< qq||
_|
S )z*Parse the file and return an Array object.N#r   c                 S   s   g | ]}|  qS r+   )split)rn   r   r+   r+   r,   r         zread.<locals>.<listcomp>r   r   c                 S   s   g | ]\}}|qS r+   r+   )rn   r   r(   r+   r+   r,   r     r   r   )r   r   r   )r   	readlinesr   
startswithr   stripr   r   r   r   rx   rJ   rN   r   r   )handler   fpr   r   r#   r   rowsr   r   matrixr%   r   r'   r   r+   r+   r,   read  sD   


(


r   c                 C   s   t jt}t j|}t j|d}| du r3t |}z
|d W t	|S  ty2   Y t	|S w t j|| }t
|}|S )zLoad and return a precalculated substitution matrix.

    >>> from Bio.Align import substitution_matrices
    >>> names = substitution_matrices.load()
    r   Nz
README.txt)ospathrealpath__file__dirnamer   listdirremover	   r   r   )namer   	directorysubdirectory	filenamesr   r+   r+   r,   load  s   
r   r;   )r   r   r   numpyr4   Bio.Filer   r5   r   r   r   r   r+   r+   r+   r,   <module>   s      >&