fai_read does not check ferror() and fai_load does not check fai_read success, so failures to read from the fai file result in an fai structure that is empty except for the bgzf and hash entries.
For example, I recently ran into this problem with an mpileup. It ended up printing errors for every base of the format:
[mplp_func] Skipping because 93561197 is outside of -2 [ref:10]
[mplp_func] Skipping because 93561197 is outside of -2 [ref:10]
[mplp_func] Skipping because 93561198 is outside of -2 [ref:10]
[mplp_func] Skipping because 93561202 is outside of -2 [ref:10]
I had this problem with the experimental bcftools mpileup but it appears that samtools mpileup would be effected as well. The error is printed by: https://github.com/samtools/samtools/blob/834bf7e9c9d4bbf7d60160138f728e7968373e3a/bam_plcmd.c#L248-L253
I attached to the process with gdb to track down this issue.
The value of fai (up the stack in bam_mpileup) was:
#6 0x0000000000464895 in bam_mpileup (argc=15, argv=0x7ffd2d13ced0) at mpileup.c:945
(gdb) print *mplp.fai
$59 = {bgzf = 0x1b2f4d0, n = 0, m = 0, name = 0x0, hash = 0x1b2f4a0}
In this particular case, the failure was a transient read failure that would have been fine on retry (if I do another fai_load() it works just fine). htslib should at least check for read errors, and probably should retry depending on which error is returned.
fai_read does not check ferror() and fai_load does not check fai_read success, so failures to read from the fai file result in an fai structure that is empty except for the bgzf and hash entries.
For example, I recently ran into this problem with an mpileup. It ended up printing errors for every base of the format:
I had this problem with the experimental
bcftools mpileupbut it appears thatsamtools mpileupwould be effected as well. The error is printed by: https://github.com/samtools/samtools/blob/834bf7e9c9d4bbf7d60160138f728e7968373e3a/bam_plcmd.c#L248-L253I attached to the process with
gdbto track down this issue.The value of fai (up the stack in bam_mpileup) was:
In this particular case, the failure was a transient read failure that would have been fine on retry (if I do another fai_load() it works just fine). htslib should at least check for read errors, and probably should retry depending on which error is returned.