File: test_KEGG_online.py

package info (click to toggle)
python-biopython 1.85%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 126,372 kB
  • sloc: xml: 1,047,995; python: 332,722; ansic: 16,944; sql: 1,208; makefile: 140; sh: 81
file content (277 lines) | stat: -rw-r--r-- 10,641 bytes parent folder | download
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
# 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 io
import unittest

import requires_internet

from Bio import SeqIO
from Bio.KEGG.KGML import KGML_parser
from Bio.KEGG.REST import kegg_conv
from Bio.KEGG.REST import kegg_find
from Bio.KEGG.REST import kegg_get
from Bio.KEGG.REST import kegg_info
from Bio.KEGG.REST import kegg_link
from Bio.KEGG.REST import kegg_list

requires_internet.check()


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

    def test_info_kegg(self):
        with kegg_info("kegg") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/info/kegg")

    def test_info_pathway(self):
        with kegg_info("pathway") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/info/pathway")

    def test_list_pathway(self):
        with kegg_list("pathway") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/pathway")

    def test_pathway_hsa(self):
        with kegg_list("pathway", "hsa") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/pathway/hsa")

    def test_list_organism(self):
        with kegg_list("organism") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/organism")

    def test_list_hsa(self):
        with kegg_list("hsa") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/hsa")

    def test_list_T01001(self):
        with kegg_list("T01001") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/T01001")

    def test_list_hsa_10458_plus_ece_Z5100(self):
        with kegg_list("hsa:10458+ece:Z5100") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/hsa:10458+ece:Z5100")

    def test_list_hsa_10458_list_ece_Z5100(self):
        with kegg_list(["hsa:10458", "ece:Z5100"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/hsa:10458+ece:Z5100")

    def test_list_cpd_C01290_plus_gl_G0009(self):
        with kegg_list("cpd:C01290+gl:G00092") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/cpd:C01290+gl:G00092")

    def test_list_cpd_C01290_list_gl_G0009(self):
        with kegg_list(["cpd:C01290", "gl:G00092"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/cpd:C01290+gl:G00092")

    def test_list_C01290_plus_G00092(self):
        with kegg_list("C01290+G00092") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/C01290+G00092")

    def test_list_C01290_list_G00092(self):
        with kegg_list(["C01290", "G00092"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/list/C01290+G00092")

    def test_find_genes_shiga_plus_toxin(self):
        with kegg_find("genes", "shiga+toxin") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/find/genes/shiga+toxin")

    def test_find_genes_shiga_list_toxin(self):
        with kegg_find("genes", ["shiga", "toxin"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/find/genes/shiga+toxin")

    def test_find_compound_C7H10O5_formula(self):
        with kegg_find("compound", "C7H10O5", "formula") as handle:
            handle.read()
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/find/compound/C7H10O5/formula"
        )

    def test_find_compound_O5C7_formula(self):
        with kegg_find("compound", "O5C7", "formula") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/find/compound/O5C7/formula")

    def test_find_compound_exact_mass(self):
        with kegg_find("compound", "174.05", "exact_mass") as handle:
            handle.read()
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/find/compound/174.05/exact_mass"
        )

    def test_find_compound_weight(self):
        with kegg_find("compound", "300-310", "mol_weight") as handle:
            handle.read()
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/find/compound/300-310/mol_weight"
        )

    def test_get_br_ko00002(self):
        with kegg_get("br:ko00002", "json") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/get/br:ko00002/json")

    def test_get_cpd_C01290_plus_gl_G00092(self):
        with kegg_get("cpd:C01290+gl:G00092") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/get/cpd:C01290+gl:G00092")

    def test_get_cpd_C01290_list_gl_G00092(self):
        with kegg_get(["cpd:C01290", "gl:G00092"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/get/cpd:C01290+gl:G00092")

    def test_get_C01290_plus_G00092(self):
        with kegg_get(["C01290+G00092"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/get/C01290+G00092")

    def test_get_C01290_list_G00092(self):
        with kegg_get(["C01290", "G00092"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/get/C01290+G00092")

    def test_get_hsa_10458_plus_ece_Z5100(self):
        with kegg_get("hsa:10458+ece:Z5100") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/get/hsa:10458+ece:Z5100")

    def test_get_hsa_10458_list_ece_Z5100(self):
        with kegg_get(["hsa:10458", "ece:Z5100"]) as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/get/hsa:10458+ece:Z5100")

    def test_get_hsa_10458_plus_ece_Z5100_as_aaseq(self):
        with kegg_get("hsa:10458+ece:Z5100", "aaseq") as handle:
            data = SeqIO.parse(handle, "fasta")
            self.assertEqual(len(list(data)), 2)
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/get/hsa:10458+ece:Z5100/aaseq"
        )

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

    def test_get_hsa_10458_plus_ece_Z5100_as_ntseq(self):
        with kegg_get("hsa:10458+ece:Z5100", "ntseq") as handle:
            data = SeqIO.parse(handle, "fasta")
            self.assertEqual(len(list(data)), 2)
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/get/hsa:10458+ece:Z5100/ntseq"
        )

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

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

    def test_conv_eco_ncbi_geneid(self):
        with kegg_conv("eco", "ncbi-geneid") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/conv/eco/ncbi-geneid")

    def test_conv_ncbi_geneid_eco(self):
        with kegg_conv("ncbi-geneid", "eco") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/conv/ncbi-geneid/eco")

    def test_conv_ncbi_gi_hsa_10458_plus_ece_Z5100(self):
        with kegg_conv("ncbi-gi", "hsa:10458+ece:Z5100") as handle:
            handle.read()
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/conv/ncbi-gi/hsa:10458+ece:Z5100"
        )

    def test_conv_ncbi_gi_hsa_10458_list_ece_Z5100(self):
        with kegg_conv("ncbi-gi", ["hsa:10458", "ece:Z5100"]) as handle:
            handle.read()
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/conv/ncbi-gi/hsa:10458+ece:Z5100"
        )

    def test_link_pathway_hsa(self):
        with kegg_link("pathway", "hsa") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/link/pathway/hsa")

    def test_link_hsa_pathway(self):
        with kegg_link("hsa", "pathway") as handle:
            handle.read()
        self.assertEqual(handle.url, "https://rest.kegg.jp/link/hsa/pathway")

    def test_pathway_hsa_10458_plus_ece_Z5100(self):
        with kegg_link("pathway", "hsa:10458+ece:Z5100") as handle:
            handle.read()
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/link/pathway/hsa:10458+ece:Z5100"
        )

    def test_pathway_hsa_10458_list_ece_Z5100(self):
        with kegg_link("pathway", ["hsa:10458", "ece:Z5100"]) as handle:
            handle.read()
        self.assertEqual(
            handle.url, "https://rest.kegg.jp/link/pathway/hsa:10458+ece:Z5100"
        )


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."""
        with kegg_get("ko03070", "kgml") as handle:
            pathway = KGML_parser.read(handle)
        self.assertEqual(pathway.name, "path:ko03070")

    def test_parser_roundtrip(self):
        """Download a KEGG pathway, write local KGML and check roundtrip."""
        with kegg_get("ko00680", "kgml") as remote_handle:
            pathway = KGML_parser.read(remote_handle)

        with io.StringIO(pathway.get_KGML()) as local_handle:
            roundtrip = KGML_parser.read(local_handle)

        self.assertEqual(pathway.name, roundtrip.name)
        self.assertEqual(len(pathway.relations), len(roundtrip.relations))


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