Skip to content

Commit 0459bed

Browse files
authored
Merge pull request #5 from Edinburgh-Genome-Foundry/dev
Implements PR #4
2 parents 2110d15 + edee997 commit 0459bed

File tree

5 files changed

+64
-52
lines changed

5 files changed

+64
-52
lines changed

codon_usage_data/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.9
1+
0.1.10
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import python_codon_tables as pct
22

33
# PRINT THE LIST OF NAMES OF ALL AVAILABLE TABLES
4-
print ('Available tables:', pct.available_codon_tables_names)
4+
print("Available tables:", pct.available_codon_tables_names)
55

66
# LOAD ONE TABLE BY NAME
77
table = pct.get_codons_table("b_subtilis_1423")
8-
print (table['T']['ACA']) # returns 0.4
9-
print (table['*']['UAA']) # returns 0.61
8+
print(table["T"]["ACA"]) # returns 0.4
9+
print(table["*"]["TAA"]) # returns 0.61
1010

1111

1212
# LOAD ALL TABLES AT ONCE
1313
codon_tables = pct.get_all_available_codons_tables()
14-
print (codon_tables['c_elegans_6239']['L']['CUA']) # returns 0.09
14+
print(codon_tables["c_elegans_6239"]["L"]["CTA"]) # returns 0.09

python_codon_tables/python_codon_tables.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
_this_dir = os.path.dirname(os.path.realpath(__file__))
1616
_tables_dir = os.path.join(_this_dir, "..", "codon_usage_data", "tables")
1717

18-
available_codon_tables_names = [
19-
filename[:-4] for filename in os.listdir(_tables_dir)
20-
]
18+
available_codon_tables_names = [filename[:-4] for filename in os.listdir(_tables_dir)]
2119

2220
available_codon_tables_shortnames = {
2321
"_".join(table_name.split("_")[:-1]): table_name
@@ -68,9 +66,7 @@ def get_codons_table(table_name, replace_U_by_T=True, web_timeout=5):
6866
6967
"""
7068
if replace_U_by_T:
71-
table = get_codons_table(
72-
table_name, replace_U_by_T=False, web_timeout=5
73-
)
69+
table = get_codons_table(table_name, replace_U_by_T=False, web_timeout=5)
7470
return table_with_U_replaced_by_T(table)
7571
if isinstance(table_name, int) or str.isdigit(table_name):
7672
return download_codons_table(taxid=table_name, timeout=web_timeout)
@@ -92,8 +88,7 @@ def get_all_available_codons_tables(replace_U_by_T=True):
9288
def download_codons_table(taxid=316407, target_file=None, timeout=5):
9389
"""Get all data from all of this package's builtin codon usage tables."""
9490
_kazusa_url = (
95-
"http://www.kazusa.or.jp/codon/cgi-bin/showcodon.cgi"
96-
"?aa=1&style=N&species=%s"
91+
"http://www.kazusa.or.jp/codon/cgi-bin/showcodon.cgi" "?aa=1&style=N&species=%s"
9792
)
9893
_codon_regexpr = r"([ATGCU]{3}) ([A-Z]|\*) (\d.\d+)"
9994
url = _kazusa_url % taxid
@@ -112,14 +107,16 @@ def download_codons_table(taxid=316407, target_file=None, timeout=5):
112107
raise err
113108

114109
html_content = web_handle.read().decode().replace("\n", " ")
110+
if "<title>not found</title>" in html_content.lower():
111+
raise RuntimeError(
112+
"Codon usage table for taxonomy ID '%s' not found:" " %s" % (taxid, url)
113+
)
115114
csv_data = "\n".join(
116115
["amino_acid,codon,relative_frequency"]
117116
+ sorted(
118117
[
119118
"%s,%s,%s" % (aa, codon, usage)
120-
for codon, aa, usage in re.findall(
121-
_codon_regexpr, html_content
122-
)
119+
for codon, aa, usage in re.findall(_codon_regexpr, html_content)
123120
]
124121
)
125122
)

python_codon_tables/tests/test_basics.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,65 @@
33
import os
44
import python_codon_tables as pct
55

6+
67
def test_basics():
78

89
# LOAD ONE TABLE BY NAME
910
table = pct.get_codons_table("b_subtilis_1423")
10-
assert table['T']['ACA'] == 0.4
11-
assert table['*']['TAA'] == 0.61
11+
assert table["T"]["ACA"] == 0.4
12+
assert table["*"]["TAA"] == 0.61
1213

1314
# LOAD ALL TABLES AT ONCE
1415
codon_tables = pct.get_all_available_codons_tables()
15-
assert codon_tables['c_elegans_6239']['L']['CTA'] == 0.09
16+
assert codon_tables["c_elegans_6239"]["L"]["CTA"] == 0.09
17+
1618

1719
def test_download_codon_table(tmpdir):
1820
table = pct.download_codons_table(taxid=316407)
19-
assert table['*']['UAG'] == 0.07
20-
target = os.path.join(str(tmpdir), 'test.csv')
21+
assert table["*"]["UAG"] == 0.07
22+
target = os.path.join(str(tmpdir), "test.csv")
2123
table = pct.download_codons_table(taxid=316407, target_file=target)
2224

25+
2326
def test_readme_example():
2427
table = pct.get_codons_table("b_subtilis_1423")
25-
assert table['T']['ACA'] == 0.4
26-
assert table['*']['TAA'] == 0.61
28+
assert table["T"]["ACA"] == 0.4
29+
assert table["*"]["TAA"] == 0.61
2730

2831
# LOAD ALL TABLES AT ONCE
2932
codons_tables = pct.get_all_available_codons_tables()
30-
assert codons_tables['c_elegans_6239']['L']['CTA'] == 0.09
33+
assert codons_tables["c_elegans_6239"]["L"]["CTA"] == 0.09
3134

3235
# GET A TABLE DIRECTLY FROM THE INTERNET
3336
table = pct.download_codons_table(taxid=316407)
34-
assert table['*']['TGA'] == 0.29
37+
assert table["*"]["TGA"] == 0.29
38+
39+
with pytest.raises(RuntimeError):
40+
table = pct.download_codons_table(taxid=000000000) # does not exist
41+
3542

3643
def test_readme_example():
3744
table = pct.get_codons_table("b_subtilis_1423")
38-
assert table['T']['ACA'] == 0.4
39-
assert table['*']['TAA'] == 0.61
45+
assert table["T"]["ACA"] == 0.4
46+
assert table["*"]["TAA"] == 0.61
4047

4148
# LOAD ALL TABLES AT ONCE
4249
codons_tables = pct.get_all_available_codons_tables()
43-
assert codons_tables['c_elegans_6239']['L']['CTA'] == 0.09
50+
assert codons_tables["c_elegans_6239"]["L"]["CTA"] == 0.09
4451

4552
# GET A TABLE DIRECTLY FROM THE INTERNET
4653
table = pct.download_codons_table(taxid=316407)
47-
assert table['*']['UGA'] == 0.29
54+
assert table["*"]["UGA"] == 0.29
55+
4856

4957
def test_get_codons_table():
5058
for table_name in (1423, "1423", "b_subtilis", "b_subtilis_1423"):
5159
table = pct.get_codons_table(table_name)
52-
assert table['T']['ACA'] == 0.4
53-
assert table['*']['TAA'] == 0.61
60+
assert table["T"]["ACA"] == 0.4
61+
assert table["*"]["TAA"] == 0.61
62+
5463

5564
def test_replace_U_by_T():
5665
table = pct.get_codons_table("b_subtilis_1423", replace_U_by_T=False)
57-
assert table['*']['UAA'] == 0.61
66+
assert table["*"]["UAA"] == 0.61
67+

setup.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
import os
2+
23
try:
34
from setuptools import setup
45
except ImportError:
56
try:
67
from .python.ez_setup import use_setuptools
8+
79
use_setuptools()
810
except ImportError:
9-
raise ImportError("python_codon_tables could not be installed, "
10-
"probably because neither setuptools nor ez_setup "
11-
"are installed on this computer. \nInstall ez_setup "
12-
"([sudo] pip install ez_setup) and try again.")
11+
raise ImportError(
12+
"python_codon_tables could not be installed, "
13+
"probably because neither setuptools nor ez_setup "
14+
"are installed on this computer. \nInstall ez_setup "
15+
"([sudo] pip install ez_setup) and try again."
16+
)
1317

1418
from setuptools import setup, find_packages
1519

16-
with open(os.path.join('codon_usage_data', 'version.txt'), 'r') as f:
20+
with open(os.path.join("codon_usage_data", "version.txt"), "r") as f:
1721
__version__ = f.read()
1822

19-
with open(os.path.join('python_codon_tables', 'README.rst'), 'r') as f:
23+
with open(os.path.join("python_codon_tables", "README.rst"), "r") as f:
2024
long_description = f.read()
2125

22-
setup(name='python_codon_tables',
23-
version=__version__,
24-
author='Zulko',
25-
description='Codon Usage Tables for Python, from kazusa.or.jp',
26-
url='https://github.com/Edinburgh-Genome-Foundry/codon-usage-tables',
27-
long_description=long_description,
28-
license='MIT',
29-
keywords="DNA codon usage",
30-
packages=find_packages(exclude='docs'),
31-
include_package_data=True,
32-
package_data={
33-
'python_codon_tables': ['../codon_usage_data/*',
34-
'../codon_usage_data/**/*']
35-
})
26+
setup(
27+
name="python_codon_tables",
28+
version=__version__,
29+
author="Zulko",
30+
description="Codon Usage Tables for Python, from kazusa.or.jp",
31+
url="https://github.com/Edinburgh-Genome-Foundry/codon-usage-tables",
32+
long_description=long_description,
33+
license="Public Domain",
34+
keywords="DNA codon usage",
35+
packages=find_packages(exclude="docs"),
36+
include_package_data=True,
37+
package_data={
38+
"python_codon_tables": ["../codon_usage_data/*", "../codon_usage_data/**/*"]
39+
},
40+
)

0 commit comments

Comments
 (0)