1 # Time-stamp: <2019-09-20 11:59:26 taoliu>
3 """Description: predict fragment size.
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 # ------------------------------------
18 # ------------------------------------
20 # ------------------------------------
21 from MACS2
.OptValidator
import opt_validate_predictd
as opt_validate
22 from MACS2
.PeakModel
import PeakModel
,NotEnoughPairsException
23 from MACS2
.Prob
import binomial_cdf_inv
24 from MACS2
.OutputWriter
import model2r_script
25 from MACS2
.Constants
import *
26 # ------------------------------------
28 # ------------------------------------
30 """The Main function/pipeline for duplication filter.
34 options
= opt_validate( o_options
)
35 # end of parsing commandline options
41 assert options
.format
!= 'BAMPE', "Pair-end data with BAMPE option doesn't work with predictd command. You can pretend your data to be single-end with -f BAM. Please try again!"
44 info("# read alignment files...")
45 treat
= load_tag_files_options (options
)
47 info("# tag size = %d", options
.tsize
)
50 info("# total tags in alignment file: %d", t0
)
53 info("# Build Peak Model...")
56 peakmodel
= PeakModel(treatment
= treat
,
57 max_pairnum
= MAX_PAIRNUM
,
61 debug("# Summary Model:")
62 debug("# min_tags: %d" % (peakmodel
.min_tags
))
63 debug("# d: %d" % (peakmodel
.d
))
64 info("# predicted fragment length is %d bps" % peakmodel
.d
)
65 info("# alternative fragment length(s) may be %s bps" % ','.join(map(str,peakmodel
.alternative_d
)))
66 info("# Generate R script for model : %s" % (options
.modelR
))
67 model2r_script(peakmodel
,options
.modelR
, options
.rfile
)
68 options
.d
= peakmodel
.d
70 except NotEnoughPairsException
:
71 warn("# Can't find enough pairs of symmetric peaks to build model!")
73 def load_tag_files_options ( options
):
74 """From the options, load alignment tags.
77 options
.info("# read treatment tags...")
78 tp
= options
.parser(options
.ifile
[0], buffer_size
=options
.buffer_size
)
79 if not options
.tsize
: # override tsize if user specified --tsize
81 options
.tsize
= ttsize
82 treat
= tp
.build_fwtrack()
84 if len(options
.ifile
) > 1:
86 for tfile
in options
.ifile
[1:]:
87 tp
= options
.parser(tfile
, buffer_size
=options
.buffer_size
)
88 treat
= tp
.append_fwtrack( treat
)
92 options
.info("tag size is determined as %d bps" % options
.tsize
)