def run( options ): options = opt_validate( options ) scaling_factor = options.sfactor pseudo_depth = 1.0/scaling_factor # not an actual depth, but its reciprocal, a trick to override SPMR while necessary. info("Read and build treatment bedGraph...") tbio = BedGraphIO.bedGraphIO(options.tfile) tbtrack = tbio.build_bdgtrack() info("Read and build control bedGraph...") cbio = BedGraphIO.bedGraphIO(options.cfile) cbtrack = cbio.build_bdgtrack() info("Build scoreTrackII...") 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 MACS2 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 ) 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 ) info("Calculate scores comparing treatment and control by '%s'..." % method) if options.ofile: ofile = os.path.join( options.outdir, options.ofile[ i ] ) else: ofile = os.path.join( options.outdir, options.oprefix + "_" + method + ".bdg" ) # build score track if method == 'ppois': sbtrack.change_score_method( ord('p') ) elif method == 'qpois': sbtrack.change_score_method( ord('q') ) elif method == 'subtract': sbtrack.change_score_method( ord('d') ) elif method == 'logFE': sbtrack.change_score_method( ord('f') ) elif method == 'FE': sbtrack.change_score_method( ord('F') ) elif method == 'logLR': # log likelihood sbtrack.change_score_method( ord('l') ) elif method == 'slogLR': # log likelihood sbtrack.change_score_method( ord('s') ) elif method == 'max': sbtrack.change_score_method( ord('M') ) else: raise Exception("Can't reach here!") info("Write bedGraph of scores...") ofhd = open(ofile,"wb") 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))
def run( options ): options = opt_validate( options ) scaling_factor = options.sfactor pseudo_depth = 1.0/scaling_factor # not an actual depth, but its reciprocal, a trick to override SPMR while necessary. info("Read and build treatment bedGraph...") tbio = BedGraphIO.bedGraphIO(options.tfile) tbtrack = tbio.build_bdgtrack() info("Read and build control bedGraph...") cbio = BedGraphIO.bedGraphIO(options.cfile) cbtrack = cbio.build_bdgtrack() info("Build scoreTrackII...") 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 MACS2 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 ) 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 ) info("Calculate scores comparing treatment and control by '%s'..." % method) if options.ofile: ofile = os.path.join( options.outdir, options.ofile[ i ] ) else: ofile = os.path.join( options.outdir, options.oprefix + "_" + method + ".bdg" ) # build score track if method == 'ppois': sbtrack.change_score_method( ord('p') ) elif method == 'qpois': sbtrack.change_score_method( ord('q') ) elif method == 'subtract': sbtrack.change_score_method( ord('d') ) elif method == 'logFE': sbtrack.change_score_method( ord('f') ) elif method == 'FE': sbtrack.change_score_method( ord('F') ) elif method == 'logLR': # log likelihood sbtrack.change_score_method( ord('l') ) elif method == 'slogLR': # log likelihood sbtrack.change_score_method( ord('s') ) elif method == 'max': sbtrack.change_score_method( ord('M') ) else: raise Exception("Can't reach here!") info("Write bedGraph of scores...") ofhd = open(ofile,"w") 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))
def run(options): info("Read and build bedGraph...") bio = BedGraphIO.bedGraphIO(options.ifile) btrack = bio.build_bdgtrack(baseline_value=0) info("Call peaks from bedGraph...") #(peaks,bpeaks) = btrack.call_broadpeaks (lvl1_cutoff=options.cutoffpeak, lvl2_cutoff=options.cutofflink, min_length=options.minlen, lvl1_max_gap=options.lvl1maxgap, lvl2_max_gap=options.lvl2maxgap) bpeaks = btrack.call_broadpeaks(lvl1_cutoff=options.cutoffpeak, lvl2_cutoff=options.cutofflink, min_length=options.minlen, lvl1_max_gap=options.lvl1maxgap, lvl2_max_gap=options.lvl2maxgap) info("Write peaks...") #nf = open ("%s_c%.1f_l%d_g%d_peaks.encodePeak" % (options.oprefix,options.cutoffpeak,options.minlen,options.lvl1maxgap),"w") if options.ofile: 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") bpeaks[1].write_to_gappedPeak(bf, name_prefix=options.oprefix + "_broadRegion") info("Done")
def run( options ): options = opt_validate( options ) info("Read and build bedGraph...") bio = BedGraphIO.bedGraphIO(options.ifile) btrack = bio.build_bdgtrack(baseline_value=0) info("Modify bedGraph...") if options.method.lower() == "p2q": btrack.p2q() else: extraparam = float(options.extraparam[0]) if options.method.lower() == "multiply": btrack.apply_func( lambda x: x * extraparam) elif options.method.lower() == "add": btrack.apply_func( lambda x: x + extraparam) elif options.method.lower() == "max": 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 ) ofile = os.path.join( options.outdir, options.ofile ) info("Write bedGraph of modified scores...") ofhd = open(ofile,"w") btrack.write_bedGraph(ofhd,name="%s_modified_scores" % (options.method.upper()),description="Scores calculated by %s" % (options.method.upper())) info("Finished '%s'! Please check '%s'!" % (options.method, ofile))
def run( options ): options = opt_validate( options ) info("Read and build bedGraph...") bio = BedGraphIO.bedGraphIO(options.ifile) btrack = bio.build_bdgtrack(baseline_value=0) info("Modify bedGraph...") if options.method.lower() == "p2q": btrack.p2q() elif options.method.lower() == "analen": btrack.analen() else: extraparam = float(options.extraparam[0]) if options.method.lower() == "multiply": btrack.apply_func( lambda x: x * extraparam) elif options.method.lower() == "add": btrack.apply_func( lambda x: x + extraparam) elif options.method.lower() == "max": 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 ) ofile = os.path.join( options.outdir, options.ofile ) info("Write bedGraph of modified scores...") ofhd = open(ofile,"wb") btrack.write_bedGraph(ofhd,name="%s_modified_scores" % (options.method.upper()),description="Scores calculated by %s" % (options.method.upper())) info("Finished '%s'! Please check '%s'!" % (options.method, ofile))
def run(options): options = opt_validate(options) #weights = options.weights info("Read and build bedGraph for each replicate...") reps = [] i = 1 for ifile in options.ifile: info("Read file #%d" % i) reps.append(BedGraphIO.bedGraphIO(ifile).build_bdgtrack()) i += 1 # first two reps info("combining tracks 1-%i with method '%s'" % (i - 1, options.method)) cmbtrack = reps[0].overlie([reps[j] for j in range(1, i - 1)], func=options.method) ofile = os.path.join(options.outdir, options.ofile) info("Write bedGraph of combined scores...") ofhd = open(ofile, "w") cmbtrack.write_bedGraph( ofhd, name="%s_combined_scores" % (options.method.upper()), description="Scores calculated by %s" % (options.method.upper())) info("Finished '%s'! Please check '%s'!" % (options.method, ofile))
def run( options ): info("Read and build bedGraph...") bio = BedGraphIO.bedGraphIO(options.ifile) btrack = bio.build_bdgtrack(baseline_value=0) if options.cutoff_analysis: info("Analyze cutoff vs number of peaks/total length of peaks/average length of peak") cutoff_analysis_result = btrack.cutoff_analysis( int(options.maxgap), int(options.minlen), 50 ) info("Write report...") if options.ofile: fhd = open( os.path.join( options.outdir, options.ofile ), 'w' ) else: fhd = open ( os.path.join( options.outdir, "%s_l%d_g%d_cutoff_analysis.txt" % (options.oprefix,options.minlen,options.maxgap)), "w" ) fhd.write( cutoff_analysis_result ) info("Done") else: info("Call peaks from bedGraph...") peaks = btrack.call_peaks(cutoff=float(options.cutoff),min_length=int(options.minlen),max_gap=int(options.maxgap),call_summits=options.call_summits) info("Write peaks...") if options.ofile: options.oprefix = options.ofile nf = open( os.path.join( options.outdir, options.ofile ), 'w' ) else: nf = open ( os.path.join( options.outdir, "%s_c%.1f_l%d_g%d_peaks.narrowPeak" % (options.oprefix,options.cutoff,options.minlen,options.maxgap)), "w" ) peaks.write_to_narrowPeak(nf, name=options.oprefix, name_prefix=options.oprefix+"_narrowPeak", score_column="score", trackline=options.trackline) info("Done")
def run( options ): info("Read and build bedGraph...") bio = BedGraphIO.bedGraphIO(options.ifile) btrack = bio.build_bdgtrack(baseline_value=0) info("Call peaks from bedGraph...") #(peaks,bpeaks) = btrack.call_broadpeaks (lvl1_cutoff=options.cutoffpeak, lvl2_cutoff=options.cutofflink, min_length=options.minlen, lvl1_max_gap=options.lvl1maxgap, lvl2_max_gap=options.lvl2maxgap) bpeaks = btrack.call_broadpeaks (lvl1_cutoff=options.cutoffpeak, lvl2_cutoff=options.cutofflink, min_length=options.minlen, lvl1_max_gap=options.lvl1maxgap, lvl2_max_gap=options.lvl2maxgap) info("Write peaks...") #nf = open ("%s_c%.1f_l%d_g%d_peaks.encodePeak" % (options.oprefix,options.cutoffpeak,options.minlen,options.lvl1maxgap),"w") if options.ofile: 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" ) bpeaks[1].write_to_gappedPeak(bf, name_prefix=options.oprefix+"_broadRegion") info("Done")
def run( options ): options = opt_validate( options ) #weights = options.weights info("Read and build bedGraph for each replicate...") reps = [] i = 1 for ifile in options.ifile: info("Read file #%d" % i) reps.append( BedGraphIO.bedGraphIO( ifile ).build_bdgtrack( ) ) i += 1 # first two reps info("combining #1 and #2 with method '%s'" % options.method) cmbtrack = reps[ 0 ].overlie( reps[ 1 ], func=options.method ) ofile = os.path.join( options.outdir, options.ofile ) info("Write bedGraph of combined scores...") ofhd = open(ofile,"wb") cmbtrack.write_bedGraph(ofhd,name="%s_combined_scores" % (options.method.upper()),description="Scores calculated by %s" % (options.method.upper())) info("Finished '%s'! Please check '%s'!" % (options.method, ofile))
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.build_bdgtrack() info("Read and build control 1 bedGraph...") c1bio = BedGraphIO.bedGraphIO(options.c1bdg) c1btrack = c1bio.build_bdgtrack() info("Read and build treatment 2 bedGraph...") t2bio = BedGraphIO.bedGraphIO(options.t2bdg) t2btrack = t2bio.build_bdgtrack() info("Read and build control 2 bedGraph...") c2bio = BedGraphIO.bedGraphIO(options.c2bdg) c2btrack = c2bio.build_bdgtrack() depth1 = options.depth1 depth2 = options.depth2 if depth1 > depth2: # scale down condition 1 to size of condition 2 depth1 = depth2 / depth1 depth2 = 1.0 elif depth1 < depth2: # scale down condition 2 to size of condition 1 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.build() twoconditionscore.finalize() (cat1, cat2, cat3) = twoconditionscore.call_peaks(min_length=options.minlen, max_gap=options.maxgap, cutoff=options.cutoff) info("Write peaks...") ofiles = [] name_prefix = [] if options.ofile: ofiles = map(lambda x: os.path.join(options.outdir, x), options.ofile) name_prefix = 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 = [ 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="condition 1", description="unique regions in condition 1", score_column="score") nf = open(ofiles[1], 'w') cat2.write_to_bed(nf, name_prefix=name_prefix[1], name="condition 2", description="unique regions in condition 2", score_column="score") nf = open(ofiles[2], 'w') cat3.write_to_bed(nf, name_prefix=name_prefix[2], name="common", description="common regions in both conditions", score_column="score") info("Done")
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.build_bdgtrack() info("Read and build control 1 bedGraph...") c1bio = BedGraphIO.bedGraphIO(options.c1bdg) c1btrack = c1bio.build_bdgtrack() info("Read and build treatment 2 bedGraph...") t2bio = BedGraphIO.bedGraphIO(options.t2bdg) t2btrack = t2bio.build_bdgtrack() info("Read and build control 2 bedGraph...") c2bio = BedGraphIO.bedGraphIO(options.c2bdg) c2btrack = c2bio.build_bdgtrack() depth1 = options.depth1 depth2 = options.depth2 if depth1 > depth2: # scale down condition 1 to size of condition 2 depth1 = depth2 / depth1 depth2 = 1.0 elif depth1 < depth2: # scale down condition 2 to size of condition 1 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.build() twoconditionscore.finalize() (cat1,cat2,cat3) = twoconditionscore.call_peaks(min_length=options.minlen, max_gap=options.maxgap, cutoff=options.cutoff) info("Write peaks...") ofiles = [] name_prefix = [] if options.ofile: ofiles = map( lambda x: os.path.join( options.outdir, x ), options.ofile ) name_prefix = 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 = [ 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="condition 1", description="unique regions in condition 1", score_column="score") nf = open( ofiles[ 1 ], 'w' ) cat2.write_to_bed(nf, name_prefix=name_prefix[ 1 ], name="condition 2", description="unique regions in condition 2", score_column="score") nf = open( ofiles[ 2 ], 'w' ) cat3.write_to_bed(nf, name_prefix=name_prefix[ 2 ], name="common", description="common regions in both conditions", score_column="score") info("Done")