Ejemplo n.º 1
0
bool
waveform::fft::lowpass_filter( adcontrols::MassSpectrum& ms, double freq )
{
    if ( ms.isCentroid() )
        return false;

    size_t totalSize = ms.size();
	(void)totalSize;
	size_t N = 32;
    while ( N < ms.size() )
		N *= 2;
	const size_t NN = ms.size();
	double sampInterval = ms.getMSProperty().getSamplingInfo().fSampInterval(); // seconds
    if ( sampInterval == 0 )
        sampInterval = ( ms.getTime( ms.size() - 1 ) - ms.getTime( 0 ) ) / ms.size();
    const double T = N * sampInterval;  // time full scale in seconds.  Freq = n/T (Hz)
    // power spectrum has N/2 points and is n/T Hz horizontal axis  := data[N/2] = (N/2)/T Hz
    size_t cutoff = size_t( T * freq );

	adportable::array_wrapper<const double> pIntens( ms.getIntensityArray(), N );

	std::vector< std::complex<double> > spc( N );
	std::vector< std::complex<double> > fft( N );
	size_t n;
	for ( n = 0; n < N && n < NN; ++n )
		spc[ n ] = std::complex<double>( pIntens[ n ] );
	while ( n < N )
		spc[ n++ ] = pIntens[ NN - 1 ];

	adportable::fft::fourier_transform( fft, spc, false );
    // appodization
    for ( size_t i = cutoff; i < N - cutoff; ++i )
        fft[ i ] = 0;
    //adportable::fft::apodization( N/2 - N/16, N / 16, fft );
	adportable::fft::fourier_transform( spc, fft, true );

	std::vector<double> data( N );
	for ( size_t i = 0; i < NN; ++i )
		data[ i ] = spc[i].real();

	ms.setIntensityArray( &data[0] );

	return true;
}
Ejemplo n.º 2
0
static void
calibresult_validation( const adcontrols::MSCalibrateResult& res
                        , const adcontrols::MassSpectrum& centroid
                        , double threshold )
{
    const adcontrols::MSReferences& ref = res.references();
    const adcontrols::MSAssignedMasses& assigned = res.assignedMasses();
    
    std::ofstream of( "massassign.txt" );
    of << "#\tm/z(observed)\ttof(us)\tintensity\t\tformula,\tm/z(exact)\tm/z(calibrated)\terror(mDa)" << std::endl;
    
    adcontrols::MSReferences::vector_type::const_iterator refIt = ref.begin();
    for ( adcontrols::MSAssignedMasses::vector_type::const_iterator it = assigned.begin(); it != assigned.end(); ++it, ++refIt ) {
        const adcontrols::MSAssignedMass& a = *it;
        
        std::string formula = adportable::string::convert( a.formula() );
        of << std::setprecision(8)
           << std::setw(4) << a.idMassSpectrum() << "\t" // id
           << std::setw(15) << std::fixed << centroid.getMass( a.idMassSpectrum() ) << "\t"           // m/z(observed)
           << std::scientific << centroid.getTime( a.idMassSpectrum() ) << "\t"      // tof
           << std::fixed << std::setprecision( 0 ) << centroid.getIntensity( a.idMassSpectrum() ) << "\t" // intensity
           << formula << "\t"
           << std::setprecision(8) << std::fixed   << it->exactMass() << "\t"                             // mass(exact)
           << std::fixed   << a.mass() << "\t"                                    // m/z(calibrated)
           << std::setprecision(1) << ( a.mass() - it->exactMass() ) * 1000 << "\t"  // error(mDa)
           << ( it->enable() ? "used" : "not used" ) 
           << std::endl;
    }
    const std::vector<double>& coeffs = res.calibration().coeffs();
    
    of << "#--------------------------- Calibration coefficients: " << std::endl;
    for ( size_t i = 0; i < coeffs.size(); ++i )
        of << std::scientific << std::setprecision(14) << coeffs[i] << std::endl;
    
    of << "#--------------------------- centroid peak list (#,mass,intensity)--------------------------" << std::endl;
    
    adcontrols::MSReferences::vector_type::const_iterator it = res.references().begin();
    for ( size_t i = 0; i < centroid.size(); ++i ) {
        if ( centroid.getIntensity( i ) > threshold ) {
            
            double mq = adcontrols::MSCalibration::compute( res.calibration().coeffs(), centroid.getTime( i ) );
            double mass = mq * mq;
            
            double error = 0;
            if ( it != res.references().end() && std::abs( it->exactMass() - mass ) < 0.2 ) {
                error = ( it->exactMass() - mass ) * 1000; // mDa
                ++it;
            }
            of << i << "\t"
               << std::setprecision(8) << std::fixed << centroid.getMass( i ) << "\t"
               << std::setprecision(8) << mass << "\t"
                    << std::setprecision(1) << centroid.getIntensityArray()[i] << std::endl;
        }
    }
}
Ejemplo n.º 3
0
        void operator () ( const adcontrols::MassSpectrum& data
                           , adportable::counting::counting_result& result
                           , std::vector< double >& processed ) {

            assert ( method.algo_ == adcontrols::threshold_method::Differential );
            assert ( findPositive == ( method.slope == adcontrols::threshold_method::CrossUp ) );

            double level = method.threshold_level;

            adportable::counting::peak_finder< findPositive > finder;

            finder( data.getIntensityArray(), data.getIntensityArray() + data.size(), result.indices2(), level );
        }
Ejemplo n.º 4
0
//virtual
bool
datafile::getSpectrum( int fcn, size_t pos, adcontrols::MassSpectrum& ms, uint32_t objId ) const
{
	(void)fcn;

	try {
		EDAL::IMSSpectrumCollectionPtr pSpectra = pAnalysis_->GetMSSpectrumCollection();
		EDAL::IMSSpectrumPtr pSpectrum = pSpectra->GetItem( long(pos) + 1 ); // 1-origin

		if ( pSpectrum->Polarity == EDAL::SpectrumPolarity::IonPolarity_Negative )
			ms.setPolarity( adcontrols::MS_POLARITY::PolarityNegative );
		else if ( pSpectrum->Polarity == EDAL::SpectrumPolarity::IonPolarity_Positive )
			ms.setPolarity( adcontrols::MS_POLARITY::PolarityPositive );
		else
			ms.setPolarity( adcontrols::MS_POLARITY::PolarityIndeterminate );

		adcontrols::MSProperty prop = ms.getMSProperty();
		prop.setTimeSinceInjection( static_cast< unsigned long >( pSpectrum->RetentionTime /* sec */ * 1.0e6 ) ); // usec
        ms.setMSProperty( prop ); // <- end of prop set

		_variant_t vMasses, vIntens;
        if ( objId <= 1 ) {
			pSpectrum->GetMassIntensityValues( EDAL::SpectrumType_Profile, &vMasses, &vIntens );
			ms.setCentroid( adcontrols::CentroidNone );  // profile
        } else { // objId should be 2
			pSpectrum->GetMassIntensityValues( EDAL::SpectrumType_Line, &vMasses, &vIntens );
			ms.setCentroid( adcontrols::CentroidNative );
		}

		SafeArray sa_masses( vMasses );
        ms.resize( sa_masses.size() );
        ms.setMassArray( reinterpret_cast< const double *>( sa_masses.p() ) );
    
		SafeArray sa_intensities( vIntens );
        ms.setIntensityArray( reinterpret_cast< const double *>( sa_intensities.p() ) );

        ms.setAcquisitionMassRange( ms.getMass( 0 ), ms.getMass( ms.size() - 1 ) );

		return true;
	} catch(_com_error& ex ) {
		ADERROR() << std::wstring( ex.ErrorMessage() );
		return false;
	}
	return false;
}
Ejemplo n.º 5
0
adcontrols::translate_state
DataInterpreter::translate_profile( adcontrols::MassSpectrum& ms
                                    , const char * data, size_t dsize
                                    , const char * meta, size_t msize
                                    , const adcontrols::MassSpectrometer& spectrometer
                                    , size_t idData ) const
{
	(void)idData;
    adportable::debug(__FILE__, __LINE__) << "translate_profile( dsize=" << dsize << ", msize=" << msize << ")";
    import_profile profile;
    import_continuum_massarray ma;
    
    const batchproc::MassSpectrometer* pSpectrometer = dynamic_cast< const batchproc::MassSpectrometer * >( &spectrometer );
    if ( pSpectrometer == 0 )
        return adcontrols::translate_error;

    if ( adportable::bzip2::is_a( data, dsize ) ) {

        std::string ar;
        adportable::bzip2::decompress( ar, data, dsize );
        adportable::debug(__FILE__, __LINE__) << "translate_profile deserialize import_profile w/ decompress";
        if ( ! adportable::serializer< import_profile >::deserialize( profile, ar.data(), ar.size() ) )
            return adcontrols::translate_error;

    } else {
        adportable::debug(__FILE__, __LINE__) << "translate_profile deserialize import_profile w/o decompress";
        if ( ! adportable::serializer< import_profile >::deserialize( profile, data, dsize ) ) 
            return adcontrols::translate_error;
    }

    if ( meta && msize ) {
        if ( adportable::bzip2::is_a( meta, msize ) ) {
            std::string ar;
            adportable::bzip2::decompress( ar, meta, msize );
            if ( ! adportable::serializer< import_continuum_massarray >::deserialize( ma, ar.data(), ar.size() ) )
                return adcontrols::translate_error;
        } else {
            if ( ! adportable::serializer< import_continuum_massarray >::deserialize( ma, meta, msize ) )
                return adcontrols::translate_error;
        }
    }

    adportable::debug(__FILE__, __LINE__) << "translate_profile checkpoint 3";
    const import_continuum_massarray& continuum_massarray = meta ? ma : pSpectrometer->continuum_massarray();
    
	ms.setMSProperty( profile.prop_ );
	ms.setPolarity( profile.polarity_ );
    ms.resize( profile.intensities_.size() );

    adportable::debug(__FILE__, __LINE__) << "translate_profile checkpoint 4";
    ms.setMassArray( continuum_massarray.masses_.data() );
    auto intens = profile.intensities_.data();
    for ( size_t i = 0; i < ms.size(); ++i )
        ms.setIntensity( i, *intens++ );

    adportable::debug(__FILE__, __LINE__) << "translate_profile checkpoint 5";
    ms.setAcquisitionMassRange( ms.getMass( 0 ), ms.getMass( ms.size() - 1 ) );

    adportable::debug(__FILE__, __LINE__) << "translate_profile checkpoint 6";

    return adcontrols::translate_complete;
}
Ejemplo n.º 6
0
bool
assign_masses::operator()( adcontrols::MSAssignedMasses& assignedMasses
                           , const adcontrols::MassSpectrum& centroid
                           , const adcontrols::MSReferences& references
                           , int mode
                           , int fcn )
{
    using adportable::array_wrapper;
    using adcontrols::MSReferences;
    
    array_wrapper<const double> masses( centroid.getMassArray(), centroid.size() );
    array_wrapper<const double> intens( centroid.getIntensityArray(), centroid.size() );
    
    for ( MSReferences::vector_type::const_iterator it = references.begin(); it != references.end(); ++it ) {
        
        double exactMass = it->exact_mass();
        array_wrapper<const double>::const_iterator lBound = std::lower_bound( masses.begin(), masses.end(), exactMass - tolerance_ );
        array_wrapper<const double>::const_iterator uBound = std::lower_bound( masses.begin(), masses.end(), exactMass + tolerance_ );
        
        if ( lBound != masses.end() ) {
            
            size_t lIdx = std::distance( masses.begin(), lBound );
            size_t uIdx = std::distance( masses.begin(), uBound );
            
            // find closest
            size_t cIdx = lIdx;
            for ( size_t i = lIdx + 1; i < uIdx; ++i ) {
                double d0 = std::abs( masses[ cIdx ] - exactMass );
                double d1 = std::abs( masses[ i ] - exactMass );
                if ( d1 < d0 )
                    cIdx = i;
            }
            
            // find highest
            array_wrapper<const double>::const_iterator hIt = std::max_element( intens.begin() + lIdx, intens.begin() + uIdx );
            if ( *hIt < threshold_ )
                continue;
            
            size_t idx = std::distance( intens.begin(), hIt );

            adcontrols::MSAssignedMass assigned( uint32_t( std::distance( references.begin(), it ) )
				                                 , fcn
                                                 , uint32_t(idx)            // idMassSpectrum (index on centroid peak)
                                                 , it->display_formula()
                                                 , it->exact_mass()
                                                 , centroid.getTime( idx )
                                                 , masses[ idx ]
                                                 , it->enable()
                                                 , false          // flags
                                                 , mode );
            // duplicate assign check
            adcontrols::MSAssignedMasses::vector_type::iterator assignIt = 
                std::find_if( assignedMasses.begin(), assignedMasses.end(), [&]( const adcontrols::MSAssignedMass& a ){
                        return a.idPeak() == idx && a.idMassSpectrum() == unsigned(fcn);
                    });
            if ( assignIt != assignedMasses.end() ) {
                // already assined to another refernce
                if ( std::fabs( assignIt->exactMass() - assignIt->mass() ) > 
                     std::fabs( assigned.exactMass() - assigned.mass() ) ) {
                    *assignIt = assigned; // replace
                }
            } else 
                assignedMasses << assigned;
        }
    }
    return true;
    
}