9ff0083910e4547fc6b35b8900f4b8f8bc09d6a3 aamp Mon Jul 11 15:18:20 2011 +0200 changed the errAbort back to a warning. Also fixing (hopefully fixing) problem loading the index of local BAM files. with remote BAM files a chdir to a trash dir is made where the index is automatically downloaded however if the index is local, then the chdir is actually moving away from the index file and it cannot load correctly. so I check if the file is a URL or not first. diff --git src/lib/bamUdc.c src/lib/bamUdc.c index 428c4cc..3eead98 100644 --- src/lib/bamUdc.c +++ src/lib/bamUdc.c @@ -118,42 +118,45 @@ { mkdirTrashDirectory(dirName); size_t len = strlen(trashDir()) + 1 + strlen(dirName) + 1; samDir = needMem(len); safef(samDir, len, "%s/%s", trashDir(), dirName); } return samDir; } #endif//ndef KNETFILE_HOOKS boolean bamFileExistsUdc(char *fileOrUrl, char *udcFuseRoot) /* Return TRUE if we can successfully open the bam file and its index file. */ { char *bamFileName = samtoolsFileNameUdcFuse(fileOrUrl, udcFuseRoot); samfile_t *fh = samopen(bamFileName, "rb", NULL); +boolean usingUrl = (strstr(fileOrUrl, "tp://") || strstr(fileOrUrl, "https://")); if (fh != NULL) { #ifndef KNETFILE_HOOKS // When file is an URL, this caches the index file in addition to validating: // Since samtools's url-handling code saves the .bai file to the current directory, // chdir to a trash directory before calling bam_index_load, then chdir back. char *runDir = getCurrentDir(); char *samDir = getSamDir(); + if (usingUrl) setCurrentDir(samDir); #endif//ndef KNETFILE_HOOKS bam_index_t *idx = bam_index_load(bamFileName); #ifndef KNETFILE_HOOKS + if (usingUrl) setCurrentDir(runDir); #endif//ndef KNETFILE_HOOKS samclose(fh); if (idx == NULL) { warn("bamFileExists: failed to read index corresponding to %s", bamFileName); return FALSE; } free(idx); // Not freeMem, freez etc -- sam just uses malloc/calloc. return TRUE; } return FALSE; } samfile_t *bamOpenUdc(char *fileOrUrl, char **retBamFileName, char *udcFuseRoot) @@ -190,49 +193,51 @@ samclose(*pSamFile); *pSamFile = NULL; } } void bamFetchUdc(char *fileOrUrl, char *position, bam_fetch_f callbackFunc, void *callbackData, samfile_t **pSamFile, char *udcFuseRoot) /* Open the .bam file, fetch items in the seq:start-end position range, * and call callbackFunc on each bam item retrieved from the file plus callbackData. * This handles BAM files with "chr"-less sequence names, e.g. from Ensembl. * The pSamFile parameter is optional. If non-NULL it will be filled in, just for * the benefit of the callback function, with the open samFile. */ { char *bamFileName = NULL; samfile_t *fh = bamOpenUdc(fileOrUrl, &bamFileName, udcFuseRoot); +boolean usingUrl = (strstr(fileOrUrl, "tp://") || strstr(fileOrUrl, "https://")); if (pSamFile != NULL) *pSamFile = fh; int chromId, start, end; int ret = bam_parse_region(fh->header, position, &chromId, &start, &end); if (ret != 0 && startsWith("chr", position)) ret = bam_parse_region(fh->header, position+strlen("chr"), &chromId, &start, &end); if (ret != 0) // If the bam file does not cover the current chromosome, OK return; - #ifndef KNETFILE_HOOKS // Since samtools' url-handling code saves the .bai file to the current directory, // chdir to a trash directory before calling bam_index_load, then chdir back. char *runDir = getCurrentDir(); char *samDir = getSamDir(); +if (usingUrl) setCurrentDir(samDir); #endif//ndef KNETFILE_HOOKS bam_index_t *idx = bam_index_load(bamFileName); #ifndef KNETFILE_HOOKS +if (usingUrl) setCurrentDir(runDir); #endif//ndef KNETFILE_HOOKS if (idx == NULL) warn("bam_index_load(%s) failed.", bamFileName); else { ret = bam_fetch(fh->x.bam, idx, chromId, start, end, callbackData, callbackFunc); if (ret != 0) warn("bam_fetch(%s, %s (chromId=%d) failed (%d)", bamFileName, position, chromId, ret); free(idx); // Not freeMem, freez etc -- sam just uses malloc/calloc. } bamClose(&fh); } boolean bamIsRc(const bam1_t *bam) @@ -568,31 +573,31 @@ } #else // If we're not compiling with samtools, make stub routines so compile won't fail: boolean bamFileExistsUdcFuse(char *bamFileName, char *udcFuseRoot) /* Return TRUE if we can successfully open the bam file and its index file. */ { warn(COMPILE_WITH_SAMTOOLS, "bamFileExistsUdcFuse"); return FALSE; } samfile_t *bamOpenUdcFuse(char *fileOrUrl, char **retBamFileName) /* Return an open bam file, dealing with some FUSE caching if need be. */ { -errAbort(COMPILE_WITH_SAMTOOLS, "bamOpenUdc"); +warn(COMPILE_WITH_SAMTOOLS, "bamOpenUdc"); return FALSE; } void bamClose(samfile_t **pSamFile) /* Close down a samefile_t */ { errAbort(COMPILE_WITH_SAMTOOLS, "bamClose"); } void bamFetchUdcFuse(char *fileOrUrl, char *position, bam_fetch_f callbackFunc, void *callbackData, samfile_t **pSamFile, char *udcFuseRoot) /* Open the .bam file, fetch items in the seq:start-end position range, * and call callbackFunc on each bam item retrieved from the file plus callbackData. * This handles BAM files with "chr"-less sequence names, e.g. from Ensembl. * The pSamFile parameter is optional. If non-NULL it will be filled in, just for