Skip to content

Commit

Permalink
Merge pull request #327 from Proteobench/add_exceptions
Browse files Browse the repository at this point in the history
Add exceptions
  • Loading branch information
RobbinBouwmeester authored Jul 16, 2024
2 parents 7deece1 + 5cf9f44 commit c11b38f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 15 deletions.
50 changes: 50 additions & 0 deletions proteobench/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class ProteobenchError(Exception):
pass


class ParseError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class ParseSettingsError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class DatapointAppendError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class DatapointGenerationError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class IntermediateFormatGenerationError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class QuantificationError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class PlotError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)


class ConvertStandardFormatError(ProteobenchError):
def __init__(self, message):
self.message = message
super().__init__(self.message)
68 changes: 56 additions & 12 deletions proteobench/modules/dda_quant_ion/module.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
from __future__ import annotations

import pandas as pd
from pandas import DataFrame

from proteobench.exceptions import (
ConvertStandardFormatError,
DatapointAppendError,
DatapointGenerationError,
IntermediateFormatGenerationError,
ParseError,
ParseSettingsError,
QuantificationError,
)
from proteobench.github.gh import DDA_QUANT_RESULTS_REPO
from proteobench.io.parsing.parse_ion import load_input_file
from proteobench.io.parsing.parse_settings_ion import ParseSettingsBuilder
Expand All @@ -27,22 +37,56 @@ def benchmarking(
) -> tuple[DataFrame, DataFrame, DataFrame]:
"""Main workflow of the module. Used to benchmark workflow results."""
# Parse user config
input_df = load_input_file(input_file, input_format)
parse_settings = ParseSettingsBuilder().build_parser(input_format)

standard_format, replicate_to_raw = parse_settings.convert_to_standard_format(input_df)
try:
input_df = load_input_file(input_file, input_format)
except pd.errors.ParserError as e:
raise ParseError(
f"Error parsing {input_format} file, please make sure the format is correct and the correct software tool is chosen: {e}"
)
except Exception as e:
raise ParseSettingsError(f"Error parsing the inpu file: {e}")

# Get quantification data
quant_score = QuantScores(
self.precursor_name, parse_settings.species_expected_ratio(), parse_settings.species_dict()
)
intermediate_data_structure = quant_score.generate_intermediate(standard_format, replicate_to_raw)
try:
parse_settings = ParseSettingsBuilder().build_parser(input_format)
except KeyError as e:
raise ParseSettingsError(f"Error parsing settings file for parsing, settings seem to be missing: {e}")
except FileNotFoundError as e:
raise ParseSettingsError(f"Could not find the parsing settings file: {e}")
except Exception as e:
raise ParseSettingsError(f"Error parsing settings file for parsing: {e}")

current_datapoint = Datapoint.generate_datapoint(
intermediate_data_structure, input_format, user_input, default_cutoff_min_prec=default_cutoff_min_prec
)
try:
standard_format, replicate_to_raw = parse_settings.convert_to_standard_format(input_df)
except KeyError as e:
raise ConvertStandardFormatError(f"Error converting to standard format, key missing: {e}")
except Exception as e:
raise ConvertStandardFormatError(f"Error converting to standard format: {e}")

try:
# Get quantification data
quant_score = QuantScores(
self.precursor_name, parse_settings.species_expected_ratio(), parse_settings.species_dict()
)
except Exception as e:
raise QuantificationError(f"Error generating quantification scores: {e}")

try:
intermediate_data_structure = quant_score.generate_intermediate(standard_format, replicate_to_raw)
except Exception as e:
raise IntermediateFormatGenerationError(f"Error generating intermediate data structure: {e}")

try:
current_datapoint = Datapoint.generate_datapoint(
intermediate_data_structure, input_format, user_input, default_cutoff_min_prec=default_cutoff_min_prec
)
except Exception as e:
raise DatapointGenerationError(f"Error generating datapoint: {e}")

all_datapoints = self.add_current_data_point(all_datapoints, current_datapoint)
try:
all_datapoints = self.add_current_data_point(all_datapoints, current_datapoint)
except Exception as e:
raise DatapointAppendError(f"Error adding current data point: {e}")

# TODO check why there are NA and inf/-inf values
return (
Expand Down
6 changes: 3 additions & 3 deletions webinterface/pages/DDA_Quant_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from pprint import pformat
from typing import Any, Dict, Optional, Type

import pages.texts.proteobench_builder as pbb
import pandas as pd
import plotly.graph_objects as go
import streamlit as st
import streamlit_utils
import pages.texts.proteobench_builder as pbb
from pages.pages_variables.dda_quant_variables import VariablesDDAQuant
from pages.texts.generic_texts import WebpageTexts
from streamlit_extras.let_it_rain import rain
Expand Down Expand Up @@ -244,10 +244,10 @@ def _run_proteobench(self) -> None:
st.session_state[self.variables_dda_quant.all_datapoints]["Highlight"] = [False] * len(
st.session_state[self.variables_dda_quant.all_datapoints].index
)

except Exception as e:
status_placeholder.error(":x: Proteobench ran into a problem")
st.exception(e)
st.error(e, icon="🚨")
# st.exception(e)
else:
self.generate_results(status_placeholder, result_performance, all_datapoints, True, input_df)

Expand Down

0 comments on commit c11b38f

Please sign in to comment.