b45b10995959ef874cb0f198d32c8fd475c0970f
galt
  Mon Apr 16 21:20:40 2012 -0700
remove -lm dependency on linefile; also enables others to use callback methods to support other types
diff --git src/lib/linefile.c src/lib/linefile.c
index 7d4da8c..dbc7d55 100644
--- src/lib/linefile.c
+++ src/lib/linefile.c
@@ -1,30 +1,29 @@
 /* lineFile - stuff to rapidly read text files and parse them into
  * lines.
  *
  * This file is copyright 2002 Jim Kent, but license is hereby
  * granted for all use - public, private or commercial. */
 
 #include "common.h"
 #include "hash.h"
 #include <fcntl.h>
 #include <signal.h>
 #include "dystring.h"
 #include "errabort.h"
 #include "linefile.h"
 #include "pipeline.h"
-#include "bigBed.h"
 #include "localmem.h"
 
 char *getFileNameFromHdrSig(char *m)
 /* Check if header has signature of supported compression stream,
    and return a phoney filename for it, or NULL if no sig found. */
 {
 char buf[20];
 char *ext=NULL;
 if (startsWith("\x1f\x8b",m)) ext = "gz";
 else if (startsWith("\x1f\x9d\x90",m)) ext = "Z";
 else if (startsWith("BZ",m)) ext = "bz2";
 else if (startsWith("PK\x03\x04",m)) ext = "zip";
 if (ext==NULL)
     return NULL;
 safef(buf, sizeof(buf), "somefile.%s", ext);
@@ -186,53 +185,30 @@
 struct lineFile *lineFileOnString(char *name, bool zTerm, char *s)
 /* Wrap a line file object around string in memory. This buffer
  * have zeroes written into it and be freed when the line file
  * is closed. */
 {
 struct lineFile *lf;
 AllocVar(lf);
 lf->fileName = cloneString(name);
 lf->fd = -1;
 lf->bufSize = lf->bytesInBuf = strlen(s);
 lf->zTerm = zTerm;
 lf->buf = s;
 return lf;
 }
 
-struct lineFile *lineFileOnBigBed(char *bigBedFileName)
-/* Wrap a line file object around a BigBed. */
-{
-struct lineFile *lf;
-AllocVar(lf);
-lf->fileName = cloneString(bigBedFileName);
-lf->bbiHandle = bigBedFileOpen(lf->fileName);
-lf->bbiChromList = bbiChromList(lf->bbiHandle);
-lf->bbiChrom = lf->bbiChromList;
-lf->bbiLm = lmInit(0);
-if (lf->bbiChrom)
-    {
-    lf->bbiIntervalList = bigBedIntervalQuery(
-	lf->bbiHandle, lf->bbiChrom->name, 0, lf->bbiChrom->size, 0, lf->bbiLm);
-    lf->bbiInterval = lf->bbiIntervalList;
-    }
-lf->fd = -1;
-lf->lineIx = 0;
-lf->bufSize = 64 * 1024;
-lf->buf = needMem(lf->bufSize);
-return lf;
-}
-
 struct lineFile *lineFileTabixMayOpen(char *fileOrUrl, bool zTerm)
 /* Wrap a line file around a data file that has been compressed and indexed
  * by the tabix command line program.  The index file <fileOrUrl>.tbi must be
  * readable in addition to fileOrUrl. If there's a problem, warn & return NULL.
  * This works only if kent/src has been compiled with USE_TABIX=1 and linked
  * with the tabix C library. */
 {
 #ifdef USE_TABIX
 int tbiNameSize = strlen(fileOrUrl) + strlen(".tbi") + 1;
 char *tbiName = needMem(tbiNameSize);
 safef(tbiName, tbiNameSize, "%s.tbi", fileOrUrl);
 tabix_t *tabix = ti_open(fileOrUrl, tbiName);
 if (tabix == NULL)
     {
     warn("Unable to open \"%s\"", fileOrUrl);
@@ -340,41 +316,36 @@
 void lineFileReuse(struct lineFile *lf)
 /* Reuse current line. */
 {
 lf->reuse = TRUE;
 }
 
 
 INLINE void noTabixSupport(struct lineFile *lf, char *where)
 {
 #ifdef USE_TABIX
 if (lf->tabix != NULL)
     lineFileAbort(lf, "%s: not implemented for lineFile opened with lineFileTabixMayOpen.", where);
 #endif // USE_TABIX
 }
 
-INLINE void noBigBedSupport(struct lineFile *lf, char *where)
-{
-if (lf->bbiHandle)
-    lineFileAbort(lf, "%s: not implemented for lineFile on BigBed.", where);
-}
-
 void lineFileSeek(struct lineFile *lf, off_t offset, int whence)
 /* Seek to read next line from given position. */
 {
 noTabixSupport(lf, "lineFileSeek");
-noBigBedSupport(lf, "lineFileSeek");
+if (lf->checkSupport)
+    lf->checkSupport(lf, "lineFileSeek");
 if (lf->pl != NULL)
     errnoAbort("Can't lineFileSeek on a compressed file: %s", lf->fileName);
 lf->reuse = FALSE;
 if (whence == SEEK_SET && offset >= lf->bufOffsetInFile
 	&& offset < lf->bufOffsetInFile + lf->bytesInBuf)
     {
     lf->lineStart = lf->lineEnd = offset - lf->bufOffsetInFile;
     }
 else
     {
     lf->lineStart = lf->lineEnd = lf->bytesInBuf = 0;
     if ((lf->bufOffsetInFile = lseek(lf->fd, offset, whence)) == -1)
 	errnoAbort("Couldn't lineFileSeek %s", lf->fileName);
     }
 }
@@ -436,72 +407,33 @@
 int endIx = lf->lineEnd;
 boolean gotLf = FALSE;
 int newStart;
 
 if (lf->reuse)
     {
     lf->reuse = FALSE;
     if (retSize != NULL)
 	*retSize = lf->lineEnd - lf->lineStart;
     *retStart = buf + lf->lineStart;
     if (lf->metaOutput && *retStart[0] == '#')
         metaDataAdd(lf, *retStart);
     return TRUE;
     }
 
-if (lf->bbiHandle)
-    {
-    int lineSize = 0;
-    if (!lf->bbiChrom)
-	return FALSE;
-    if (!lf->bbiInterval)
-	return FALSE;
-    lineSize = 1024; // some extra room
-    lineSize += strlen(lf->bbiChrom->name);
-    if (lf->bbiInterval->rest)
-	lineSize += strlen(lf->bbiInterval->rest);
+if (lf->nextCallBack)
+    return lf->nextCallBack(lf, retStart, retSize);
     
-    if (lineSize > lf->bufSize)
-	lineFileExpandBuf(lf, lineSize * 2);
-    safef(lf->buf, lf->bufSize, "%s\t%u\t%u", lf->bbiChrom->name, lf->bbiInterval->start, lf->bbiInterval->end);
-    if (lf->bbiInterval->rest)
-	{
-	safecat(lf->buf, lf->bufSize, "\t");
-	safecat(lf->buf, lf->bufSize, lf->bbiInterval->rest);
-	}
-    lf->bufOffsetInFile = -1;
-    lf->bytesInBuf = strlen(lf->buf);
-    lf->lineIx++;
-    lf->lineStart = 0;
-    lf->lineEnd = lineSize;
-    *retStart = lf->buf;
-    if (retSize != NULL)
-	*retSize = lineSize;
-
-    lf->bbiInterval = lf->bbiInterval->next;
-    if (!lf->bbiInterval)
-	{
-	lmCleanup(&lf->bbiLm);
-	lf->bbiLm = lmInit(0);
-	lf->bbiChrom = lf->bbiChrom->next;
-	if(lf->bbiChrom)
-	    lf->bbiIntervalList = bigBedIntervalQuery(
-		lf->bbiHandle, lf->bbiChrom->name, 0, lf->bbiChrom->size, 0, lf->bbiLm);
-	}
-
-    return TRUE;
-    }
 
 #ifdef USE_TABIX
 if (lf->tabix != NULL && lf->tabixIter != NULL)
     {
     // Just use line-oriented ti_read:
     int lineSize = 0;
     const char *line = ti_read(lf->tabix, lf->tabixIter, &lineSize);
     if (line == NULL)
 	return FALSE;
     lf->bufOffsetInFile = -1;
     lf->bytesInBuf = lineSize;
     lf->lineIx = -1;
     lf->lineStart = 0;
     lf->lineEnd = lineSize;
     if (lineSize > lf->bufSize)
@@ -705,36 +637,32 @@
         pipelineFree(&lf->pl);
         }
     else if (lf->fd > 0 && lf->fd != fileno(stdin))
 	{
 	close(lf->fd);
 	freeMem(lf->buf);
 	}
 #ifdef USE_TABIX
     else if (lf->tabix != NULL)
 	{
 	if (lf->tabixIter != NULL)
 	    ti_iter_destroy(lf->tabixIter);
 	ti_close(lf->tabix);
 	}
 #endif // USE_TABIX
-    if (lf->bbiHandle)
-	{
-    	lmCleanup(&lf->bbiLm);
-	bbiChromInfoFreeList(&lf->bbiChromList);
-	bbiFileClose(&lf->bbiHandle);
-	}
+    if (lf->closeCallBack)
+        lf->closeCallBack(lf);
     freeMem(lf->fileName);
     metaDataFree(lf);
     freez(pLf);
     }
 }
 
 void lineFileCloseList(struct lineFile **pList)
 /* Close up a list of line files. */
 {
 struct lineFile *el, *next;
 
 for (el = *pList; el != NULL; el = next)
     {
     next = el->next;
     lineFileClose(&el);