3d890482f243af7cdb51d56b5efde7c43e2ed59e braney Wed Dec 15 13:13:52 2010 -0800 add routine to let validateFiles check for the signature at the beginning and end of bigWigs diff --git src/lib/bbiRead.c src/lib/bbiRead.c index a5e0fed..26bbc71 100644 --- src/lib/bbiRead.c +++ src/lib/bbiRead.c @@ -25,30 +25,67 @@ struct bbiZoomLevel *closestLevel = NULL; struct bbiZoomLevel *level; for (level = levelList; level != NULL; level = level->next) { int diff = desiredReduction - level->reductionLevel; if (diff >= 0 && diff < closestDiff) { closestDiff = diff; closestLevel = level; } } return closestLevel; } +boolean bbiFileCheckSigs(char *fileName, bits32 sig, char *typeName) +/* check file signatures at beginning and end of file */ +{ +int fd = mustOpenFd(fileName, O_RDONLY); +bits32 magic; +boolean isSwapped = FALSE; + +// look for signature at the beginning of the file +mustReadFd(fd, &magic, sizeof(magic)); + +if (magic != sig) + { + magic = byteSwap32(magic); + isSwapped = TRUE; + if (magic != sig) + return FALSE; + } + +// look for signature at the end of the file +mustLseek(fd, -sizeof(magic), SEEK_END); +mustReadFd(fd, &magic, sizeof(magic)); + +if (isSwapped) + { + magic = byteSwap32(magic); + if (magic != sig) + return FALSE; + } +else + { + if (magic != sig) + return FALSE; + } + +return TRUE; +} + struct bbiFile *bbiFileOpen(char *fileName, bits32 sig, char *typeName) /* Open up big wig or big bed file. */ { /* This code needs to agree with code in two other places currently - bigBedFileCreate, * and bigWigFileCreate. I'm thinking of refactoring to share at least between * bigBedFileCreate and bigWigFileCreate. It'd be great so it could be structured * so that it could send the input in one chromosome at a time, and send in the zoom * stuff only after all the chromosomes are done. This'd potentially reduce the memory * footprint by a factor of 2 or 4. Still, for now it works. -JK */ struct bbiFile *bbi; AllocVar(bbi); bbi->fileName = cloneString(fileName); struct udcFile *udc = bbi->udc = udcFileOpen(fileName, udcDefaultDir()); /* Read magic number at head of file and use it to see if we are proper file type, and