Exemple #1
0
void copyPslXaToTab(char *pslFile, char *tabFile)
/* copy a single PSL XA to the tab file */
{
struct xAli *xa;
char *row[23];
struct lineFile *lf = lineFileOpen(pslFile, TRUE);
struct pipeline *pl = NULL;
FILE *tabFh = NULL;
if (noSort)
    tabFh = mustOpen(tabFile, "w");
else
    {
    if (pslCreateOpts & PSL_WITH_BIN)
	pl = pipelineOpen(outPipeBin, pipelineWrite, tabFile, NULL);
    else
	pl = pipelineOpen(outPipeNoBin, pipelineWrite, tabFile, NULL);
    tabFh = pipelineFile(pl);
    }
while (lineFileRow(lf, row))
    {
    xa = xAliLoad(row);
    if (pslCreateOpts & PSL_WITH_BIN)
        fprintf(tabFh, "%u\t", hFindBin(xa->tStart, xa->tEnd));
    xAliTabOut(xa, tabFh);
    xAliFree(&xa);
    }
lineFileClose(&lf);
if (noSort)
    carefulClose(&tabFh);
else
    {
    pipelineWait(pl);
    pipelineFree(&pl);
    }
}
void writeResults(char *tableName)
/* read through preliminary table with chimp and macaque alleles. */
/* lookup into snpHash. */
{
struct hashEl *hel= NULL;
char query[512];
struct sqlConnection *conn = hAllocConn();
struct sqlResult *sr;
char **row;
struct snpSubset *subsetElement = NULL;
int bin = 0;
int start = 0;
int end = 0;

verbose(1, "write results...\n");
safef(query, sizeof(query), "select chrom, chromStart, chromEnd, name, species, strand, allele from %s", tableName);
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    hel = hashLookup(snpHash, row[3]);
    if (hel == NULL) continue;
    subsetElement = (struct snpSubset *)hel->val;
    start = subsetElement->start;
    end = subsetElement->end;
    bin = hFindBin(start, end);
    fprintf(outputFileHandle, "%d\t%s\t%d\t%d\t%s\t", bin, subsetElement->chrom, start, end, row[3]); 
    fprintf(outputFileHandle, "0\t%s\t%s\t%s\t", subsetElement->strand, subsetElement->refUCSC, subsetElement->observed);
    fprintf(outputFileHandle, "%s\t%s\t%s\t%s\t%s\t%s\n", row[4], row[0], row[1], row[2], row[5], row[6]);
    }

}
void outputSummary(FILE *f, struct mafSummary *ms)
/* output to .tab file */
{
fprintf(f, "%u\t", hFindBin(ms->chromStart, ms->chromEnd));
mafSummaryTabOut(ms, f);
summaryCount++;
}
Exemple #4
0
void copyPslToTab(char *pslFile, char *tabFile)
/* copy a single PSL to the tab file */
{
struct psl *psl;
struct lineFile *lf = pslFileOpen(pslFile);
struct pipeline *pl = NULL;
FILE *tabFh = NULL;
if (noSort)
    tabFh = mustOpen(tabFile, "w");
else
    {
    if (pslCreateOpts & PSL_WITH_BIN)
	pl = pipelineOpen(outPipeBin, pipelineWrite, tabFile, NULL);
    else
	pl = pipelineOpen(outPipeNoBin, pipelineWrite, tabFile, NULL);
    tabFh = pipelineFile(pl);
    }
while ((psl = pslNext(lf)) != NULL)
    {
    if (pslCreateOpts & PSL_WITH_BIN)
        fprintf(tabFh, "%u\t", hFindBin(psl->tStart, psl->tEnd));
    pslTabOut(psl, tabFh);
    pslFree(&psl);
    }
lineFileClose(&lf);
if (noSort)
    carefulClose(&tabFh);
else
    {
    pipelineWait(pl);
    pipelineFree(&pl);
    }
}
Exemple #5
0
void hgPhMouse(char *database, char *track, int fileCount, char *fileNames[])
/* hgPhMouse - Load phMouse track. */
{
int i;
char *fileName;
char *tabName = "phMouse.tab";
FILE *f = mustOpen(tabName, "w");
struct lineFile *lf;
char *words[32], *s, c;
int wordCount;
int oneSize, totalSize = 0;

for (i=0; i<fileCount; ++i)
    {
    struct bed *bedList = NULL, *bed;
    fileName = fileNames[i];
    lf = lineFileOpen(fileName, TRUE);
    printf("Reading %s ", fileName);
    fflush(stdout);
    while ((wordCount = lineFileChop(lf, words)) > 0)
        {
	if (wordCount < 7)
	   errAbort("Expecting at least 7 words line %d of %s", 
	   	lf->lineIx, fileName);
	AllocVar(bed);
	bed->chrom = cloneString(words[0]);
	bed->chromStart = lineFileNeedNum(lf, words, 1);
	bed->chromEnd = lineFileNeedNum(lf, words, 2);
	bed->score = lineFileNeedNum(lf, words, 6);
	s = strrchr(words[3], '|');
	c = s[1];
	s[0] = 0;
	if (c != '+' && c != '-')
	    errAbort("Misformed strandless trace name line %d of %s",
	    	lf->lineIx, lf->fileName);
	bed->name = cloneString(words[3]);
	bed->strand[0] = c;
	slAddHead(&bedList, bed);
	}
    oneSize = slCount(bedList);
    printf("%d alignments ", oneSize);
    totalSize += oneSize;
    fflush(stdout);
    slSort(&bedList, bedCmp);
    printf("sorted ");
    fflush(stdout);
    for (bed = bedList; bed != NULL; bed = bed->next)
        {
	int bin = hFindBin(bed->chromStart, bed->chromEnd);
	fprintf(f, "%d\t", bin);
	bedTabOutN(bed, 6, f);
	}
    printf("tabbed out\n");
    bedFreeList(&bedList);
    }
carefulClose(&f);
printf("Loading %d items into %s.%s\n", totalSize, database, track);
loadDatabase(database, track, tabName);
remove(tabName);
}
void writeBedTab(char *fileName, struct bedStub *bedList, int bedSize)
/* Write out bed list to tab-separated file. */
{
struct bedStub *bed;
FILE *f = mustOpen(fileName, "w");
char *words[64];
int i, wordCount;
for (bed = bedList; bed != NULL; bed = bed->next)
    {
    if (!noBin)
        if (fprintf(f, "%u\t", hFindBin(bed->chromStart, bed->chromEnd)) <= 0)
	    writeFailed(fileName);
    if (strictTab)
	wordCount = chopTabs(bed->line, words);
    else
	wordCount = chopLine(bed->line, words);
    for (i=0; i<wordCount; ++i)
        {
	/*	new definition for old "reserved" field, now itemRgb */
	/*	and when itemRgb, it is a comma separated string r,g,b */
	if (itemRgb && (i == 8))
	    {
	    char *comma;
	    /*  Allow comma separated list of rgb values here   */
	    comma = strchr(words[8], ',');
	    if (comma)
		{
		int itemRgb = 0;
		if (-1 == (itemRgb = bedParseRgb(words[8])))
		    errAbort("ERROR: expecting r,g,b specification, "
				"found: '%s'", words[8]);
		else
		    if (fprintf(f, "%d", itemRgb) <= 0)
			writeFailed(fileName);

		verbose(2, "itemRgb: %s, rgb: %#x\n", words[8], itemRgb);
		}
	    else
		if (fputs(words[i], f) == EOF)
		    writeFailed(fileName);
	    }
	else
	    if (fputs(words[i], f) == EOF)
		writeFailed(fileName);

	if (i == wordCount-1)
	    {
	    if (fputc('\n', f) == EOF)
		writeFailed(fileName);
	    }
	else
	    if (fputc('\t', f) == EOF)
		writeFailed(fileName);
	}
    }
fclose(f);
}
Exemple #7
0
void gapFileToTable(struct sqlConnection *conn, char *gapFileName,
		    char *gapTableName)
/* Build a single gap table from a single gap file. */
{
struct lineFile *lf = lineFileOpen(gapFileName, TRUE);
char tabFileName[256];
FILE *tabFile = NULL;
char *words[16];
int wordCount;

safef(tabFileName, sizeof(tabFileName), "%s.tab", gapTableName);
tabFile = mustOpen(tabFileName, "w");
while ((wordCount = lineFileChop(lf, words)) > 0)
    {
    if (wordCount < 5)
	errAbort("Short line %d of %s", lf->lineIx, lf->fileName);
    if (words[4][0] == 'N' || words[4][0] == 'U')
	{
	int len = strlen(words[0]);
	if (len > maxChromNameSize)
	    {
	    maxChromNameSize = len;
	    if (maxChromNameSize > 254)
		errAbort("ERROR: chrom name size is over 254(%d) characters: "
			"'%s'", maxChromNameSize, words[0]);
	    }
	struct agpGap gap;
	agpGapStaticLoad(words, &gap);
	gap.chromStart -= 1;
	fprintf(tabFile, "%u\t", hFindBin(gap.chromStart, gap.chromEnd));
	agpGapTabOut(&gap, tabFile);
	}
    }
lineFileClose(&lf);
fclose(tabFile);

if (! noLoad)
    {
    struct dyString *ds = newDyString(2048);
    if (unsplit)
	sqlDyStringPrintf(ds,  createGapUnsplit, gapTableName,
		maxChromNameSize, maxChromNameSize);
    else
	sqlDyStringPrintf(ds, createGapSplit, gapTableName);
    char query[1024];
    sqlRemakeTable(conn, gapTableName, ds->string);
    sqlSafef(query, sizeof(query), "LOAD data local infile '%s' into table %s", 
	  tabFileName, gapTableName);
    sqlUpdate(conn, query);
    remove(tabFileName);
    freeDyString(&ds);
    }
}
void readFreq()
{
FILE *outputFileHandle = mustOpen("snpFreq.tab", "w");
FILE *logFileHandle = mustOpen("snpFreq.log", "w");
char query[512];
struct sqlConnection *conn = hAllocConn();
struct sqlResult *sr;
char **row;
struct hashEl *alleleHashEl = NULL;
struct hashEl *snpHashEl = NULL;
struct coords *coordsInstance = NULL;
char snpName[32];
int bin = 0;

safef(query, sizeof(query), "select snp_id, allele_id, freq from SNPAlleleFreq");
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    alleleHashEl = hashLookup(alleleHash, row[1]);
    if (alleleHashEl == NULL)
        {
        fprintf(logFileHandle, "couldn't find allele_id %s\n", row[1]);
	continue;
	}
    safef(snpName, sizeof(snpName), "rs%s", row[0]);
    snpHashEl = hashLookup(snpHash, snpName);
    if (snpHashEl == NULL)
        {
	fprintf(logFileHandle, "skipping snp_id %s\n", row[0]);
	continue;
	}
    coordsInstance = (struct coords *)snpHashEl->val;
    /* could add bin here */
    bin = hFindBin(coordsInstance->start, coordsInstance->end);
    fprintf(outputFileHandle, "%d\t%s\t%d\t%d\t%s\t%s\t%f\n",
            bin, coordsInstance->chrom, coordsInstance->start, coordsInstance->end,
	    snpName, (char *)alleleHashEl->val, sqlFloat(row[2]));
    }
carefulClose(&outputFileHandle);
carefulClose(&logFileHandle);
sqlFreeResult(&sr);
hFreeConn(&conn);
}
void addGlBin(char *in, char *out)
/* Copy in to out, but adding bin field in first column. */
{
    char *row[4];
    int i, start, end;
    struct lineFile *lf = lineFileOpen(in, TRUE);
    FILE *f = mustOpen(out, "w");

    while (lineFileRow(lf, row))
    {
        start = sqlUnsigned(row[1]);
        end = sqlUnsigned(row[2]);
        fprintf(f, "%u", hFindBin(start, end));
        for (i=0; i<ArraySize(row); ++i)
            fprintf(f, "\t%s", row[i]);
        fprintf(f, "\n");
    }
    carefulClose(&f);
    lineFileClose(&lf);
}
Exemple #10
0
static void gbGeneTblWriteGene(struct gbGeneTbl *ggt, struct gbStatus* status,
                               struct psl* psl, struct sqlConnection *conn)
/* write genePred row */
{
struct genePred* gp
    = genePredFromPsl3(psl, &status->cds, 
                       (ggt->hasExtCols ? genePredAllFlds : 0), genePredPslCdsMod3,
                       genePredStdInsertMergeSize, genePredStdInsertMergeSize);
FILE *fh = gbGeneTblGetTabFh(ggt, conn);
if (ggt->hasExtCols)
    {
    /* add gene name */
    freeMem(gp->name2);
    gp->name2 = cloneString(status->geneName);
    }
if (ggt->hasBin)
    fprintf(fh, "%u\t", hFindBin(gp->txStart, gp->txEnd));
genePredTabOut(gp, fh);
genePredFree(&gp);
}
Exemple #11
0
void copyGene(char *db, struct genePred *gene, FILE *tabFh)
/* copy one gene to the tab file */
{
unsigned holdOptFields = gene->optFields;
unsigned optFields = (genePredScoreFld|genePredName2Fld|genePredCdsStatFld|genePredExonFramesFld);

if (gGenePredExt && ((optFields & optFields) != optFields))
    errAbort("genePred %s doesn't have fields required for -genePredExt", gene->name);

if (gNoValidate || checkGene(db, gene))
    {
    if (!gGenePredExt)
        gene->optFields = 0;  /* omit optional fields */

    if (gBin)
        fprintf(tabFh, "%u\t", hFindBin(gene->txStart, gene->txEnd));
    genePredTabOut(gene, tabFh);

    gene->optFields = holdOptFields; /* restore optional fields */
    }
}
Exemple #12
0
void getSnps()
{
char query[512];
struct sqlConnection *conn = hAllocConn();
struct sqlResult *sr;
char **row;
int start = 0;
int end = 0;
int bin = 0;

sqlSafef(query, sizeof(query), "select * from chrX_snp126 where chromEnd < 2709520");
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    fprintf(outputFileHandle, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t", 
                              row[0], "chrY", row[2], row[3], row[4], row[5], row[6], row[7], row[8]);
    fprintf(outputFileHandle, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", 
                              row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17]);
    }
sqlFreeResult(&sr);

sqlSafef(query, sizeof(query), "select * from chrX_snp126 where chromEnd > 154584237");
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    start = sqlUnsigned(row[2]);
    end = sqlUnsigned(row[3]);
    start = start - 97140800;
    end = end - 97140800;
    bin = hFindBin(start, end);
    fprintf(outputFileHandle, "%d\t%s\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t", 
                              bin, "chrY", start, end, row[4], row[5], row[6], row[7], row[8]);
    fprintf(outputFileHandle, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", 
                              row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17]);
    }
sqlFreeResult(&sr);

hFreeConn(&conn);
}
Exemple #13
0
void readOneOut(char *rmskFile)
/* Read .out file rmskFile, check each line, and print OK lines to .tab. */
{
struct lineFile *lf;
char *line, *words[24];
int lineSize, wordCount;

/* Open .out file and process header. */
lf = lineFileOpen(rmskFile, TRUE);
if (!lineFileNext(lf, &line, &lineSize))
    errAbort("Empty %s", lf->fileName);
if (!startsWith("   SW  perc perc", line))
    {
    if (!startsWith("   SW   perc perc", line))
	errAbort("%s doesn't seem to be a RepeatMasker .out file, first "
	    "line seen:\n%s", lf->fileName, line);
    }
lineFileNext(lf, &line, &lineSize);
lineFileNext(lf, &line, &lineSize);

/* Process line oriented records of .out file. */
while (lineFileNext(lf, &line, &lineSize))
    {
    static struct rmskOut2 r;
    char *s;

    wordCount = chopLine(line, words);
    if (wordCount < 14)
        errAbort("Expecting 14 or 15 words line %d of %s", 
	    lf->lineIx, lf->fileName);
    r.swScore = atoi(words[0]);
    r.milliDiv = makeMilli(words[1], lf);
    r.milliDel = makeMilli(words[2], lf);
    r.milliIns = makeMilli(words[3], lf);
    r.genoName = words[4];
    r.genoStart = atoi(words[5])-1;
    r.genoEnd = atoi(words[6]);
    r.genoLeft = parenInt(words[7], lf);
    r.strand[0]  = (words[8][0] == '+' ? '+' : '-');
    r.repName = words[9];
    r.repClass = words[10];
    char *repClassTest = cloneString(r.repClass);
    stripChar(repClassTest, '(');
    stripChar(repClassTest, ')');
    int nonDigitCount = countLeadingNondigits(repClassTest);
    int wordOffset = 0;
    // this repClass is only digits, (or only (digits) with surrounding parens)
    //   this is the sign of an empty field here
    // due to custom library in use that has no class/family indication
    if (0 == nonDigitCount)
        {
        wordOffset = 1;
        r.repClass = cloneString("Unspecified");
        r.repFamily = cloneString("Unspecified");
        }
    else
        {
        s = strchr(r.repClass, '/');
        if (s == NULL)
            r.repFamily = r.repClass;
        else
           {
           *s++ = 0;
           r.repFamily = s;
           }
        }
    r.repStart = parenInt(words[11-wordOffset], lf);
    r.repEnd = atoi(words[12-wordOffset]);
    r.repLeft = parenInt(words[13-wordOffset], lf);
    r.id = atoi(words[14-wordOffset]);
    if (words[8][0] == 'C')
	{
	r.repLeft = parenInt(words[11-wordOffset], lf);
	r.repStart = parenInt(words[13-wordOffset], lf);
	}
    if (checkRepeat(&r, lf))
        {
	FILE *f = getFileForChrom(r.genoName);
        if (!noBin)
            fprintf(f, "%u\t", hFindBin(r.genoStart, r.genoEnd));
        rmskOut2TabOut(&r, f);
        }
    }
}
Exemple #14
0
void writeBedTab(char *fileName, struct bedStub *bedList)
/* Write out bed list to tab-separated file. */
{
struct bedStub *bed;
FILE *f = mustOpen(fileName, "w");
char *words[64];
int i, wordCount;
for (bed = bedList; bed != NULL; bed = bed->next)
    {
    if (!noBin)
        {
        // allow for zero-length at start of seq [bin code can't handle 0-0]
        unsigned end = (bed->chromEnd > 0) ? bed->chromEnd : 1;
        if (fprintf(f, "%u\t", hFindBin(bed->chromStart, end)) <= 0)
	    writeFailed(fileName);
        }
    if (strictTab)
	wordCount = chopTabs(bed->line, words);
    else
	wordCount = chopLine(bed->line, words);
    for (i=0; i<wordCount; ++i)
        {
	/*	new definition for old "reserved" field, now itemRgb */
	/*	and when itemRgb, it is a comma separated string r,g,b */
	if (itemRgb && (i == 8))
	    {
	    char *comma;
	    /*  Allow comma separated list of rgb values here   */
	    comma = strchr(words[8], ',');
	    if (comma)
		{
		int itemRgb = 0;
		if (-1 == (itemRgb = bedParseRgb(words[8])))
		    errAbort("ERROR: expecting r,g,b specification, "
				"found: '%s'", words[8]);
		else
		    if (fprintf(f, "%d", itemRgb) <= 0)
			writeFailed(fileName);

		verbose(2, "itemRgb: %s, rgb: %#x\n", words[8], itemRgb);
		}
	    else
		if (fputs(words[i], f) == EOF)
		    writeFailed(fileName);
	    }
	else if ((dotIsNull > 0) && (dotIsNull == i) && sameString(words[i],"."))
        /* If the . was used to represent NULL, replace with -1 in the tables */
	    {
	    if (fputs("-1", f) == EOF)
		writeFailed(fileName);
	    }
	else
	    if (fputs(words[i], f) == EOF)
		writeFailed(fileName);

	if (i == wordCount-1)
	    {
	    if (fputc('\n', f) == EOF)
		writeFailed(fileName);
	    }
	else
	    if (fputc('\t', f) == EOF)
		writeFailed(fileName);
	}
    }
fclose(f);
}
Exemple #15
0
void writeResults(char *tableName)
{
char query[512];
struct sqlConnection *conn = hAllocConn();
struct sqlResult *sr;
char **row;

struct hashEl *helChimp = NULL, *helMacaque = NULL;
struct orthoSnp *chimpData, *macaqueData, *missingData = NULL;

char *snpId;
char *humanObserved;
char *humanAllele;
char *humanStrand;

int humanCount = 0;
int chimpOnlyCount = 0;
int macaqueOnlyCount = 0;
int missingCount = 0;
int bothCount = 0;
int chromStart = 0;
int chromEnd = 0;
int binVal = 0;

AllocVar(missingData);
missingData->chrom = cloneString("?");
missingData->start = 0;
missingData->end = 0;
missingData->strand = cloneString("?");
missingData->allele = cloneString("?");

sqlSafef(query, sizeof(query), "select chrom, chromStart, chromEnd, name, strand, refUCSC, observed from %s", tableName);
sr = sqlGetResult(conn, query);
while ((row = sqlNextRow(sr)) != NULL)
    {
    chromStart = sqlUnsigned(row[1]);
    chromEnd = sqlUnsigned(row[2]);
    snpId = cloneString(row[3]);
    humanStrand = cloneString(row[4]);
    humanAllele = cloneString(row[5]);
    humanObserved = cloneString(row[6]);
    humanCount++;

    helChimp = hashLookup(chimpHash, snpId);
    if (helChimp != NULL)
        chimpData = (struct orthoSnp *)helChimp->val;
    else
        chimpData = missingData;

    helMacaque = hashLookup(macaqueHash, snpId);
    if (helMacaque != NULL)
        macaqueData = (struct orthoSnp *)helMacaque->val;
    else
        macaqueData = missingData;

    if (!helChimp && !helMacaque)
        {
	fprintf(missingFileHandle, "%s not found\n", snpId);
	missingCount++;
	continue;
	}

    if (helChimp && helMacaque)
	bothCount++;

    if (!helChimp && helMacaque)
	macaqueOnlyCount++;

    if (helChimp && !helMacaque)
	chimpOnlyCount++;

    binVal = hFindBin(chromStart, chromEnd);
    fprintf(outputFileHandle, "%d\t%s\t%s\t%s\t", binVal, row[0], row[1], row[2]);
    fprintf(outputFileHandle, "%s\t%s\t%s\t%s\t", snpId, humanObserved, humanAllele, humanStrand);
    fprintf(outputFileHandle, "%s\t%d\t%d\t", chimpData->chrom, chimpData->start, chimpData->end);
    fprintf(outputFileHandle, "%s\t%s\t", chimpData->allele, chimpData->strand);
    fprintf(outputFileHandle, "%s\t%d\t%d\t", macaqueData->chrom, macaqueData->start, macaqueData->end);
    fprintf(outputFileHandle, "%s\t%s\n", macaqueData->allele, macaqueData->strand);
    }

verbose(1, "humanCount = %d\n", humanCount);
verbose(1, "chimpOnlyCount = %d\n", chimpOnlyCount);
verbose(1, "macaqueOnlyCount = %d\n", macaqueOnlyCount);
verbose(1, "missingCount = %d\n", missingCount);
verbose(1, "bothCount = %d\n", bothCount);
}
void cnWriteTables(char *chrom, struct cnFill *fillList, FILE *f, int depth)
/* Recursively write out fill and gap lists. */
{
    char qName[64];
    struct cnFill *fill;
    for (fill = fillList; fill != NULL; fill = fill->next)
    {
        if (fill->chainId != 0)
        {
            if (fill->type == NULL)
                errAbort("No type field, please run netSyntenic on input");
            if (fill->tN < 0)
            {
                if (warnFlag)
                {
                    if (!warned)
                    {
                        fprintf(stderr, "Warning: missing fields\n");
                        warned = TRUE;
                    }
                }
                else
                    errAbort("Missing fields.  Please run netClass on input");
            }
        }
        if (fill->score < 0)
            fill->score = 0;
        if (!noBin)
            fprintf(f, "%d\t", hFindBin(fill->tStart, fill->tStart + fill->tSize));
        qName[0] = 0;
        if (qPrefix != NULL)
        {
            strcat(qName, qPrefix);
            strcat(qName, "-");
        }
        strcat(qName, fill->qName);
        fprintf(f, "%d\t", depth);
        fprintf(f, "%s\t", chrom);
        fprintf(f, "%d\t", fill->tStart);
        fprintf(f, "%d\t", fill->tStart + fill->tSize);
        fprintf(f, "%c\t", fill->qStrand);
        fprintf(f, "%s\t", qName);
        fprintf(f, "%d\t", fill->qStart);
        fprintf(f, "%d\t", fill->qStart + fill->qSize);
        fprintf(f, "%d\t", fill->chainId);
        fprintf(f, "%d\t", fill->ali);
        fprintf(f, "%1.1f\t", fill->score);
        fprintf(f, "%d\t", fill->qOver);
        fprintf(f, "%d\t", fill->qFar);
        fprintf(f, "%d\t", fill->qDup);
        fprintf(f, "%s\t", (fill->type == NULL ? "gap" : fill->type));
        fprintf(f, "%d\t", fill->tN);
        fprintf(f, "%d\t", fill->qN);
        fprintf(f, "%d\t", fill->tR);
        fprintf(f, "%d\t", fill->qR);
        fprintf(f, "%d\t", fill->tNewR);
        fprintf(f, "%d\t", fill->qNewR);
        fprintf(f, "%d\t", fill->tOldR);
        fprintf(f, "%d\t", fill->qOldR);
        fprintf(f, "%d\t", fill->tTrf);
        fprintf(f, "%d\n", fill->qTrf);
        if (fill->children)
            cnWriteTables(chrom, fill->children, f, depth+1);
    }
}
void splitAgp(char *agpName, char *goldFileName, char *gapFileName)
/* Split up agp file into gold and gap files. */
{
    struct lineFile *lf;
    char *words[16];
    int wordCount;
    FILE *goldTab, *gapTab;

    /* Scan through .agp file splitting it into gold
     * and gap components. */
    goldTab = mustOpen(goldFileName, "w");
    gapTab = mustOpen(gapFileName, "w");
    lf = lineFileOpen(agpName, TRUE);
    while ((wordCount = lineFileChop(lf, words)) > 0)
    {
        int start, end;
        if (wordCount < 5)
            errAbort("Short line %d of %s", lf->lineIx, lf->fileName);
        int len = strlen(words[0]);
        if (len > maxChromNameSize)
        {
            maxChromNameSize = len;
            if (maxChromNameSize > 254)
                errAbort("ERROR: chrom name size is over 254(%d) characters: "
                         "'%s'", maxChromNameSize, words[0]);
        }

        start = sqlUnsigned(words[1])-1;
        end = sqlUnsigned(words[2]);
        if (words[4][0] == 'N' || words[4][0] == 'U')
        {
            struct agpGap gap;
            agpGapStaticLoad(words, &gap);
            gap.chromStart -= 1;
            fprintf(gapTab, "%u\t", hFindBin(start, end));
            agpGapTabOut(&gap, gapTab);
            verbose(3,"#GAP\t%s:%d-%d\n", gap.chrom, gap.chromStart, gap.chromEnd);
        }
        else
        {
            struct agpFrag gold;
            agpFragStaticLoad(words, &gold);
            agpFragValidate(&gold);
            len = strlen(words[5]);
            if (len > maxFragNameSize)
            {
                maxFragNameSize = len;
                if (maxFragNameSize > 254)
                    errAbort("ERROR: fragment name size is over 254(%d) "
                             "characters: '%s'", maxFragNameSize, words[5]);
            }
            // file is 1-based. agpFragLoad() now assumes 0-based.
            // and agpFragTabOut() will assume 1-based, but we will load
            // the generated file straight into the database, so
            // subtract 2:
            gold.chromStart -= 2;
            gold.fragStart  -= 2;
            fprintf(goldTab, "%u\t", hFindBin(start, end));
            agpFragTabOut(&gold, goldTab);
        }
    }
    lineFileClose(&lf);
    carefulClose(&goldTab);
    carefulClose(&gapTab);

}