augur.utils module

exception augur.utils.AugurException

Bases: Exception

exception augur.utils.InvalidTreeError

Bases: Exception

Represents an error loading a phylogenetic tree from a filename.

augur.utils.ambiguous_date_to_date_range(uncertain_date, fmt, min_max_year=None)

Annotate each node in the given tree with its parent.

>>> import io
>>> tree ="(A, (B, C))"), "newick")
>>> not any([hasattr(node, "parent") for node in tree.find_clades()])
>>> tree = annotate_parents_for_tree(tree)
>>> tree.root.parent is None
>>> all([hasattr(node, "parent") for node in tree.find_clades()])
augur.utils.available_cpu_cores(fallback: int = 1) → int

Returns the number (an int) of CPU cores available to this process, if determinable, otherwise the number of CPU cores available to the computer, if determinable, otherwise the fallback number (which defaults to 1).


Returns the first line of the given text, ignoring leading and trailing whitespace.


Returns a string of the current augur version.

augur.utils.get_json_name(args, default=None)
augur.utils.get_numerical_dates(meta_dict, name_col=None, date_col='date', fmt=None, min_max_year=None)

Return dictionary mapping child node names to parent node names

augur.utils.is_date_ambiguous(date, ambiguous_by='any')

Returns whether a given date string in the format of YYYY-MM-DD is ambiguous by a given part of the date (e.g., day, month, year, or any parts).

  • date (str) – Date string in the format of YYYY-MM-DD

  • ambiguous_by (str) – Field of the date string to test for ambiguity (“day”, “month”, “year”, “any”)


Convenience method to check if a file is a vcf file.

>>> is_vcf("./foo")
>>> is_vcf("./foo.vcf")
>>> is_vcf("./foo.vcf.GZ")
augur.utils.json_to_tree(json_dict, root=True)

Returns a Bio.Phylo tree corresponding to the given JSON dictionary exported by tree_to_json.

Assigns links back to parent nodes for the root of the tree.

Test opening a JSON from augur export v1.

>>> import json
>>> json_fh = open("tests/data/json_tree_to_nexus/flu_h3n2_ha_3y_tree.json", "r")
>>> json_dict = json.load(json_fh)
>>> tree = json_to_tree(json_dict)
>>> len(tree.clades)
>>> tree.clades[0].name
>>> hasattr(tree, "attr")
>>> "dTiter" in tree.attr
>>> tree.clades[0]
>>> tree.clades[0].branch_length > 0

Test opening a JSON from augur export v2.

>>> json_fh = open("tests/data/zika.json", "r")
>>> json_dict = json.load(json_fh)
>>> tree = json_to_tree(json_dict)
>>> hasattr(tree, "name")
>>> len(tree.clades) > 0
>>> tree.clades[0].branch_length > 0
augur.utils.load_features(reference, feature_names=None)

Load masking sites from either a BED file or a masking file.


mask_file (str) – Path to the BED or masking file


Sorted list of unique zero-indexed sites

Return type


augur.utils.myopen(fname, mode)

Argument value validation and casting function for –nthreads.

augur.utils.open_file(fname, mode)

Open a file using either or open() depending on file name. Semantics identical to open()


Read a BED file and return a list of excluded sites.

Note: This function assumes the given file is a BED file. On parsing failures, it will attempt to skip the first line and retry, but no other error checking is attempted. Incorrectly formatted files will raise errors.

  • bed_file (str) – Path to the BED file

  • Returns

  • --------

  • list[int] – Sorted list of unique zero-indexed sites

augur.utils.read_colors(overrides=None, use_defaults=True)
augur.utils.read_lat_longs(overrides=None, use_defaults=True)

Read a masking file and return a list of excluded sites.

Masking files have a single masking site per line, either alone or as the second column of a tab-separated file. These sites are assumed to be one-indexed, NOT zero-indexed. Incorrectly formatted lines will be skipped.

  • mask_file (str) – Path to the masking file

  • Returns

  • --------

  • list[int] – Sorted list of unique zero-indexed sites

augur.utils.read_metadata(fname, query=None)
augur.utils.read_node_data(fnames, tree=None)
augur.utils.read_strains(*files, comment_char='#')

Reads strain names from one or more plain text files and returns the set of distinct strains.

Strain names can be commented with full-line or inline comments. For example, the following is a valid strain names file:

# this is a comment at the top of the file strain1 # exclude strain1 because it isn’t sequenced properly strain2

# this is an empty line that will be ignored.


files (one or more str) – one or more names of text files with one strain name per line


strain names from the given input files

Return type


augur.utils.read_tree(fname, min_terminals=3)

Safely load a tree from a given filename or raise an error if the file does not contain a valid tree.

  • fname (str) – name of a file containing a phylogenetic tree

  • min_terminals (int) – minimum number of terminals required for the parsed tree as a sanity check on the tree


InvalidTreeError – If the given file exists but does not seem to contain a valid tree format.


BioPython tree instance

Return type


augur.utils.run_shell_command(cmd, raise_errors=False, extra_env=None)

Run the given command string via Bash with error checking.

Returns True if the command exits normally. Returns False if the command exits with failure and “raise_errors” is False (the default). When “raise_errors” is True, exceptions are rethrown.

If an extra_env mapping is passed, the provided keys and values are overlayed onto the default subprocess environment.

augur.utils.write_VCF_translation(prot_dict, vcf_file_name, ref_file_name)

Writes out a VCF-style file (which seems to be minimally handleable by vcftools and pyvcf) of the AA differences between sequences and the reference. This is a similar format created/used by read_in_vcf except that there is one of these dicts (with sequences, reference, positions) for EACH gene.

Also writes out a fasta of the reference alignment.

EBH 12 Dec 2017

augur.utils.write_json(data, file_name, indent=2, include_version=True)

Write data as JSON to the given file_name, creating parent directories if necessary. The augur version is included as a top-level key “augur_version”.

  • data (dict) – data to write out to JSON

  • file_name (str) – file name to write to

  • indent (int or None, optional) – JSON indentation level. Default is None if the environment variable AUGUR_MINIFY_JSON is truthy, else 1

  • include_version (bool, optional) – Include the augur version. Default: True.