
Các công cụ dựa trên HTSlib như bcftools và samtools có thể làm việc với dữ liệu hệ gen lưu trên server từ xa. Dù dữ liệu nằm trên AWS S3, truy cập qua FTP, hay được host trên endpoint HTTPS, những công cụ này có thể truy vấn và trích xuất một phần file từ xa mà không cần tải toàn bộ tập dữ liệu. Hướng dẫn này trình bày xác thực, các mẫu truy cập file từ xa và những workflow thực tế.
HTSlib là một thư viện C nền tảng cho nhiều công cụ hệ gen. Các công cụ xây trên HTSlib — bao gồm bcftools, samtools, tabix và những công cụ khác — thừa hưởng hỗ trợ gốc cho truy cập file từ xa qua truy vấn ngẫu nhiên. Phần này giải thích cách HTSlib làm việc với file từ xa và những giao thức được hỗ trợ.
HTSlib cho phép truy cập file từ xa hiệu quả qua cơ chế yêu cầu byte-range. Thay vì tải toàn bộ file, nó dùng chỉ mục file để xác định vị trí byte chính xác và chỉ yêu cầu những byte đó từ server từ xa.
Cách hoạt động:
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 minutesYêu cầu quan trọng: file phải có chỉ mục (.tbi, .bai, .csi)
HTSlib hỗ trợ một danh sách giao thức qua libcurl. Hãy kiểm tra cài đặt của bạn:
# View all supported protocolspixi run samtools --version 2>&1 | grep -A 20 "HTSlib URL"Ví dụ output đầy đủ:
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: memPhân loại giao thức:
| 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 |
Lưu ý về phạm vi giao thức: bài này tập trung vào những giao thức phổ biến nhất cho bioinformatics: S3, HTTPS, FTP và GCS. Nhiều giao thức khác có sẵn nhưng ít được dùng cho dữ liệu hệ gen.
Những giao thức sau được dùng rộng rãi để lưu và truy cập dữ liệu hệ gen.
s3://bucket-name/path/file.vcf.gzhttps://server.com/path/file.vcf.gzftp://server.com/path/file.vcf.gzgs://bucket-name/path/file.vcf.gzTruy cập file từ xa cần file có chỉ mục để truy vấn theo vùng hiệu quả:
.vcf.gz.tbi.bam.bai.bcf.csiSo sánh băng thông khi truy vấn chr1:1-100000 từ file VCF 1GB:
| 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 |
Điểm mấu chốt: truy cập từ xa có chỉ mục chỉ truyền một phần nhỏ kích thước file.
Một cách để cài bcftools và samtools có hỗ trợ file từ xa là dùng Pixi với các kênh conda-forge và bioconda. Những kênh này gồm binary đã biên dịch sẵn với hỗ trợ S3, FTP và HTTPS được bật trong HTSlib.
Tạo một thư mục mới cho dự án và khởi tạo môi trường Pixi:
# 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 samtoolsLệnh này tạo file pixi.toml:
[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 --versionCả hai công cụ báo cáo các tính năng HTSlib bao gồm hỗ trợ S3, GCS và libcurl:
bcftools 1.23Using htslib 1.23...
Samtools 1.23...HTSlib compilation details: Features: build=configure libcurl=yes S3=yes GCS=yes ...Dòng quan trọng là S3=yes, cho biết hỗ trợ S3 đã được bật.
pixi.lock ghim cùng phiên bản giữa các máyĐể truy cập từ xa qua HTTPS/HTTP hoạt động hiệu quả, server phải hỗ trợ HTTP Range Request. Hãy xác minh điều này trước khi dựa vào truy vấn từ xa:
# 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! ✓Header phản hồi sẽ cho thấy:
HTTP/1.1 206 Partial Content ← supports Range requestsAccept-Ranges: bytes ← confirms supportContent-Length: 501 ← returns partial contentAWS S3 là repository phổ biến cho tập dữ liệu hệ gen. HTSlib hỗ trợ S3 qua giao thức s3://.
Đặt thông tin xác thực AWS qua biến môi trường:
export AWS_ACCESS_KEY_ID="your_access_key"export AWS_SECRET_ACCESS_KEY="your_secret_key"export AWS_DEFAULT_REGION="us-east-1"Nếu chạy trên EC2 có gắn IAM role, HTSlib tự động dùng thông tin xác thực của role; không cần biến môi trường.
HTSlib tôn trọng ~/.aws/credentials và ~/.aws/config:
aws configure # Interactive setupTruy vấn một vùng hệ gen cụ thể từ file VCF từ xa:
# 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 vTrích read từ những vùng hệ gen cụ thể:
# 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.bamMột workflow hiệu quả để trích xuất tập con từ 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 IDCác repository hệ gen công khai hữu ích để kiểm tra truy cập file từ xa. Dù S3 có sẵn, endpoint HTTPS thường đáng tin cậy hơn cho kiểm tra nhanh.
Truy vấn dữ liệu NCBI dbSNP qua HTTPS:
NCBI cung cấp truy cập công khai cho các biến thể phổ biến. Hãy kiểm tra với dữ liệu thực:
# 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 -20Output mong đợi với các biến thể thực:
1 10177 A AC1 10352 T TA1 10352 T TA1 10616 CCGCCGTTGCAAAGGCGCGCCG C1 10642 G A1 11008 C G1 11012 C GKhám phá các file có sẵn:
# 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 -20Truy vấn theo vùng (với file có chỉ mục):
# 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-2000000Lưu ý về S3 và HTTPS:
Các server FTP host nhiều tập dữ liệu hệ gen công khai. HTSlib hỗ trợ giao thức ftp://.
Nhiều repository công khai cho phép FTP ẩn danh:
# No credentials neededbcftools view ftp://ftp.ncbi.nlm.nih.gov/genomes/H_sapiens/VCF/common_all.vcf.gz chr1:1-1000000Với server FTP riêng, đưa thông tin xác thực vào URL:
bcftools view ftp://username:password@ftp.example.com/data/variants.vcf.gz chr1:1-1000000Lưu ý bảo mật: thông tin xác thực nhúng trong URL hiển thị trong danh sách process. Nên dùng biến môi trường:
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-1000000Các repository hệ gen công khai phổ biến truy cập được qua 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 hoặc .csi trên server FTP để truy vấn theo vùng hiệu quả.export CURL_CONNECT_TIMEOUT=30export CURL_TIMEOUT=60Endpoint HTTPS thường cần xác thực dựa trên token và ngày càng phổ biến cho dữ liệu host trên cloud.
Với endpoint chấp nhận Bearer token (phổ biến trên nền tảng cloud):
# 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-1000000Với dịch vụ cần header tuỳ biến, dùng curl với 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-1000000Nhiều cơ sở dữ liệu cung cấp truy cập HTTPS cho dữ liệu hệ gen:
# 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'Với truy vấn lặp lại, hãy cân nhắc cache chỉ mục cục bộ:
# 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-1000000Nếu gặp lỗi xác minh chứng chỉ:
# 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-1000000Lưu ý bảo mật: chỉ tắt xác minh chứng chỉ cho endpoint nội bộ đáng tin cậy hoặc khi kiểm thử.
Truy cập file từ xa hiệu quả phụ thuộc vào việc đánh chỉ mục đúng cách. Các công cụ HTSlib dùng yêu cầu byte-range để chỉ lấy dữ liệu cần thiết.
# 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 -lTruy cập từ xa có chỉ mục (với truy vấn theo vùng):
Truy cập từ xa không có chỉ mục:
Ví dụ so sánh hiệu năng:
# 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 neededTrước khi làm việc với file từ xa, nên kiểm tra bcftools và samtools cục bộ. Workflow dưới đây xác minh cài đặt và thực hành truy vấn theo vùng.
Tạo file VCF kiểm tra:
# 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.gzKiểm tra truy vấn vùng với bcftools:
# 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 -lTạo và kiểm tra file BAM:
# 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.bamKiểm tra truy vấn vùng với samtools:
# 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.bamKhi đã quen với file cục bộ, cùng những lệnh đó hoạt động với URL từ xa S3, FTP hoặc HTTPS; chỉ cần thay tên file cục bộ bằng URL từ xa.
# 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 -lVấn đề: lỗi "File not found" hoặc 404
aws s3 ls s3://bucket-name/path/file.vcf.gzVấn đề: xác thực S3 thất bại
aws sts get-caller-identityVấn đề: không tìm thấy chỉ mục (thiếu .tbi hoặc .bai)
aws s3 ls s3://bucket-name/file.vcf.gz.tbiVấn đề: "Connection timeout" trên FTP
export CURL_CONNECT_TIMEOUT=60export CURL_TIMEOUT=120Vấn đề: lỗi chứng chỉ SSL trên HTTPS
curl -k https://endpoint.com/file.vcf.gz.tbi # -k disables cert verificationVới truy cập S3:
# VPC endpoint for S3 (no data transfer charges within VPC)s3://bucket-name/keyVới truy vấn theo lô lớn:
bcftools view s3://bucket/variants.vcf.gz chr1 -o chr1.vcf.gzbcftools index chr1.vcf.gz# Then query locally multiple timesVới hiệu năng FTP:
# Enable verbose output for debuggingexport HTS_LOG_LEVEL=debug
# Check curl verbose outputexport CURL_VERBOSE=1Thiết lập (một lần):
# 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 --versionDùng file từ xa:
# 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