def weblogo_from_dist(dist, filename='logo.eps'): """Generate a weblogo from a pssm distribution""" import weblogolib data = weblogo_data_from_dist(dist) options = weblogolib.LogoOptions() options.size = weblogolib.LogoSize(stack_width=5.4 * 12, stack_height=5.4 * 12 * 5) options.color_scheme = weblogolib.std_color_schemes["classic"] format = weblogolib.LogoFormat(data, options) weblogolib.eps_formatter(data, format, open(filename, 'w'))
def create_logo(self, seqs=[]): """Create sequence logo for input sequences.""" # seperate headers headers, instances = [list(x) for x in zip(*seqs)] if self.options.sequence_type is 'rna': alphabet = Alphabet('ACGU') elif self.options.sequence_type is 'protein': alphabet = Alphabet('ACDEFGHIKLMNPQRSTVWY') else: alphabet = Alphabet('AGCT') motif_corebio = SeqList(alist=instances, alphabet=alphabet) data = wbl.LogoData().from_seqs(motif_corebio) format = wbl.LogoFormat(data, self.options) if self.output_format == 'png': return wbl.png_formatter(data, format) elif self.output_format == 'png_print': return wbl.png_print_formatter(data, format) elif self.output_format == 'jpeg': return wbl.jpeg_formatter(data, format) else: return wbl.eps_formatter(data, format)
def generate_logo(seqfile, title): ''' Generate the sequence logo from the specified sequences. Args: seqfile (str): The path to a sequence file. ''' with open(seqfile, 'r') as fh: seqlen = len(fh.readline().rstrip('\n')) fh.seek(0) seqs = wl.read_seq_data(fh) data = wl.LogoData.from_seqs(seqs) options = wl.LogoOptions() options.title = title options.fineprint = '' #options.stack_width = 16 options.first_index = -1 * int(seqlen / 2) form = wl.LogoFormat(data, options) eps = wl.eps_formatter(data, form) eps_file = seqfile[:-4] + '.eps' with open(eps_file, 'wb') as fh: fh.write(eps)
def weblogo_from_dist( dist, filename = 'logo.eps' ): """Generate a weblogo from a pssm distribution""" import weblogolib data = weblogo_data_from_dist( dist ) options = weblogolib.LogoOptions() options.size = weblogolib.LogoSize( stack_width = 5.4*12, stack_height = 5.4*12*5 ) options.color_scheme = weblogolib.std_color_schemes[ "classic" ] #format = weblogolib.LogoFormat( data, options ) weblogolib.eps_formatter( data, #format, open( filename, 'w' ) )
def make_pdf_weblogo( self, pdf_path, prior=None ): # prior could also be = weblogolib.parse_prior('equiprobable', corebio.seq.unambiguous_protein_alphabet) ) assert (pdf_path.endswith('.pdf')) assert (self.design_positions != None) assert (self.starting_seq != None) pdf_basedir = os.path.dirname(pdf_path) if not os.path.isdir(pdf_basedir): os.makedirs(pdf_basedir) eps_logo_filename = pdf_path[:-4] + '.eps' data = weblogolib.LogoData.from_seqs(self.raw_seqs, prior=prior) options = weblogolib.LogoOptions() options.show_fineprint = False options.xaxis_tic_interval = 1 options.number_interval = 1 options.number_fontsize = 3 options.stacks_per_line = 40 options.show_errorbars = False logo_format = weblogolib.LogoFormat(data, options) eps_binary = weblogolib.eps_formatter(data, logo_format) eps_str = eps_binary.decode() eps_str = replace_logo_numbers(eps_str, self.design_positions, self.starting_seq) with open(eps_logo_filename, 'w') as f: f.write(eps_str) eps_to_pdf(eps_logo_filename)
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()
def generate_weblogo(seq_id, seq_strs): seqs = weblogolib.read_seq_data(io.StringIO('\n'.join(seq_strs)), input_parser=array_io.read) data = weblogolib.LogoData.from_seqs(seqs) options = weblogolib.LogoOptions() options.title = seq_id options.unit_name = "probability" format = weblogolib.LogoFormat(data, options) eps = weblogolib.eps_formatter(data, format) with open(os.path.join(WEBLOGO_PATH, seq_id + '.eps'), 'wb') as f: f.write(eps)
def logo(dist, tag, make_png=False, make_eps=True, write_title=True): "Generate a logo with the given tag in the given directory." import weblogolib as W import corebio.seq data = W.LogoData.from_counts(corebio.seq.unambiguous_dna_alphabet, dist) scale = 5.4 * 4 options = W.LogoOptions( logo_title=write_title and tag or None, stack_width=scale, stack_aspect_ratio=5, color_scheme=W.colorscheme.nucleotide, show_xaxis=False, show_yaxis=False, show_fineprint=False, ) format_ = W.LogoFormat(data, options) filename = 'logo-%s' % tag if make_eps: W.eps_formatter(data, format_, open('%s.eps' % filename, 'w')) if make_png: W.png_formatter(data, format_, open('%s.png' % filename, 'w'))
def generate_weblogo(file_path, seq_strs, unit_name=None): seqs = weblogolib.read_seq_data( io.StringIO('\n'.join(seq_strs)), input_parser=array_io.read, alphabet=corebio_seq.reduced_protein_alphabet) data = weblogolib.LogoData.from_seqs(seqs) options = weblogolib.LogoOptions(color_scheme=get_color_scheme()) if unit_name is not None: options.unit_name = unit_name format = weblogolib.LogoFormat(data, options) eps = weblogolib.eps_formatter(data, format) with open(file_path, 'wb') as f: f.write(eps)
def logo(dist, tag, make_png=True, make_eps=False, write_title=True): "Generate a logo named with the given tag." import corebio.seq import weblogolib as W if tuple(map(int, W.__version__.split('.'))) < (3, 4): raise ValueError('weblogolib version 3.4 or higher required') data = W.LogoData.from_counts(corebio.seq.unambiguous_dna_alphabet, dist) scale = 5.4 * 4 options = W.LogoOptions( logo_title=write_title and tag or None, stack_width=scale, stack_aspect_ratio=5, color_scheme=W.colorscheme.nucleotide, show_xaxis=False, show_yaxis=False, show_fineprint=False, ) format_ = W.LogoFormat(data, options) filename = 'logo-%s' % tag if make_eps: open('%s.eps' % filename, 'w').write(W.eps_formatter(data, format_)) if make_png: open('%s.png' % filename, 'w').write(W.png_formatter(data, format_))