o
    Rŀg                     @   s
  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 i 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i 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#d0Zi dddd1d	d2d
ddddddd&dd3dd4dd5dd6dd7dd8dd9dd:d!d;d#d<Z	i dd=dd>d	dd
ddddddd&dd?dd4dd5dd6dd7dd8dd9dd:d!d;d#d<Z
i dd@ddd	dd
ddddddd&dd)ddAddBddCddDddEddFddGd!dHd#dIZi ddJddKd	dd
ddddddd&ddLddMddNddOddPddQddRddSd!dTd#dUZi ddVddWd	dd
ddddddd&ddXddYddZdd[dd\dd]dd^dd_d!d`d#dadbdcdddedfdgdhdidjdkdldmZi ddnddd	dd
ddddddddodpddqddrddsdtdud#dvddwdxdydzd{d!d|d}d~dddddZi ddddddddddddddddddddddddddddddddddi ddddddddddddddddddddddddddddddddēddƓi ddȓddʓdd̓ddΓddГddғddԓdd֓ddؓddړddܓddޓddddddddddi dddddddddddddddddddddddd ddddddddd	d
i 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i dɐd0dϐd1dːd2d͐d3dѐd4dאd5dӐd6dՐd7dِd8dߐd9dېd:dݐd;dd<dd=dd>dd?dd@i ddAddBddCddDdÐdEdŐdFdǐdGddHddIddJdd7ddKddLdd1ddMddNddOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^Zi d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~ddddddddddddddddddZi 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i dddddddddddddddeddmddudd}ddcddkddsdd{ddaddiddddddddÐdĐdŐdƐdÐdĐdŐdƐdǜZddȐdɄZdʐd˄ Zdd͐d΄Z	ddѐd҄ZddԐdՄZ	Ӑ	Ӑ									ӐddؐdلZdڐdۄ Z	Ӑ								ܐ	ܐ	ݐ						ސddߐdZedkrddlmZ e  dS dS (  a  Calculate the melting temperature of nucleotide sequences.

This module contains three different methods to calculate the melting
temperature of oligonucleotides:

1. Tm_Wallace: 'Rule of thumb'
2. Tm_GC: Empirical formulas based on GC content. Salt and mismatch corrections
   can be included.
3. Tm_NN: Calculation based on nearest neighbor thermodynamics. Several tables
   for DNA/DNA, DNA/RNA and RNA/RNA hybridizations are included.
   Correction for mismatches, dangling ends, salt concentration and other
   additives are available.

General parameters for most Tm methods:
 - seq -- A Biopython sequence object or a string.
 - check -- Checks if the sequence is valid for the given method (default=
   True). In general, whitespaces and non-base characters are removed and
   characters are converted to uppercase. RNA will be backtranscribed.
 - strict -- Do not allow base characters or neighbor duplex keys (e.g.
   'AT/NA') that could not or not unambiguously be evaluated for the respective
   method (default=True). Note that W (= A or T) and S (= C or G) are not
   ambiguous for Tm_Wallace and Tm_GC. If 'False', average values (if
   applicable) will be used.

This module is not able to detect self-complementary and it will not use
alignment tools to align an oligonucleotide sequence to its target sequence.
Thus it can not detect dangling-ends and mismatches by itself (don't even think
about bulbs and loops). These parameters have to be handed over to the
respective method.

Other public methods of this module:
 - make_table     : To create a table with thermodynamic data.
 - salt_correction: To adjust Tm to a given salt concentration by different
   formulas. This method is called from Tm_GC and Tm_NN but may
   also be accessed 'manually'. It returns a correction term, not
   a corrected Tm!
 - chem_correction: To adjust Tm regarding the chemical additives DMSO and
   formaldehyde. The method returns a corrected Tm. Chemical
   correction is not an integral part of the Tm methods and must
   be called additionally.

For example:

    >>> from Bio.SeqUtils import MeltingTemp as mt
    >>> from Bio.Seq import Seq
    >>> mystring = 'CGTTCCAAAGATGTGGGCATGAGCTTAC'
    >>> myseq = Seq(mystring)
    >>> print('%0.2f' % mt.Tm_Wallace(mystring))
    84.00
    >>> print('%0.2f' % mt.Tm_Wallace(myseq))
    84.00
    >>> print('%0.2f' % mt.Tm_GC(myseq))
    58.97
    >>> print('%0.2f' % mt.Tm_NN(myseq))
    60.32

Using different thermodynamic tables, e.g. from Breslauer '86 or Sugimoto '96:

    >>> print('%0.2f' % mt.Tm_NN(myseq, nn_table=mt.DNA_NN1))  # Breslauer '86
    72.19
    >>> print('%0.2f' % mt.Tm_NN(myseq, nn_table=mt.DNA_NN2))  # Sugimoto '96
    65.47

Tables for RNA and RNA/DNA hybrids are included:

    >>> print('%0.2f' % mt.Tm_NN(myseq, nn_table=mt.RNA_NN1))  # Freier '86
    73.35
    >>> print('%0.2f' % mt.Tm_NN(myseq, nn_table=mt.R_DNA_NN1))  # Sugimoto '95
    58.45

Several types of salc correction (for Tm_NN and Tm_GC):

    >>> for i in range(1, 8):
    ...     print("Type: %d, Tm: %0.2f" % (i, Tm_NN(myseq, saltcorr=i)))
    ...
    Type: 1, Tm: 54.27
    Type: 2, Tm: 54.02
    Type: 3, Tm: 59.60
    Type: 4, Tm: 60.64
    Type: 5, Tm: 60.32
    Type: 6, Tm: 59.78
    Type: 7, Tm: 59.78

Correction for other monovalent cations (K+, Tris), Mg2+ and dNTPs according
to von Ahsen et al. (2001) or Owczarzy et al. (2008) (for Tm_NN and Tm_GC):

    >>> print('%0.2f' % mt.Tm_NN(myseq, Na=50, Tris=10))
    60.79
    >>> print('%0.2f' % mt.Tm_NN(myseq, Na=50, Tris=10, Mg=1.5))
    67.39
    >>> print('%0.2f' % mt.Tm_NN(myseq, Na=50, Tris=10, Mg=1.5, saltcorr=7))
    66.81
    >>> print('%0.2f' % mt.Tm_NN(myseq, Na=50, Tris=10, Mg=1.5, dNTPs=0.6,
    ...                          saltcorr=7))
    66.04

Dangling ends and mismatches, e.g.::

    Oligo:     CGTTCCaAAGATGTGGGCATGAGCTTAC       CGTTCCaAAGATGTGGGCATGAGCTTAC
               ::::::X:::::::::::::::::::::  or   ::::::X:::::::::::::::::::::
    Template:  GCAAGGcTTCTACACCCGTACTCGAATG      TGCAAGGcTTCTACACCCGTACTCGAATGC

Here:

    >>> print('%0.2f' % mt.Tm_NN('CGTTCCAAAGATGTGGGCATGAGCTTAC'))
    60.32
    >>> print('%0.2f' % mt.Tm_NN('CGTTCCAAAGATGTGGGCATGAGCTTAC',
    ...                    c_seq='GCAAGGcTTCTACACCCGTACTCGAATG'))
    55.39
    >>> print('%0.2f' % mt.Tm_NN('CGTTCCAAAGATGTGGGCATGAGCTTAC', shift=1,
    ...                   c_seq='TGCAAGGcTTCTACACCCGTACTCGAATGC'))
    55.69

The same for RNA:

    >>> print('%0.2f' % mt.Tm_NN('CGUUCCAAAGAUGUGGGCAUGAGCUUAC',
    ...                   c_seq='UGCAAGGcUUCUACACCCGUACUCGAAUGC',
    ...                   shift=1, nn_table=mt.RNA_NN3,
    ...                   de_table=mt.RNA_DE1))
    73.00

Note, that thermodynamic data are not available for all kind of mismatches,
e.g. most double mismatches or terminal mismatches combined with dangling ends:

    >>> print('%0.2f' % mt.Tm_NN('CGTTCCAAAGATGTGGGCATGAGCTTAC',
    ...                   c_seq='TtCAAGGcTTCTACACCCGTACTCGAATGC',
    ...                   shift=1))
    Traceback (most recent call last):
    ValueError: no thermodynamic data for neighbors '.C/TT' available

Make your own tables, or update/extend existing tables. E.g., add values for
locked nucleotides. Here, 'locked A' (and its complement) should be represented
by '1':

    >>> mytable = mt.make_table(oldtable=mt.DNA_NN3,
    ...                         values={'A1/T1':(-6.608, -17.235),
    ...                         '1A/1T':(-6.893, -15.923)})
    >>> print('%0.2f' % mt.Tm_NN('CGTTCCAAAGATGTGGGCATGAGCTTAC'))
    60.32
    >>> print('%0.2f' % mt.Tm_NN('CGTTCCA1AGATGTGGGCATGAGCTTAC',
    ...                           nn_table=mytable, check=False))
    ... # 'check' must be False, otherwise '1' would be discarded
    62.53

    N)BiopythonWarning)Seq)SeqUtilsinitr   r   init_A/Tinit_G/Cinit_oneG/C)r   0init_allA/T)r   g4	init_5T/Asym)r   AA/TT)333333"g      8AT/TA)333333!fffff7TA/AT)      fffff0CA/GT)333333g)GT/CA)      gL1CT/GA)333333g4GA/CT)ffffff      +CG/GC)';GC/CG)333333&33333:GG/CC)g      &g:)333333?      ")r   ffffff)       fffff5)r   gffffff.)ffffffgffffff2)ffffff       5)"     9)r,   ffffff0)皙!     7)g'g      =)      %gffffff:)%ffffff<)ffffff@ffffff@)皙?ffffff)g3333336)gffffff4)r<   L5)      !g333336) gffffff6)r   r.   )r-   r;   )333333%g333333;)皙#gffffff8)r*   gfffff3)皙?)皙@皙@)ffffffr=   )r   皙%)rC         /)g333333 g6)r4   r"   )gffffff$g333333:)rF   3333333)g*g     A)r*   gffffff3)gffffff,33333sA)gffffff(g33333=)gzG@      )g(\@g      %@)gHzGg      3)g(\"r%   )g(\g     4)gzG$gfffff:)&g     =)g(\$g;)gzG(g     @@)gHzG%r%   )g(\-g33333sB)gHz*gY@)g@g(\@)g@gGz&@)g\(\g3)gQ8"g9)r>   fffff6)g(\&<)g(\'gL?)r5   g     <)gQk*rJ   )g(\%gffffff;)g
ףp=
0gLD)g\(\,g     A)g)\+g33333sG)gR1gYL)gGzg333333')gQ)g(\$g?)gHzG)g33333sC)g(\r.   )g=
ףp=+)gzG"g333338)gQr
   )gGz.&rN   )GT/TGGG/TTAG/TTTG/ATTT/AGTG/GTAT/TGCG/GTCT/GGGG/CTGT/CG)ffffff?333333zTT/AA)g      'g333333B)r   g5)      g333333)皙 r   zTG/AC)$r6   )g)gfffff?)gL0ǧGzAG/TC)r   r3   zTC/AG)r   rM   )r*   皙1)g"3333337)皙皙()r   rb   )      r   )r(   g:)r   r+   )zCC/GGzAC/TGr   r   r   r   rS   )      ?g?rW   )      r_   rX   )ffffffgffffff'rY   )r:   r*   rZ   )ffffff
@$@rR   )333333@gL0@r[   )皙rd   rQ   )r8   g      #@rT   )皙333333rV   )r)   rU   )r   g333333zAA/TG)333333ffffffzAG/TA)ffffffrq   CA/GGCG/GA)      ffffff*GA/CG)rp         GG/CA)      ?皙	@TA/AG)ffffff?r}   TG/AA)      @g@zAC/TT)r}   rB   zAT/TC)333333ro   zCC/GT)皙      zCT/GC)rK   ffffffzGC/CT)r7   g@zGT/CC)@g      +@zTC/AT)333333?r}   zTT/AC)rf   r}   zAA/TC)r7   gffffff@zAC/TA)g333333@g333333-@zCA/GC)r\   g@zCC/GA)r'   rp   zGA/CC)r   ffffff,@zGC/CA)rr   ffffffzTA/AC)g333333@g       @zTC/AA)gffffff@g3333334@zAA/TA)r   g333333?zCA/GA)zGA/CA)333333rA   zTA/AA)@g)@zAC/TC)g        rl   zCC/GC)rK   r<   zGC/CC)@!@zTC/AC)ffffff@gffffff0@zAG/TG)      #zCG/GG)皙g.zGG/CG)r   皙/zTG/AG)皙?r   zAT/TT)皙rG   zCT/GT)      r   zGT/CT)皙r?   zTT/AT)rB   rK   zAI/TC)!r0   zTI/AC)rc   gffffff1zAC/TI)r2   gffffff9zTC/AI)r   rP   zCI/GC)ggffffff+zGI/CC)333333g3zCC/GI)r_   g7zGC/CI)r   333333)zAI/TA)r_   g      9zTI/AA)g333333ffffff&zAA/TI)rr   zTA/AI)r   ffffffzCI/GA)@r   zGI/CA)r   g5zCA/GI)r^         4zGA/CI)rF   g3333334zAI/TT)g\(\?rr   zTI/AT)r         6zAT/TI)r   333332zTT/AI)r   333333zCI/GT)rx   333333zGI/CT)      r@   zCT/GI)r9   rx   zGT/CI)r   g333333(zAI/TG)r   r   zTI/AG)ffffffr>   zAG/TI)r9   rO   zTG/AI)rf   rf   zCI/GG)gffffff@gL5@zGI/CG)皙皙	zCG/GI)rk   gfffff0@zGG/CI)rF   r   zAI/TI)gffffff
r!   zTI/AI)r9   rq   )g?r   )      r   )zCI/GIzGI/CI)r   r   )rg   333333)r   gffffff%)r*   g     6)rm   rz   )rr   r   ) ffffff)r]   r@   )r   r   )r   r   )r   r   )rr   rI   )r   r   )r   r   )r   r   )皙g3333335)皙ru   )rO   r   )r   rc   )r   r   )r   皙+)r   gffffff)rq   rc   )r   r^   )r   rn   )rq   r   )r   r*   )r   r   )g      r   )rr   r   )r   rl   )rg   rc   )r]   g333333#)r   rH   )r   r$   )r   rL   )g       g)r   r   )r   gffffff!)r   r/   )r   r(   )r,   r   )rC   g/)rc   皙0)r]   r4   )rA   )rs   rt   rw   ry   r|   r~   rS   rW   rX   rY   rZ   r[   rT   rU   AA/.T)rB   r7   zAC/.G)r   ra   zAG/.C)gg      $zAT/.A)r   rF   CA/.T)r'   ri   zCC/.G)rl   r   zCG/.C)ru   r!   zCT/.A)rh   g      *GA/.T)r   r   GC/.G)r   g      ,zGG/.C)r]   r5   zGT/.A)r         .TA/.T)gr   TC/.G)ru   r5   zTG/.C)r   r   zTT/.A)皙ɿr   z.A/AT)rr   r   )r   r]   )rc   g     0)r   r   )g@g-@)r   rm   )r   r   )r   r   )r   r   )r]   r   )r   r`   )rh   333333*)g333333@rj   )rl   r   )gr   )r   r   ).C/AG.G/AC.T/AA.A/CT.C/CG.G/CC.T/CA.A/GT.C/GG.G/GC.T/GA.A/TT.C/TG.G/TC.T/TAr   )r   rv   r   )r   r   r   )re   g333333.r   )rq   re   r   )r(   r3   r   )rh   r@   r   )r   r;   r   )g      g(\O4r   )r   gL4r   )r:   gr   )gr1   r   )r   gffffff#z.T/AGz.T/CGz.T/GGz.T/TG)rC   r   r   )rr   r   r   )r   r1   r   )r   r   z.G/ATz.G/CTz.G/GTz.G/TT)r   rp   )rE   g6@)r'   r   )r   r   )r}   r{   )r   g-)gٿr   )r   r   )ri   g333333'@)皙?r{   )r)   r   )r   r   )rD   g333333 @)r}   g      @)g@g333333%@)r   r   zAT/.GzCT/.GzGT/.GzTT/.Gr   r   r   r   zAG/.TzCG/.TzGG/.TzTG/.Tc                 C   s   | du r:i 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}n|   }|rE|| |S )a  Return a table with thermodynamic parameters (as dictionary).

    Arguments:
     - oldtable: An existing dictionary with thermodynamic parameters.
     - values: A dictionary with new or updated values.

    E.g., to replace the initiation parameters in the Sugimoto '96 dataset with
    the initiation parameters from Allawi & SantaLucia '97:

    >>> from Bio.SeqUtils.MeltingTemp import make_table, DNA_NN2
    >>> table = DNA_NN2                               # Sugimoto '96
    >>> table['init_A/T']
    (0, 0)
    >>> newtable = make_table(oldtable=DNA_NN2, values={'init': (0, 0),
    ...                       'init_A/T': (2.3, 4.1),
    ...                       'init_G/C': (0.1, -2.8)})
    >>> print("%0.1f, %0.1f" % newtable['init_A/T'])
    2.3, 4.1

    Nr   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r    r#   r&   )copyupdate)oldtablevaluestable r   L/var/www/html/myenv/lib/python3.10/site-packages/Bio/SeqUtils/MeltingTemp.py
make_table{  sP   	

r   c                    sd   d |   } tt|  } |dkr| S |dkrd |dkr$d d  fdd| D } | S )	a  Return a sequence which fulfills the requirements of the given method (PRIVATE).

    All Tm methods in this package require the sequence in uppercase format.
    Most methods make use of the length of the sequence (directly or
    indirectly), which can only be expressed as len(seq) if the sequence does
    not contain whitespaces and other non-base characters. RNA sequences are
    backtranscribed to DNA. This method is PRIVATE.

    Arguments:
     - seq: The sequence as given by the user (passed as string).
     - method: Tm_Wallace, Tm_GC or Tm_NN.

    >>> from Bio.SeqUtils import MeltingTemp as mt
    >>> mt._check('10 ACGTTGCAAG tccatggtac', 'Tm_NN')
    'ACGTTGCAAGTCCATGGTAC'

     
Tm_WallaceTm_GC)ABCDGHIKMNRSTVWXYTm_NN)r   r   r   r   r   c                    s   g | ]}| v r|qS r   r   ).0basebasesetr   r   
<listcomp>  s    z_check.<locals>.<listcomp>)joinsplitupperstrr   back_transcribe)seqmethodr   r   r   _check  s   r      c                 C   s(  |dv r
|s
t dd}|s|S | | |d  }|d }	t||||fdkr9|dkr9||k r9|dt||  7 }|d }
|tddv rJ|
sJt d	|dkrUd
t|
 }|dkrfd
t|
dd|
    }|dkrqdt|
 }|dkr|dt|
 }|dkrdt|d  t|
 }|dkrdt	|d d d t|
 dt|
d   }|dkrd\}}}}d\}}}|dkr|d }d}|| ||	  d  t|| ||	  d d d| |	   d|  }	|dkrSt|	|
 }|dk rdt	|d d d t|
 dt|
d   }|S |dk rSd d!d"t|
 t|
   }d#d$d%t|
  d&t|
d    }d'd(d)t|
  d*t|
d    }||t|	  t	|d||t|	    ddt|d   ||t|	  |t|	d     d }|dkrt d+|S ),a  Calculate a term to correct Tm for salt ions.

    Depending on the Tm calculation, the term will correct Tm or entropy. To
    calculate corrected Tm values, different operations need to be applied:

     - methods 1-4: Tm(new) = Tm(old) + corr
     - method 5: deltaS(new) = deltaS(old) + corr
     - methods 6+7: Tm(new) = 1/(1/Tm(old) + corr)

    Arguments:
     - Na, K, Tris, Mg, dNTPS: Millimolar concentration of respective ion. To
       have a simple 'salt correction', just pass Na. If any of K, Tris, Mg and
       dNTPS is non-zero, a 'sodium-equivalent' concentration is calculated
       according to von Ahsen et al. (2001, Clin Chem 47: 1956-1961):
       [Na_eq] = [Na+] + [K+] + [Tris]/2 + 120*([Mg2+] - [dNTPs])^0.5
       If [dNTPs] >= [Mg2+]: [Na_eq] = [Na+] + [K+] + [Tris]/2
     - method: Which method to be applied. Methods 1-4 correct Tm, method 5
       corrects deltaS, methods 6 and 7 correct 1/Tm. The methods are:

       1. 16.6 x log[Na+]
          (Schildkraut & Lifson (1965), Biopolymers 3: 195-208)
       2. 16.6 x log([Na+]/(1.0 + 0.7*[Na+]))
          (Wetmur (1991), Crit Rev Biochem Mol Biol 126: 227-259)
       3. 12.5 x log(Na+]
          (SantaLucia et al. (1996), Biochemistry 35: 3555-3562
       4. 11.7 x log[Na+]
          (SantaLucia (1998), Proc Natl Acad Sci USA 95: 1460-1465
       5. Correction for deltaS: 0.368 x (N-1) x ln[Na+]
          (SantaLucia (1998), Proc Natl Acad Sci USA 95: 1460-1465)
       6. (4.29(%GC)-3.95)x1e-5 x ln[Na+] + 9.40e-6 x ln[Na+]^2
          (Owczarzy et al. (2004), Biochemistry 43: 3537-3554)
       7. Complex formula with decision tree and 7 empirical constants.
          Mg2+ is corrected for dNTPs binding (if present)
          (Owczarzy et al. (2008), Biochemistry 47: 5336-5353)

    Examples
    --------
    >>> from Bio.SeqUtils.MeltingTemp import salt_correction
    >>> print('%0.2f' % salt_correction(Na=50, method=1))
    -21.60
    >>> print('%0.2f' % salt_correction(Na=50, method=2))
    -21.85
    >>> print('%0.2f' % salt_correction(Na=100, Tris=20, method=2))
    -16.45
    >>> print('%0.2f' % salt_correction(Na=100, Tris=20, Mg=1.5, method=2))
    -10.99

    )         zKsequence is missing (is needed to calculate GC content or sequence length).r          @gMbP?r   x   r   z>Total ion concentration of zero is not allowed in this method.g0@   rf   r}      g      )@   gffffff'@r   gZd;O?r   g)\(@ignoreg@gh㈵>g)>)\(\@gx&g
ףp=
@Q?)gHg     @J@Q @g     L@g      @g)\(?g      @r   g`"?gI+?r   gX9v?g>p?gT[r?r   gv?gPn?g/$u?z.Allowed values for parameter 'method' are 1-7.)

ValueErrorsummathsqrtrangelog10lenlogr   gc_fraction)Nar   TrisMgdNTPsr   r   corrMonmgmonabcdefgdntpskar   r   r   r   salt_correction  s   1$ 





 "
r        ??c                 C   st   |r| || 8 } |r8|dkr| || 8 } |dkr0|du s |dk r$t d| d|d  d | 7 } |d	vr8t d
| S )a$  Correct a given Tm for DMSO and formamide.

    Please note that these corrections are +/- rough approximations.

    Arguments:
     - melting_temp: Melting temperature.
     - DMSO: Percent DMSO.
     - fmd: Formamide concentration in %(fmdmethod=1) or molar (fmdmethod=2).
     - DMSOfactor: How much should Tm decreases per percent DMSO. Default=0.65
       (von Ahsen et al. 2001). Other published values are 0.5, 0.6 and 0.675.
     - fmdfactor: How much should Tm decrease per percent formamide.
       Default=0.65. Several papers report factors between 0.6 and 0.72.
     - fmdmethod:

         1. Tm = Tm - factor(%formamide) (Default)
         2. Tm = Tm + (0.453(f(GC)) - 2.88) x [formamide]

       Here f(GC) is fraction of GC.
       Note (again) that in fmdmethod=1 formamide concentration is given in %,
       while in fmdmethod=2 it is given in molar.
     - GC: GC content in percent.

    Examples:
        >>> from Bio.SeqUtils import MeltingTemp as mt
        >>> mt.chem_correction(70)
        70
        >>> print('%0.2f' % mt.chem_correction(70, DMSO=3))
        67.75
        >>> print('%0.2f' % mt.chem_correction(70, fmd=5))
        66.75
        >>> print('%0.2f' % mt.chem_correction(70, fmdmethod=2, fmd=1.25,
        ...                                    GC=50))
        66.68

    r   r   Nr   z'GC' is missing or negativegˡE?      Y@g
ףp=
@)r   r   z'fmdmethod' must be 1 or 2)r   )melting_tempDMSOfmd
DMSOfactor	fmdfactor	fmdmethodGCr   r   r   chem_correctionP  s   &r%  Tc                 C   s   t | } |rt| d} dtt| jd dtt| jd  }dtt| jd dtt| jd	  d
tt| jd  }|rE|rEtd||7 }|S )a  Calculate and return the Tm using the 'Wallace rule'.

    Tm = 4 degC * (G + C) + 2 degC * (A+T)

    The Wallace rule (Thein & Wallace 1986, in Human genetic diseases: a
    practical approach, 33-50) is often used as rule of thumb for approximate
    Tm calculations for primers of 14 to 20 nt length.

    Non-DNA characters (e.g., E, F, J, !, 1, etc) are ignored by this method.

    Examples:
        >>> from Bio.SeqUtils import MeltingTemp as mt
        >>> mt.Tm_Wallace('ACGTTGCAATGCCGTA')
        48.0
        >>> mt.Tm_Wallace('ACGT TGCA ATGC CGTA')
        48.0
        >>> mt.Tm_Wallace('1ACGT2TGCA3ATGC4CGTA')
        48.0

    r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   r   g
@)r   r   gUUUUUU@)r   r   zFambiguous bases B, D, H, K, M, N, R, V, Y not allowed when strict=True)r   r   r  mapcountr   )r   checkstrictr  tmpr   r   r   r     s$   
r   r   2   c              
      s  |
dkrt dt  |rt d |r$t fdddD r$t dt dd	 }|r;| d
d t  8 }|rD|\}}}}n`|dkrPd\}}}}d}
|dkr\d\}}}}d}
|dkrhd\}}}}d}
|dkrtd\}}}}d}
|dkrd\}}}}d}
|dkrd\}}}}d}
|dkrd\}}}}d}
|dkrd\}}}}d}
|dkrt d|||  |t   }|
r|t|||||	 |
d7 }|r|| d
d t   8 }|S )al
  Return the Tm using empirical formulas based on GC content.

    General format: Tm = A + B(%GC) - C/N + salt correction - D(%mismatch)

    A, B, C, D: empirical constants, N: primer length
    D (amount of decrease in Tm per % mismatch) is often 1, but sometimes other
    values have been used (0.6-1.5). Use 'X' to indicate the mismatch position
    in the sequence. Note that this mismatch correction is a rough estimate.

    >>> from Bio.SeqUtils import MeltingTemp as mt
    >>> print("%0.2f" % mt.Tm_GC('CTGCTGATXGCACGAGGTTATGG', valueset=2))
    69.20

    Arguments:
     - valueset: A few often cited variants are included:

        1. Tm = 69.3 + 0.41(%GC) - 650/N
           (Marmur & Doty 1962, J Mol Biol 5: 109-118; Chester & Marshak 1993),
           Anal Biochem 209: 284-290)
        2. Tm = 81.5 + 0.41(%GC) - 675/N - %mismatch
           'QuikChange' formula. Recommended (by the manufacturer) for the
           design of primers for QuikChange mutagenesis.
        3. Tm = 81.5 + 0.41(%GC) - 675/N + 16.6 x log[Na+]
           (Marmur & Doty 1962, J Mol Biol 5: 109-118; Schildkraut & Lifson
           1965, Biopolymers 3: 195-208)
        4. Tm = 81.5 + 0.41(%GC) - 500/N + 16.6 x log([Na+]/(1.0 + 0.7 x
           [Na+])) - %mismatch
           (Wetmur 1991, Crit Rev Biochem Mol Biol 126: 227-259). This is the
           standard formula in approximative mode of MELTING 4.3.
        5. Tm = 78 + 0.7(%GC) - 500/N + 16.6 x log([Na+]/(1.0 + 0.7 x [Na+]))
           - %mismatch
           (Wetmur 1991, Crit Rev Biochem Mol Biol 126: 227-259). For RNA.
        6. Tm = 67 + 0.8(%GC) - 500/N + 16.6 x log([Na+]/(1.0 + 0.7 x [Na+]))
           - %mismatch
           (Wetmur 1991, Crit Rev Biochem Mol Biol 126: 227-259). For RNA/DNA
           hybrids.
        7. Tm = 81.5 + 0.41(%GC) - 600/N + 16.6 x log[Na+]
           Used by Primer3Plus to calculate the product Tm. Default set.
        8. Tm = 77.1 + 0.41(%GC) - 528/N + 11.7 x log[Na+]
           (von Ahsen et al. 2001, Clin Chem 47: 1956-1961). Recommended 'as a
           tradeoff between accuracy and ease of use'.

     - userset: Tuple of four values for A, B, C, and D. Usersets override
       valuesets.
     - Na, K, Tris, Mg, dNTPs: Concentration of the respective ions [mM]. If
       any of K, Tris, Mg and dNTPS is non-zero, a 'sodium-equivalent'
       concentration is calculated and used for salt correction (von Ahsen et
       al., 2001).
     - saltcorr: Type of salt correction (see method salt_correction).
       Default=0. 0 or None means no salt correction.
     - mismatch: If 'True' (default) every 'X' in the sequence is counted as
       mismatch.

    r   z0salt-correction method 5 not applicable to Tm_GCr   c                 3   s    | ]}| v V  qd S )Nr   )r   xr   r   r   	<genexpr>  s    zTm_GC.<locals>.<genexpr>	KMNRYBVDHzHambiguous bases B, D, H, K, M, N, R, V, Y not allowed when 'strict=True'weightedd   r   g      I@r   )g33333SQ@=
ףp=?i  r   r   r   )     `T@r2  i  r   r   r   )r3  r2    r   )g     S@r}   r4  r   r   )g     P@r   r4  r   r   )r3  r2  iX  r      )gfffffFS@r2  i  r   z0allowed values for parameter 'valueset' are 0-8.)r	  r   r
  r  r  r   r   r  )	r   r   r   anyr   r  r'  r  r  )r   r(  r)  valuesetusersetr	  r   r
  r  r  saltcorrmismatch
percent_gcr   r   r   r   r  r   r-  r   r     s`   D
r   c                 C   s(   |r
t d| dtd|  t dS )zLThrow an error or a warning if there is no data for the neighbors (PRIVATE).z$no thermodynamic data for neighbors z
 availablezJno themodynamic data for neighbors %r available. Calculation will be wrongN)r   warningswarnr   )	neighborsr)  r   r   r   
_key_error2  s   r?     Fr   c           %   	   C   s  |st }|st}|st}|st}t| } |st|  }t|}|r-t| d} t|d}| }|}d}d}d}d}|sDt| t|krZ|dkrNd| |  }|dk rZdt	| | }t|t|krn|t|t| d 7 }t|t|k r|t|t| d 7 }|
ds|
dr|dd }|dd }|
ds|
ds|ds|dr|dd }|dd }|ds|ds|
ds|
dr|dd d	 |dd  }z||| | 7 }||| | 7 }W n ty   t|| Y nw |dd }|dd }|ds|drZ|d
d ddd d	 |d
d ddd  }z||| | 7 }||| | 7 }W n tyM   t|| Y nw |dd }|dd }|dd ddd d	 |dd ddd  }||v r||| | 7 }||| | 7 }|dd }|dd }|d
d d	 |d
d  }||v r||| | 7 }||| | 7 }|dd }|dd }||d | 7 }||d | 7 }t| ddkr||d | 7 }||d | 7 }n||d | 7 }||d | 7 }| 
dr||d | 7 }||d | 7 }| dr(||d | 7 }||d | 7 }| d | d  }|d|d }|d|d }||d | | 7 }||d | | 7 }||d | | 7 }||d | | 7 }tt|d D ]}|||d  d	 |||d   } | |v r|||  | 7 }|||  | 7 }qt| ddd |v r||| ddd  | 7 }||| ddd  | 7 }qt| |v r|||  | 7 }|||  | 7 }qt| ddd |v r||| ddd  | 7 }||| ddd  | 7 }qtt| | qt|	|
d  d }!|r(|	d }!||d | 7 }||d | 7 }d}"|r8t||||||| d}#|dkrA||#7 }d| ||"t|!   d }$|dv rY|$|#7 }$|dv rjdd|$d  |#  d }$|$S ) a  Return the Tm using nearest neighbor thermodynamics.

    Arguments:
     - seq: The primer/probe sequence as string or Biopython sequence object.
       For RNA/DNA hybridizations seq must be the RNA sequence.
     - c_seq: Complementary sequence. The sequence of the template/target in
       3'->5' direction. c_seq is necessary for mismatch correction and
       dangling-ends correction. Both corrections will automatically be
       applied if mismatches or dangling ends are present. Default=None.
     - shift: Shift of the primer/probe sequence on the template/target
       sequence, e.g.::

                           shift=0       shift=1        shift= -1
        Primer (seq):      5' ATGC...    5'  ATGC...    5' ATGC...
        Template (c_seq):  3' TACG...    3' CTACG...    3'  ACG...

       The shift parameter is necessary to align seq and c_seq if they have
       different lengths or if they should have dangling ends. Default=0
     - table: Thermodynamic NN values, eight tables are implemented:
       For DNA/DNA hybridizations:

        - DNA_NN1: values from Breslauer et al. (1986)
        - DNA_NN2: values from Sugimoto et al. (1996)
        - DNA_NN3: values from Allawi & SantaLucia (1997) (default)
        - DNA_NN4: values from SantaLucia & Hicks (2004)

       For RNA/RNA hybridizations:

        - RNA_NN1: values from Freier et al. (1986)
        - RNA_NN2: values from Xia et al. (1998)
        - RNA_NN3: values from Chen et al. (2012)

       For RNA/DNA hybridizations:

        - R_DNA_NN1: values from Sugimoto et al. (1995)
          Note that ``seq`` must be the RNA sequence.

       Use the module's maketable method to make a new table or to update one
       one of the implemented tables.
     - tmm_table: Thermodynamic values for terminal mismatches.
       Default: DNA_TMM1 (SantaLucia & Peyret, 2001)
     - imm_table: Thermodynamic values for internal mismatches, may include
       insosine mismatches. Default: DNA_IMM1 (Allawi & SantaLucia, 1997-1998;
       Peyret et al., 1999; Watkins & SantaLucia, 2005)
     - de_table: Thermodynamic values for dangling ends:

        - DNA_DE1: for DNA. Values from Bommarito et al. (2000) (default)
        - RNA_DE1: for RNA. Values from Turner & Mathews (2010)

     - dnac1: Concentration of the higher concentrated strand [nM]. Typically
       this will be the primer (for PCR) or the probe. Default=25.
     - dnac2: Concentration of the lower concentrated strand [nM]. In PCR this
       is the template strand which concentration is typically very low and may
       be ignored (dnac2=0). In oligo/oligo hybridization experiments, dnac1
       equals dnac1. Default=25.
       MELTING and Primer3Plus use k = [Oligo(Total)]/4 by default. To mimic
       this behaviour, you have to divide [Oligo(Total)] by 2 and assign this
       concentration to dnac1 and dnac2. E.g., Total oligo concentration of
       50 nM in Primer3Plus means dnac1=25, dnac2=25.
     - selfcomp: Is the sequence self-complementary? Default=False. If 'True'
       the primer is thought binding to itself, thus dnac2 is not considered.
     - Na, K, Tris, Mg, dNTPs: See method 'Tm_GC' for details. Defaults: Na=50,
       K=0, Tris=0, Mg=0, dNTPs=0.
     - saltcorr: See method 'Tm_GC'. Default=5. 0 means no salt correction.

    r   r   r   .z..Nr   /r   r   r   r	   r   r   r   r   r   r   r   r   g&.>r   gn?)r	  r   r
  r  r  r   r   r   i  gfffffq@)r   r   r   r   )r   r   )DNA_NN3DNA_TMM1DNA_IMM1DNA_DE1r   r   
complementr   r  abs
startswithendswithKeyErrorr?  r   r  r'  r  r  r  r  )%r   r(  r)  c_seqshiftnn_table	tmm_table	imm_tablede_tablednac1dnac2selfcompr	  r   r
  r  r  r9  tmp_seqtmp_cseqdelta_hdelta_sd_hd_sleft_deright_deleft_tmm	right_tmmendsATr$  
basenumberr>  kr   r  r  r   r   r   r   ?  s   W

00






r   __main__)run_doctest)NN)r   r   r   r   r   r   N)r   r   r  r  r   N)TT)TTr   Nr+  r   r   r   r   r   T)TTNr   NNNNr@  r@  Fr+  r   r   r   r   r   )__doc__r  r<  Bior   r   r   DNA_NN1DNA_NN2rE  DNA_NN4RNA_NN1RNA_NN2RNA_NN3	R_DNA_NN1rG  rF  rH  RNA_DE1r   r   r  r%  r   r   r?  r   __name__
Bio._utilsrf  r   r   r   r   <module>   s   



			

   
%			




			

00v8.~ s
