Exemplo 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;
}
Exemplo 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;
        }
    }
}
Exemplo n.º 3
0
bool
MSChromatogramExtractor::impl::doMSLock( adcontrols::lockmass::mslock& mslock
                                       , const adcontrols::MassSpectrum& centroid
                                       , const adcontrols::MSLockMethod& m )
{
    // TODO: consider how to handle segmented spectrum -- current impl is always process first 
    adcontrols::MSFinder find( m.tolerance( m.toleranceMethod() ), m.algorithm(), m.toleranceMethod() );

    for ( auto& msref : msrefs_ ) {
        size_t idx = find( centroid, msref.second );
        if ( idx != adcontrols::MSFinder::npos ) 
            mslock << adcontrols::lockmass::reference( msref.first, msref.second, centroid.getMass( idx ), centroid.getTime( idx ) );
    }

    if ( mslock.fit() ) {
        // mslock( centroid, true );
        return true;
    }
    return false;
}
Exemplo n.º 4
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;
    
}
Exemplo n.º 5
0
bool
QuanSampleProcessor::doMSLock( adcontrols::MSPeakInfo& pkInfo // will override
                               , adcontrols::MassSpectrum& centroid // will override
                               , const adcontrols::MSLockMethod& m
                               , const adcontrols::QuanCompounds& compounds )
{
    // find reference peak by mass window
    adcontrols::lockmass::mslock mslock;

    // TODO: consider how to handle segmented spectrum -- current impl is always process first 
    adcontrols::MSFinder find( m.tolerance( m.toleranceMethod() ), m.algorithm(), m.toleranceMethod() );

    for ( auto& compound : compounds ) {
        if ( compound.isLKMSRef() ) {
            double exactMass = cformula_->getMonoIsotopicMass( compound.formula() );
            size_t idx = find( centroid, exactMass );
            if ( idx != adcontrols::MSFinder::npos ) {
                // add found peaks into mslock
                mslock << adcontrols::lockmass::reference( compound.formula(), exactMass, centroid.getMass( idx ), centroid.getTime( idx ) );
            }
        }
    }

    if ( mslock.fit() ) {
        mslock( centroid, true );
        mslock( pkInfo, true );
        return true;
    }
    return false;
}