
HTSlib-based tools such as bcftools and samtools can work with genomic data stored on remote servers. Whether the data is in AWS S3, available over FTP, or hosted on HTTPS endpoints, these tools can query and subset remote files without downloading entire datasets. This guide covers authentication, remote file access patterns, and practical workflows.
HTSlib is a C library that powers many genomic tools. Tools built on HTSlib — including bcftools, samtools, tabix, and others — inherit native support for remote file access via random-access queries. This section explains how HTSlib works with remote files and which protocols are supported.
HTSlib enables efficient remote file access through a byte-range request mechanism. Instead of downloading entire files, it uses file indexes to determine exact byte positions and requests only those bytes from the remote server.
How it works:
User Request: bcftools view s3://bucket/variants.vcf.gz chr1:1000-2000
HTSlib Process:1. Reads index file (.vcf.gz.tbi) to find byte ranges2. Index lookup: "chr1:1000-2000 is at bytes 5000-15000"3. Sends byte-range request to remote server4. Remote server returns ONLY requested bytes (10KB, not 1GB!)5. HTSlib decompresses partial data6. Returns matching variants to user
Result: 10KB downloaded instead of 1GBTime: 1-5 seconds instead of 5-30 minutesCritical requirement: indexed files (.tbi, .bai, .csi)
HTSlib supports a list of protocols via libcurl. Check your installation:
# View all supported protocolspixi run samtools --version 2>&1 | grep -A 20 "HTSlib URL"Complete output example:
HTSlib URL scheme handlers present: built-in: file, preload, data Google Cloud: gs+http, gs+https, gs libcurl: gophers, smtp, wss, smb, rtsp, tftp, pop3, smbs, imaps, pop3s, ws, ftps, ftp, gopher, imap, http, https, sftp, smtps, scp, dict, mqtt, telnet Amazon S3: s3+https, s3, s3+http crypt4gh: crypt4gh mem: memProtocol categories:
| Category | Protocols | Typical Use |
|---|---|---|
| Built-in | file, preload, data | Local files, pipes |
| Cloud Storage | s3://, gs:// | AWS, Google Cloud |
| HTTP-based | http://, https://, ftps:// | Web servers, FTP over SSL |
| Other | ftp://, sftp://, scp://, smb:// | Traditional file servers |
| Specialized | mqtt, dict, imap, etc | Not typical for genomics |
Note on protocol coverage: this post focuses on the most common protocols for bioinformatics: S3, HTTPS, FTP, and GCS. Many other protocols are available but are used less often for genomic data.
The following protocols are widely used for storing and accessing genomic data.
s3://bucket-name/path/file.vcf.gzhttps://server.com/path/file.vcf.gzftp://server.com/path/file.vcf.gzgs://bucket-name/path/file.vcf.gzRemote file access requires indexed files for efficient region-based queries:
.vcf.gz.tbi index.bam.bai index.bcf.csi indexBandwidth comparison for querying chr1:1-100000 from a 1GB VCF file:
| Scenario | Bytes Transferred | Time | Method |
|---|---|---|---|
| Indexed S3 | ~700KB | <1 second | Byte-range request |
| Indexed HTTPS | ~700KB | 1-2 seconds | HTTP Range header |
| Indexed FTP | ~700KB | 3-5 seconds | FTP REST seek |
| Unindexed remote | 1000MB+ | 5-30 minutes | Sequential download |
| Local indexed file | ~700KB | <0.5 seconds | Direct file seek |
Key insight: indexed remote access transfers a small fraction of the file size.
One way to install bcftools and samtools with remote file support is using Pixi with the conda-forge and bioconda channels. These channels include pre-compiled binaries with S3, FTP, and HTTPS support enabled in HTSlib.
Create a new directory for the project and initialize a Pixi environment:
# Create project directorymkdir -p ~/htslib-project && cd ~/htslib-project
# Initialize Pixi with conda-forge and bioconda channelspixi init --channel conda-forge --channel bioconda .
# Add bcftools and samtoolspixi add bcftools samtoolsThis creates a pixi.toml file:
[workspace]channels = ["conda-forge", "bioconda"]name = "htslib-project"platforms = ["linux-64"]version = "0.1.0"
[dependencies]bcftools = ">=1.23,<2"samtools = ">=1.23,<2"# Run bcftools versionpixi run bcftools --version
# Run samtools versionpixi run samtools --versionBoth tools report HTSlib features including S3, GCS, and libcurl support:
bcftools 1.23Using htslib 1.23...
Samtools 1.23...HTSlib compilation details: Features: build=configure libcurl=yes S3=yes GCS=yes ...The key line is S3=yes, which indicates S3 support is enabled.
pixi.lock pins the same versions across machinesFor HTTPS/HTTP remote access to work efficiently, the server must support HTTP Range Requests. Verify this before relying on remote queries:
# Test if server supports Range Requestscurl -I -H "Range: bytes=0-500" https://ftp.ncbi.nlm.nih.gov/snp/organisms/human_9606_b150_GRCh37p13/VCF/00-common_all.vcf.gz
# Expected response: HTTP/1.1 206 Partial Content# This means byte-range requests work! ✓Response headers will show:
HTTP/1.1 206 Partial Content ← supports Range requestsAccept-Ranges: bytes ← confirms supportContent-Length: 501 ← returns partial contentAWS S3 is a common repository for genomic datasets. HTSlib supports S3 through the s3:// protocol.
Set AWS credentials as environment variables:
export AWS_ACCESS_KEY_ID="your_access_key"export AWS_SECRET_ACCESS_KEY="your_secret_key"export AWS_DEFAULT_REGION="us-east-1"If running on EC2 with an IAM role attached, HTSlib uses the role's credentials automatically; no environment variables are needed.
HTSlib respects ~/.aws/credentials and ~/.aws/config:
aws configure # Interactive setupQuery a specific genomic region from a remote VCF file:
# Query chromosome 1, positions 1-1000000bcftools view s3://my-bucket/data/variants.vcf.gz chr1:1-1000000
# Count variants in a regionbcftools view -H s3://my-bucket/data/variants.vcf.gz chr1:1-1000000 | wc -l
# Extract samples and subset to regionbcftools view -s sample1,sample2 s3://my-bucket/data/variants.vcf.gz chr1:1-1000000 -O vExtract reads from specific genomic regions:
# Query region from remote BAMsamtools view s3://my-bucket/data/aligned.bam chr1:1-1000000
# Count reads in regionsamtools view -c s3://my-bucket/data/aligned.bam chr1:1-1000000
# Extract to local BAM filesamtools view -b s3://my-bucket/data/aligned.bam chr1:1-1000000 -o region.bamAn efficient workflow for extracting subsets from S3:
# 1. List available S3 files (using AWS CLI)aws s3 ls s3://my-bucket/data/ --recursive
# 2. Query specific region from VCFbcftools view \ s3://my-bucket/data/chr1.vcf.gz \ chr1:10000000-20000000 \ -O v -o local_subset.vcf
# 3. Validate and compressbgzip -f local_subset.vcftabix -p vcf local_subset.vcf.gz
# 4. Annotate or further process locallybcftools annotate local_subset.vcf.gz -a annotation.vcf.gz -c IDPublic genomic repositories are useful for testing remote file access. While S3 is available, HTTPS endpoints are often more reliable for quick testing.
Query NCBI dbSNP data via HTTPS:
NCBI provides public access to common variants. Test with real data:
# Query NCBI dbSNP common variants (via HTTPS)pixi run bcftools view \ https://ftp.ncbi.nlm.nih.gov/snp/organisms/human_9606_b150_GRCh37p13/VCF/00-common_all.vcf.gz \ | pixi run bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\n' \ | head -20Expected output showing real variants:
1 10177 A AC1 10352 T TA1 10352 T TA1 10616 CCGCCGTTGCAAAGGCGCGCCG C1 10642 G A1 11008 C G1 11012 C GDiscover available files:
# List NCBI FTP VCF filescurl -s https://ftp.ncbi.nlm.nih.gov/snp/organisms/human_9606_b150_GRCh37p13/VCF/ \ | grep -i ".vcf.gz\"" | grep -o "href=\"[^\"]*\"" | cut -d'"' -f2
# Or for 1000 Genomes in S3:aws s3 ls s3://1000genomes/phase3/data/ --no-sign-request | head -20Region-based queries (with indexed files):
# Query specific chromosome region# Note: Requires .tbi index at the remote locationpixi run bcftools view \ https://ftp.ncbi.nlm.nih.gov/snp/organisms/human_9606_b150_GRCh37p13/VCF/00-common_all.vcf.gz \ chr1:1000000-2000000Note on S3 vs HTTPS:
FTP servers host many public genomic datasets. HTSlib supports the ftp:// protocol.
Many public repositories allow anonymous FTP:
# No credentials neededbcftools view ftp://ftp.ncbi.nlm.nih.gov/genomes/H_sapiens/VCF/common_all.vcf.gz chr1:1-1000000For private FTP servers, include credentials in the URL:
bcftools view ftp://username:password@ftp.example.com/data/variants.vcf.gz chr1:1-1000000Security note: credentials embedded in a URL are visible in process listings. Prefer environment variables:
export FTP_USER="your_username"export FTP_PASSWORD="your_password"
# HTSlib will use these automaticallybcftools view ftp://ftp.example.com/data/variants.vcf.gz chr1:1-1000000Common public genomic repositories accessible via FTP:
# Query NCBI's common variants filebcftools view \ ftp://ftp.ncbi.nlm.nih.gov/snp/organisms/human_9606_b150_GRCh37p13/VCF/common_all.vcf.gz \ chr1:1-1000000 \ | bcftools query -f '%CHROM\t%POS\t%AC\t%AF\n' \ | head -10# Query Ensembl VCF (example path)bcftools view \ ftp://ftp.ensembl.org/pub/data_files/homo_sapiens/VEP/Homo_sapiens_phenotypes_associated_variants-GRCh38.vcf.gz \ 1:1-1000000
# List available Ensembl filesftp ftp.ensembl.org # Interactive FTP session.tbi or .csi indexes are present on the FTP server for efficient region queries.export CURL_CONNECT_TIMEOUT=30export CURL_TIMEOUT=60HTTPS endpoints often require token-based authentication and are increasingly common for cloud-hosted data.
For endpoints that accept Bearer tokens (common on cloud platforms):
# Option 1: Set environment variableexport HTTPS_AUTH_BEARER="your_bearer_token"bcftools view https://api.example.com/data/variants.vcf.gz chr1:1-1000000
# Option 2: Pass in URL (less secure)bcftools view https://token:your_bearer_token@api.example.com/data/variants.vcf.gz chr1:1-1000000For services requiring custom headers, use curl with process substitution:
# Create a wrapper using curl and process substitutionbcftools view \ <(curl -H "Authorization: Bearer TOKEN" \ https://api.example.com/data/variants.vcf.gz \ --output - ) \ chr1:1-1000000# URL-embedded credentials (not recommended for security)bcftools view https://username:password@secure.example.com/data/variants.vcf.gz chr1:1-1000000Many databases provide HTTPS access to genomic data:
# Example: Query genome annotation from HTTPS endpointbcftools view https://example.genomics.org/public/variants.vcf.gz chr1:1-1000000
# Extract specific informationbcftools view https://example.genomics.org/public/variants.vcf.gz chr1:1-1000000 \ | bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\t%AC\n'For repeated queries, consider caching indexes locally:
# Download index file (small, fast)curl -o variants.vcf.gz.tbi https://api.example.com/data/variants.vcf.gz.tbi
# Query still uses remote file but with local index knowledgebcftools view https://api.example.com/data/variants.vcf.gz chr1:1-1000000If you encounter certificate validation errors:
# Disable certificate verification (use cautiously)export SSL_NO_VERIFY=1bcftools view https://api.example.com/data/variants.vcf.gz chr1:1-1000000
# Or use curl environment variableexport CURL_CA_BUNDLE=""bcftools view https://api.example.com/data/variants.vcf.gz chr1:1-1000000Security note: only disable certificate verification for trusted internal endpoints or testing.
Efficient remote file access depends on proper indexing. HTSlib tools use byte-range requests to fetch only the necessary data.
# For bgzip-compressed VCF filestabix -p vcf variants.vcf.gz
# Verify indexls -lh variants.vcf.gz*# CSI indexes support larger files and datasetsbcftools index -c variants.bcf
# Verify indexls -lh variants.bcf.csi# Standard BAM indexingsamtools index aligned.bam
# Creates aligned.bam.bails -lh aligned.bam.bai# Check if VCF index is validtabix -l variants.vcf.gz | head -10
# Query a specific region (tests index)bcftools view variants.vcf.gz chr1:1-1000 -H | wc -lIndexed remote access (with a region query):
Unindexed remote access:
Example performance comparison:
# Indexed query (fast, ~1-5 seconds)time bcftools view s3://my-bucket/variants.vcf.gz chr1:1-1000000 > /dev/null
# Without index (very slow, minutes or fails)# HTSlib automatically uses index if available, so explicitly avoid it only if neededBefore working with remote files, it helps to test bcftools and samtools locally. The workflow below verifies the installation and practices region queries.
Create a test VCF file:
# Create test directorymkdir -p ~/genomics-test && cd ~/genomics-test
# Create test VCFcat > test.vcf << 'EOF'##fileformat=VCFv4.2##INFO=<ID=DP,Number=1,Type=Integer,Description="Total read depth">##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT sample1 sample2chr1 10000 . A G 60 . DP=100 GT 0/1 1/1chr1 20000 . C T 50 . DP=80 GT 0/0 0/1chr1 30000 . G A 70 . DP=120 GT 1/1 0/1chr1 100000 . T C 55 . DP=90 GT 0/1 0/0EOF
# Compress and index (requires bgzip and tabix from pixi)pixi run bgzip -f test.vcfpixi run tabix -p vcf test.vcf.gzTest bcftools region queries:
# Query specific regionpixi run bcftools view test.vcf.gz chr1:1-50000
# Extract specific samplespixi run bcftools view -s sample1 test.vcf.gz chr1:10000-50000
# Count variants in regionpixi run bcftools view -H test.vcf.gz chr1:1-100000 | wc -lCreate and test a BAM file:
# Create test SAM filecat > test.sam << 'EOF'@HD VN:1.0 SO:coordinate@SQ SN:chr1 LN:1000000read1 0 chr1 100 60 10M * 0 0 ACGTACGTAC IIIIIIIIIIread2 0 chr1 120 60 10M * 0 0 TGCATGCATG IIIIIIIIIIread3 0 chr1 50000 60 10M * 0 0 AAAAAAAAAA IIIIIIIIIIread4 0 chr1 100000 60 10M * 0 0 TTTTTTTTTT IIIIIIIIIIEOF
# Convert to BAM and indexpixi run samtools view -b test.sam -o test.bampixi run samtools index test.bamTest samtools region queries:
# View reads in regionpixi run samtools view test.bam chr1:1-60000
# Count reads in regionpixi run samtools view -c test.bam chr1:1-60000
# Extract region to new filepixi run samtools view -b test.bam chr1:50000-100000 -o region.bamOnce comfortable with local files, the same commands work with remote S3, FTP, or HTTPS URLs; replace the local filenames with remote URLs.
# Define gene regions (example: gene1, gene2)cat > genes.bed << 'EOF'1 11869 14409 DDX11L11 17369 17436 MIR6859-1EOF
# Extract variants from remote file for gene regionsbcftools view \ s3://my-bucket/variants.vcf.gz \ -R genes.bed \ -O v -o local_genes.vcf
# Compress and indexbgzip -f local_genes.vcftabix -p vcf local_genes.vcf.gz# Compare variants for two samples in different S3 filesbcftools isec \ <(bcftools view -s sample1 s3://bucket1/variants.vcf.gz) \ <(bcftools view -s sample2 s3://bucket2/variants.vcf.gz) \ -p comparison_output
# Shows shared and unique variantsls comparison_output/# Annotate S3-hosted VCF using FTP-hosted annotation filebcftools annotate \ s3://my-bucket/variants.vcf.gz \ -a ftp://ftp.ncbi.nlm.nih.gov/snp/organisms/human_9606_b150_GRCh37p13/VCF/common_all.vcf.gz \ -c ID \ -O v -o annotated.vcf# Create list of regionscat > regions.txt << 'EOF'chr1:1-1000000chr2:2000000-3000000chr3:5000000-6000000EOF
# Extract each region from remote BAM, save locallywhile read region; do samtools view \ s3://my-bucket/aligned.bam \ $region \ -b -o ${region//:/_}.bam samtools index ${region//:/_}.bamdone < regions.txt# Pipe remote data through multiple toolsbcftools view s3://my-bucket/variants.vcf.gz chr1:1-1000000 \ | bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\t%AC\t%AF\n' \ | awk '$6 > 0.05' \ | tee filtered_variants.tsv \ | wc -lIssue: "File not found" or 404 errors
aws s3 ls s3://bucket-name/path/file.vcf.gzIssue: authentication failures with S3
aws sts get-caller-identityIssue: index not found (.tbi or .bai missing)
aws s3 ls s3://bucket-name/file.vcf.gz.tbiIssue: "Connection timeout" on FTP
export CURL_CONNECT_TIMEOUT=60export CURL_TIMEOUT=120Issue: HTTPS SSL certificate errors
curl -k https://endpoint.com/file.vcf.gz.tbi # -k disables cert verificationFor S3 access:
# VPC endpoint for S3 (no data transfer charges within VPC)s3://bucket-name/keyFor large batch queries:
bcftools view s3://bucket/variants.vcf.gz chr1 -o chr1.vcf.gzbcftools index chr1.vcf.gz# Then query locally multiple timesFor FTP performance:
# Enable verbose output for debuggingexport HTS_LOG_LEVEL=debug
# Check curl verbose outputexport CURL_VERBOSE=1Setup (one time):
# Create project directorymkdir -p ~/genomics-work && cd ~/genomics-work
# Initialize Pixi with conda-forge and biocondapixi init --channel conda-forge --channel bioconda .
# Add toolspixi add bcftools samtools
# Verify installationpixi run bcftools --versionpixi run samtools --versionUsing remote files:
# 1. Set up authentication (optional, for private buckets)export AWS_ACCESS_KEY_ID="..."export AWS_SECRET_ACCESS_KEY="..."export AWS_DEFAULT_REGION="us-east-1"
# 2. Query real remote file (NCBI dbSNP via HTTPS - no auth needed)pixi run bcftools view \ https://ftp.ncbi.nlm.nih.gov/snp/organisms/human_9606_b150_GRCh37p13/VCF/00-common_all.vcf.gz \ | pixi run bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\n' \ | head -20
# 3. Or for your own S3 data:pixi run bcftools view s3://my-bucket/variants.vcf.gz chr1:1-1000000 -O v -o chr1_subset.vcf
# 4. Create local index for repeated usepixi run bgzip -f chr1_subset.vcfpixi run tabix -p vcf chr1_subset.vcf.gz