src/hg/lib/bamFile.c 1.20

1.20 2010/02/25 22:05:02 angie
If compiling with KNETFILE_HOOKS (samtools extension that lets us hook in udc for URL I/O), don't cd to trash while loading the index -- if udc.cacheDir is a relative path, then that breaks, and if using udc we don't need samtools' index-caching.
Index: src/hg/lib/bamFile.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/lib/bamFile.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -b -B -U 4 -r1.19 -r1.20
--- src/hg/lib/bamFile.c	13 Feb 2010 00:18:39 -0000	1.19
+++ src/hg/lib/bamFile.c	25 Feb 2010 22:05:02 -0000	1.20
@@ -149,8 +149,9 @@
     }
 return cloneString(fileOrUrl);
 }
 
+#ifndef KNETFILE_HOOKS
 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. */
 {
@@ -164,24 +165,29 @@
     safef(samDir, len, "%s/%s", trashDir(), dirName);
     }
 return samDir;
 }
+#endif//ndef KNETFILE_HOOKS
 
 boolean bamFileExists(char *fileOrUrl)
 /* Return TRUE if we can successfully open the bam file and its index file. */
 {
 char *bamFileName = samtoolsFileName(fileOrUrl);
 samfile_t *fh = samopen(bamFileName, "rb", NULL);
 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();
     setCurrentDir(samDir);
+#endif//ndef KNETFILE_HOOKS
     bam_index_t *idx = bam_index_load(bamFileName);
+#ifndef KNETFILE_HOOKS
     setCurrentDir(runDir);
+#endif//ndef KNETFILE_HOOKS
     samclose(fh);
     if (idx == NULL)
 	{
 	warn("bamFileExists: failed to read index corresponding to %s", bamFileName);
@@ -225,21 +231,28 @@
 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();
 setCurrentDir(samDir);
+#endif//ndef KNETFILE_HOOKS
 bam_index_t *idx = bam_index_load(bamFileName);
+#ifndef KNETFILE_HOOKS
 setCurrentDir(runDir);
+#endif//ndef KNETFILE_HOOKS
 if (idx == NULL)
     warn("bam_index_load(%s) failed.", bamFileName);
-ret = bam_fetch(fh->x.bam, idx, chromId, start, end, callbackData, callbackFunc);
-if (ret != 0)
+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.
+    free(idx); // Not freeMem, freez etc -- sam just uses malloc/calloc.
+    }
 samclose(fh);
 }
 
 boolean bamIsRc(const bam1_t *bam)