Skip to content

Commit b063c32

Browse files
committed
attempt at switch for calculating population frequencies
1 parent ba65e4f commit b063c32

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

tests/test_data_model.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def check_ts(self, ts):
211211
C1 = compute_mutation_counts(ts)
212212
C2 = model.compute_population_mutation_counts(ts)
213213
nt.assert_array_equal(C1, C2)
214-
tsm = model.TSModel(ts)
214+
tsm = model.TSModel(ts, calc_population_frequencies=True)
215215
df = tsm.mutations_df
216216
nt.assert_array_equal(df["pop_A_freq"], C1[0] / ts.num_samples)
217217
nt.assert_array_equal(df["pop_B_freq"], C1[1] / ts.num_samples)
@@ -242,7 +242,7 @@ def test_no_metadata_schema(self):
242242
def test_no_populations(self):
243243
tables = single_tree_example_ts().dump_tables()
244244
tables.populations.add_row(b"{}")
245-
tsm = model.TSModel(tables.tree_sequence())
245+
tsm = model.TSModel(tables.tree_sequence(), calc_population_frequencies=True)
246246
with pytest.raises(ValueError, match="must be assigned to populations"):
247247
tsm.mutations_df
248248

tsqc/__main__.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
logger = daiquiri.getLogger("tsqc")
2222

2323

24-
def load_data(path):
24+
def load_data(path, calc_population_frequencies):
2525
logger.info(f"Loading {path}")
2626
try:
2727
ts = tskit.load(path)
28+
2829
except tskit.FileFormatError:
2930
ts = tszip.decompress(path)
3031

31-
tsm = model.TSModel(ts, path.name)
32+
tsm = model.TSModel(ts, calc_population_frequencies, path.name)
3233
return tsm
3334

3435

@@ -152,13 +153,26 @@ def setup_logging(log_level, no_log_filter):
152153
is_flag=True,
153154
help="Do not filter the output log (advanced debugging only)",
154155
)
155-
def main(path, port, show, log_level, no_log_filter, annotations_file):
156+
@click.option(
157+
"--calc-population-frequencies",
158+
default=False,
159+
help="Calculate population frequencies for sample nodes",
160+
)
161+
def main(
162+
path,
163+
port,
164+
show,
165+
log_level,
166+
no_log_filter,
167+
annotations_file,
168+
calc_population_frequencies,
169+
):
156170
"""
157171
Run the tsqc server.
158172
"""
159173
setup_logging(log_level, no_log_filter)
160174

161-
tsm = load_data(pathlib.Path(path))
175+
tsm = load_data(pathlib.Path(path), calc_population_frequencies)
162176
if annotations_file:
163177
config.ANNOTATIONS_FILE = annotations_file
164178

tsqc/model.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class TSModel:
356356
convenience methods for analysing the tree sequence.
357357
"""
358358

359-
def __init__(self, ts, name=None):
359+
def __init__(self, ts, calc_population_frequencies=False, name=None):
360360
self.ts = ts
361361
self.name = name
362362

@@ -366,6 +366,7 @@ def __init__(self, ts, name=None):
366366
self.nodes_num_mutations = np.bincount(
367367
self.ts.mutations_node, minlength=self.ts.num_nodes
368368
)
369+
self.calc_population_frequencies = calc_population_frequencies
369370

370371
@property
371372
def file_uuid(self):
@@ -456,7 +457,7 @@ def mutations_df(self):
456457
self.mutations_inherited_state = inherited_state
457458

458459
population_data = {}
459-
if ts.num_populations > 0:
460+
if ts.num_populations > 0 and self.calc_population_frequencies:
460461
pop_mutation_count = compute_population_mutation_counts(ts)
461462
for pop in ts.populations():
462463
name = f"pop{pop.id}"

0 commit comments

Comments
 (0)