Esempio n. 1
0
    def __init__(self, sample_peaks_df, params: RecalParams):
        self.params = params
        mean_spectrum = hybrid_mean_spectrum(
            sample_peaks_df, params.analyzer, params.peak_width_sigma_1 + params.jitter_sigma_1, 0
        )
        db_hits = get_db_hits(mean_spectrum, params, params.jitter_sigma_1)
        eval_peaks = (
            db_hits[(db_hits.weight > 0) & (db_hits.n_hits > 2)]
            .sort_values("weight", ascending=False)
            .drop_duplicates("mz")
            .sort_values("mz")
        )
        eval_peaks = sample_across_mass_range(eval_peaks, eval_peaks.weight, 4, 25)
        eval_peaks = (
            eval_peaks.drop(columns={'mz', 'mz_stddev', 'mz_mx', 'ints', 'ints_stddev', 'ints_mx'})
            .reset_index(drop=True)
            .rename_axis(index='mol_idx')
        )

        self.eval_peaks = eval_peaks
        self._mz_lo, self._mz_hi = mass_accuracy_bounds(
            self.eval_peaks.db_mz, params.analyzer, params.jitter_sigma_1 * 10
        )
        self.ppm_sigma_1 = ppm_to_sigma_1(1, params.analyzer, params.base_mz)
        self.collected_peak_sets = defaultdict(lambda: defaultdict(list))
Esempio n. 2
0
    def __init__(self, params: RecalParams, ppm='5', segments='1', precision='0.2'):
        self.params = params
        self.align_sigma_1 = ppm_to_sigma_1(float(ppm), params.analyzer, params.base_mz)
        self.n_segments = int(segments)
        self.n_steps = int(np.round(float(ppm) / float(precision)))
        self.profile_mode = params.profile_mode

        self.peak_width_sigma_1 = params.peak_width_sigma_1
        self.jitter_sigma_1 = params.jitter_sigma_1
        self.analyzer = params.analyzer

        self.coef_ = {}
        self.warps_ = [{} for i in range(self.n_segments + 1)]

        self.ref_s = None
        self.align_nodes = None
        self.sample_mzs = None
        self.sample_mzs_bounds = None
        self.sample_before = None
        self.sample_after = None

        if gradient is None or signal is None:
            logger.debug(
                'Missing optional dependency pyMSpec or scipy. Debug images of peak alignment may be inaccurate'
            )
Esempio n. 3
0
    def __init__(self, params: RecalParams, ppm='500'):
        self.params = params
        self.recal_sigma_1 = ppm_to_sigma_1(float(ppm), params.analyzer,
                                            params.base_mz)

        self.analyzer = params.analyzer
        self.jitter_sigma_1 = params.jitter_sigma_1

        self.db_hits = None
        self.model = None
Esempio n. 4
0
 def __init__(self, params: RecalParams, ppm='20'):
     self.align_sigma_1 = ppm_to_sigma_1(float(ppm), params.analyzer,
                                         params.base_mz)
     self.jitter_sigma_1 = params.jitter_sigma_1
     self.analyzer = params.analyzer
     self.min_mz = None
     self.max_mz = None
     self.coef_ = {}
     self.lo_warp_ = {}
     self.hi_warp_ = {}
     self.target_spectrum = None
Esempio n. 5
0
    def __init__(self,
                 params: RecalParams,
                 ppm='20',
                 segments='4',
                 precision='0.1'):
        self.params = params
        self.recal_sigma_1 = ppm_to_sigma_1(float(ppm), params.analyzer,
                                            params.base_mz)
        self.n_segments = int(segments)
        self.n_steps = int(np.round(float(ppm) / float(precision)))
        self.jitter_sigma_1 = params.jitter_sigma_1
        self.analyzer = params.analyzer

        self.recal_spectrum = None
        self.recal_nodes = None
        self.recal_move = None
        self.skip = False
Esempio n. 6
0
def test_ppm_to_sigma_1_tof():
    assert ppm_to_sigma_1(1, "tof", 1) == 1e-6
    assert ppm_to_sigma_1(2, "tof", 1) == 2e-6
    assert ppm_to_sigma_1(2, "tof", 4) == 2.5e-7
Esempio n. 7
0
def test_ppm_to_sigma_1_fticr():
    assert ppm_to_sigma_1(1, "ft-icr", 200) == 2e-6
    assert ppm_to_sigma_1(2, "ft-icr", 200) == 4e-6
    assert ppm_to_sigma_1(2, "ft-icr", 100) == 2e-6
Esempio n. 8
0
def test_ppm_to_sigma_1_orbitrap():
    assert ppm_to_sigma_1(1, "orbitrap", 200) == 2e-6
    assert ppm_to_sigma_1(2, "orbitrap", 200) == 4e-6
    assert ppm_to_sigma_1(2, "orbitrap", 100) == 2e-6
Esempio n. 9
0
    def __init__(
        self,
        analyzer: str = 'orbitrap',
        source: str = 'maldi',
        matrix: str = DEFAULT,
        polarity: PolarityType = 'positive',
        rp: float = 140000.0,
        base_mz: float = 200.0,
        peak_width_ppm: Union[float, DefaultType] = DEFAULT,
        jitter_ppm: float = 3.0,
        adducts: Union[Sequence[str], DefaultType] = DEFAULT,
        profile_mode: bool = False,
        dbs: Union[Sequence[Union[Path, str]], DefaultType] = DEFAULT,
        targeted_dbs: Union[Sequence[Union[Path, str]], DefaultType] = (),
        transforms: Union[Sequence[Sequence[str]], DefaultType] = DEFAULT,
    ):
        from msi_recal.math import ppm_to_sigma_1  # Avoid circular import

        assert polarity in ('positive',
                            'negative'), f'Invalid polarity "{polarity}"'

        self.analyzer = analyzer = normalize_analyzer_type(analyzer)
        self.rp = rp
        self.base_mz = base_mz
        if peak_width_ppm is DEFAULT:
            self.peak_width_ppm = 15 if profile_mode else 0
        else:
            self.peak_width_ppm = peak_width_ppm
        self.peak_width_sigma_1 = ppm_to_sigma_1(self.peak_width_ppm, analyzer,
                                                 base_mz)
        self.jitter_ppm = jitter_ppm
        self.jitter_sigma_1 = ppm_to_sigma_1(self.jitter_ppm, analyzer,
                                             base_mz)

        if adducts is DEFAULT:
            adducts = _default_adducts(polarity, source)

        if matrix is DEFAULT:
            if 'maldi' in source.lower():
                matrix = {
                    'positive': 'dhb',
                    'negative': 'dan'
                }.get(polarity.lower())
            else:
                matrix = None

        if dbs is DEFAULT:
            dbs = ['cm3']

        if matrix is not None and matrix.lower() != 'none':
            for mat in re.split('[,;/|]', matrix):
                norm_mat = mat.lower().strip()
                norm_mat = {'norharmane': 'nor'}.get(norm_mat, norm_mat)
                matrix_db = f'matrix_{norm_mat}_{polarity[:3]}'
                if matrix_db in BUILTIN_DBS:
                    dbs.append(matrix_db)
                else:
                    logger.warning(
                        f'No peak database available for matrix {mat}. Supported MALDI matrices:'
                        + FORMATTED_MATRIXES)

        self.charge = {'positive': 1, 'negative': -1}[polarity]
        self.adducts = adducts
        self.profile_mode = profile_mode

        self.db_paths = [Path(BUILTIN_DBS.get(db, db)) for db in dbs]
        for db_path in self.db_paths:
            assert db_path.exists(), f'{db_path} not found'

        self.targeted_dbs = [
            Path(BUILTIN_DBS.get(db, db)) for db in targeted_dbs
        ]

        if transforms is DEFAULT:
            transforms = [
                ['align_msiwarp', '5', '1', '0.2'],
                ['recal_ransac', '50'],
                # ['recal_msiwarp', '20', '4', '0.1'],
            ]
        self.transforms = transforms

        if analyzer == 'ft-icr':
            self.instrument_model = cpyMSpec.InstrumentModel(
                'fticr', rp, base_mz)
        else:
            self.instrument_model = cpyMSpec.InstrumentModel(
                analyzer, rp, base_mz)