-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpytaxonkit.py
More file actions
1239 lines (1090 loc) · 41.9 KB
/
Copy pathpytaxonkit.py
File metadata and controls
1239 lines (1090 loc) · 41.9 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2020, DHS.
#
# This file is part of pytaxonkit (http://github.com/bioforensics/pytaxonkit)
# and is licensed under the BSD license.
#
#
# This Software was prepared for the Department of Homeland Security
# (DHS) by the Battelle National Biodefense Institute, LLC (BNBI) as
# part of contract HSHQDC-15-C-00064 to manage and operate the National
# Biodefense Analysis and Countermeasures Center (NBACC), a Federally
# Funded Research and Development Center.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
from builtins import list as pylist
from collections import namedtuple
from io import StringIO
import json
import os
import pandas as pd
from pandas import UInt32Dtype, StringDtype
from pytaxonkit_version import get_versions
import pytest
from subprocess import Popen, PIPE
import sys
from tempfile import NamedTemporaryFile
from warnings import warn
__version__ = get_versions()["version"]
del get_versions
class TaxonKitCLIError(RuntimeError):
pass
class NCBITaxonomyDumpNotFoundError(FileNotFoundError):
pass
def _get_taxonkit_version():
proc = Popen(["taxonkit", "version"], stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate()
if proc.returncode != 0:
raise TaxonKitCLIError(err) # pragma: no cover
return out.strip()
def log(*args, level="debug"): # pragma: no cover
print(f"[pytaxonkit::{level}]", *args, file=sys.stderr)
def validate_data_dir(path):
filepath = os.path.join(path, "nodes.dmp")
if not os.path.isfile(filepath) or os.stat(filepath).st_size == 0:
raise NCBITaxonomyDumpNotFoundError(path)
return os.path.realpath(path)
def test_validate_data_dir():
realdir = os.path.realpath(os.path.expanduser("~/.taxonkit/"))
assert validate_data_dir(os.path.expanduser("~/.taxonkit/")) == realdir
with pytest.raises(NCBITaxonomyDumpNotFoundError):
assert validate_data_dir("/path/to/a/non/existent/directory/taxonkit")
def validate_threads(value):
if value is None:
return None
try:
threadcount = int(value)
return str(threadcount)
except ValueError:
log(f'invalid thread count "{value}"; resetting to taxonkit default', level="warning")
return None
def validate_n(value):
if value is None:
return None
try:
n = int(value)
return str(n)
except ValueError:
log(f'invalid n count "{value}"; resetting to taxonkit default', level="warning")
return None
def test_validate_threads(capsys):
assert validate_threads(None) is None
assert validate_threads(2) == "2"
assert validate_threads("16") == "16"
out, err = capsys.readouterr()
assert out == err == ""
assert validate_threads("StuffedCrust") is None
out, err = capsys.readouterr()
assert out == ""
m = '[pytaxonkit::warning] invalid thread count "StuffedCrust"; resetting to taxonkit default'
assert err.strip() == m
__taxonkitversion__ = _get_taxonkit_version()
# -------------------------------------------------------------------------------------------------
# taxonkit list
# -------------------------------------------------------------------------------------------------
BasicTaxon = namedtuple("BasicTaxon", ["taxid", "rank", "name"])
class ListResult:
def __init__(self, jsondata):
self._data = json.loads(jsondata)
def __len__(self):
return len(self._data)
def __str__(self):
return json.dumps(self._data, indent=4)
def __iter__(self):
for taxonstr, taxtree in self._data.items():
taxid, remainder = taxonstr.split("[", 1)
rank, name = remainder.split("]", 1)
taxon = BasicTaxon(taxid=int(taxid.strip()), rank=rank, name=name.strip())
taxtree = ListResult(json.dumps(taxtree))
yield taxon, taxtree
def _do_traverse(self, tree):
for taxonstr, taxtree in tree.items():
taxid, remainder = taxonstr.split("[", 1)
rank, name = remainder.split("]", 1)
taxon = BasicTaxon(taxid=int(taxid.strip()), rank=rank, name=name.strip())
yield taxon
if len(taxtree) > 0:
for subtaxon in self._do_traverse(taxtree):
yield subtaxon
@property
def traverse(self):
for taxon in self._do_traverse(self._data):
yield taxon
def list(ids, raw=False, threads=None, data_dir=None, debug=False):
"""list taxon tree of given taxids
Parameters
----------
ids : list or iterable
A list of taxids (ints or strings are ok)
raw : bool, default False
The `ListResult` object returned by default provides convenient programmatic access to the
result, but it also introduces a significant amount of overhead; for very large results, it
is recommended to set `raw=True` so that the raw JSON data (loaded into a dictionary) is
returned
threads : int, default None
Override the default taxonkit threads setting
data_dir : str, default None
Specify the location of the NCBI taxonomy `.dmp` files; by default, taxonkit searches in
`~/.taxonkit/`
debug : bool, default False
Print debugging output, e.g., system calls to `taxonkit`
Returns
-------
ListResult or dict
The `pytaxonkit.ListResult` class provides convenient access to the full taxonomic tree
associated with each top level `taxonkit list` result. When `raw=True`, a dict built from
the raw JSON is returned.
Examples
--------
>>> import pytaxonkit
>>> result = pytaxonkit.list([268197, 9903])
>>> for taxon, tree in result:
... subtaxa = [t for t in tree.traverse]
... print(f'Top level result: {taxon.name} ({taxon.taxid}); {len(subtaxa)} related taxa')
...
Top level result: Polistes comanchus (268197); 2 related taxa
Top level result: Bos (9903); 36 related taxa
>>> subtaxa[0]
BasicTaxon(taxid=9904, rank='species', name='Bos gaurus')
>>> pytaxonkit.list([9605], raw=True)
{'9605 [genus] Homo': {'9606 [species] Homo sapiens': {'63221 [subspecies] Homo sapiens neanderthalensis': {}, "741158 [subspecies] Homo sapiens subsp. 'Denisova'": {}}, '1425170 [species] Homo heidelbergensis': {}, '2665952 [no rank] environmental samples': {'2665953 [species] Homo sapiens environmental sample': {}}, '2813598 [no rank] unclassified Homo': {'2813599 [species] Homo sp.': {}}}}
""" # noqa: E501
idlist = ",".join(map(str, ids))
if idlist == "":
warn("No input for pytaxonkit.list", UserWarning)
return
arglist = ["taxonkit", "list", "--json", "--show-name", "--show-rank", "--ids", idlist]
if threads:
arglist.extend(("--threads", validate_threads(threads)))
if data_dir:
arglist.extend(("--data-dir", validate_data_dir(data_dir))) # pragma: no cover
if debug:
log(*arglist) # pragma: no cover
proc = Popen(arglist, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate()
if proc.returncode != 0:
raise TaxonKitCLIError(err) # pragma: no cover
if raw:
return json.loads(out)
else:
return ListResult(out)
def test_list_leaves(capsys):
result = list([8204, 2468], debug=True) # Nota bene: `list` here is `pytaxonkit.list`
assert len(result) == 2
top_level_taxa = [taxon for taxon, tree in result]
sub_trees = [tree for taxon, tree in result]
assert top_level_taxa == [
BasicTaxon(taxid=8204, rank="species", name="Anarhichas lupus"),
BasicTaxon(taxid=2468, rank="species", name="Plasmid NR79"),
]
assert all(len(t) == 0 for t in sub_trees)
out, err = capsys.readouterr()
data = "[pytaxonkit::debug] taxonkit list --json --show-name --show-rank --ids 8204,2468"
assert err.strip() == data
@pytest.mark.parametrize(
"taxid,taxon,subtaxon,subsubtaxon",
[
(
83882,
BasicTaxon(taxid=83882, rank="genus", name="Apogon"),
BasicTaxon(taxid=638272, rank="subgenus", name="Apogon"),
BasicTaxon(taxid=308069, rank="species", name="Apogon maculatus"),
),
(
44568,
BasicTaxon(taxid=44568, rank="genus", name="Parasimulium"),
BasicTaxon(taxid=61057, rank="subgenus", name="Parasimulium"),
BasicTaxon(taxid=61060, rank="species", name="Parasimulium crosskeyi"),
),
],
)
def test_list_genera(taxid, taxon, subtaxon, subsubtaxon):
result = list([taxid], threads=1) # Nota bene: `list` here is `pytaxonkit.list`
tax, tree = next(iter(result))
subtax, subtree = next(iter(tree))
subsubtax, subsubtree = next(iter(subtree))
assert tax == taxon
assert subtax == subtaxon
assert subsubtax == subsubtaxon
assert len(subsubtree) == 0
def test_list_str():
result = list(["20019"])
with open("sweetleaf.json", "r") as fh:
assert str(result) == fh.read().strip()
def test_list_empty():
with pytest.warns(UserWarning, match="No input for pytaxonkit.list"):
result = list([])
assert result is None
@pytest.mark.parametrize(
"taxid,name",
[
(668369, "Escherichia coli DH5[alpha]"),
(11022, "Eastern equine encephalitis virus (STRAIN VA33[TEN BROECK])"),
],
)
def test_list_taxon_name_with_extra_brackets_regression(taxid, name):
result = iter(list([taxid]))
taxon, tree = next(result)
assert taxon.name == name
assert taxon.taxid == taxid
assert len(tree) == 0
# -------------------------------------------------------------------------------------------------
# taxonkit lineage
# -------------------------------------------------------------------------------------------------
def lineage(
ids,
formatstr=None,
threads=None,
data_dir=None,
debug=False,
):
"""query lineage of given taxids
Executes `taxonkit lineage` and `taxonkit reformat` to provide both a full and a "standard"
lineage for each taxid in `ids`.
Parameters
----------
ids : list or iterable
A list of taxids (ints or strings are ok)
formatstr : str, default None
See [taxonkit reformat2 documentation](https://bioinf.shenwei.me/taxonkit/usage/#reformat)
for instructions on setting `formatstr` to override the default standard lineage format
threads : int
Override the default taxonkit threads setting
data_dir : str, default None
Specify the location of the NCBI taxonomy `.dmp` files; by default, taxonkit searches in
`~/.taxonkit/`
debug : bool, default False
Print debugging output, e.g., system calls to `taxonkit`
Returns
-------
DataFrame
A two-dimensional data structure. The `Code` column refers to the status code returned by
`taxonkit lineage`: -1 for queries not found, 0 for deleted taxids (in `delnodes.dmp`), new
taxids for merged taxids (in `merged.dmp`), and own taxid for queries found in `nodes.dmp`.
See [taxonkit lineage](https://bioinf.shenwei.me/taxonkit/usage/#lineage) for details.
Examples
--------
>>> import pytaxonkit
>>> result = pytaxonkit.lineage([1325911, 1649473, 1401311])
>>> result.columns
Index(['TaxID', 'Code', 'Name', 'Lineage', 'LineageTaxIDs', 'Rank', 'FullLineage', 'FullLineageTaxIDs', 'FullLineageRanks'], dtype='object')
>>> result[["TaxID", "Lineage", "LineageTaxIDs"]]
TaxID Lineage LineageTaxIDs
0 1325911 Eukaryota;Arthropoda;Insecta;Hymenoptera;Eucharitidae;Pogonocharis; 2759;6656;50557;7399;216140;1325911;
1 1649473 Bacteria;Bacteroidota;Cytophagia;Cytophagales;Spirosomataceae;Nibrella; 2;976;768503;768507;2896860;1649473;
2 1401311 Eukaryota;Arthropoda;Insecta;Coleoptera;Staphylinidae;Styngetus; 2759;6656;50557;7041;29026;1401311;
>>> result = pytaxonkit.lineage(["1382510", "929505", "390333"], formatstr="{family};{genus};{species};{subspecies|strain}")
>>> result[["TaxID", "Lineage", "LineageTaxIDs"]]
TaxID Lineage LineageTaxIDs
0 1382510 Enterobacteriaceae;Salmonella;Salmonella bongori;Salmonella bongori serovar 48:z41:-- str. RKS3044 543;590;54736;1382510
1 929505 Clostridiaceae;Clostridium;Clostridium botulinum;Clostridium botulinum C str. Stockholm 31979;1485;1491;929505
2 390333 Lactobacillaceae;Lactobacillus;Lactobacillus delbrueckii;Lactobacillus delbrueckii subsp. bulgaricus 33958;1578;1584;1585
""" # noqa: E501
idlist = "\n".join(map(str, ids))
if idlist == "":
warn("No input for pytaxonkit.lineage", UserWarning)
return
arglist = [
"taxonkit",
"lineage",
"--show-lineage-taxids",
"--show-rank",
"--show-status-code",
"--show-name",
"--show-lineage-ranks",
]
if threads:
arglist.extend(("--threads", validate_threads(threads)))
if data_dir:
arglist.extend(("--data-dir", validate_data_dir(data_dir))) # pragma: no cover
if debug:
log(*arglist)
with NamedTemporaryFile(suffix="-lineage.txt") as lineagefile:
proc = Popen(arglist, stdin=PIPE, stdout=lineagefile, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate(input=idlist)
if proc.returncode != 0:
raise TaxonKitCLIError(err) # pragma: no cover
lineagefile.flush()
os.fsync(lineagefile.fileno())
extraargs = []
if formatstr:
extraargs.extend(("--format", formatstr))
if threads:
extraargs.extend(("--threads", validate_threads(threads)))
if data_dir:
extraargs.extend(("--data-dir", validate_data_dir(data_dir))) # pragma: no cover
arglist = [
"taxonkit",
"reformat2",
*extraargs,
"--taxid-field",
"1",
"--show-lineage-taxids",
lineagefile.name,
]
if debug:
log(*arglist)
proc = Popen(arglist, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate()
if proc.returncode != 0:
raise TaxonKitCLIError(err) # pragma: no cover
columnorderin = [
"TaxID",
"Code",
"FullLineage",
"FullLineageTaxIDs",
"Name",
"Rank",
"FullLineageRanks",
"Lineage",
"LineageTaxIDs",
]
columnorderout = [
"TaxID",
"Code",
"Name",
"Lineage",
"LineageTaxIDs",
"Rank",
"FullLineage",
"FullLineageTaxIDs",
"FullLineageRanks",
]
data = pd.read_csv(
StringIO(out), sep="\t", header=None, names=columnorderin, index_col=False
)
data = data[columnorderout]
return data
def name(ids, data_dir=None, debug=False):
"""rapid taxon name retrieval
Uses the `--no-lineage` option in `taxonkit lineage` for rapid retrieval of taxon names.
Parameters
----------
ids : list or iterable
A list of taxids (ints or strings are ok)
data_dir : str, default None
Specify the location of the NCBI taxonomy `.dmp` files; by default, taxonkit searches in
`~/.taxonkit/`
debug : bool, default False
Print debugging output, e.g., system calls to `taxonkit`
Returns
-------
DataFrame
A two-dimensional data structure with TaxIDs and taxon names.
Examples
--------
>>> import pytaxonkit
>>> name(["151837", "2216222", "517824"])
TaxID Name
0 151837 Hiraea smilacina
1 2216222 Paramyia sp. BIOUG21706-A10
2 517824 soil bacterium Cipr-S1N-M1LLLSSL-1
"""
idlist = "\n".join(map(str, ids)) + "\n"
if idlist == "\n":
warn("No input for pytaxonkit.name", UserWarning)
return
arglist = ["taxonkit", "lineage", "--show-name", "--no-lineage"]
if data_dir:
arglist.extend(("--data-dir", validate_data_dir(data_dir))) # pragma: no cover
if debug:
log(*arglist)
proc = Popen(arglist, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate(input=idlist)
data = pd.read_csv(
StringIO(out), sep="\t", header=None, names=["TaxID", "Name"], index_col=False
)
return data
def test_lineage(capsys):
result = lineage(["1082657", "265720", "1191594", "106649", "2868953"], debug=True)
assert result.TaxID.equals(pd.Series([1082657, 265720, 1191594, 106649, 2868953]))
assert result.Code.equals(pd.Series([1082657, 265720, 1191594, 106649, 2868953]))
assert result.Lineage.equals(
pd.Series(
[
"Eukaryota;Discosea;;Longamoebia;Acanthamoebidae;Acanthamoeba;Acanthamoeba sp. TW95",
"Bacteria;Bacteroidota;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas;Porphyromonas genomosp. P3",
"Eukaryota;Basidiomycota;Agaricomycetes;Russulales;Russulaceae;Russula;Russula carmesina",
"Bacteria;Pseudomonadota;Gammaproteobacteria;Moraxellales;Moraxellaceae;Acinetobacter;Acinetobacter guillouiae",
"Eukaryota;Arthropoda;Insecta;Hemiptera;Lygaeidae;Lygaeosoma;Lygaeosoma sardeum",
]
)
)
print(result.LineageTaxIDs.to_list())
assert result.LineageTaxIDs.equals(
pd.Series(
[
"2759;555280;;1485168;33677;5754;1082657",
"2;976;200643;171549;171551;836;265720",
"2759;5204;155619;452342;5401;5402;1191593",
"2;1224;1236;2887326;468;469;106649",
"2759;6656;50557;7524;7533;2868952;2868953",
]
)
)
assert result.Rank.equals(pd.Series(["species", "species", "varietas", "species", "species"]))
out, err = capsys.readouterr()
assert "taxonkit lineage --show-lineage-taxids --show-rank --show-status-code" in err
assert "taxonkit reformat2 --taxid-field 1 --show-lineage-taxids" in err
def test_lineage_single_taxid():
result = lineage([128370])
assert result.TaxID.iloc[0] == 128370
def test_lineage_threads():
result = lineage(["200643"], threads=1)
assert (
result.FullLineageRanks.iloc[0] == "cellular root;domain;kingdom;clade;clade;phylum;class"
)
expected = "cellular organisms;Bacteria;Pseudomonadati;FCB group;Bacteroidota/Chlorobiota group;Bacteroidota;Bacteroidia"
assert result.FullLineage.iloc[0] == expected
def test_lineage_name():
result = lineage(["526061"])
assert result.Name.iloc[0] == "Henosepilachna sp. AGBA-2008"
def test_lineage_format():
formatstr = "k__{domain|acellular root|superkingdom};p__{phylum};c__{class};o__{order};f__{family};g__{genus};s__{species}"
result = lineage([64191], formatstr=formatstr)
obs_out = result.Lineage.iloc[0]
exp_out = "k__Bacteria;p__Pseudomonadota;c__Alphaproteobacteria;o__;f__;g__;s__magnetic proteobacterium strain rj53"
assert exp_out == obs_out
formatstr = "k__{domain|acellular root|superkingdom};PHYLUM:{phylum};CLASS:{class};o__{order};f__{family};g__{genus};s__{species}"
result = lineage(["229933"], formatstr=formatstr)
obs_out = result.Lineage.iloc[0]
exp_out = "k__Bacteria;PHYLUM:Pseudomonadota;CLASS:Alphaproteobacteria;o__Rickettsiales;f__Anaplasmataceae;g__Wolbachia;s__Wolbachia endosymbiont of Togo hemipterus (strain 1)"
assert exp_out == obs_out
def test_name_debug(capsys):
result = name([207661, 1353792, 1597281], debug=True)
assert result.Name.equals(
pd.Series(
[
"Ahnfeltiopsis concinna",
"Picobirnavirus turkey/USA-1512/2010",
"Isopoda sp. NZAC 03013534",
]
)
)
out, err = capsys.readouterr()
assert "taxonkit lineage --show-name --no-lineage" in err
def test_name_regression():
result = name([6])
print(result)
assert len(result) == 1
assert result.Name[0] == "Azorhizobium"
def test_lineage_empty():
with pytest.warns(UserWarning, match="No input for pytaxonkit.lineage"):
result = lineage([])
assert result is None
def test_name_empty():
with pytest.warns(UserWarning, match="No input for pytaxonkit.name"):
result = name([])
assert result is None
# -------------------------------------------------------------------------------------------------
# taxonkit name2taxid
# -------------------------------------------------------------------------------------------------
def name2taxid(
names, sciname=False, threads=None, data_dir=None, debug=False, fuzzy=False, fuzzy_top_n=None
):
"""query taxid by taxon scientific name
Parameters
----------
names : list or iterable
A list of species names or synonyms
sciname: bool, default False
By default, both scientific names and synonyms are supported; when `sciname=True`, synonyms
are ignored
threads : int
Override the default taxonkit threads setting
data_dir : str, default None
Specify the location of the NCBI taxonomy `.dmp` files; by default, taxonkit searches in
`~/.taxonkit/`
fuzzy: bool, default False
By default, name matches need to be exact; when 'fuzzy=True' fuzzy
fuzzy_top_n : int
Override the default taxonkit setting for number of matches in fuzzy search
debug : bool, default False
Print debugging output, e.g., system calls to `taxonkit`
Returns
-------
DataFrame
A two-dimensional data structure.
Examples
--------
>>> import pytaxonkit
>>> names = ["Phyllobolus spinuliferus", "Alteromonas putrefaciens", "Rexia erectus"]
>>> pytaxonkit.name2taxid(names)
Name TaxID Rank
0 Phyllobolus spinuliferus 359607 species
1 Alteromonas putrefaciens 24 species
2 Rexia erectus 262902 species
>>> pytaxonkit.name2taxid(names, sciname=True)
Name TaxID Rank
0 Phyllobolus spinuliferus <NA> <NA>
1 Alteromonas putrefaciens <NA> <NA>
2 Rexia erectus <NA> <NA>
"""
namelist = "\n".join(map(str, names))
if namelist == "":
warn("No input for pytaxonkit.name2taxid", UserWarning)
return
arglist = ["taxonkit", "name2taxid", "--show-rank"]
if sciname:
arglist.append("--sci-name")
if threads:
arglist.extend(("--threads", validate_threads(threads)))
if data_dir:
arglist.extend(("--data-dir", validate_data_dir(data_dir))) # pragma: no cover
if fuzzy:
arglist.append("--fuzzy")
if fuzzy_top_n:
arglist.extend(("--fuzzy-top-n", validate_n(fuzzy_top_n)))
if debug:
log(*arglist) # pragma: no cover
proc = Popen(arglist, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate(input=namelist)
if proc.returncode != 0:
raise TaxonKitCLIError(err) # pragma: no cover
columns = {
"Name": StringDtype(),
"TaxID": UInt32Dtype(),
"Rank": StringDtype(),
}
data = pd.read_csv(
StringIO(out), sep="\t", header=None, names=columns, dtype=columns, index_col=False
)
return data
def test_name2taxid(capsys):
result = name2taxid(["Chaetocerotales", "Diptera", "Rickettsiales", "Hypocreales"], debug=True)
taxids = pd.Series([265576, 7147, 766, 5125], dtype=UInt32Dtype())
ranks = pd.Series(["order", "order", "order", "order"], dtype=StringDtype())
assert result.TaxID.equals(taxids)
assert result.Rank.equals(ranks)
out, err = capsys.readouterr()
assert "[pytaxonkit::debug] taxonkit name2taxid --show-rank" in err
def test_name2taxid_threads():
result = name2taxid(["FCB group"], threads="1")
assert str(result) == " Name TaxID Rank\n0 FCB group 1783270 clade"
def test_name2taxid_empty():
with pytest.warns(UserWarning, match="No input for pytaxonkit.name2taxid"):
result = name2taxid([])
assert result is None
def test_name2taxid_fuzzy():
result = name2taxid(["Paramecium tetraurelia strain Stock d4-2"], fuzzy=True)
print(result.to_string())
assert len(result) == 1
row = result.iloc[0]
assert row.TaxID == 412030
assert row.Rank == "strain"
# -------------------------------------------------------------------------------------------------
# taxonkit filter
# -------------------------------------------------------------------------------------------------
def filter(
ids,
threads=None,
equal_to=None,
higher_than=None,
lower_than=None,
discard_norank=False,
save_predictable=False,
discard_root=False,
root_taxid=None,
blacklist=None,
rank_file=None,
debug=False,
):
"""filter taxids by taxonomic rank (or a range of ranks)
Executes the `taxonkit filter` command to include or exclude taxa at the specified ranks.
Parameters
----------
ids : list or iterable
A list of taxids (ints or strings are ok)
threads : int
Override the default taxonkit threads setting
equal_to : str or list, default None
Keep only taxa at the specified rank(s); can be a string or a list of strings
higher_than : str, default None
Keep only taxa ranked higher than the specified rank
lower_than : str, default None
Keep only taxa ranked lower than the specified rank
discard_norank : bool, default False
Discard generic ranks without an explicit ranking order ("no rank" and "clade")
save_predictable : bool, default False
When `discard_norank=True`, do not discard some special ranks without order where the rank
of the closest higher node is still lower than rank cutoff
discard_root : bool, default False
Discard root taxon
root_taxid : int or str
override taxid of the root taxon
blacklist : list of strs
A list of ranks to exclude
rank_file : str, default None
Specify the location of the rank definition and order file; by default, taxonkit uses
`~/taxonkit/ranks.txt`
debug : bool, default False
Print debugging output, e.g., system calls to `taxonkit`
Returns
-------
list
A list of taxids passing the specified filters.
>>> import pytaxonkit
>>> taxids = [131567, 2759, 33154, 33208, 6072, 33213, 33317, 1206794, 88770, 6656, 197563, 197562, 6960, 50557, 85512, 7496, 33340, 33392, 85604, 7088]
>>> result = pytaxonkit.filter(taxids, equal_to='phylum', higher_than='phylum')
>>> pytaxonkit.name(result)
TaxID Name
0 131567 cellular organisms
1 2759 Eukaryota
2 33154 Opisthokonta
3 33208 Metazoa
4 6072 Eumetazoa
5 33213 Bilateria
6 33317 Protostomia
7 1206794 Ecdysozoa
8 88770 Panarthropoda
9 6656 Arthropoda
10 197563 Mandibulata
11 197562 Pancrustacea
12 85512 Dicondylia
>>> taxids = [131567, 2759, 33154, 33208, 6072, 33213, 33317, 1206794, 88770, 6656, 197563, 197562, 6960, 50557, 85512, 7496, 33340, 33342, 7524]
>>> result = pytaxonkit.filter(taxids, lower_than='phylum', discard_norank=True)
>>> pytaxonkit.name(result)
TaxID Name
0 6960 Hexapoda
1 50557 Insecta
2 7496 Pterygota
3 33340 Neoptera
4 33342 Paraneoptera
5 7524 Hemiptera
"""
if higher_than is not None and lower_than is not None:
raise ValueError('cannot specify "higher_than" and "lower_than" simultaneously')
idlist = "\n".join(map(str, ids))
if idlist == "":
warn("No input for pytaxonkit.filter", UserWarning)
return
arglist = ["taxonkit", "filter"]
if threads:
arglist.extend(("--threads", validate_threads(threads)))
if equal_to:
if isinstance(equal_to, (pylist, tuple)):
equal_to = ",".join(equal_to)
arglist.extend(["--equal-to", equal_to])
if higher_than:
arglist.extend(["--higher-than", higher_than])
if lower_than:
arglist.extend(["--lower-than", lower_than])
if discard_norank:
arglist.append("--discard-noranks")
if save_predictable:
arglist.append("--save-predictable-norank")
if discard_root: # pragma: no cover
arglist.append("--discard-root")
if blacklist:
arglist.extend(["--black-list", ",".join(blacklist)])
if root_taxid: # pragma: no cover
arglist.extend(["--root-taxid", str(root_taxid)])
if rank_file: # pragma: no cover
arglist.extend(["--rank-file", rank_file])
if debug:
log(*arglist)
proc = Popen(arglist, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate(input=idlist)
data = pd.read_csv(StringIO(out), header=None, names=["TaxID"], index_col=False)
return pylist(data.TaxID)
def list_ranks(rank_file=None, debug=False):
"""list all supported taxonomic ranks
Parameters
----------
rank_file : str, default None
Specify the location of the rank definition and order file; by default, taxonkit uses
`~/.taxonkit/ranks.txt`
debug : bool, default False
Print debugging output, e.g., system calls to `taxonkit`
Returns
-------
list
A list of taxonomic ranks.
>>> import pytaxonkit
>>> ranks = pytaxonkit.list_ranks()
>>> ranks[:5]
['life', 'acellular', ['root', 'cellular'], 'root', ['domain', 'empire', 'realm', 'superkingdom']]
""" # noqa: E501
arglist = ["taxonkit", "filter", "--list-order"]
if rank_file: # pragma: no cover
arglist.extend(["--rank-file", rank_file])
if debug:
log(*arglist)
proc = Popen(arglist, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate(input="")
ranks = pylist()
for line in out.strip().split():
rankvalue = line.split(",") if "," in line else line
ranks.append(rankvalue)
return ranks
def list_ranks_db(rank_file=None, debug=False):
"""list all taxonomic ranks present in the NCBI taxonomy database
Parameters
----------
rank_file : str, default None
Specify the location of the rank definition and order file; by default, taxonkit uses
`~/taxonkit/ranks.txt`
debug : bool, default False
Print debugging output, e.g., system calls to `taxonkit`
Returns
-------
list
A list of taxonomic ranks.
>>> import pytaxonkit
>>> ranks = pytaxonkit.list_ranks_db()
>>> ranks[:5]
['acellular root', 'cellular root', 'domain', 'realm', 'kingdom']
"""
arglist = ["taxonkit", "filter", "--list-ranks"]
if rank_file: # pragma: no cover
arglist.extend(["--rank-file", rank_file])
if debug:
log(*arglist)
proc = Popen(arglist, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
out, err = proc.communicate(input="")
data = pd.read_csv(StringIO(out), header=None, names=["Rank"], index_col=False)
return pylist(data.Rank)
@pytest.mark.parametrize(
"discard_norank,exp_result",
[
(True, [131567, 2759, 33208, 6656, 6960, 50557, 7496, 33340, 33392, 7399]),
(
False,
[
131567,
2759,
33154,
33208,
6072,
33213,
33317,
1206794,
88770,
6656,
197563,
197562,
6960,
50557,
85512,
7496,
33340,
33392,
7399,
],
),
],
)
def test_filter_higher_than(discard_norank, exp_result):
taxids = [
131567,
2759,
33154,
33208,
6072,
33213,
33317,
1206794,
88770,
6656,
197563,
197562,
6960,
50557,
85512,
7496,
33340,
33392,
7399,
7400,
7434,
34735,
7458,
70987,
83322,
481579,
2056706,
599582,
]
obs_result = filter(
taxids, threads=1, higher_than="order", equal_to="order", discard_norank=discard_norank
)
print(obs_result)
assert obs_result == exp_result
def test_filter_lower_than(capsys):
taxids = [
131567,
2759,
33154,
33208,
6072,
33213,
33317,
1206794,
88770,
6656,
197563,
197562,
6960,
50557,
85512,
7496,
33340,
33392,
7041,
41071,
535382,
41073,
706613,
586004,
87479,
412111,
]
obs_result = filter(taxids, lower_than="family", discard_norank=True, debug=True)
exp_result = [706613, 586004, 87479, 412111]
assert obs_result == exp_result
terminal = capsys.readouterr()
assert "taxonkit filter --lower-than family" in terminal.err
def test_filter_equal_to_multi(capsys):
taxids = [
131567,
2759,
33154,
33208,
6072,
33213,