@@ -390,15 +390,14 @@ def plotHeatmap(ma, chrBinBoundaries, fig, position, args, cmap, xlabel=None,
390
390
pValueMin = args .vMinBigwig , pValueMax = args .vMaxBigwig , pResolution = pResolution )
391
391
392
392
393
- def translate_region (region_string ):
393
+ def translate_region (region_string , ma ):
394
394
"""
395
395
Takes an string and returns a list
396
396
of chrom, start, end.
397
397
If the region string only contains
398
398
the chrom, then start and end
399
- are set to a 0 and 1e15
399
+ are set to matrix start and end on this chromosome
400
400
"""
401
-
402
401
# region_string = toBytes(region_string)
403
402
region_string = region_string .replace ("," , "" )
404
403
region_string = region_string .replace (";" , "" )
@@ -407,14 +406,24 @@ def translate_region(region_string):
407
406
408
407
fields = region_string .split (":" )
409
408
chrom = fields [0 ]
409
+
410
+ chrom = check_chrom_str_bytes (ma .interval_trees , chrom )
411
+ if chrom not in list (ma .interval_trees ):
412
+ chrom = change_chrom_names (chrom )
413
+ chrom = check_chrom_str_bytes (ma .interval_trees , chrom )
414
+ if chrom not in list (ma .interval_trees ):
415
+ exit (
416
+ "Chromosome name {} in --region not in matrix" .format (change_chrom_names (chrom )))
417
+
418
+ first_bin , last_bin = ma .getChrBinRange (chrom )
410
419
try :
411
420
region_start = int (fields [1 ])
412
421
except IndexError :
413
- region_start = 0
422
+ region_start = ma . getBinPos ( first_bin )[ 1 ]
414
423
try :
415
424
region_end = int (fields [2 ])
416
425
except IndexError :
417
- region_end = 1e15 # vert large number
426
+ region_end = ma . getBinPos ( last_bin - 1 )[ 2 ]
418
427
419
428
return chrom , region_start , region_end
420
429
@@ -559,43 +568,27 @@ def plotPerChr(hic_matrix, cmap, args, pBigwig, pResolution):
559
568
args , hic_matrix )
560
569
plotHeatmap (matrix , chr_bin_boundary , fig , None ,
561
570
args , cmap , xlabel = chrname , ylabel = chrname ,
562
- start_pos = start_pos1 , start_pos2 = start_pos2 , pNorm = norm , pAxis = axis , pBigwig = bigwig_info , pChromsomeStartEndDict = chromosome_start_end (hic_matrix ), pResolution = pResolution )
571
+ start_pos = start_pos1 , start_pos2 = start_pos2 , pNorm = norm , pAxis = axis , pBigwig = bigwig_info ,
572
+ pChromsomeStartEndDict = chromosome_start_end (hic_matrix ), pResolution = pResolution )
563
573
return fig
564
574
565
575
566
576
def getRegion (args , ma ):
567
577
chrom = region_start = region_end = idx1 = start_pos1 = chrom2 = region_start2 = region_end2 = idx2 = start_pos2 = None
568
- chrom , region_start , region_end = translate_region (args .region )
569
-
570
- chrom = check_chrom_str_bytes (ma .interval_trees , chrom )
571
- # if type(next(iter(ma.interval_trees))) in [np.bytes_, bytes]:
572
- # chrom = toBytes(chrom)
573
-
574
- if chrom not in list (ma .interval_trees ):
575
-
576
- chrom = change_chrom_names (chrom )
577
-
578
- chrom = check_chrom_str_bytes (ma .interval_trees , chrom )
579
-
580
- # if type(next(iter(ma.interval_trees))) in [np.bytes_, bytes]:
581
- # chrom = toBytes(chrom)
582
-
583
- if chrom not in list (ma .interval_trees ):
584
- exit (
585
- "Chromosome name {} in --region not in matrix" .format (change_chrom_names (chrom )))
578
+ chrom , region_start , region_end = translate_region (args .region , ma )
586
579
587
580
args .region = [chrom , region_start , region_end ]
588
581
is_cooler = check_cooler (args .matrix )
589
582
if is_cooler :
590
583
idx1 , start_pos1 = zip (* [(idx , x [1 ]) for idx , x in enumerate (ma .cut_intervals ) if x [0 ] == chrom and # noqa: W504
591
- ((x [1 ] >= region_start and x [2 ] < region_end ) or # noqa: W504
584
+ ((x [1 ] >= region_start and x [2 ] <= region_end ) or # noqa: W504
592
585
(x [1 ] < region_end and x [2 ] < region_end and x [2 ] > region_start ) or # noqa: W504
593
586
(x [1 ] > region_start and x [1 ] < region_end ))])
594
587
else :
595
588
idx1 , start_pos1 = zip (* [(idx , x [1 ]) for idx , x in enumerate (ma .cut_intervals ) if x [0 ] == chrom and # noqa: W504
596
- x [1 ] >= region_start and x [2 ] < region_end ])
589
+ x [1 ] >= region_start and x [2 ] <= region_end ])
597
590
if hasattr (args , 'region2' ) and args .region2 :
598
- chrom2 , region_start2 , region_end2 = translate_region (args .region2 )
591
+ chrom2 , region_start2 , region_end2 = translate_region (args .region2 , ma )
599
592
chrom2 = check_chrom_str_bytes (ma .interval_trees , chrom2 )
600
593
601
594
# if type(next(iter(ma.interval_trees))) in [np.bytes_, bytes]:
@@ -780,7 +773,6 @@ def main(args=None):
780
773
matrix = np .asarray (ma .getMatrix ().astype (float ))
781
774
782
775
resolution = ma .getBinSize ()
783
-
784
776
matrix_length = len (matrix [0 ])
785
777
log .debug ("Number of data points matrix: {}" .format (matrix_length ))
786
778
0 commit comments