Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions alpharaw/dia/normal_dia.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from collections import defaultdict

import numpy as np
from alphatims.alphatims_wrapper import convert_to_alphatims, remove_unused_peaks
from alphatims.bruker import TimsTOF

from alpharaw.ms_data_base import MSData_Base
from alpharaw.utils.df_processing import remove_unused_peaks
from alpharaw.utils.timstof import convert_to_alphatims


class NormalDIAGrouper:
Expand Down
48 changes: 4 additions & 44 deletions alpharaw/utils/df_processing.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
from typing import Tuple
"""Deprecated functions for processing dataframes in alpharaw."""

import pandas as pd
from alphabase.peptide.fragment import (
compress_fragment_indices,
remove_unused_fragments,
)


def remove_unused_peaks(
spectrum_df: pd.DataFrame,
peak_df: pd.DataFrame,
) -> Tuple[pd.DataFrame, pd.DataFrame]:
"""Removes unused peaks of removed spectra,
reannotates the peak_start_idx and peak_stop_idx

Parameters
----------
spectrum_df : pd.DataFrame
Spectrum dataframe which contains peak_start_idx and peak_stop_idx columns

peak_df : pd.DataFrame
The peak dataframe which should be compressed by removing unused peaks.

Returns
-------
pd.DataFrame, pd.DataFrame
The reindexed spectrum DataFrame and the sliced peak DataFrame
"""

spectrum_df, (peak_df,) = remove_unused_fragments(
spectrum_df,
(peak_df,),
frag_start_col="peak_start_idx",
frag_stop_col="peak_stop_idx",
def remove_unused_peaks(*args, **kwargs):
raise DeprecationWarning(
"remove_unused_peaks has been moved to alphatims and will be removed from alpharaw in a future version."
)
return spectrum_df, peak_df

spectrum_df = spectrum_df.sort_values(["peak_start_idx"], ascending=True)
frag_idx = spectrum_df[["peak_start_idx", "peak_stop_idx"]].values

new_frag_idx, fragment_pointer = compress_fragment_indices(frag_idx)

spectrum_df[["peak_start_idx", "peak_stop_idx"]] = new_frag_idx
spectrum_df = spectrum_df.sort_index()

return (spectrum_df, peak_df.iloc[fragment_pointer].copy().reset_index(drop=True))
40 changes: 5 additions & 35 deletions alpharaw/utils/timstof.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
import typing
"""Deprecated functions for converting to alphatims format."""

import pandas as pd
from alphatims.bruker import TimsTOF

from alpharaw.ms_data_base import MSData_Base
from alpharaw.utils.df_processing import remove_unused_peaks
from alpharaw.wrappers.alphatims_wrapper import AlphaTimsWrapper


def convert_to_alphatims(
spec_df: pd.DataFrame,
peak_df: pd.DataFrame,
dda: bool = False,
) -> typing.Tuple[MSData_Base, TimsTOF]:
"""
Convert any spectrum dataframe or sliced spectrum dataframe
and its peak dataframe into AlphaTims' TimsTOF object (AlphaTimsWrapper).

Args:
spec_df (pd.DataFrame):
spectrum dataframe or sliced spectrum dataframe in AlphaRaw's format.
peak_df (pd.DataFrame):
peak dataframe in AlphaRaw's format by removing unused peaks in spec_df.
dda (bool):
if dda data.

Returns:
MSData_Base: AlphaRaw object
TimsTOF: AlphaTims' TimsTOF object (AlphaTimsWrapper).
"""
spec_df, peak_df = remove_unused_peaks(spec_df, peak_df)
ms_data = MSData_Base()
ms_data.spectrum_df = spec_df
ms_data.peak_df = peak_df

return ms_data, AlphaTimsWrapper(ms_data, dda=dda)
def convert_to_alphatims(*args, **kwargs):
raise DeprecationWarning(
"convert_to_alphatims has been moved to alphatims and will be removed from alpharaw in a future version."
)
138 changes: 6 additions & 132 deletions alpharaw/viz/plot_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import typing

import numpy as np
import pandas as pd
import plotly.graph_objects as go
from alphatims.bruker import TimsTOF


def plot_scatter(
Expand Down Expand Up @@ -52,138 +49,15 @@ def plot_scatter(
)


alphatims_labels = {
"rt": "rt_values",
"im": "mobility_values",
}


def plot_line_tims(
tims_data: TimsTOF,
tims_raw_indices: np.ndarray,
tims_view_indices: np.array,
name: str,
legend_group: str,
marker_color: str,
view_dim: typing.Literal["rt", "im"] = "rt", # or 'im'
intensity_scale: float = 1.0,
rt_unit: str = "minute",
) -> go.Figure:
"""
Plot an XIC line on alphatims `TimsTOF` data

Parameters
----------
tims_data : TimsTOF
The alphatims `TimsTOF` object
tims_raw_indices : np.ndarray
Raw indices on `TimsTOF` object
tims_view_indices : np.array
View indices on `TimsTOF` object
name : str
Display name
legend_group : str
Lines will be grouped by `legend_group`
marker_color : str
Color of the scatter (x, y)
view_dim : "rt", "im", optional
View dimension, "rt" or "im", by default "rt"
rt_unit : str, optional
RT unit, by default "minute"

Returns
-------
go.Figure
Line plot
"""
x_dimension = alphatims_labels[view_dim]

intensities = tims_data.bin_intensities(tims_raw_indices, [x_dimension])
if view_dim == "rt":
x_ticks = tims_data.rt_values[tims_view_indices]
intensities = intensities[tims_view_indices]
if rt_unit == "minute":
x_ticks /= 60.0
else:
x_ticks = tims_data.mobility_values[tims_view_indices]
intensities = intensities[tims_view_indices]

return plot_line(
x_ticks,
intensities * intensity_scale,
name=name,
marker_color=marker_color,
legend_group=legend_group,
x_text=view_dim.upper(),
def plot_line_tims(*args, **kwargs) -> go.Figure:
raise DeprecationWarning(
"plot_line_tims has been moved to alphaviz and will be removed from alpharaw in a future version."
)


def plot_line_tims_fast(
tims_data: TimsTOF,
tims_raw_indices: np.ndarray,
tims_view_indices: np.array,
name: str,
legend_group: str,
marker_color: str,
view_dim: typing.Literal["rt", "im"] = "rt",
intensity_scale: float = 1.0,
rt_unit: str = "minute",
add_peak_area=True,
) -> go.Figure:
"""
Plot an XIC line on alphatims `TimsTOF` data

Parameters
----------
tims_data : TimsTOF
The alphatims `TimsTOF` object
tims_raw_indices : np.ndarray
Raw indices on `TimsTOF` object
tims_view_indices : np.array
View indices on `TimsTOF` object
name : str
Display name
legend_group : str
Lines will be grouped by `legend_group`
marker_color : str
Color of the scatter (x, y)
view_dim : "rt", "im", optional
View dimension, "rt" or "im", by default "rt"
intensity_scale : float, optional
Intensity scale of mirror plot, by default 1.0
rt_unit : str, optional
RT unit, by default "minute"
add_peak_area : bool, optional
If add peak area in the hover text, by default True

Returns
-------
go.Figure
_description_
"""
x_dimension = alphatims_labels[view_dim]

intensities = tims_data.bin_intensities(tims_raw_indices, [x_dimension])
if view_dim == "rt":
x_ticks = tims_data.rt_values[tims_view_indices]
intensities = intensities[tims_view_indices]
if rt_unit == "minute":
x_ticks /= 60.0
else:
x_ticks = tims_data.mobility_values[tims_view_indices]
intensities = intensities[tims_view_indices]

if add_peak_area:
peak_area = abs(np.trapz(y=intensities, x=x_ticks))

return plot_line(
x_ticks,
intensities * intensity_scale,
name=name,
marker_color=marker_color,
legend_group=legend_group,
x_text=view_dim.upper(),
other_info=f"Peak area: {peak_area:.2e}" if add_peak_area else "",
def plot_line_tims_fast(*args, **kwargs) -> go.Figure:
raise DeprecationWarning(
"plot_line_tims_fast has been moved to alphaviz and will be removed from alpharaw in a future version."
)


Expand Down
Loading
Loading