Skip to content

Commit 755532b

Browse files
maol-cortevaandrewprzh
authored andcommitted
Fix bam indexing for large chromosomes
Large genomes with large chromosomes (larger than some 500Mb) cannot be indexed by samtools index in "bai" format. The requirement is to use the "csi" index instead. The changes in this commit introduce "csi" when the "bai" fails
1 parent 9857604 commit 755532b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/read_mapper.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,18 @@ def align_fasta(aligner, fastq_file, annotation_file, args, label, out_dir):
378378
logger.info("Indexing alignments")
379379
try:
380380
pysam.index(alignment_bam_path)
381+
except pysam.SamtoolsError as err:
382+
if "failed to create index" in err.value:
383+
logger.info(f"Samtools failed to generate default .bai index; with error: {err.value}")
384+
logger.info("Trying to build a CSI index instead")
385+
try:
386+
pysam.index('-@', str(args.threads), '-c', alignment_bam_path)
387+
except pysam.SamtoolsError as err:
388+
logger.error(f"Failed to create CSI index: {err.value}")
389+
exit(-1)
390+
else:
391+
logger.error(err.value)
392+
exit(-1)
381393
except OSError as err:
382394
logger.error(err)
383395
exit(-1)

0 commit comments

Comments
 (0)