def fetch(self, genome_range1, genome_range2=None): """ Return ------ matrix : numpy.ndarray """ from coolbox.utilities.genome import GenomeRange if genome_range2 is None: genome_range2 = genome_range1 genome_range1 = to_gr(genome_range1) genome_range2 = to_gr(genome_range2) if isinstance(genome_range1, str): genome_range1 = GenomeRange(genome_range1) if isinstance(genome_range2, str): genome_range2 = GenomeRange(genome_range2) if genome_range1.chrom.startswith("chr"): genome_range1.change_chrom_names() if genome_range2.chrom.startswith("chr"): genome_range2.change_chrom_names() binsize = self.infer_binsize(genome_range1) self.fetched_binsize = binsize # expose fetched binsize straw_list = self.__fetch_straw_list(genome_range1, genome_range2, binsize) matrix = self.__list_to_matrix(straw_list, genome_range1, genome_range2, binsize) return matrix
def fetch(self, genome_range1, genome_range2=None): # TODO what if genome_ranges are invalid if genome_range2 is None: genome_range2 = genome_range1 genome_range1 = to_gr(genome_range1) genome_range2 = to_gr(genome_range2) cool = self.get_cool(genome_range1) if genome_range1.chrom not in cool.chromnames: genome_range1.change_chrom_names() if genome_range2.chrom not in cool.chromnames: genome_range2.change_chrom_names() try: mat = cool.matrix(balance=self.balance).fetch( str(genome_range1), str(genome_range2)) except ValueError as e: log.warning(str(e)) log.warning( "Data is not balanced, force to use unbalanced matrix.") mat = cool.matrix(balance=False).fetch(str(genome_range1), str(genome_range2)) return mat
def infer_binsize(self, genome_range): from .tools import infer_resolution genome_range = to_gr(genome_range) if self.is_multi: resolutions = [k for k in self.coolers.keys()] if self.binsize == 'auto': binsize = infer_resolution(genome_range, resolutions) else: assert self.binsize in resolutions, \ "Multi-Cooler file not contain the resolution {}.".format(self.binsize) binsize = int(self.binsize) else: binsize = self.cool.binsize return binsize
def fetch(self, gr1, gr2=None): """ Return ------ matrix : numpy.ndarray """ from coolbox.utilities.genome import GenomeRange flip = False if gr2 is None: gr2 = gr1 gr1 = to_gr(gr1) gr2 = to_gr(gr2) if gr2.start < gr1.start: flip = True gr1, gr2 = gr2, gr1 if isinstance(gr1, str): gr1 = GenomeRange(gr1) if isinstance(gr2, str): gr2 = GenomeRange(gr2) if gr1.chrom.startswith("chr"): gr1.change_chrom_names() if gr2.chrom.startswith("chr"): gr2.change_chrom_names() binsize = self.infer_binsize(gr1) self.fetched_binsize = binsize # expose fetched binsize straw_iter = self.__fetch_straw_iter(gr1, gr2, binsize) mat = self.__straw_to_matrix(straw_iter, gr1, gr2, binsize) if flip: mat = mat.T return mat