def __init__(self, file_name=None, mode='a', data_fields=None, default_field=None, default_value=0.0, regions=None, edges=None, _table_name_regions='region_data', _table_name_edges='edges', tmpdir=None): RegionMatrixTable.__init__(self, file_name=file_name, additional_edge_fields=data_fields, mode=mode, tmpdir=tmpdir, default_score_field=default_field, default_value=default_value, _table_name_regions=_table_name_regions, _table_name_edges=_table_name_edges) ArchitecturalFeature.__init__(self) for edge_table in self._edge_table_iter(): if len(edge_table) > 0: self._calculated = True break if regions is not None: self.add_regions(regions) # process data if edges is not None: self.add_edges(edges)
def as_data_frame(self, key, weight_column=None): """ See :class:`~RegionMatrixTable` """ return RegionMatrixTable.as_data_frame(self, key, weight_column=weight_column)
def _get_matrix(self, row_ranges=None, col_ranges=None, weight_column=None, default_value=0.0): return RegionMatrixTable._get_matrix(self, row_ranges=row_ranges, col_ranges=col_ranges, weight_column=weight_column)
def as_matrix(self, key=slice(0, None, None), values_from=None, mask_missing=False, impute_missing=False, default_value=0.0): """ See :class:`~RegionMatrixTable` """ return RegionMatrixTable.as_matrix(self, key=key, values_from=values_from, mask_missing=mask_missing, impute_missing=impute_missing)
def setup_method(self, method): self.rmt = RegionMatrixTable( additional_edge_fields={ 'weight': tables.Int32Col(pos=0), 'foo': tables.Int32Col(pos=1), 'bar': tables.Float32Col(pos=2), 'baz': tables.StringCol(50, pos=3) }) for i in range(10): if i < 5: chromosome = 'chr1' start = i * 1000 end = (i + 1) * 1000 elif i < 8: chromosome = 'chr2' start = (i - 5) * 1000 end = (i + 1 - 5) * 1000 else: chromosome = 'chr3' start = (i - 8) * 1000 end = (i + 1 - 8) * 1000 node = GenomicRegion(chromosome=chromosome, start=start, end=end) self.rmt.add_region(node) self.rmt.flush() for i in range(10): for j in range(i, 10): edge = Edge(source=i, sink=j, weight=i * j, foo=i, bar=j, baz='x' + str(i * j)) self.rmt.add_edge(edge) self.rmt.flush()
class TestRegionMatrixTable: def setup_method(self, method): self.rmt = RegionMatrixTable( additional_edge_fields={ 'weight': tables.Int32Col(pos=0), 'foo': tables.Int32Col(pos=1), 'bar': tables.Float32Col(pos=2), 'baz': tables.StringCol(50, pos=3) }) for i in range(10): if i < 5: chromosome = 'chr1' start = i * 1000 end = (i + 1) * 1000 elif i < 8: chromosome = 'chr2' start = (i - 5) * 1000 end = (i + 1 - 5) * 1000 else: chromosome = 'chr3' start = (i - 8) * 1000 end = (i + 1 - 8) * 1000 node = GenomicRegion(chromosome=chromosome, start=start, end=end) self.rmt.add_region(node) self.rmt.flush() for i in range(10): for j in range(i, 10): edge = Edge(source=i, sink=j, weight=i * j, foo=i, bar=j, baz='x' + str(i * j)) self.rmt.add_edge(edge) self.rmt.flush() def teardown_method(self, method): self.rmt.close() def test_matrix(self): m = self.rmt.matrix() for row_region in m.row_regions: i = row_region.ix for col_region in m.col_regions: j = col_region.ix if np.ma.is_masked(m[i, j]): assert np.ma.is_masked(m[j, i]) else: assert m[i, j] == m[j, i] == i * j m = self.rmt.matrix(score_field='foo') for row_region in m.row_regions: i = row_region.ix for col_region in m.col_regions: j = col_region.ix if np.ma.is_masked(m[i, j]): assert np.ma.is_masked(m[j, i]) else: assert m[i, j] == m[j, i] == min(i, j) m = self.rmt.matrix(score_field='bar') for row_region in m.row_regions: i = row_region.ix for col_region in m.col_regions: j = col_region.ix if np.ma.is_masked(m[i, j]): assert np.ma.is_masked(m[j, i]) else: assert m[i, j] == m[j, i] == max(i, j) def test_matrix_subset(self): m = self.rmt.matrix(key=('chr2', 'chr2'), score_field='bar') for i, row_region in enumerate(m.row_regions): for j, col_region in enumerate(m.col_regions): assert (np.ma.is_masked(m[i, j]) and np.ma.is_masked(m[j, i])) or \ (m[i, j] == m[j, i] == max(row_region.ix, col_region.ix)) m = self.rmt.matrix(key=('chr2', 'chr3'), score_field='bar', mask=False) for i, row_region in enumerate(m.row_regions): for j, col_region in enumerate(m.col_regions): assert m[i, j] == max(row_region.ix, col_region.ix) m = self.rmt.matrix(key=('chr3', 'chr2'), score_field='bar', mask=False) for i, row_region in enumerate(m.row_regions): for j, col_region in enumerate(m.col_regions): assert m[i, j] == max(row_region.ix, col_region.ix)
def _get_nodes_from_key(self, key, as_index=False): return RegionMatrixTable._get_nodes_from_key(self, key, as_index=as_index)
def close(self, **kwargs): RegionMatrixTable.close(self, **kwargs)
def _edges_iter(self): return RegionMatrixTable._edges_iter(self)
def edges_sorted(self, sortby, *args, **kwargs): """ See :class:`~RegionMatrixTable` """ return RegionMatrixTable.edges_sorted(self, sortby, *args, **kwargs)
def _nodes_iter(self): return RegionMatrixTable._nodes_iter(self)
def get_edge(self, ix, lazy=False, **kwargs): """ See :class:`~RegionMatrixTable` """ return RegionMatrixTable.get_edge(self, ix, lazy=lazy)
def get_node(self, key): """ See :class:`~RegionMatrixTable` """ return RegionMatrixTable.get_node(self, key)
def _getitem_nodes(self, key, as_index=False): return RegionMatrixTable._getitem_nodes(self, key, as_index=as_index)