gtdblib.taxonomy package
Submodules
gtdblib.taxonomy.taxonomy module
- class gtdblib.taxonomy.taxonomy.Taxonomy(d: Taxon, p: Taxon, c: Taxon, o: Taxon, f: Taxon, g: Taxon, s: Taxon)
Bases:
object- CLASS_INDEX = 2
- DOMAIN_INDEX = 0
- FAMILY_INDEX = 4
- GENUS_INDEX = 5
- ORDER_INDEX = 3
- PHYLUM_INDEX = 1
- RANK_INDEX = {'c__': 2, 'd__': 0, 'f__': 4, 'g__': 5, 'o__': 3, 'p__': 1, 's__': 6}
- RANK_LABELS = ('domain', 'phylum', 'class', 'order', 'family', 'genus', 'species')
- RANK_PREFIXES = ('d__', 'p__', 'c__', 'o__', 'f__', 'g__', 's__')
- SPECIES_INDEX = 6
- c
- d
- f
- g
- o
- p
- s
- gtdblib.taxonomy.taxonomy.check_full_taxonomy(tax_str: str) bool
Check if a taxonomy string contains exactly the 7 canonical ranks in order.
Problems with the taxonomy string are logged as errors.
- Parameters:
tax_str – Taxonomy string (e.g. d__Bacteria;p__;…;s__).
- Returns:
True if the taxonomy string is complete, else False.
- gtdblib.taxonomy.taxonomy.extant_taxa(taxonomy: Dict[str, List[str]]) Dict[str, Set[str]]
Get the extant taxa (e.g. genomes) in each named group at all ranks.
- Parameters:
taxonomy – Mapping from accessions to a list of taxa for the 7 canonical ranks.
- Returns:
Mapping from each named taxon to the accessions it contains.
- gtdblib.taxonomy.taxonomy.extant_taxa_for_rank(rank_label: str, taxonomy: Dict[str, List[str]]) Dict[str, Set[str]]
Get the extant taxa (e.g. genomes) in each named group at a rank.
- Parameters:
rank_label – Rank of interest (e.g. ‘class’).
taxonomy – Mapping from accessions to a list of taxa for the 7 canonical ranks.
- Returns:
Mapping from each named taxon at the rank to the accessions it contains.
- gtdblib.taxonomy.taxonomy.fill_missing_ranks(taxa: List[str], warning: bool = True) List[str]
Fill in missing internal and trailing ranks.
Taxa are assumed to be in rank order, but some ranks may be missing or repeated (e.g. [d__Bacteria, p__Firmicutes, p__Firmicutes_A, f__Bacillaceae]).
Example: [d__Bacteria, c__Bacilli, g__Bacillus] => [d__Bacteria, p__, c__Bacilli, o__, f__, g__Bacillus, s__]
- Parameters:
taxa – List of taxa.
warning – Flag indicating if a warning should be logged for taxa that are not in canonical rank order.
- Returns:
List of taxa with all missing ranks filled in, or the original taxa if they are not in canonical rank order.
- gtdblib.taxonomy.taxonomy.fill_trailing_ranks(taxa: List[str]) List[str]
Fill in missing trailing ranks in a taxonomy string.
This function assumes it will be provided with a list of taxa from the 7 canonical ranks in rank order. Any trailing ranks are filled in. The input list is not modified.
Example: [d__Bacteria, p__Firmicutes] => [d__Bacteria, p__Firmicutes, c__, o__, f__, g__, s__]
- Parameters:
taxa – List of taxa.
- Raises:
GtdbLibException – If the last taxon is missing a rank prefix.
- Returns:
List of taxa with filled trailing ranks.
- gtdblib.taxonomy.taxonomy.read_taxonomy(taxonomy_file: str, use_canonical_gid: bool = False) Dict[str, List[str]]
Read Greengenes-style taxonomy file.
This method is generic in that it can read any Greengenes-style taxonomy string and does not strictly require there to be the seven canonical ranks present.
- Expected format is:
<accession> <taxonomy string>
- where the taxonomy string will typically have the formats:
- Parameters:
taxonomy_file – File indicating Greengenes-style taxonomic assignments.
use_canonical_gid – Flag indicating if accessions should be converted to their canonical form.
- Returns:
Mapping from accessions to a list of taxa.
- gtdblib.taxonomy.taxonomy.read_taxonomy_from_tree(tree: str, warnings: bool = True) Dict[str, List[str]]
Obtain the taxonomy for each extant taxa as specified by internal tree labels.
This method is generic in that it can read any Greengenes-style taxonomy string and does not strictly require extant taxa to be classified to the seven canonical ranks.
- Parameters:
tree – Filename of Newick tree or Dendropy tree object.
warnings – Flag indicating if issues reading tree should be logged as warnings.
- Returns:
Mapping from extant taxon labels to a list of taxa.
- gtdblib.taxonomy.taxonomy.sort_taxa(taxa: Iterable[str], reverse: bool = False) List[str]
Sort taxa by rank (domain to species) and then alphabetically.
- Parameters:
taxa – Taxa to sort, each with a rank prefix (e.g. p__Firmicutes).
reverse – Flag indicating if the order should be reversed.
- Returns:
Sorted list of taxa. Taxa without a canonical rank prefix are omitted.
- gtdblib.taxonomy.taxonomy.write_taxonomy(taxonomy: Dict[str, List[str]], output_file: str)
Write Greengenes-style taxonomy file.
- Parameters:
taxonomy – Mapping from accessions to a list of taxa.
output_file – File to write taxonomy to.
gtdblib.taxonomy.validation module
Methods for validating a Greengenes-style taxonomy.
- This module validates canonical 7 rank Greengenes-style taxonomy strings:
- gtdblib.taxonomy.validation.duplicate_names(taxonomy: Dict[str, List[str]], check_species: bool = True) Dict[str, List[str]]
Identify duplicate names in taxonomy.
Parameters
- taxonomyd[unique_id] -> [d__<taxon>; …; s__<taxon>]
Taxonomy strings indexed by unique ids.
Returns
- dictd[taxon] -> lineages
List of lineages for duplicate taxa.
- gtdblib.taxonomy.validation.taxonomic_consistency(taxonomy: Dict[str, List[str]], report_errors: bool = True) Dict[str, str]
Determine taxonomically consistent classification for taxa at each rank.
Parameters
- taxonomyd[unique_id] -> [d__<taxon>; …; s__<taxon>]
Taxonomy strings indexed by unique ids.
- report_errorsboolean
Flag indicating if errors should be written to screen.
Returns
- dictd[taxa] -> expected parent
Expected parent taxon for taxa at all taxonomic ranks. If the taxonomy is inconsistent, the parent indicated by the last processed genome is used.
- gtdblib.taxonomy.validation.validate_species_name(species_name: str, require_full: bool = True, require_prefix: bool = True) Tuple[bool, Optional[str]]
Validate species name.
A full species name should be binomial and include a ‘generic name’ (genus) and a ‘specific epithet’ (species), i.e. Escherichia coli. This method assumes the two names should be separated by a space.
Parameters
- species_namestr
Species name to validate
- require_fullboolean
Flag indicating if species name must include ‘generic name’ and ‘specific epithet’.
- require_prefixboolean
Flag indicating if name must start with the species prefix (’s__’).
Returns
- boolean
True if species name is valid, otherwise False.
- str
Reason for failing validation, otherwise None.
- gtdblib.taxonomy.validation.validate_taxonomy(taxonomy: Dict[str, List[str]], check_prefixes: bool, check_ranks: bool, check_hierarchy: bool, check_species: bool, check_group_names: bool, check_duplicate_names: bool, check_capitalization: bool, report_errors: bool = True) Tuple[Dict[str, str], Dict[str, List[str]], Dict[str, List[str]], Dict[str, Set[str]], Dict[str, List[str]], Set[str]]
Check if taxonomy forms a strict hierarchy with all expected ranks.
This method implements a full workflow for validating Greengenes-style taxonomy strings assigned to a set of accessions. It identifies a number of common issues and the issues checked can be configured using the set of boolean inputs.
Parameters
- taxonomyd[unique_id] -> [d__<taxon>; …; s__<taxon>]
Taxonomy strings indexed by unique ids.
- check_prefixesboolean
Flag indicating if prefix of taxon should be validated.
- check_ranksboolean
Flag indicating if the presence of all ranks should be validated.
- check_hierarchyboolean
Flag indicating if the taxonomic hierarchy should be validated.
- check_speciesboolean
Flag indicating if the taxonomic consistency of named species should be validated.
- check_group_namesboolean
Flag indicating if group names should be checked for invalid characters.
- check_duplicate_namesboolean
Flag indicating if group names should be checked for duplicates.
- check_capitalizationboolean
Flag indicating if taxon names should be checked for starting with a capital letter.
- report_errorsboolean
Flag indicating if errors should be written to screen.
Returns
- dictd[taxon_id] -> taxonomy
Taxa with invalid number of ranks.
- dictd[taxon_id] -> [taxon, taxonomy]
Taxa with invalid rank prefixes.
- dict: d[taxon_id] -> [species name, error message]
Taxa with invalid species names.
- dict: d[child_taxon] -> two or more parent taxa
Taxa with invalid hierarchies.
- dict: d[taxon_id] -> [taxon, error message]
Taxa with invalid characters in their names.
- set
Taxa that do not start with a capital letter.