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; } } }
bool DataprocHandler::doMSCalibration( adcontrols::MSCalibrateResult& res , adcontrols::MassSpectrum& centroid , const adcontrols::MSCalibrateMethod& m , const adcontrols::MSAssignedMasses& assigned ) { using adcontrols::MSProperty; const double tolerance = m.massToleranceDa(); const double threshold = centroid.getMaxIntensity() * m.minimumRAPercent() / 100; res.tolerance( tolerance ); // set tolerance in result res.threshold( threshold ); // set threshold in result std::map< size_t, size_t > mode_map; for ( adcontrols::MSAssignedMasses::vector_type::const_iterator it = assigned.begin(); it != assigned.end(); ++it ) mode_map[ it->mode() ]++; // std::map<size_t, size_t>::iterator itMax = std::max_element( mode_map.begin(), mode_map.end() ); // int mode = static_cast<int>(itMax->first); mass_calibrator calibrator( assigned, centroid.getMSProperty() ); adcontrols::MSCalibration calib; if ( ! calibrator.polfit( calib, m.polynomialDegree() + 1 ) ) return false; res.references( m.references() ); res.calibration( calib ); centroid.setCalibration( calib, true ); // m/z assign based on manually determined peaks // continue auto-assign assign_masses assign( tolerance, threshold ); adcontrols::MSAssignedMasses assignedMasses; adcontrols::segment_wrapper< adcontrols::MassSpectrum > segments( centroid ); for ( size_t n = 0; n < segments.size(); ++n ) { assign( assignedMasses, segments[n], m.references(), 0, static_cast<int>(n) ); } mass_calibrator calibrator2( assignedMasses, centroid.getMSProperty() ); if ( calibrator2.polfit( calib, m.polynomialDegree() + 1 ) ) { for ( auto it: assignedMasses ) it.mass( calibrator2.compute_mass( it.time(), it.mode(), calib ) ); centroid.setCalibration( calib, true ); res.calibration( calib ); res.assignedMasses( assignedMasses ); return true; } return false; }
bool rawdata::applyCalibration( const std::wstring& dataInterpreterClsid, const adcontrols::MSCalibrateResult& calibResult ) { #if 0 boost::uuids::uuid objid; auto it = std::find_if( conf_.begin(), conf_.end(), [=](const adutils::AcquiredConf::data& c){ return c.dataInterpreterClsid == dataInterpreterClsid; }); if ( it != conf_.end() ) objid = it->objid; else { if ( conf_.empty() ) adutils::v3::AcquiredConf::create_table_v3( dbf_.db() ); adutils::v3::AcquiredConf::data d; d.objid = objid; d.dataInterpreterClsid.resize( dataInterpreterClsid.size() ); std::copy( dataInterpreterClsid.begin(), dataInterpreterClsid.end(), d.dataInterpreterClsid.begin() ); if ( !adutils::v3::AcquiredConf::insert( dbf_.db(), objid, d ) ) return false; } const std::wstring calibId = calibResult.calibration().calibId(); std::string device; if ( adportable::binary::serialize<>()(calibResult, device) ) { adutils::mscalibio::writeCalibration( dbf_.db(), uint32_t( objid ), calibId.c_str(), calibResult.dataClass(), device.data(), device.size() ); loadAcquiredConf(); loadCalibrations(); return true; } #endif return false; }
bool DataprocHandler::doMSCalibration( adcontrols::MSCalibrateResult& res , adcontrols::MassSpectrum& centroid , const adcontrols::MSCalibrateMethod& m ) { using adcontrols::MSProperty; res.calibration( centroid.calibration() ); res.references( m.references() ); double tolerance = m.massToleranceDa(); double threshold = adcontrols::segments_helper::max_intensity( centroid ) * m.minimumRAPercent() / 100; res.tolerance( tolerance ); res.threshold( threshold ); assign_masses assigner( tolerance, threshold ); adcontrols::MSAssignedMasses assignedMasses; adcontrols::segment_wrapper< adcontrols::MassSpectrum > segments( centroid ); int n = 0; for ( auto seg: segments ) assigner( assignedMasses, seg, res.references(), seg.mode(), n++ ); res.assignedMasses( assignedMasses ); // set peak assign result // annotate each peak on spectrum doAnnotateAssignedPeaks( centroid, assignedMasses ); mass_calibrator calibrator( assignedMasses, centroid.getMSProperty() ); adcontrols::MSCalibration calib; if ( calibrator.polfit( calib, m.polynomialDegree() + 1 ) ) { for ( auto it: assignedMasses ) { double mass = calibrator.compute_mass( it.time(), it.mode(), calib ); it.mass( mass ); } res.calibration( calib ); // res.assignedMasses( assignedMasses ); #if defined _DEBUG && 0 calibresult_validation( res, centroid, threshold ); #endif return true; } return false; }