-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e97d2fd
commit 4d6dbf5
Showing
7 changed files
with
101 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ build/ | |
run_dir/ | ||
.venv/ | ||
example_out/ | ||
__pycache__/ | ||
|
||
test/data/d1_* | ||
test/data/d2_* | ||
|
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,72 @@ | ||
""" | ||
Import this file if you want to use rawhash2 in Python. | ||
""" | ||
|
||
from pathlib import Path | ||
from typing import List, TypeVar | ||
import cppyy | ||
import cppyy.ll | ||
import contextlib | ||
import pyslow5 | ||
import numpy as np | ||
|
||
def get_read_signal(slow5_filename, read_id: str): | ||
with contextlib.closing(pyslow5.Open(slow5_filename, "r")) as s5: | ||
return s5.get_read(read_id, pA=True)["signal"] | ||
|
||
def prepare_signal_for_rawhash(raw_signal: np.ndarray[float]): | ||
""" | ||
Remove outliers, convert to numpy | ||
raw_signal should be floats (with offset, range, digitisation scaling) | ||
""" | ||
raw_signal = raw_signal[(raw_signal > 30.) & (raw_signal < 200.)] | ||
return raw_signal | ||
|
||
class Alignment: | ||
def __init__(self, contig, ref_start, ref_end, is_pos_strand): | ||
self.contig = contig | ||
self.ref_start = ref_start | ||
self.ref_end = ref_end | ||
self.is_pos_strand = is_pos_strand | ||
@staticmethod | ||
def from_cppyy(alignment): | ||
return Alignment( | ||
contig=alignment.contig, | ||
ref_start=alignment.ref_start, | ||
ref_end=alignment.ref_end, | ||
is_pos_strand=alignment.is_pos_strand, | ||
) | ||
def __repr__(self): | ||
return f"Alignment(contig: {self.contig}, start: {self.ref_start}, end: {self.ref_end}, pos_strand: {self.is_pos_strand})" | ||
|
||
def load_rawhash_wrapper_lib(rawhash_lib_dir: Path): | ||
rawhash_lib_dir = Path(rawhash_lib_dir) | ||
header_file = rawhash_lib_dir / "src/rawhash_wrapper.hpp" | ||
# library_file = rawhash_lib_dir / "build/src/librawhash2_wrapper.so" | ||
cppyy.include(str(header_file)) | ||
cppyy.add_library_path("/home/mmordig/rawhash_project/rawhash2_new/build/src") # for shared libs | ||
cppyy.load_library("librawhash2_wrapper") | ||
|
||
# define type RawHashMapper | ||
|
||
RawHashMapper = TypeVar("RawHashMapper") | ||
def get_rawhash_mapper(rawhash_args: List[str]) -> RawHashMapper: | ||
args = ["my_dummy_program"] + rawhash_args | ||
mapper = cppyy.gbl.RawHashMapper(len(args), args) | ||
return mapper | ||
|
||
def get_rawhash_alignments(mapper: RawHashMapper, raw_signal: np.ndarray[float]) -> List[Alignment]: | ||
raw_signal = np.ascontiguousarray(raw_signal, dtype=np.float32) | ||
alignments = mapper.map(cppyy.ll.cast["float*"](raw_signal.ctypes.data), len(raw_signal)) | ||
# return alignments | ||
return [Alignment.from_cppyy(alignment) for alignment in alignments] | ||
|
||
def parse_paf_line(line): | ||
"""returns read_id, alignment""" | ||
fields = line.strip().split("\t") | ||
return fields[0], Alignment( | ||
contig=fields[5], | ||
ref_start=int(fields[7]), | ||
ref_end=int(fields[8]), | ||
is_pos_strand=fields[4] == "+", | ||
) |
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
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