static int32_t countOverlapsEntry(GTFtree *t, GTFentry *e, uint32_t start, uint32_t end, int strand, int matchType, int strandType, int direction, int32_t max, FILTER_ENTRY_FUNC ffunc) {
    int dir;
    int32_t cnt = 0;
    if(!e) return cnt;
    
    switch(matchType) {
    case GTF_MATCH_EXACT :
        if((dir = rangeExact(start, end, e)) == 0) {
            cnt = 1;
        }
        break;
    case GTF_MATCH_WITHIN :
        if((dir = rangeAny(start, end, e)) == 0) {
            if(rangeWithin(start, end, e) == 0) cnt = 1;
        }
        break;
    case GTF_MATCH_CONTAIN :
        if((dir = rangeAny(start, end, e)) == 0) {
            if(rangeContains(start, end, e) == 0) cnt = 1;
        }
        break;
    case GTF_MATCH_START :
        if((dir = rangeStart(start, end, e)) == 0) {
            cnt = 1;
        }
        break;
    case GTF_MATCH_END :
        if((dir = rangeEnd(start, end, e)) == 0) {
            cnt = 1;
        }
        break;
    default :
        if((dir = rangeAny(start, end, e)) == 0) {
            cnt = 1;
        }
        break;
    }

    if(cnt) {
        if(!matchingStrand(e, strand, strandType)) cnt = 0;
    }

    if(cnt && ffunc) {
        if(!ffunc(t, e)) cnt = 0;
    }

    if(max && cnt >= max) return max;

    if(direction) {
        if(dir > 0) return cnt;
        return cnt + countOverlapsEntry(t, e->right, start, end, strand, matchType, strandType, direction, max, ffunc);
    } else {
        if(dir < 0) return cnt;
        return cnt + countOverlapsEntry(t, e->left, start, end, strand, matchType, strandType, direction, max, ffunc);
    }
}
Ejemplo n.º 2
0
void mergeAll()
/* read through nameHash */
/* look up in 4 population hashes */
/* call logMissing to report on missing data */
/* check chrom, position, strand, observed */
{
char outputFileName[64];
FILE *outputFileHandle = NULL;
struct hashCookie cookie;
struct hashEl *nameHashElement = NULL;
struct hashEl *helCEU = NULL;
struct hashEl *helCHB = NULL;
struct hashEl *helJPT = NULL;
struct hashEl *helYRI = NULL;
boolean allMatch = TRUE;
struct hapmapSnpsCombined *hap = NULL;

safef(outputFileName, sizeof(outputFileName), "hapmapSnpsCombined.tab");
outputFileHandle = mustOpen(outputFileName, "w");

cookie = hashFirst(nameHash);

while ((nameHashElement = hashNext(&cookie)) != NULL)
    {
    helCEU = hashLookup(hashCEU, nameHashElement->name);
    helCHB = hashLookup(hashCHB, nameHashElement->name);
    helJPT = hashLookup(hashJPT, nameHashElement->name);
    helYRI = hashLookup(hashYRI, nameHashElement->name);
    /* should convert to instance of hapmapSnps here */

    logMissing(nameHashElement->name, helCEU, helCHB, helJPT, helYRI);

    allMatch = matchingChroms(helCEU, helCHB, helJPT, helYRI);
    if (!allMatch)
        {
	fprintf(errorFileHandle, "different chroms for %s\n", nameHashElement->name);
	continue;
	}

    allMatch = matchingPositions(helCEU, helCHB, helJPT, helYRI);
    if (!allMatch)
        {
	fprintf(errorFileHandle, "different positions for %s\n", nameHashElement->name);
	continue;
	}

    /* if strand is different, log and fix if possible */
    allMatch = matchingStrand(helCEU, helCHB, helJPT, helYRI);
    if (!allMatch)
        {
	fprintf(errorFileHandle, "different strands for %s\n", nameHashElement->name);
	hap = fixStrandAndMerge(helCEU, helCHB, helJPT, helYRI);
	if (hap)
	    writeOutput(hap, outputFileHandle);
	continue;
	}

    /* different observed is not a fatal error? */
    allMatch = matchingObserved(helCEU, helCHB, helJPT, helYRI);
    if (!allMatch)
        {
	fprintf(errorFileHandle, "different observed for %s\n", nameHashElement->name);
	continue;
	}

    /* should just have 2 alleles in all */
    allMatch = matchingAlleles(helCEU, helCHB, helJPT, helYRI);
    if (!allMatch)
        {
	fprintf(errorFileHandle, "different alleles for %s\n", nameHashElement->name);
	continue;
	}
        
    hap = mergeOne(helCEU, helCHB, helJPT, helYRI);
    if (hap) 
        writeOutput(hap, outputFileHandle);

    }
carefulClose(&outputFileHandle);
}