Skip to content

Commit

Permalink
Use date_parsing for frequencies --min-date/--max-date
Browse files Browse the repository at this point in the history
Reasons:

1. Sync-up with new filter --min-date/--max-date features (support for relative dates)
2. Less code duplication
  • Loading branch information
victorlin committed Apr 12, 2022
1 parent aebf816 commit 5df2749
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions augur/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
infer frequencies of mutations or clades
"""
import json, os, sys
import datetime
import numpy as np
from collections import defaultdict
from Bio import Phylo, AlignIO
from Bio.Align import MultipleSeqAlignment
import treetime.utils

from .frequency_estimators import get_pivots, alignment_frequencies, tree_frequencies
from .frequency_estimators import AlignmentKdeFrequencies, TreeKdeFrequencies, TreeKdeFrequenciesError
from .utils import date_parsing
from .utils import read_metadata, read_node_data, write_json, get_numerical_dates


Expand All @@ -26,10 +25,10 @@ def register_arguments(parser):
help="number of units between pivots")
parser.add_argument("--pivot-interval-units", type=str, default="months", choices=['months', 'weeks'],
help="space pivots by months (default) or by weeks")
parser.add_argument('--min-date', type=numeric_date,
help="date to begin frequencies calculations; may be specified as an Augur-style numeric date (with the year as the integer part) or YYYY-MM-DD")
parser.add_argument('--max-date', type=numeric_date,
help="date to end frequencies calculations; may be specified as an Augur-style numeric date (with the year as the integer part) or YYYY-MM-DD")
parser.add_argument('--min-date', type=date_parsing.numeric_date,
help=f"date to begin frequencies calculations; may be specified as: {date_parsing.SUPPORTED_DATE_HELP_TEXT}")
parser.add_argument('--max-date', type=date_parsing.numeric_date,
help=f"date to end frequencies calculations; may be specified as: {date_parsing.SUPPORTED_DATE_HELP_TEXT}")

# Tree-specific arguments
parser.add_argument('--tree', '-t', type=str,
Expand Down Expand Up @@ -230,19 +229,3 @@ def run(args):

write_json(frequencies, args.output)
print("mutation frequencies written to", args.output, file=sys.stdout)


def numeric_date(date):
"""
Converts the given *date* string to a :py:class:`float`.
*date* may be given as a number (a float) with year as the integer part, or
in the YYYY-MM-DD (ISO 8601) syntax.
>>> numeric_date("2020.42")
2020.42
>>> numeric_date("2020-06-04")
2020.42486...
"""
try:
return float(date)
except ValueError:
return treetime.utils.numeric_date(datetime.date(*map(int, date.split("-", 2))))

0 comments on commit 5df2749

Please sign in to comment.