B
    b              X   @   s  d Z ddlZddlZddlmZm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Zddddddddddddddddd dZ	dd!d"ddddd#d$d%d&d'd(d)d*d+d,dZ
d-d.dddddd/d$d$d&d'd(d)d*d+d0dZd1dddddddd2d3d4d5d6d7d8d9d:dZd;d<dddddd=d>d?d@dAdBdCdDdEdFdZdGdHddddddIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^Zd_ddddddd`dadbdcdddedfdgdhdidjdkdldmdndodpZdqdrdsdtdudvdwdxdydzd{d|d}d}d~dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddǜWZddddddddddddddddddddddddddddddddddddddddddddddddd0Zddddddddd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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d1d2d(d)d*d*d3d4d5d6d3d4d5d6d70ZdUd8d9Zd:d; ZdVd=d>ZdWdAdBZdXdDdEZdYdHdIZdJdK ZdZdOdPZd[dQdRZedSkrddTlmZ e  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.

Tm_staluc is the 'old' NN calculation and is kept for compatibility. It is,
however, recommended to use Tm_NN instead, since Tm_staluc may be depreceated
in the future. Also, Tm_NN has much more options. Using Tm_staluc and Tm_NN
with default parameters gives (essentially) the same results.

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 unambigiously 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.73
    >>> print('%0.2f' % mt.Tm_NN(myseq))
    60.32

Tm_NN with default values gives same result as 'old' Tm_staluc. However, values
differ for RNA, since Tm_staluc had some errors for RNA calculation. These
errors have been fixed in this version.

New Tm_NN can do slightly more:
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)SeqUtilsSeq)BiopythonWarning)BiopythonDeprecationWarning)r   r   )r   g0)r   g4)r   g)g333333"g      8)g333333!gfffff7)g      gfffff0)g333333g))g      gL1)g333333g4)gffffffg      +)g'g;)g333333&g33333:)g      &g:)initzinit_A/Tzinit_G/Czinit_oneG/Czinit_allA/Tz	init_5T/AsymzAA/TTzAT/TAzTA/ATzCA/GTzGT/CAzCT/GAzGA/CTzCG/GCzGC/CGzGG/CC)g333333?g      ")r   gffffff)g       gfffff5)gffffffgffffff.)gffffffgffffff2)gffffff g      5)g"g     9)gffffffgffffff0)g!g     7)g'g      =)g      %gffffff:)g%gffffff<)gffffff@gffffff@)g?gffffff)gg3333336)ggffffff4)ggL5)g      !g333336)g gffffff6)g333333g      5)gffffff g3333336)g333333%g333333;)g#gffffff8)g       gfffff3)g?g)g@g@)gffffffgL5)g       g      3)r   g%)gg      /)g333333 g6)g      %g;)gffffff$g333333:)gffffffg3333333)g*g     A)g       gffffff3)gffffff,g33333sA)gffffff(g33333=)gzG@g      )g(\@g      %@)gHzGg      3)g(\"g33333:)g(\g     4)gzG$gfffff:)g&g     =)g(\$g;)gzG(g     @@)gHzG%g33333:)g(\-g33333sB)gHz*gY@)g@g(\@)g@gGz&@)g\(\g3)gQ8"g9)g      !gfffff6)g(\&g<)g(\'gL?)g%g     <)gQk*g33333sA)g(\%gffffff;)g
ףp=
0gLD)g\(\,g     A)g)\+g33333sG)gR1gYL)gGzg333333')gQg)g(\$g?)gHzG)g33333sC)g(\g      5)g=
ףp=g+)gzG"g333338)gQg0)gGz.&g<)r   zinit_A/Tzinit_G/Czinit_oneG/Czinit_allA/Tz	init_5T/Ar   zAA/TTzAT/TAzTA/ATzCA/GTzGT/CAzCT/GAzGA/CTzCG/GCzGC/CGzGG/CCzGT/TGzGG/TTzAG/TTzTG/ATzTT/AGzTG/GTzAT/TGzCG/GTzCT/GGzGG/CTzGT/CG)gffffff?g333333)g      'g333333B)g333333g5)g      g333333)g gfffff7)g$gffffff<)g)gfffff?)gL0ǧG)g333333"g     7)g333333!gfffff6)g       g1)g"g3333337)gg()g333333g3333337)g      g      +)g      "g:)g333333gfffff5)r   zinit_A/Tzinit_G/Czinit_oneG/Czinit_allA/Tz	init_5T/Ar   zTT/AAzGT/CAzCT/GAzAT/TAzTG/ACzGG/CCzCG/GCzAG/TCzTC/AGzGC/CGzCC/GGzAC/TGzTA/ATzGA/CTzCA/GTzAA/TT)g      ?g?)g      g )gffffffgffffff')gffffffg       )gffffff
@g$@)g333333@gL0@)gg()gffffff@g      #@)gg333333)gffffffg)gg333333)g333333gffffff)gffffffgffffff)g      gffffff*)g333333g      )g      ?g	@)gffffff?gffffff?)g      @g@)gffffff?g?)g333333g)gg      )g      gffffff)gffffff@g@)g@g      +@)g333333?gffffff?)g      ?gffffff?)gffffff@gffffff@)g333333@g333333-@)gffffff?g@)g333333?g333333)g@gffffff,@)gffffffgffffff)g333333@g       @)gffffff@g3333334@)g333333?g333333?)gg)g333333g#)g@g)@)g        g)g      g)g@g!@)gffffff@gffffff0@)gg      #)gg.)g      g/)g?g@)gg%)g      g/)gg )g?g      )g!g     9)ggffffff1)g!gffffff9)gg+)ggffffff+)g333333g3)g g7)g      g333333))g g      9)g333333gffffff&)gffffffg)ggffffff)g@g!@)g333333g5)g      g      4)gffffffg3333334)g\(\?gffffff)g      g      6)gffffffg333332)gg333333)g      g333333)g      g333333%)g?g      )g333333g333333()gg/)gffffffg      !)g?g)g      ?g      ?)gffffff@gL5@)gg	)g333333@gfffff0@)gffffffg      6)gffffff
g')g?gffffff)g?g      @)g      g)WzAG/TTzAT/TGzCG/GTzCT/GGzGG/CTzGG/TTzGT/CGzGT/TGzTG/ATzTG/GTzTT/AGzAA/TGzAG/TAzCA/GGzCG/GAzGA/CGzGG/CAzTA/AGzTG/AAzAC/TTzAT/TCzCC/GTzCT/GCzGC/CTzGT/CCzTC/ATzTT/ACzAA/TCzAC/TAzCA/GCzCC/GAzGA/CCzGC/CAzTA/ACzTC/AAzAA/TAzCA/GAzGA/CAzTA/AAzAC/TCzCC/GCzGC/CCzTC/ACzAG/TGzCG/GGzGG/CGzTG/AGzAT/TTzCT/GTzGT/CTzTT/ATzAI/TCzTI/ACzAC/TIzTC/AIzCI/GCzGI/CCzCC/GIzGC/CIzAI/TAzTI/AAzAA/TIzTA/AIzCI/GAzGI/CAzCA/GIzGA/CIzAI/TTzTI/ATzAT/TIzTT/AIzCI/GTzGI/CTzCT/GIzGT/CIzAI/TGzTI/AGzAG/TIzTG/AIzCI/GGzGI/CGzCG/GIzGG/CIzAI/TIzTI/AIzCI/GIzGI/CI)gg333333)g      g333333)g333333gffffff%)g       g     6)gg      ?)gffffffg)g gffffff)g333333g333333%)gg )gg)gffffffg      #)gffffffg3333333)g333333g      )g	g!)gffffffgfffff0)gg3333335)gg      )ggffffff)gg)gg      )g      g+)g	gffffff)gffffffg)gg      )gg333333)gffffffg333333)g	g       )gg      +)g      g333333)gffffffg333333)gffffffg)g      g)g333333g333333#)g      g      /)g333333g333333&)gffffffg&)g       g)g333333g333333)g	gffffff!)g      g")gffffffg      ")gffffffg333332)gg/)gg0)g333333g      %)gg#)0zAA/TAzTA/AAzCA/GAzGA/CAzAC/TCzTC/ACzCC/GCzGC/CCzAG/TGzTG/AGzCG/GGzGG/CGzAT/TTzTT/ATzCT/GTzGT/CTzAA/TCzAC/TAzCA/GCzCC/GAzGA/CCzGC/CAzTA/ACzTC/AAzAC/TTzAT/TCzCC/GTzCT/GCzGC/CTzGT/CCzTC/ATzTT/ACzAA/TGzAG/TAzCA/GGzCG/GAzGA/CGzGG/CAzTA/AGzTG/AAzAG/TTzAT/TGzCG/GTzCT/GGzGG/CTzGT/CGzTG/ATzTT/AG)g?gffffff@)g333333g1)gg      $)g333333gffffff)g333333?gffffff
@)gg333333))g      g')gffffffg      *)gg)gffffffg      ,)g333333g%)gg      .)gg      4)g      g%)gg+)gɿg      )gffffffg)g g333333)gg     0)g      g)g@g-@)gɿg)gg)g@gffffff,@)gg)g333333gffffff&)g	g$)gffffffg333333*)g333333@g$@)gg333333*)gg      .)gffffffg333333)) zAA/.TzAC/.GzAG/.CzAT/.AzCA/.TzCC/.GzCG/.CzCT/.AzGA/.TzGC/.GzGG/.CzGT/.AzTA/.TzTC/.GzTG/.CzTT/.Az.A/ATz.C/AGz.G/ACz.T/AAz.A/CTz.C/CGz.G/CCz.T/CAz.A/GTz.C/GGz.G/GCz.T/GAz.A/TTz.C/TGz.G/TCz.T/TA)ggffffff*)gg)g      g333333.)gffffffg      )g      "g     7)gffffffg333333%)g333333!g3333336)g      g(\O4)ggL4)gffffffg)ggffffff0)ggffffff#)gg0)gffffffgffffff)g333333gffffff0)gg333333)g      g333333)g@g6@)g333333?g@)gg      )gffffff?g	@)gffffffg-)gٿg)g333333gffffff)gffffff
@g333333'@)g?g	@)gffffffg)g?gffffff@)g@g333333 @)gffffff?g      @)g@g333333%@)0z.T/AAz.T/CAz.T/GAz.T/TAz.G/ACz.G/CCz.G/GCz.G/TCz.C/AGz.C/CGz.C/GGz.C/TGz.T/AGz.T/CGz.T/GGz.T/TGz.A/ATz.A/CTz.A/GTz.A/TTz.G/ATz.G/CTz.G/GTz.G/TTzAT/.AzCT/.AzGT/.AzTT/.AzAG/.CzCG/.CzGG/.CzTG/.CzAC/.GzCC/.GzGC/.GzTC/.GzAT/.GzCT/.GzGT/.GzTT/.GzAA/.TzCA/.TzGA/.TzTA/.TzAG/.TzCG/.TzGG/.TzTG/.Tc             C   sL   | dkr2dddddddddddddddddd}n|   }|rH|| |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

    N)r   r   )r   zinit_A/Tzinit_G/Czinit_oneG/Czinit_allA/Tz	init_5T/Ar   zAA/TTzAT/TAzTA/ATzCA/GTzGT/CAzCT/GAzGA/CTzCG/GCzGC/CGzGG/CC)copyupdate)Zoldtablevaluestable r   7lib/python3.7/site-packages/Bio/SeqUtils/MeltingTemp.py
make_table  s,    

r   c                sd   d |   } tt|  } |dkr0| S |dkr<d |dkrHd d  fdd| D } | S )	a  Return a sequence which fullfils 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 | ]}| kr|qS r   r   ).0base)basesetr   r   
<listcomp>  s    z_check.<locals>.<listcomp>)joinsplitupperstrr   Zback_transcribe)seqmethodr   )r&   r   _check  s    r.      c             C   sB  |dkr|st d|r t|}d}|s,|S | | |d  }|d }	t||||fdkr~|dks~||k r~|dt||  7 }|d }
|tddkr|
st d	|dkrd
t|
 }|dkrd
t|
dd|
    }|dkrdt|
 }|dkrdt|
 }|dkr*dt|d  t|
 }|dkrjdt	
| d d d t|
 dt|
d   }|dkr,d\}}}}d\}}}|dkr|d }d}|| ||	  d  t|| ||	  d d d| |	   d|  }	|dkrt|	|
 }|dk rDdt	
| d d d t|
 dt|
d   }|S |dk rd 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r>t 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 import MeltingTemp as mt
    >>> print('%0.2f' % mt.salt_correction(Na=50, method=1))
    -21.60
    >>> print('%0.2f' % mt.salt_correction(Na=50, method=2))
    -21.85
    >>> print('%0.2f' % mt.salt_correction(Na=100, Tris=20, method=2))
    -16.45
    >>> print('%0.2f' % mt.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   g       @gMbP?r2   x   r/   z>Total ion concentration of zero is not allowed in this method.g0@   g      ?gffffff?   g      )@   gffffff'@r0   gZd;O?r1   g)\(@d   g@gh㈵>g)>)g\(\@gx&g
ףp=
@gQ?)gHg     @J@gQ @g     L@g      @g)\(?g      @g\(\@g`"?gI+?gQ?gX9v?g>p?gT[r?gQ @gv?gPn?g/$u?z.Allowed values for parameter 'method' are 1-7.)
ValueErrorr+   summathZsqrtrangeZlog10lenlogr   GC)Nar   TrisMgdNTPsr-   r,   corrZMonZmgZmonabcdefgZdntpsZkar   r   r   r   salt_correction  sl    1$


"




6
 &&B,
rK         ??c             C   st   |r| || 8 } |rp|dkr(| || 8 } |dkr`|dks@|dk rHt d| d|d  d | 7 } |d	krpt 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/   r4   Nr   z'GC' is missing or negativegˡE?g      Y@g
ףp=
@)r/   r4   z'fmdmethod' must be 1 or 2)r8   )melting_tempZDMSOZfmdZ
DMSOfactorZ	fmdfactorZ	fmdmethodr>   r   r   r   chem_correction^  s    &rO   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  }|r|rtdn||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   r4   )r   r   r    r6   )r   r   r   r5   )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.   r9   mapcountr8   )r,   checkstrictrN   tmpr   r   r   r     s    
<r   r2   2   c          
   C   s  |
dkrt dt| } |r&t| d} t| }tt| jdd t|  tt| jdd t|   tt| jdd	 t|   }|r|rt d
n||7 }|r|\}}}}n|dkrd\}}}}d}
|dkrd\}}}}d}
|dkrd\}}}}d}
|dkrd\}}}}d}
|dkr,d\}}}}d}
|dkrFd\}}}}d}
|dkr`d\}}}}d}
|dkrzd\}}}}d}
|dkrt d|||  |t| d   }|
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=5. 0 or None means no salt correction.
     - mismatch: If 'True' (default) every 'X' in the sequence is counted as
       mismatch.

    r0   z0salt-correction method 5 not applicable to Tm_GCr   )r   r   r   r   r"   g      I@)r   r   g{GP@)r   r   g
ףp=@@zHambiguous bases B, D, H, K, M, N, R, V, Y not allowed when 'strict=True'r/   )g33333SQ@g=
ףp=?i  r/   r   r4   )g     `T@g=
ףp=?i  r/   r5   r6   )g     `T@g=
ףp=?i  r/   )g     S@gffffff?i  r/   r1   )g     P@g?i  r/   r2   )g     `T@g=
ףp=?iX  r/      )gfffffFS@g=
ףp=?i  r/   z0allowed values for parameter 'valueset' are 0-8.g      ?)r?   r   r@   rA   rB   r,   r-   r!   g      Y@)
r8   r+   r.   r   r>   r9   rP   rQ   r<   rK   )r,   rR   rS   ZvaluesetZusersetr?   r   r@   rA   rB   saltcorrZmismatchZ
percent_gcrT   r   r   r   r   rN   r   r   r   r     s\    D

T





r   c             C   s&   |rt d|  ntd|  t dS )zLThrow an error or a warning if there is no data for the neighbors (PRIVATE).z0no thermodynamic data for neighbors %r availablezJno themodynamic data for neighbors %r available. Calculation will be wrongN)r8   warningswarnr   )	neighborsrS   r   r   r   
_key_error@  s    r[      Fr0   c       %   	   C   s  |st }|st}|st}|s t}t| } |s:t|  }t|}|rZt| d} t|d}| }|}d}d}d}d}|st| t|kr|dkrd| |  }|dk rdt	| | }t|t|kr|t|t| d 7 }t|t|k r|t|t| d 7 }x6|
ds |
dr<|dd }|dd }qW x6|dsX|drt|dd }|dd }q@W |
ds|
dr|dd d	 |dd  }y$||| | 7 }||| | 7 }W n  tk
r   t|| Y nX |dd }|dd }|ds |dr|d
d ddd d	 |d
d ddd  }y$||| | 7 }||| | 7 }W n  tk
r   t|| Y nX |dd }|dd }|dd ddd d	 |dd ddd  }||kr ||| | 7 }||| | 7 }|dd }|dd }|d
d d	 |d
d  }||kr~||| | 7 }||| | 7 }|dd }|dd }||d | 7 }||d | 7 }t| dkr||d | 7 }||d | 7 }n ||d | 7 }||d | 7 }| 
dr||d | 7 }||d | 7 }| drH||d | 7 }||d | 7 }| d | d  }|d|d }|d|d }||d | | 7 }||d | | 7 }||d | | 7 }||d | | 7 }x6tt|d D ] }|||d  d	 |||d   } | |kr:|||  | 7 }|||  | 7 }n| ddd |kr||| ddd  | 7 }||| ddd  | 7 }n| |kr|||  | 7 }|||  | 7 }nT| ddd |kr||| ddd  | 7 }||| ddd  | 7 }n
t| | qW |	|
d  d }!|rH|	d }!||d | 7 }||d | 7 }d}"|rht||||||| d}#|dkrz||#7 }d| ||"t|!   d }$|dkr|$|#7 }$|dkrd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: valuse 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..Nr4   /r   zinit_allA/Tzinit_oneG/Cr   z	init_5T/Ar   r   r   zinit_A/Tzinit_G/Cg       @g&.>r   gn?)r?   r   r@   rA   rB   r-   r,   r0   i  gfffffq@)r/   r4   r5   r6   )r1   r2   )DNA_NN3DNA_TMM1DNA_IMM1DNA_DE1r+   r   Z
complementr.   r<   abs
startswithendswithKeyErrorr[   r   r>   rQ   r;   rK   r:   r=   )%r,   rR   rS   Zc_seqshiftnn_tableZ	tmm_tableZ	imm_tableZde_tablednac1dnac2Zselfcompr?   r   r@   rA   rB   rW   Ztmp_seqZtmp_cseqZdelta_hZdelta_sZd_hZd_sZleft_deZright_deZleft_tmmZ	right_tmmZendsATr>   Z
basenumberrZ   kr   rC   rN   r   r   r   r#   M  s    W

00

$




r#   c             C   s^   t dt |s(t| |d |d |dS |dkrJt| |d |d |tdS td| ddS )	a  Return DNA/DNA Tm using nearest neighbor thermodynamics (OBSOLETE).

    This method may be depreceated in the future. Use Tm_NN instead. Tm_NN
    with default values gives the same result as Tm_staluc.

    s is the sequence as string or Seq object
    dnac is DNA concentration [nM]
    saltc is salt concentration [mM].
    rna=0 is for DNA/DNA (default), use 1 for RNA/RNA hybridisation.

    For DNA/DNA, see Allawi & SantaLucia (1997), Biochemistry 36: 10581-10594
    For RNA/RNA, see Xia et al (1998), Biochemistry 37: 14719-14735

    Examples
    --------
    >>> print("%0.2f" % Tm_staluc('CAGTCAGTACGTACGTGTACTGCCGTA'))
    59.87
    >>> print("%0.2f" % Tm_staluc('CAGTCAGTACGTACGTGTACTGCCGTA', rna=True))
    77.90

    You can also use a Seq object instead of a string,

    >>> from Bio.Seq import Seq
    >>> s = Seq('CAGTCAGTACGTACGTGTACTGCCGTA')
    >>> print("%0.2f" % Tm_staluc(s))
    59.87
    >>> print("%0.2f" % Tm_staluc(s, rna=True))
    77.90

    z2Tm_staluc is deprecated; please use Tm_NN instead.g       @)rk   rl   r?   r/   )rk   rl   r?   rj   zrna=z not supportedN)rX   rY   r   r#   RNA_NN2r8   )sZdnacZsaltcZrnar   r   r   	Tm_staluc?  s    "rq   __main__)run_doctest)NN)r   r   r   r   r   r/   N)r   r   rL   rM   r/   N)TT)TTr2   NrU   r   r   r   r   r   T)TTNr   NNNNr\   r\   FrU   r   r   r   r   r0   )rU   rU   r   ) __doc__r:   rX   ZBior   r   r   r   ZDNA_NN1ZDNA_NN2ra   ZDNA_NN4ZRNA_NN1ro   ZRNA_NN3Z	R_DNA_NN1rc   rb   rd   ZRNA_DE1r   r.   rK   rO   r   r   r[   r#   rq   __name__Z
Bio._utilsrs   r   r   r   r   <module>   s  


00x7.          r                 a.