Example #1
0
 def draw_peak_pair(self,
                    pair,
                    color='red',
                    alpha=0.8,
                    fontsize=12,
                    label=None,
                    rotation=45,
                    **kwargs):
     p1, p2 = pair
     self.ax.plot((p1.mz, p2.mz), (p1.intensity, p2.intensity),
                  color=color,
                  alpha=alpha,
                  **kwargs)
     draw_peaklist(pair, ax=self.ax, alpha=0.4, color='orange')
     if label:
         midx = (p1.mz + p2.mz) / 2
         # interpolate the midpoint's height
         midy = (p1.intensity * (p2.mz - midx) + p2.intensity *
                 (midx - p1.mz)) / (p2.mz - p1.mz)
         if isinstance(label, (list, tuple)):
             label = '-'.join(map(str, label))
         else:
             label = str(label)
         self.ax.text(midx,
                      midy,
                      label,
                      fontsize=fontsize,
                      ha='center',
                      va='bottom',
                      rotation=rotation,
                      clip_on=True)
    def draw_peak_pair(self, pair, color='red', alpha=0.8, fontsize=12, label=None, rotation=45, **kwargs):
        p1, p2 = pair
        self.ax.plot((p1.mz, p2.mz), (p1.intensity, p2.intensity),
                     color=color, alpha=alpha, **kwargs)
        kwargs.setdefault("clip_on", self.clip_labels)
        clip_on = kwargs['clip_on']
        draw_peaklist(pair, ax=self.ax, alpha=0.4, color='orange')
        if label:
            midx = (p1.mz + p2.mz) / 2
            # interpolate the midpoint's height
            midy = (p1.intensity * (p2.mz - midx) + p2.intensity * (midx - p1.mz)) / (p2.mz - p1.mz)

            # find the angle of the line connecting the two peaks
            xlo = min(p1.mz, p2.mz)
            xhi = max(p1.mz, p2.mz)
            adj = xhi - xlo
            ylo = min(p1.intensity, p2.intensity)
            yhi = max(p1.intensity, p2.intensity)
            opp = yhi - ylo
            hypot = np.hypot(adj, opp)
            rotation = np.arccos(adj / hypot)

            if isinstance(label, (list, tuple)):
                label = '-'.join(map(str, label))
            else:
                label = str(label)
            self.ax.text(midx, midy, label, fontsize=fontsize,
                         ha='center', va='bottom', rotation=rotation, clip_on=clip_on)
Example #3
0
 def _ghost_in_theoretical_envelopes(self):
     ax = self.ax
     for result in self.results:
         draw_peaklist(result.fit.theoretical,
                       ax=ax,
                       color='green',
                       alpha=0.5,
                       lw=0.5)
 def draw_all_peaks(self, color='black', alpha=0.5, **kwargs):
     draw_peaklist(
         self.spectrum_match.deconvoluted_peak_set,
         alpha=0.3, color='grey', ax=self.ax, **kwargs)
     try:
         draw_peaklist(
             self.spectrum_match._sanitized_spectrum,
             color='grey', ax=self.ax, alpha=0.5, **kwargs)
     except AttributeError:
         pass
Example #5
0
 def _highlight_matched_envelopes(self):
     ax = self.ax
     for result in self.results:
         score = result['score']
         a = max(1 - score, 0.1)
         draw_peaklist(result.fit.experimental,
                       ax=ax,
                       color='red',
                       alpha=a,
                       lw=0.5)
 def draw_matched_peaks(self, color='red', alpha=0.8, fontsize=12, ion_series_to_color=None, **kwargs):
     if ion_series_to_color is None:
         ion_series_to_color = {}
     try:
         solution_map = self.spectrum_match.solution_map
     except AttributeError:
         return
     for peak, fragment in solution_map:
         try:
             peak_color = ion_series_to_color.get(fragment.series, color)
         except AttributeError:
             peak_color = ion_series_to_color.get(fragment.kind, color)
         draw_peaklist([peak], alpha=alpha, ax=self.ax, color=peak_color)
         self.label_peak(fragment, peak, fontsize=fontsize, **kwargs)
Example #7
0
 def draw_all_peaks(self, color='black', alpha=0.5, **kwargs):
     draw_peaklist(self.spectrum_match.spectrum,
                   alpha=0.3,
                   color='grey',
                   ax=self.ax,
                   **kwargs)
     try:
         draw_peaklist(self.spectrum_match._sanitized_spectrum,
                       color='grey',
                       ax=self.ax,
                       alpha=0.5,
                       **kwargs)
     except AttributeError:
         pass
Example #8
0
    def draw_matched_peaks(self,
                           color='red',
                           alpha=0.8,
                           fontsize=12,
                           ion_series_to_color=None,
                           **kwargs):
        if ion_series_to_color is None:
            ion_series_to_color = {}

        for peak, fragment in self.spectrum_match.solution_map:
            try:
                peak_color = ion_series_to_color.get(fragment.series, color)
            except AttributeError:
                peak_color = ion_series_to_color.get(fragment.kind, color)
            draw_peaklist([peak], alpha=alpha, ax=self.ax, color=peak_color)
            self.label_peak(fragment, peak, fontsize=fontsize, **kwargs)
Example #9
0
    def draw_peak_pair(self,
                       pair,
                       color='red',
                       alpha=0.8,
                       fontsize=12,
                       label=None,
                       rotation=45,
                       **kwargs):
        p1, p2 = pair
        self.ax.plot((p1.mz, p2.mz), (p1.intensity, p2.intensity),
                     color=color,
                     alpha=alpha,
                     **kwargs)
        kwargs.setdefault("clip_on", self.clip_labels)
        clip_on = kwargs['clip_on']
        draw_peaklist(pair, ax=self.ax, alpha=0.4, color='orange')
        if label:
            # pylint: disable=assignment-from-no-return
            midx = (p1.mz + p2.mz) / 2
            # interpolate the midpoint's height
            midy = (p1.intensity * (p2.mz - midx) + p2.intensity *
                    (midx - p1.mz)) / (p2.mz - p1.mz)

            # find the angle of the line connecting the two peaks
            xlo = min(p1.mz, p2.mz)
            xhi = max(p1.mz, p2.mz)
            adj = xhi - xlo
            ylo = min(p1.intensity, p2.intensity)
            yhi = max(p1.intensity, p2.intensity)
            opp = yhi - ylo
            hypot = np.hypot(adj, opp)
            rotation = np.arccos(adj / hypot)

            if isinstance(label, (list, tuple)):
                label = '-'.join(map(str, label))
            else:
                label = str(label)
            self.ax.text(midx,
                         midy,
                         label,
                         fontsize=fontsize,
                         ha='center',
                         va='bottom',
                         rotation=rotation,
                         clip_on=clip_on)
Example #10
0
 def draw_tid(composition, charge, ax=None):
     tid = brainpy.isotopic_variants(composition, charge=charge)
     if ax is None:
         fig, ax = plt.subplots(1)
     ax = draw_peaklist(tid, ax=ax)
     lo, hi = ax.get_xlim()
     lo -= 0.5
     hi += 0.5
     ax.set_xlim(lo, hi)
     return ax
Example #11
0
def annotate_spectrum(peaks, annotated_peaks, mz_region_start=120, mz_region_end=3000):
    ax = draw_peaklist(peaks, alpha=0.5)
    upper = max(ax.get_ylim())
    for solution in annotated_peaks:
        draw_peaklist(solution.fit.theoretical, alpha=0.8, ax=ax, color='red')
        y = max(p.intensity for p in solution.fit.experimental)
        y = min(y + 2000, upper * 0.8)
        label = "%s" % sorted(solution.solution.name.split("|"), key=len)[0]
        if solution.charge > 1:
            label += "$^{%d}$" % solution.charge
        ax.text(solution.mz, y, label, rotation=90, va='bottom', ha='center', fontsize=16)

    ax.set_xlim(mz_region_start, mz_region_end)
    max_xticks = 500
    xloc = plt.MaxNLocator(max_xticks)
    ax.xaxis.set_major_locator(xloc)
    for tl in ax.get_xticklabels():
        tl.set(rotation=90)
    fig = ax.get_figure()
    fig.set_figwidth(128)
    fig.set_figheight(64)
    return ax
 def format_axes(self):
     draw_peaklist([], self.ax, pretty=True)
     self.ax.set_ylim(0, self.upper)
Example #13
0
def draw_envelope_subgraph(envelopes, scale_factor=1.0, overlap_fn=peak_overlap, ax=None, **kwargs):
    layers = layout_layers(envelopes)
    max_score = max(e.score for e in envelopes)
    peaks = set()

    for e in envelopes:
        peaks.update(e.fit.experimental)

    peaks = sorted(peaks, key=lambda x: x.mz)
    peaks = [p.clone() for p in peaks]
    total_intensity = sum(p.intensity for p in peaks)

    start = peaks[0].mz
    end = peaks[-1].mz

    if ax is None:
        figure, ax = plt.subplots(1, 1)

    row_width = float('inf')
    annotation_text_size = 3. * scale_factor
    layer_height = 0.56 * scale_factor
    y_step = (layer_height + 0.05) * - scale_factor
    origin_y = cur_y = -layer_height - 0.075

    cur_position = peaks[0].mz

    for layer in layers:
        layer.sort(key=lambda x: x.start)

    while cur_position < end:
        next_row = cur_position + row_width

        for layer in layers:
            c = 0
            for envelope in layer:
                if envelope.start < cur_position:
                    continue
                elif envelope.start > next_row:
                    break
                c += 1
                rect = mpatches.Rectangle(
                    (envelope.start - 0.01, cur_y),
                    width=0.01 + envelope.end - envelope.start, height=layer_height,
                    facecolor='lightblue', edgecolor='black', linewidth=0.15,
                    alpha=min(max(envelope.score / max_score, 0.2), 0.8))
                ax.add_patch(rect)
                text_path = TextPath(
                    (envelope.start + 0.1, cur_y + 0.2),
                    "%0.2f, %d" % (envelope.score, envelope.fit.charge), size=annotation_text_size / 14.5,
                    prop=font_options, stretch=200)
                patch = mpatches.PathPatch(text_path, facecolor='grey', lw=0.04)
                ax.add_patch(patch)

            if c > 0:
                cur_y += y_step
        cur_y += y_step / 5
        cur_position = next_row

    for p in peaks:
        p.intensity = (p.intensity / total_intensity) * abs(origin_y - cur_y) * 8

    draw_peaklist(peaks, ax=ax)

    ax.set_ylim(cur_y, max(p.intensity for p in peaks) + 0.2)
    ax.set_xlim(start - 0.2, end + 0.2)
    ax.axes.get_yaxis().set_visible(False)
    return ax
Example #14
0
 def format_axes(self):
     draw_peaklist([], self.ax, pretty=True)
     self.ax.set_ylim(0, self.upper)
Example #15
0
 def _draw_whole_spectrum(self):
     ax = draw_peaklist(self.peaklist, lw=0.25, alpha=0.5)
     self.ax = ax
     return ax