Coverage for src/python/ensembl/ncbi_taxonomy/models.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.15.1, created at 2026-07-13 09:48 +0000

1# See the NOTICE file distributed with this work for additional information 

2# regarding copyright ownership. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14"""NCBI Taxonomy database ORM.""" 

15 

16# Ignore some pylint and mypy checks due to the nature of SQLAlchemy ORMs 

17# pylint: disable=missing-class-docstring 

18# mypy: disable-error-code="misc, valid-type" 

19 

20from sqlalchemy import ( 

21 Column, 

22 ForeignKey, 

23 join, 

24) 

25from sqlalchemy.dialects.mysql import ( 

26 INTEGER, 

27 TINYINT, 

28 VARCHAR, 

29 CHAR, 

30) 

31from sqlalchemy.orm import ( 

32 relationship, 

33 column_property, 

34) 

35from sqlalchemy.orm import declarative_base 

36 

37Base = declarative_base() 

38 

39 

40class NCBITaxaNode(Base): 

41 __tablename__ = "ncbi_taxa_node" 

42 

43 taxon_id = Column(INTEGER(10), primary_key=True) 

44 parent_id = Column(INTEGER(10), ForeignKey("ncbi_taxa_node.taxon_id"), nullable=False, index=True) 

45 rank = Column(CHAR(32), nullable=False, index=True) 

46 genbank_hidden_flag = Column(TINYINT(1), nullable=False, default=0) 

47 left_index = Column(INTEGER(10), nullable=False, default=0, index=True) 

48 right_index = Column(INTEGER(10), nullable=False, default=0, index=True) 

49 root_id = Column(INTEGER(10), nullable=False, default=1) 

50 

51 parent = relationship("NCBITaxaNode", remote_side=[taxon_id]) 

52 children = relationship("NCBITaxaName") 

53 

54 

55class NCBITaxaName(Base): 

56 __tablename__ = "ncbi_taxa_name" 

57 

58 taxon_id = Column(INTEGER(10), ForeignKey("ncbi_taxa_node.taxon_id"), index=True, primary_key=True) 

59 name = Column(VARCHAR(500), index=True, primary_key=True) 

60 name_class = Column(VARCHAR(50), nullable=False, index=True) 

61 

62 

63class NCBITaxonomy(Base): 

64 ncbi_taxa_name_table = NCBITaxaName.__table__ 

65 ncbi_taxa_node_table = NCBITaxaNode.__table__ 

66 

67 name_node_join = join(ncbi_taxa_name_table, ncbi_taxa_node_table) 

68 

69 __table__ = name_node_join 

70 

71 taxon_id = column_property(ncbi_taxa_name_table.c.taxon_id, ncbi_taxa_node_table.c.taxon_id)