1 # Time-stamp: <2024-10-02 16:11:19 Tao Liu>
3 """Description: Naive call differential peaks from 4 bedGraph tracks for scores.
5 This code is free software; you can redistribute it and/or modify it
6 under the terms of the BSD License (see the file LICENSE included with
10 # ------------------------------------
12 # ------------------------------------
15 from MACS3
.IO
import BedGraphIO
16 from MACS3
.Signal
import ScoreTrack
18 # ------------------------------------
20 # ------------------------------------
22 # ------------------------------------
24 # ------------------------------------
25 from MACS3
.Utilities
.Logger
import logging
27 logger
= logging
.getLogger(__name__
)
30 error
= logger
.critical
32 # ------------------------------------
34 # ------------------------------------
36 # ------------------------------------
38 # ------------------------------------
42 if options
.maxgap
>= options
.minlen
:
43 error("MAXGAP should be smaller than MINLEN! Your input is MAXGAP = %d and MINLEN = %d" % (options
.maxgap
, options
.minlen
))
45 info("Read and build treatment 1 bedGraph...")
46 t1bio
= BedGraphIO
.bedGraphIO(options
.t1bdg
)
47 t1btrack
= t1bio
.read_bedGraph()
49 info("Read and build control 1 bedGraph...")
50 c1bio
= BedGraphIO
.bedGraphIO(options
.c1bdg
)
51 c1btrack
= c1bio
.read_bedGraph()
53 info("Read and build treatment 2 bedGraph...")
54 t2bio
= BedGraphIO
.bedGraphIO(options
.t2bdg
)
55 t2btrack
= t2bio
.read_bedGraph()
57 info("Read and build control 2 bedGraph...")
58 c2bio
= BedGraphIO
.bedGraphIO(options
.c2bdg
)
59 c2btrack
= c2bio
.read_bedGraph()
61 depth1
= options
.depth1
62 depth2
= options
.depth2
64 if depth1
> depth2
: # scale down condition 1 to size of condition 2
65 depth1
= depth2
/ depth1
67 elif depth1
< depth2
: # scale down condition 2 to size of condition 1
68 depth2
= depth1
/ depth2
70 else: # no need to scale down any
74 twoconditionscore
= ScoreTrack
.TwoConditionScores(t1btrack
,
80 twoconditionscore
.build()
81 twoconditionscore
.finalize()
82 (cat1
, cat2
, cat3
) = twoconditionscore
.call_peaks(min_length
=options
.minlen
, max_gap
=options
.maxgap
, cutoff
=options
.cutoff
)
84 info("Write peaks...")
87 ofiles
= [os
.path
.join(options
.outdir
, x
) for x
in options
.ofile
]
88 name_prefix
= [x
.encode() for x
in options
.ofile
]
90 ofiles
= [os
.path
.join(options
.outdir
, "%s_c%.1f_cond1.bed" % (options
.oprefix
, options
.cutoff
)),
91 os
.path
.join(options
.outdir
, "%s_c%.1f_cond2.bed" % (options
.oprefix
, options
.cutoff
)),
92 os
.path
.join(options
.outdir
, "%s_c%.1f_common.bed" % (options
.oprefix
, options
.cutoff
))
94 name_prefix
= [x
.encode() for x
in [options
.oprefix
+"_cond1_", options
.oprefix
+"_cond2_", options
.oprefix
+"_common_"]]
96 nf
= open(ofiles
[0], 'w')
97 cat1
.write_to_bed(nf
, name_prefix
=name_prefix
[0], name
=b
"condition 1", description
=b
"unique regions in condition 1", score_column
="score")
100 nf
= open(ofiles
[1], 'w')
101 cat2
.write_to_bed(nf
, name_prefix
=name_prefix
[1], name
=b
"condition 2", description
=b
"unique regions in condition 2", score_column
="score")
104 nf
= open(ofiles
[2], 'w')
105 cat3
.write_to_bed(nf
, name_prefix
=name_prefix
[2], name
=b
"common", description
=b
"common regions in both conditions", score_column
="score")