def spectrum_match_info(self, glycopeptide): spectrum_match_ref = glycopeptide.best_spectrum_match scan_id = spectrum_match_ref.scan.scan_id scan = self.scan_loader.get_scan_by_id(scan_id) try: mass_shift = spectrum_match_ref.mass_shift except Exception: mass_shift = Unmodified if mass_shift.name != Unmodified.name: mass_shift = mass_shift.convert() else: mass_shift = Unmodified match = CoverageWeightedBinomialScorer.evaluate( scan, glycopeptide.structure.convert(), error_tolerance=self.analysis. parameters["fragment_error_tolerance"], mass_shift=mass_shift) specmatch_artist = TidySpectrumMatchAnnotator(match, ax=figax()) specmatch_artist.draw(fontsize=10, pretty=True) annotated_match_ax = specmatch_artist.ax scan_title = scan.id if len(scan_title) > 60: scan_title = '\n'.join(textwrap.wrap(scan_title, 60)) annotated_match_ax.set_title(scan_title, fontsize=18) annotated_match_ax.set_ylabel(annotated_match_ax.get_ylabel(), fontsize=16) annotated_match_ax.set_xlabel(annotated_match_ax.get_xlabel(), fontsize=16) sequence_logo_plot = glycopeptide_match_logo(match, ax=figax()) xlim = list(sequence_logo_plot.get_xlim()) xlim[0] += 1 sequence_logo_plot.set_xlim(xlim[0], xlim[1]) spectrum_plot = png_plot(annotated_match_ax, svg_width="100%", bbox_inches='tight', height=3 * 1.5, width=8 * 1.5, img_width="100%", patchless=True) logo_plot = png_plot(sequence_logo_plot, svg_width="100%", img_width="100%", xml_transform=scale_fix_xml_transform, bbox_inches='tight', height=2, width=6 * 1.5, patchless=True) return dict(spectrum_plot=spectrum_plot, logo_plot=logo_plot, precursor_mass_accuracy=match.precursor_mass_accuracy(), spectrum_match=match)
def chromatogram_figures(chroma): figures = [] plot = SmoothingChromatogramArtist([chroma], colorizer=lambda *a, **k: 'green', ax=figax()).draw( label_function=lambda *a, **k: "", legend=False).ax plot.set_title("Aggregated\nExtracted Ion Chromatogram", fontsize=24) chroma_svg = svguri_plot(plot, bbox_inches='tight', height=5, width=9, svg_width="100%") figures.append(chroma_svg) if len(chroma.adducts) > 1: adducts = list(chroma.adducts) labels = {} rest = chroma for adduct in adducts: with_adduct, rest = rest.bisect_adduct(adduct) labels[adduct] = with_adduct adduct_plot = SmoothingChromatogramArtist( labels.values(), colorizer=lambda *a, **k: 'green', ax=figax()).draw( label_function=lambda *a, **k: tuple(a[0].adducts)[0].name, legend=False).ax adduct_plot.set_title("Adduct-Separated\nExtracted Ion Chromatogram", fontsize=24) adduct_separation = svguri_plot(adduct_plot, bbox_inches='tight', height=5, width=9, svg_width="100%") figures.append(adduct_separation) if len(chroma.charge_states) > 1: charge_separating_plot = ChargeSeparatingSmoothingChromatogramArtist( [chroma], ax=figax()).draw(label_function=lambda x, *a, **kw: str( tuple(x.charge_states)[0]), legend=False).ax charge_separating_plot.set_title( "Charge-Separated\nExtracted Ion Chromatogram", fontsize=24) charge_separation = svguri_plot(charge_separating_plot, bbox_inches='tight', height=5, width=9, svg_width="100%") figures.append(charge_separation) return figures
def draw_glycoforms(self, glycoprotein): ax = figax() layout = GlycoformLayout(glycoprotein, glycoprotein.identified_glycopeptides, ax=ax) layout.draw() svg = layout.to_svg(scale=2.0, height_padding_scale=1.1) return svg
def chromatogram_plot(self, glycopeptide): ax = figax() try: SmoothingChromatogramArtist( glycopeptide, ax=ax, label_peaks=False, colorizer=lambda x: "#48afd0").draw(legend=False) ax.set_xlabel("Time (Minutes)", fontsize=16) ax.set_ylabel("Relative Abundance", fontsize=16) return png_plot(ax, bbox_inches='tight', img_height='100%') except ValueError: return "<div style='text-align:center;'>No Chromatogram Found</div>"
def draw_glycoforms(self, glycoprotein): ax = figax() layout = GlycoformLayout(glycoprotein, glycoprotein.identified_glycopeptides, ax=ax) layout.draw() svg = layout.to_svg(scale=2.0, height_padding_scale=1.1) # svg = plot_glycoforms_svg( # glycoprotein, glycoprotein.identified_glycopeptides, ax=ax, # margin_left=85, margin_top=0, height_padding_scale=1.1) return svg
def render_chromatograms(reader): acc = [] for scan_id in reader.extended_index.ms1_ids: header = reader.get_scan_header_by_id(scan_id) acc.extend(header.arrays[1]) threshold = np.percentile(acc, 90) ex = ChromatogramExtractor(reader, minimum_intensity=threshold, minimum_mass=300) chroma = ex.run() window_width = 0.01 ax = figax() a = SmoothingChromatogramArtist(list(chroma) + [ex.total_ion_chromatogram], ax=ax, colorizer=lambda *a, **k: 'lightblue') a.draw(label_function=lambda *a, **kw: "") rt, intens = ex.total_ion_chromatogram.as_arrays() a.draw_generic_chromatogram("TIC", rt, intens, 'lightblue') a.ax.set_ylim(0, max(intens) * 1.1) if reader.extended_index.msn_ids: ox_time = [] ox_current = [] for scan_id in reader.extended_index.msn_ids: scan = reader.get_scan_header_by_id(scan_id) mz, intens = scan.arrays total = 0 for ion in standard_oxonium_ions: coords = sweep(mz, ion.mass() + 1.007, window_width) total += intens[coords].sum() ox_time.append(scan.scan_time) ox_current.append(total) oxonium_axis = ax.twinx() stub = SimpleChromatogram(ex.total_ion_chromatogram.time_converter) for key in ex.total_ion_chromatogram: stub[key] = 0 oxonium_artist = SmoothingChromatogramArtist( [stub], ax=oxonium_axis).draw(label_function=lambda *a, **kw: "") rt, intens = ox_time, ox_current oxonium_axis.set_ylim(0, max(intens) * 1.1) oxonium_axis.yaxis.tick_right() oxonium_axis.axes.spines['right'].set_visible(True) oxonium_axis.set_ylabel("Oxonium Abundance", fontsize=18) oxonium_artist.draw_generic_chromatogram("Oxonium Ions", rt, intens, 'green') fig = a.ax.get_figure() fig.set_figwidth(10) return png_plot(ax, patchless=True, bbox_inches='tight')
def draw_lcms_map(self): if self.abundance_threshold is None: self._estimate_threshold() ax = figax() artist = LCMSMapArtist.from_peak_loader( self.reader, threshold=self.abundance_threshold / 2., ax=ax) artist.draw() return png_plot(ax, patchless=True, bbox_inches='tight', width=10, height=10)
def chromatogram_plot(self, glycopeptide): ax = figax() try: SmoothingChromatogramArtist( glycopeptide, ax=ax, label_peaks=False, colorizer=lambda x: "#48afd0").draw(legend=False) ax.set_xlabel("Time (Minutes)", fontsize=16) ax.set_ylabel("Relative Abundance", fontsize=16) return png_plot(ax, bbox_inches='tight', img_height='100%') except ValueError: return "<div style='text-align:center;'>No Chromatogram Found</div>"
def chromatogram_figures(chroma): figures = [] plot = SmoothingChromatogramArtist( [chroma], colorizer=lambda *a, **k: 'green', ax=figax()).draw( label_function=lambda *a, **k: "", legend=False).ax plot.set_title("Aggregated\nExtracted Ion Chromatogram", fontsize=24) chroma_svg = svguri_plot( plot, bbox_inches='tight', height=5, width=9, svg_width="100%") figures.append(chroma_svg) if len(chroma.mass_shifts) > 1: mass_shifts = list(chroma.mass_shifts) labels = {} rest = chroma for mass_shift in mass_shifts: with_mass_shift, rest = rest.bisect_mass_shift(mass_shift) labels[mass_shift] = with_mass_shift mass_shift_plot = SmoothingChromatogramArtist( labels.values(), colorizer=lambda *a, **k: 'green', ax=figax()).draw( label_function=lambda *a, **k: tuple(a[0].mass_shifts)[0].name, legend=False).ax mass_shift_plot.set_title( "mass_shift-Separated\nExtracted Ion Chromatogram", fontsize=24) mass_shift_separation = svguri_plot( mass_shift_plot, bbox_inches='tight', height=5, width=9, svg_width="100%") figures.append(mass_shift_separation) if len(chroma.charge_states) > 1: charge_separating_plot = ChargeSeparatingSmoothingChromatogramArtist( [chroma], ax=figax()).draw( label_function=lambda x, *a, **kw: str( tuple(x.charge_states)[0]), legend=False).ax charge_separating_plot.set_title( "Charge-Separated\nExtracted Ion Chromatogram", fontsize=24) charge_separation = svguri_plot( charge_separating_plot, bbox_inches='tight', height=5, width=9, svg_width="100%") figures.append(charge_separation) return figures
def draw_chromatograms(self): if self.chromatograms is None: self.build_chromatograms() ax = figax() chromatograms = list(self.chromatograms) if len(chromatograms): chromatograms.append(self.total_ion_chromatogram) chromatograms = [ chrom for chrom in chromatograms if len(chrom) > 0 ] a = SmoothingChromatogramArtist( chromatograms, ax=ax, colorizer=lambda *a, **k: 'lightblue') a.draw(label_function=lambda *a, **kw: "") rt, intens = self.total_ion_chromatogram.as_arrays() a.draw_generic_chromatogram("TIC", rt, intens, 'lightblue') a.ax.set_ylim(0, max(intens) * 1.1) chromatogram_artist = a fig = chromatogram_artist.ax.get_figure() fig.set_figwidth(10) fig.set_figheight(5) # if self.reader.extended_index.msn_ids: # oxonium_axis = ax.twinx() # stub = SimpleChromatogram( # self.total_ion_chromatogram.time_converter) # for key in self.total_ion_chromatogram: # stub[key] = 0 # oxonium_ion_artist = SmoothingChromatogramArtist( # [stub], # ax=oxonium_axis).draw( # label_function=lambda *a, **kw: "") # rt, intens = self.oxonium_ion_chromatogram # oxonium_axis.set_ylim(0, max(intens) * 1.1) # oxonium_axis.yaxis.tick_right() # oxonium_axis.axes.spines['right'].set_visible(True) # oxonium_axis.set_ylabel("Oxonium Abundance", fontsize=18) # oxonium_ion_artist.draw_generic_chromatogram( # "Oxonium Ions", rt, intens, 'green') else: ax.text(0.5, 0.5, "No chromatograms extracted", ha='center', fontsize=16) ax.axis('off') return png_plot(ax, patchless=True, bbox_inches='tight', width=12, height=8)
def site_specific_abundance_plots(self, glycoprotein): axes = OrderedDict() for glyco_type in glycoprotein.glycosylation_types: for site in sorted(glycoprotein.glycosylation_sites_for(glyco_type)): spanning_site = glycoprotein.site_map[glyco_type][site] if len(spanning_site) == 0: continue bundle = BundledGlycanComposition.aggregate(spanning_site) if len(bundle) == 0: continue ax = figax() AggregatedAbundanceArtist( bundle, ax=ax, colorizer=glycan_colorizer_type_map[glyco_type]).draw() ax.set_title("%s Glycans\nat Site %d" % (glyco_type.name, site + 1,), fontsize=18) axes[site, glyco_type] = svguri_plot(ax, bbox_inches='tight') return axes
def site_specific_abundance_plots(self, glycoprotein): axes = OrderedDict() for glyco_type in glycoprotein.glycosylation_types: for site in sorted( glycoprotein.glycosylation_sites_for(glyco_type)): spanning_site = glycoprotein.site_map[glyco_type][site] if len(spanning_site) == 0: continue bundle = BundledGlycanComposition.aggregate(spanning_site) ax = figax() AggregatedAbundanceArtist( bundle, ax=ax, colorizer=glycan_colorizer_type_map[glyco_type]).draw() ax.set_title("%s Glycans\nat Site %d" % ( glyco_type.name, site, ), fontsize=18) axes[site, glyco_type] = svguri_plot(ax, bbox_inches='tight') return axes
def spectrum_match_info(self, glycopeptide): matched_scans = [] for solution_set in glycopeptide.spectrum_matches: best_solution = solution_set.best_solution() try: selected_solution = solution_set.solution_for(glycopeptide.structure) except KeyError: continue pass_threshold = abs(selected_solution.score - best_solution.score) < 1e-6 if not pass_threshold: continue if isinstance(selected_solution.scan, SpectrumReference): scan = self.session.query(MSScan).filter( MSScan.scan_id == selected_solution.scan.id, MSScan.sample_run_id == self.analysis.sample_run_id).first().convert() else: scan = selected_solution.scan scan.score = selected_solution.score matched_scans.append(scan) spectrum_match_ref = max(glycopeptide.spectrum_matches, key=lambda x: x.score) scan_id = spectrum_match_ref.scan.scan_id scan = self.scan_loader.get_scan_by_id(scan_id) try: mass_shift = spectrum_match_ref[0].mass_shift except Exception: mass_shift = Unmodified if mass_shift.name != Unmodified.name: mass_shift = mass_shift.convert() else: mass_shift = Unmodified match = CoverageWeightedBinomialScorer.evaluate( scan, glycopeptide.structure.convert(), error_tolerance=self.analysis.parameters["fragment_error_tolerance"], mass_shift=mass_shift) specmatch_artist = TidySpectrumMatchAnnotator(match, ax=figax()) specmatch_artist.draw(fontsize=10, pretty=True) annotated_match_ax = specmatch_artist.ax scan_title = scan.id if len(scan_title) > 60: scan_title = '\n'.join(textwrap.wrap(scan_title, 60)) annotated_match_ax.set_title(scan_title, fontsize=18) annotated_match_ax.set_ylabel(annotated_match_ax.get_ylabel(), fontsize=16) annotated_match_ax.set_xlabel(annotated_match_ax.get_xlabel(), fontsize=16) sequence_logo_plot = glycopeptide_match_logo(match, ax=figax()) xlim = list(sequence_logo_plot.get_xlim()) xlim[0] += 1 sequence_logo_plot.set_xlim(xlim[0], xlim[1]) spectrum_plot = png_plot( annotated_match_ax, svg_width="100%", bbox_inches='tight', height=3 * 1.5, width=8 * 1.5, img_width="100%", patchless=True) logo_plot = png_plot( sequence_logo_plot, svg_width="100%", img_width="100%", xml_transform=scale_fix_xml_transform, bbox_inches='tight', height=2, width=6 * 1.5, patchless=True) return dict( spectrum_plot=spectrum_plot, logo_plot=logo_plot, precursor_mass_accuracy=match.precursor_mass_accuracy(), spectrum_match=match)
def draw_glycoforms(self, glycoprotein): ax = figax() layout = GlycoformLayout(glycoprotein, glycoprotein.identified_glycopeptides, ax=ax) layout.draw() svg = layout.to_svg(scale=2.0, height_padding_scale=1.1) return svg
def spectrum_match_info(self, glycopeptide): matched_scans = [] for solution_set in glycopeptide.spectrum_matches: best_solution = solution_set.best_solution() try: selected_solution = solution_set.solution_for( glycopeptide.structure) except KeyError: continue pass_threshold = abs(selected_solution.score - best_solution.score) < 1e-6 if not pass_threshold: continue if isinstance(selected_solution.scan, SpectrumReference): scan = self.session.query(MSScan).filter( MSScan.scan_id == selected_solution.scan.id, MSScan.sample_run_id == self.analysis.sample_run_id).first().convert() else: scan = selected_solution.scan scan.score = selected_solution.score matched_scans.append(scan) spectrum_match_ref = max(glycopeptide.spectrum_matches, key=lambda x: x.score) scan_id = spectrum_match_ref.scan.scan_id scan = self.scan_loader.get_scan_by_id(scan_id) match = CoverageWeightedBinomialScorer.evaluate( scan, glycopeptide.structure.convert(), error_tolerance=self.analysis. parameters["fragment_error_tolerance"]) specmatch_artist = SpectrumMatchAnnotator(match, ax=figax()) specmatch_artist.draw(fontsize=10, pretty=True) annotated_match_ax = specmatch_artist.ax annotated_match_ax.set_title("%s\n" % (scan.id, ), fontsize=18) annotated_match_ax.set_ylabel(annotated_match_ax.get_ylabel(), fontsize=16) annotated_match_ax.set_xlabel(annotated_match_ax.get_xlabel(), fontsize=16) sequence_logo_plot = glycopeptide_match_logo(match, ax=figax()) xlim = list(sequence_logo_plot.get_xlim()) xlim[0] += 1 sequence_logo_plot.set_xlim(xlim[0], xlim[1]) spectrum_plot = png_plot(annotated_match_ax, svg_width="100%", bbox_inches='tight', height=3 * 1.5, width=8 * 1.5, img_width="100%", patchless=True) logo_plot = png_plot(sequence_logo_plot, svg_width="100%", img_width="100%", xml_transform=scale_fix_xml_transform, bbox_inches='tight', height=2, width=6 * 1.5, patchless=True) return dict(spectrum_plot=spectrum_plot, logo_plot=logo_plot, precursor_mass_accuracy=match.precursor_mass_accuracy(), spectrum_match=match)