B
    b              A   @   sl  d Z ddlZddlmZ ddlmZ ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd@Zddgd	d
gddddddgddgdgddgddddgddgdddgdd d!d"gd#d$gd%d&d'd(gd)d*d+d,gd-d.d/gd0d1d2d3d4d5gd6d7gd8d9d:d;d<d=gd>gd?d@dAdBgdCdDgdEdFgdGZG dHdI dIZdS )Jz%Methods for codon usage calculations.    N   )SharpEcoliIndex)SeqIO)@TTTTTCTTATTGCTTCTCCTACTGATTATCATAATGGTTGTCGTAGTGTATTACTAATAGCATCACCAACAGAATAACAAAAAGGATGACGAAGAGTCTTCCTCATCGCCTCCCCCACCGACTACCACAACGGCTGCCGCAGCGTGTTGCTGATGGCGTCGCCGACGGAGTAGCAGAAGGGGTGGCGGAGGGr5   r6   r!   r"   r%   r(   r'   r&   r>   r=   r   r   r   r   r   r)   r,   r+   r*   r    r   r   r7   r   r.   r/   r0   r-   r   r   r3   r2   r4   r1   rA   rD   rC   rB   r   r   r   r   r   r
   r	   r   r   r   r   r;   r:   r<   r9   r@   r?   r8   r   r   r   r   r$   r#   r   r   )ZCYSZASPZSERZGLNZMETZASNZPROZLYSZSTOPZTHRZPHEZALAZGLYZILEZLEUZHISZARGZTRPZVALZGLUZTYRc               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )CodonAdaptationIndexaO  A codon adaptation index (CAI) implementation.

    Implements the codon adaptation index (CAI) described by Sharp and
    Li (Nucleic Acids Res. 1987 Feb 11;15(3):1281-95).

    NOTE - This implementation does not currently cope with alternative genetic
    codes: only the synonymous codons in the standard table are considered.
    c             C   s   i | _ i | _dS )zInitialize the class.N)indexcodon_count)self rI   6lib/python3.7/site-packages/Bio/SeqUtils/CodonUsage.py__init__I   s    zCodonAdaptationIndex.__init__c             C   s
   || _ dS )zSet up an index to be used when calculating CAI for a gene.

        Just pass a dictionary similar to the SharpEcoliIndex in the
        CodonUsageIndices module.
        N)rF   )rH   rF   rI   rI   rJ   set_cai_indexO   s    z"CodonAdaptationIndex.set_cai_indexc       
      C   s   | j i ks| ji krtd| | xtD ]}d}g }t| }x|D ]}|| j| 7 }qFW x0|D ](}t|t| }|| j| |  qbW t|}x&t	|D ]\}	}||	 | | j |< qW q,W dS )a  Generate a codon usage index from a FASTA file of CDS sequences.

        Takes a location of a Fasta file containing CDS sequences
        (which must all have a whole number of codons) and generates a codon
        usage index.

        RCSU values
        zVan index has already been set or a codon count has been done. Cannot overwrite either.g        N)
rF   rG   
ValueError_count_codonsSynonymousCodonsfloatlenappendmax	enumerate)
rH   
fasta_fileZaaZtotalZrcsuZcodonscodondenominatorZrcsu_maxZcodon_indexrI   rI   rJ   generate_indexW   s     




z#CodonAdaptationIndex.generate_indexc             C   s   d\}}| j i kr| t | r,| }xrtdt|dD ]^}|||d  }|| j kr|dkr|t| j | 7 }|d7 }q>|dkr>t	d|| j f q>W t
||d  S )	zCalculate the CAI (float) for the provided DNA sequence (string).

        This method uses the Index (either the one you set or the one you
        generated) and returns the CAI for the DNA sequence.
        )r   r   r      )r   r8   r   )r7   r   r   z!illegal codon in sequence: %s.
%sg      ?)rF   rL   r   islowerupperrangerQ   mathlog	TypeErrorZexp)rH   dna_sequenceZ	cai_valueZ
cai_lengthirV   rI   rI   rJ   cai_for_gene   s    



z!CodonAdaptationIndex.cai_for_genec          	   C   s   t |}t | _xt|dD ]}t|j rDt|j	 }n
t|j}xXt
dt|dD ]D}|||d  }|| jkr| j|  d7  < q`td||jf q`W q"W W d Q R X d S )NZfastar   rY   r   zillegal codon %s in gene: %s)open
CodonsDictcopyrG   r   parsestrseqrZ   r[   r\   rQ   r_   id)rH   rU   ZhandleZ
cur_recordr`   ra   rV   rI   rI   rJ   rN      s    



z"CodonAdaptationIndex._count_codonsc             C   s.   x(t | jD ]}td|| j| f  qW dS )zbPrint out the index used.

        This just gives the index when the objects is printed.
        z%s	%.3fN)sortedrF   print)rH   ra   rI   rI   rJ   print_index   s    z CodonAdaptationIndex.print_indexN)
__name__
__module____qualname____doc__rK   rL   rX   rb   rN   rl   rI   rI   rI   rJ   rE   ?   s   )rE   )	rp   r]   ZCodonUsageIndicesr   ZBior   rd   rO   rE   rI   rI   rI   rJ   <module>   sR   




