File size: 5,869 Bytes
746e176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# ── Subset full HCC1143 BAMs to chr17 for pipeline development ───────────────
# Input:  full WES BAMs (wherever you downloaded them)
# Output: hcc1143_T_clean.bam / hcc1143_N_clean.bam (chr17 only)
#
# Why chr17: TP53 is on chr17 — the known HCC1143 driver mutation
# we use as our validated anchor epitope. Subsetting to chr17 keeps
# the test data small (~50MB vs ~23GB) while retaining the mutation
# we actually care about.
# ─────────────────────────────────────────────────────────────────────────────

TUMOR_FULL=~/melanoma-pipeline/data/full/hcc1143_T_clean.bam
NORMAL_FULL=~/melanoma-pipeline/data/full/hcc1143_N_clean.bam
OUT=~/melanoma-pipeline/data/test

mkdir -p $OUT

# Subset tumor to chr17
# -b: output BAM format, -h: include header, -@ 8: 8 threads
samtools view \
    -b \
    -h \
    -@ 8 \
    $TUMOR_FULL \
    chr17 \
    -o $OUT/tumor_chr17.bam

# Index tumor subset
samtools index \
    -@ 8 \
    $OUT/tumor_chr17.bam \
    $OUT/tumor_chr17.bai

# Subset normal to chr17
samtools view \
    -b \
    -h \
    -@ 8 \
    $NORMAL_FULL \
    chr17 \
    -o $OUT/normal_chr17.bam

# Index normal subset
samtools index \
    -@ 8 \
    $OUT/normal_chr17.bam \
    $OUT/normal_chr17.bai

# Verify read counts
echo "Tumor chr17 reads:"
samtools flagstat $OUT/tumor_chr17.bam

echo "Normal chr17 reads:"
samtools flagstat $OUT/normal_chr17.bam


# ── Extract HLA reads from normal BAM for OptiType ───────────────────────────
# Always use normal BAM for HLA typing — tumor DNA can have LOH
# (Loss of Heterozygosity) at HLA loci which gives wrong alleles.
#
# Three-bucket strategy to avoid missing HLA reads due to ALT contigs:
#   Bucket 1 — primary MHC region on chr6
#   Bucket 2 — ALT contigs (reads that overflowed from primary chr6 assembly)
#   Bucket 3 — unmapped reads (too divergent to map anywhere in hg38)
# All three are merged and remapped against OptiType's HLA-specific reference.
# ─────────────────────────────────────────────────────────────────────────────

OUT_HLA=~/melanoma-pipeline/data/hla
mkdir -p $OUT_HLA

# Bucket 1 — primary MHC region on chr6
# 28510120-33480577 = full MHC locus including HLA-A, B, C and flanking genes
samtools view \
    -b \
    -h \
    -@ 8 \
    $NORMAL_FULL \
    chr6:28510120-33480577 \
    -o $OUT_HLA/hla_chr6_primary.bam

# Bucket 2 — ALT contigs
# Reads from patients with HLA alleles too divergent for the primary reference
# — most common in non-European populations. Dynamically finds all ALT/HLA
# contigs in the BAM header so this works regardless of hg38 build variant.
ALT_CONTIGS=$(samtools view -H $NORMAL_FULL \
    | grep "^@SQ" \
    | grep -iE "_alt|HLA" \
    | awk '{print $2}' \
    | sed 's/SN://')

if [ -n "$ALT_CONTIGS" ]; then
    ALT_TMP=$(mktemp -d)
    while IFS= read -r contig; do
        safe_name="${contig//\//_}"
        samtools view -b -h -@ 8 $NORMAL_FULL "$contig" -o "$ALT_TMP/${safe_name}.bam"
    done <<< "$ALT_CONTIGS"
    samtools merge -f -@ 8 $OUT_HLA/hla_alt_contigs.bam "$ALT_TMP"/*.bam
    rm -rf "$ALT_TMP"
else
    # No ALT contigs — create an empty BAM so downstream merge still works
    samtools view -b -h $NORMAL_FULL -o $OUT_HLA/hla_alt_contigs.bam /dev/null 2>/dev/null || \
        samtools view -H $NORMAL_FULL -b -o $OUT_HLA/hla_alt_contigs.bam
fi

# Bucket 3 — unmapped reads
# Small fraction of BAM but catches rare/unusual alleles that couldn't
# map anywhere in hg38 due to extreme divergence from the reference
# -f 4: flag 4 = read unmapped
samtools view \
    -b \
    -f 4 \
    -@ 8 \
    $NORMAL_FULL \
    -o $OUT_HLA/hla_unmapped.bam

# Merge all three buckets into one candidate pool
samtools merge \
    -f \
    -@ 8 \
    $OUT_HLA/hla_candidates.bam \
    $OUT_HLA/hla_chr6_primary.bam \
    $OUT_HLA/hla_alt_contigs.bam \
    $OUT_HLA/hla_unmapped.bam

# Convert merged BAM to paired FASTQ for remapping
# Sort by name first (-n) so read pairs are adjacent
samtools sort -n -@ 8 $OUT_HLA/hla_candidates.bam \
    | samtools fastq \
    -1 $OUT_HLA/hla_candidates_R1.fastq \
    -2 $OUT_HLA/hla_candidates_R2.fastq \
    -0 /dev/null \
    -s /dev/null

# Remap against OptiType's HLA-specific reference
# This is the critical step — reads that were on ALT contigs or unmapped
# now find their correct HLA allele sequence. The HLA reference contains
# sequences for thousands of known alleles across all populations.
# hla_reference_dna.fasta is bundled inside the fred2/optitype Docker image
# at /usr/local/bin/data/hla_reference_dna.fasta
# Extract it once and reuse:
#   docker run --rm fred2/optitype cat /usr/local/bin/data/hla_reference_dna.fasta \
#       > ~/melanoma-pipeline/reference/hla_reference_dna.fasta
HLA_REF=~/melanoma-pipeline/reference/hla_reference_dna.fasta

# Index HLA reference if not already done
if [ ! -f "${HLA_REF}.bwt" ]; then
    echo "Indexing HLA reference..."
    bwa index $HLA_REF
fi

bwa mem \
    -t 8 \
    $HLA_REF \
    $OUT_HLA/hla_candidates_R1.fastq \
    $OUT_HLA/hla_candidates_R2.fastq \
    | samtools sort -n -@ 8 \
    | samtools fastq \
    -F 4 \
    -1 $OUT_HLA/hla_fished_R1.fastq \
    -2 $OUT_HLA/hla_fished_R2.fastq \
    -0 /dev/null \
    -s /dev/null

# hla_fished_R1/R2.fastq are now ready to pass to OptiType (Step 3 of the pipeline)
echo "HLA read extraction complete."
echo "Fished read pairs: $(( $(wc -l < $OUT_HLA/hla_fished_R1.fastq) / 4 ))"
echo "Ready for OptiType: $OUT_HLA/hla_fished_R1.fastq + hla_fished_R2.fastq"