Skip to content

Commit

Permalink
clean up repr and str methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Dec 27, 2023
1 parent e250b35 commit 7591231
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 23 deletions.
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ Currently supports `SummarizedExperiment` & `RangedSummarizedExperiment` classes
First create necessary sample data

```python
from random import random
import pandas as pd
import numpy as np
from genomicranges import GenomicRanges
from biocframe import BiocFrame

nrows = 200
ncols = 6
counts = np.random.rand(nrows, ncols)
df_gr = pd.DataFrame(
row_data = BiocFrame(
{
"seqnames": [
"chr1",
Expand All @@ -51,9 +52,7 @@ df_gr = pd.DataFrame(
}
)

gr = genomicranges.from_pandas(df_gr)

colData = pd.DataFrame(
col_data = pd.DataFrame(
{
"treatment": ["ChIP", "Input"] * 3,
}
Expand All @@ -66,20 +65,41 @@ To create a `SummarizedExperiment`,
from summarizedexperiment import SummarizedExperiment

tse = SummarizedExperiment(
assays={"counts": counts}, row_data=df_gr, col_data=colData
assays={"counts": counts}, row_data=row_data, column_data=col_data
)
```

## output
class: SummarizedExperiment
dimensions: (200, 6)
assays(1): ['counts']
row_data columns(6): ['seqnames', 'starts', 'ends', 'strand', 'score', 'GC']
row_names(0):
column_data columns(1): ['treatment']
column_names(0):
metadata(0):

To create a `RangedSummarizedExperiment`

```python
from summarizedexperiment import RangedSummarizedExperiment
from genomicranges import GenomicRanges

trse = RangedSummarizedExperiment(
assays={"counts": counts}, row_ranges=gr, col_data=colData
assays={"counts": counts}, row_data=row_data, row_ranges=GenomicRanges.from_pandas(row_data.to_pandas()), column_data=col_data
)
```

## output
class: RangedSummarizedExperiment
dimensions: (200, 6)
assays(1): ['counts']
row_data columns(6): ['seqnames', 'starts', 'ends', 'strand', 'score', 'GC']
row_names(0):
column_data columns(1): ['treatment']
column_names(0):
metadata(0):

For more examples, checkout the [documentation](https://biocpy.github.io/SummarizedExperiment/).

<!-- pyscaffold-notes -->
Expand Down
49 changes: 42 additions & 7 deletions src/summarizedexperiment/BaseSE.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,48 @@ def dims(self) -> Tuple[int, int]:
##########################

def __repr__(self) -> str:
pattern = (
f"Class {type(self).__name__} with {self.shape[0]} features and {self.shape[1]} samples \n"
f" assays: {', '.join(list(self.assays.keys()))} \n"
f" row_data: {self._rows.row_names if self._rows is not None else None} \n"
f" column_data: {self._cols.column_names if self._cols is not None else None}"
)
return pattern
"""
Returns:
A string representation.
"""
output = f"{type(self).__name__}(number_of_rows={self.shape[0]}"
output += f", number_of_columns={self.shape[1]}"
output += ", assays=" + ut.print_truncated_list(self.assay_names)
output += ", row_data=" + self._rows.__repr__()
output += ", column_data=" + self._cols.__repr__()

if self._row_names is not None:
output += ", row_names=" + ut.print_truncated_list(self._row_names)

if self._column_names is not None:
output += ", column_names=" + ut.print_truncated_list(self._column_names)

if len(self._metadata) > 0:
output += ", metadata=" + ut.print_truncated_dict(self._metadata)

output += ")"
return output

def __str__(self) -> str:
"""
Returns:
A pretty-printed string containing the contents of this object.
"""
output = f"class: {type(self).__name__}\n"

output += f"dimensions: ({self.shape[0]}, {self.shape[1]})\n"

output += f"assays({len(self.assay_names)}): {ut.print_truncated_list(self.assay_names)}\n"

output += f"row_data columns({len(self._rows.column_names)}): {ut.print_truncated_list(self._rows.column_names)}\n"
output += f"row_names({0 if self._row_names is None else len(self._row_names)}): {' ' if self._row_names is None else ut.print_truncated_list(self._row_names)}\n"

output += f"column_data columns({len(self._cols.column_names)}): {ut.print_truncated_list(self._cols.column_names)}\n"
output += f"column_names({0 if self._column_names is None else len(self._column_names)}): {' ' if self._column_names is None else ut.print_truncated_list(self._column_names)}\n"

output += f"metadata({str(len(self.metadata))}): {ut.print_truncated_list(list(self.metadata.keys()), sep=' ', include_brackets=False, transform=lambda y: y)}\n"

return output

########################
######>> assays <<######
Expand Down
53 changes: 45 additions & 8 deletions src/summarizedexperiment/RangedSummarizedExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,51 @@ def copy(self):
##########################

def __repr__(self) -> str:
pattern = (
f"Class {type(self).__name__} with {self.shape[0]} features and {self.shape[1]} "
"samples \n"
f" assays: {list(self.assays.keys())} \n"
f" row_data: {self._rows.columns if self._rows is not None else None} \n"
f" column_data: {self._cols.columns if self._cols is not None else None}"
)
return pattern
"""
Returns:
A string representation.
"""
output = f"{type(self).__name__}(number_of_rows={self.shape[0]}"
output += f", number_of_columns={self.shape[1]}"
output += ", assays=" + ut.print_truncated_list(self.assay_names)

output += ", row_data=" + self._rows.__repr__()
if self._row_names is not None:
output += ", row_names=" + ut.print_truncated_list(self._row_names)

output += ", column_data=" + self._cols.__repr__()
if self._column_names is not None:
output += ", column_names=" + ut.print_truncated_list(self._column_names)

if self._row_ranges is not None:
output += "row_ranges=" + self._row_ranges.__repr__()

if len(self._metadata) > 0:
output += ", metadata=" + ut.print_truncated_dict(self._metadata)

output += ")"
return output

def __str__(self) -> str:
"""
Returns:
A pretty-printed string containing the contents of this object.
"""
output = f"class: {type(self).__name__}\n"

output += f"dimensions: ({self.shape[0]}, {self.shape[1]})\n"

output += f"assays({len(self.assay_names)}): {ut.print_truncated_list(self.assay_names)}\n"

output += f"row_data columns({len(self._rows.column_names)}): {ut.print_truncated_list(self._rows.column_names)}\n"
output += f"row_names({0 if self._row_names is None else len(self._row_names)}): {' ' if self._row_names is None else ut.print_truncated_list(self._row_names)}\n"

output += f"column_data columns({len(self._cols.column_names)}): {ut.print_truncated_list(self._cols.column_names)}\n"
output += f"column_names({0 if self._column_names is None else len(self._column_names)}): {' ' if self._column_names is None else ut.print_truncated_list(self._column_names)}\n"

output += f"metadata({str(len(self.metadata))}): {ut.print_truncated_list(list(self.metadata.keys()), sep=' ', include_brackets=False, transform=lambda y: y)}\n"

return output

############################
######>> row_ranges <<######
Expand Down
2 changes: 1 addition & 1 deletion src/summarizedexperiment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
del version, PackageNotFoundError

from .SummarizedExperiment import SummarizedExperiment
# from .RangedSummarizedExperiment import RangedSummarizedExperiment
from .RangedSummarizedExperiment import RangedSummarizedExperiment

0 comments on commit 7591231

Please sign in to comment.