-
Notifications
You must be signed in to change notification settings - Fork 38
Fix chromsizes in scaling #283
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -52,9 +52,8 @@ def assign_regs(chroms, pos, regs): | |
|
|
||
| regs_dict = { | ||
| chrom.encode(): regs_per_chrom[["start", "end"]] | ||
| .values | ||
| .flatten() | ||
| .astype(np.int64) | ||
| .values.flatten() | ||
| .astype(np.int64) | ||
| for chrom, regs_per_chrom in gb_regs | ||
| } | ||
|
|
||
|
|
@@ -135,11 +134,17 @@ def make_empty_cross_region_table( | |
|
|
||
|
|
||
| def bins_pairs_by_distance( | ||
| pairs_df, dist_bins, regions=None, chromsizes=None, ignore_trans=False, | ||
| pairs_df, | ||
| dist_bins, | ||
| regions=None, | ||
| chromsizes=None, | ||
| ignore_trans=False, | ||
| keep_unassigned=False, | ||
| ): | ||
|
|
||
| dist_bins = np.r_[dist_bins, np.iinfo(np.int64).max] | ||
| # Explicitly remove unmapped pair | ||
| pairs_df = pairs_df[(pairs_df["chrom1"] != "!") & (pairs_df["chrom2"] != "!")] | ||
| if regions is None: | ||
| if chromsizes is None: | ||
| chroms = sorted( | ||
|
|
@@ -188,7 +193,6 @@ def bins_pairs_by_distance( | |
| pairs_df.chrom2.values, pairs_df.pos2.values, regions | ||
| ).T | ||
|
|
||
|
|
||
| pairs_reduced_df = pd.DataFrame( | ||
| { | ||
| "chrom1": pairs_df.chrom1.values, | ||
|
|
@@ -208,10 +212,11 @@ def bins_pairs_by_distance( | |
| ) | ||
|
|
||
| if not keep_unassigned: | ||
| pairs_reduced_df = (pairs_reduced_df | ||
| .query('(start1 >= 0) and (start2 >= 0)') | ||
| pairs_reduced_df = ( | ||
| pairs_reduced_df.query("(start1 >= 0) and (start2 >= 0)") | ||
| # do not test for end1 and end2, as they can be -1 if regions and not specified | ||
| .reset_index(drop=True)) | ||
| .reset_index(drop=True) | ||
| ) | ||
|
|
||
| pairs_reduced_df["min_dist"] = np.where( | ||
| pairs_reduced_df["dist_bin_idx"] > 0, | ||
|
|
@@ -220,7 +225,7 @@ def bins_pairs_by_distance( | |
| ) | ||
|
|
||
| pairs_reduced_df["max_dist"] = np.where( | ||
| pairs_reduced_df["dist_bin_idx"] < len(dist_bins)-1, | ||
| pairs_reduced_df["dist_bin_idx"] < len(dist_bins) - 1, | ||
| dist_bins[pairs_reduced_df["dist_bin_idx"]], | ||
| np.iinfo(np.int64).max, | ||
| ) | ||
|
|
@@ -349,7 +354,7 @@ def compute_scaling( | |
| Parameters | ||
| ---------- | ||
| pairs : pd.DataFrame or str or file-like object | ||
| A table with pairs of genomic coordinates representing contacts. | ||
| A table with pairs of genomic coordinates representing contacts. | ||
| It can be a pandas DataFrame, a path to a pairs file, or a file-like object. | ||
| regions : bioframe viewframe or None, optional | ||
| Genomic regions of interest. It can be anything that can serve as input to bioframe.from_any, | ||
|
|
@@ -380,16 +385,21 @@ def compute_scaling( | |
| """ | ||
|
|
||
| dist_bins = geomspace( | ||
| dist_range[0], | ||
| dist_range[0], | ||
| dist_range[1], | ||
| int(np.round(np.log10(dist_range[1]/dist_range[0])*n_dist_bins_decade)) | ||
| int(np.round(np.log10(dist_range[1] / dist_range[0]) * n_dist_bins_decade)), | ||
| ) | ||
|
|
||
| if isinstance(pairs, pd.DataFrame): | ||
| pairs_df = pairs | ||
|
|
||
| elif isinstance(pairs, str) or hasattr(pairs, "buffer") or hasattr(pairs, "peek"): | ||
| pairs_df, _, _ = pairsio.read_pairs(pairs, nproc=nproc_in, chunksize=chunksize) | ||
| pairs_df, _, chromsizes_extracted = pairsio.read_pairs( | ||
|
Member
Author
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. Real change |
||
| pairs, nproc=nproc_in, chunksize=chunksize | ||
| ) | ||
| # Use chromsizes from the header if not provided explicitly | ||
| if chromsizes is None: | ||
| chromsizes = chromsizes_extracted | ||
| else: | ||
| raise ValueError( | ||
| "pairs must be either a path to a pairs file or a pd.DataFrame" | ||
|
|
@@ -405,7 +415,7 @@ def compute_scaling( | |
| regions=regions, | ||
| chromsizes=chromsizes, | ||
| ignore_trans=ignore_trans, | ||
| keep_unassigned=keep_unassigned | ||
| keep_unassigned=keep_unassigned, | ||
| ) | ||
|
|
||
| sc = sc_chunk if sc is None else sc.add(sc_chunk, fill_value=0) | ||
|
|
@@ -416,7 +426,6 @@ def compute_scaling( | |
| else trans_counts.add(trans_counts_chunk, fill_value=0) | ||
| ) | ||
|
|
||
|
|
||
| # if not (isinstance(regions, pd.DataFrame) and | ||
| # (set(regions.columns) == set(['chrom', 'start','end']))): | ||
| # raise ValueError('regions must be provided as a dict or chrom-indexed Series of chromsizes or as a bedframe.') | ||
|
|
@@ -428,10 +437,9 @@ def compute_scaling( | |
|
|
||
| if not ignore_trans: | ||
| trans_counts.reset_index(inplace=True) | ||
| trans_counts["n_bp2"] = ( | ||
| (trans_counts["end1"] - trans_counts["start1"]) * ( | ||
| trans_counts["n_bp2"] = (trans_counts["end1"] - trans_counts["start1"]) * ( | ||
| trans_counts["end2"] - trans_counts["start2"] | ||
| )) | ||
| ) | ||
|
|
||
| return sc, trans_counts | ||
|
|
||
|
|
@@ -451,12 +459,12 @@ def norm_scaling_factor(bins, cfreqs, norm_window): | |
| """ | ||
|
|
||
| lo, hi = np.searchsorted(bins, norm_window) | ||
| return cfreqs[lo:hi+1].mean() | ||
| return cfreqs[lo : hi + 1].mean() | ||
|
|
||
|
|
||
| def norm_scaling(bins, cfreqs, norm_window, log_input=False): | ||
| """ | ||
| Normalize a contact-frequency-vs-distance curve, by setting the average contact frequency | ||
| Normalize a contact-frequency-vs-distance curve, by setting the average contact frequency | ||
| in a given window to 1.0. | ||
|
|
||
| Args: | ||
|
|
@@ -468,14 +476,14 @@ def norm_scaling(bins, cfreqs, norm_window, log_input=False): | |
| Returns: | ||
| float or array-like: The normalized contact frequencies. | ||
| """ | ||
|
|
||
| norm = norm_scaling_factor(bins, cfreqs, norm_window) | ||
| if log_input: | ||
| return cfreqs - norm | ||
| else: | ||
| return cfreqs / norm | ||
|
|
||
|
|
||
| def unity_norm_scaling(bins, cfreqs, norm_range=(1e4, 1e9)): | ||
| bin_lens = np.diff(bins) | ||
| bin_mids = np.sqrt(bins[1:] * bins[:-1]) | ||
|
|
||
This file contains hidden or 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
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.
Real change