𧬠PyHIV Command Line Interfaceο
A comprehensive command-line interface for HIV-1 sequence alignment, subtyping, and gene region splitting.
π¦ Installationο
Install PyHIV using pip:
pip install pyhiv-tools
Or install from source:
git clone https://github.com/anaapspereira/PyHIV.git
cd PyHIV
pip install -e .
Verify installation:
pyhiv --version
π Getting Startedο
Run PyHIV with default settings:
pyhiv run /path/to/fastas/
This will:
Align sequences with reference genomes
Perform HIV-1 subtyping
Split sequences into gene regions
Save results to
PyHIV_results/
π§ Commandsο
pyhiv runο
Main command to process HIV-1 sequences.
pyhiv run [OPTIONS] FASTAS_DIR
Arguments:
FASTAS_DIR: Directory containing input FASTA files (required)
pyhiv validateο
Validate input directory without processing.
pyhiv validate FASTAS_DIR
Checks:
Directory exists and is readable
FASTA files are present
Lists found files (up to 10)
pyhiv update reference-datasetο
Refresh the packaged HIV-1 reference dataset (reference_fastas/ and sequences_with_locations.tsv) from LANLβs latest reference alignment and CRF database, optionally refreshing GenBank feature annotations. Also available as the top-level alias pyhiv update-reference-dataset.
pyhiv update reference-dataset [OPTIONS]
Options:
Option |
Default |
Description |
|---|---|---|
|
|
Reference dataset root to update |
|
|
Email sent to NCBI E-utilities (required by |
|
|
Optional NCBI API key |
|
off |
Refetch GenBank feature locations for references whose features are |
|
off |
Check for available updates without writing FASTA or TSV files |
|
off |
Skip the confirmation prompt |
Without --yes or --dry-run, the command asks for confirmation before overwriting the reference dataset. --refresh-features requires an NCBI email, passed via --email or the NCBI_EMAIL environment variable.
# Preview available updates without changing anything
pyhiv update reference-dataset --dry-run
# Apply the update, skipping the confirmation prompt
pyhiv update reference-dataset --yes
# Also refresh missing GenBank feature annotations
pyhiv update reference-dataset --refresh-features --email you@example.com --yes
βοΈ Optionsο
Processing Optionsο
Option |
Default |
Description |
|---|---|---|
|
|
Enable/disable HIV-1 subtyping |
|
|
Splitting mode: |
Output Optionsο
Option |
Default |
Description |
|---|---|---|
|
|
Output directory for results |
Performance Optionsο
Option |
Default |
Description |
|---|---|---|
|
All CPUs |
Number of parallel jobs |
`βalignment-tool [edlib-HW |
MAFFT |
parasail-NW |
|
|
K-mer size used to prefilter candidate references |
|
|
Number of top k-mer ranked references to align; use |
|
|
Comma-separated HIV-1 reference groups used for subtyping; use |
edlib-HW is the default alignment backend. parasail-NW/parasail, PyFamsa, and MAFFT are available through --alignment-tool. parasail-NW requires the optional parasail extra β install with pip install pyhiv-tools[parasail] β since it has no prebuilt wheel on some platforms (e.g. macOS on Apple Silicon). PyFamsa requires the optional famsa extra β install with pip install pyhiv-tools[famsa] β since pyfamsa is GPL-3.0 licensed and is kept out of the default (MIT) install. MAFFT requires an external mafft executable; PyHIV resolves it from PYHIV_MAFFT_BIN, then mafft on PATH. PyHIV checks the selected tool is actually usable before processing starts, and exits immediately with an install hint if it isnβt (e.g. missing parasail, or no mafft executable on PATH) rather than failing per-reference partway through a run.
Before final alignment, PyHIV ranks references using query/reference k-mer containment and aligns only the top candidates by default. Use --reference-top-k 0 to keep the original all-reference strategy. By default, subtyping uses group M references from reference_fastas, selected through the group column in sequences_with_locations.tsv; use --reference-groups M,N,O,P (or --reference-groups all) to include groups N, O, and P. This M-only default only applies when splitting is enabled or --reference-groups is set explicitly; if splitting is disabled and --reference-groups is omitted, all reference groups are eligible.
Sequences longer than 12000 nucleotides are skipped with this warning: The submitted sequence is longer than the HIV-1 genome.
Progress & Reporting Optionsο
Option |
Default |
Description |
|---|---|---|
|
|
Show a terminal progress bar while processing input sequences |
|
|
Generate a PDF report ( |
Display Optionsο
Option |
Description |
|---|---|
|
Enable detailed output |
|
Suppress all non-error output |
|
Show version and exit |
|
Show help message and exit |
π§ Environment Variablesο
Variable |
Used by |
Description |
|---|---|---|
|
Every command ( |
Overrides the reference dataset root for the whole tool, not just the update command. Defaults to the packaged |
|
|
Email sent to NCBI E-utilities; equivalent to |
|
|
Optional NCBI API key; equivalent to |
|
|
Path to the |
An explicit CLI flag always takes priority over its corresponding environment variable.
πΌ Usage Examplesο
Basic Usageο
Default processing:
pyhiv run sequences/
Custom output directory:
pyhiv run sequences/ -o my_results/
Parallel processing with 8 jobs:
pyhiv run sequences/ -j 8
Advanced Optionsο
Alignment only (no subtyping or splitting):
pyhiv run sequences/ --subtyping false --splitting false
Subtyping without gene splitting:
pyhiv run sequences/ --splitting false
Subtyping with HXB2-based splitting:
pyhiv run sequences/ --splitting hxb2
Verbose output with timing:
pyhiv run sequences/ -v
Quiet mode for scripting:
pyhiv run sequences/ -q
Validationο
Check input files before processing:
pyhiv validate sequences/
Example output:
β Found 15 FASTA file(s)
Files:
β’ sequence1.fasta
β’ sequence2.fasta
β’ sequence3.fa
...
Pipeline Examplesο
Complete workflow:
# 1. Validate inputs
pyhiv validate data/raw_sequences/
# 2. Process with verbose output
pyhiv run data/raw_sequences/ -o results/run1/ -v
# 3. Process subset without splitting
pyhiv run data/subset/ -o results/run2/ --splitting false -j 4
Integration with shell scripts:
#!/bin/bash
INPUT_DIR="sequences/"
OUTPUT_DIR="results_$(date +%Y%m%d_%H%M%S)"
# Validate first
if pyhiv validate "$INPUT_DIR"; then
echo "Validation passed, starting processing..."
pyhiv run "$INPUT_DIR" -o "$OUTPUT_DIR" -j 8
else
echo "Validation failed!"
exit 1
fi
π₯ Input Requirementsο
Supported Formatsο
PyHIV accepts FASTA files with the following extensions:
.fasta.fa.fna(nucleic acid).ffn(nucleotide coding regions)
Directory Structureο
sequences/
βββ sample1.fasta
βββ sample2.fa
βββ sample3.fasta
βββ subfolder/
βββ sample4.fasta
PyHIV recursively searches for FASTA files in all subdirectories. The runβs own --output-dir is always excluded from this search, so pointing --output-dir at a subfolder of FASTAS_DIR is safe β a rerun wonβt re-ingest FASTA files it generated on a previous run (e.g. best_alignment_*.fasta, gene-region files).
File Requirementsο
Valid FASTA format
HIV-1 sequences (DNA or RNA)
Sequence IDs should be unique
π Output Structureο
Default Output (PyHIV_results/)ο
PyHIV_results/
βββ final_table.tsv # Summary table
βββ best_alignment_inputA_sample1.fasta # Best alignments
βββ best_alignment_inputB_sample2.fasta
βββ gag/ # Gene regions (if --splitting)
β βββ inputA_sample1_gag.fasta
β βββ inputB_sample2_gag.fasta
βββ pol/
β βββ inputA_sample1_pol.fasta
β βββ inputB_sample2_pol.fasta
βββ env/
βββ ...
Output Filesο
final_table.tsvο
Summary table with columns:
Column |
Description |
|---|---|
File Name |
Source FASTA file name |
Sequence |
Input sequence ID |
Reference |
Best matching reference accession |
Group |
HIV-1 reference group |
Subtype |
HIV-1 subtype (if |
Closest Subtypes |
Top 3 closest unique group/subtype calls by alignment score |
Subtype Score Warning |
|
Splitting Reference |
Reference accession used for gene splitting |
Most Matching Gene Region |
Gene with most matches |
Present Gene Regions |
All detected gene regions |
Example:
File Name Sequence Reference Group Subtype Closest Subtypes Splitting Reference Most Matching Gene Region Present Gene Regions
sample_1.fasta seq001 K03455 M B M:B (score=9120); M:C (score=8894); M:A1 (...) K03455 pol gag, pol, env
sample_2.fasta seq002 AF004885 M C M:C (score=9015); M:B (score=8801); M:A1 (...) AF004885 env pol, env
Alignment Filesο
best_alignment_<file_stem>_<sequence_id>.fasta: Contains reference and query alignmentsplitting_alignment_<file_stem>_<sequence_id>.fasta: Contains the HXB2 alignment when--splitting hxb2is used with subtyping, or when--splitting truefalls back to HXB2 because the selected subtype reference has no annotated featuresFormat: Multi-FASTA with reference sequence and aligned query
Gene Region Filesο
When --splitting is enabled:
Organized by gene (gag, pol, env, etc.)
One file per sequence per gene
Contains extracted gene region from alignment
PDF Reportο
When --reporting is enabled (default), PyHIV writes PyHIV_report_all_sequences.pdf to the output directory with one page per processed sequence, summarizing its alignment, subtyping, and gene-splitting results. Disable it with --no-reporting.
β‘ Advanced Usageο
Performance Tuningο
Optimize for large datasets:
# Use all CPUs (the default when -j is omitted)
pyhiv run sequences/
# Limit to 4 cores to avoid memory issues
pyhiv run sequences/ -j 4
Memory considerations:
-jsizes one worker pool thatβs created once per run and reused for every sequence, not spawned per sequence, but each worker process still loads its own copy of the reference sequencesReduce
-jvalue if encountering memory errorsProcess in batches for very large datasets
Batch Processingο
# Process multiple directories
for dir in batch1/ batch2/ batch3/; do
pyhiv run "$dir" -o "results_$(basename $dir)" -q
done
Integration with Other Toolsο
Export to CSV:
pyhiv run sequences/ -o results/
python -c "import pandas as pd; df = pd.read_csv('results/final_table.tsv', sep='\t'); df.to_csv('results.csv', index=False)"
Filter by subtype:
pyhiv run sequences/ -o results/ -v
awk -F'\t' '$5 == "B"' results/final_table.tsv > subtype_B.tsv
π οΈ Troubleshootingο
Common Issuesο
No FASTA files found:
Error: No FASTA files found in the input directory.
Check file extensions (must be .fasta, .fa, etc.)
Verify directory path is correct
Use
pyhiv validateto diagnose
Output directory exists:
Warning: Output directory 'PyHIV_results' already exists. Files may be overwritten.
Files will be overwritten
Use
-oto specify a different directoryOr remove existing directory:
rm -rf PyHIV_results/
Memory errors:
MemoryError: Unable to allocate array
Reduce parallel jobs:
-j 2or-j 1Process sequences in smaller batches
Close other applications
Import errors:
Error: Could not import PyHIV module
Verify installation:
pip list | grep pyhiv-toolsReinstall:
pip install --force-reinstall pyhiv-toolsCheck Python version compatibility
Debug Modeο
Enable verbose output for debugging:
pyhiv run sequences/ -v
This shows:
Version information
Number of input files
Processing parameters
Elapsed time
Generated output files
Full stack traces on errors
Getting Helpο
# Show all commands
pyhiv --help
# Show help for specific command
pyhiv run --help
pyhiv validate --help
pyhiv update reference-dataset --help
Exit Codesο
Code |
Meaning |
|---|---|
0 |
Success |
1 |
Error during processing |
130 |
Interrupted by user (Ctrl+C) |
π Performance Tipsο
Use validation first -
pyhiv validateis fast and catches input errorsAdjust parallelism - Start with default (all CPUs), reduce if memory is limited
Disable unused features - Use
--splitting falseif you only need alignmentsBatch processing - For thousands of sequences, split into smaller batches
SSD storage - Use SSD for output directory to improve I/O performance
π€ Contributingο
Found a bug or have a feature request? Please open an issue on GitHub.
π§Ύ Licenseο
PyHIV is released under the MIT License. See LICENSE file for details.
ποΈ Citationο
If you use PyHIV in your research, please cite:
Manuscript in preparation. Please cite this repository if you use PyHIV in your research.