Sequences and Strings - part II
Section 1 December 14th, 2006Another important task for many biologists is to merge/concatenate different strings of DNA in one unique sequence. We can modify the previous script to concatenate two distinct DNA sequences in one.
We start using code_01 structure, adding some extra elements (line 3):
#! /usr/bin/env python myDNA = "ACGTACGTACGTACGTACGTACGT" myDNA2 = "TCGATCGATCGATCGATCGA" print myDNA, myDNA2
So far, we added a new string containing an extra DNA sequence and we print both sequences. In Python print statement automatically adds a new line at the end of the string to be printed, unless you add a comma (,) to the end. The comma is also needed if you are going to print more than one string in order to separate them (try removing the comma from the code above).
Now, how do we merge myDNA and myDNA2? Easy in Python: just sum them with a plus signal:
myDNA3 = myDNA + myDNA2 print myDNA3
Notice that in Python strings are immutable, meaning they cannot be changed. This immutability confers some advantages to the code where strings (in Python strings are not variables) cannot be modified anywhere in the program and also allowing some performance gain in the interpreter. So, in order to have our sequences merged we create a third sequence that receives both strings. Finally our code will be (some captions were added):
#! /usr/bin/env python myDNA = "ACGTACGTACGTACGTACGTACGT" myDNA2 = "TCGATCGATCGATCGATCGA" print "First and Second sequences" print myDNA, myDNA2 myDNA3 = myDNA + myDNA2 print "Concatenated sequence" print myDNA3
Easy, eh? Of course these two simple scripts do no scratch the surface of Python programming, but they are a start.
The above code can be downloaded from the repository.
August 7th, 2007 at 12:18 am
I am teacher in VIT University India, I am handling python lab for bioinformatics students.
October 11th, 2007 at 1:06 pm
Paolo:
For some reason, the line which was supposed to appear in boldface looks like this on my vanilla system (Compaq laptop computer, Windows XL, Firefox):
myDNA2 = “TCGATCGATCGATCGATCGA”
I’m guessing it’s got something to do with the HTML source code in which the page was written.
But since I know nothing about HTML except what the letters stand for, I could be wrong.
Anyway, the teaching points are clear: that strings are immutable, and how to concatenate them and print them.
Joe Oettinger
October 11th, 2007 at 1:24 pm
Whoops! The line printed in boldface in my comment!
So the fact that it didn’t print in boldface in the tutorial must mean that some code before or after it prevents that.
Maybe this will show how the line appears on my screen (I put a dash inside the symbols).
If this prints in boldface, then I don’t know how to show how the line appears on my screen:
myDNA2 = “TCGATCGATCGATCGATCGA”
I wish I hadn’t even brought it up.
Joe
October 11th, 2007 at 2:15 pm
Hi Joe
There was an error on the first code. The strong tags appearing, shouldn’t be there. It was probably an error that stayed when I change the code highlighting feature. Those strong tags doesn’t have anything to do with Python and should be omitted.
Sorry for that.
Cheers
Paulo