pyhiv.align package

Submodules

pyhiv.align.align_with_reference module

pyhiv.align.align_with_reference.align_with_references(test_sequence, references_dir=None, n_jobs=None, alignment_tool='edlib-HW', kmer_size=15, reference_top_k=30, allowed_reference_accessions=None, include_alignment_scores=False, executor=None)[source]

Aligns a test sequence with reference sequences in parallel and returns the best match.

Parameters:
  • test_sequence (SeqRecord) – A BioPython SeqRecord object representing the test sequence.

  • references_dir (Path, optional) – Path to the directory containing reference sequences. Defaults to REFERENCE_GENOMES_FASTAS_DIR, containing reference genomes for HIV-1 subtyping.

  • n_jobs (int, optional) – Number of worker processes to use for parallel processing. Defaults to using all available CPU cores. Ignored when executor is provided.

  • alignment_tool (str, optional) – Alignment tool to use. Defaults to edlib-HW.

  • kmer_size (int, optional) – K-mer size used to prefilter candidate references before alignment.

  • reference_top_k (int, optional) – Number of top k-mer ranked references to align. Use 0 or a negative value to align all references.

  • allowed_reference_accessions (set[str], optional) – Reference accessions allowed for alignment. If omitted, all FASTA references in references_dir are eligible.

  • include_alignment_scores (bool, optional) – If True, append a ranked list of (score, reference_name) tuples to the returned best alignment.

  • executor (Executor, optional) – A pre-existing executor to submit alignment tasks to. When provided, it is reused as-is and its lifecycle stays owned by the caller, instead of this call creating and tearing down its own process pool. Pass a single shared executor when aligning many sequences in a batch so worker processes are started once and reused across all of them, rather than per call.

Returns:

A tuple containing the test sequence, reference sequence, and the reference file name with the best alignment.

Return type:

Tuple[str, str, str]

pyhiv.align.align_with_reference.build_reference_kmer_index(references, size=15)[source]
Parameters:
  • references (List[Bio.SeqRecord.SeqRecord])

  • size (int)

Return type:

List[set[str]]

pyhiv.align.align_with_reference.calculate_alignment_score(seq1, seq2)[source]

Calculate the alignment score between two sequences.

Parameters:
  • seq1 (str) – First sequence to compare.

  • seq2 (str) – Second sequence to compare.

Returns:

Number of positions where the sequences are equal.

Return type:

int

pyhiv.align.align_with_reference.kmer_shortlist(test_sequence, references, size=15, top_k=30, reference_kmers=None)[source]
Parameters:
  • test_sequence (Bio.SeqRecord.SeqRecord)

  • references (List[Bio.SeqRecord.SeqRecord])

  • size (int)

  • top_k (int)

  • reference_kmers (List[set[str]] | None)

Return type:

List[Bio.SeqRecord.SeqRecord]

pyhiv.align.align_with_reference.kmers(sequence, size)[source]
Parameters:
  • sequence (str)

  • size (int)

Return type:

set[str]

pyhiv.align.align_with_reference.process_alignment(test_seq, ref_seq, alignment_tool='edlib-HW')[source]

Aligns test sequence with a reference sequence and calculates the score.

Parameters:
  • test_seq (SeqRecord) – A BioPython SeqRecord object representing the test sequence.

  • ref_seq (SeqRecord) – A BioPython SeqRecord object representing the reference sequence.

  • alignment_tool (str)

Returns:

A tuple containing the alignment score, the aligned test sequence, the aligned reference sequence, and the reference sequence name.

Return type:

Tuple[int, str, str, str]

pyhiv.align.align_with_reference.reference_accession_from_name(reference_name)[source]
Parameters:

reference_name (str)

Return type:

str

pyhiv.align.align_with_reference.resolve_worker_count(n_jobs)[source]

Resolve the number of parallel worker processes to use for alignment.

Falls back to all available CPU cores when n_jobs is not given (None or 0), matching the documented default behaviour.

Parameters:

n_jobs (int | None)

Return type:

int

pyhiv.align.famsa module

pyhiv.align.famsa.pyfamsa_align(test_seq, ref_seq)[source]

Aligns a test sequence to a reference sequence using the pyfamsa library.

Parameters:
  • test_seq (SeqRecord) – The test sequence to align. Should be a Biopython SeqRecord object containing the test sequence ID and sequence.

  • ref_seq (SeqRecord) – The reference sequence to align against. Should be a Biopython SeqRecord object containing the reference sequence ID and sequence.

Returns:

test_alignedstr

The aligned test sequence.

ref_alignedstr

The aligned reference sequence.

Return type:

tuple

pyhiv.align.tools module

pyhiv.align.tools.align_sequences(test_seq, ref_seq, alignment_tool='edlib-HW')[source]

Align a test sequence to a reference sequence using the selected tool.

Recommended modes:

  • edlib-HW:

    Use when the test sequence is a small region that should be located inside a longer reference sequence. Example: HIV-1 protease against a full HIV-1 genome.

Returns:

query_aligned, reference_aligned

Return type:

Tuple of

Parameters:
  • test_seq (Bio.SeqRecord.SeqRecord)

  • ref_seq (Bio.SeqRecord.SeqRecord)

  • alignment_tool (str | None)

pyhiv.align.tools.edlib_hw_align(test_seq, ref_seq)[source]

Align a shorter query sequence inside a longer reference using edlib HW mode.

This does three things:

  1. Uses edlib HW mode to locate the full query inside the reference.

  2. Re-aligns the query against only that matched reference window using NW.

  3. Projects the query back onto full reference coordinates by padding with ‘-’ before and after the matched region.

The returned alignment has exactly len(ref_seq) columns. This means query insertion columns relative to the reference are removed, because they do not have reference coordinates.

Parameters:
  • test_seq (Bio.SeqRecord.SeqRecord)

  • ref_seq (Bio.SeqRecord.SeqRecord)

Return type:

Tuple[str, str]

pyhiv.align.tools.get_mafft_binary()[source]

Resolve MAFFT from an environment override or PATH.

Return type:

str | None

pyhiv.align.tools.mafft_align(test_seq, ref_seq)[source]

Align two sequences using the external MAFFT executable.

Parameters:
  • test_seq (Bio.SeqRecord.SeqRecord)

  • ref_seq (Bio.SeqRecord.SeqRecord)

Return type:

Tuple[str, str]

pyhiv.align.tools.normalize_alignment_tool(alignment_tool)[source]

Return the canonical alignment tool name.

Parameters:

alignment_tool (str | None)

Return type:

str

pyhiv.align.tools.parasail_nw_align(test_seq, ref_seq)[source]

Align two sequences using parasail Needleman-Wunsch with traceback.

Parameters:
  • test_seq (Bio.SeqRecord.SeqRecord)

  • ref_seq (Bio.SeqRecord.SeqRecord)

Return type:

Tuple[str, str]

pyhiv.align.tools.validate_alignment_tool_available(alignment_tool='edlib-HW')[source]

Validate that the selected alignment backend is usable in this environment.

Worker-level alignment errors are logged per reference and skipped, so missing optional backends must be detected before the pipeline starts.

Parameters:

alignment_tool (str | None)

Return type:

str

Module contents