def draw_gel(self, digest_metadata, xlabel, output_dir): strain_name = digest_metadata['ID'] cpn60UT_ladder = custom_ladder( "cpn60UT", { 25: 250, 50: 225, 100: 205, 200: 186, 300: 171, 400: 158, 500: 149, 600: 139 }) LADDER_100_to_1k = custom_ladder( "cpn60UT", { 25: 249, 50: 226, 100: 205, 200: 186, 300: 171, 400: 158, 500: 149, 650: 139, 850: 128, 1000: 121 }) # ladder = cpn60UT_ladder.modified(background_color="#E2EDFF", label_fontdict={"rotation": -90}, label="MW") ladder = LADDER_100_to_1k.modified(background_color="#E2EDFF", label_fontdict={"rotation": -90}, label="MW") patterns = [] for renzyme in sorted(self.renzymes): patterns.append( BandsPattern(digest_metadata[str((renzyme, 'Band Sizes'))], ladder, label_fontdict={"rotation": -90}, label=renzyme)) # patterns_set = BandsPatternsSet(patterns=[ladder] + patterns, ladder=ladder, label_fontdict={"size": 7}, label="Migration Distance", ladder_ticks=3) patterns_set = BandsPatternsSet(patterns=[ladder] + patterns, ladder=ladder, ladder_ticks=3) ax = patterns_set.plot() ax.set_xlabel(xlabel) strain_filename = strain_name.replace("/", "-").replace("(", "_").replace( ")", "_") outfile = os.path.join(output_dir, strain_filename + ".png") if not os.path.exists(outfile): ax.figure.savefig(outfile, bbox_inches="tight", dpi=600) return outfile
def work(self): self.logger(message="Reading Data...") data = self.data records = [records_from_data_file(f)[0][0] for f in data.files] for f, record in zip(data.files, records): record.linear = not f.circularity record.id = f.name ladder = bandwagon.custom_ladder(None, LADDERS[data.ladder].bands) digestions = data.digestions self.logger(message="Generating report...") preview, report = generate_report(records=records, digestions=digestions, ladder=ladder, group_by="digestions", full_report=data.makeReport, show_band_sizes=data.showBandsSizes) if data.makeReport: report = data_to_html_data(report, 'zip') return JobResult(preview_html='<img src="%s"/>' % preview, file_data=report, file_name=None if (report is None) else "digestion_report.zip", file_mimetype="application/zip")
def plot_digestions( self, digestions, axes=None, bands_props=None, patterns_props=None, patternset_props=None, target_file=None, close_figure=False, ): """Plot the patterns for each sequence, for each digestion in the list. Requires Bandwagon. Parameters ---------- digestions A list of digestions, e.g. [('EcoRI',), ('BamHI', 'XbaI'), ...] axes Axes on which to plot the plot. There should be as many axes in the list as there are digestions provided. Ideally the axes would be vertically stacked. If not provided, new axes are created and returned in the end bands_props, patterns_props, patternset_props Graphical properties (colors, labels, etc.) of band patterns, see code and BandWagon for more details. target_file The name of the (PNG, SVG, JPEG...) file in which to write the plot. Returns ------- axes The axes of the generated figure (if a target file is written to, the figure is closed and None is returned instead) """ if not PLOTS_AVAILABLE: raise ImportError("Plotting requires Bandwagon installed") bands_props = updated_dict({"size": 10, "rotation": 50}, bands_props) patternset_props = updated_dict({"ladder_ticks": 5}, patternset_props) if isinstance(self.ladder.bands, (list, tuple)): ladder = self.ladder else: ladder = bandwagon.custom_ladder(None, self.ladder.bands) ndig, nseq = len(digestions), len(self.sequences) if axes is None: fig, axes = plt.subplots(ndig, 1, figsize=(0.5 * nseq, 3 * ndig)) if ndig == 1: axes = [axes] for ax, digestion in zip(axes, digestions): bandwagon.BandsPatternsSet(patterns=[ ladder if ax == axes[0] else ladder.modified(label=None) ] + [ bandwagon.BandsPattern( bands=self.sequences_digestions[seq_name][digestion] ["bands"], label=seq_name if (ax == axes[0]) else None, ladder=ladder, global_bands_props=bands_props, topology=self.records[seq_name].annotations["topology"], ) for seq_name in self.sequences ], ladder=ladder, label=", ".join([ " + ".join([e for e in d]) for d in [digestion] ]), global_patterns_props=patterns_props, **patternset_props).plot(ax) if target_file is not None: axes[0].figure.savefig(target_file, bbox_inches="tight") if close_figure: plt.close(axes[0].figure) return axes
def work(self): self.logger(message="Exploring possible digestions...") data = self.data ladder = LADDERS[data.ladder] enzymes = data.possible_enzymes records = records_from_data_files(data.files) for record in records: set_record_topology(record, data.topology) # sequences = OrderedDict( # [(record.id, str(record.seq)) for record in records] # ) self.logger(message="Initializing...") if data.goal == "ideal": mini, maxi = data.bands_range problem = IdealDigestionsProblem( sequences=records, enzymes=enzymes, ladder=ladder, min_bands=mini, max_bands=maxi, max_enzymes_per_digestion=data.max_enzymes) else: problem = SeparatingDigestionsProblem( sequences=records, enzymes=enzymes, ladder=ladder, max_enzymes_per_digestion=data.max_enzymes, ) self.logger(message="Selecting digestions...") score, selected_digestions = problem.select_digestions( max_digestions=data.max_digestions, search="full") bands_props = (None if not data.show_bands_sizes else dict( label="=size", label_fontdict=dict(size=6))) axes = problem.plot_digestions( selected_digestions, patterns_props={"label_fontdict": { "rotation": 35 }}, bands_props=bands_props, ) figure_data = matplotlib_figure_to_svg_base64_data(axes[0].figure, bbox_inches="tight") if data.plot_cuts: ladder = bandwagon.custom_ladder(None, ladder.bands) self.logger(message="Plotting cuts maps...") zip_root = flametree.file_tree("@memory") bandwagon.plot_records_digestions( target=zip_root._file("Details.pdf").open("wb"), ladder=ladder, records_and_digestions=[(rec, digestion) for rec in records for digestion in selected_digestions], ) pdf_data = zip_root["Details.pdf"].read("rb") pdf_data = data_to_html_data(pdf_data, datatype="pdf") else: pdf_data = None return { "figure_data": figure_data, "digestions": selected_digestions, "score": score, "pdf_data": pdf_data, }
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from bandwagon import BandsPattern from bandwagon import BandsPatternsSet from bandwagon import custom_ladder LADDER_100_to_4k = custom_ladder( "100-4k", { 100: 205, 200: 186, 300: 171, 400: 158, 500: 149, 650: 139, 850: 128, 1000: 121, 1650: 100, 2000: 90, 3000: 73, 4000: 65, }, ) ladder = LADDER_100_to_4k.modified(label="Ladder", background_color="#ffffaf") patterns = [ BandsPattern([10000, 7750, 4000, 3000, 2000, 1500, 1000, 750, 500], ladder, label="C1"),