<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 An introduction to solving biological problems with Python
Session 1.3
Step1:  You can of course use variables in tuples and other data structures
Step2:  Tuples can be packed and unpacked with a convenient syntax. The number of variables used to unpack the tuple must match the number of elements in the tuple.
Step3:  Lists
Can contain any number of items
Can contain different types of items
Can be altered once created (they are mutable)
Items have a particular order
Lists are created with square brackets around their items
Step4:  Lists and tuples can contain other list and tuples, or any other type of collection
Step5:  You can convert between tuples and lists with the <tt>tuple</tt> and <tt>list</tt> functions. Note that these create a new collection with the same items, and leave the original unaffected.
Step6:  Manipulating tuples and lists
Once your data is in a list or tuple, python supports a number of ways you can access elements of the list and manipulate the list in useful ways, such as sorting the data.
Tuples and lists can generally be used in very similar ways.
Index access
You can access individual elements of the collection using their index, note that the first element is at index 0. Negative indices count backwards from the end.
Step7:  Slices
You can also access a range of items, known as slices, from inside lists and tuples using a colon 
Step8:  in operator
You can check if a value is in a tuple or list with the <tt>in</tt> operator, and you can negate this with <tt>not</tt>
Step9:  len() and count() functions
You can get the length of a list or tuple with the in-built <tt>len()</tt> function, and you can count the number of particular elements contained in a list with the <tt>.count()</tt> function.
Step10:  Modifying lists
You can alter lists in place, but not tuples
Step11:  Tuples cannot be altered once they have been created, if you try to do so, you'll get an error.
Step12:  You can add elements to the end of a list with <tt>append()</tt>
Step13:  or insert values at a certain position with <tt>insert()</tt>, by supplying the desired position as well as the new value
Step14:  You can remove values with <tt>remove()</tt>
Step15:  and delete values by index with <tt>del</tt>
Step16:  It's often useful to be able to combine arrays together, which can be done with <tt>extend()</tt> (as <tt>append</tt> would add the whole list as a single element in the list)
Step17:  The plus symbol <tt>+</tt> is shorthand for the extend operation when applied to lists
Step18:  Slice syntax can be used on the left hand side of an assignment operation to assign subregions of a list
Step19:  You can change the order of elements in a list
Step20:  Note that both of these change the list, if you want a sorted copy of the list while leaving the original untouched, use <tt>sorted()</tt>
Step21:  Getting help from the official Python documentation
The most useful information is online on https
Step22:  Exercise 1.3.1
Create a list of DNA codons for the protein sequence CLYSY based on the codon variables you defined previously.
Print the DNA sequence of the protein to the screen.
Print the DNA codon of the last amino acid in the protein sequence.
Create two more variables containing the DNA sequence of a stop codon and a start codon, and replace the first element of the DNA sequence with the start codon and append the stop codon to the end of the DNA sequence. Print out the resulting DNA sequence.
String manipulations
Strings are a lot like tuples of characters, and individual characters and substrings can be accessed and manipulated using similar operations we introduced above.
Step23:  Just as with tuples, trying to assign a value to an element of a string results in an error
Step24:  Python provides a number of useful functions that let you manipulate strings
The <tt>in</tt> operator lets you check if a substring is contained within a larger string, but it does not tell you where the substring is located. This is often useful to know and python provides the <tt>.find()</tt> method which returns the index of the first occurrence of the search string, and the <tt>.rfind()</tt> method to start searching from the end of the string.
If the search string is not found in the string both these methods return -1.
Step25:  When we are reading text from files  (which we will see later on), often there is unwanted whitespace at the start or end of the string. We can remove leading whitespace with the <tt>.lstrip()</tt> method, trailing whitespace with <tt>.rstrip()</tt>, and whitespace from both ends with <tt>.strip()</tt>.
All of these methods return a copy of the changed string, so if you want to replace the original you can assign the result of the method call to the original variable.
Step26:  You can split a string into a list of substrings using the <tt>.split()</tt> method, supplying the delimiter as an argument to the method. If you don't supply any delimiter the method will split the string on whitespace by default (which is very often what you want!)
To split a string into its component characters you can simply cast the string to a list
Step27:  <tt>.split()</tt> is the counterpart to the <tt>.join()</tt> method that lets you join the elements of a list into a string only if all the elements are of type String
Step28:  We also saw earlier that the <tt>+</tt> operator lets you concatenate strings together into a larger string.
Note that this operator only works on variables of the same type. If you want to concatenate a string with an integer (or some other type), first you have to cast the integer to a string with the <tt>str()</tt> function.
Step29:  To get more information about these two methods split() and join() we could find it online in the Python documentation starting from www.python.org or get help using the help() builtin function.
<ASSISTANT_TASK:>
Python Code:
a = (123, 54, 92) # tuple of 4 integers
b = () # empty tuple
c = ("Ala",) # tuple of a single string (note the trailing ",")
d = (2, 3, False, "Arg", None) # a tuple of mixed types
print(a)
print(b)
print(c)
print(d)
Explanation: An introduction to solving biological problems with Python
Session 1.3: Collections Lists and Strings
Tuples, Lists and Manipulating tuples and lists | Exercise 1.3.1
String manipulations | Exercise 1.3.2
As well as the basic data types we introduced above, very commonly you will want to store and operate on collections of values, and python has several data structures that you can use to do this. The general idea is that you can place several items into a single collection and then refer to that collection as a whole. Which one you will use will depend on what problem you are trying to solve.
Tuples
Can contain any number of items
Can contain different types of items
Cannot be altered once created (they are immutable)
Items have a defined order
A tuple is created by using round brackets around the items it contains, with commas seperating the individual elements.
End of explanation
x = 1.2
y = -0.3
z = 0.9
t = (x, y, z)
print(t)
Explanation: You can of course use variables in tuples and other data structures
End of explanation
t = 2, 3, 4 # tuple packing
print('t is', t)
x, y, z = t # tuple unpacking
print('x is', x)
print('y is', y)
print('z is', z)
Explanation: Tuples can be packed and unpacked with a convenient syntax. The number of variables used to unpack the tuple must match the number of elements in the tuple.
End of explanation
a = [1, 3, 9]
b = ["ATG"]
c = []
print(a)
print(b)
print(c)
Explanation: Lists
Can contain any number of items
Can contain different types of items
Can be altered once created (they are mutable)
Items have a particular order
Lists are created with square brackets around their items:
End of explanation
matrix = [[1, 0], [0, 2]]
print(matrix)
Explanation: Lists and tuples can contain other list and tuples, or any other type of collection:
End of explanation
a = (1, 4, 9, 16)     # A tuple of numbers
b = ['G','C','A','T'] # A list of characters
print(a)
print(b)
l = list(a)   # Make a list based on a tuple 
print(l)
t = tuple(b)  # Make a tuple based on a list
print(t)
Explanation: You can convert between tuples and lists with the <tt>tuple</tt> and <tt>list</tt> functions. Note that these create a new collection with the same items, and leave the original unaffected.
End of explanation
t = (123, 54, 92, 87, 33)
x = [123, 54, 92, 87, 33]
print('t is', t)
print('t[0] is', t[0])
print('t[2] is', t[2])
print('x is', x)
print('x[-1] is', x[-1])
Explanation: Manipulating tuples and lists
Once your data is in a list or tuple, python supports a number of ways you can access elements of the list and manipulate the list in useful ways, such as sorting the data.
Tuples and lists can generally be used in very similar ways.
Index access
You can access individual elements of the collection using their index, note that the first element is at index 0. Negative indices count backwards from the end.
End of explanation
t = (123, 54, 92, 87, 33)
x = [123, 54, 92, 87, 33]
print('t[1:3] is', t[1:3])
print('x[2:] is', x[2:])
print('x[:-1] is', x[:-1])
Explanation: Slices
You can also access a range of items, known as slices, from inside lists and tuples using a colon : to indicate the beginning and end of the slice inside the square brackets. Note that the slice notation [a:b] includes positions from a up to but not including b.
End of explanation
t = (123, 54, 92, 87, 33)
x = [123, 54, 92, 87, 33]
print('123 in', x, 123 in x)
print('234 in', t, 234 in t)
print('999 not in', x, 999 not in x)
Explanation: in operator
You can check if a value is in a tuple or list with the <tt>in</tt> operator, and you can negate this with <tt>not</tt>
End of explanation
t = (123, 54, 92, 87, 33)
x = [123, 54, 92, 87, 33]
print("length of t is", len(t))
print("number of 33s in x is", x.count(33))
Explanation: len() and count() functions
You can get the length of a list or tuple with the in-built <tt>len()</tt> function, and you can count the number of particular elements contained in a list with the <tt>.count()</tt> function.
End of explanation
x = [123, 54, 92, 87, 33]
print(x)
x[2] = 33
print(x)
Explanation: Modifying lists
You can alter lists in place, but not tuples
End of explanation
t = (123, 54, 92, 87, 33)
print(t)
t[1] = 4
Explanation: Tuples cannot be altered once they have been created, if you try to do so, you'll get an error.
End of explanation
x = [123, 54, 92, 87, 33]
x.append(101)
print(x)
Explanation: You can add elements to the end of a list with <tt>append()</tt>
End of explanation
x = [123, 54, 92, 87, 33]
x.insert(3, 1111)
print(x)
Explanation: or insert values at a certain position with <tt>insert()</tt>, by supplying the desired position as well as the new value
End of explanation
x = [123, 54, 92, 87, 33]
x.remove(123)
print(x)
Explanation: You can remove values with <tt>remove()</tt>
End of explanation
x = [123, 54, 92, 87, 33]
print(x)
del x[0]
print(x)
Explanation: and delete values by index with <tt>del</tt>
End of explanation
a = [1,2,3]
b = [4,5,6]
a.extend(b)
print(a)
a.append(b)
print(a)
Explanation: It's often useful to be able to combine arrays together, which can be done with <tt>extend()</tt> (as <tt>append</tt> would add the whole list as a single element in the list)
End of explanation
a = [1, 2, 3]
b = [4, 5, 6]
a = a + b
print(a)
Explanation: The plus symbol <tt>+</tt> is shorthand for the extend operation when applied to lists:
End of explanation
a = [1, 2, 3, 4, 5, 6]
a[1:3] = [9, 9, 9, 9]
print(a)
Explanation: Slice syntax can be used on the left hand side of an assignment operation to assign subregions of a list
End of explanation
a = [1, 3, 5, 4, 2]
a.reverse()
print(a)
a.sort()
print(a)
Explanation: You can change the order of elements in a list
End of explanation
a = [2, 5, 7, 1]
b = sorted(a)
print(a)
print(b)
Explanation: Note that both of these change the list, if you want a sorted copy of the list while leaving the original untouched, use <tt>sorted()</tt>
End of explanation
help(len)
help(list)
help(list.insert)
help(list.count)
Explanation: Getting help from the official Python documentation
The most useful information is online on https://www.python.org/ website and should be used as a reference guide.
Python 3.5.2 documentation is the starting page with links to tutorials and libraries' documentation for Python 3
The Python Tutorial
The Python Standard Library Reference is the documentation of all libraries included within Python as well as built-in functions and data types like:
Text Sequence Type — str
Numeric Types — int, float
Sequence Types — list, tuple
Set Types — set
Mapping Types — dict
Getting help directly from within Python using help()
End of explanation
text = "ATGTCATTTGT"
print(text[0])
print(text[-2])
print(text[0:6])
print("ATG" in text)
print("TGA" in text)
print(len(text))
Explanation: Exercise 1.3.1
Create a list of DNA codons for the protein sequence CLYSY based on the codon variables you defined previously.
Print the DNA sequence of the protein to the screen.
Print the DNA codon of the last amino acid in the protein sequence.
Create two more variables containing the DNA sequence of a stop codon and a start codon, and replace the first element of the DNA sequence with the start codon and append the stop codon to the end of the DNA sequence. Print out the resulting DNA sequence.
String manipulations
Strings are a lot like tuples of characters, and individual characters and substrings can be accessed and manipulated using similar operations we introduced above.
End of explanation
text = "ATGTCATTTGT"
text[0:2] = "CCC" 
Explanation: Just as with tuples, trying to assign a value to an element of a string results in an error
End of explanation
dna = "ATGTCACCGTTT"
index = dna.find("TCA")
print("TCA is at position:", index)
index = dna.rfind('C')
print("The last Cytosine is at position:", index)
print("Position of a stop codon:", dna.find("TGA"))
Explanation: Python provides a number of useful functions that let you manipulate strings
The <tt>in</tt> operator lets you check if a substring is contained within a larger string, but it does not tell you where the substring is located. This is often useful to know and python provides the <tt>.find()</tt> method which returns the index of the first occurrence of the search string, and the <tt>.rfind()</tt> method to start searching from the end of the string.
If the search string is not found in the string both these methods return -1.
End of explanation
s = "    Chromosome Start End                     "
print(len(s), s)
s = s.lstrip()
print(len(s), s)
s = s.rstrip()
print(len(s), s)
s = "    Chromosome Start End                     "
s = s.strip()
print(len(s), s)
Explanation: When we are reading text from files  (which we will see later on), often there is unwanted whitespace at the start or end of the string. We can remove leading whitespace with the <tt>.lstrip()</tt> method, trailing whitespace with <tt>.rstrip()</tt>, and whitespace from both ends with <tt>.strip()</tt>.
All of these methods return a copy of the changed string, so if you want to replace the original you can assign the result of the method call to the original variable.
End of explanation
seq = "ATG TCA CCG GGC"
codons = seq.split(" ")
print(codons)
bases = list(seq) # a tuple of character converted into a list
print(bases)
Explanation: You can split a string into a list of substrings using the <tt>.split()</tt> method, supplying the delimiter as an argument to the method. If you don't supply any delimiter the method will split the string on whitespace by default (which is very often what you want!)
To split a string into its component characters you can simply cast the string to a list
End of explanation
seq = "ATG TCA CCG GGC"
codons = seq.split(" ")
print(codons)
print("|".join(codons))
Explanation: <tt>.split()</tt> is the counterpart to the <tt>.join()</tt> method that lets you join the elements of a list into a string only if all the elements are of type String:
End of explanation
s = "chr"
chrom_number = 2
print(s + str(chrom_number))
Explanation: We also saw earlier that the <tt>+</tt> operator lets you concatenate strings together into a larger string.
Note that this operator only works on variables of the same type. If you want to concatenate a string with an integer (or some other type), first you have to cast the integer to a string with the <tt>str()</tt> function.
End of explanation
help(str.split)
help(str.join)
Explanation: To get more information about these two methods split() and join() we could find it online in the Python documentation starting from www.python.org or get help using the help() builtin function.
End of explanation
<END_TASK>