Skip to content

Commit

Permalink
liniting
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimwolff committed Nov 22, 2018
1 parent 3fee43f commit 264f91d
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 78 deletions.
4 changes: 2 additions & 2 deletions hicexplorer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
logging.basicConfig(level=logging.DEBUG)
import warnings
import warnings
import sys

if not sys.warnoptions:
warnings.simplefilter("ignore")
warnings.simplefilter("ignore")
1 change: 1 addition & 0 deletions hicexplorer/hicAggregateContacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
warnings.simplefilter(action="ignore", category=RuntimeWarning)
warnings.simplefilter(action="ignore", category=PendingDeprecationWarning)


def parse_arguments(args=None):
parser = argparse.ArgumentParser(add_help=False,
description='Takes a list of positions in the hic-matrix and '
Expand Down
22 changes: 11 additions & 11 deletions hicexplorer/hicAverageRegions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def parse_arguments(args=None):
parserRequired.add_argument('--regions', '-r',
help='BED file which stores a list of regions that are summed and averaged',
required=True)
parserMutuallyExclusiveGroup = parser.add_mutually_exclusive_group(required=True)
parserMutuallyExclusiveGroup = parser.add_mutually_exclusive_group(required=True)
parserMutuallyExclusiveGroup.add_argument('--range', '-ra',
help='Range of region up- and downstream of each region to include in genomic units.',
nargs=2,
type=int)
help='Range of region up- and downstream of each region to include in genomic units.',
nargs=2,
type=int)
parserMutuallyExclusiveGroup.add_argument('--rangeInBins', '-rib',
help='Range of region up- and downstream of each region to include in bin units.',
nargs=2,
type=int)
help='Range of region up- and downstream of each region to include in bin units.',
nargs=2,
type=int)
parserRequired.add_argument('--outFileName', '-out',
help='File name to save the adjusted matrix.')
parserOpt = parser.add_argument_group('Optional arguments')
Expand All @@ -55,7 +55,6 @@ def calculateViewpointRange(pHiCMatrix, pViewpoint, pRange):
This function computes the correct start and end position of a viewpoint given the reference and the range.
'''


max_length = pHiCMatrix.getBinPos(pHiCMatrix.getChrBinRange(pViewpoint[0])[1] - 1)[2]
bin_size = pHiCMatrix.getBinSize()
_range = [pRange[0], pRange[1]]
Expand All @@ -71,11 +70,13 @@ def calculateViewpointRange(pHiCMatrix, pViewpoint, pRange):
_range[1] = (max_length - int(pViewpoint[2])) + bin_size
return region_start, region_end, _range


def getBinIndices(pHiCMatrix, pViewpoint):
return pHiCMatrix.getRegionBinRange(pViewpoint[0], pViewpoint[1], pViewpoint[2])


def calculateViewpointRangeBins(pHiCMatrix, pViewpoint, pRange):

viewpoint_index = getBinIndices(pHiCMatrix, pViewpoint)[0]
start = viewpoint_index - pRange[0]
end = viewpoint_index + pRange[1]
Expand Down Expand Up @@ -125,5 +126,4 @@ def main(args=None):

summed_matrix /= len(indices_values)


save_npz(args.outFileName, summed_matrix)
save_npz(args.outFileName, summed_matrix)
11 changes: 5 additions & 6 deletions hicexplorer/hicBuildMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import logging
log = logging.getLogger(__name__)

import warnings
warnings.simplefilter(action="ignore", category=RuntimeWarning)
warnings.simplefilter(action="ignore", category=PendingDeprecationWarning)

Expand Down Expand Up @@ -1381,13 +1380,13 @@ def main(args=None):
unlink(args.outFileName.name)

if args.outFileName.name.endswith('.cool') and args.binSize is not None and len(args.binSize) > 2:

matrixFileHandlerOutput = MatrixFileHandler(pFileType='cool')
matrixFileHandlerOutput.set_matrix_variables(hic_ma.matrix,
hic_ma.cut_intervals,
hic_ma.nan_bins,
hic_ma.correction_factors,
hic_ma.distance_counts)
hic_ma.cut_intervals,
hic_ma.nan_bins,
hic_ma.correction_factors,
hic_ma.distance_counts)
matrixFileHandlerOutput.save(args.outFileName.name + '::/resolutions/' + str(args.binSize[0]), pSymmetric=True, pApplyCorrection=False)

for resolution in args.binSize[1:]:
Expand Down
9 changes: 4 additions & 5 deletions hicexplorer/hicConvertFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from copy import deepcopy


def parse_arguments(args=None):
"""
get command line arguments
Expand Down Expand Up @@ -148,7 +149,7 @@ def main(args=None):
chromosomes_to_load = None
if args.chromosome:
chromosomes_to_load = [args.chromosome]

applyCorrectionCoolerLoad = True
if args.load_raw_values:
applyCorrectionCoolerLoad = False
Expand All @@ -168,7 +169,6 @@ def main(args=None):

matrixFileHandlerOutput = MatrixFileHandler(pFileType=args.outputFormat, pEnforceInteger=args.enforce_integer, pFileWasH5=format_was_h5)


matrixFileHandlerOutput.set_matrix_variables(_matrix, cut_intervals, nan_bins,
correction_factors, distance_counts)
matrixFileHandlerOutput.save(
Expand All @@ -185,19 +185,18 @@ def main(args=None):
hic_matrix = HiCMatrix.hiCMatrix()
hic_matrix.setMatrix(_matrix, cut_intervals)


bin_size = hic_matrix.getBinSize()

for j, resolution in enumerate(args.resolutions):
hic_matrix_res = deepcopy(hic_matrix)

_mergeFactor = int(resolution) // bin_size

log.debug('bin size {}'.format(bin_size))
log.debug('_mergeFactor {}'.format(_mergeFactor))
if int(resolution) != bin_size:
merged_matrix = hicMergeMatrixBins.merge_bins(
hic_matrix_res, _mergeFactor)
hic_matrix_res, _mergeFactor)
else:
merged_matrix = hic_matrix_res
append = False
Expand Down
2 changes: 1 addition & 1 deletion hicexplorer/hicMergeTADbins.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def parse_arguments():
help='Name for the resulting matrix file.',
required=True)
parser.add_argument('--version', action='version',
version='%(prog)s {}'.format(__version__))
version='%(prog)s {}'.format(__version__))
return parser


Expand Down
2 changes: 1 addition & 1 deletion hicexplorer/hicNormalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ def main(args=None):
hic_matrix.matrix.data[mask] = 0
hic_matrix.matrix.eliminate_zeros()

hic_matrix.save(args.outFileName[i], pApplyCorrection=False)
hic_matrix.save(args.outFileName[i], pApplyCorrection=False)
12 changes: 7 additions & 5 deletions hicexplorer/hicPlotAverageRegions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import matplotlib.cm as cm
import matplotlib.gridspec as gridspec
import numpy as np
from scipy.ndimage import rotate
from scipy.ndimage import rotate
from mpl_toolkits.axes_grid1 import make_axes_locatable


def parse_arguments(args=None):

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -71,7 +73,7 @@ def parse_arguments(args=None):
def main(args=None):

args = parse_arguments().parse_args(args)

matrix = load_npz(args.matrix)

matrix = matrix.toarray()
Expand All @@ -80,8 +82,8 @@ def main(args=None):
matrix = rotate(matrix, 45)
matrix_shapes = matrix.shape
# log.debug('matrix.shapes {}'.format(matrix_shapes))
matrix = matrix[:matrix_shapes[0]//2, :]
norm=None
matrix = matrix[:matrix_shapes[0] // 2, :]
norm = None
if args.vMax is not None or args.vMin is not None:
matrix = matrix.clip(min=args.vMin, max=args.vMax)
if args.log1p or args.log:
Expand Down Expand Up @@ -116,4 +118,4 @@ def main(args=None):

fig.colorbar(matrix_axis, cax=cax)
plt.tight_layout()
plt.savefig(args.outputFile, dpi=args.dpi)
plt.savefig(args.outputFile, dpi=args.dpi)
1 change: 1 addition & 0 deletions hicexplorer/hicPlotMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
warnings.simplefilter(action="ignore", category=RuntimeWarning)
warnings.simplefilter(action="ignore", category=PendingDeprecationWarning)


def parse_arguments(args=None):
parser = argparse.ArgumentParser(add_help=False,
description='Creates a Heatmap of a HiC matrix.')
Expand Down
11 changes: 6 additions & 5 deletions hicexplorer/hicTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def parse_arguments(args=None):

parserOpt.add_argument('--method', '-me',
help='Transformation method to use for input matrix. Transformation is computed per chromosome.',
choices=['obs_exp', 'obs_exp_lieberman', 'obs_exp_non_zero','pearson', 'covariance', 'norm'],
choices=['obs_exp', 'obs_exp_lieberman', 'obs_exp_non_zero', 'pearson', 'covariance', 'norm'],
default='obs_exp')

parserOpt.add_argument('--chromosomes',
Expand Down Expand Up @@ -86,20 +86,23 @@ def _obs_exp_norm(pSubmatrix):
obs_exp_matrix_ = convertInfsToZeros(csr_matrix(obs_exp_matrix_)).todense()
return obs_exp_matrix_


def _obs_exp(pSubmatrix):

obs_exp_matrix_ = obs_exp_matrix(pSubmatrix)
obs_exp_matrix_ = convertNansToZeros(csr_matrix(obs_exp_matrix_))
obs_exp_matrix_ = convertInfsToZeros(csr_matrix(obs_exp_matrix_)).todense()
return obs_exp_matrix_


def _obs_exp_non_zero(pSubmatrix):

obs_exp_matrix_ = obs_exp_matrix_non_zero(pSubmatrix)
obs_exp_matrix_ = convertNansToZeros(csr_matrix(obs_exp_matrix_))
obs_exp_matrix_ = convertInfsToZeros(csr_matrix(obs_exp_matrix_)).todense()
return obs_exp_matrix_


def main(args=None):

args = parse_arguments().parse_args(args)
Expand All @@ -114,7 +117,6 @@ def main(args=None):
if args.chromosomes:
hic_ma.keepOnlyTheseChr(args.chromosomes)


trasf_matrix = lil_matrix(hic_ma.matrix.shape)

if args.method == 'norm':
Expand Down Expand Up @@ -175,12 +177,11 @@ def main(args=None):

trasf_matrix[chr_range[0]:chr_range[1], chr_range[0]:chr_range[1]] = lil_matrix(_pearson(submatrix.todense()))
else:
trasf_matrix= csr_matrix(_pearson(hic_ma.matrix.todense()))

trasf_matrix = csr_matrix(_pearson(hic_ma.matrix.todense()))

elif args.method == 'covariance':
if args.perChromosome:

for chrname in hic_ma.getChrNames():
chr_range = hic_ma.getChrBinRange(chrname)
submatrix = hic_ma.matrix[chr_range[0]:chr_range[1], chr_range[0]:chr_range[1]]
Expand Down
2 changes: 0 additions & 2 deletions hicexplorer/test/general/test_hicAggregateContacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def test_hicAggregateContacts_clustering():
os.remove(outfile_heatmaps.name)



@pytest.mark.skipif(MID_MEMORY > memory,
reason="Travis has too less memory to run it.")
def test_hicAggregateContacts_clustering_cool():
Expand Down Expand Up @@ -140,7 +139,6 @@ def test_hicAggregateContacts_3d():
os.remove(outfile_aggregate_3d.name)



@pytest.mark.skipif(MID_MEMORY > memory,
reason="Travis has too less memory to run it.")
def test_hicAggregateContacts_3d_cooler():
Expand Down
4 changes: 3 additions & 1 deletion hicexplorer/test/general/test_hicAverageRegions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "test_data/")


def test_average_regions():

outfile = NamedTemporaryFile(suffix='.npz', prefix='average_region', delete=False)
Expand All @@ -27,6 +28,7 @@ def test_average_regions():

os.remove(outfile.name)


def test_average_regions_range_in_bins():

outfile = NamedTemporaryFile(suffix='.npz', prefix='average_region', delete=False)
Expand All @@ -40,4 +42,4 @@ def test_average_regions_range_in_bins():

nt.assert_almost_equal(test_file.data, new_file.data, decimal=0)

os.remove(outfile.name)
os.remove(outfile.name)
8 changes: 5 additions & 3 deletions hicexplorer/test/general/test_hicConvertFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ def test_hicConvertFormat_h5_to_cool():
nt.assert_array_almost_equal(test.matrix.data, new.matrix.data, decimal=DELTA_DECIMAL)
# os.unlink(outfile.name)


def test_hicConvertFormat_h5_to_cool_enforce_integer():

# original_matrix = ''
outfile = NamedTemporaryFile(suffix='.cool', delete=False)
outfile.close()

args = "--matrices {} --outFileName {} --inputFormat h5 --outputFormat cool ".format(original_matrix_h5_li, outfile.name).split()
args = "--matrices {} --outFileName {} --inputFormat h5 --outputFormat cool ".format(original_matrix_cool, outfile.name).split()
hicConvertFormat.main(args)

test = hm.hiCMatrix(original_matrix_cool)
Expand All @@ -70,20 +71,21 @@ def test_hicConvertFormat_h5_to_cool_enforce_integer():
print('issubclass(test.matrix.data.dtype.type, np.integer) {}'.format(issubclass(test.matrix.data.dtype.type, np.integer)))
assert issubclass(test.matrix.data.dtype.type, np.integer)


def test_hicConvertFormat_h5_to_homer():
pass
# os.unlink(outfile.name)


def test_hicConvertFormat_h5_to_ginteractions():
pass

# os.unlink(outfile.name)


def test_hicConvertFormat_h5_to_mcool():
pass

# os.unlink(outfile.name)


Expand Down
24 changes: 0 additions & 24 deletions hicexplorer/test/general/test_hicFindTADs.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,6 @@ def test_find_TADs_fdr_chromosomes():

shutil.rmtree(tad_folder)

# def test_find_TADs_fdr_dekker():
# # full test case with build of the matrix and search for tads
# matrix = ROOT + "dekker.txt.gz"
# tad_folder = mkdtemp(prefix="test_case_find_tads_fdr_dekker")
# args = "--matrix {} --minDepth 150000 --maxDepth 300000 --numberOfProcessors 2 --step 50000 \
# --outPrefix {}/test_multiFDR_dekker \
# --correctForMultipleTesting fdr".format(matrix, tad_folder).split()

# hicFindTADs.main(args)

# new = hm.hiCMatrix(tad_folder + "/test_multiFDR_dekker_zscore_matrix.h5")
# test = hm.hiCMatrix(ROOT + 'find_TADs/dekker/multiFDR_dekker_zscore_matrix.h5')
# nt.assert_equal(test.matrix.data, new.matrix.data)
# nt.assert_equal(test.cut_intervals, new.cut_intervals)

# print(tad_folder + "/test_multiFDR_boundaries.bed")
# assert are_files_equal(ROOT + "find_TADs/dekker/multiFDR_dekker_boundaries.bed", tad_folder + "/test_multiFDR_dekker_boundaries.bed")
# assert are_files_equal(ROOT + "find_TADs/dekker/multiFDR_dekker_domains.bed", tad_folder + "/test_multiFDR_dekker_domains.bed")
# assert are_files_equal(ROOT + "find_TADs/dekker/multiFDR_dekker_tad_score.bm", tad_folder + "/test_multiFDR_dekker_tad_score.bm")
# assert are_files_equal(ROOT + "find_TADs/dekker/multiFDR_dekker_boundaries.gff", tad_folder + "/test_multiFDR_dekker_boundaries.gff")
# assert are_files_equal(ROOT + "find_TADs/dekker/multiFDR_dekker_score.bedgraph", tad_folder + "/test_multiFDR_dekker_score.bedgraph")

# shutil.rmtree(tad_folder)


def test_find_TADs_bonferroni():
# reduced test case, the z-score matrix is given to decrease run time
Expand Down
2 changes: 0 additions & 2 deletions hicexplorer/test/general/test_hicPCA.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ def test_pca_bigwig_gene_density_intermediate_matrices_norm():

test_pearson_h5 = hm.hiCMatrix(ROOT + "hicPCA/pearson_norm.h5")



nt.assert_array_almost_equal(test_pearson.matrix.data, new_pearson.matrix.data, decimal=DELTA_DECIMAL)
nt.assert_array_almost_equal(test_obs_exp.matrix.data, new_obs_exp.matrix.data, decimal=DELTA_DECIMAL)

Expand Down

0 comments on commit 264f91d

Please sign in to comment.