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);
}
Exemple #2
0
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 prepareSpectrum()
{
  OpenSwath::SpectrumPtr sptr = (OpenSwath::SpectrumPtr)(new OpenSwath::Spectrum);
  std::vector<OpenSwath::BinaryDataArrayPtr> binaryDataArrayPtrs;
  OpenSwath::BinaryDataArrayPtr data1 = (OpenSwath::BinaryDataArrayPtr)(new OpenSwath::BinaryDataArray);
  OpenSwath::BinaryDataArrayPtr data2 = (OpenSwath::BinaryDataArrayPtr)(new OpenSwath::BinaryDataArray);

  static const double arr1[] = {
    10, 20, 50, 100, 50, 20, 10, // peak at 499 -> 260-20 = 240 intensity within 0.05 Th
    3, 7, 15, 30, 15, 7, 3,      // peak at 500 -> 80-6 = 74 intensity within 0.05 Th
    1, 3, 9, 15, 9, 3, 1,        // peak at 501 -> 41-2 = 39 intensity within 0.05 Th
    3, 9, 3,                     // peak at 502 -> 15 intensity within 0.05 Th

    10, 20, 50, 100, 50, 20, 10, // peak at 600 -> 260-20 = 240 intensity within 0.05 Th
    3, 7, 15, 30, 15, 7, 3,      // peak at 601 -> 80-6 = 74 intensity within 0.05 Th
    1, 3, 9, 15, 9, 3, 1,        // peak at 602 -> sum([ 9, 15, 9, 3, 1]) = 37 intensity within 0.05 Th
    3, 9, 3                      // peak at 603
  };
  std::vector<double> intensity (arr1, arr1 + sizeof(arr1) / sizeof(arr1[0]) );
  static const double arr2[] = {
    498.97, 498.98, 498.99, 499.0, 499.01, 499.02, 499.03,
    499.97, 499.98, 499.99, 500.0, 500.01, 500.02, 500.03,
    500.97, 500.98, 500.99, 501.0, 501.01, 501.02, 501.03,
    501.99, 502.0, 502.01,

    599.97, 599.98, 599.99, 600.0, 600.01, 600.02, 600.03,
    600.97, 600.98, 600.99, 601.0, 601.01, 601.02, 601.03,
    // note that this peak at 602 is special since it is integrated from 
    // [(600+2*1.0033548) - 0.025, (600+2*1.0033548)  + 0.025] = [601.9817096 to 602.0317096]
    601.97, 601.98, 601.99, 602.0, 602.01, 602.02, 602.03,
    602.99, 603.0, 603.01
  };
  std::vector<double> mz (arr2, arr2 + sizeof(arr2) / sizeof(arr2[0]) );
  data1->data = mz;
  data2->data = intensity;

  sptr->setMZArray(data1);
  sptr->setIntensityArray( data2 );
  return sptr;
}
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;
}
  /// integrate all masses in window
  bool integrateWindow(const OpenSwath::SpectrumPtr spectrum, double mz_start,
                       double mz_end, double & mz, double & intensity, bool centroided)
  {
    //check precondtion
    OPENSWATH_PRECONDITION( std::adjacent_find(spectrum->getMZArray()->data.begin(),
            spectrum->getMZArray()->data.end(), std::greater<double>()) == spectrum->getMZArray()->data.end(),
          "Precondition violated: m/z vector needs to be sorted!" )

    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 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();
  }