peakbed = filefun.Bedfile(bedfile[:, 0], bedfile[:, 1], bedfile[:, 2], bedfile[:,3])

# save to this file
if not options.o:
    basename = os.path.splitext(os.path.basename(options.b))[0]
else: basename = options.o

"""
Now find 'significantly' up peaks and 'significantly' down peaks
"""
peaks.score = peaks.findScore(parameters.indices_low, parameters.indices_high)
peaks.nullscore = peaks.score[noExprChange]
peaks.upinNfib, peaks.downinNfib = peaks.divideIntoTwo(parameters.indices_low, parameters.indices_high)

# get FDR value for log change in accessibility
peaks.qvalue = seqfun.getFDR(peaks.score, peaks.nullscore)
peaks.ttest_pvalue = np.array([st.ttest_ind(peaks.peakCount[i, parameters.indices_high], peaks.peakCount[i, parameters.indices_low])[1] for i in range(peaks.peakCount.shape[0])])

# make criteria
criteriaUp = np.all((peaks.qvalue < 0.01, peaks.score > 0, peaks.ttest_pvalue < 0.01), axis=0)
criteriaDown = np.all((peaks.qvalue < 0.01, peaks.score < 0, peaks.ttest_pvalue < 0.01), axis=0)
criteriaBackground = np.logical_not(np.any((criteriaUp, criteriaDown), 0))

# print
print "number of peaks significantly up with Nfib: %d"%np.sum(criteriaUp)
print "number of peaks significantly down with Nfib: %d"%np.sum(criteriaDown)
print "other peaks: %d"%np.sum(criteriaBackground)

peakbed.strand = np.array(['.']*peaks.numPeaks)
bedFile = np.transpose([peakbed.chr, peakbed.start, peakbed.end, peakbed.name, peakbed.strand, peaks.qvalue, peaks.ttest_pvalue, peaks.downinNfib, peaks.upinNfib])
bed_file_name_high = '%s.upwNfib.bed'%basename
        # plot it if it is chr8
        if chrm=='chr8':
            plotAmplifiedRegion(windowedLocs, np.log2(foldChange/foldChangeRandom))
            plt.title(parameters.headers_noreplicates[sample])
            plt.tight_layout()
            plt.savefig('%s.enrichment_across_%s.hypervsnot.pdf'%(outfile, chrm))
    # and save        
    np.savetxt('%s.hypervsnot.enrichment_values.iterations_%d.mat'%(outfile, numIterations), logFoldChange, delimiter='\t')
    
    # save qvalue of enrichment vs depletion bed file
    qvaluesAll = np.empty(0)
    for chrm in chrms:
        for i, loc in enumerate(windowedLocsDict[chrm]):
            foldChange = np.mean(signalDensityDict[chrm][i, parameters.indices_high_noreplicates])/np.mean(signalDensityDict[chrm][i, parameters.indices_low_noreplicates])
            foldChangeRand = np.mean(signalDensityRandomDict[chrm][:, i, parameters.indices_high_noreplicates], axis=1)/np.mean(signalDensityRandomDict[chrm][:, i, parameters.indices_low_noreplicates], axis=1)
            q = seqfun.getFDR([np.log2(foldChange)], np.log2(foldChangeRand))
            qvaluesAll = np.append(qvaluesAll, q)
    # and save bed file
    windowedLocBed = filefun.loadBed('%s.windowed_locs.bed'%outfile)
    windowedLocBed.saveBed('tmp.bed', indx=np.all((qvaluesAll < 0.05, logFoldChange > 0), axis=0))
    os.system('bedtools merge -i tmp.bed > %s'%('%s.windowed_locs.hypervsnot.enriched.bed'%outfile))
    
    windowedLocBed.saveBed('tmp.bed', indx=np.all((qvaluesAll < 0.05, logFoldChange < 0), axis=0))
    os.system('bedtools merge -i tmp.bed > %s'%('%s.windowed_locs.hypervsnot.depleted.bed'%outfile))
except IndexError:
    print 'did not save hypraccesible vs not bigwig file'


# save a couple example traces
chrm='chr8'
plotAmplifiedRegion(windowedLocsDict[chrm], enrichmentDict[chrm][:,0])