-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from deeptools/expectedMatrix
Expected matrix
- Loading branch information
Showing
6 changed files
with
73 additions
and
32 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import division | ||
|
||
import sys | ||
import argparse | ||
import numpy as np | ||
import os | ||
from unidecode import unidecode | ||
from hicmatrix import HiCMatrix as hm | ||
import warnings | ||
|
||
def parse_arguments(): | ||
parser = argparse.ArgumentParser(description = "Making the expected matrix") | ||
#Required | ||
parser.add_argument("-m", | ||
dest = "count_matrix", | ||
required = True) | ||
parser.add_argument("-o", | ||
dest = "output_matrix", | ||
required = True) | ||
return parser | ||
def expected_interactions(matrix): | ||
""" | ||
Computes the expected number of interactions per distance | ||
""" | ||
|
||
expected_interactions = np.zeros(matrix.shape[0]) | ||
row, col = matrix.nonzero() | ||
distance = np.absolute(row - col) | ||
occurences = np.arange(matrix.shape[0], 0, -1) | ||
|
||
for i, distance_ in enumerate(distance): | ||
expected_interactions[distance_] += matrix.data[i] | ||
expected_interactions /= occurences | ||
|
||
mask = np.isnan(expected_interactions) | ||
expected_interactions[mask] = 0 | ||
mask = np.isinf(expected_interactions) | ||
expected_interactions[mask] = 0 | ||
return expected_interactions | ||
|
||
|
||
def _expected_matrix(matrix): | ||
expected_interactions_in_distance_ = expected_interactions(matrix) | ||
row, col = matrix.nonzero() | ||
distance = np.absolute(row - col).astype(np.int32) | ||
|
||
if len(matrix.data) > 0: | ||
data_type = type(matrix.data[0]) | ||
expected = expected_interactions_in_distance_[distance] | ||
matrix.data = matrix.data.astype(np.float32) | ||
matrix.data = expected | ||
matrix.data = np.nan_to_num(matrix.data).astype(data_type) | ||
return matrix | ||
|
||
|
||
def main(): | ||
parser = parse_arguments() | ||
args = parser.parse_args() | ||
ma = hm.hiCMatrix(args.count_matrix) | ||
expected_matrix = _expected_matrix(ma.matrix) | ||
ma.setMatrixValues(expected_matrix) | ||
ma.save(args.output_matrix, pApplyCorrection=False) |
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