-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: remove pysam and bwa warmup hack #65
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
>>> bwa.map_all(queries=[query]) | ||
[BwaResult(query=Query(id='NA', bases='AAAAAA'), hit_count=3968, hits=[])] | ||
>>> bwa.close() | ||
True | ||
|
||
``` | ||
""" # noqa: E501 | ||
|
@@ -45,10 +46,10 @@ | |
from typing import cast | ||
|
||
import pysam | ||
from fgpyo import sam | ||
from fgpyo import sequence | ||
from fgpyo.sam import Cigar | ||
from pysam import AlignedSegment | ||
from pysam import AlignmentHeader | ||
|
||
from prymer.api import coordmath | ||
from prymer.util.executable_runner import ExecutableRunner | ||
|
@@ -201,6 +202,7 @@ class BwaAlnInteractive(ExecutableRunner): | |
reverse_complement: reverse complement each query sequence before alignment. | ||
include_alt_hits: if True include hits to references with names ending in _alt, otherwise | ||
do not include them. | ||
header: the SAM alignment header. | ||
clintval marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
def __init__( | ||
|
@@ -284,20 +286,14 @@ def __init__( | |
|
||
super().__init__(command=command) | ||
|
||
# HACK ALERT | ||
# This is a hack. By trial and error, pysam.AlignmentFile() will block reading unless | ||
# there's at least a few records due to htslib wanting to read a few records for format | ||
# auto-detection. Lame. So a hundred queries are sent to the aligner to align enable the | ||
# htslib auto-detection to complete, and for us to be able to read using pysam. | ||
num_warmup: int = 100 | ||
for i in range(num_warmup): | ||
query = Query(id=f"ignoreme:{i}", bases="A" * 100) | ||
fastq_str = query.to_fastq(reverse_complement=self.reverse_complement) | ||
self._subprocess.stdin.write(fastq_str) | ||
self.__signal_bwa() # forces the input to be sent to the underlying process. | ||
self._reader = sam.reader(path=self._subprocess.stdout, file_type=sam.SamFileType.SAM) | ||
for _ in range(num_warmup): | ||
next(self._reader) | ||
header = [] | ||
for line in self._subprocess.stdout: | ||
if line.startswith("@"): | ||
header.append(line) | ||
if line.startswith("@PG"): | ||
break | ||
clintval marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.header = AlignmentHeader.from_text("".join(header)) | ||
|
||
def __signal_bwa(self) -> None: | ||
"""Signals BWA to process the queries""" | ||
|
@@ -334,13 +330,18 @@ def map_all(self, queries: list[Query]) -> list[BwaResult]: | |
for query in queries: | ||
fastq_str = query.to_fastq(reverse_complement=self.reverse_complement) | ||
self._subprocess.stdin.write(fastq_str) | ||
self.__signal_bwa() # forces the input to be sent to the underlying process. | ||
|
||
# Force the input to be sent to the underlying process. | ||
self.__signal_bwa() | ||
|
||
# Read back the results | ||
results: list[BwaResult] = [] | ||
for query in queries: | ||
# get the next alignment and convert to a result | ||
results.append(self._to_result(query=query, rec=next(self._reader))) | ||
line: str = next(self._subprocess.stdout).strip() | ||
clintval marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert not line.startswith("@"), f"SAM record must not start with '@'! {line}" | ||
alignment = AlignedSegment.fromstring(line, self.header) | ||
results.append(self._to_result(query=query, rec=alignment)) | ||
|
||
return results | ||
|
||
|
@@ -412,7 +413,3 @@ def to_hits(self, rec: AlignedSegment) -> list[BwaHit]: | |
hits = [hit for hit in hits if not hit.refname.endswith("_alt")] | ||
|
||
return hits | ||
|
||
def close(self) -> None: | ||
self._reader.close() | ||
super().close() | ||
Comment on lines
-416
to
-418
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although we no longer need to override this method, it should have conformed to the same shape as the superclass: def close(self) -> bool:
self._reader.close()
return super().close() |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of: https://github.com/fulcrumgenomics/prymer/pull/65/files#r1787936312