#!/usr/bin/python2.7
import time
import os
import sys
import subprocess


def oss(cmd):
    print(cmd + "\n")
    os.system(cmd)

# ----------
# Arguments:
# ----------
root = sys.path[0]
print("Root is: " + str(root))
genome = "hg19"

inputPeakfile = sys.argv[1]

genome = "hg19"
if len(sys.argv) > 2:
    genome = sys.argv[2]

topN = 10000
if len(sys.argv) > 3:
    topN = int(sys.argv[3])

user = 'cboix'
if len(sys.argv) > 4:
    user = sys.argv[4]

# ----------
# Variables:
# ----------
genome_motifset = dict()
genome_motifset["hg19"] = "tfh"
genome_motifset["mm9"] = "tf"
genome_motifset["mm10"] = "tfh"

motifdir = "/broad/compbio/pouyak/motifs/verts/insts/" + \
    genome + "/" + genome_motifset[genome] + "/Intergenic/optmm"

genome_maskname = dict()
genome_maskname["hg19"] = "268435455"
genome_maskname['mm9'] = "1048575"
genome_maskname["mm10"] = "1099511627775"
maskname = genome_maskname[genome]

motifMatchFile = motifdir + "/8mer/" + maskname + "/matches/0.0.gz"
tmpdir = "/broad/hptmp/" + user + "/motifpipeline/" + \
    os.path.basename(inputPeakfile).split(".")[0]
cmd = "mkdir -p " + tmpdir
oss(cmd)
tmpfile = tmpdir + "/" + os.path.basename(inputPeakfile)

# ------------------
# Reformat peak file
# ------------------
print("Reformatting peak file...")
tmpfile_origwidth = tmpdir + "/" + \
    os.path.basename(inputPeakfile) + ".origwidth"
topNfilterStr = "|awk '$6<" + str(topN) + "{print}'"
cmd = "sh " + root + "/reformat.sh " + \
    inputPeakfile + topNfilterStr + " > " + tmpfile
oss(cmd)
cmd = "sh " + root + "/reformat_originalwidth.sh " + \
    inputPeakfile + topNfilterStr + "  > " + tmpfile_origwidth
oss(cmd)


def IsGoodSize(fn):
    if not os.path.isfile(fn):
        return False
    statinfo = os.stat(fn)
    if statinfo.st_size > 80000:
        return True
    else:
        return False

# Compute global enrichment score
gbdir = tmpfile + ".global_enrichment_score"
GlobalEnrichmentFn = gbdir + "/enrichments.txt.gz"
if not IsGoodSize(GlobalEnrichmentFn):
    print("Compute global enrichment score")
    cmd = "rm -rf " + gbdir + "; "
    cmd += "zsh " + root + "/Enricher.zzz.sh -d " + gbdir + " -o " + genome + \
        " -i " + tmpfile_origwidth + " -C 0 -p  " + motifdir + \
        " -P 0 -Z 1.96 -N 0 -n 0.0 -k " + maskname + "  -K 8"
    # set Number of Nodes = 0, make it local execution
    oss(cmd + "&")
else:
    print("Global enrichment scores already present in " +
          GlobalEnrichmentFn)

# grep motif-overlap
tmpfile2 = tmpfile + ".ol"
print("Grep motif overlap")
if not os.path.isfile(tmpfile2):
    cmd = "grep-overlap " + motifMatchFile + " " + \
        tmpfile + "|grep -v 8mer_C > " + tmpfile2
    oss(cmd)

# Compute positional and peak-rank score
PosRankEnrichmentFn = tmpfile2 + ".pickle.score"
print("Compute positional and peak-rank score")
if not os.path.isfile(PosRankEnrichmentFn):
    cmd = "python2.7 " + root + "/computePositionBias_RankBias.py " + tmpfile2
    oss(cmd)

# Compute CombineAvgScore
print("Waiting on Enricher...")
round = 0
while True:
    round += 1
    lastoutput = ""
    cmd = "ps aux|grep " + user + "|grep Enricher.zzz|wc -l"
    # if IsGoodSize(GlobalEnrichmentFn) or round>2000:
    output = subprocess.check_output(cmd, shell=True).strip()
    if output != lastoutput:
        print(output)
        lastoutput = output
    if output == "2":
        break
    time.sleep(3)
print("Enricher is done.")
#
print("Calculating: Combine Average Score")
cmd = "python2.7 " + root + "/combineAvgScore.py " + \
    GlobalEnrichmentFn + " " + PosRankEnrichmentFn
oss(cmd)
#
print("Calculating: Accept/Reject")
inputSummaryPickleFn = PosRankEnrichmentFn + ".summary.pickle"
cmd = "python2.7 " + root + "/AcceptReject.py " + inputSummaryPickleFn
oss(cmd)

tfname_HGNCname = dict()
for line in open(root + "/data/name-mapping.txt"):
    comps = line.strip().replace("_Neurons", "").split()
    tfname_HGNCname[comps[0].upper()] = comps[1]

TFname = "unknownTF"
comps = os.path.basename(inputPeakfile).split("_")
if len(comps) > 1:
    TFname = comps[1].upper()
    if TFname in tfname_HGNCname:
        TFname = tfname_HGNCname[TFname]
    TFname = TFname.replace("ALPHA", "A").replace("BETA", "B").replace(
        "GAMMA", "C").upper()

print("Making figures for TF name" + TFname)
cmd = "python2.7 " + root + "/makehtml_png.py " + str(tmpdir) + " " + str(TFname) + \
    " " + str(user) + " > " + str(tmpdir) + "/index.html"
oss(cmd)

5a2e8bbc4f308b22e681b421cd7a5b55738b47c1
<mask token>


def oss(cmd):
    print(cmd + '\n')
    os.system(cmd)


<mask token>


def IsGoodSize(fn):
    if not os.path.isfile(fn):
        return False
    statinfo = os.stat(fn)
    if statinfo.st_size > 80000:
        return True
    else:
        return False


<mask token>

<mask token>


def oss(cmd):
    print(cmd + '\n')
    os.system(cmd)


<mask token>
print('Root is: ' + str(root))
<mask token>
if len(sys.argv) > 2:
    genome = sys.argv[2]
<mask token>
if len(sys.argv) > 3:
    topN = int(sys.argv[3])
<mask token>
if len(sys.argv) > 4:
    user = sys.argv[4]
<mask token>
oss(cmd)
<mask token>
print('Reformatting peak file...')
<mask token>
oss(cmd)
<mask token>
oss(cmd)


def IsGoodSize(fn):
    if not os.path.isfile(fn):
        return False
    statinfo = os.stat(fn)
    if statinfo.st_size > 80000:
        return True
    else:
        return False


<mask token>
if not IsGoodSize(GlobalEnrichmentFn):
    print('Compute global enrichment score')
    cmd = 'rm -rf ' + gbdir + '; '
    cmd += ('zsh ' + root + '/Enricher.zzz.sh -d ' + gbdir + ' -o ' +
        genome + ' -i ' + tmpfile_origwidth + ' -C 0 -p  ' + motifdir +
        ' -P 0 -Z 1.96 -N 0 -n 0.0 -k ' + maskname + '  -K 8')
    oss(cmd + '&')
else:
    print('Global enrichment scores already present in ' + GlobalEnrichmentFn)
<mask token>
print('Grep motif overlap')
if not os.path.isfile(tmpfile2):
    cmd = ('grep-overlap ' + motifMatchFile + ' ' + tmpfile +
        '|grep -v 8mer_C > ' + tmpfile2)
    oss(cmd)
<mask token>
print('Compute positional and peak-rank score')
if not os.path.isfile(PosRankEnrichmentFn):
    cmd = 'python2.7 ' + root + '/computePositionBias_RankBias.py ' + tmpfile2
    oss(cmd)
print('Waiting on Enricher...')
<mask token>
while True:
    round += 1
    lastoutput = ''
    cmd = 'ps aux|grep ' + user + '|grep Enricher.zzz|wc -l'
    output = subprocess.check_output(cmd, shell=True).strip()
    if output != lastoutput:
        print(output)
        lastoutput = output
    if output == '2':
        break
    time.sleep(3)
print('Enricher is done.')
print('Calculating: Combine Average Score')
<mask token>
oss(cmd)
print('Calculating: Accept/Reject')
<mask token>
oss(cmd)
<mask token>
for line in open(root + '/data/name-mapping.txt'):
    comps = line.strip().replace('_Neurons', '').split()
    tfname_HGNCname[comps[0].upper()] = comps[1]
<mask token>
if len(comps) > 1:
    TFname = comps[1].upper()
    if TFname in tfname_HGNCname:
        TFname = tfname_HGNCname[TFname]
    TFname = TFname.replace('ALPHA', 'A').replace('BETA', 'B').replace('GAMMA',
        'C').upper()
print('Making figures for TF name' + TFname)
<mask token>
oss(cmd)

<mask token>


def oss(cmd):
    print(cmd + '\n')
    os.system(cmd)


root = sys.path[0]
print('Root is: ' + str(root))
genome = 'hg19'
inputPeakfile = sys.argv[1]
genome = 'hg19'
if len(sys.argv) > 2:
    genome = sys.argv[2]
topN = 10000
if len(sys.argv) > 3:
    topN = int(sys.argv[3])
user = 'cboix'
if len(sys.argv) > 4:
    user = sys.argv[4]
genome_motifset = dict()
genome_motifset['hg19'] = 'tfh'
genome_motifset['mm9'] = 'tf'
genome_motifset['mm10'] = 'tfh'
motifdir = ('/broad/compbio/pouyak/motifs/verts/insts/' + genome + '/' +
    genome_motifset[genome] + '/Intergenic/optmm')
genome_maskname = dict()
genome_maskname['hg19'] = '268435455'
genome_maskname['mm9'] = '1048575'
genome_maskname['mm10'] = '1099511627775'
maskname = genome_maskname[genome]
motifMatchFile = motifdir + '/8mer/' + maskname + '/matches/0.0.gz'
tmpdir = '/broad/hptmp/' + user + '/motifpipeline/' + os.path.basename(
    inputPeakfile).split('.')[0]
cmd = 'mkdir -p ' + tmpdir
oss(cmd)
tmpfile = tmpdir + '/' + os.path.basename(inputPeakfile)
print('Reformatting peak file...')
tmpfile_origwidth = tmpdir + '/' + os.path.basename(inputPeakfile
    ) + '.origwidth'
topNfilterStr = "|awk '$6<" + str(topN) + "{print}'"
cmd = ('sh ' + root + '/reformat.sh ' + inputPeakfile + topNfilterStr +
    ' > ' + tmpfile)
oss(cmd)
cmd = ('sh ' + root + '/reformat_originalwidth.sh ' + inputPeakfile +
    topNfilterStr + '  > ' + tmpfile_origwidth)
oss(cmd)


def IsGoodSize(fn):
    if not os.path.isfile(fn):
        return False
    statinfo = os.stat(fn)
    if statinfo.st_size > 80000:
        return True
    else:
        return False


gbdir = tmpfile + '.global_enrichment_score'
GlobalEnrichmentFn = gbdir + '/enrichments.txt.gz'
if not IsGoodSize(GlobalEnrichmentFn):
    print('Compute global enrichment score')
    cmd = 'rm -rf ' + gbdir + '; '
    cmd += ('zsh ' + root + '/Enricher.zzz.sh -d ' + gbdir + ' -o ' +
        genome + ' -i ' + tmpfile_origwidth + ' -C 0 -p  ' + motifdir +
        ' -P 0 -Z 1.96 -N 0 -n 0.0 -k ' + maskname + '  -K 8')
    oss(cmd + '&')
else:
    print('Global enrichment scores already present in ' + GlobalEnrichmentFn)
tmpfile2 = tmpfile + '.ol'
print('Grep motif overlap')
if not os.path.isfile(tmpfile2):
    cmd = ('grep-overlap ' + motifMatchFile + ' ' + tmpfile +
        '|grep -v 8mer_C > ' + tmpfile2)
    oss(cmd)
PosRankEnrichmentFn = tmpfile2 + '.pickle.score'
print('Compute positional and peak-rank score')
if not os.path.isfile(PosRankEnrichmentFn):
    cmd = 'python2.7 ' + root + '/computePositionBias_RankBias.py ' + tmpfile2
    oss(cmd)
print('Waiting on Enricher...')
round = 0
while True:
    round += 1
    lastoutput = ''
    cmd = 'ps aux|grep ' + user + '|grep Enricher.zzz|wc -l'
    output = subprocess.check_output(cmd, shell=True).strip()
    if output != lastoutput:
        print(output)
        lastoutput = output
    if output == '2':
        break
    time.sleep(3)
print('Enricher is done.')
print('Calculating: Combine Average Score')
cmd = ('python2.7 ' + root + '/combineAvgScore.py ' + GlobalEnrichmentFn +
    ' ' + PosRankEnrichmentFn)
oss(cmd)
print('Calculating: Accept/Reject')
inputSummaryPickleFn = PosRankEnrichmentFn + '.summary.pickle'
cmd = 'python2.7 ' + root + '/AcceptReject.py ' + inputSummaryPickleFn
oss(cmd)
tfname_HGNCname = dict()
for line in open(root + '/data/name-mapping.txt'):
    comps = line.strip().replace('_Neurons', '').split()
    tfname_HGNCname[comps[0].upper()] = comps[1]
TFname = 'unknownTF'
comps = os.path.basename(inputPeakfile).split('_')
if len(comps) > 1:
    TFname = comps[1].upper()
    if TFname in tfname_HGNCname:
        TFname = tfname_HGNCname[TFname]
    TFname = TFname.replace('ALPHA', 'A').replace('BETA', 'B').replace('GAMMA',
        'C').upper()
print('Making figures for TF name' + TFname)
cmd = 'python2.7 ' + root + '/makehtml_png.py ' + str(tmpdir) + ' ' + str(
    TFname) + ' ' + str(user) + ' > ' + str(tmpdir) + '/index.html'
oss(cmd)

import time
import os
import sys
import subprocess


def oss(cmd):
    print(cmd + '\n')
    os.system(cmd)


root = sys.path[0]
print('Root is: ' + str(root))
genome = 'hg19'
inputPeakfile = sys.argv[1]
genome = 'hg19'
if len(sys.argv) > 2:
    genome = sys.argv[2]
topN = 10000
if len(sys.argv) > 3:
    topN = int(sys.argv[3])
user = 'cboix'
if len(sys.argv) > 4:
    user = sys.argv[4]
genome_motifset = dict()
genome_motifset['hg19'] = 'tfh'
genome_motifset['mm9'] = 'tf'
genome_motifset['mm10'] = 'tfh'
motifdir = ('/broad/compbio/pouyak/motifs/verts/insts/' + genome + '/' +
    genome_motifset[genome] + '/Intergenic/optmm')
genome_maskname = dict()
genome_maskname['hg19'] = '268435455'
genome_maskname['mm9'] = '1048575'
genome_maskname['mm10'] = '1099511627775'
maskname = genome_maskname[genome]
motifMatchFile = motifdir + '/8mer/' + maskname + '/matches/0.0.gz'
tmpdir = '/broad/hptmp/' + user + '/motifpipeline/' + os.path.basename(
    inputPeakfile).split('.')[0]
cmd = 'mkdir -p ' + tmpdir
oss(cmd)
tmpfile = tmpdir + '/' + os.path.basename(inputPeakfile)
print('Reformatting peak file...')
tmpfile_origwidth = tmpdir + '/' + os.path.basename(inputPeakfile
    ) + '.origwidth'
topNfilterStr = "|awk '$6<" + str(topN) + "{print}'"
cmd = ('sh ' + root + '/reformat.sh ' + inputPeakfile + topNfilterStr +
    ' > ' + tmpfile)
oss(cmd)
cmd = ('sh ' + root + '/reformat_originalwidth.sh ' + inputPeakfile +
    topNfilterStr + '  > ' + tmpfile_origwidth)
oss(cmd)


def IsGoodSize(fn):
    if not os.path.isfile(fn):
        return False
    statinfo = os.stat(fn)
    if statinfo.st_size > 80000:
        return True
    else:
        return False


gbdir = tmpfile + '.global_enrichment_score'
GlobalEnrichmentFn = gbdir + '/enrichments.txt.gz'
if not IsGoodSize(GlobalEnrichmentFn):
    print('Compute global enrichment score')
    cmd = 'rm -rf ' + gbdir + '; '
    cmd += ('zsh ' + root + '/Enricher.zzz.sh -d ' + gbdir + ' -o ' +
        genome + ' -i ' + tmpfile_origwidth + ' -C 0 -p  ' + motifdir +
        ' -P 0 -Z 1.96 -N 0 -n 0.0 -k ' + maskname + '  -K 8')
    oss(cmd + '&')
else:
    print('Global enrichment scores already present in ' + GlobalEnrichmentFn)
tmpfile2 = tmpfile + '.ol'
print('Grep motif overlap')
if not os.path.isfile(tmpfile2):
    cmd = ('grep-overlap ' + motifMatchFile + ' ' + tmpfile +
        '|grep -v 8mer_C > ' + tmpfile2)
    oss(cmd)
PosRankEnrichmentFn = tmpfile2 + '.pickle.score'
print('Compute positional and peak-rank score')
if not os.path.isfile(PosRankEnrichmentFn):
    cmd = 'python2.7 ' + root + '/computePositionBias_RankBias.py ' + tmpfile2
    oss(cmd)
print('Waiting on Enricher...')
round = 0
while True:
    round += 1
    lastoutput = ''
    cmd = 'ps aux|grep ' + user + '|grep Enricher.zzz|wc -l'
    output = subprocess.check_output(cmd, shell=True).strip()
    if output != lastoutput:
        print(output)
        lastoutput = output
    if output == '2':
        break
    time.sleep(3)
print('Enricher is done.')
print('Calculating: Combine Average Score')
cmd = ('python2.7 ' + root + '/combineAvgScore.py ' + GlobalEnrichmentFn +
    ' ' + PosRankEnrichmentFn)
oss(cmd)
print('Calculating: Accept/Reject')
inputSummaryPickleFn = PosRankEnrichmentFn + '.summary.pickle'
cmd = 'python2.7 ' + root + '/AcceptReject.py ' + inputSummaryPickleFn
oss(cmd)
tfname_HGNCname = dict()
for line in open(root + '/data/name-mapping.txt'):
    comps = line.strip().replace('_Neurons', '').split()
    tfname_HGNCname[comps[0].upper()] = comps[1]
TFname = 'unknownTF'
comps = os.path.basename(inputPeakfile).split('_')
if len(comps) > 1:
    TFname = comps[1].upper()
    if TFname in tfname_HGNCname:
        TFname = tfname_HGNCname[TFname]
    TFname = TFname.replace('ALPHA', 'A').replace('BETA', 'B').replace('GAMMA',
        'C').upper()
print('Making figures for TF name' + TFname)
cmd = 'python2.7 ' + root + '/makehtml_png.py ' + str(tmpdir) + ' ' + str(
    TFname) + ' ' + str(user) + ' > ' + str(tmpdir) + '/index.html'
oss(cmd)

#!/usr/bin/python2.7
import time
import os
import sys
import subprocess


def oss(cmd):
    print(cmd + "\n")
    os.system(cmd)

# ----------
# Arguments:
# ----------
root = sys.path[0]
print("Root is: " + str(root))
genome = "hg19"

inputPeakfile = sys.argv[1]

genome = "hg19"
if len(sys.argv) > 2:
    genome = sys.argv[2]

topN = 10000
if len(sys.argv) > 3:
    topN = int(sys.argv[3])

user = 'cboix'
if len(sys.argv) > 4:
    user = sys.argv[4]

# ----------
# Variables:
# ----------
genome_motifset = dict()
genome_motifset["hg19"] = "tfh"
genome_motifset["mm9"] = "tf"
genome_motifset["mm10"] = "tfh"

motifdir = "/broad/compbio/pouyak/motifs/verts/insts/" + \
    genome + "/" + genome_motifset[genome] + "/Intergenic/optmm"

genome_maskname = dict()
genome_maskname["hg19"] = "268435455"
genome_maskname['mm9'] = "1048575"
genome_maskname["mm10"] = "1099511627775"
maskname = genome_maskname[genome]

motifMatchFile = motifdir + "/8mer/" + maskname + "/matches/0.0.gz"
tmpdir = "/broad/hptmp/" + user + "/motifpipeline/" + \
    os.path.basename(inputPeakfile).split(".")[0]
cmd = "mkdir -p " + tmpdir
oss(cmd)
tmpfile = tmpdir + "/" + os.path.basename(inputPeakfile)

# ------------------
# Reformat peak file
# ------------------
print("Reformatting peak file...")
tmpfile_origwidth = tmpdir + "/" + \
    os.path.basename(inputPeakfile) + ".origwidth"
topNfilterStr = "|awk '$6<" + str(topN) + "{print}'"
cmd = "sh " + root + "/reformat.sh " + \
    inputPeakfile + topNfilterStr + " > " + tmpfile
oss(cmd)
cmd = "sh " + root + "/reformat_originalwidth.sh " + \
    inputPeakfile + topNfilterStr + "  > " + tmpfile_origwidth
oss(cmd)


def IsGoodSize(fn):
    if not os.path.isfile(fn):
        return False
    statinfo = os.stat(fn)
    if statinfo.st_size > 80000:
        return True
    else:
        return False

# Compute global enrichment score
gbdir = tmpfile + ".global_enrichment_score"
GlobalEnrichmentFn = gbdir + "/enrichments.txt.gz"
if not IsGoodSize(GlobalEnrichmentFn):
    print("Compute global enrichment score")
    cmd = "rm -rf " + gbdir + "; "
    cmd += "zsh " + root + "/Enricher.zzz.sh -d " + gbdir + " -o " + genome + \
        " -i " + tmpfile_origwidth + " -C 0 -p  " + motifdir + \
        " -P 0 -Z 1.96 -N 0 -n 0.0 -k " + maskname + "  -K 8"
    # set Number of Nodes = 0, make it local execution
    oss(cmd + "&")
else:
    print("Global enrichment scores already present in " +
          GlobalEnrichmentFn)

# grep motif-overlap
tmpfile2 = tmpfile + ".ol"
print("Grep motif overlap")
if not os.path.isfile(tmpfile2):
    cmd = "grep-overlap " + motifMatchFile + " " + \
        tmpfile + "|grep -v 8mer_C > " + tmpfile2
    oss(cmd)

# Compute positional and peak-rank score
PosRankEnrichmentFn = tmpfile2 + ".pickle.score"
print("Compute positional and peak-rank score")
if not os.path.isfile(PosRankEnrichmentFn):
    cmd = "python2.7 " + root + "/computePositionBias_RankBias.py " + tmpfile2
    oss(cmd)

# Compute CombineAvgScore
print("Waiting on Enricher...")
round = 0
while True:
    round += 1
    lastoutput = ""
    cmd = "ps aux|grep " + user + "|grep Enricher.zzz|wc -l"
    # if IsGoodSize(GlobalEnrichmentFn) or round>2000:
    output = subprocess.check_output(cmd, shell=True).strip()
    if output != lastoutput:
        print(output)
        lastoutput = output
    if output == "2":
        break
    time.sleep(3)
print("Enricher is done.")
#
print("Calculating: Combine Average Score")
cmd = "python2.7 " + root + "/combineAvgScore.py " + \
    GlobalEnrichmentFn + " " + PosRankEnrichmentFn
oss(cmd)
#
print("Calculating: Accept/Reject")
inputSummaryPickleFn = PosRankEnrichmentFn + ".summary.pickle"
cmd = "python2.7 " + root + "/AcceptReject.py " + inputSummaryPickleFn
oss(cmd)

tfname_HGNCname = dict()
for line in open(root + "/data/name-mapping.txt"):
    comps = line.strip().replace("_Neurons", "").split()
    tfname_HGNCname[comps[0].upper()] = comps[1]

TFname = "unknownTF"
comps = os.path.basename(inputPeakfile).split("_")
if len(comps) > 1:
    TFname = comps[1].upper()
    if TFname in tfname_HGNCname:
        TFname = tfname_HGNCname[TFname]
    TFname = TFname.replace("ALPHA", "A").replace("BETA", "B").replace(
        "GAMMA", "C").upper()

print("Making figures for TF name" + TFname)
cmd = "python2.7 " + root + "/makehtml_png.py " + str(tmpdir) + " " + str(TFname) + \
    " " + str(user) + " > " + str(tmpdir) + "/index.html"
oss(cmd)
