4727e0251c76f69905d0748adab43484166d6870 angie Fri Mar 14 16:42:14 2014 -0700 In code review, Max pointed out that it's non-standard to conditionallyinclude header files in the middle of a .c file. While looking at it, I realized that there were a lot of unnecessary #ifdef's for KNETFILE_HOOKS in general, all because there was not yet an #else implementation of knetUdcInstall, which can be a no-op if KNETFILE_HOOKS is not used. So I yanked out a lot of unnecessary old stuff, and tested basic bam and vcf track operations with a few settings: USE_SAMTABIX USE_BAM USE_TABIX KNETFILE_HOOKS 1 (these tree are all implied by USE_SAMTABIX) 0 1 0 0 0 1 0 1 0 0 1 1 USE_TABIX without KNETFILE_HOOKS doesn't work because the tabix lib wants to download .tbi files into the current working directory, cgi-bin, and we don't allow that (and shouldn't!). If we were going to support that, we could add a change-dir solution as in bamFile.c, but I don't think we need to support it now that there's samtabix. refs #6235, refs #12850 diff --git src/lib/bamFile.c src/lib/bamFile.c index fd226e1..70b9fd2 100644 --- src/lib/bamFile.c +++ src/lib/bamFile.c @@ -1,64 +1,78 @@ /* bamFile -- interface to binary alignment format files using Heng Li's samtools lib. */ #include "common.h" #include "portable.h" #include "bamFile.h" #ifdef USE_BAM #include "htmshell.h" #include "udc.h" -#ifndef KNETFILE_HOOKS +#ifdef KNETFILE_HOOKS +// If KNETFILE_HOOKS is used (as recommended!), then we can simply call bam_index_load +// without worrying about the samtools lib creating local cache files in cgi-bin: + +static bam_index_t *bamOpenIdx(char *fileOrUrl) +/* If fileOrUrl has a valid accompanying .bai file, parse and return the index; + * otherwise return NULL. */ +{ +bam_index_t *idx = bam_index_load(fileOrUrl); +return idx; +} + +#else// no KNETFILE_HOOKS +// Oh well. The unmodified samtools lib downloads .bai files into the current +// working directory, which is cgi-bin -- not good. So we need to temporarily +// change to a trash directory, let samtools download there, then pop back to +// cgi-bin. + static char *getSamDir() /* Return the name of a trash dir for samtools to run in (it creates files in current dir) * and make sure the directory exists. */ { static char *samDir = NULL; char *dirName = "samtools"; if (samDir == NULL) { 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 static bam_index_t *bamOpenIdx(char *fileOrUrl) /* If fileOrUrl has a valid accompanying .bai file, parse and return the index; * otherwise return 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(); boolean usingUrl = (strstr(fileOrUrl, "tp://") || strstr(fileOrUrl, "https://")); if (usingUrl) setCurrentDir(samDir); -#endif//ndef KNETFILE_HOOKS bam_index_t *idx = bam_index_load(fileOrUrl); -#ifndef KNETFILE_HOOKS if (usingUrl) setCurrentDir(runDir); -#endif//ndef KNETFILE_HOOKS return idx; } +#endif//ndef KNETFILE_HOOKS + static void bamCloseIdx(bam_index_t **pIdx) /* Free unless already NULL. */ { if (pIdx != NULL && *pIdx != NULL) { free(*pIdx); // Not freeMem, freez etc -- sam just uses malloc/calloc. *pIdx = NULL; } } boolean bamFileExists(char *fileOrUrl) /* Return TRUE if we can successfully open the bam file and its index file. * NOTE: this doesn't give enough diagnostics */ { char *bamFileName = fileOrUrl;