1 # Time-stamp: <2019-09-25 10:04:37 taoliu>
3 """Description: compare bdg files
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
14 from MACS2
.IO
import BedGraphIO
15 from MACS2
.OptValidator
import opt_validate_bdgcmp
as opt_validate
17 from math
import log
as mlog
19 # ------------------------------------
21 # ------------------------------------
22 logging
.basicConfig(level
=20,
23 format
='%(levelname)-5s @ %(asctime)s: %(message)s ',
24 datefmt
='%a, %d %b %Y %H:%M:%S',
29 # ------------------------------------
31 # ------------------------------------
32 error
= logging
.critical
# function alias
33 warn
= logging
.warning
36 # ------------------------------------
38 # ------------------------------------
41 options
= opt_validate( options
)
42 scaling_factor
= options
.sfactor
43 pseudo_depth
= 1.0/scaling_factor
# not an actual depth, but its reciprocal, a trick to override SPMR while necessary.
45 info("Read and build treatment bedGraph...")
46 tbio
= BedGraphIO
.bedGraphIO(options
.tfile
)
47 tbtrack
= tbio
.build_bdgtrack()
49 info("Read and build control bedGraph...")
50 cbio
= BedGraphIO
.bedGraphIO(options
.cfile
)
51 cbtrack
= cbio
.build_bdgtrack()
53 info("Build scoreTrackII...")
54 sbtrack
= tbtrack
.make_scoreTrackII_for_macs( cbtrack
, depth1
= pseudo_depth
, depth2
= pseudo_depth
)
55 if abs(scaling_factor
-1) > 1e-6:
56 # Only for the case while your input is SPMR from MACS2 callpeak; Let's override SPMR.
57 info("Values in your input bedGraph files will be multiplied by %f ..." % scaling_factor
)
58 sbtrack
.change_normalization_method( ord('M') ) # a hack to override SPMR
59 sbtrack
.set_pseudocount( options
.pseudocount
)
61 already_processed_method_list
= []
62 for (i
, method
) in enumerate(options
.method
):
63 if method
in already_processed_method_list
:
66 already_processed_method_list
.append( method
)
68 info("Calculate scores comparing treatment and control by '%s'..." % method
)
70 ofile
= os
.path
.join( options
.outdir
, options
.ofile
[ i
] )
72 ofile
= os
.path
.join( options
.outdir
, options
.oprefix
+ "_" + method
+ ".bdg" )
75 sbtrack
.change_score_method( ord('p') )
76 elif method
== 'qpois':
77 sbtrack
.change_score_method( ord('q') )
78 elif method
== 'subtract':
79 sbtrack
.change_score_method( ord('d') )
80 elif method
== 'logFE':
81 sbtrack
.change_score_method( ord('f') )
83 sbtrack
.change_score_method( ord('F') )
84 elif method
== 'logLR': # log likelihood
85 sbtrack
.change_score_method( ord('l') )
86 elif method
== 'slogLR': # log likelihood
87 sbtrack
.change_score_method( ord('s') )
89 sbtrack
.change_score_method( ord('M') )
91 raise Exception("Can't reach here!")
93 info("Write bedGraph of scores...")
94 ofhd
= open(ofile
,"w")
95 sbtrack
.write_bedGraph(ofhd
,name
="%s_Scores" % (method
.upper()),description
="Scores calculated by %s" % (method
.upper()), column
= 3)
96 info("Finished '%s'! Please check '%s'!" % (method
, ofile
))