Emit per-batch CRAM from dorado's native aligner; drop the merge step

--emit-cram (reference FASTA required) shrinks the per-chunk alignment
artifacts to roughly half of BAM, and the label job now consumes the
per-batch CRAMs directly, so the dataset-sized merged BAM intermediate
is gone entirely. pysam opens the CRAMs with reference_filename; the
rocm fallback keeps the fastq->minimap2->bam chain and the merged BAM.
This commit is contained in:
Tom Kasper
2026-10-16 20:05:43 +01:00
parent 2ce1435a96
commit b53ddf0962
4 changed files with 95 additions and 89 deletions
+60 -56
View File
@@ -53,8 +53,8 @@ def _source_run(record, fallback):
if __name__ == '__main__':
input_bamfile = snakemake.input[0]
reference_genomes = snakemake.input[1]
alignment_paths = [str(p) for p in snakemake.input.alignments]
reference_genomes = snakemake.input.reference
output_parquet = snakemake.output[0]
dataset_tag = snakemake.wildcards.dataset
@@ -67,68 +67,72 @@ if __name__ == '__main__':
'ref_length' : len(ref.seq)
}
# Open Bamfile for reading (unsorted BAM -> plain iteration, no index needed)
bamfile = pysam.AlignmentFile(input_bamfile,'rb')
# Transcribe Taxonomy from Sequence ID dict to bam header position list for fast lookup
ref_data = []
for ref_id in bamfile.header.references:
if ref_id not in ref_tax_data:
raise KeyError(f'reference {ref_id} from BAM header missing in {reference_genomes}')
ref_data.append(
ReferenceMetadata(
ref_id = ref_id,
genus = ref_tax_data[ref_id]['genus'],
species = ref_tax_data[ref_id]['species'],
ref_len = ref_tax_data[ref_id]['ref_length']
)
)
# Parse alignments
# Parse alignments (per-batch CRAMs on the cuda branch, one merged BAM on rocm;
# CRAM decoding needs the reference fasta)
data = []
n_records = 0
n_unmapped = 0
n_secondary = 0
n_supplementary = 0
n_primary = 0
for record in bamfile:
n_records += 1
if record.is_unmapped:
n_unmapped += 1
continue
if record.is_secondary:
n_secondary += 1
continue
if record.is_supplementary:
n_supplementary += 1
continue
n_primary += 1
nm_count = -1
for tag in record.get_tags():
if tag[0] == 'NM':
nm_count = tag[1]
assert nm_count != -1
assert isinstance(nm_count,int)
rlen = record.infer_read_length()
assert rlen is not None
qlen = record.infer_query_length()
assert qlen is not None
ref = ref_data[record.reference_id]
data.append(
{
'read_id' : record.query_name,
'ref_name' : ref.ref_id,
'genus' : ref.genus,
'species' : ref.species,
'identity' : 1 - (nm_count / qlen),
'aligned_len' : qlen,
'query_len' : rlen,
'source_run' : _source_run(record, dataset_tag),
}
)
for path in alignment_paths:
if path.endswith('.cram'):
bamfile = pysam.AlignmentFile(path, 'rc', reference_filename=str(reference_genomes))
else:
bamfile = pysam.AlignmentFile(path, 'rb')
# Transcribe Taxonomy from Sequence ID dict to bam header position list for fast lookup
ref_data = []
for ref_id in bamfile.header.references:
if ref_id not in ref_tax_data:
raise KeyError(f'reference {ref_id} from alignment header missing in {reference_genomes}')
ref_data.append(
ReferenceMetadata(
ref_id = ref_id,
genus = ref_tax_data[ref_id]['genus'],
species = ref_tax_data[ref_id]['species'],
ref_len = ref_tax_data[ref_id]['ref_length']
)
)
for record in bamfile:
n_records += 1
if record.is_unmapped:
n_unmapped += 1
continue
if record.is_secondary:
n_secondary += 1
continue
if record.is_supplementary:
n_supplementary += 1
continue
n_primary += 1
nm_count = -1
for tag in record.get_tags():
if tag[0] == 'NM':
nm_count = tag[1]
assert nm_count != -1
assert isinstance(nm_count,int)
rlen = record.infer_read_length()
assert rlen is not None
qlen = record.infer_query_length()
assert qlen is not None
ref = ref_data[record.reference_id]
data.append(
{
'read_id' : record.query_name,
'ref_name' : ref.ref_id,
'genus' : ref.genus,
'species' : ref.species,
'identity' : 1 - (nm_count / qlen),
'aligned_len' : qlen,
'query_len' : rlen,
'source_run' : _source_run(record, dataset_tag),
}
)
df = pd.DataFrame(data)
df.to_parquet(output_parquet,index=False)
print(f'Processed {input_bamfile}:')
print(f'Processed {len(alignment_paths)} alignment file(s): {", ".join(alignment_paths)}')
print(f'\t{n_records} alignment records')
print(f'\t{n_primary} primary alignments')
print(f'\t{n_unmapped} unmapped reads')