You are on page 1of 3

Lab 1

Running Python
There are several ways to run Python code: 1. from the interpreter:
>>> dna ='atgc' >>> dna atgc

2. from a file: If a file mydna.py contains:


#! /local/bin/python dna ='atgc' print dna

it can be executed from the command line:


localhost~$ python mydna.py atgc

or using the #! notation:


localhost~$./mydna.py atgc

It is also possible to execute files during an interactive interpreter session:


caroline:~> python Python 2.5.2 (#1, May 27 2008, 13:20:02) [GCC 4.2.3 (Debian prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> execfile("mydna") atgc

or to load a file from the command line before entering Python in interactive mode (-i):
localhost~$ python -i mydna.py atgc >>>

this is very convenient when your Python file contains definitions (functions, classes,...) that you want to test interactively. 3. from other programs embedding the Python interpreter:
#include <Python.h> int main(int argc, char** argv) { Py_Initialize(); PyRun_SimpleString("dna = atgagag + tagagga"); PyRun_SimpleString("print Dna is:, dna"); return 0;
}

Exercise 1.1 Write a program to ask the user for a sequence then print its length. Example output:
Enter a sequence: ATTAC

It is 5 bases long. Exercise 1.2 Modify the program so it also prints the number of A, T, C, and G characters in the sequence Example output: Enter a sequence: ATTAC It is 5 bases long adenine: 2 thymine: 2 cytosine: 1 guanine: 0 Exercise 1.3 Modify the program to allow both lower-case and upper-case characters in the sequence Example output: Enter a sequence: ATTgtc It is 6 bases long adenine: 1 thymine: 3 cytosine: 1 guanine: 1

Exercise 1.4: Modify the program to print the number of unknown characters in the sequence Example output: Enter a sequence: ATTU*gtc It is 7 bases long adenine: 1 thymine: 3 cytosine: 1 guanine: 1 unknown: 2

Exercise 1.5: GC content Calculate the GC percent of dna. (Insert an arbitrary DNA sequence at the interpreter, for example : >>> dna=gcatgacgttattacgactctgtcacgccgcggtgcgactgaggcgtggcgtcg

Exercise 1.6: DNA complement Find the DNA complement. Use the replace or maketrans and translate function to accomplish the task. For the information of some of the function, you can type >>> help(replace) For accessing the method, you need to import the string module >>> from string import *
Help on function replace in module string:replace(s, old, new, maxsplit=-1) replace (str, old, new[, maxsplit]) -> string Return a copy of string str with all occurrences of substring old replaced by new. If the optional argument maxsplit is given, only the first maxsplit occurrences are replaced.

You might also like