OpenMS
Annotation1DPeakItem.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Johannes Veit $
6 // $Authors: Johannes Junker $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 
16 
17 #include <QtGui/QColor>
18 
19 namespace OpenMS
20 {
21 
25  template <class DataPoint> // e.g. Peak1D
27  public Annotation1DItem
28  {
29 public:
31  Annotation1DPeakItem(const DataPoint& peak_position, const QString& text, const QColor& color) :
32  Annotation1DItem(text), peak_position_(peak_position), position_(peak_position), color_(color)
33  {
34  }
35 
38 
40  ~Annotation1DPeakItem() override = default;
41 
42  // Docu in base class
43  void draw(Plot1DCanvas* const canvas, QPainter& painter, bool flipped = false) override
44  {
45  painter.save();
46 
47  painter.setPen(color_);
48 
49  QPoint position_widget, peak_position_widget;
50 
51  // translate units to pixel coordinates
52  canvas->dataToWidget(canvas->getMapper().map(position_), position_widget, flipped);
53  canvas->dataToWidget(canvas->getMapper().map(peak_position_), peak_position_widget, flipped);
54 
55  // pre-compute bounding box of text_item
56  const auto prebox = QApplication::fontMetrics().boundingRect(position_widget.x(), position_widget.y(), 0, 0, Qt::AlignCenter, getText());
57  // Shift position of the widget/text, so it sits 'on top' of the peak
58  // We can only do that there, since we do not know the state of 'flipped' in general
59  // Compute the delta in data-units, NOT pixels, since the shift (up/down, or even left/right) depends on state of 'flipped' and axis
60  const auto deltaXY_in_units = canvas->widgetToDataDistance(prebox.width(), prebox.height()).abs(); // abs() to make sure y axis is not negative
61  const auto delta_gravity_in_units = canvas->getGravitator().swap().gravitateZero(deltaXY_in_units); // only keep gravity dim
62  // recompute 'position_widget', shifting the text up by 1/2 box
63  canvas->dataToWidget(canvas->getMapper().map(position_) + delta_gravity_in_units / 2, position_widget, flipped);
64  // re-compute bounding box of text_item on with new position!
65  bounding_box_ = QApplication::fontMetrics().boundingRect(position_widget.x(), position_widget.y(), 0, 0, Qt::AlignCenter, getText());
66 
67 
68  // draw connection line between anchor point and current position if pixel coordinates differ significantly
69  if ((position_widget - peak_position_widget).manhattanLength() > 2)
70  {
71  QPointF border_point = GUIHelpers::intersectionPoint(bounding_box_, peak_position_widget);
72  if (bounding_box_.center() != border_point)
73  {
74  painter.save();
75  painter.setPen(Qt::DashLine);
76  painter.drawLine(peak_position_widget, border_point);
77  painter.restore();
78  }
79  }
80 
81  // some pretty printing
82  QString text = text_;
83  if (!text.contains("<\\")) // don't process HTML strings again
84  {
85  // extract ion index
86  {
87  QRegularExpression reg_exp(R"(([abcdwxyz])(\d+))");
88  QRegularExpressionMatch match = reg_exp.match(text);
89  if (text.indexOf(reg_exp) == 0) // only process if at the beginning of the string
90  {
91  text.replace(reg_exp, "\\1<sub>\\2</sub>");
92  }
93  else // protein-protein XL specific ion names
94  { // e.g. "[alpha|ci$y1]"
95  QRegularExpression reg_exp_xlms(R"((ci|xi)[$][abcxyz](\d+))");
96  auto match_pos = text.indexOf(reg_exp_xlms);
97  if ((match_pos == 6) || (match_pos == 7))
98  {
99  // set the match_pos to the position of the ion index
100  match_pos += 3; // skip "ci$" or "xi$"
101  ++match_pos; // skip the ion type (=captured(1))
102  QString charge_str = match.captured(2);
103  // put sub html tag around number
104  text = text.left(match_pos) + QString("<sub>") + charge_str + QString("</sub>") + text.right(text.size() - match_pos - charge_str.size());
105  }
106  }
107  }
108 
109  // common losses
110  text.replace("H2O1", "H<sub>2</sub>O"); // mind the order with H2O substitution
111  text.replace("H2O", "H<sub>2</sub>O");
112  text.replace("NH3", "NH<sub>3</sub>");
113  text.replace("H3N1", "NH<sub>3</sub>");
114  text.replace("C1H4O1S1", "H<sub>4</sub>COS"); // methionine sulfoxide loss
115 
116  // nucleotide XL related losses
117  text.replace("H3PO4", "H<sub>3</sub>PO<sub>4</sub>");
118  text.replace("HPO3", "HPO<sub>3</sub>");
119  text.replace("C3O", "C<sub>3</sub>O");
120 
121  // charge format: +z
122  QRegularExpression charge_rx(R"([\+|\-](\d+)$)");
123  int match_pos = text.indexOf(charge_rx);
124  if (match_pos > 0)
125  {
126  text = text.left(match_pos) + QString("<sup>") + text[match_pos] // + or -
127  + charge_rx.match(text).captured(1) + QString("</sup>"); // charge
128  }
129 
130  // charge format: z+
131  charge_rx = QRegularExpression(R"((\d+)[\+|\-]$)");
132  match_pos = text.indexOf(charge_rx);
133  if (match_pos > 0)
134  {
135  auto charge_match = charge_rx.match(text).captured(1);
136  text = text.left(match_pos) + QString("<sup>") + charge_match // charge
137  + text[match_pos + charge_match.size()] + QString("</sup>"); // + or -
138  }
139 
140  text.replace(QRegularExpression(R"(\+\+$)"), "<sup>2+</sup>");
141  text.replace(QRegularExpression(R"(\+$)"), "");
142  text.replace(QRegularExpression(R"(\-\-$)"), "<sup>2-</sup>");
143  text.replace(QRegularExpression(R"(\-$)"), "");
144  }
145 
146  text = "<font color=\"" + color_.name() + "\">" + text + "</font>";
147 
148  // draw html text
149  {
150  QTextDocument td;
151  td.setHtml(text);
152  painter.save();
153  double w = td.size().width();
154  double h = td.size().height();
155  painter.translate(position_widget.x() - w / 2, position_widget.y() - h / 2);
156  td.drawContents(&painter);
157  painter.restore();
158  }
159 
160  if (selected_)
161  {
162  drawBoundingBox_(painter);
163  }
164 
165  painter.restore();
166  }
167 
168  // Docu in base class
169  void move(const PointXYType delta, const Gravitator& /*gr*/, const DimMapper<2>& dim_mapper) override
170  {
171  auto pos_xy = dim_mapper.map(position_);
172  pos_xy += delta;
173  dim_mapper.fromXY(pos_xy, position_);
174  }
175 
177  void setPosition(const DataPoint& position)
178  {
179  position_ = position;
180  }
181 
183  const DataPoint& getPosition() const
184  {
185  return position_;
186  }
187 
189  const DataPoint& getPeakPosition() const
190  {
191  return peak_position_;
192  }
193 
194  // Docu in base class
195  void ensureWithinDataRange(Plot1DCanvas* const canvas, const int layer_index) override
196  {
197  canvas->pushIntoDataRange(position_, layer_index);
198  }
199 
201  void setColor(const QColor& color)
202  {
203  color_ = color;
204  }
205 
207  const QColor& getColor() const
208  {
209  return color_;
210  }
211 
214  {
215  // add new fragment annotation
216  QString peak_anno = this->getText().trimmed();
217 
218  // check for newlines in the label and only continue with the first line for charge determination
219  peak_anno.remove('\r');
220  QStringList lines = peak_anno.split('\n', Qt::SkipEmptyParts);
221  if (lines.size() > 1)
222  {
223  peak_anno = lines[0];
224  }
225 
226  // regular expression for a charge at the end of the annotation
227  QRegularExpression reg_exp(R"(([\+|\-]\d+)$)");
228 
229  // read charge and text from annotation item string
230  // we support two notations for the charge suffix: '+2' or '++'
231  // cut and convert the trailing + or - to a proper charge
232  int match_pos = peak_anno.indexOf(reg_exp);
233  int tmp_charge(0);
234  if (match_pos >= 0)
235  {
236  tmp_charge = reg_exp.match(peak_anno).captured(1).toInt();
237  peak_anno = peak_anno.left(match_pos);
238  }
239  else
240  {
241  // count number of + and - in suffix (e.g., to support "++" as charge 2 annotation)
242  int plus(0), minus(0);
243 
244  for (int p = (int)peak_anno.size() - 1; p >= 0; --p)
245  {
246  if (peak_anno[p] == '+')
247  {
248  ++plus;
249  continue;
250  }
251  else if (peak_anno[p] == '-')
252  {
253  ++minus;
254  continue;
255  }
256  else // not '+' or '-'?
257  {
258  if (plus > 0 && minus == 0) // found pluses?
259  {
260  tmp_charge = plus;
261  peak_anno = peak_anno.left(peak_anno.size() - plus);
262  break;
263  }
264  else if (minus > 0 && plus == 0) // found minuses?
265  {
266  tmp_charge = -minus;
267  peak_anno = peak_anno.left(peak_anno.size() - minus);
268  break;
269  }
270  break;
271  }
272  }
273  }
274 
276  fa.charge = tmp_charge;
277  fa.mz = this->getPeakPosition().getMZ();
278  fa.intensity = this->getPeakPosition().getIntensity();
279  if (lines.size() > 1)
280  {
281  peak_anno.append("\n").append(lines[1]);
282  }
283  fa.annotation = peak_anno;
284 
285  return fa;
286  }
287 
288  // Docu in base class
289  Annotation1DItem* clone() const override
290  {
291  return new Annotation1DPeakItem(*this);
292  }
293 
294  protected:
296  DataPoint peak_position_;
297 
299  DataPoint position_;
300 
302  QColor color_;
303  };
304 } // namespace OpenMS
An abstract class acting as an interface for the different 1D annotation items.
Definition: Annotation1DItem.h:36
QRectF bounding_box_
The current bounding box of this item on the canvas where it has last been drawn.
Definition: Annotation1DItem.h:83
QString text_
The displayed text.
Definition: Annotation1DItem.h:89
void drawBoundingBox_(QPainter &painter)
Draws the bounding_box_.
const QString & getText() const
Returns the text of the item.
bool selected_
Determines whether this item is currently selected on the canvas.
Definition: Annotation1DItem.h:86
A peak annotation item.
Definition: Annotation1DPeakItem.h:28
const QColor & getColor() const
Returns the color of the label.
Definition: Annotation1DPeakItem.h:207
Annotation1DItem * clone() const override
Creates a copy of the item on the heap and returns a pointer.
Definition: Annotation1DPeakItem.h:289
~Annotation1DPeakItem() override=default
Destructor.
const DataPoint & getPosition() const
Returns the position of the label (peak)
Definition: Annotation1DPeakItem.h:183
void draw(Plot1DCanvas *const canvas, QPainter &painter, bool flipped=false) override
Draws the item on painter.
Definition: Annotation1DPeakItem.h:43
const DataPoint & getPeakPosition() const
Returns the position of the annotated peak.
Definition: Annotation1DPeakItem.h:189
QColor color_
The color of the label.
Definition: Annotation1DPeakItem.h:302
Annotation1DPeakItem(const Annotation1DPeakItem &rhs)=default
Copy constructor.
void ensureWithinDataRange(Plot1DCanvas *const canvas, const int layer_index) override
Ensures that the item has coordinates within the visible area of the canvas.
Definition: Annotation1DPeakItem.h:195
PeptideHit::PeakAnnotation toPeakAnnotation() const
Convert the 'text()' to a Peptide::PeakAnnotation.
Definition: Annotation1DPeakItem.h:213
DataPoint peak_position_
The position of the anchor (e.g. the Peak1D)
Definition: Annotation1DPeakItem.h:296
void setColor(const QColor &color)
Set the color of the label.
Definition: Annotation1DPeakItem.h:201
Annotation1DPeakItem(const DataPoint &peak_position, const QString &text, const QColor &color)
Constructor.
Definition: Annotation1DPeakItem.h:31
void setPosition(const DataPoint &position)
Sets the position of the label.
Definition: Annotation1DPeakItem.h:177
void move(const PointXYType delta, const Gravitator &, const DimMapper< 2 > &dim_mapper) override
Moves the item on the drawing canvas; behavior depends on item type and is implemented in the subclas...
Definition: Annotation1DPeakItem.h:169
DataPoint position_
The position of the label (e.g. the Peak1D)
Definition: Annotation1DPeakItem.h:299
DPosition & abs() noexcept
Make all dimension values positive.
Definition: DPosition.h:115
void fromXY(const DRange< N_DIM > &in, RangeManager< Ranges... > &output) const
Definition: DimMapper.h:709
Point map(const T &data) const
convert an OpenMS datatype (such as Feature) to an N_DIM-dimensional point
Definition: DimMapper.h:673
Manipulates X or Y component of points in the X-Y plane, by assuming one axis (either X or Y axis) ha...
Definition: Plot1DCanvas.h:42
QPoint gravitateZero(QPoint p) const
Definition: Plot1DCanvas.h:204
Gravitator swap() const
Swap gravity axis (from X to Y, or vice versa)
Definition: Plot1DCanvas.h:108
Canvas for visualization of one or several spectra.
Definition: Plot1DCanvas.h:295
void dataToWidget(const DPosition< 2 > &peak, QPoint &point, bool flipped=false)
For convenience - calls dataToWidget.
const Gravitator & getGravitator() const
Get gravity manipulation object to apply gravity to points.
Definition: Plot1DCanvas.h:504
void pushIntoDataRange(T &data_point, const int layer_index)
Pushes a data point back into the valid data range of the current layer area. Useful for annotation i...
Definition: Plot1DCanvas.h:434
PointXYType widgetToDataDistance(double x, double y)
compute distance in data coordinates (unit axis as shown) when moving x/y pixel in chart/widget coord...
Definition: Plot1DCanvas.h:412
const DimMapper< 2 > & getMapper() const
Get Mapper to translate between values for axis (X/Y) and units (m/z, RT, intensity,...
const double h
Definition: Constants.h:141
QPointF intersectionPoint(const QRectF &rect, const QPointF &p)
Find the point on a rectangle where a ray/line from a point p to its center would intersect at.
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Contains annotations of a peak.
Definition: PeptideHit.h:58
double intensity
Definition: PeptideHit.h:62
double mz
Definition: PeptideHit.h:61
String annotation
Definition: PeptideHit.h:59
int charge
Definition: PeptideHit.h:60