pyhiv.loading package

Submodules

pyhiv.loading.read_fastas module

pyhiv.loading.read_fastas.discover_fasta_files(input_folder, exclude_dirs=())[source]

Recursively find FASTA files with supported extensions under input_folder.

Parameters:
  • input_folder (Path) – Directory to search, including its subdirectories.

  • exclude_dirs (Iterable[Path], optional) – Directories to skip entirely, e.g. a run’s own output directory, so that previously generated alignment or gene-region FASTA files aren’t picked up again as new input on a later run.

Returns:

Sorted list of matching FASTA file paths.

Return type:

List[Path]

pyhiv.loading.read_fastas.read_input_fastas(input_folder, exclude_dirs=())[source]

Reads nucleotide FASTA files (.fasta, .fa, .fna, .ffn) from a specified input folder and its subdirectories.

Parameters:
  • input_folder (Path) – Path to the folder containing the FASTA files.

  • exclude_dirs (Iterable[Path], optional) – Directories to skip, e.g. this run’s own output directory, so that previously generated result FASTAs aren’t re-read as new input.

Returns:

A list of BioPython SeqRecord objects containing sequence IDs and sequences. Each record’s annotations include “source_file” (the file’s path relative to input_folder, using “/” separators) and “source_path” (its full path).

Return type:

List[SeqRecord]

Raises:

NotADirectoryError – If the input folder does not exist or is not a directory.

pyhiv.loading.reference_update module

class pyhiv.loading.reference_update.CrfReference(label: 'str', crf: 'str', strain: 'str', accession: 'str')[source]

Bases: object

Parameters:
  • label (str)

  • crf (str)

  • strain (str)

  • accession (str)

accession: str
crf: str
label: str
strain: str
class pyhiv.loading.reference_update.DesiredReference(accession: 'str', subtype: 'str', sequence: 'str | None', source: 'str')[source]

Bases: object

Parameters:
  • accession (str)

  • subtype (str)

  • sequence (str | None)

  • source (str)

accession: str
sequence: str | None
source: str
subtype: str
exception pyhiv.loading.reference_update.GenBankRecordPartial[source]

Bases: RuntimeError

Raised when a GenBank record is not complete or nearly complete.

exception pyhiv.loading.reference_update.GenBankRecordUnavailable[source]

Bases: RuntimeError

Raised when NCBI has no GenBank record for a requested accession.

class pyhiv.loading.reference_update.LanlAlignment(year: 'int', content: 'str')[source]

Bases: object

Parameters:
  • year (int)

  • content (str)

content: str
property filename: str
year: int
class pyhiv.loading.reference_update.LanlAlignmentUpdateResult(year: 'int | None', path: 'Path | None', updated: 'bool', fasta_records: 'int', fasta_files_added: 'int', fasta_files_updated: 'int', fasta_files_removed: 'int', fasta_files_reclassified: 'int' = 0)[source]

Bases: object

Parameters:
  • year (int | None)

  • path (Path | None)

  • updated (bool)

  • fasta_records (int)

  • fasta_files_added (int)

  • fasta_files_updated (int)

  • fasta_files_removed (int)

  • fasta_files_reclassified (int)

fasta_files_added: int
fasta_files_reclassified: int = 0
fasta_files_removed: int
fasta_files_updated: int
fasta_records: int
path: Path | None
updated: bool
year: int | None
class pyhiv.loading.reference_update.LanlReferenceRecord(accession: 'str', subtype: 'str', sequence: 'str')[source]

Bases: object

Parameters:
  • accession (str)

  • subtype (str)

  • sequence (str)

accession: str
sequence: str
subtype: str
exception pyhiv.loading.reference_update.LocalReferenceSequenceConflict[source]

Bases: RuntimeError

Raised when local FASTAs for one accession contain different sequences.

exception pyhiv.loading.reference_update.ReconciliationError[source]

Bases: RuntimeError

Raised when reference FASTAs cannot be reconciled deterministically.

class pyhiv.loading.reference_update.UpdateResult(added_sequences: 'int', added_rows: 'int', updated_rows: 'int', updated_feature_rows: 'int', total_references: 'int', sequences_with_locations: 'Path', reference_fastas_dir: 'Path', lanl_alignment_year: 'int | None' = None, lanl_alignment_updated: 'bool' = False, lanl_fasta_records: 'int' = 0, lanl_fasta_files_added: 'int' = 0, lanl_fasta_files_updated: 'int' = 0, lanl_fasta_files_removed: 'int' = 0, reclassified_references: 'int' = 0, partial_sequences_rejected: 'int' = 0, duplicate_rows_removed: 'int' = 0, crfs_with_multiple_references: 'int' = 0, pending_ncbi_references: 'int' = 0, pending_ncbi_accessions: 'tuple[str, ...]' = (), failed_sequences: 'int' = 0, failed_accessions: 'tuple[str, ...]' = ())[source]

Bases: object

Parameters:
  • added_sequences (int)

  • added_rows (int)

  • updated_rows (int)

  • updated_feature_rows (int)

  • total_references (int)

  • sequences_with_locations (Path)

  • reference_fastas_dir (Path)

  • lanl_alignment_year (int | None)

  • lanl_alignment_updated (bool)

  • lanl_fasta_records (int)

  • lanl_fasta_files_added (int)

  • lanl_fasta_files_updated (int)

  • lanl_fasta_files_removed (int)

  • reclassified_references (int)

  • partial_sequences_rejected (int)

  • duplicate_rows_removed (int)

  • crfs_with_multiple_references (int)

  • pending_ncbi_references (int)

  • pending_ncbi_accessions (tuple[str, ...])

  • failed_sequences (int)

  • failed_accessions (tuple[str, ...])

added_rows: int
added_sequences: int
crfs_with_multiple_references: int = 0
duplicate_rows_removed: int = 0
failed_accessions: tuple[str, ...] = ()
failed_sequences: int = 0
lanl_alignment_updated: bool = False
lanl_alignment_year: int | None = None
lanl_fasta_files_added: int = 0
lanl_fasta_files_removed: int = 0
lanl_fasta_files_updated: int = 0
lanl_fasta_records: int = 0
partial_sequences_rejected: int = 0
pending_ncbi_accessions: tuple[str, ...] = ()
pending_ncbi_references: int = 0
reclassified_references: int = 0
reference_fastas_dir: Path
sequences_with_locations: Path
total_references: int
property up_to_date: bool
updated_feature_rows: int
updated_rows: int
pyhiv.loading.reference_update.accession_is_nonterminal_lanl_name_component(text, match)[source]
Parameters:
  • text (str)

  • match (Match)

Return type:

bool

pyhiv.loading.reference_update.accession_is_parenthesized_reference_strain(text, match)[source]
Parameters:
  • text (str)

  • match (Match)

Return type:

bool

pyhiv.loading.reference_update.accession_ranges_from_text(text)[source]
Parameters:

text (str)

Return type:

tuple[tuple[str, str], …]

pyhiv.loading.reference_update.accessions_from_text(text)[source]
Parameters:

text (str)

Return type:

tuple[str, …]

pyhiv.loading.reference_update.add_crf_references(found, row_references, rejected_by_label)[source]
Parameters:
  • found (dict[str, list[CrfReference]])

  • row_references (tuple[CrfReference, ...])

  • rejected_by_label (dict[str, set[str]])

Return type:

None

pyhiv.loading.reference_update.add_desired_reference(desired, reference)[source]
Parameters:
Return type:

None

pyhiv.loading.reference_update.are_same_crf_number_aliases(first, second)[source]
Parameters:
  • first (str)

  • second (str)

Return type:

bool

pyhiv.loading.reference_update.backup_existing_dir(path)[source]
Parameters:

path (Path)

Return type:

None

pyhiv.loading.reference_update.backup_existing_file(path)[source]
Parameters:

path (Path)

Return type:

None

pyhiv.loading.reference_update.build_desired_reference_map(alignment_records, crf_references)[source]
Parameters:
Return type:

dict[str, DesiredReference]

pyhiv.loading.reference_update.choose_existing_reference_row(rows, current_subtype)[source]
Parameters:
  • rows (list[dict[str, str]])

  • current_subtype (str)

Return type:

dict[str, str] | None

pyhiv.loading.reference_update.classification_context_references_from_text(label, crf, text)[source]
Parameters:
  • label (str)

  • crf (str)

  • text (str)

Return type:

tuple[CrfReference, …]

pyhiv.loading.reference_update.classification_reference_contexts(label, text)[source]
Parameters:
  • label (str)

  • text (str)

Return type:

list[str]

pyhiv.loading.reference_update.clean_feature_name(value)[source]
Return type:

str

pyhiv.loading.reference_update.commit_staged_reference_dataset(staged_references_dir, references_dir, staged_sequences_path, sequences_path)[source]
Parameters:
  • staged_references_dir (Path)

  • references_dir (Path)

  • staged_sequences_path (Path)

  • sequences_path (Path)

Return type:

None

pyhiv.loading.reference_update.commit_staged_reference_fastas(staged_references_dir, references_dir)[source]
Parameters:
  • staged_references_dir (Path)

  • references_dir (Path)

Return type:

None

pyhiv.loading.reference_update.count_crfs_with_multiple_references(crf_references)[source]
Parameters:

crf_references (Mapping[str, CrfReference | Iterable[CrfReference]] | Iterable[CrfReference])

Return type:

int

pyhiv.loading.reference_update.crf_label_number(label)[source]
Parameters:

label (str)

Return type:

str | None

pyhiv.loading.reference_update.dedupe_accessions(accessions)[source]
Parameters:

accessions (list[str])

Return type:

list[str]

pyhiv.loading.reference_update.dedupe_crf_references(references)[source]
Parameters:

references (list[CrfReference])

Return type:

list[CrfReference]

pyhiv.loading.reference_update.default_reference_row(fasta_info)[source]
Parameters:

fasta_info (dict[str, str])

Return type:

dict[str, str]

pyhiv.loading.reference_update.download_lanl_reference_alignment(year)[source]
Parameters:

year (int)

Return type:

str

pyhiv.loading.reference_update.download_latest_lanl_reference_alignment()[source]
Return type:

LanlAlignment

pyhiv.loading.reference_update.duplicate_row_count(existing_table)[source]
Parameters:

existing_table (pandas.DataFrame)

Return type:

int

pyhiv.loading.reference_update.expand_accession_range(start, end)[source]
Parameters:
  • start (str)

  • end (str)

Return type:

tuple[str, …]

pyhiv.loading.reference_update.extract_lanl_alignment_content(html)[source]
Parameters:

html (str)

Return type:

str

pyhiv.loading.reference_update.extract_reference_strain(text, accession)[source]
Parameters:
  • text (str)

  • accession (str)

Return type:

str

pyhiv.loading.reference_update.feature_bounds(feature)[source]
Return type:

tuple[int, int]

pyhiv.loading.reference_update.fetch_genbank_sequence_and_features(accession)[source]
Parameters:

accession (str)

Return type:

tuple[str, dict[str, tuple[int, int]] | None]

pyhiv.loading.reference_update.genome_context_references_from_text(label, crf, text)[source]
Parameters:
  • label (str)

  • crf (str)

  • text (str)

Return type:

tuple[CrfReference, …]

pyhiv.loading.reference_update.genome_reference_contexts(text)[source]
Parameters:

text (str)

Return type:

list[str]

pyhiv.loading.reference_update.get_gene_locations(features)[source]
Return type:

dict[str, tuple[int, int]]

pyhiv.loading.reference_update.has_subgenomic_sequence_annotation(text)[source]
Parameters:

text (str)

Return type:

bool

pyhiv.loading.reference_update.has_valid_genome_context(text)[source]
Parameters:

text (str)

Return type:

bool

pyhiv.loading.reference_update.index_existing_reference_rows(existing_table)[source]
Parameters:

existing_table (pandas.DataFrame)

Return type:

dict[str, list[dict[str, str]]]

pyhiv.loading.reference_update.infer_group(subtype)[source]
Parameters:

subtype (str)

Return type:

str

pyhiv.loading.reference_update.is_hiv2_text(text)[source]
Parameters:

text (str)

Return type:

bool

pyhiv.loading.reference_update.latest_local_lanl_alignment(reference_base_dir)[source]
Parameters:

reference_base_dir (Path)

Return type:

Path | None

pyhiv.loading.reference_update.load_lanl_crf_references()[source]
Return type:

dict[str, tuple[CrfReference, …]]

pyhiv.loading.reference_update.load_lanl_reference_alignment_years()[source]
Return type:

list[int]

pyhiv.loading.reference_update.local_reference_sequence(final_path, local_paths)[source]
Parameters:
  • final_path (Path)

  • local_paths (list[Path])

Return type:

str | None

pyhiv.loading.reference_update.needs_feature_update(features, refresh_features)[source]
Parameters:

refresh_features (bool)

Return type:

bool

pyhiv.loading.reference_update.non_partial_genome_contexts(segment)[source]
Parameters:

segment (str)

Return type:

list[str]

pyhiv.loading.reference_update.normalize_accession(accession)[source]
Parameters:

accession (str)

Return type:

str

pyhiv.loading.reference_update.normalize_crf_references(crf_references)[source]
Parameters:

crf_references (Mapping[str, CrfReference | Iterable[CrfReference]] | Iterable[CrfReference])

Return type:

tuple[CrfReference, …]

pyhiv.loading.reference_update.normalize_sequence(sequence, keep_gaps=False)[source]
Parameters:
  • sequence (str)

  • keep_gaps (bool)

Return type:

str

pyhiv.loading.reference_update.parse_lanl_alignment_records(alignment_csv)[source]
Parameters:

alignment_csv (str)

Return type:

list[LanlReferenceRecord]

pyhiv.loading.reference_update.parse_lanl_crf_detail_text(label, crf, text)[source]
Parameters:
  • label (str)

  • crf (str)

  • text (str)

Return type:

tuple[CrfReference, …]

pyhiv.loading.reference_update.parse_lanl_crf_row(row_html)[source]
Parameters:

row_html (str)

Return type:

tuple[CrfReference, …]

pyhiv.loading.reference_update.parse_reference_fasta(path)[source]
Parameters:

path (Path)

Return type:

dict[str, str]

pyhiv.loading.reference_update.parse_reference_name(stem)[source]
Parameters:

stem (str)

Return type:

dict[str, str]

pyhiv.loading.reference_update.partial_genome_annotation_re()[source]
Return type:

Pattern

pyhiv.loading.reference_update.preferred_crf_alias_label(first, second)[source]
Parameters:
  • first (str)

  • second (str)

Return type:

str

pyhiv.loading.reference_update.preferred_desired_reference(first, second)[source]
Parameters:
Return type:

DesiredReference

pyhiv.loading.reference_update.read_existing_table(path)[source]
Parameters:

path (Path)

Return type:

pandas.DataFrame

pyhiv.loading.reference_update.rebuild_sequences_with_locations(references_dir, sequences_path, fetched_features=None, refresh_features=False, dry_run=False, fetcher=None, sleep_seconds=0.34, output_path=None)[source]
Parameters:
  • references_dir (Path)

  • sequences_path (Path)

  • fetched_features (dict[str, dict[str, tuple[int, int]] | None] | None)

  • refresh_features (bool)

  • dry_run (bool)

  • fetcher (Callable[[str], tuple[str, dict[str, tuple[int, int]] | None]] | None)

  • sleep_seconds (float)

  • output_path (Path | None)

Return type:

dict[str, int]

pyhiv.loading.reference_update.reconcile_reference_fastas(desired_references, references_dir, dry_run=False, output_dir=None, fetcher=None, fetched_features=None)[source]
Parameters:
  • references_dir (Path)

  • dry_run (bool)

  • output_dir (Path | None)

  • fetcher (Callable[[str], tuple[str, dict[str, tuple[int, int]] | None]] | None)

  • fetched_features (dict[str, dict[str, tuple[int, int]] | None] | None)

Return type:

dict[str, int]

pyhiv.loading.reference_update.reference_accessions_in_dir(references_dir)[source]
Parameters:

references_dir (Path)

Return type:

set[str]

pyhiv.loading.reference_update.reference_fasta_content(accession, subtype, sequence)[source]
Parameters:
  • accession (str)

  • subtype (str)

  • sequence (str)

Return type:

str

pyhiv.loading.reference_update.reference_fasta_matches(path, accession, subtype, sequence)[source]
Parameters:
  • path (Path)

  • accession (str)

  • subtype (str)

  • sequence (str)

Return type:

bool

pyhiv.loading.reference_update.reference_fasta_name(accession, subtype)[source]
Parameters:
  • accession (str)

  • subtype (str)

Return type:

str

pyhiv.loading.reference_update.reference_fasta_paths_by_accession(references_dir)[source]
Parameters:

references_dir (Path)

Return type:

dict[str, list[Path]]

pyhiv.loading.reference_update.reference_key(accession, subtype)[source]
Parameters:
  • accession (str)

  • subtype (str)

Return type:

str

pyhiv.loading.reference_update.reference_strain_text(cells, text, match)[source]
Parameters:
  • cells (list[str])

  • text (str)

  • match (Match)

Return type:

str

pyhiv.loading.reference_update.references_from_text(label, crf, text)[source]
Parameters:
  • label (str)

  • crf (str)

  • text (str)

Return type:

tuple[CrfReference, …]

pyhiv.loading.reference_update.sanitize_filename_part(value)[source]
Parameters:

value (str)

Return type:

str

pyhiv.loading.reference_update.save_lanl_alignment_file(reference_base_dir, alignment, dry_run=False)[source]
Parameters:
  • reference_base_dir (Path)

  • alignment (LanlAlignment)

  • dry_run (bool)

Return type:

bool

pyhiv.loading.reference_update.serialize_cell(value)[source]
Return type:

str

pyhiv.loading.reference_update.serialize_features(features)[source]
Parameters:

features (dict[str, tuple[int, int]] | None)

Return type:

str

pyhiv.loading.reference_update.strip_html(value)[source]
Parameters:

value (str)

Return type:

str

pyhiv.loading.reference_update.subgenomic_accessions_from_text(text)[source]
Parameters:

text (str)

Return type:

set[str]

pyhiv.loading.reference_update.unique_backup_path(path)[source]
Parameters:

path (Path)

Return type:

Path

pyhiv.loading.reference_update.update_lanl_reference_alignment(reference_base_dir, references_dir, dry_run=False, alignment_loader=None, crf_loader=None, genbank_fetcher=None)[source]
Parameters:
  • reference_base_dir (Path)

  • references_dir (Path)

  • dry_run (bool)

  • alignment_loader (Callable[[], LanlAlignment] | None)

  • crf_loader (Callable[[], Mapping[str, CrfReference | Iterable[CrfReference]] | Iterable[CrfReference]] | None)

  • genbank_fetcher (Callable[[str], tuple[str, dict[str, tuple[int, int]] | None]] | None)

Return type:

LanlAlignmentUpdateResult

pyhiv.loading.reference_update.update_reference_dataset(base_dir=None, email=None, ncbi_api_key=None, dry_run=False, refresh_features=False, sleep_seconds=0.34, update_lanl_alignment=True, lanl_alignment_loader=None, crf_loader=None, genbank_fetcher=None)[source]
Parameters:
  • base_dir (Path | None)

  • email (str | None)

  • ncbi_api_key (str | None)

  • dry_run (bool)

  • refresh_features (bool)

  • sleep_seconds (float)

  • update_lanl_alignment (bool)

  • lanl_alignment_loader (Callable[[], LanlAlignment] | None)

  • crf_loader (Callable[[], Mapping[str, CrfReference | Iterable[CrfReference]] | Iterable[CrfReference]] | None)

  • genbank_fetcher (Callable[[str], tuple[str, dict[str, tuple[int, int]] | None]] | None)

Return type:

UpdateResult

pyhiv.loading.reference_update.useful_metadata_score(row)[source]
Parameters:

row (dict[str, str])

Return type:

int

pyhiv.loading.reference_update.validate_final_reference_dataset(table, references_dir)[source]
Parameters:
  • table (pandas.DataFrame)

  • references_dir (Path)

Return type:

None

pyhiv.loading.reference_update.validate_hiv1_complete_or_near_complete(record, sequence)[source]
Parameters:

sequence (str)

Return type:

None

pyhiv.loading.reference_update.validate_reference_fasta_dir(references_dir)[source]
Parameters:

references_dir (Path)

Return type:

None

pyhiv.loading.reference_update.write_reference_fasta(path, accession, subtype, sequence)[source]
Parameters:
  • path (Path)

  • accession (str)

  • subtype (str)

  • sequence (str)

Return type:

None

Module contents

pyhiv.loading.discover_fasta_files(input_folder, exclude_dirs=())[source]

Recursively find FASTA files with supported extensions under input_folder.

Parameters:
  • input_folder (Path) – Directory to search, including its subdirectories.

  • exclude_dirs (Iterable[Path], optional) – Directories to skip entirely, e.g. a run’s own output directory, so that previously generated alignment or gene-region FASTA files aren’t picked up again as new input on a later run.

Returns:

Sorted list of matching FASTA file paths.

Return type:

List[Path]

pyhiv.loading.read_input_fastas(input_folder, exclude_dirs=())[source]

Reads nucleotide FASTA files (.fasta, .fa, .fna, .ffn) from a specified input folder and its subdirectories.

Parameters:
  • input_folder (Path) – Path to the folder containing the FASTA files.

  • exclude_dirs (Iterable[Path], optional) – Directories to skip, e.g. this run’s own output directory, so that previously generated result FASTAs aren’t re-read as new input.

Returns:

A list of BioPython SeqRecord objects containing sequence IDs and sequences. Each record’s annotations include “source_file” (the file’s path relative to input_folder, using “/” separators) and “source_path” (its full path).

Return type:

List[SeqRecord]

Raises:

NotADirectoryError – If the input folder does not exist or is not a directory.

pyhiv.loading.validate_reference_paths(paths=None)[source]

Ensure all required reference files and directories exist.

Parameters:

paths (dict[str, Path] | None)