Skip to content

Commit

Permalink
update GCoverages
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Kuo committed Mar 4, 2024
1 parent e2ac991 commit 24f29eb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
22 changes: 11 additions & 11 deletions genomkit/coverages/gcoverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GCoverages:
genomic coverages. It provides utilities for handling and analyzing the
interactions of many genomic coverages.
"""
def __init__(self, bin_size: int = 1, load: str = ""):
def __init__(self, bin_size: int = 1, load: str = "", windows=None):
"""Initialize GCoverages object.
:param bin_size: Size of the bin for coverage calculation.
Expand All @@ -33,7 +33,7 @@ def __init__(self, bin_size: int = 1, load: str = ""):
self.calculate_coverage_GRegions(regions=regions,
scores=regions)

def load_coverage_from_bigwig(self, filename: str, windows=False):
def load_coverage_from_bigwig(self, filename: str, windows=None):
"""Load coverage data from a bigwig file.
:param filename: Path to the bigwig file.
Expand Down Expand Up @@ -72,7 +72,7 @@ def load_coverage_from_bigwig(self, filename: str, windows=False):
self.coverage[window] = coverage
bw.close()

def calculate_coverage_from_bam(self, filename: str, windows=False):
def calculate_coverage_from_bam(self, filename: str, windows=None):
"""Calculate coverage from a BAM file.
:param filename: Path to the BAM file.
Expand Down Expand Up @@ -105,26 +105,26 @@ def calculate_coverage_from_bam(self, filename: str, windows=False):
len(self.coverage[r]),
self.bin_size)]

def calculate_coverage_GRegions(self, regions, scores,
def calculate_coverage_GRegions(self, scores, windows=None,
strandness: bool = False):
"""Calculate the coverage from two GRegions. `regions` defines the loci
"""Calculate the coverage from two GRegions. `windows` defines the loci
for the coverage `scores` contains the scores loaded into the coverage.
:param regions: Define the loci and the length of the coverage
:type regions: GRegions
:param windows: Define the windows and the length of the coverage
:type windows: GRegions
:param scores: Provide the scores for calculating the coverage
:type scores: GRegions
:param strandness: Make this operation strandness specific, defaults
to False
:type strandness: bool, optional
"""
from genomkit import GRegions
assert isinstance(regions, GRegions)
assert isinstance(windows, GRegions)
assert isinstance(scores, GRegions)
filtered_scores = scores.intersect(target=regions,
filtered_scores = scores.intersect(target=windows,
mode="ORIGINAL")
for region in tqdm(regions, desc="Calculating coverage",
total=len(regions)):
for region in tqdm(windows, desc="Calculating coverage",
total=len(windows)):
self.coverage[region] = np.zeros(shape=len(region) //
self.bin_size)
for target in filtered_scores:
Expand Down
43 changes: 41 additions & 2 deletions genomkit/coverages/gcoverages_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,48 @@ def __init__(self, name: str = "", load_dict=None):
paths, defaults to None
:type load_dict: dict, optional
"""
self.name = name
self.collection = OrderedDict()
if load_dict:
for name, filename in load_dict.items():
self.add(name=name,
regions=GCoverages(name=name,
load=filename))
gcov=GCoverages(name=name,
load=filename))

def add(self, name, gcov):
"""Add a GCoverages into GCoveragesSet.
:param name: name of a GCoverages
:type name: str
:param gcov: A GCoverages
:type gcov: GCoverages
"""
self.collection[name] = gcov

def __len__(self):
"""Return the number of GCoverages in this set.
:return: Number of GCoverages
:rtype: int
"""
return len(self.collection)

def __getattr__(self, key):
if key in self.collection:
return self.collection[key]
else:
raise AttributeError(
f"'{self.collection.__class__.__name__}'"
f" object has no attribute '{key}'"
)

def __setattr__(self, key, value):
self.collection[key] = value

def get_names(self):
"""Return the names of all GCoverages.
:return: Names
:rtype: list
"""
return list(self.collection.keys())
2 changes: 1 addition & 1 deletion tests/test_gcoverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_calculate_coverage_GRegions(self):
load=os.path.join(script_path,
"test_files/bed/example2.bed"))
cov = GCoverages()
cov.calculate_coverage_GRegions(regions=regions, scores=regions)
cov.calculate_coverage_GRegions(windows=regions, scores=regions)
self.assertEqual(len(cov.coverage.keys()), 4)
self.assertEqual(cov.coverage[regions[0]][0], 10)
self.assertEqual(cov.coverage[regions[1]][0], 20)
Expand Down

0 comments on commit 24f29eb

Please sign in to comment.