Bioconductor / Rsamtools

Binary alignment (BAM), FASTA, variant call (BCF), and tabix file import
https://bioconductor.org/packages/Rsamtools
Other
27 stars 27 forks source link

.bai files are not found when "bam" is part of the file path #4

Open ericfournier2 opened 5 years ago

ericfournier2 commented 5 years ago

Hello,

the bam_index_load_local function from bam_index.c fails to find index files named my_file.bai if the substring "bam" is present anywhere in the path. For example, the following file pair fails to be recognized:

"/home/bioinfo5/Desktop/UPM-CBGP/analysis/201810_Brapa_ChIP_FL/bams/3_filtdupes_FL_C1.bai" "/home/bioinfo5/Desktop/UPM-CBGP/analysis/201810_Brapa_ChIP_FL/bams/3_filtdupes_FL_C1.bam"

This is because when looking for the "bam" suffix, bam_index_load_local uses strstr, and fails if the returned index does not correspond to the last three characters. This could be fixed by looping while s is different than 0 when looking for the suffix (something like this, untested):

char *s = strstr(fn, "bam");
while(s) {
    if (s == fn + strlen(fn) - 3) {
        strcpy(fnidx, fn);
    fnidx[strlen(fn)-1] = 'i';
        fp = fopen(fnidx, "rb");
    } else {
        s = strstr(s, "bam");
    }
}

Cheers, -Eric