#!/usr/bin/env python # coding: utf-8 # # dnacalc.py # >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-4 # > # >[http://www.sinauer.com/practical-computing-for-biologists.html](http://www.sinauer.com/practical-computing-for-biologists.html) # >see [practicalcomputing.org](practicalcomputing.org) # > # >scripts freely available by the original authors at practicalcomputing.org # >DIRECT LINK: [http://practicalcomputing.org/files/pcfb_examples.zip](http://practicalcomputing.org/files/pcfb_examples.zip) # >Updated to Python 3 by Wayne Decatur # >#### posted as a Gist and IPython Notebook by Wayne (fomightez at GitHub) with full credit and reference to original code authors. # #### dnacalc.py calculates percent of bases in a DNA sequence. <br/> # This program takes a DNA sequence (without checking) <br/> # and shows its length and the nucleotide composition <br/> # This program is described in Chapter 8 of PCfB <br/> The code: # In[1]: DNASeq = "AGTCTCTGACTCTCTCTTCGGTCTCT" #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)) # **See the code in action and explore it interactively [here](https://www.pythonanywhere.com/gists/6153106/dnacalc.py/ipython).** # **Obtain a copy of this entire IPython Notebook [here](https://gist.github.com/fomightez/6180942) in order to explore it interactively.** # # # # # # # # # ### <br/> Additional aid and exploration below: # In[2]: get_ipython().run_line_magic('whos', '') # 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.) # #### See the subsequent variations that build upon these ideas and make more versatile versions as described in the book.