File: test_KEGG_online.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 46,856 kB
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (289 lines) | stat: -rw-r--r-- 9,607 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Copyright 2014 by Kevin Wu.
# Copyright 2014 by Peter Cock.
# All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Tests for online functionality of the KEGG module."""

# Builtins
import os
import unittest
import tempfile

import requires_internet
requires_internet.check()

from Bio.KEGG.KGML import KGML_parser
from Bio.KEGG.REST import *

from Bio import SeqIO

# TODO - revert to using with statements once we drop
# Python 2.6 and 2.7, see http://bugs.python.org/issue12487


class KEGGTests(unittest.TestCase):
    """Tests for KEGG REST API."""

    def test_info_kegg(self):
        h = kegg_info("kegg")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/info/kegg")
        h.close()

    def test_info_pathway(self):
        h = kegg_info("pathway")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/info/pathway")
        h.close()

    def test_list_pathway(self):
        h = kegg_list("pathway")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/pathway")
        h.close()

    def test_pathway_hsa(self):
        h = kegg_list("pathway", "hsa")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/pathway/hsa")
        h.close()

    def test_list_organism(self):
        h = kegg_list("organism")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/organism")
        h.close()

    def test_list_hsa(self):
        h = kegg_list("hsa")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/hsa")
        h.close()

    def test_list_T01001(self):
        h = kegg_list("T01001")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/T01001")
        h.close()

    def test_list_hsa_10458_plus_ece_Z5100(self):
        h = kegg_list("hsa:10458+ece:Z5100")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/hsa:10458+ece:Z5100")
        h.close()

    def test_list_hsa_10458_list_ece_Z5100(self):
        h = kegg_list(["hsa:10458", "ece:Z5100"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/hsa:10458+ece:Z5100")
        h.close()

    def test_list_cpd_C01290_plus_gl_G0009(self):
        h = kegg_list("cpd:C01290+gl:G00092")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/cpd:C01290+gl:G00092")
        h.close()

    def test_list_cpd_C01290_list_gl_G0009(self):
        h = kegg_list(["cpd:C01290", "gl:G00092"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/cpd:C01290+gl:G00092")
        h.close()

    def test_list_C01290_plus_G00092(self):
        h = kegg_list("C01290+G00092")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/C01290+G00092")
        h.close()

    def test_list_C01290_list_G00092(self):
        h = kegg_list(["C01290", "G00092"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/list/C01290+G00092")
        h.close()

    def test_find_genes_shiga_plus_toxin(self):
        h = kegg_find("genes", "shiga+toxin")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/find/genes/shiga+toxin")
        h.close()

    def test_find_genes_shiga_list_toxin(self):
        h = kegg_find("genes", ["shiga", "toxin"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/find/genes/shiga+toxin")
        h.close()

    def test_find_compound_C7H10O5_formula(self):
        h = kegg_find("compound", "C7H10O5", "formula")
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/find/compound/C7H10O5/formula")
        h.close()

    def test_find_compound_O5C7_formula(self):
        h = kegg_find("compound", "O5C7", "formula")
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/find/compound/O5C7/formula")
        h.close()

    def test_find_compound_exact_mass(self):
        h = kegg_find("compound", "174.05", "exact_mass")
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/find/compound/174.05/exact_mass")
        h.close()

    def test_find_compound_weight(self):
        h = kegg_find("compound", "300-310", "mol_weight")
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/find/compound/300-310/mol_weight")
        h.close()

    def test_get_cpd_C01290_plus_gl_G00092(self):
        h = kegg_get("cpd:C01290+gl:G00092")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/get/cpd:C01290+gl:G00092")
        h.close()

    def test_get_cpd_C01290_list_gl_G00092(self):
        h = kegg_get(["cpd:C01290", "gl:G00092"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/get/cpd:C01290+gl:G00092")
        h.close()

    def test_get_C01290_plus_G00092(self):
        h = kegg_get(["C01290+G00092"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/get/C01290+G00092")
        h.close()

    def test_get_C01290_list_G00092(self):
        h = kegg_get(["C01290", "G00092"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/get/C01290+G00092")
        h.close()

    def test_get_hsa_10458_plus_ece_Z5100(self):
        h = kegg_get("hsa:10458+ece:Z5100")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/get/hsa:10458+ece:Z5100")
        h.close()

    def test_get_hsa_10458_list_ece_Z5100(self):
        h = kegg_get(["hsa:10458", "ece:Z5100"])
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/get/hsa:10458+ece:Z5100")
        h.close()

    def test_get_hsa_10458_plus_ece_Z5100_as_aaseq(self):
        h = kegg_get("hsa:10458+ece:Z5100", "aaseq")
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/get/hsa:10458+ece:Z5100/aaseq")
        data = SeqIO.parse(h, 'fasta')
        self.assertEqual(len(list(data)), 2)
        h.close()

    def test_get_hsa_10458_list_ece_Z5100_as_aaseq(self):
        h = kegg_get(["hsa:10458", "ece:Z5100"], "aaseq")
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/get/hsa:10458+ece:Z5100/aaseq")
        data = SeqIO.parse(h, 'fasta')
        self.assertEqual(len(list(data)), 2)
        h.close()

    def test_get_hsa_10458_plus_ece_Z5100_as_ntseq(self):
        h = kegg_get("hsa:10458+ece:Z5100", "ntseq")
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/get/hsa:10458+ece:Z5100/ntseq")
        data = SeqIO.parse(h, 'fasta')
        self.assertEqual(len(list(data)), 2)
        h.close()

    def test_get_hsa_10458_list_ece_Z5100_as_ntseq(self):
        h = kegg_get(["hsa:10458", "ece:Z5100"], "ntseq")
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/get/hsa:10458+ece:Z5100/ntseq")
        data = SeqIO.parse(h, 'fasta')
        self.assertEqual(len(list(data)), 2)
        h.close()

    def test_get_hsa05130_image(self):
        h = kegg_get("hsa05130", "image")
        data = h.read()
        self.assertEqual(data[:4], b"\x89PNG")
        self.assertEqual(h.url, "http://rest.kegg.jp/get/hsa05130/image")
        h.close()

    def test_conv_eco_ncbi_geneid(self):
        h = kegg_conv("eco", "ncbi-geneid")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/conv/eco/ncbi-geneid")
        h.close()

    def test_conv_ncbi_geneid_eco(self):
        h = kegg_conv("ncbi-geneid", "eco")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/conv/ncbi-geneid/eco")
        h.close()

    def test_conv_ncbi_gi_hsa_10458_plus_ece_Z5100(self):
        h = kegg_conv("ncbi-gi", "hsa:10458+ece:Z5100")
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/conv/ncbi-gi/hsa:10458+ece:Z5100")
        h.close()

    def test_conv_ncbi_gi_hsa_10458_list_ece_Z5100(self):
        h = kegg_conv("ncbi-gi", ["hsa:10458", "ece:Z5100"])
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/conv/ncbi-gi/hsa:10458+ece:Z5100")
        h.close()

    def test_link_pathway_hsa(self):
        h = kegg_link("pathway", "hsa")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/link/pathway/hsa")
        h.close()

    def test_link_hsa_pathway(self):
        h = kegg_link("hsa", "pathway")
        h.read()
        self.assertEqual(h.url, "http://rest.kegg.jp/link/hsa/pathway")
        h.close()

    def test_pathway_hsa_10458_plus_ece_Z5100(self):
        h = kegg_link("pathway", "hsa:10458+ece:Z5100")
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/link/pathway/hsa:10458+ece:Z5100")
        h.close()

    def test_pathway_hsa_10458_list_ece_Z5100(self):
        h = kegg_link("pathway", ["hsa:10458", "ece:Z5100"])
        h.read()
        self.assertEqual(h.url,
                         "http://rest.kegg.jp/link/pathway/hsa:10458+ece:Z5100")
        h.close()


class KGMLPathwayTests(unittest.TestCase):
    """Tests with metabolic maps."""

    def test_parse_remote_pathway(self):
        """Download a KEGG pathway from the KEGG server and parse KGML."""
        h = kegg_get("ko03070", "kgml")
        pathway = KGML_parser.read(h)
        self.assertEqual(pathway.name, "path:ko03070")
        h.close()


if __name__ == '__main__':
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)