Skip to content
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

fix: Check against HN tag instead of is_unmapped property #69

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions prymer/offtarget/bwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ def __str__(self) -> str:

@dataclass(init=True, frozen=True)
class BwaResult:
"""Represents zero or more hits or alignments found by BWA for the given query. The number of
hits found may be more than the number of hits listed in the `hits` attribute.
"""
Represents zero or more hits or alignments found by BWA for the given query.

The number of hits found may be more than the number of hits listed in the `hits` attribute.

Attributes:
query: the query (as given, no RC'ing)
Expand Down Expand Up @@ -346,7 +348,8 @@ def map_all(self, queries: list[Query]) -> list[BwaResult]:
return results

def _to_result(self, query: Query, rec: pysam.AlignedSegment) -> BwaResult:
"""Converts the query and alignment to a result.
"""
Convert the query and alignment to a result.

Args:
query: the original query
Expand All @@ -357,17 +360,10 @@ def _to_result(self, query: Query, rec: pysam.AlignedSegment) -> BwaResult:
"Query and Results are out of order" f"Query=${query.id}, Result=${rec.query_name}"
)

num_hits: Optional[int] = int(rec.get_tag("HN")) if rec.has_tag("HN") else None
if rec.is_unmapped:
if num_hits is not None and num_hits > 0:
raise ValueError(f"Read was unmapped but num_hits > 0: {rec}")
return BwaResult(query=query, hit_count=0, hits=[])
elif num_hits > self.max_hits:
return BwaResult(query=query, hit_count=num_hits, hits=[])
else:
hits = self.to_hits(rec=rec)
hit_count = num_hits if len(hits) == 0 else len(hits)
return BwaResult(query=query, hit_count=hit_count, hits=hits)
num_hits: int = int(rec.get_tag("HN")) if rec.has_tag("HN") else 0
hits: list[BwaHit] = self.to_hits(rec=rec) if 0 < num_hits <= self.max_hits else []
msto marked this conversation as resolved.
Show resolved Hide resolved

return BwaResult(query=query, hit_count=num_hits, hits=hits)

def to_hits(self, rec: AlignedSegment) -> list[BwaHit]:
"""Extracts the hits from the given alignment. Beyond the current alignment
Expand Down
Loading