# NSCI 580A5 fall 2017

### Sidebar

NSCI 580A5

Instructors
Tai Montgomery
Asa Ben-Hur

assignments:assignment3

## ASSIGNMENT 3

Due date: 10/10/17 at 10am

#### Exercise 1: Fibonacci numbers

Write a function called fibonacci(n) that returns the nth Fibonacci number. Recall that Fibonacci numbers are defined as the sequence

1, 1, 3, 5, 8, 13,…

where each number in the sequence is the sum of the previous two.

#### Exercise 2: substring counting

Write a function called substring_count(string, substring) that returns the number of times substring occurs within string. For example the string 'AA' occurs 3 times in the string 'GAATAAATGC'. If the substring does not occur, the return value should be 0.

#### Exercise 3: GC content

Write a function called gc_content(sequence) that computes the GC content of a DNA sequence, i.e. the fraction of G or C nucleotides in the sequence. Your function should work regardless of the case in which the sequence is provided (upper/lower case). Note that a DNA sequence can contain positions that are ambiguous. These are represented by ambiguity codes. For example, 'N' denotes that any nucleotide is possible in that position. In computing GC content ignore positions that contain ambiguous nucleotides. For example, for the sequence

TTACTNGGAGNT

An easy way to do this is to create a for loop in the form:

for nucleotide in sequence :
# do stuff

#### Exercise 4: Reverse complement

Write a function called reverse_complement(sequence) that receives as input a DNA sequence and computes its reverse complement. For example, the reverse complement of AGTCATG is CATGACT. In computing the reverse complement assume that any character that is not A,C,G, or T is its own complement.

Make sure your code works correctly on different inputs, and that all possible types of input are handled correctly (e.g. non-ACGT characters in a DNA sequence).

### Submission

Submit a single module named assignment3.py via Canvas by the due date. In writing your code use the following template. The “main” segment of the module should be used to test each of the functions. Your code will be tested with a Python script that looks something like the following:

assignment3test.py

import assignment3

def test_fibonacci() :
print('testing fibonacci')
n = 7
correct_value = 13
program_value = assignment3.fibonacci(n)
if correct_value == program_value :
print ('code runs correctly on ', n)
else :
print ('code incorrect on: ', n, 'correct value is: ', correct_value, 'computed value is: ', program_value)

def test_substring_count() :
print('testing gc_contet')
string = 'GAATAAATGC'
substring = 'AA'
correct_value = 3
program_value = assignment3.substring_count(string, substring)
if correct_value == program_value :
print ('code runs correctly on ', string, substring)
else :
print ('code incorrect on: ', string, substring, 'correct value is: ', correct_value, 'computed value is: ', program_value)

def test_gc_content() :
print('testing gc_contet')
sequence = 'TTACTNGGAGNT'
correct_value = 0.4
program_value = assignment3.gc_content(sequence)
if correct_value == program_value :
print ('code runs correctly on ', sequence)
else :
print ('code incorrect on: ', sequence, 'correct value is: ', correct_value, 'computed value is: ', program_value)

def test_reverse_complement() :
print('testing reverse_complement')
sequence = 'AGTCATG'
correct_value = 'CATGACT'
program_value = assignment3.reverse_complement(sequence)
if correct_value == program_value :
print ('code runs correctly on ', sequence)
else :
print ('code incorrect on: ', sequence, 'correct value is: ', correct_value, 'computed value is: ', program_value)

if __name__=='__main__' :
test_fibonacci()
test_substring_count()
test_gc_content()
test_reverse_complement()