Skip to content

分析流程 › RNA-seq › 03 比對

參考基因組、比對與 BAM QC (Reference, Alignment & BAM QC) ​

理解 reference genome / annotation 的版本一致性,使用 STAR 或 HISAT2 做 splice-aware alignment,處理 SAM/BAM/CRAM,並判讀 alignment QC。

參考基因組與註解 ​

輸入資料:

  • Genome FASTA:染色體或 scaffold sequence。
  • Transcriptome FASTA:transcript sequence,常用於 Salmon/kallisto。
  • GTF/GFF3:gene、transcript、exon、CDS、UTR 等 genomic feature。
ConceptPractical implication
Genome assembly versionGRCh38、GRCm39、TAIR10 等版本不可混用
Annotation sourceEnsembl、GENCODE、NCBI RefSeq 的 gene model 與 ID 系統不同
Chromosome namingchr1 與 1 不一致會讓 reads 或 features 無法匹配
Gene ID / transcript IDcount matrix 與 downstream annotation 的主鍵
GTF vs GFF3GTF 常用於 RNA-seq quantification;GFF3 attribute 結構較泛用

GTF 中 gene、transcript、exon、CDS 的層級可以想成:

text
gene
  transcript 1
    exon 1
    exon 2
    CDS
  transcript 2
    exon 1
    exon 3

注意:FASTA 與 GTF 版本不一致會造成 STAR index junction 錯誤、featureCounts assignment 低、tx2gene mapping 不完整,或 chr 命名不相容。該下載哪個 FASTA / GTF 見 參考基因組 與 基因註解;格式見 GTF / GFF3。

剪接感知比對 (Splice-aware Alignment) ​

RNA-seq reads 可能跨越 exon-exon junction,因此 aligner 必須允許 read 分段對到基因組不同 exon。Splice-aware alignment 會辨識 intron gap、canonical splice site(常見 GT-AG、GC-AG、AT-AC)、exon-exon junction 與 soft clipping。

TermMeaning
Unique mappingread 最佳位置唯一,對 gene-level counting 較穩
Multi-mappingread 可對到多個相似位置,例如 paralog、repeat、rRNA
Soft clippingread 端點未對上但保留在 BAM sequence 中,CIGAR 以 S 表示
Mismatchread 與 reference base 不一致,可能是錯誤、SNP 或 RNA editing
Alignment scorealigner 對 match/mismatch/gap 的總分
MAPQmapping quality,反映 mapping 位置信心;不同 aligner 定義不完全相同

STAR ​

STAR 使用 seed search 加上 stitching,速度快、junction detection 強,常用於 mammalian RNA-seq。它記憶體需求高,human genome index 通常需要數十 GB RAM。

建立索引 ​

bash
READ_LENGTH=101 THREADS=16 \
bash scripts/star_alignment.sh reference/genome.fa reference/genes.gtf data/trimmed_fastq results/03_star

核心參數:

ParameterMeaning
--runMode genomeGenerate建立 genome index
--sjdbGTFfile將已知 exon junction 納入 index,提高 junction mapping
--sjdbOverhang通常設為 read length - 1
--outSAMtype BAM SortedByCoordinate直接輸出 coordinate-sorted BAM
--quantMode GeneCounts產生 STAR GeneCounts,適合快速檢查但 featureCounts 更彈性
--twopassMode Basicfirst pass 找 junction,second pass 使用 novel junction 改善 mapping

單端 ​

bash
STAR --runThreadN 8 \
  --genomeDir results/03_star/index \
  --readFilesIn sample_R1.fastq.gz \
  --readFilesCommand zcat \
  --outFileNamePrefix results/03_star/bam/sample. \
  --outSAMtype BAM SortedByCoordinate \
  --quantMode GeneCounts

雙端 ​

bash
STAR --runThreadN 8 \
  --genomeDir results/03_star/index \
  --readFilesIn sample_R1.fastq.gz sample_R2.fastq.gz \
  --readFilesCommand zcat \
  --outFileNamePrefix results/03_star/bam/sample. \
  --outSAMtype BAM SortedByCoordinate \
  --quantMode GeneCounts \
  --twopassMode Basic

STAR 本身不需要 featureCounts-style strand parameter 來對齊,但 --quantMode GeneCounts 輸出會包含 unstranded、forward stranded、reverse stranded 三種 gene count 欄位。正式 counting 仍應依 library strandness 設定 featureCounts -s。

Log.final.out ​

STAR mapping summary

圖 1. STAR mapping summary 示意圖。重點不是單看 total mapped,而是 uniquely mapped、multi-mapped、too short、unmapped 與 chimeric/fusion-related 指標是否符合資料型態。

常看的欄位:

  • Number of input reads
  • Uniquely mapped reads %
  • % of reads mapped to multiple loci
  • % of reads unmapped: too short
  • Mismatch rate per base
  • Deletion/Insertion rate per base

適用情境:需要 BAM、junction-level information、fusion detection、variant calling 或 genome browser inspection。限制:index 大、RAM 高、不同版本與參數會影響 junction 結果。

HISAT2 ​

HISAT2 使用 hierarchical graph FM index,記憶體需求通常低於 STAR。搭配 splice site 與 exon information 可改善已知 junction mapping,常與 StringTie 做 transcript assembly。

完整 script:

bash
THREADS=12 \
bash scripts/hisat2_alignment.sh reference/genome.fa reference/genes.gtf data/trimmed_fastq results/03_hisat2

重要參數:

ParameterMeaning
hisat2_extract_splice_sites.py從 GTF 產生 splice site hints
hisat2_extract_exons.py從 GTF 產生 exon hints
hisat2-build --ss --exon建立含 annotation hints 的 index
--dtadownstream transcriptome assembly mode,保留較長 alignment 給 StringTie

單端:

bash
hisat2 -p 8 --dta -x results/03_hisat2/index/genome \
  -U sample_R1.fastq.gz \
  2> results/03_hisat2/logs/sample.summary.txt |
  samtools sort -o results/03_hisat2/bam/sample.sorted.bam -

雙端:

bash
hisat2 -p 8 --dta -x results/03_hisat2/index/genome \
  -1 sample_R1.fastq.gz -2 sample_R2.fastq.gz \
  2> results/03_hisat2/logs/sample.summary.txt |
  samtools sort -o results/03_hisat2/bam/sample.sorted.bam -

HISAT2 summary 會列出 overall alignment rate、concordantly aligned pairs、discordantly aligned pairs、multi-mapping reads。若 paired-end concordance 很低,需檢查 insert size、R1/R2 是否交換、library orientation 或樣本污染。

STAR、HISAT2、Salmon、kallisto 比較 ​

FeatureSTARHISAT2Salmonkallisto
Principlesplice-aware genomic alignmentgraph/FM-index genomic alignmentlightweight mapping / selective alignmentpseudoalignment
Speedfast but index heavyfast, lower memoryvery fastvery fast
Memoryhigh for large genomesmoderatelow to moderatelow
Output levelBAM, junction, optional gene countsBAMtranscript abundancetranscript abundance
Produces BAMyesyesno in common quant modeno
Gene-level countingvia featureCounts/HTSeq or STAR GeneCountsvia featureCounts/HTSeqvia tximportvia tximport
Transcript quantificationpossible but not primarywith StringTie/RSEM workflowsprimaryprimary
Alternative splicingjunction/BAM availableStringTie-friendlytranscript abundance, not novel junction discoverytranscript abundance
Fusion detectionSTAR-Fusion/STAR chimeric modelimitednono
Variant callingpossible with careful RNA-seq caveatspossible with caveatsnono
Novel transcript discoverypossible with StringTie/Scallopstrong StringTie pairingnono
DE analysisgene counts into edgeR/DESeq2/limmagene counts into edgeR/DESeq2/limmatximport into edgeR/DESeq2/limmatximport into edgeR/DESeq2/limma
Main limitationRAM and storagesometimes lower junction sensitivity than STARdepends on transcriptome annotationless flexible bias modeling than Salmon

重點:alignment 與 pseudoalignment 不一定能互相取代。若後續要看 BAM、junction、fusion、variant 或 novel transcript,選 STAR/HISAT2;若主要是 known transcript/gene abundance 與 DE,Salmon/kallisto 通常更快。

BAM 處理 ​

SAM 是文字格式,BAM 是壓縮二進位格式,CRAM 是 reference-based 壓縮格式。大多數 downstream tools 使用 coordinate-sorted BAM 加上 BAM index。

bash
# SAM to BAM
samtools view -@ 4 -bS sample.sam > sample.bam

# Coordinate sorting: genome browser、variant calling、featureCounts 常用
samtools sort -@ 4 -o sample.sorted.bam sample.bam
samtools index sample.sorted.bam

# Name sorting: 某些 paired-end 操作或 transcript assembly 前處理可能需要
samtools sort -n -@ 4 -o sample.name_sorted.bam sample.bam

# QC
samtools flagstat sample.sorted.bam
samtools stats sample.sorted.bam > sample.stats.txt
samtools idxstats sample.sorted.bam > sample.idxstats.txt
samtools view -H sample.sorted.bam | head

FLAG、CIGAR(RNA-seq 的 N 代表跨越 intron)與 primary / secondary / supplementary 的意義見 SAM / BAM / CRAM;flagstat 判讀見 samtools。MAPQ 在 STAR(255 = 唯一)與 HISAT2 的刻度不同,見 比對品質。

比對 QC (Alignment QC) ​

完整 script:

bash
bash scripts/bam_qc.sh results/03_star/bam results/05_bam_qc

RNA-seq alignment QC 常看:

MetricMeaningInterpretation
Total reads輸入 reads/fragments 數與 FASTQ read count 對齊
Mapped reads至少對到 reference 的 reads過低需查污染、reference、品質
Uniquely mapped唯一位置 readsgene-level quantification 通常偏好
Multi-mapped多位置 readsrepeat、paralog、rRNA 會增加
Properly pairedpaired-end orientation/距離合理過低查 insert size 或 read pairing
Insert sizefragment length distributionlibrary prep 與 aligner summary 對照
DuplicationPCR/optical/high-expression duplicationRNA-seq 沒有固定門檻
Strandnesssense/antisense read distribution錯誤會讓 counting 幾乎失敗
rRNA contaminationreads mapping to rRNA過高表示 depletion/polyA selection 問題
Gene body coverage5'/3' coverage uniformitydegradation 或 protocol bias
Junction saturationnovel junction discovery 是否趨於飽和splicing analysis 重要
Read distributionexonic/intronic/intergenicpre-mRNA、nuclear RNA 或 DNA contamination 會影響

可用工具:samtools、RSeQC、Picard、Qualimap、MultiQC。沒有單一 QC 門檻適用所有 RNA-seq;要根據物種、library preparation、read length、sample type 與研究目的綜合判斷。