def getBetweenHeter(cool, chrom_heter_pairs, method='median'):
    method_func = np.median if method == 'median' else np.mean
    contact_data = []
    hm = cool2matrix(cool)
    for chrom_pair in chrom_heter_pairs:
        chrom1, chrom2 = chrom_pair
        chrom_hm = getChromPairsMatrix(cool, hm, chrom1, chrom2)
        contact_data.extend(method_func(chrom_hm, axis=1))
    return contact_data
def getBetweenHomo(cool, chrom_homo_pairs, method='median'):
    method_func = np.median if method == 'median' else np.mean
    contact_data = []
    hm = cool2matrix(cool)
    for homo_chroms in chrom_homo_pairs:
        chrom_pairs = list(combinations(homo_chroms, 2))
        for chrom_pair in chrom_pairs:
            chrom1, chrom2 = chrom_pair
            chrom_hm = getChromPairsMatrix(cool, hm, chrom1, chrom2)
            contact_data.extend((method_func(chrom_hm, axis=1)))

    return contact_data
def getWithinChromosome(cool, method='median'):
    method_func = np.median if method == 'median' else np.mean
    chroms = cool.chromnames
    hm = cool2matrix(cool)
    contact_data = []
    for chrom in chroms:
        idx = cool.bins().fetch(chrom).index
        start = idx[0]
        end = idx[-1]
        contact_mean_data = method_func(hm[start:end + 1, start:end + 1],
                                        axis=1)
        contact_data.extend(contact_mean_data)
    return contact_data
def main(args):
    p = p = argparse.ArgumentParser(prog=__file__,
                                    description=__doc__,
                                    conflict_handler='resolve')
    pReq = p.add_argument_group('Required arguments')
    pOpt = p.add_argument_group('Optional arguments')
    pReq.add_argument('-m',
                      '--matrix',
                      required=True,
                      help='contact matrix with cool formats')
    pOpt.add_argument('-o', '--output', help='output of picture')
    pOpt.add_argument(
        '--method',
        choices=['average', 'median'],
        default='median',
        help='method of interaction frequency per bin [default: %(default)s]')
    pOpt.add_argument('-h',
                      '--help',
                      action='help',
                      help='show help message and exit.')

    args = p.parse_args(args)

    output = args.matrix.rsplit('.', 1)[0] + '_TransInteractionWithinHomo_boxplot.pdf' \
                if not args.output else args.output
    method_func = np.median if args.method == 'median' else np.mean
    cool = cooler.Cooler(args.matrix)
    resolution_string = chrom_size_convert(cool.binsize)
    all_chroms = cool.chromnames
    chrom_heter_pairs = list(
        filter(is_heter, list(combinations(all_chroms, 2))))
    homo_chroms = sorted(set(map(lambda x: x[:-1], cool.chromnames)))

    chrom_homo_pairs = find_homo_chrom(homo_chroms, all_chroms)

    hm = cool2matrix(cool)

    boxprops = dict(color='black', linewidth=1.5)
    medianprops = dict(color='black', linewidth=2.5)
    whiskerprops = dict(linestyle='--')
    fig, axes = plt.subplots(2, 4, figsize=(8, 4.5), sharey=True)
    plt.subplots_adjust(hspace=0.45)
    axes_list = [ax for ax_row in axes for ax in ax_row]
    data = []
    for i, chroms in enumerate(chrom_homo_pairs):
        chr_data = []
        chrom_pairs = list(permutations(chroms, 2))
        homo_data = []

        for chrom in chroms:
            haps = (list(filter(lambda x: x[0] == chrom, chrom_pairs)))
            haps_data = []
            for chrom_pair in haps:
                chrom1, chrom2 = chrom_pair
                chrom_matrix = getChromPairsMatrix(cool, hm, chrom1, chrom2)
                chrom_matrix.sort(axis=1)
                haps_data.extend(method_func(chrom_matrix, axis=1))

            homo_data.append(haps_data)
        # homo_data.append(getChromPairsMatrix(cool, hm, chrom_pair[0], chrom_pair[1]))
        ax = axes_list[i]
        bplot = ax.boxplot(homo_data,
                           showfliers=False,
                           patch_artist=True,
                           notch=True,
                           widths=0.35,
                           medianprops=medianprops,
                           whiskerprops=whiskerprops,
                           boxprops=boxprops)
        for patch, color in zip(bplot['boxes'],
                                ['#a83836', '#275e8c', '#df8384', '#8dc0ed']):
            patch.set_facecolor(color)
        ax.set_xticklabels(chroms, rotation=45)
    fig.text(
        0.02,
        0.5,
        "Interaction Frequency\n(contacts / {})".format(resolution_string),
        rotation=90,
        va='center')

    plt.savefig(output, dpi=300, bbox_inches='tight')
    plt.savefig(output.rsplit('.', 1)[0] + '.png',
                dpi=300,
                bbox_inches='tight')
Beispiel #5
0
def plotCisTrans(args):
    """
    %(prog)s <coolfile> [coolfile ...] [Options]

        calculate the cis and trans interaction, and plot the barplot.
    
    """
    p = argparse.ArgumentParser(prog=plotCisTrans.__name__,
                        description=plotCisTrans.__doc__,
                        conflict_handler='resolve')
    pReq = p.add_argument_group('Required arguments')
    pOpt = p.add_argument_group('Optional arguments')
    pReq.add_argument('cool', nargs='+', 
            help='cool file of hicmatrix')
    pOpt.add_argument('--sample', nargs='+',
            help='sample name of each cool file [default: coolprefix]', )
    pOpt.add_argument('--perchr', action='store_true', default=False,
            help='plot cis/trans ration per chromosome [default: %(default)]')
    pOpt.add_argument('--hideLegend', action='store_true', default=False, 
            help='hide legend label [default: %(default)s]')
    pOpt.add_argument('--colors', nargs="*", default=None,
            help='colors of bar [default: %(default)s]')
    pOpt.add_argument('-o', '--outprefix', default='out',
            help='out prefix of output picture and result [default: %(default)]')
    pOpt.add_argument('-h', '--help', action='help',
            help='show help message and exit.')
    
    args = p.parse_args(args)
    outprefix = args.outprefix
    cis_list = []
    trans_list = []
    for coolfile in args.cool:
        cool = cooler.Cooler(coolfile)
        hm = cool2matrix(cool)
        # calculate cis counts
        counts = np.zeros(cool.info['nbins'])
        for chrom in cool.chromnames:
            idx = cool.bins().fetch(chrom).index
            counts[idx] = np.triu(hm[idx][:, idx]).sum(axis=1)
        cis_list.append(int(counts.sum()))

        ## calculate trans counts
        counts = np.zeros(cool.info['nbins'])
        for chrom in cool.chromnames:
            idx = cool.bins().fetch(chrom).index
            start = idx[0]
            end = idx[-1]
            hm[start: end + 1, start: end + 1] = 0
            counts[idx] = hm[idx].sum(axis=1)
        counts = counts / 2
        trans_list.append(int(counts.sum()))
        #print(counts.sum())
    if len(cis_list) == 1 and len(trans_list) == 1:
        cis, trans = cis_list[0], trans_list[0]
        fig, ax = plt.subplots(figsize=(2.8, 4))
        ax.bar([1.1, 2], [cis, trans], width=.6, align='center',
            color=['#ffa040', '#55a0fb'], edgecolor='#606060',
            linewidth=2)
        ax.set_xticks([1, 2])
        ax.set_xlim(0.5, 2.5)
        ax.set_ylim(0, max([cis, trans]) * 1.2)
        ax.set_xticklabels(['${cis}$', '${trans}$'], fontsize=14)
        ax.set_ylabel('Contact probability', fontsize=15)
        ax.tick_params(axis='both', which='major', labelsize=14,)
        ax.tick_params(axis='x', which='major', width=0)
        ax.tick_params(axis='y', which='major', width=2, length=5)
        ax.spines['left'].set_linewidth(2)
        ax.spines['bottom'].set_linewidth(0)
        ax.ticklabel_format(axis='y', style='sci', scilimits=(0, 0), useMathText=True)
        sns.despine()
        outpdf = outprefix + "_cis_trans.pdf"
        outpng = outprefix + "_cis_trans.png"
        plt.savefig(outpdf, bbox_inches='tight', dpi=300)
        plt.savefig(outpng, bbox_inches='tight', dpi=300)
        logging.debug("Successful plotting total cis trans barplot in `{}`".format(outpdf))
    output = outprefix + "_cis_trans.tsv"
    ## output total cis and trans counts 
    with open(output, 'w') as out:
        print("Cis", end='\t', file=out)
        print('\t'.join(map(str, cis_list)), file=out)
        print("Trans", end='\t', file=out)
        print('\t'.join(map(str, trans_list)), file=out)
        logging.debug("Output total cis and trans count to `{}`".format(output))
    plt.close() ## close plt
    ## plotting per chromosome cis/trans ratio barplot
    if args.perchr:
        if not args.sample:
            samples = list(map(lambda x: x.split(".")[0], args.cool))
        else:
            samples = args.sample
            if len(samples) != len(args.cool):
                logging.error('cool must equal sample')
        legend = False if args.hideLegend else True
        colors = args.colors if args.colors else None
        data = concatCisTransData(args.cool, samples)
        outtsv = outprefix + "_cis_trans_ratio_perchr.tsv"
        data.to_csv(outtsv, sep='\t', header=None, index=None)
        logging.debug("Successful outputting cis/trans ratio data to `{}`".format(outtsv))
        ax = plotCisTransPerChrom(data, legend=legend)
        outpdf = outprefix + "_cis_trans_ratio_perchr.pdf"
        outpng = outprefix + "_cis_trans_ratio_perchr.png"
        plt.savefig(outpdf, bbox_inches='tight', dpi=300)
        plt.savefig(outpng, bbox_inches='tight', dpi=300)
        logging.debug("Successful plotting cis/trans ratio per chrom barplot in `{}`".format(outpdf))