Coverage for teiphy/format.py: 100.00%

21 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-01-15 16:06 +0000

1from enum import Enum 

2 

3 

4class FormatUnknownException(Exception): 

5 pass 

6 

7 

8class Format(Enum): 

9 NEXUS = 'NEXUS' 

10 HENNIG86 = 'HENNIG86' 

11 PHYLIP = 'PHYLIP' 

12 FASTA = 'FASTA' 

13 BEAST = 'BEAST' 

14 CSV = 'CSV' 

15 TSV = 'TSV' 

16 EXCEL = 'EXCEL' 

17 STEMMA = 'STEMMA' 

18 

19 @classmethod 

20 def infer(cls, suffix: str): 

21 suffix_map = { 

22 ".nex": cls.NEXUS, 

23 ".nexus": cls.NEXUS, 

24 ".nxs": cls.NEXUS, 

25 ".tnt": cls.HENNIG86, 

26 ".ph": cls.PHYLIP, 

27 ".phy": cls.PHYLIP, 

28 ".fa": cls.FASTA, 

29 ".fasta": cls.FASTA, 

30 ".xml": cls.BEAST, 

31 ".csv": cls.CSV, 

32 ".tsv": cls.TSV, 

33 ".xlsx": cls.EXCEL, 

34 } 

35 

36 suffix_lower = suffix.lower() 

37 if suffix_lower in suffix_map: 

38 return suffix_map[suffix_lower] 

39 

40 allowed_suffixes = ', '.join(suffix_map.keys()) 

41 raise FormatUnknownException( 

42 f"Cannot infer format from suffix '{suffix}'. " f"Please set explicitly or use one of: {allowed_suffixes}." 

43 )