END_SECTION START_SECTION(OpenSwathDataAccessHelper::convertToSpectrumPtr(sptr)) { MSSpectrum<> sptr,omsptr; Peak1D p1; p1.setIntensity(1.0f); p1.setMZ(2.0); Peak1D p2; p2.setIntensity(2.0f); p2.setMZ(10.0); Peak1D p3; p3.setIntensity(3.0f); p3.setMZ(30.0); TEST_STRING_EQUAL(sptr.getName(), "") sptr.setName("my_fancy_name"); sptr.push_back(p1); sptr.push_back(p2); sptr.push_back(p3); OpenSwath::SpectrumPtr p = OpenSwathDataAccessHelper::convertToSpectrumPtr(sptr); OpenSwathDataAccessHelper::convertToOpenMSSpectrum(p,omsptr); TEST_REAL_SIMILAR(p->getMZArray()->data[0],2.0); TEST_REAL_SIMILAR(p->getMZArray()->data[1],10.0); TEST_REAL_SIMILAR(p->getMZArray()->data[2],30.0); TEST_REAL_SIMILAR(p->getIntensityArray()->data[0],1.0f); TEST_REAL_SIMILAR(p->getIntensityArray()->data[1],2.0f); TEST_REAL_SIMILAR(p->getIntensityArray()->data[2],3.0f); }
/// integrate all masses in window bool integrateWindow(const OpenSwath::SpectrumPtr spectrum, double mz_start, double mz_end, double & mz, double & intensity, bool centroided) { #ifdef OPENMS_ASSERTIONS //check precondtion if (std::adjacent_find(spectrum->getMZArray()->data.begin(), spectrum->getMZArray()->data.end(), std::greater<double>()) != spectrum->getMZArray()->data.end()) { throw std::runtime_error("Precondition MZ vector needs to be sorted!"); } #endif intensity = 0; if (!centroided) { // get the weighted average for noncentroided data. // TODO this is not optimal if there are two peaks in this window (e.g. if the window is too large) mz = 0; intensity = 0; std::vector<double>::const_iterator mz_arr_end = spectrum->getMZArray()->data.end(); std::vector<double>::const_iterator int_it = spectrum->getIntensityArray()->data.begin(); // this assumes that the spectra are sorted! std::vector<double>::const_iterator mz_it = std::lower_bound(spectrum->getMZArray()->data.begin(), spectrum->getMZArray()->data.end(), mz_start); std::vector<double>::const_iterator mz_it_end = std::lower_bound(mz_it, mz_arr_end, mz_end); // also advance intensity iterator now std::iterator_traits< std::vector<double>::const_iterator >::difference_type iterator_pos = std::distance((std::vector<double>::const_iterator)spectrum->getMZArray()->data.begin(), mz_it); std::advance(int_it, iterator_pos); for (; mz_it != mz_it_end; ++mz_it, ++int_it) { intensity += (*int_it); mz += (*int_it) * (*mz_it); } if (intensity > 0.) { mz /= intensity; return true; } else { mz = -1; intensity = 0; return false; } } else { // not implemented throw "Not implemented"; } }
void OpenSwathDataAccessHelper::convertToOpenMSSpectrum(const OpenSwath::SpectrumPtr sptr, OpenMS::MSSpectrum<> & spectrum) { // recreate a spectrum from the data arrays! OpenSwath::BinaryDataArrayPtr mz_arr = sptr->getMZArray(); OpenSwath::BinaryDataArrayPtr int_arr = sptr->getIntensityArray(); spectrum.reserve(mz_arr->data.size()); for (Size i = 0; i < mz_arr->data.size(); i++) { Peak1D p; p.setMZ(mz_arr->data[i]); p.setIntensity(int_arr->data[i]); spectrum.push_back(p); } }
OpenSwath::SpectrumPtr prepareShiftedSpectrum() { OpenSwath::SpectrumPtr sptr = prepareSpectrum(); // shift the peaks by a fixed amount in ppm for (std::size_t i = 0; i < sptr->getMZArray()->data.size() / 2.0; i++) { sptr->getMZArray()->data[i] += sptr->getMZArray()->data[i] / 1000000 * 15; // shift first peak by 15 ppm } for (std::size_t i = sptr->getMZArray()->data.size() / 2.0; i < sptr->getMZArray()->data.size(); i++) { sptr->getMZArray()->data[i] += sptr->getMZArray()->data[i] / 1000000 * 10; // shift second peak by 10 ppm } return sptr; }
void ChromatogramExtractorAlgorithm::extractChromatograms(const OpenSwath::SpectrumAccessPtr input, std::vector< OpenSwath::ChromatogramPtr >& output, std::vector<ExtractionCoordinates> extraction_coordinates, double mz_extraction_window, bool ppm, String filter) { Size input_size = input->getNrSpectra(); if (input_size < 1) { return; } if (output.size() != extraction_coordinates.size()) { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Output and extraction coordinates need to have the same size"); } int used_filter = getFilterNr_(filter); // assert that they are sorted! if (std::adjacent_find(extraction_coordinates.begin(), extraction_coordinates.end(), ExtractionCoordinates::SortExtractionCoordinatesReverseByMZ) != extraction_coordinates.end()) { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Input to extractChromatogram needs to be sorted by m/z"); } //go through all spectra startProgress(0, input_size, "Extracting chromatograms"); for (Size scan_idx = 0; scan_idx < input_size; ++scan_idx) { setProgress(scan_idx); OpenSwath::SpectrumPtr sptr = input->getSpectrumById(scan_idx); OpenSwath::SpectrumMeta s_meta = input->getSpectrumMetaById(scan_idx); OpenSwath::BinaryDataArrayPtr mz_arr = sptr->getMZArray(); OpenSwath::BinaryDataArrayPtr int_arr = sptr->getIntensityArray(); std::vector<double>::const_iterator mz_start = mz_arr->data.begin(); std::vector<double>::const_iterator mz_end = mz_arr->data.end(); std::vector<double>::const_iterator mz_it = mz_arr->data.begin(); std::vector<double>::const_iterator int_it = int_arr->data.begin(); if (sptr->getMZArray()->data.size() == 0) continue; // go through all transitions / chromatograms which are sorted by // ProductMZ. We can use this to step through the spectrum and at the // same time step through the transitions. We increase the peak counter // until we hit the next transition and then extract the signal. for (Size k = 0; k < extraction_coordinates.size(); ++k) { double integrated_intensity = 0; double current_rt = s_meta.RT; if (extraction_coordinates[k].rt_end - extraction_coordinates[k].rt_start > 0 && (current_rt < extraction_coordinates[k].rt_start || current_rt > extraction_coordinates[k].rt_end) ) { continue; } if (used_filter == 1) { extract_value_tophat( mz_start, mz_it, mz_end, int_it, extraction_coordinates[k].mz, integrated_intensity, mz_extraction_window, ppm); } else if (used_filter == 2) { throw Exception::NotImplemented(__FILE__, __LINE__, __PRETTY_FUNCTION__); } // Time is first, intensity is second output[k]->binaryDataArrayPtrs[0]->data.push_back(current_rt); output[k]->binaryDataArrayPtrs[1]->data.push_back(integrated_intensity); } } endProgress(); }