-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from Clinical-Genomics/separate_coverage_stat…
…s_handler Separate coverage stats handler
- Loading branch information
Showing
4 changed files
with
70 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import subprocess | ||
import tempfile | ||
from typing import List, Tuple | ||
|
||
from chanjo2.constants import CHROMOSOMES | ||
|
||
CHROM_INDEX = 0 | ||
START_INDEX = 1 | ||
STOP_INDEX = 2 | ||
STATS_MEAN_COVERAGE_INDEX = 3 | ||
|
||
|
||
def get_d4tools_intervals_mean_coverage( | ||
d4_file_path: str, intervals: List[str] | ||
) -> List[float]: | ||
"""Return the mean value over a list of intervals of a d4 file.""" | ||
|
||
if intervals: | ||
tmp_bed_file = tempfile.NamedTemporaryFile() | ||
with open(tmp_bed_file.name, "w") as bed_file: | ||
bed_file.write("\n".join(intervals)) | ||
|
||
return get_d4tools_intervals_coverage( | ||
d4_file_path=d4_file_path, bed_file_path=tmp_bed_file.name | ||
) | ||
chromosomes_mean_cov = get_d4tools_chromosome_mean_coverage( | ||
d4_file_path=d4_file_path, chromosomes=CHROMOSOMES | ||
) | ||
return [chrom_cov[1] for chrom_cov in chromosomes_mean_cov] | ||
|
||
|
||
def get_d4tools_intervals_coverage( | ||
d4_file_path: str, bed_file_path: str | ||
) -> List[float]: | ||
"""Return the coverage for intervals of a d4 file that are found in a bed file.""" | ||
|
||
d4tools_stats_mean_cmd: str = subprocess.check_output( | ||
["d4tools", "stat", "--region", bed_file_path, d4_file_path, "--stat", "mean"], | ||
text=True, | ||
) | ||
return [ | ||
float(line.rstrip().split("\t")[3]) | ||
for line in d4tools_stats_mean_cmd.splitlines() | ||
] | ||
|
||
|
||
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 stats_data[CHROM_INDEX] in chromosomes: | ||
chromosomes_coverage.append( | ||
(stats_data[CHROM_INDEX], float(stats_data[STATS_MEAN_COVERAGE_INDEX])) | ||
) | ||
return chromosomes_coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters