import sys

# Usage: python make_composite_trackDb.py samples.tsv trackDb.txt

tsv_file = sys.argv[1]
out_file = sys.argv[2]

with open(tsv_file) as f:
    lines = [line.strip().split("\t") for line in f if line.strip()]
    lines = sorted(lines, key=lambda x: x[1])  # Sort by sample name

with open(out_file, "w") as out:
    # Parent composite track
    out.write(
        """track BRBseqsamples
shortLabel BRBseq-samples
longLabel BRBseq samples
type bigWig
compositeTrack on
visibility dense
autoScale group
"""
    )

    # Child tracks
    for i, (barcode, sample) in enumerate(lines, 1):
        out.write(
f"""
    track {sample}
    parent BRBseqsamples
    bigDataUrl {barcode}.bigWig
    shortLabel {sample}
    longLabel {sample}
    type bigWig
"""
        )
