Skip to content

Instantly share code, notes, and snippets.

@fomightez
Last active December 20, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomightez/6181093 to your computer and use it in GitHub Desktop.
Save fomightez/6181093 to your computer and use it in GitHub Desktop.
dnacalc1.ipynb from Practical Computing for Biologists by Steven H. D. Haddock and Casey W. Dunn AS A STATIC IPYTHON Notebook. Posted as a Gist by Wayne Decatur (fomightez) with full credit and reference to the original authors and note where the freely share the code online. You can see an interactive IPython gist of this at https://www.pythona…
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "dnacalc1.ipynb"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"dnacalc1.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
">code by Steven H. D. Haddock and Casey W. Dunn as described in: \n",
">Practical Computing for Biologists \n",
">by Steven H. D. Haddock and Casey W. Dunn \n",
">published in 2011 by Sinauer Associates. \n",
">ISBN 978-0-87893-391-4 \n",
">\n",
">[http://www.sinauer.com/practical-computing-for-biologists.html](http://www.sinauer.com/practical-computing-for-biologists.html) \n",
">see [practicalcomputing.org](practicalcomputing.org) \n",
">\n",
">scripts freely available by the original authors at practicalcomputing.org \n",
">DIRECT LINK: [http://practicalcomputing.org/files/pcfb_examples.zip](http://practicalcomputing.org/files/pcfb_examples.zip) \n",
">####posted as a Gist and IPython Notebook by Wayne (fomightez at GitHub) with full credit and reference to original code authors."
]
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"dnacalc1.py calculates percent of bases in a DNA sequence. <br/>\n",
"# This program takes a DNA sequence (without checking) <br/>\n",
"# and shows its length and the nucleotide composition <br/>\n",
"# This program is described in Chapter 8 of PCfB <br/>\n",
"The code:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"DNASeq = \"ATGTCTCATTCAAAGCA\" \n",
"#DNASeq = raw_input(\"Enter a sequence: \")\n",
"DNASeq = DNASeq.upper() # convert to uppercase for .count() function\n",
"DNASeq = DNASeq.replace(\" \",\"\") # remove spaces\n",
"\n",
"print 'Sequence:', DNASeq \n",
" \n",
"# below are nested functions: first find the length, then make it float\n",
" \n",
"SeqLength = float(len(DNASeq)) \n",
" \n",
"print \"Sequence Length:\", SeqLength\n",
" \n",
"NumberA = DNASeq.count('A')\n",
"NumberC = DNASeq.count('C')\n",
"NumberG = DNASeq.count('G')\n",
"NumberT = DNASeq.count('T')\n",
" \n",
"# Old way to output the Numbers\n",
"# print \"A:\", NumberA/SeqLength\n",
"# print \"C:\", NumberC/SeqLength\n",
"# print \"G:\", NumberG/SeqLength\n",
"# print \"T:\", NumberT/SeqLength\n",
" \n",
"# Calculate percentage and output to 1 decimal\n",
"print \"A: %.1f\" % (100 * NumberA / SeqLength)\n",
"print \"C: %.1f\" % (100 * NumberC / SeqLength)\n",
"print \"G: %.1f\" % (100 * NumberG / SeqLength)\n",
"print \"T: %.1f\" % (100 * NumberT / SeqLength)\n",
" \n",
"#\n",
"# End of Chapter 8\n",
" \n",
"# Beginning of Chapter 9\n",
"#\n",
"# Calculating primer melting points with different formulas by length\n",
" \n",
"TotalStrong = NumberG + NumberC\n",
"TotalWeak = NumberA + NumberT\n",
" \n",
"if SeqLength >= 14:\n",
" #formula for sequences > 14 nucleotides long \n",
"\tMeltTempLong = 64.9 + 41 * (TotalStrong - 16.4) / SeqLength \n",
"\tprint \"Tm Long (>14): %.1f C\" % (MeltTempLong)\n",
"else:\n",
"\t#formula for sequences less than 14 nucleotides long\n",
"\tMeltTemp = (4 * TotalStrong) + (2 * TotalWeak)\n",
"\tprint \"Tm Short: %.1f C\" % (MeltTemp)\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sequence: ATGTCTCATTCAAAGCA\n",
"Sequence Length: 17.0\n",
"A: 35.3\n",
"C: 23.5\n",
"G: 11.8\n",
"T: 29.4\n",
"Tm Long (>14): 39.8 C\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**See the code in action and explore it interactively [here](https://www.pythonanywhere.com/gists/6153147/dnacalc1.py/ipython2).** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Obtain a copy of this entire IPython Notebook [here](https://gist.github.com/fomightez/6181093) in order to explore it interactively.** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"\n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"<br/>\n",
"Additional aid and exploration below:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%whos"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Variable Type Data/Info\n",
"---------------------------------\n",
"DNASeq str ATGTCTCATTCAAAGCA\n",
"MeltTempLong float 39.8176470588\n",
"NumberA int 6\n",
"NumberC int 4\n",
"NumberG int 2\n",
"NumberT int 5\n",
"SeqLength float 17.0\n",
"TotalStrong int 6\n",
"TotalWeak int 11\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above special command lets us see what is defined and can be used in an IPython Notebook. \n",
"(For some reason it doesn't work for any of the initiating variables over in the interactive gist console.)"
]
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"See the subsequent variations that build upon these ideas and make more versatile versions as described in the book. \n",
" \n",
" "
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment