def parse_pfam(infile, color=None, y=-2.5, size=8): entities = [] spans = [] with open(infile) as f: for l in f: if l.startswith('#'): continue elif not l.strip(): continue else: sl = l.strip().split() #label = l[181:].strip() #start = int(l[152:157].strip()) #end = int(l[158:163].strip()) #label = sl[-1] label = sl[1] start = int(sl[19]) end = int(sl[20]) dy = 0 for span in spans: if (span[0] <= start <= span[1]) or (span[0] <= end <= span[1]): dy = 0.3 entities.append( quod.Region([[start, end]], [y - 0.15 + dy, 0.15], label, style=color, size=size)) spans.append([start, end]) return entities
def parse_pfam(infile, styledict, color=None, y=-2.75, size=8): entities = [] spans = [] with open(infile) as f: for l in f: if l.startswith('#'): continue elif not l.strip(): continue else: sl = l.strip().split() #label = l[181:].strip() #start = int(l[152:157].strip()) #end = int(l[158:163].strip()) #label = sl[-1] domain = sl[1] #TODO: pass clandict around to speed up clan searches clan = get_clan(domain) style = 3 if clan.strip(): if clan in styledict: style = styledict[clan] else: if styledict: style = max(styledict.values()) + 1 style = 3 styledict[clan] = style elif domain in styledict: style = styledict[domain] else: if styledict: style = max(styledict.values()) + 1 else: style = 3 styledict[domain] = style #print(styledict) start = int(sl[19]) end = int(sl[20]) dy = 0 ystep = 0.7 for span in spans: if (span[0] <= start <= span[1]) or (span[0] <= end <= span[1]): dy += ystep entities.append( quod.Region([[start, end]], [y - 0.2 + dy, 0.2], domain, style=style, size=size)) #entities.append(quod.Region([[start, end]], [y-0.2+dy+ystep, 0.2], domain, style=style, size=size)) #entities.append(quod.Region([[start, end]], [y-0.2+dy+ystep*2, 0.2], domain, style=style, size=size)) #entities.append(quod.Region([[start, end]], [y-0.2+dy+ystep*3, 0.2], domain, style=style, size=size)) #entities.append(quod.Region([[start, end]], [y-0.2+dy+ystep*4, 0.2], domain, style=style, size=size)) spans.append([start, end]) return entities, styledict
def run_quod(cfg): entities = {} maxlens = {} for name in cfg['subplots']: entities[name] = [] maxlens[name] = 0 for seq in cfg['sequences'][name]: entities[name].append(quod.What(seq)) maxlens[name] = max(maxlens[name], len(entities[name][-1])) #walls if name in cfg['walls']: for walldef in cfg['walls'][name]: entities[name].append(quod.Wall(spans=[[walldef['start'], walldef['end']]], y=walldef['y'], ylim=walldef['lim'], thickness=walldef['thickness'], wedge=walldef['thickness'])) #domains if name in cfg['domains']: for domaindef in cfg['domains'][name]: entities[name].append(quod.Region( spans=domaindef['xlim'], yspan=domaindef['ylim'], label=domaindef['label'], style=domaindef['color'], pos=domaindef['pos'], size=domaindef['fontsize'], center=domaindef['halign'] )) cfg['weights'][name] *= maxlens[name] if 'tms' in cfg: do_tms_stuff(entities, cfg) fig = Figure() canvas = FigureCanvas(fig) fig.set_tight_layout(True) gs = gridspec.GridSpec(len(cfg['rowscheme']), cfg['hres']) hgap = cfg['hgap']/cfg['width']/2 margin = 0.03 if 'margin' not in cfg else cfg['margin'] gs.update( left=margin + hgap, right=cfg['width'] - margin, top=cfg['height'] - margin-hgap, bottom=margin, wspace=0) def get_limit(wt1, wtlist, offset=0): x = int((offset + wt1/sum(wtlist)) * cfg['hres']) return max(0, min(x, cfg['hres'])) if cfg['mode'] == 'hvordan': lims = [] cfg['lims'] = {} for namelist in cfg['rowscheme']: row = [cfg['weights'][name] for name in namelist] last = -hgap if len(namelist) == 1: cfg['lims'][namelist[0]] = [0, cfg['hres']] else: bounds = [0] s = 0 rawweights = [cfg['weights'][name] for name in namelist] weights = [w/sum(rawweights) for w in rawweights] for i in range(len(namelist) - 1): bounds.append((weights[i] - hgap) * cfg['hres']) bounds.append((weights[i] + hgap) * cfg['hres']) #for i in namelist bounds.append(cfg['hres']) bounds = [int(x) for x in bounds] for i in range(0, len(namelist)): name = namelist[i] cfg['lims'][name] = [bounds[2*i], bounds[2*i + 1]] axdict = {} for r, row in enumerate(cfg['rowscheme']): for name in row: #axdict[name] = fig.add_subplot(gs[r, cfg['lims'][name][0]:cfg['lims'][name][1]]) axdict[name] = fig.add_subplot(gs[r,cfg['lims'][name][0]:cfg['lims'][name][1]]) n = 0 name2row = {} stretchlabel = {} firstcol = set() for r, row in enumerate(cfg['rowscheme']): for c, name in enumerate(row): if not c: firstcol.add(name) name2row[name] = r if 'ylabel' in cfg and name in cfg['ylabel']: ylabel = cfg['ylabel'][name] else: ylabel = '' if c else None yticks = [] if c else None plot(cfg, name, fig, canvas, axdict[name], entities[name], ylabel=ylabel, yticks=yticks) try: title = cfg['title'][name] except KeyError: title = chr(65 + n) axdict[name].set_title(title, loc='left') n += 1 if len(row) > 1: ax = fig.add_subplot(gs[r,:]) ax.set_xticks([0]) ax.axes.get_yaxis().set_visible(False) ax.set_frame_on(False) #TODO: expose customizing xlabels per-plot/per-row ax.set_xlabel('Position') ax.tick_params(labelcolor=(0,0,0,0), color=(0,0,0,0)) stretchlabel[r] = ax elif len(row) == 1: #ax = fig.add_subplot(gs[r,:]) ax = axdict[row[0]] ax.set_xlabel('Position') stretchlabel[r] = ax if 'xlabel' in cfg: for name in cfg['xlabel']: stretchlabel[name2row[name]].set_xlabel(cfg['xlabel'][name]) if 'ylabel' in cfg: for name in cfg['ylabel']: stretchlabel[name2row[name]].set_ylabel(cfg['ylabel'][name]) if 'font' in cfg: for name in cfg['font']: for target in cfg['font'][name]: if target.endswith('ticks'): axdict[name].tick_params(labelsize=cfg['font'][name][target]) if name in firstcol: stretchlabel[name2row[name]].tick_params(labelsize=cfg['font'][name][target]) elif target.endswith('xaxis'): axdict[name].set_xlabel(axdict[name].get_xlabel(), fontsize=cfg['font'][name][target]) stretchlabel[name2row[name]].set_xlabel(stretchlabel[name2row[name]].get_xlabel(), fontsize=cfg['font'][name][target]) elif target.endswith('yaxis'): axdict[name].set_ylabel(axdict[name].get_ylabel(), fontsize=cfg['font'][name][target]) elif target == 'title': axdict[name].set_title(axdict[name].get_title(), fontsize=cfg['font'][name][target]) #gs.tight_layout(fig, pad=cfg['margin'], w_pad=cfg['hgap'], h_pad=0.0) gs.tight_layout(fig, pad=0, w_pad=cfg['hgap'], h_pad=cfg['vgap']) fig.savefig(cfg['outfile'], dpi=cfg['dpi']) else: raise NotImplementedError('Unimplemented mode: {}'.format(cfg['mode']))
def get_domain(self): '''return an object consisting of some text and a strongly colored box''' y = -2 if self.y is None else self.y return quod.Region(spans=[[self.left, self.right]], yspan=[y-0.15, 0.15], label=self.label, style=self.style)
def run_quod(cfg): entities = {} maxlens = {} for name in cfg['subplots']: entities[name] = [] maxlens[name] = 0 mode = cfg['mode'][name] if mode in ('hvordan', 'quod'): if cfg['frag'][name]: for i in range(0, len(cfg['sequences'][name]), 2): entities[name].append( quod.FragmentWhat(cfg['sequences'][name][i], cfg['sequences'][name][i + 1])) maxlens[name] = max(maxlens[name], len(entities[name][-1])) else: for seq in cfg['sequences'][name]: entities[name].append(quod.What(seq)) maxlens[name] = max(maxlens[name], len(entities[name][-1])) elif mode in ('avehas', ): if avehas3 is None: raise ImportError('Could not find avehas3.py') for seq in cfg['sequences'][name]: entities[name].append( avehas3.Avehas(io.BytesIO(seq.encode('utf-8')), amphi=cfg['amphi'][name])) #print(name, seq, maxlens[name]) #for msa in avehas3.AlignIO.parse( #print(len(entities[name][-1])) maxlens[name] = max(maxlens[name], len(entities[name][-1])) elif mode in ('empty', ): spacer = Spacer(length=cfg['baselength'][name]) maxlens[name] = cfg['baselength'][name] entities[name].append(spacer) for seq in cfg['sequences'][name]: #walls if name in cfg['walls']: for walldef in cfg['walls'][name]: entities[name].append( quod.Wall(spans=[[walldef['start'], walldef['end']]], y=walldef['y'], ylim=walldef['lim'], thickness=walldef['thickness'], wedge=walldef['thickness'])) #domains if name in cfg['domains']: for domaindef in cfg['domains'][name]: entities[name].append( quod.Region(spans=domaindef['xlim'], yspan=domaindef['ylim'], label=domaindef['label'], style=domaindef['color'], pos=domaindef['pos'], size=domaindef['fontsize'], center=domaindef['halign'])) if name in cfg['text']: label = cfg['text'][name] entities[name].append( quod.Text(label['pos'], label['text'], label.get('halign', 'l'), label.get('font', None))) if cfg['baselength'][name] is None: cfg['baselength'][name] = maxlens[name] #cfg['length'][name] = maxlens[name] #cfg['weight'][name] = maxlens[name] cfg['length'][name] = cfg['baselength'][name] * cfg['weight'][name] if 'tms' in cfg: do_tms_stuff(entities, cfg) fig = quod.Figure() fig.set_tight_layout(True) gs = gridspec.GridSpec(len(cfg['rowscheme']), cfg['hres']) hgap = cfg['hgap'] / cfg['width'] / 2 margin = 0.03 if 'margin' not in cfg else cfg['margin'] gs.update(left=margin + hgap, right=cfg['width'] - margin, top=cfg['height'] - margin - hgap, bottom=margin, wspace=0) def get_limit(wt1, wtlist, offset=0): x = int((offset + wt1 / sum(wtlist)) * cfg['hres']) return max(0, min(x, cfg['hres'])) if True: lims = [] cfg['lims'] = {} for namelist in cfg['rowscheme']: row = [cfg['length'][name] for name in namelist] last = -hgap if len(namelist) == 1: cfg['lims'][namelist[0]] = [0, cfg['hres']] else: bounds = [0] s = 0 rawweight = [cfg['length'][name] for name in namelist] weight = [w / sum(rawweight) for w in rawweight] for i in range(len(namelist) - 1): bounds.append((weight[i] - hgap) * cfg['hres']) bounds.append((weight[i] + hgap) * cfg['hres']) #for i in namelist bounds.append(cfg['hres']) bounds = [int(x) for x in bounds] for i in range(0, len(namelist)): name = namelist[i] cfg['lims'][name] = [bounds[2 * i], bounds[2 * i + 1]] axdict = {} for r, row in enumerate(cfg['rowscheme']): for name in row: #axdict[name] = fig.add_subplot(gs[r, cfg['lims'][name][0]:cfg['lims'][name][1]]) axdict[name] = fig.add_subplot( gs[r, cfg['lims'][name][0]:cfg['lims'][name][1]]) n = 0 name2row = {} stretchlabel = {} firstcol = set() for r, row in enumerate(cfg['rowscheme']): for c, name in enumerate(row): if not c: firstcol.add(name) name2row[name] = r if 'ylabel' in cfg and name in cfg['ylabel']: ylabel = cfg['ylabel'][name] else: ylabel = '' if c else None yticks = None #[] if c else None plot(cfg, name, fig, axdict[name], entities[name], ylabel=ylabel, yticks=yticks) if c: axdict[name].set_yticklabels( [] * len(axdict[name].get_yticklabels())) try: title = cfg['title'][name] except KeyError: title = chr(65 + n) titlefont = get_titlefont(cfg, name) #TODO: implement rowtitle separately #axdict[name].set_title(cfg['title'].get(name, ''), loc='left', fontsize=titlefont) titlepad = quod.matplotlib.rcParams['axes.titlepad'] transOffset = matplotlib.transforms.ScaledTranslation( 0., titlepad / 72., fig.dpi_scale_trans) if name in cfg['ctitle']: subtitlefont = get_titlefont(cfg, name, subname='ctitle', default=titlefont) t = axdict[name].text(0.5, 1.0, cfg['ctitle'].get(name, ''), ha='center', va='baseline', fontsize=subtitlefont, transform=axdict[name].transAxes) t.set_transform(axdict[name].transAxes + transOffset) if name in cfg['ltitle']: subtitlefont = get_titlefont(cfg, name, subname='ltitle', default=titlefont) t = axdict[name].text(0.0, 1.0, cfg['ltitle'].get(name, ''), ha='left', va='baseline', fontsize=subtitlefont, transform=axdict[name].transAxes) t.set_transform(axdict[name].transAxes + transOffset) if name in cfg['rtitle']: subtitlefont = get_titlefont(cfg, name, subname='rtitle', default=titlefont) t = axdict[name].text(1.0, 1.0, cfg['rtitle'].get(name, ''), ha='right', va='baseline', fontsize=subtitlefont, transform=axdict[name].transAxes) t.set_transform(axdict[name].transAxes + transOffset) if name in cfg['title']: axdict[name].set_title(cfg['title'].get(name, ''), loc='left', fontsize=titlefont) n += 1 if len(row) > 1: ax = fig.add_subplot(gs[r, :]) ax.set_xticks([0]) ax.axes.get_yaxis().set_visible(False) #ax.axes.get_xaxis().set_visible(False) ax.set_frame_on(False) #TODO: expose customizing xlabels per-plot/per-row ax.set_xlabel('Position') ax.tick_params(labelcolor=(1, 1, 1, 0), color=(1, 1, 1, 0)) for side in ['left', 'right', 'bottom', 'top']: ax.spines[side].set_color((1, 1, 1, 0)) ax.spines[side].set_linewidth(0) stretchlabel[r] = ax elif len(row) == 1: #ax = fig.add_subplot(gs[r,:]) ax = axdict[row[0]] ax.set_xlabel('Position') stretchlabel[r] = ax if 'xlabel' in cfg: for name in cfg['xlabel']: if name.startswith('row:'): stretchlabel[name2row[name[4:]]].set_xlabel( cfg['xlabel'][name]) else: axdict[name].set_xlabel(cfg['xlabel'][name]) stretchlabel[name2row[name]].set_xlabel(' ') if 'ylabel' in cfg: for name in cfg['ylabel']: stretchlabel[name2row[name]].set_ylabel(cfg['ylabel'][name]) if 'font' in cfg: for name in cfg['font']: for target in cfg['font'][name]: if target.endswith('ticks'): axdict[name].tick_params( labelsize=cfg['font'][name][target]) if name in firstcol: stretchlabel[name2row[name]].tick_params( labelsize=cfg['font'][name][target]) elif target.endswith('xaxis'): axdict[name].set_xlabel( axdict[name].get_xlabel(), fontsize=cfg['font'][name][target]) stretchlabel[name2row[name]].set_xlabel( stretchlabel[name2row[name]].get_xlabel(), fontsize=cfg['font'][name][target]) elif target.endswith('yaxis'): axdict[name].set_ylabel( axdict[name].get_ylabel(), fontsize=cfg['font'][name][target]) #gs.tight_layout(fig, pad=cfg['margin'], w_pad=cfg['hgap'], h_pad=0.0) #gs.tight_layout(fig, pad=0, w_pad=cfg['hgap'], h_pad=cfg['vgap']) gs.tight_layout(fig, pad=cfg['margin'], w_pad=cfg['hgap'], h_pad=cfg['vgap']) fig.savefig(cfg['outfile'], dpi=cfg['dpi']) else: raise NotImplementedError('Unimplemented mode: {}'.format(cfg['mode']))