From fb995261624510036eb40680ac7511b0c9bddce3 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Wed, 5 Feb 2025 17:17:37 +0000 Subject: [PATCH] Updated iteration on asynchronous aligner --- src/autobigs/engine/analysis/aligners.py | 23 ++++++++++++----------- src/autobigs/engine/analysis/bigsdb.py | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/autobigs/engine/analysis/aligners.py b/src/autobigs/engine/analysis/aligners.py index c21be51..e680c79 100644 --- a/src/autobigs/engine/analysis/aligners.py +++ b/src/autobigs/engine/analysis/aligners.py @@ -10,7 +10,7 @@ from autobigs.engine.structures.alignment import AlignmentStats, PairwiseAlignme class AsyncPairwiseAlignmentEngine(AbstractContextManager): def __enter__(self): - self._thread_pool = ThreadPoolExecutor(self._max_threads) + self._thread_pool = ThreadPoolExecutor(self._max_threads, thread_name_prefix="async-pairwise-alignment") return self def __init__(self, aligner: PairwiseAligner, max_threads: int = 4): @@ -29,19 +29,20 @@ class AsyncPairwiseAlignmentEngine(AbstractContextManager): self._work_complete.put(future) def work(self, reference, query, **associated_data): - alignment_results = sorted(self._aligner.align(reference, query))[0] - top_alignment_stats = alignment_results.counts() + alignments = self._aligner.align(reference, query) + top_alignment = sorted(alignments)[0] + top_alignment_stats = top_alignment.counts() top_alignment_gaps = top_alignment_stats.gaps top_alignment_identities = top_alignment_stats.identities top_alignment_mismatches = top_alignment_stats.mismatches - top_alignment_score = alignment_results.score # type: ignore + top_alignment_score = top_alignment.score # type: ignore return PairwiseAlignment( - alignment_results.sequences[0], - alignment_results.sequences[1], - alignment_results.indices[0], - alignment_results.indices[1], + top_alignment.sequences[0], + top_alignment.sequences[1], + top_alignment.indices[0], + top_alignment.indices[1], AlignmentStats( - percent_identity=top_alignment_identities/alignment_results.length, + percent_identity=top_alignment_identities/top_alignment.length, mismatches=top_alignment_mismatches, gaps=top_alignment_gaps, score=top_alignment_score @@ -50,8 +51,8 @@ class AsyncPairwiseAlignmentEngine(AbstractContextManager): async def next_completed(self) -> Union[tuple[PairwiseAlignment, dict[str, Any]], None]: if self._work_complete.empty() and len(self._work_left): return None - future_now: Future = await asyncio.wrap_future(self._work_complete.get()) - completed: tuple[PairwiseAlignment, dict[str, Any]] = (future_now).result() + future_now = await asyncio.wrap_future(self._work_complete.get()) + completed: tuple[PairwiseAlignment, dict[str, Any]] = future_now self._work_left.remove(future_now) return completed diff --git a/src/autobigs/engine/analysis/bigsdb.py b/src/autobigs/engine/analysis/bigsdb.py index 14feb1b..1f7954d 100644 --- a/src/autobigs/engine/analysis/bigsdb.py +++ b/src/autobigs/engine/analysis/bigsdb.py @@ -209,7 +209,7 @@ class LocalBIGSdbMLSTProfiler(BIGSdbMLSTProfiler): async def determine_mlst_allele_variants(self, query_sequence_strings: Iterable[str]) -> AsyncGenerator[Allele, Any]: aligner = PairwiseAligner("blastn") aligner.mode = "local" - with AsyncPairwiseAlignmentEngine(aligner) as aligner_engine: + with AsyncPairwiseAlignmentEngine(aligner, max_threads=2) as aligner_engine: for query_sequence_string in query_sequence_strings: for locus in self._loci: async for allele_variant in read_fasta(self.get_locus_cache_path(locus)):