ProteoWizard
IPrecursorMaskCodec.hpp
Go to the documentation of this file.
1 //
2 // $Id: IPrecursorMaskCodec.hpp 10730 2017-04-10 15:45:03Z chambm $
3 //
4 //
5 // Original author: Jarrett Egertson <jegertso .@. uw.edu>
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 
20 #ifndef _IPRECURSORMASKCODEC_HPP
21 #define _IPRECURSORMASKCODEC_HPP
22 
23 #include "DemuxTypes.hpp"
24 #include <boost/format.hpp>
25 
26 namespace pwiz{
27 namespace analysis{
28 
29  inline double precursor_upper_offset(const msdata::Precursor& p)
30  {
31  auto upperOffsetParam = p.isolationWindow.cvParam(cv::MS_isolation_window_upper_offset);
32  if (upperOffsetParam.value.empty())
33  throw std::runtime_error("precursor_upper_offset() No isolation window upper offset m/z specified");
34  double upperOffset = upperOffsetParam.valueAs<double>();
35  if (upperOffset <= 0.0)
36  throw std::runtime_error("precursor_upper_offset() Positive values expected for isolation window m/z offsets");
37  return upperOffset;
38  }
39 
40  inline double precursor_lower_offset(const msdata::Precursor& p)
41  {
42  auto lowerOffsetParam = p.isolationWindow.cvParam(cv::MS_isolation_window_lower_offset);
43  if (lowerOffsetParam.value.empty())
44  throw std::runtime_error("precursor_lower_offset() No isolation window lower offset m/z specified");
45  double lowerOffset = lowerOffsetParam.valueAs<double>();
46  if (lowerOffset <= 0.0)
47  throw std::runtime_error("precursor_lower_offset() Positive values expected for isolation window m/z offsets");
48  return lowerOffset;
49  }
50 
51  inline double precursor_target(const msdata::Precursor& p)
52  {
53  auto targetParam = p.isolationWindow.cvParam(cv::MS_isolation_window_target_m_z);
54  if (targetParam.value.empty())
55  throw std::runtime_error("precursor_target() No isolation window target m/z specified");
56  return targetParam.valueAs<double>();
57  }
58 
59  inline double precursor_mz_low(const msdata::Precursor& p)
60  {
62  }
63 
64  inline double precursor_mz_high(const msdata::Precursor& p)
65  {
67  }
68 
69  inline double precursor_iso_center(const msdata::Precursor& p)
70  {
71  double target = precursor_target(p);
72  double mzLow = target - precursor_lower_offset(p);
73  double mzHigh = target + precursor_upper_offset(p);
74  return (mzLow + mzHigh) / 2.0;
75  }
76 
77  inline double precursor_iso_width(const msdata::Precursor& p)
78  {
80  }
81 
82  inline std::string prec_to_string(const msdata::Precursor& p)
83  {
84  return str(boost::format("%.2f") % precursor_iso_center(p));
85  }
86 
87  inline bool stringToFloatCompare(std::string i, std::string j){ return stof(i) < stof(j); }
88 
89  // TODO Rework the MZHash system to be strongly typed. Implicit conversion of uint to float and vice-versa is possible.
90  typedef uint64_t MZHash;
91 
92  /// A method of hashing an isolation window to a unique long value
93  /// mz is and m/z of a unique point in the isolation window, such as
94  /// the lower bound, upper bound, or center. This value is multiplied
95  /// by 100000000 and rounded to convert the isolation m/z to an integer that
96  /// is used as the hash. This creates an effective fuzzy window of
97  /// +/- 5e-8 m/z. For example: a window with m/z 500.49 would
98  /// be hashed to 50049000000.
100  {
101  public:
102 
103  /// Hash a floating-point m/z value to an integer
104  static MZHash Hash(double mz)
105  {
106  auto mult = mz * 100000000.0;
107  auto rounded = llround(mult);
108  return static_cast<MZHash>(rounded);
109  }
110 
111  /// Unhash an integer to a floating-point m/z value
112  static double UnHash(MZHash hashed)
113  {
114  return hashed / 100000000.0;
115  }
116  };
117 
118  /// A container for describing the isolation windows that are dedicated to columns of the design matrix for demultiplexing.
119  ///
120  /// Ideally, a ProteoWizard Precursor container could be used instead with manipulation of its internal data since a demultiplexed spectrum
121  /// is in many ways able to be thought of as an isolated spectrum with narrower isolation boundaries. However, this is used as a slimmer
122  /// container in favor of the existing ProteoWizard Precursor because in the case of overlapping spectra the Precursor object would have
123  /// to be copied and manipulated to split the isolation ranges.
124  struct DemuxWindow
125  {
126  MZHash mzLow; ///< Start m/z of the window range
127 
128  MZHash mzHigh; ///< End m/z of the window range
129 
130  /// Constructs a DemuxWindow from a Precursor by using its isolation window.
131  explicit DemuxWindow(const msdata::Precursor& p)
132  {
133  double target = precursor_target(p);
134  mzLow = IsoWindowHasher::Hash(target - precursor_lower_offset(p));
135  mzHigh = IsoWindowHasher::Hash(target + precursor_upper_offset(p));
136  }
137 
138  /// Constructs a DemuxWindow for a given mass range.
139  explicit DemuxWindow(MZHash mzLow, MZHash mzHigh) : mzLow(mzLow), mzHigh(mzHigh)
140  {
141  }
142 
143  /// Isolation windows are sorted by their start value
144  bool operator<(const DemuxWindow& rhs) const { return this->mzLow < rhs.mzLow; }
145 
146  /// Can be used to find whether the mass range of another DemuxWindow is a subset of this one.
147  bool Contains(const DemuxWindow& inner) const
148  {
149  return inner.mzLow >= this->mzLow && inner.mzHigh <= this->mzHigh;
150  }
151 
152  /// Used to find whether a window's center is contained within this window
153  bool ContainsCenter(const DemuxWindow& inner) const
154  {
155  auto center = static_cast<MZHash>(llround(inner.mzLow + (inner.mzHigh - inner.mzLow) / 2.0));
156  return center >= this->mzLow && center <= this->mzHigh;
157  }
158 
159  /// Can be used to find whether two windows are identical within the error of the hash
160  bool operator==(const DemuxWindow& rhs) const { return rhs.Contains(*this) && this->Contains(rhs); }
161 
162  /// Can be used to find whether two windows are identical within the error of the hash
163  bool operator!=(const DemuxWindow& rhs) const { return !(*this == rhs); }
164  };
165 
166  /// A container that wraps DemuxWindow to preserve the full precision window boundaries
168  {
169 
170  /// Constructs an IsolationWindow from a Precursor
171  explicit IsolationWindow(const msdata::Precursor& p) :
172  lowMz(precursor_mz_low(p)), highMz(precursor_mz_high(p)), demuxWindow(p) {}
173 
174  /// Constructs an IsolationWindow from a given mass range
175  IsolationWindow(double mzLow, double mzHigh) :
176  lowMz(mzLow), highMz(mzHigh), demuxWindow(IsoWindowHasher::Hash(mzLow), IsoWindowHasher::Hash(mzHigh)) {}
177 
178  double lowMz; ///< Full precision lower m/z bound
179 
180  double highMz; ///< Full precision upper m/z bound
181 
182  /// Set of isolation window boundaries that provides useful operations for sorting and comparing
183  /// different isolation windows.
185 
186  /// Isolation windows are sorted by their start value
187  bool operator<(const IsolationWindow& rhs) const { return this->demuxWindow < rhs.demuxWindow; }
188  };
189 
190  /// Interface for generating and accessing precursor masks for a demultiplexing scheme.
192  {
193  public:
194 
195  /// Shared pointer definition
196  typedef boost::shared_ptr<IPrecursorMaskCodec> ptr;
197 
198  /// Constant shared pointer definition
199  typedef boost::shared_ptr<const IPrecursorMaskCodec> const_ptr;
200 
201  /// Generates a design matrix row describing which precursor isolation windows are present in the given spectrum. This row can be weighted by
202  /// a given scalar.
203  /// @param[in] sPtr Multiplexed spectrum from which to extract precursor windows.
204  /// @param[in] weight Scalar value by which to weight the resulting design matrix vector.
205  /// This weighting is a simple scalar multiplication of the vector.
206  /// @return Design matrix row describing which precursor isolation windows are present in the given spectrum.
207  virtual Eigen::VectorXd GetMask(msdata::Spectrum_const_ptr sPtr, double weight = 1.0) const = 0;
208 
209  /// Generates a design matrix row describing which precursor isolation windows are present in the given spectrum and places it into the specified
210  /// row of the user-provided matrix.
211  /// @param[in] sPtr Multiplexed spectrum from which to extract precursor windows.
212  /// @param[out] m Matrix in which to place the design vector.
213  /// @param[in] rowNum Row of the matrix in which to place the design vector corresponding to the given spectrum.
214  /// @param[in] weight Scalar value by which to weight the resulting design matrix vector.
215  /// This weighting is a simple scalar multiplication of the vector.
216  /// \pre Out array must be same size as GetDemuxBlockSize()
217  virtual void GetMask(msdata::Spectrum_const_ptr sPtr, DemuxTypes::MatrixType& m, size_t rowNum, double weight = 1.0) const = 0;
218 
219  /// Identifies the precursor windows within a spectrum and returns the indices to the design matrix columns corresponding to those windows
220  /// @param[in] sPtr Multiplexed spectrum from which to extract precursor windows.
221  /// @param[out] indices Indices of the design matrix columns that correspond to the precursor windows in the given spectrum.
222  virtual void SpectrumToIndices(msdata::Spectrum_const_ptr sPtr, std::vector<size_t>& indices) const = 0;
223 
224  /// Returns the precursor window for a given index.
225  /// @param[in] i Index of the column of the design matrix corresponding to the precursor window.
226  /// @return A DemuxWindow describing the m/z range of the precursor window.
227  virtual IsolationWindow GetIsolationWindow(size_t i) const = 0;
228 
229  /// Returns the total number of demux'd precursor windows. This is the number of possible indices returned by SpectrumToIndices().
230  virtual size_t GetNumDemuxWindows() const = 0;
231 
232  /// Returns the number of spectra required to cover all precursor isolation windows
233  virtual int GetSpectraPerCycle() const = 0;
234 
235  /// Returns the number of precursor isolations per spectrum. This is verified to be constant for all spectra.
236  virtual int GetPrecursorsPerSpectrum() const = 0;
237 
238  /// Returns the number of overlap repeats per cycle. So for no overlap, this returns 1. For an overlap that splits each precursor in two, this returns 2. Etc.
239  virtual int GetOverlapsPerCycle() const = 0;
240 
241  /// Returns the number of windows required to demultiplex
242  virtual size_t GetDemuxBlockSize() const = 0;
243 
244  /// Returns a descriptor of the processing done by this PrecursorMaskCodec.
245  /// WARNING: It is important that this gives a string containing "Demultiplexing" in order for SpectrumWorkerThreads.cpp to handle demultiplexing properly.
246  /// @return The processing method performed by this PrecursorMaskCodec
247  virtual msdata::ProcessingMethod GetProcessingMethod() const = 0;
248 
249  virtual ~IPrecursorMaskCodec() {}
250  };
251 
252 
253 
254 } // namespace analysis
255 } // namespace pwiz
256 
257 #endif // _IPRECURSORMASKCODEC_HPP
A method of hashing an isolation window to a unique long value mz is and m/z of a unique point in the...
IsolationWindow(const msdata::Precursor &p)
Constructs an IsolationWindow from a Precursor.
bool stringToFloatCompare(std::string i, std::string j)
MS_isolation_window_lower_offset
isolation window lower offset: The extent of the isolation window in m/z below the isolation window t...
Definition: cv.hpp:3031
boost::shared_ptr< IPrecursorMaskCodec > ptr
Shared pointer definition.
IsolationWindow(double mzLow, double mzHigh)
Constructs an IsolationWindow from a given mass range.
The method of precursor ion selection and activation.
Definition: MSData.hpp:310
A container for describing the isolation windows that are dedicated to columns of the design matrix f...
bool operator==(const DemuxWindow &rhs) const
Can be used to find whether two windows are identical within the error of the hash.
A container that wraps DemuxWindow to preserve the full precision window boundaries.
bool ContainsCenter(const DemuxWindow &inner) const
Used to find whether a window&#39;s center is contained within this window.
double precursor_iso_center(const msdata::Precursor &p)
double precursor_mz_high(const msdata::Precursor &p)
MZHash mzHigh
End m/z of the window range.
IsolationWindow isolationWindow
this element captures the isolation (or &#39;selection&#39;) window configured to isolate one or more precurs...
Definition: MSData.hpp:325
double precursor_lower_offset(const msdata::Precursor &p)
double precursor_target(const msdata::Precursor &p)
double precursor_upper_offset(const msdata::Precursor &p)
std::string prec_to_string(const msdata::Precursor &p)
Interface for generating and accessing precursor masks for a demultiplexing scheme.
Description of the default peak processing method. This element describes the base method used in the...
Definition: MSData.hpp:253
DemuxWindow(const msdata::Precursor &p)
Constructs a DemuxWindow from a Precursor by using its isolation window.
bool operator<(const DemuxWindow &rhs) const
Isolation windows are sorted by their start value.
static MZHash Hash(double mz)
Hash a floating-point m/z value to an integer.
double lowMz
Full precision lower m/z bound.
DemuxWindow(MZHash mzLow, MZHash mzHigh)
Constructs a DemuxWindow for a given mass range.
MS_isolation_window_target_m_z
isolation window target m/z: The primary or reference m/z about which the isolation window is defined...
Definition: cv.hpp:3028
double mz(double neutralMass, int protonDelta, int electronDelta=0, int neutronDelta=0)
Definition: Ion.hpp:78
double precursor_mz_low(const msdata::Precursor &p)
PWIZ_API_DECL Reader_Bruker_Format format(const std::string &path)
returns Bruker format of &#39;path&#39; if it is a Bruker directory; otherwise returns empty string ...
bool Contains(const DemuxWindow &inner) const
Can be used to find whether the mass range of another DemuxWindow is a subset of this one...
boost::shared_ptr< const IPrecursorMaskCodec > const_ptr
Constant shared pointer definition.
Matrix< DemuxScalar, Dynamic, Dynamic > MatrixType
Definition: DemuxTypes.hpp:38
bool operator!=(const DemuxWindow &rhs) const
Can be used to find whether two windows are identical within the error of the hash.
boost::shared_ptr< const msdata::Spectrum > Spectrum_const_ptr
Definition: DemuxTypes.hpp:29
MZHash mzLow
Start m/z of the window range.
double precursor_iso_width(const msdata::Precursor &p)
double highMz
Full precision upper m/z bound.
static double UnHash(MZHash hashed)
Unhash an integer to a floating-point m/z value.
MS_isolation_window_upper_offset
isolation window upper offset: The extent of the isolation window in m/z above the isolation window t...
Definition: cv.hpp:3034
DemuxWindow demuxWindow
Set of isolation window boundaries that provides useful operations for sorting and comparing differen...
bool operator<(const IsolationWindow &rhs) const
Isolation windows are sorted by their start value.