Skip to content

Commit 4c5a2d9

Browse files
committed
some extras
1 parent c04e176 commit 4c5a2d9

32 files changed

+506
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tex2pdf*
99
.mypy_cache
1010
.log
1111
.coverage
12+
.idea

01_hello/hello04_argparse.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
# Purpose: Say hello
3+
4+
import argparse
5+
6+
parser = argparse.ArgumentParser(description='Say hello')
7+
parser.add_argument('name', help='Whom to greet')
8+
args = parser.parse_args()
9+
name = args.name
10+
print('Hello, ' + name + '!')

02_crowsnest/crowsnest.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Me <[email protected]>
4+
Date : today
5+
Purpose: Rock the Casbah
6+
"""
7+
8+
import argparse
9+
import os
10+
import sys
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""Get command-line arguments"""
16+
17+
parser = argparse.ArgumentParser(
18+
description='Rock the Casbah',
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
20+
21+
parser.add_argument('positional',
22+
metavar='str',
23+
help='A positional argument')
24+
25+
parser.add_argument('-a',
26+
'--arg',
27+
help='A named string argument',
28+
metavar='str',
29+
type=str,
30+
default='')
31+
32+
parser.add_argument('-i',
33+
'--int',
34+
help='A named integer argument',
35+
metavar='int',
36+
type=int,
37+
default=0)
38+
39+
parser.add_argument('-f',
40+
'--file',
41+
help='A readable file',
42+
metavar='FILE',
43+
type=argparse.FileType('r'),
44+
default=None)
45+
46+
parser.add_argument('-o',
47+
'--on',
48+
help='A boolean flag',
49+
action='store_true')
50+
51+
return parser.parse_args()
52+
53+
54+
# --------------------------------------------------
55+
def main():
56+
"""Make a jazz noise here"""
57+
58+
args = get_args()
59+
str_arg = args.arg
60+
int_arg = args.int
61+
file_arg = args.file
62+
flag_arg = args.on
63+
pos_arg = args.positional
64+
65+
print('str_arg = "{}"'.format(str_arg))
66+
print('int_arg = "{}"'.format(int_arg))
67+
print('file_arg = "{}"'.format(file_arg.name))
68+
print('flag_arg = "{}"'.format(flag_arg))
69+
print('positional = "{}"'.format(pos_arg))
70+
71+
72+
# --------------------------------------------------
73+
if __name__ == '__main__':
74+
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

extra/07_proteins/Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.PHONY: test pdf clean
2+
3+
pdf:
4+
asciidoctor-pdf README.adoc
5+
6+
test:
7+
pytest -xv test.py
8+
9+
clean:
10+
rm -rf __pycache__

extra/07_proteins/README.adoc

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
= Python Dictionaries: Translate DNA/RNA to AA
2+
3+
Write a Python program called `translate.py` that translates a given DNA/RNA sequence to amino acids.
4+
The input sequence is expected to be a positional argument.
5+
The codon table is expected for the `-c` or `--codons` option and should be a valid, readable file.
6+
The `-o` or `--output` option (default `'out.txt'`) will name a file to write the output.
7+
8+
When run with no inputs, the program should print a brief usage:
9+
10+
----
11+
$ ./translate.py
12+
usage: translate.py [-h] -c FILE [-o FILE] str
13+
translate.py: error: the following arguments are required: str, -c/--codons
14+
----
15+
16+
Or a longer usage for the `-h` or `--help` flags:
17+
18+
----
19+
$ ./translate.py -h
20+
usage: translate.py [-h] -c FILE [-o FILE] str
21+
22+
Translate DNA/RNA to proteins
23+
24+
positional arguments:
25+
str DNA/RNA sequence
26+
27+
optional arguments:
28+
-h, --help show this help message and exit
29+
-c FILE, --codons FILE
30+
A file with codon translations (default: None)
31+
-o FILE, --outfile FILE
32+
Output filename (default: out.txt)
33+
----
34+
35+
The program should reject a `--codons` argument that fails to name a file:
36+
37+
----
38+
$ ./translate.py -c lkdflk ACTG
39+
usage: translate.py [-h] -c FILE [-o FILE] str
40+
translate.py: error: argument -c/--codons: can't open 'lkdflk': \
41+
[Errno 2] No such file or directory: 'lkdflk'
42+
----
43+
44+
If given good input, write the results to the proper output file.
45+
Note here that the input string is lowercase:
46+
47+
----
48+
$ ./translate.py -c codons.dna gaactacaccgttctcctggt
49+
Output written to "out.txt".
50+
----
51+
52+
And now the `out.txt` file should contain the following:
53+
54+
----
55+
$ cat out.txt
56+
ELHRSPG
57+
----
58+
59+
Note that the `-o` flag should make the program write the output to a different file.
60+
Note here that the input sequence is uppercase:
61+
62+
----
63+
$ ./translate.py UGGCCAUGGCGCCCAGAACUG --codons codons.rna -o foo.txt
64+
Output written to "foo.txt".
65+
----
66+
67+
Your program will be given the wrong codon table for a given sequence type, e.g., a DNA table for an RNA sequence:
68+
69+
----
70+
$ ./translate.py UGGCCAUGGCGCCCAGAACUG --codons codons.dna
71+
Output written to "out.txt".
72+
----
73+
74+
This means that your program will be unable to find some of the codons in which case it should print a `-` for a missing codon:
75+
76+
----
77+
$ cat out.txt
78+
-P-RPE-
79+
----
80+
81+
If you are creating a dictionary from the codon table, e.g.:
82+
83+
----
84+
$ head -3 codons.rna
85+
AAA K
86+
AAC N
87+
AAG K
88+
----
89+
90+
Such that you have something like this:
91+
92+
----
93+
>>> codons = dict(AAA='K', AAC='N', AAG='K')
94+
----
95+
96+
Everything is fine as long as you ask for codons that are defined but will fail at runtime if you ask for a codon that does not exist:
97+
98+
----
99+
>>> codons['AAC']
100+
'N'
101+
>>> codons['AAT']
102+
Traceback (most recent call last):
103+
File "<stdin>", line 1, in <module>
104+
KeyError: 'AAT'
105+
----
106+
107+
What dictionary access method will prevent you from creating an exception if you ask for a key that doesn't exist and will also allow you to set a default value?
108+
109+
== Extracting k-mers
110+
111+
A k-mer is a `k`-length sequence of contigous characters in a string (a "mer" like "polymer").
112+
Here is a piece of code that you can use to extract all the 3-mers from the sequence `seq`:
113+
114+
----
115+
>>> seq = 'actga'
116+
>>> k = 3
117+
>>> for codon in [seq[i:i + k] for i in range(0, len(seq) - k + 1)]:
118+
... print(codon)
119+
...
120+
act
121+
ctg
122+
tga
123+
----
124+
125+
You will then use the `codon` as a key to lookup the amino acid translation from the `codons` dictionary you build using the input file.
126+
127+
== Tests
128+
129+
A passing test suite looks like this:
130+
131+
----
132+
$ make test
133+
pytest -xv test.py
134+
============================= test session starts ==============================
135+
...
136+
collected 10 items
137+
138+
test.py::test_exists PASSED [ 10%]
139+
test.py::test_usage PASSED [ 20%]
140+
test.py::test_no_args PASSED [ 30%]
141+
test.py::test_missing_input PASSED [ 40%]
142+
test.py::test_missing_codons PASSED [ 50%]
143+
test.py::test_bad_codon_file PASSED [ 60%]
144+
test.py::test_good_input1 PASSED [ 70%]
145+
test.py::test_good_input2 PASSED [ 80%]
146+
test.py::test_good_input3 PASSED [ 90%]
147+
test.py::test_good_input4 PASSED [100%]
148+
149+
============================== 10 passed in 0.50s ==============================
150+
----

extra/07_proteins/README.pdf

64.4 KB
Binary file not shown.

extra/07_proteins/codons.dna

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
AAA K
2+
AAC N
3+
AAG K
4+
AAT N
5+
ACA T
6+
ACC T
7+
ACG T
8+
ACT T
9+
AGA R
10+
AGC S
11+
AGG R
12+
AGT S
13+
ATA I
14+
ATC I
15+
ATG M
16+
ATT I
17+
CAA Q
18+
CAC H
19+
CAG Q
20+
CAT H
21+
CCA P
22+
CCC P
23+
CCG P
24+
CCT P
25+
CGA R
26+
CGC R
27+
CGG R
28+
CGT R
29+
CTA L
30+
CTC L
31+
CTG L
32+
CTT L
33+
GAA E
34+
GAC D
35+
GAG E
36+
GAT D
37+
GCA A
38+
GCC A
39+
GCG A
40+
GCT A
41+
GGA G
42+
GGC G
43+
GGG G
44+
GGT G
45+
GTA V
46+
GTC V
47+
GTG V
48+
GTT V
49+
TAA Stop
50+
TAC Y
51+
TAG Stop
52+
TAT Y
53+
TCA S
54+
TCC S
55+
TCG S
56+
TCT S
57+
TGA Stop
58+
TGC C
59+
TGG W
60+
TGT C
61+
TTA L
62+
TTC F
63+
TTG L
64+
TTT F

extra/07_proteins/codons.rna

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
AAA K
2+
AAC N
3+
AAG K
4+
AAU N
5+
ACA T
6+
ACC T
7+
ACG T
8+
ACU T
9+
AGA R
10+
AGC S
11+
AGG R
12+
AGU S
13+
AUA I
14+
AUC I
15+
AUG M
16+
AUU I
17+
CAA Q
18+
CAC H
19+
CAG Q
20+
CAU H
21+
CCA P
22+
CCC P
23+
CCG P
24+
CCU P
25+
CGA R
26+
CGC R
27+
CGG R
28+
CGU R
29+
CUA L
30+
CUC L
31+
CUG L
32+
CUU L
33+
GAA E
34+
GAC D
35+
GAG E
36+
GAU D
37+
GCA A
38+
GCC A
39+
GCG A
40+
GCU A
41+
GGA G
42+
GGC G
43+
GGG G
44+
GGU G
45+
GUA V
46+
GUC V
47+
GUG V
48+
GUU V
49+
UAA Stop
50+
UAC Y
51+
UAG Stop
52+
UAU Y
53+
UCA S
54+
UCC S
55+
UCG S
56+
UCU S
57+
UGA Stop
58+
UGC C
59+
UGG W
60+
UGU C
61+
UUA L
62+
UUC F
63+
UUG L
64+
UUU F

extra/07_proteins/input.dna

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gaactacaccgttctcctggt

extra/07_proteins/input.rna

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGAA

0 commit comments

Comments
 (0)