Пример #1
0
def jd2washU(fs, fout, cut, ext):
    """
    Convert PETs to washU long range interactions. 
    Track format according to http://wiki.wubrowse.org/Long-range
    @param fs: files of .jd files 
    @param fout: prefix of output files
    """
    print "Converting %s to washU track." % (",".join(fs))
    tmp = str(random.random())
    with open(tmp, "w") as f:
        for fin in fs:
            print "converting %s" % fin
            key, mat = parseJd(fin, cut)
            for t in mat:
                a = (key[0], max([0, t[1] - ext]), t[1] + ext)
                b = (key[1], max([0, t[2] - ext]), t[2] + ext)
                linea = [
                    a[0], a[1], a[2],
                    "%s:%s-%s,1" % (b[0], b[1], b[2]), t[0], "."
                ]
                lineb = [
                    b[0], b[1], b[2],
                    "%s:%s-%s,1" % (a[0], a[1], a[2]), t[0], "."
                ]
                f.write("\t".join(map(str, linea)) + "\n")
                f.write("\t".join(map(str, lineb)) + "\n")
    c1 = "bedtools sort -i %s > %s" % (tmp, fout)
    c2 = "rm %s" % tmp
    c3 = "bgzip %s" % fout
    c4 = "tabix -p bed %s.gz" % fout
    callSys([c1, c2, c3, c4])
    print "Converting %s to washU random accessed track finished." % fout
Пример #2
0
def loops2washU(fin, fout, logger, significant=1):
    """
    Convert interaction level loop file to washU long range interactions. 
    Track format according to http://wiki.wubrowse.org/Long-range
    @param fin: interactions in loop file
    @param fout: washU long-range interaction text file prefix
    @param significant: if set 1, only convert significant loops.
    """
    logger.info("Converting %s to washU long range interaction track." % fin)
    tmp = str(random.random())
    with open(tmp, "w") as f:
        for i, line in enumerate(open(fin)):
            if i == 0:
                continue
            line = line.split("\n")[0].split("\t")
            #only using significant results
            if significant and float(line[-1]) < 1:
                continue
            if str(line[1]) == "inf":
                line[1] = 100
            a = parseIv(line[6])
            b = parseIv(line[7])
            linea = [
                a[0], a[1], a[2],
                "%s,1" % line[7]
            ]
            lineb = [
                b[0], b[1], b[2],
                "%s,1" % line[6]
            ]
            f.write("\t".join(map(str, linea)) + "\n")
            f.write("\t".join(map(str, lineb)) + "\n")
    c1 = "bedtools sort -i %s > %s" % (tmp, fout)
    c2 = "rm %s" % tmp
    c3 = "bgzip %s" % fout
    c4 = "tabix -p bed %s.gz" % fout
    callSys([c1, c2, c3, c4])
    logger.info(
        "Converting %s to washU long range interaction track finished." % fin)
Пример #3
0
def jd2hic(fs, fout, cut, org, resolution):
    """
    Convert reads level bedpe to HIC.
    Track format according to https://github.com/theaidenlab/juicer/wiki/Pre#file-format
    @param fs: files of .jd files 
    @param fout: prefix of output files
    """
    print "Converting %s to .hic file which could be loaded in juicebox" % (
        ",".join(fs))
    tmp = str(random.random())
    ss = {"+": 0, "-": 1}
    with open(tmp, "w") as f:
        for fin in fs:
            print "converting %s" % fin
            key, mat = parseJd(fin, cut)
            for t in mat:
                line = [0, key[0], t[1], 0, 1, key[1], t[2], 1]
                f.write("\t".join(map(str, line)) + "\n")
    c1 = "juicer_tools pre -n -r {resolution} -d {fin} {fout} {org}".format(
        resolution=resolution, fin=tmp, fout=fout, org=org)
    c2 = "rm %s" % tmp
    callSys([c1, c2])
    print "Converting %s to juicer's hic file finished." % fout