Commit 925f3c5e authored by Blaise Li's avatar Blaise Li
Browse files

Update IO exercises and rebase.py solution.

parent 9148e2b7
Pipeline #10486 passed with stages
in 22 seconds
......@@ -13,29 +13,35 @@ Exercises
Exercise
--------
Write a function which take the path of file as parameter
Write a function that takes the path of file as parameter
and display it's content on the screen.
We wait same behavior as the shell *cat* command. ::
We expect the same behavior as the shell ``cat`` command.
::
import sys
import os
def cat(path):
if not os.path.exists(path):
sys.exit("no such file: {0}".format(path)
# Exit Python with a non-zero value
# to signify a failure
sys.exit("no such file: {0}".format(path))
with open(path, 'r') as infile:
for line in infile:
print(line)
# By default, print adds a "\n" to what it prints
# lines from a file already end with "\n".
print(line, end="")
Exercise
--------
Write a function which take the path of a file in rebase format
and return in a dictionary the collection of the enzyme contains in the file.
Write a function that takes the path of a file in rebase format
and returns in a dictionary the collection of the enzyme contained in the file.
The sequence of the binding site must be cleaned up.
use the file :download:`rebase_light.txt <_static/data/rebase_light.txt>` to test your code.
Use the file :download:`rebase_light.txt <_static/data/rebase_light.txt>` to test your code.
.. literalinclude:: _static/code/rebase.py
:linenos:
......@@ -46,11 +52,11 @@ use the file :download:`rebase_light.txt <_static/data/rebase_light.txt>` to tes
Exercise
--------
write a function which take the path of a fasta file (containing only one sequence)
and return a data structure of your choice that allow to stock
Write a function that takes the path of a fasta file
and returns a data structure of your choice that allows to store
the id of the sequence and the sequence itself.
use the file :download:`seq.fasta <_static/data/seq.fasta>` to test your code.
Use the file :download:`seq.fasta <_static/data/seq.fasta>` to test your code.
.. literalinclude:: _static/code/fasta_reader.py
:linenos:
......
#!/usr/bin/env python3
def rebase_parser(rebase_file):
"""
:param rebase_file: the rebase file to parse
:type rebase_file: file object
:return: at each call return a tuple (str enz name, str binding site)
:return: at each call yields a tuple (str enz name, str binding site)
:rtype: iterator
"""
def clean_seq(seq):
......@@ -15,25 +16,38 @@ def rebase_parser(rebase_file):
if char in 'ACGT':
clean_seq += char
return clean_seq
for line in rebase_file:
fields = line.split()
#fields = fields.split()
name = fields[0]
seq = clean_seq(fields[2])
yield (name, seq)
def rebase2dict(rebase_path):
"""
:param rebase_path: the path to rebase file to parse
:type rebase_path: str
:return: a dict with items (str enz name, str binding site)
"""
with open(rebase_path, 'r') as rebase_input:
# enz_dict = {}
# for (name, seq) in rebase_parser(rebase_input):
# enz_dict[name] = seq
enz_dict = dict(rebase_parser(rebase_input))
return enz_dict
if __name__ == '__main__':
import sys
import os.path
if len(sys.argv) != 2:
sys.exit("usage multiple_fasta fasta_path")
sys.exit("Usage: rebase.py rebase_file")
rebase_path = sys.argv[1]
if not os.path.exists(rebase_path):
sys.exit("No such file: {}".format(rebase_path))
with open(rebase_path, 'r') as rebase_input:
for enz in rebase_parser(rebase_input):
print enz
\ No newline at end of file
enz_dict = rebase2dict(rebase_path)
print(enz_dict)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment