#include "tools.h" #include #include #include int verbose = 0; void debug(const char *format, ...) { if ( !verbose ) return; va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); } void error(const char *format, ...) { va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); exit(-1); } int freadString(FILE *fil, char *s, int lenMax) { int len; size_t nread = fread(&len, sizeof(int), 1, fil); if (!nread && ferror(fil)) error("fread failed - abort.\n", nread); if (!nread && feof(fil)) { debug("EOF encountered, returning 0.\n"); return 0; } if (len > lenMax) error("String too long (%d/%d) - abort\n", len,lenMax); return fread(s, 1, len, fil); } void fwriteString (FILE *fil, char *s) { size_t nwritten, len; len = strlen(s) + 1 ; nwritten = fwrite (&len, sizeof(int), 1, fil); if ( nwritten!=1 ) error("fwriteString: could not write??\n"); nwritten = fwrite (s, 1, len, fil); if ( nwritten!=len ) error("fwriteString: could not write?? %d %d\n", len,nwritten); } void fwriteDict (FILE *fil, DICT *dict) { int i; size_t nwritten; fwriteString(fil, "DICT") ; nwritten = fwrite (&dict->max, sizeof(int), 1, fil) ; if ( nwritten != 1 ) error("fwriteDict: could not write??\n"); for (i = 0 ; i < dict->max ; ++i) fwriteString (fil, dictName (dict, i)) ; } DICT *freadDict (FILE *fil) { char string[256]; DICT *dict; int n; debug("Reading the dictionary..\n"); if ( !freadString(fil, string, 256) ) error("freadDict: Failed to fill the buffer.\n"); if ( strcmp(string, "DICT") ) error("Was the binary genotype file made with hapmap2bin? The magic string DICT not found!\n"); if ( fread(&n, sizeof(int),1, fil) != 1) error("Could not read the number of chromosomes.\n"); debug("Number of chromosomes in the binary genotype file: %d\n", n); dict = dictCreate(n); while (n--) { if ( !freadString(fil, string, 256) ) error("Could not read string.\n"); if ( !dictAdd(dict, string, 0)) error("Failed to add the string in the dictionary: %s\n", string); debug("Added into dictionary: %s\n", string); } debug("Dictionary created.\n"); return dict; } #define arrayExists(a) ((a) && (a)->magic == ARRAY_MAGIC) BOOL arrayInsertFast(Array a, void * s, int (*order)(void*, void*)) { int i, j, arraySize; if (!arrayExists (a)) { fprintf (stderr, "arrayInsert called on bad array %lx\n", (unsigned long)a) ; exit (-1) ; } if (arrayFind(a, s, &i,order)) return FALSE ; /* no doubles */ arraySize = arrayMax(a) ; j = arraySize + 1 ; uArray(a,j-1) ; /* to create space */ if ( i+1 < arraySize ) { char *src,*dst; src = uArray(a,i+1); dst = uArray(a,i+2); memmove(dst,src,(j-i-1)*a->size); } memcpy(uArray(a,i+1),s,a->size); return TRUE ; } // Find the start and length of each chromosome and check ordering. The arrays starts // and lengths will be filled. void chromosome_ranges(Array starts, Array lengths, Array chrArray, Array posArray) { int lastChr, lastPos, i, nsites, chr, pos; lastPos = -1; lastChr = -1; nsites = arrayMax(chrArray) - 1; if ( nsites+1 != arrayMax(posArray) ) error("chromosome_ranges: the array dimensions do not agree: %d %d\n", nsites,arrayMax(posArray)); for (i=0; i= 0) { if ( lastChr >= arrayMax(chrArray) ) error("chromosome_ranges: FIXME %d %d\n", lastChr,arrayMax(chrArray)); array(lengths, lastChr, int) = i - arr(starts, lastChr, int); // i is now the 1st index in the next chromosome } lastChr = chr; array(starts, lastChr, int) = i; lastPos = -1; } pos = arr(posArray, i, int); if (pos < lastPos) error("chromosome_ranges: Bad order detected, isite=%d, pos=%d prev_pos=%d\n", i,pos,lastPos); // debug("chromosome_ranges: %d %d .. %d\n", chr,pos, i); lastPos = pos; } if (lastChr >= 0) { if ( lastChr >= arrayMax(chrArray) ) error("chromosome_ranges: FIXME %d %d\n", lastChr,arrayMax(chrArray)); array(lengths, lastChr, int) = i - arr(starts, lastChr, int); } }