Skip to content

Commit e543111

Browse files
committed
Added mean region width
1 parent e171d33 commit e543111

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

bindings/python/gtars/models/__init__.pyi

+6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class RegionSet:
8282
"""
8383
...
8484

85+
def mean_region_width(self) -> int:
86+
"""
87+
Mean width of the regions
88+
"""
89+
...
90+
8591
def __len__(self) -> int:
8692
"""
8793
Size of the regionset

bindings/python/src/models/region_set.rs

+4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ impl PyRegionSet {
157157
self.regionset.sort();
158158
Ok(())
159159
}
160+
161+
fn mean_region_width(&self) -> PyResult<u32> {
162+
Ok(self.regionset.mean_region_width())
163+
}
160164
}
161165

162166
#[pyclass(name = "TokenizedRegionSet", module = "gtars.models")]

gtars/src/common/models/region_set.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ impl TryFrom<&Path> for RegionSet {
5050

5151
let reader = match path.is_file() {
5252
true => get_dynamic_reader(path).expect("!Can't read file"),
53-
false => get_dynamic_reader_from_url(path).expect("!Can't get file neither from path or url!")
53+
false => get_dynamic_reader_from_url(path)
54+
.expect("!Can't get file neither from path or url!"),
5455
};
5556

5657
let mut header: String = String::new();
@@ -368,6 +369,20 @@ impl RegionSet {
368369
false
369370
}
370371

372+
pub fn mean_region_width(&self) -> u32 {
373+
if self.is_empty() {
374+
return 0;
375+
}
376+
let sum: u32 = self
377+
.regions
378+
.iter()
379+
.map(|region| region.end - region.start)
380+
.sum();
381+
let count: u32 = self.regions.len() as u32;
382+
383+
sum / count
384+
}
385+
371386
///
372387
/// Get number of regions in RegionSet
373388
///

0 commit comments

Comments
 (0)