Home

Functions

In Chapter 13 of Hello World we come to functions, which provide a way to re-use code.


GenBank and FASTA

FASTA format has a one-line header, followed by the sequence, in the following format.

MNENLFASFIAPTILGLPAAVLIILFPPLLIPTSKYLINNRLITTQQWLIKLTSKQMMAM
HNTKGRTWSLMLVSLIIFITTTNLLGLLPHSFTPTTQLSMNLAMAIPLWAGAVIMGFRSK
IKNALAHFLPQGTPTPLIPMLVIIETISLLIQPVALAVRLTANITAGHLLMHLIGNATLA
MSTINLPSTLIIFTILILLTILEIAVALIQAYVFTLLVSLYLHDNT

GenBank format has a longer header, followed by the keyword

ORIGIN

followed by the sequence as follows

        1 mnenlfasfi aptilglpaa vliilfppll iptskylinn rlittqqwli kltskqmmam
       61 hntkgrtwsl mlvsliifit ttnllgllph sftpttqlsm nlamaiplwa gavimgfrsk
      121 iknalahflp qgtptplipm lviietisll iqpvalavrl tanitaghll mhlignatla
      181 mstinlpstl iiftilillt ileiavaliq ayvftllvsl ylhdnt

followed by

//

to terminate.

Warmup problems

Write two functions: one should read a GenBank file, and write out the sequence in FASTA format; the other should read a FASTA file, and write out the sequence in GenBank format. You can put dummy headers in your output.

Submit the program


Recursive Functions

This topic is optional.

In mathematics, recursive means that a thing is somehow defined in terms of itself. In programming, recursive means that a function calls itself, or two or more functions call each other. Some people find recursive functions irresistible, and if you like the story of the Little Harmonic Labyrinth, you will, too.

The factorial function is commonly given as an example of recursion.

def fac(n):
    if n==0:
        return 1
    return n*fac(n-1)

For a more interesting example, here is another version of our file-copy example, using recursion to create a loop.

def recurse(i,o):
    s = i.readline()
    if not s:
        return
    o.write(s)
    recurse(i,o)

inp = open('original.txt')
out = open('copy.txt','w')
recurse(inp,out)
inp.close()
out.close()

Notice that here the last line of the function is a simple call to itself. This is known as tail recursion, and is particularly important.

Fibonacci Numbers

The Fibonacci sequence may be the oldest example of a population-biology problem. Fibonacci stated it as a problem of counting rabbits. You started with one pair of baby rabbits. They would grow for a month, and then produce a pair of offspring each month. Likewise with each new pair of rabbits. Here fib(n) is the number of rabbit pairs after n months.

def fib(n):
    if n<2:
        return 1
    return fib(n-1) + fib(n-2)

The above is not a very good definition, though, because it needlessly makes two recursive calls. Try fib(30) or higher and you will notice the time increasing exponentially.

Improve fib(n) so that it makes only one recursive call. The trick is to return a tuple with the last two Fibonacci numbers.

Submit the program