Differences

This shows you the differences between two versions of the page.

part3 [2009/05/11 11:44]
nuin created
part3 [2009/05/11 11:50] (current)
nuin
Line 20: Line 20:
-Python subroutines do not exist. [[http://www.diveintopython.org/getting_to_know_python/declaring_functions.html | Everything is a function, all functions return a value (even if it's None), and all functions start with //def//]]. This statement is from Dive into Python, a book on Python programming available for free. As mentioned Python functions start with the word <code>def</code>, which is followed by the function name that is followed by the arguments the function receives in between parentheses. Something like+Python subroutines do not exist. [[http://www.diveintopython.org/getting_to_know_python/declaring_functions.html | Everything is a function, all functions return a value (even if it's None), and all functions start with //def//]]. This statement is from Dive into Python, a book on Python programming available for free. As mentioned Python functions start with the word //def//, which is followed by the function name that is followed by the arguments the function receives in between parentheses. Something like
<code python>def my_first_function(somevalue):</code> <code python>def my_first_function(somevalue):</code>
Line 48: Line 48:
print sequence</code> print sequence</code>
-Not very useful, at first sight, but gives us an impression of what a function looks like. Basically we define a function //add_tail// that receives <code>seq</code> as a parameter. Don't worry about variable scope now, we will see it later. The rest of the script is just like things we saw before, except for the line //sequence = add_tail//(sequence). Here we are saving memory (yep, not that much and not even impressive) by assigning the return value of the function to the same string where we have the sequence stored. Run the scritp and get ready for the command line arguments.+Not very useful, at first sight, but gives us an impression of what a function looks like. Basically we define a function //add_tail// that receives //seq// as a parameter. Don't worry about variable scope now, we will see it later. The rest of the script is just like things we saw before, except for the line //sequence = add_tail//(sequence). Here we are saving memory (yep, not that much and not even impressive) by assigning the return value of the function to the same string where we have the sequence stored. Run the scritp and get ready for the command line arguments. 
 + 
 + 
 +==== Command line arguments and a second take on functions ==== 
 + 
 + 
 +We have seen, briefly, how to define and use a function in Python. Now we are going to jump forward a bit and create a new function and at the same time take a look on command line parameters that can be passed to the script. 
 + 
 +If you have used command line applications before, you might have encountered programs that asks for a file name, a calculation parameter, etc to be passed in the command line. Python scripts are no different, they accept such parameters. For this we have the //sys// module that has system specific parameters and functions. We have used before the //sys.exit//, imported as an extra module function. Every operating system (even Windows) has arguments in its command line, and programming languages usually call such arguments //argv// (in the C/C++ you have argv in the parameters of the main function). Lists in Python start at 0 (zero), and for the argument list the first item is the script/program name. Basically if we have this 
 + 
 +<code>$> python myscript.py DNA.txt</code> 
 + 
 +<code>myscript.py</code> is the argument 0 in the list and DNA.txt is the argument 1. So whenever we create a script that receives arguments in the command line, we have to start (in most cases, be aware) from 1. In Python using system arguments in the CLI will look like 
 + 
 +<code python>sys 
 + 
 +filename = sys.argv[1] 
 +valueone = sys.argv[2] 
 +...</code> 
 + 
 +We will a variation of our previous script that counts the bases, now with command line arguments and a function (with no "error" checking at first) 
 + 
 +<code python>#!/usr/bin/env python 
 + 
 +import sys 
 + 
 +def count_nucleotide_types(seq): 
 +    result = [] 
 +    totalA = seq.count('A') 
 +    totalC = seq.count('C') 
 +    totalG = seq.count('G') 
 +    totalT = seq.count('T') 
 + 
 +    result.append(totalA) 
 +    result.append(totalC) 
 +    result.append(totalG) 
 +    result.append(totalT) 
 + 
 +    return result 
 + 
 +sequencefile = open(sys.argv[1], 'r').readlines() 
 +sequence = ''.join(sequencefile) 
 +sequence = sequence.replace('n', '') 
 +values = count_nucleotide_types(sequence) 
 +print "Found " + str(result[0] + "As" 
 +print "Found " + str(result[0] + "Cs" 
 +print "Found " + str(result[0] + "Gs" 
 +print "Found " + str(result[0] + "Ts"</code> 
 + 
 +Few new things here. We created a function //count_nucleotide_types// that should receive a string containing the sequence. The "real" first line of the program flow is the one that gets the name of the file from the command line argument, open and read it. We then convert the list to a string, modify it a but and throw it to the function. Get the result back, and done. 
 + 
 +With functions we actually don't save coding time/length (at least here), we make out code more organized, easier to read and somewhat easier to someone else read and understand it. It is not a good coding practice to have long programs/scripts with no functions, no subdivision, no structure. Functions are sometimes good program nuggets that can be reused in the same application or even ported/copied to other applications and reused indefinitely. Soon we will see a function and class that reads a FASTA file in Python that can be used anywhere in any program that needs such feature.  
 + 
 + 
 + 
 
part3.txt · Last modified: 2009/05/11 11:50 by nuin
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki