hcc1143_cancer_and_normal_data / test /create_test_data.sh
SlitherCode's picture
Add files using upload-large-folder tool
746e176 verified
# ── 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"