'this is a string'
'this is a modified string'
'my'
. 'm'
in the alphabet. For the next several exercises, please download the following files and save them in the same folder as this notebook.
http://bioinfo.umassmed.edu/bootstrappers/bootstrappers-courses/python1/Python_I/yeast/Saccharomyces_cerevisiae.R64-1-1.78_transcripts.bed
http://bioinfo.umassmed.edu/bootstrappers/bootstrappers-courses/python1/Python_I/yeast/Saccharomyces_cerevisiae.R64-1-1.78_sample.gtf
http://bioinfo.umassmed.edu/bootstrappers/bootstrappers-courses/python1/Python_I/yeast/sacCer3.genome
http://bioinfo.umassmed.edu/bootstrappers/bootstrappers-courses/python1/Python_I/yeast/README.txt
x = open('sacCer3.genome') # x is now a 'file object' variable
print(x)
y=x.readline()
print(y)
print(x.readline())
print(x.readline())
print(x.readlines()) # note this is readlines and not readline
print(x.readline())
x.seek(0)
print(x.readline())
x.seek(8)
print(x.readline())
print(x.readline())
In [7]:
In [ ]:
{python}
x = open('sacCer3.genome')
for line in x:
print('the current line of the file is: ' + line)
Notice that when you put a file handle as the object being iterated through, Python essentially executes the .readline()
method of file objects again and again, and stores the result into the loop’s user-defined variable until the file runs out of lines. Alternatively, try this implementation to avoid making a file object variable at all:
{python}
for line in open('sacCer3.genome'):
print('the current line is: ' + line)
In [ ]:
In [0]:
In [0]:
{python}
output_file=open('test_output.txt', 'w')
for letter in 'ACCGT':
output_file.write(letter)
output_file.close()
In [7]:
{python}
x = open('Saccharomyces_cerevisiae.R64-1-1.78_sample.gtf')
y = open('test_output2.txt', 'w')
for line in x:
y.write(line)
y.close()
Afterward, you’ll want to open 'test_output2.txt' to see if it looks the same as 'Saccharomyces_cerevisiae.R64-1-1.78_sample.gtf'
In [ ]:
In [7]:
In [7]:
In [7]:
In [7]:
import
and genomic data (30 minutes){python}
import ucscgenome
genome = ucscgenome.Genome("sacCer3")
sequence = genome["chrIV"]
print(sequence[100:110])
In [1]:
import ucscgenome
genome = ucscgenome.Genome("sacCer3")
sequence = genome["chrIV"]
print(sequence[100:110])
In [ ]:
In [9]:
The following code makes it possible to translate one set of characters into another set of character in a string. How could you apply this to obtain a reverse complement sequence from a forward sequence?
{python}
import string
t = string.maketrans("aei", "qwe") # this create a 'translate string' `t`
print('this needs to be translated'.translate(t))
In [2]:
# example
import string
t = string.maketrans("aei", "qwe") # this create a 'translate string' `t`
print('this needs to be translated'.translate(t))
In [ ]:
In [ ]: