Skip to content

Commit 53f040c

Browse files
Add list-dates with counts CLI command
1 parent 0691436 commit 53f040c

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

sc2ts/cli.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,27 @@ def initialise(ts, match_db, additional_problematic_sites, verbose, log_file):
213213
inference.MatchDb.initialise(match_db)
214214

215215

216+
@click.command()
217+
@click.argument("metadata", type=click.Path(exists=True, dir_okay=False))
218+
@click.option("--counts/--no-counts", default=False)
219+
@click.option("-v", "--verbose", count=True)
220+
@click.option("-l", "--log-file", default=None, type=click.Path(dir_okay=False))
221+
def list_dates(metadata, counts, verbose, log_file):
222+
"""
223+
List the dates included in specified metadataDB
224+
"""
225+
setup_logging(verbose, log_file)
226+
with sc2ts.MetadataDb(metadata) as metadata_db:
227+
counter = metadata_db.date_sample_counts()
228+
if counts:
229+
for k, v in counter.items():
230+
print(k, v, sep="\t")
231+
232+
else:
233+
for k in counter:
234+
print(k)
235+
236+
216237
@click.command()
217238
@click.argument("alignments", type=click.Path(exists=True, dir_okay=False))
218239
@click.argument("metadata", type=click.Path(exists=True, dir_okay=False))
@@ -509,6 +530,7 @@ def cli():
509530
cli.add_command(export_metadata)
510531

511532
cli.add_command(initialise)
533+
cli.add_command(list_dates)
512534
cli.add_command(daily_extend)
513535
cli.add_command(validate)
514536
cli.add_command(annotate_recombinants)

sc2ts/metadata.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self, path):
1919
uri = f"file:{path}"
2020
uri += "?mode=ro"
2121
self.uri = uri
22+
self.path = path
2223
self.conn = sqlite3.connect(uri, uri=True)
2324
self.conn.row_factory = dict_factory
2425

@@ -73,6 +74,14 @@ def get(self, date):
7374
for row in self.conn.execute(sql, [date]):
7475
yield row
7576

77+
def date_sample_counts(self):
78+
sql = "SELECT date, COUNT(*) FROM samples GROUP BY date ORDER BY date;"
79+
counts = collections.Counter()
80+
with self.conn:
81+
for row in self.conn.execute(sql):
82+
counts[row["date"]] = row["COUNT(*)"]
83+
return counts
84+
7685
def get_days(self, date=None):
7786
if date is None:
7887
date = "2000-01-01"

tests/test_cli.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,67 @@ def test_additional_problematic_sites(self, tmp_path, additional):
4444
other_ts.tables.assert_equals(ts.tables)
4545
match_db = sc2ts.MatchDb(match_db_path)
4646
assert len(match_db) == 0
47+
48+
49+
class TestListDates:
50+
def test_defaults(self, metadata_db):
51+
runner = ct.CliRunner(mix_stderr=False)
52+
result = runner.invoke(
53+
cli.cli,
54+
f"list-dates {metadata_db.path}",
55+
catch_exceptions=False,
56+
)
57+
assert result.exit_code == 0
58+
assert result.stdout.splitlines() == [
59+
"2020-01-01",
60+
"2020-01-19",
61+
"2020-01-24",
62+
"2020-01-25",
63+
"2020-01-28",
64+
"2020-01-29",
65+
"2020-01-30",
66+
"2020-01-31",
67+
"2020-02-01",
68+
"2020-02-02",
69+
"2020-02-03",
70+
"2020-02-04",
71+
"2020-02-05",
72+
"2020-02-06",
73+
"2020-02-07",
74+
"2020-02-08",
75+
"2020-02-09",
76+
"2020-02-10",
77+
"2020-02-11",
78+
"2020-02-13",
79+
]
80+
81+
def test_counts(self, metadata_db):
82+
runner = ct.CliRunner(mix_stderr=False)
83+
result = runner.invoke(
84+
cli.cli,
85+
f"list-dates {metadata_db.path} --counts",
86+
catch_exceptions=False,
87+
)
88+
assert result.exit_code == 0
89+
assert result.stdout.splitlines() == [
90+
"2020-01-01\t1",
91+
"2020-01-19\t1",
92+
"2020-01-24\t2",
93+
"2020-01-25\t3",
94+
"2020-01-28\t2",
95+
"2020-01-29\t4",
96+
"2020-01-30\t5",
97+
"2020-01-31\t1",
98+
"2020-02-01\t5",
99+
"2020-02-02\t5",
100+
"2020-02-03\t2",
101+
"2020-02-04\t5",
102+
"2020-02-05\t1",
103+
"2020-02-06\t3",
104+
"2020-02-07\t2",
105+
"2020-02-08\t4",
106+
"2020-02-09\t2",
107+
"2020-02-10\t2",
108+
"2020-02-11\t2",
109+
"2020-02-13\t4",
110+
]

0 commit comments

Comments
 (0)