Skip to content

Commit

Permalink
Speed up function
Browse files Browse the repository at this point in the history
  • Loading branch information
Chiara Rasi committed Feb 19, 2024
1 parent 3751d73 commit e559c81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/chanjo2/endpoints/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def d4_interval_coverage(query: FileCoverageQuery):
if None in [query.start, query.end]: # Coverage over an entire chromosome
return IntervalCoverage(
mean_coverage=get_d4tools_chromosome_mean_coverage(
d4_file_path=query.coverage_file_path, chromosome=query.chromosome
),
d4_file_path=query.coverage_file_path, chromosomes=[query.chromosome]
)[0][1],
completeness={},
interval_id=interval,
)
Expand Down
30 changes: 16 additions & 14 deletions src/chanjo2/meta/handle_d4.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ def get_intervals_coords_list(
return interval_coords


def get_d4tools_chromosome_mean_coverage(d4_file_path: str, chromosome=str) -> float:
"""Return mean coverage over one entire chromosome."""
def get_d4tools_chromosome_mean_coverage(
d4_file_path: str, chromosomes=List[str]
) -> List[Tuple[str, float]]:
"""Return mean coverage over entire chromosomes."""

chromosomes_stats_mean_cmd: List[str] = subprocess.check_output(
["d4tools", "stat", "-s" "mean", d4_file_path],
text=True,
).splitlines()

chromosomes_coverage: List[Tuple[str, float]] = []
for line in chromosomes_stats_mean_cmd:
stats_data: List[str] = line.split("\t")
if chromosome == stats_data[CHROM_INDEX]:
return float(stats_data[STATS_MEAN_COVERAGE_INDEX])
if stats_data[CHROM_INDEX] in chromosomes:
chromosomes_coverage.append(
(stats_data[CHROM_INDEX], float(stats_data[STATS_MEAN_COVERAGE_INDEX]))
)
return chromosomes_coverage


def get_d4tools_intervals_mean_coverage(
Expand Down Expand Up @@ -345,17 +350,14 @@ def predict_sex(x_cov: float, y_cov: float) -> str:
def get_samples_sex_metrics(d4_file_path: str) -> Dict:
"""Compute coverage over sex chromosomes and predicted sex."""

sex_chroms_coverage: List[float] = [
get_d4tools_chromosome_mean_coverage(
d4_file_path=d4_file_path, chromosome=chrom
)
for chrom in ["X", "Y"]
]
sex_chroms_coverage: List[Tuple[str, float]] = get_d4tools_chromosome_mean_coverage(
d4_file_path=d4_file_path, chromosomes=["X", "Y"]
)

return {
"x_coverage": round(sex_chroms_coverage[0], 1),
"y_coverage": round(sex_chroms_coverage[1], 1),
"x_coverage": round(sex_chroms_coverage[0][1], 1),
"y_coverage": round(sex_chroms_coverage[1][1], 1),
"predicted_sex": predict_sex(
x_cov=sex_chroms_coverage[0], y_cov=sex_chroms_coverage[1]
x_cov=sex_chroms_coverage[0][1], y_cov=sex_chroms_coverage[1][1]
),
}

0 comments on commit e559c81

Please sign in to comment.