-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ancestor lengths in genome and time bins #68
Open
savitakartik
wants to merge
4
commits into
tskit-dev:main
Choose a base branch
from
savitakartik:ancestor_span_heatmap_v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb483e2
Added child_left, child_right columns to nodes_df and tests for these.
savitakartik e86158c
int slider removed, edge case fixed, bin count logged, actual node co…
savitakartik 47536db
naively handling case for heatmap where time units are uncalibrated.
savitakartik cb74ebc
incremented nodes cache version
savitakartik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,7 +436,7 @@ def edges_df(self): | |
) | ||
|
||
@cached_property | ||
@disk_cache("v1") | ||
@disk_cache("v2") | ||
def nodes_df(self): | ||
ts = self.ts | ||
child_left, child_right = self.child_bounds( | ||
|
@@ -449,6 +449,10 @@ def nodes_df(self): | |
"time": ts.nodes_time, | ||
"num_mutations": self.nodes_num_mutations, | ||
"ancestors_span": child_right - child_left, | ||
"child_left": child_left, # FIXME add test for this | ||
"child_right": child_right, # FIXME add test for this | ||
"child_left": child_left, # FIXME add test for this | ||
"child_right": child_right, # FIXME add test for this | ||
"is_sample": is_sample, | ||
} | ||
) | ||
|
@@ -458,6 +462,8 @@ def nodes_df(self): | |
"time": "float64", | ||
"num_mutations": "int", | ||
"ancestors_span": "float64", | ||
"child_left": "float64", | ||
"child_right": "float64", | ||
"is_sample": "bool", | ||
} | ||
) | ||
|
@@ -584,3 +590,62 @@ def calc_mutations_per_tree(self): | |
mutations_per_tree = np.zeros(self.ts.num_trees, dtype=np.int64) | ||
mutations_per_tree[unique_values] = counts | ||
return mutations_per_tree | ||
|
||
def compute_ancestor_spans_heatmap_data(self, num_x_bins, num_y_bins): | ||
""" | ||
Calculates the average ancestor span in a genomic-time window | ||
""" | ||
if self.ts.time_units == tskit.TIME_UNITS_UNCALIBRATED: | ||
logger.warning( | ||
"Cannot compute ancestor spans for uncalibrated tree sequence" | ||
) | ||
return pd.DataFrame( | ||
{ | ||
"position": [], | ||
"time": [], | ||
"overlapping_node_count_log10": [], | ||
"overlapping_node_count": [], | ||
} | ||
) | ||
else: | ||
nodes_df = self.nodes_df[self.nodes_df.ancestors_span != -np.inf] | ||
nodes_df = nodes_df.reset_index(drop=True) | ||
nodes_left = nodes_df.child_left | ||
nodes_right = nodes_df.child_right | ||
nodes_time = nodes_df.time | ||
|
||
x_bins = np.linspace(nodes_left.min(), nodes_right.max(), num_x_bins + 1) | ||
y_bins = np.linspace(0, nodes_time.max(), num_y_bins + 1) | ||
heatmap_counts = np.zeros((num_x_bins, num_y_bins)) | ||
|
||
x_starts = np.digitize(nodes_left, x_bins, right=True) | ||
x_ends = np.digitize(nodes_right, x_bins, right=True) | ||
y_starts = np.digitize(nodes_time, y_bins, right=True) | ||
|
||
for u in range(len(nodes_left)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably slow - should we do it with numba? |
||
x_start = max(0, x_starts[u] - 1) | ||
x_end = max(0, x_ends[u] - 1) | ||
y_bin = max(0, y_starts[u] - 1) | ||
heatmap_counts[x_start : x_end + 1, y_bin] += 1 | ||
|
||
x_coords = np.repeat(x_bins[:-1], num_y_bins) | ||
y_coords = np.tile(y_bins[:-1], num_x_bins) | ||
overlapping_node_count = heatmap_counts.flatten() | ||
overlapping_node_count[overlapping_node_count == 0] = 1 | ||
# FIXME - better way to avoid log 0 above? | ||
df = pd.DataFrame( | ||
{ | ||
"position": x_coords.flatten(), | ||
"time": y_coords.flatten(), | ||
"overlapping_node_count_log10": np.log10(overlapping_node_count), | ||
"overlapping_node_count": overlapping_node_count, | ||
} | ||
) | ||
return df.astype( | ||
{ | ||
"position": "int", | ||
"time": "int", | ||
"overlapping_node_count_log10": "int", | ||
"overlapping_node_count": "int", | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplication here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, looks like it's tested now?