Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/macs3/reformat pyproject #662

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2020, macs3 project team All rights reserved.
Copyright (c) 2024, macs3 project team All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
25 changes: 13 additions & 12 deletions MACS3/Commands/bdgbroadcall_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time-stamp: <2024-05-15 10:41:43 Tao Liu>
# Time-stamp: <2024-10-02 15:55:43 Tao Liu>

"""Description: Fine-tuning script to call broad peaks from a single
bedGraph track for scores.
Expand All @@ -13,7 +13,6 @@
# python modules
# ------------------------------------

import sys
import os
from MACS3.IO import BedGraphIO
# ------------------------------------
Expand All @@ -23,36 +22,38 @@
# ------------------------------------
# Misc functions
# ------------------------------------
import logging
import MACS3.Utilities.Logger
from MACS3.Utilities.Logger import logging

logger = logging.getLogger(__name__)
debug = logger.debug
info = logger.info
error = logger.critical
warn = logger.warning
debug = logger.debug
info = logger.info
error = logger.critical
warn = logger.warning

# ------------------------------------
# Classes
# ------------------------------------

# ------------------------------------
# Main function
# ------------------------------------
def run( options ):


def run(options):
info("Read and build bedGraph...")
bio = BedGraphIO.bedGraphIO(options.ifile)
btrack = bio.read_bedGraph(baseline_value=0)

info("Call peaks from bedGraph...")

bpeaks = btrack.call_broadpeaks (options.cutoffpeak, options.cutofflink, options.minlen, options.lvl1maxgap, options.lvl2maxgap)
bpeaks = btrack.call_broadpeaks(options.cutoffpeak, options.cutofflink, options.minlen, options.lvl1maxgap, options.lvl2maxgap)

info("Write peaks...")

if options.ofile:
bf = open( os.path.join( options.outdir, options.ofile ), "w" )
bf = open(os.path.join(options.outdir, options.ofile), "w")
options.oprefix = options.ofile
else:
bf = open ( os.path.join( options.outdir, "%s_c%.1f_C%.2f_l%d_g%d_G%d_broad.bed12" % (options.oprefix,options.cutoffpeak,options.cutofflink,options.minlen,options.lvl1maxgap,options.lvl2maxgap)), "w" )
bf = open(os.path.join(options.outdir, "%s_c%.1f_C%.2f_l%d_g%d_G%d_broad.bed12" % (options.oprefix,options.cutoffpeak, options.cutofflink, options.minlen, options.lvl1maxgap, options.lvl2maxgap)), "w")
bpeaks.write_to_gappedPeak(bf, name_prefix=(options.oprefix+"_broadRegion").encode(), score_column="score", trackline=options.trackline)
info("Done")
56 changes: 28 additions & 28 deletions MACS3/Commands/bdgcmp_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time-stamp: <2024-05-15 10:13:23 Tao Liu>
# Time-stamp: <2024-10-02 16:06:33 Tao Liu>

"""Description: compare bdg files

Expand All @@ -7,14 +7,10 @@
the distribution).
"""

import sys
import os

from MACS3.IO import BedGraphIO
from MACS3.Utilities.OptValidator import opt_validate_bdgcmp

from math import log as mlog

# ------------------------------------
# constants
# ------------------------------------
Expand All @@ -27,15 +23,19 @@
# Main function
# ------------------------------------

def run( options ):
options = opt_validate_bdgcmp( options )

def run(options):
options = opt_validate_bdgcmp(options)
info = options.info
warn = options.warn
debug = options.debug
error = options.error
# warn = options.warn
# debug = options.debug
# error = options.error

scaling_factor = options.sfactor
pseudo_depth = 1.0/scaling_factor # not an actual depth, but its reciprocal, a trick to override SPMR while necessary.

# not an actual depth, but its reciprocal, a trick to override
# SPMR while necessary.
pseudo_depth = 1.0/scaling_factor

info("Read and build treatment bedGraph...")
tbio = BedGraphIO.bedGraphIO(options.tfile)
Expand All @@ -46,47 +46,47 @@ def run( options ):
cbtrack = cbio.read_bedGraph()

info("Build ScoreTrackII...")
sbtrack = tbtrack.make_ScoreTrackII_for_macs( cbtrack, depth1 = pseudo_depth, depth2 = pseudo_depth )
sbtrack = tbtrack.make_ScoreTrackII_for_macs(cbtrack, depth1=pseudo_depth, depth2=pseudo_depth)
if abs(scaling_factor-1) > 1e-6:
# Only for the case while your input is SPMR from MACS3 callpeak; Let's override SPMR.
info("Values in your input bedGraph files will be multiplied by %f ..." % scaling_factor)
sbtrack.change_normalization_method( ord('M') ) # a hack to override SPMR
sbtrack.set_pseudocount( options.pseudocount )
sbtrack.change_normalization_method(ord('M')) # a hack to override SPMR
sbtrack.set_pseudocount(options.pseudocount)

already_processed_method_list = []
for (i, method) in enumerate(options.method):
if method in already_processed_method_list:
continue
else:
already_processed_method_list.append( method )
already_processed_method_list.append(method)

info("Calculate scores comparing treatment and control by '%s'..." % method)
if options.ofile:
ofile = os.path.join( options.outdir, options.ofile[ i ] )
ofile = os.path.join(options.outdir, options.ofile[i])
else:
ofile = os.path.join( options.outdir, options.oprefix + "_" + method + ".bdg" )
ofile = os.path.join(options.outdir, options.oprefix + "_" + method + ".bdg")
# build score track
if method == 'ppois':
sbtrack.change_score_method( ord('p') )
sbtrack.change_score_method(ord('p'))
elif method == 'qpois':
sbtrack.change_score_method( ord('q') )
sbtrack.change_score_method(ord('q'))
elif method == 'subtract':
sbtrack.change_score_method( ord('d') )
sbtrack.change_score_method(ord('d'))
elif method == 'logFE':
sbtrack.change_score_method( ord('f') )
sbtrack.change_score_method(ord('f'))
elif method == 'FE':
sbtrack.change_score_method( ord('F') )
sbtrack.change_score_method(ord('F'))
elif method == 'logLR': # log likelihood
sbtrack.change_score_method( ord('l') )
sbtrack.change_score_method(ord('l'))
elif method == 'slogLR': # log likelihood
sbtrack.change_score_method( ord('s') )
sbtrack.change_score_method(ord('s'))
elif method == 'max':
sbtrack.change_score_method( ord('M') )
sbtrack.change_score_method(ord('M'))
else:
raise Exception("Can't reach here!")

info("Write bedGraph of scores...")
ofhd = open( ofile, "w" )
ofhd = open(ofile, "w")
# write_bedGraph function for ScoreTrack
sbtrack.write_bedGraph(ofhd,name="%s_Scores" % (method.upper()),description="Scores calculated by %s" % (method.upper()), column = 3)
sbtrack.write_bedGraph(ofhd, name="%s_Scores" % (method.upper()), description="Scores calculated by %s" % (method.upper()), column=3)
info("Finished '%s'! Please check '%s'!" % (method, ofile))
66 changes: 31 additions & 35 deletions MACS3/Commands/bdgdiff_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time-stamp: <2024-05-15 10:42:27 Tao Liu>
# Time-stamp: <2024-10-02 16:11:19 Tao Liu>

"""Description: Naive call differential peaks from 4 bedGraph tracks for scores.

Expand All @@ -11,7 +11,6 @@
# python modules
# ------------------------------------

import sys
import os
from MACS3.IO import BedGraphIO
from MACS3.Signal import ScoreTrack
Expand All @@ -23,28 +22,26 @@
# ------------------------------------
# Misc functions
# ------------------------------------
import logging
import MACS3.Utilities.Logger
from MACS3.Utilities.Logger import logging

logger = logging.getLogger(__name__)
debug = logger.debug
info = logger.info
error = logger.critical
warn = logger.warning
debug = logger.debug
info = logger.info
error = logger.critical
warn = logger.warning
# ------------------------------------
# Classes
# ------------------------------------

# ------------------------------------
# Main function
# ------------------------------------
def run( options ):


def run(options):
if options.maxgap >= options.minlen:
error("MAXGAP should be smaller than MINLEN! Your input is MAXGAP = %d and MINLEN = %d" % (options.maxgap, options.minlen))

LLR_cutoff = options.cutoff
ofile_prefix = options.oprefix

info("Read and build treatment 1 bedGraph...")
t1bio = BedGraphIO.bedGraphIO(options.t1bdg)
t1btrack = t1bio.read_bedGraph()
Expand All @@ -68,44 +65,43 @@ def run( options ):
depth1 = depth2 / depth1
depth2 = 1.0
elif depth1 < depth2: # scale down condition 2 to size of condition 1
depth2 = depth1/ depth2
depth2 = depth1 / depth2
depth1 = 1.0
else: # no need to scale down any
depth1 = 1.0
depth2 = 1.0

twoconditionscore = ScoreTrack.TwoConditionScores( t1btrack,
c1btrack,
t2btrack,
c2btrack,
depth1,
depth2 )
twoconditionscore = ScoreTrack.TwoConditionScores(t1btrack,
c1btrack,
t2btrack,
c2btrack,
depth1,
depth2)
twoconditionscore.build()
twoconditionscore.finalize()
(cat1,cat2,cat3) = twoconditionscore.call_peaks(min_length=options.minlen, max_gap=options.maxgap, cutoff=options.cutoff)
(cat1, cat2, cat3) = twoconditionscore.call_peaks(min_length=options.minlen, max_gap=options.maxgap, cutoff=options.cutoff)

info("Write peaks...")

if options.ofile:
ofiles = [os.path.join( options.outdir, x ) for x in options.ofile]
name_prefix = [ x.encode() for x in options.ofile ]
ofiles = [os.path.join(options.outdir, x) for x in options.ofile]
name_prefix = [x.encode() for x in options.ofile]
else:
ofiles = [ os.path.join( options.outdir, "%s_c%.1f_cond1.bed" % (options.oprefix,options.cutoff)),
os.path.join( options.outdir, "%s_c%.1f_cond2.bed" % (options.oprefix,options.cutoff)),
os.path.join( options.outdir, "%s_c%.1f_common.bed" % (options.oprefix,options.cutoff))
]
name_prefix = [ x.encode() for x in [ options.oprefix+"_cond1_", options.oprefix+"_cond2_", options.oprefix+"_common_" ]]

nf = open( ofiles[ 0 ], 'w' )
cat1.write_to_bed(nf, name_prefix=name_prefix[ 0 ], name=b"condition 1", description=b"unique regions in condition 1", score_column="score")
ofiles = [os.path.join(options.outdir, "%s_c%.1f_cond1.bed" % (options.oprefix, options.cutoff)),
os.path.join(options.outdir, "%s_c%.1f_cond2.bed" % (options.oprefix, options.cutoff)),
os.path.join(options.outdir, "%s_c%.1f_common.bed" % (options.oprefix, options.cutoff))
]
name_prefix = [x.encode() for x in [options.oprefix+"_cond1_", options.oprefix+"_cond2_", options.oprefix+"_common_"]]

nf = open(ofiles[0], 'w')
cat1.write_to_bed(nf, name_prefix=name_prefix[0], name=b"condition 1", description=b"unique regions in condition 1", score_column="score")
nf.close()

nf = open( ofiles[ 1 ], 'w' )
cat2.write_to_bed(nf, name_prefix=name_prefix[ 1 ], name=b"condition 2", description=b"unique regions in condition 2", score_column="score")
nf = open(ofiles[1], 'w')
cat2.write_to_bed(nf, name_prefix=name_prefix[1], name=b"condition 2", description=b"unique regions in condition 2", score_column="score")
nf.close()

nf = open( ofiles[ 2 ], 'w' )
cat3.write_to_bed(nf, name_prefix=name_prefix[ 2 ], name=b"common", description=b"common regions in both conditions", score_column="score")
nf = open(ofiles[2], 'w')
cat3.write_to_bed(nf, name_prefix=name_prefix[2], name=b"common", description=b"common regions in both conditions", score_column="score")
nf.close()
info("Done")

32 changes: 15 additions & 17 deletions MACS3/Commands/bdgopt_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Time-stamp: <2024-05-15 11:15:48 Tao Liu>
# Time-stamp: <2024-10-02 16:12:50 Tao Liu>

"""Description: Modify bedGraph file

Expand All @@ -10,7 +10,6 @@
# ------------------------------------
# python modules
# ------------------------------------
import sys
import os

from MACS3.IO import BedGraphIO
Expand All @@ -31,13 +30,15 @@
# ------------------------------------
# Main function
# ------------------------------------
def run( options ):
options = opt_validate_bdgopt( options )


def run(options):
options = opt_validate_bdgopt(options)
info = options.info
warn = options.warn
debug = options.debug
error = options.error
# warn = options.warn
# debug = options.debug
# error = options.error

info("Read and build bedGraph...")
bio = BedGraphIO.bedGraphIO(options.ifile)
btrack = bio.read_bedGraph(baseline_value=0)
Expand All @@ -49,18 +50,15 @@ def run( options ):
else:
extraparam = float(options.extraparam[0])
if options.method.lower() == "multiply":
btrack.apply_func( lambda x: x * extraparam)
btrack.apply_func(lambda x: x * extraparam)
elif options.method.lower() == "add":
btrack.apply_func( lambda x: x + extraparam)
btrack.apply_func(lambda x: x + extraparam)
elif options.method.lower() == "max":
btrack.apply_func( lambda x: x if x> extraparam else extraparam )
btrack.apply_func(lambda x: x if x > extraparam else extraparam)
elif options.method.lower() == "min":
btrack.apply_func( lambda x: x if x< extraparam else extraparam )
btrack.apply_func(lambda x: x if x < extraparam else extraparam)

ofile = BedGraphIO.bedGraphIO( os.path.join( options.outdir, options.ofile ), data = btrack )
ofile = BedGraphIO.bedGraphIO(os.path.join(options.outdir, options.ofile), data=btrack)
info("Write bedGraph of modified scores...")
ofile.write_bedGraph(name="%s_modified_scores" % (options.method.upper()),description="Scores calculated by %s" % (options.method.upper()))
ofile.write_bedGraph(name="%s_modified_scores" % (options.method.upper()), description="Scores calculated by %s" % (options.method.upper()))
info("Finished '%s'! Please check '%s'!" % (options.method, ofile.bedGraph_filename))



Loading