示例#1
0
def weblogoPOIM(logofile, poim, max_len):
    """instead of plotting the POIM heatmap, create a weblogo from the 1st-degree poim"""
    warnings.filterwarnings('ignore', ' This call to matplotlib.use()*')
    from  corebio.data import rna_letters, dna_letters, amino_acid_letters
    from weblogolib import LogoData, LogoOptions, LogoFormat, classic, png_print_formatter
    #print "WEBLOGO!"
    #print "Writing ", logofile
    #print poim[0]
    positive_logo = []
    negative_logo = []
    for i in xrange(len(poim[0])):
        positive_logo.append([])
        negative_logo.append([])
        for j in xrange(len(poim[0][i])):
            if poim[0][i][j] < 0:
                positive_logo[i].append(0)
                negative_logo[i].append(poim[0][i][j] * -10000)
            else:
                negative_logo[i].append(0)
                positive_logo[i].append(poim[0][i][j] * 1000)
    #print "Positive logo: ", positive_logo
    #print "Negative logo: ", negative_logo
    pos_data = LogoData.from_counts('ACGT', numpy.array(positive_logo).T, None)
    neg_data = LogoData.from_counts("ACGT", numpy.array(negative_logo).T, None)
    neg_opt = LogoOptions()
    neg_opt.fineprint += " from KIRMES POIM data"
    #logoopt.number_interval = 5
    neg_opt.small_fontsize = 4 
    neg_opt.title_fontsize = 8
    neg_opt.scale_width = False
    title = os.path.split(logofile)[1]
    title = title[:title.rfind(".")]
    if "_" in title:
        title = title[title.rfind("_") + 1:]
    neg_opt.logo_title = title + " Negative Logo"
    neg_format = LogoFormat(neg_data, neg_opt)
    pos_opt = LogoOptions()
    #pos_opt.show_ends = True
    pos_opt.scale_width = False
    pos_opt.logo_title = title + " Positive Sequence Logo"
    pos_opt.show_fineprint = False
    pos_opt.color_scheme = classic
    pos_format = LogoFormat(pos_data, pos_opt)
    neg_logo = open(logofile + "n.png", 'w')
    png_print_formatter(neg_data, neg_format, neg_logo)
    neg_logo.close()
    pos_logo = open(logofile + "p.png", 'w')
    png_print_formatter(pos_data, pos_format, pos_logo)
    pos_logo.close()
    concatPNG(logofile, (logofile + "p.png", logofile + "n.png"))
    os.remove(logofile + "n.png")
    os.remove(logofile + "p.png")
示例#2
0
def _build_logodata(options):

    if options.input_parser != "transfac":
        seqs = read_seq_data(options.fin,
                             options.input_parser.read,
                             alphabet=options.alphabet,
                             ignore_lower_case=options.ignore_lower_case)

        if options.reverse:
            seqs = SeqList([s.reverse() for s in seqs], seqs.alphabet)

        if options.complement:
            seqs = SeqList([Seq(s, seqs.alphabet).complement() for s in seqs],
                           seqs.alphabet)

        prior = parse_prior(options.composition, seqs.alphabet, options.weight)
        data = LogoData.from_seqs(seqs, prior)

    else:
        from corebio.matrix import Motif

        if options.ignore_lower_case:
            raise ValueError(
                "error: option --ignore-lower-case incompatible with matrix input"
            )

        #FIXME : implement
        if options.reverse:
            raise ValueError(
                "error: option --reverse incompatible with matrix input")

        #FIXME : implement
        if options.complement:
            raise ValueError(
                "error: option --complement incompatible with matrix input")

        motif = Motif.read_transfac(options.fin, alphabet=options.alphabet)
        prior = parse_prior(options.composition, motif.alphabet,
                            options.weight)
        data = LogoData.from_counts(motif.alphabet, motif, prior)

    return data
示例#3
0
    def write_weblogo(self, filepath):
        matrix_tuple = []
        for distribution in self.values:
            matrix_tuple.append(tuple(distribution))

        dataArray = np.array(tuple(matrix_tuple))

        alph = Alphabet(''.join(self.alphabet))

        weblogoData = LogoData.from_counts(alph, dataArray)
        weblogoOptions = LogoOptions(color_scheme=classic)
        weblogoOptions.title = "PWM"
        weblogoFormat = LogoFormat(weblogoData, weblogoOptions)
        weblogo_file = open(filepath, 'w')
        weblogo_file.write(png_print_formatter(weblogoData, weblogoFormat))
        weblogo_file.close()
示例#4
0
def outputMotif(theta_motif, theta_background_matrix, lambda_motif, logev, k, outstr):
    from weblogolib import LogoData, LogoOptions, LogoFormat, png_formatter, eps_formatter, unambiguous_dna_alphabet
    _pv_format = "%3.1fe%+04.0f"
    f_string = sprint_logx(log(lambda_motif), 1, _pv_format)
    g_string = sprint_logx(logev, 1, _pv_format)
    print(("Motif {0:s} had a fraction of {1:s}").format(str(k), f_string))
    print(("Motif {0:s} had an E-value of {1:s}").format(str(k), g_string))
    print 'Saving motif as a png...'
    data = LogoData.from_counts(counts=theta_motif,alphabet=unambiguous_dna_alphabet)#,prior=theta_background_matrix[0])#Does prior mess things up?
    options = LogoOptions()
    options.title = 'Motif'
    forma = LogoFormat(data, options)
    fout = open(outstr + "Motif_" + str(k) + '.png', 'w')
    png_formatter(data, forma, fout)
    fout.close()
    print 'Saving motif as an eps...'
    fout = open(outstr + "Motif_" + str(k) + '.eps', 'w')
    eps_formatter(data, forma, fout)
    fout.close()
示例#5
0
def outputMotif(theta_motif, theta_background_matrix, lambda_motif, logev, k, outstr):
    from weblogolib import LogoData, LogoOptions, LogoFormat, png_formatter, eps_formatter, unambiguous_dna_alphabet
    _pv_format = "%3.1fe%+04.0f"
    f_string = sprint_logx(log(lambda_motif), 1, _pv_format)
    g_string = sprint_logx(logev, 1, _pv_format)
    print(("Motif {0:s} had a fraction of {1:s}").format(str(k), f_string))
    print(("Motif {0:s} had an E-value of {1:s}").format(str(k), g_string))
    print 'Saving motif as a png...'
    data = LogoData.from_counts(counts=theta_motif,alphabet=unambiguous_dna_alphabet)#,prior=theta_background_matrix[0])#Does prior mess things up?
    options = LogoOptions()
    options.title = 'Motif'
    forma = LogoFormat(data, options)
    fout = open(outstr + "Motif_" + str(k) + '.png', 'w')
    png_formatter(data, forma, fout)
    fout.close()
    print 'Saving motif as an eps...'
    fout = open(outstr + "Motif_" + str(k) + '.eps', 'w')
    eps_formatter(data, forma, fout)
    fout.close()
示例#6
0
def createSeqLogo(pfm, filename, fformat='eps'):
    """Create sequence logo for an individual cRBM motif.

    Parameters
    -----------
    pfm : numpy-array
        2D numpy array representing a PFM. See :meth:`CRBM.getPFMs`
    path : str
        Output folder.
    fformat : str
        File format for storing the sequence logos. Default: 'eps'.
    """
    alph = Alphabet('ACGT')
    weblogoData = LogoData.from_counts(
        alph, pfm.T)  #, c)#, learner.c.get_value().reshape(-1))
    weblogoOptions = LogoOptions(color_scheme=classic)
    weblogoFormat = LogoFormat(weblogoData, weblogoOptions)
    content = formatters[fformat](weblogoData, weblogoFormat)
    f = open(filename, "wb")
    f.write(content)
    f.close()
示例#7
0
文件: _cli.py 项目: cran/RWebLogo
        motif_flag = True
    except ValueError, motif_err :
        # Failed reading Motif, try reading as multiple sequence data.
        seqs = read_seq_data(fin, 
            options.input_parser.read,
            alphabet=options.alphabet,
            ignore_lower_case = options.ignore_lower_case)   

    if motif_flag :
        if options.ignore_lower_case:
            raise ValueError("error: option --ignore-lower-case incompatible with matrix input")
        if options.reverse: motif.reverse()
        if options.complement: motif.complement()

        prior = parse_prior( options.composition,motif.alphabet, options.weight)
        data = LogoData.from_counts(motif.alphabet, motif, prior)
    else :
        if options.reverse: 
            seqs = SeqList([s.reverse() for s in seqs], seqs.alphabet)
        
        if options.complement :
            seqs= SeqList( [Seq(s,seqs.alphabet).complement() for s in seqs], seqs.alphabet)

        prior = parse_prior( options.composition,seqs.alphabet, options.weight)
        data = LogoData.from_seqs(seqs, prior)




    return data
     
示例#8
0
文件: _cli.py 项目: snehamitra/NPLB
        seqs = read_seq_data(fin,
                             options.input_parser.read,
                             alphabet=options.alphabet,
                             ignore_lower_case=options.ignore_lower_case)

    if motif_flag:
        if options.ignore_lower_case:
            raise ValueError(
                "error: option --ignore-lower-case incompatible with matrix input"
            )
        if options.reverse: motif.reverse()
        if options.complement: motif.complement()

        prior = parse_prior(options.composition, motif.alphabet,
                            options.weight)
        data = LogoData.from_counts(motif.alphabet, motif, prior)
    else:
        if options.reverse:
            seqs = SeqList([s.reverse() for s in seqs], seqs.alphabet)

        if options.complement:
            seqs = SeqList([Seq(s, seqs.alphabet).complement() for s in seqs],
                           seqs.alphabet)

        prior = parse_prior(options.composition, seqs.alphabet, options.weight)
        data = LogoData.from_seqs(seqs, prior)

    return data


def _build_logoformat(logodata, opts):
示例#9
0
文件: _cli.py 项目: jnktsj/heatlogo
            options.alphabet = None
        seqs = read_seq_data(
            fin, options.input_parser.read, alphabet=options.alphabet, ignore_lower_case=options.ignore_lower_case
        )

    if motif_flag:
        if options.ignore_lower_case:
            raise ValueError("option --ignore-lower-case incompatible with matrix input")
        if options.reverse:
            motif.reverse()
        if options.complement:
            motif.complement()

        if not isCodon:
            prior, compos = parse_prior(fin_compos, motif.alphabet, fin_weight)
            data = LogoData.from_counts(motif.alphabet, motif, options.stats_func, prior, compos, second_data)
        else:
            raise ValueError("option --sequence-type 'codon' incompatible with matrix input")
    else:
        if options.codon_frame < 0 and isCodon:
            options.reverse = True
            options.complement = True

        if options.reverse:
            seqs = SeqList([s.reverse() for s in seqs], seqs.alphabet)

        if options.complement:
            seqs = SeqList([Seq(s, seqs.alphabet).complement() for s in seqs], seqs.alphabet)

        if isCodon:
            if abs(options.codon_frame) > 1: