|
| 1 | +import tskit |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +import argparse |
| 5 | + |
| 6 | +def compute_pairwise_coalescence_time(ts, leaf_index, query_index, s): |
| 7 | + windows = np.arange(0, ts.sequence_length, s) |
| 8 | + windows = np.append(windows, ts.sequence_length) |
| 9 | + times = ts.diversity(sample_sets = [leaf_index, query_index], windows=windows, mode='branch')/2 |
| 10 | + return times |
| 11 | + |
| 12 | +def compute_all_pairwise_coalescence_times(ts, leaf_index, s): |
| 13 | + windows = np.arange(0, ts.sequence_length, s) |
| 14 | + n = len(windows) |
| 15 | + m = ts.num_samples - 1 |
| 16 | + all_coalescence_times = np.zeros((n, m)) |
| 17 | + index = 0 |
| 18 | + for i in range(0, ts.num_samples): |
| 19 | + if i != leaf_index: |
| 20 | + print(i) |
| 21 | + all_coalescence_times[:, index] = compute_pairwise_coalescence_time(ts, leaf_index, i, s) |
| 22 | + index += 1 |
| 23 | + return all_coalescence_times |
| 24 | + |
| 25 | +def main(): |
| 26 | + parser = argparse.ArgumentParser(description="Calculate pairwise coalescence times for a given leaf node.") |
| 27 | + parser.add_argument("--trees_file", type=str, required=True, help="Path to the tree sequence file.") |
| 28 | + parser.add_argument("--leaf_index", type=int, required=True, help="Index of the leaf node.") |
| 29 | + parser.add_argument("--interval_size", type=int, required=True, help="Size of the interval.") |
| 30 | + parser.add_argument("--output_file", type=str, required=True, help="Output filename.") |
| 31 | + |
| 32 | + args = parser.parse_args() |
| 33 | + |
| 34 | + ts = tskit.load(args.trees_file) |
| 35 | + leaf_index = args.leaf_index |
| 36 | + interval_size = args.interval_size |
| 37 | + output_file = args.output_file |
| 38 | + |
| 39 | + all_coalescence_times = compute_all_pairwise_coalescence_times(ts, leaf_index, interval_size) |
| 40 | + np.savetxt(output_file, all_coalescence_times, delimiter=",") |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + main() |
0 commit comments