예제 #1
0
void liftOver(char *oldFile, char *mapFile, double minMatch, 
                double minBlocks, int minSizeT, int minSizeQ,
                int minChainT, int minChainQ, bool multiple, bool noSerial, char *chainTable,
                char *newFile, char *unmappedFile)
/* liftOver - Move annotations from one assembly to another. */
{
struct hash *chainHash = newHash(0);		/* Old chromosome name keyed, chromMap valued. */
FILE *mapped = mustOpen(newFile, "w");
FILE *unmapped = mustOpen(unmappedFile, "w");
int errCt;

if (!fileExists(oldFile))
    errAbort("Can't find file: %s\n", oldFile);
verbose(1, "Reading liftover chains\n");
readLiftOverMap(mapFile, chainHash);
verbose(1, "Mapping coordinates\n");
if (optionExists("gff"))
    {
    fprintf(stderr, "WARNING: -gff is not recommended.\nUse 'ldHgGene -out=<file.gp>' and then 'liftOver -genePred <file.gp>'\n");
    if (multiple)
        errAbort("ERROR: -multiple is not supported for -gff.");
    if (chainTable)
        errAbort("ERROR: -chainTable is not supported for -gff.");
    liftOverGff(oldFile, chainHash, minMatch, minBlocks, mapped, unmapped);
    }
else if (optionExists("genePred"))
    {
    if (chainTable)
        errAbort("ERROR: -chainTable is not supported for -genePred.");
    liftOverGenePred(oldFile, chainHash, minMatch, minBlocks, fudgeThick,
                     mapped, unmapped, multiple);
    }
else if (optionExists("sample"))
    {
    if (multiple)
        errAbort("ERROR: -multiple is not supported for -sample.");
    if (chainTable)
        errAbort("ERROR: -chainTable is not supported for -sample.");
    liftOverSample(oldFile, chainHash, minMatch, minBlocks, fudgeThick,
                        mapped, unmapped);
    }
else if (optionExists("pslT"))
    {
    verbose(1, "Consider using pslMap instead of liftOver for PSL.\n");
    if (multiple)
        errAbort("ERROR: -multiple is not supported for -pslT.");
    if (chainTable)
        errAbort("ERROR: -chainTable is not supported for -pslT.");
    liftOverPsl(oldFile, chainHash, minMatch, minBlocks, fudgeThick,
                        mapped, unmapped);
    }
else if (optionExists("ends"))
    liftOverBedPlusEnds(oldFile, chainHash, minMatch, minBlocks, 
                minSizeT, minSizeQ, 
                minChainT, minChainQ, fudgeThick, mapped, unmapped, multiple, noSerial,
		chainTable, bedPlus, hasBin, tabSep, ends, &errCt);
else if (optionExists("bedPlus"))
    liftOverBedPlus(oldFile, chainHash, minMatch, minBlocks, 
                minSizeT, minSizeQ, 
                minChainT, minChainQ, fudgeThick, mapped, unmapped, multiple, noSerial,
		chainTable, bedPlus, hasBin, tabSep, &errCt);
else if (optionExists("positions"))
    liftOverPositions(oldFile, chainHash, minMatch, minBlocks, minSizeT, minSizeQ, 
                minChainT, minChainQ, fudgeThick, mapped, unmapped, multiple, 
		chainTable, &errCt);
else
    liftOverBed(oldFile, chainHash, minMatch, minBlocks, minSizeT, minSizeQ, 
                minChainT, minChainQ, fudgeThick, mapped, unmapped, multiple, noSerial,
		chainTable, &errCt);
if (!optionExists("positions"))
/* I guess liftOverPositions closes these files.  This is a little akward though. */
    {
    carefulClose(&mapped);
    carefulClose(&unmapped);
    }
}
void doMiddle(struct cart *theCart)
/* Set up globals and make web page */
{
/* struct liftOverChain *chainList = NULL, *chain; */
char *userData;
/* char *dataFile; */
char *dataFormat;
char *organism;
char *db;
float minBlocks, minMatch;
boolean multiple, fudgeThick;
int minSizeQ, minSizeT;
boolean refreshOnly = FALSE;

/* char *err = NULL; */
struct liftOverChain *chainList = NULL, *choice;

cart = theCart;

if (cgiOptionalString(HGLFT_ERRORHELP_VAR))
    {
    puts("<PRE>");
    puts(liftOverErrHelp());
    //system("/usr/bin/cal");
    puts("</PRE>");
    return;
    }

/* Get data to convert - from userData variable, or if 
 * that is empty from a file. */

if (cartOptionalString(cart, "SubmitFile"))
    userData = cartOptionalString(cart, HGLFT_DATAFILE_VAR);
else
    userData = cartOptionalString(cart, HGLFT_USERDATA_VAR);
dataFormat = cartCgiUsualString(cart, HGLFT_DATAFORMAT_VAR, DEFAULT_FORMAT);
cartWebStart(cart, NULL, "Lift Genome Annotations");

getDbAndGenome(cart, &db, &organism, oldVars);

chainList = liftOverChainListFiltered();

choice = defaultChoices(chainList, db);
if (choice == NULL)
    errAbort("Sorry, no conversions available from this assembly\n");

minSizeQ = cartCgiUsualInt(cart, HGLFT_MINSIZEQ, choice->minSizeQ);
minSizeT = cartCgiUsualInt(cart, HGLFT_MINSIZET, choice->minSizeT);
minBlocks = cartCgiUsualDouble(cart, HGLFT_MINBLOCKS, choice->minBlocks);
minMatch = cartCgiUsualDouble(cart, HGLFT_MINMATCH, choice->minMatch);
fudgeThick = cartCgiUsualBoolean(cart, HGLFT_FUDGETHICK, (choice->fudgeThick[0]=='Y') ? TRUE : FALSE);
multiple = cartCgiUsualBoolean(cart, HGLFT_MULTIPLE, (choice->multiple[0]=='Y') ? TRUE : FALSE);
refreshOnly = cartCgiUsualInt(cart, HGLFT_REFRESHONLY_VAR, 0);

webMain(choice, dataFormat, multiple);
liftOverChainFreeList(&chainList);

if (!refreshOnly && userData != NULL && userData[0] != '\0')
    {
    struct hash *chainHash = newHash(0);
    char *chainFile;
    struct tempName oldTn, mappedTn, unmappedTn;
    FILE *old, *mapped, *unmapped;
    char *line;
    int lineSize;
    char *fromDb, *toDb;
    int ct = 0, errCt = 0;

    /* read in user data and save to file */
    makeTempName(&oldTn, HGLFT, ".user");
    old = mustOpen(oldTn.forCgi, "w");
    fputs(userData, old);
    fputs("\n", old);           /* in case user doesn't end last line */
    carefulClose(&old);
    chmod(oldTn.forCgi, 0666);

    /* setup output files -- one for converted lines, the other
     * for lines that could not be mapped */
    makeTempName(&mappedTn, HGLFT, ".bed");
    makeTempName(&unmappedTn, HGLFT, ".err");
    mapped = mustOpen(mappedTn.forCgi, "w");
    chmod(mappedTn.forCgi, 0666);
    unmapped = mustOpen(unmappedTn.forCgi, "w");
    chmod(unmappedTn.forCgi, 0666);

    fromDb = cgiString(HGLFT_FROMDB_VAR);
    toDb = cgiString(HGLFT_TODB_VAR);
    chainFile = liftOverChainFile(fromDb, toDb);
    if (chainFile == NULL)
        errAbort("ERROR: Can't convert from %s to %s: no chain file loaded",
                                fromDb, toDb);
    readLiftOverMap(chainFile, chainHash);
    if (sameString(dataFormat, WIGGLE_FORMAT))
        /* TODO: implement Wiggle */
	{}
    else if (sameString(dataFormat, POSITION_FORMAT))
	{
	/* minSizeT here and in liftOverChain.c/h has been renamed minChainT in liftOver.c */
	/* ignore multiple, it must be false when position is used */
	ct = liftOverPositions(oldTn.forCgi, chainHash, 
			minMatch, minBlocks, 0, minSizeQ,
			minSizeT, 0, 
			fudgeThick, mapped, unmapped, FALSE, NULL, &errCt);

	
        }
    else if (sameString(dataFormat, BED_FORMAT))
        {
	/* minSizeT here and in liftOverChain.c/h has been renamed minChainT in liftOver.c */
        ct = liftOverBed(oldTn.forCgi, chainHash, 
			minMatch, minBlocks, 0, minSizeQ,
			minSizeT, 0,
			fudgeThick, mapped, unmapped, multiple, NULL, &errCt);
        }
    else
        /* programming error */
        errAbort("ERROR: Unsupported data format: %s\n", dataFormat);

    webNewSection("Results");
    if (ct)
        {
        /* some records succesfully converted */
        cgiParagraph("");
        printf("Successfully converted %d record", ct);
        printf("%s: ", ct > 1 ? "s" : "");
        printf("<A HREF=%s TARGET=_blank>View Conversions</A>\n", mappedTn.forCgi);
        }
    if (errCt)
        {
        /* some records not converted */
        cgiParagraph("");
        printf("Conversion failed on %d record", errCt);
        printf("%s. &nbsp;&nbsp;&nbsp;", errCt > 1 ? "s" : "");
        printf("<A HREF=%s TARGET=_blank>Display failure file</A>&nbsp; &nbsp;\n",
                         unmappedTn.forCgi);
        printf("<A HREF=\"../cgi-bin/hgLiftOver?%s=1\" TARGET=_blank>Explain failure messages</A>\n", HGLFT_ERRORHELP_VAR);
        puts("<P>Failed input regions:\n");
        struct lineFile *errFile = lineFileOpen(unmappedTn.forCgi, TRUE);
        puts("<BLOCKQUOTE><PRE>\n");
        while (lineFileNext(errFile, &line, &lineSize))
            puts(line);
        lineFileClose(&errFile);
        puts("</PRE></BLOCKQUOTE>\n");
        }
    if (sameString(dataFormat, POSITION_FORMAT) && multiple)
	{
        puts("<BLOCKQUOTE><PRE>\n");
        puts("Note: multiple checkbox ignored since it is not supported for position format.");
        puts("</PRE></BLOCKQUOTE>\n");
	}
    carefulClose(&unmapped);
    }
webDataFormats();
webDownloads();
cartWebEnd();
}