code by Steven H. D. Haddock and Casey W. Dunn as described in:
Practical Computing for Biologists
by Steven H. D. Haddock and Casey W. Dunn
published in 2011 by Sinauer Associates.
ISBN 978-0-87893-391-4http://www.sinauer.com/practical-computing-for-biologists.html
see practicalcomputing.orgscripts freely available by the original authors at practicalcomputing.org
DIRECT LINK: http://practicalcomputing.org/files/pcfb_examples.zip
Updated to Python 3 by Wayne Decaturposted as a Gist and IPython Notebook by Wayne (fomightez at GitHub) with full credit and reference to original code authors.¶
DNASeq = "ATGTCTCATTCAAAGCA"
#DNASeq = raw_input("Enter a sequence: ")
DNASeq = DNASeq.upper() # convert to uppercase for .count() function
DNASeq = DNASeq.replace(" ","") # remove spaces
print ('Sequence:', DNASeq)
# below are nested functions: first find the length, then make it float
SeqLength = float(len(DNASeq))
print ("Sequence Length:", SeqLength)
NumberA = DNASeq.count('A')
NumberC = DNASeq.count('C')
NumberG = DNASeq.count('G')
NumberT = DNASeq.count('T')
# Old way to output the Numbers
# print "A:", NumberA/SeqLength
# print "C:", NumberC/SeqLength
# print "G:", NumberG/SeqLength
# print "T:", NumberT/SeqLength
# Calculate percentage and output to 1 decimal
print ("A: %.1f" % (100 * NumberA / SeqLength))
print ("C: %.1f" % (100 * NumberC / SeqLength))
print ("G: %.1f" % (100 * NumberG / SeqLength))
print ("T: %.1f" % (100 * NumberT / SeqLength))
#
# End of Chapter 8
# Beginning of Chapter 9
#
# Calculating primer melting points with different formulas by length
TotalStrong = NumberG + NumberC
TotalWeak = NumberA + NumberT
if SeqLength >= 14:
#formula for sequences > 14 nucleotides long
MeltTempLong = 64.9 + 41 * (TotalStrong - 16.4) / SeqLength
print ("Tm Long (>14): %.1f C" % (MeltTempLong))
else:
#formula for sequences less than 14 nucleotides long
MeltTemp = (4 * TotalStrong) + (2 * TotalWeak)
print ("Tm Short: %.1f C" % (MeltTemp))
Sequence: ATGTCTCATTCAAAGCA Sequence Length: 17.0 A: 35.3 C: 23.5 G: 11.8 T: 29.4 Tm Long (>14): 39.8 C
See the code in action and explore it interactively here.
Obtain a copy of this entire IPython Notebook here in order to explore it interactively.
%whos
Variable Type Data/Info --------------------------------- DNASeq str ATGTCTCATTCAAAGCA MeltTempLong float 39.81764705882354 NumberA int 6 NumberC int 4 NumberG int 2 NumberT int 5 SeqLength float 17.0 TotalStrong int 6 TotalWeak int 11
The above special command lets us see what is defined and can be used in an IPython Notebook.
(For some reason it doesn't work for any of the initiating variables over in the interactive gist console.)