Skip to content

utils

fix_multiindex_dtypes(index)

Assigns correct dtypes to (column) multiindex

Source code in pyhdx/web/utils.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def fix_multiindex_dtypes(index: pd.MultiIndex) -> pd.MultiIndex:
    """Assigns correct dtypes to (column) multiindex"""
    for level, name in enumerate(index.names):
        if name in [
            "state",
            "D_uptake_fit_ID",
            "guess_ID",
            "fit_ID",
            "comparison_name",
            "comparison_state",
        ]:
            index = multiindex_astype(index, level, "category")
            categories = list(index.unique(level=level))
            index = multiindex_set_categories(index, level, categories, ordered=True)
        elif name in ["exposure"]:
            index = multiindex_astype(index, level, "float")

    return index

get_view(widget)

If the widget object has a view attribute, return this, otherwise return the widget

Source code in pyhdx/web/utils.py
18
19
20
21
22
23
24
25
def get_view(
    widget,
):  # widget is viewable or widget or custom somthing with view method
    """If the widget object has a `view` attribute, return this, otherwise return the widget"""
    if hasattr(widget, "view"):
        return widget.view
    else:
        return widget

load_state(file_input, hdx_spec, data_dir, states=None, names=None)

Loader method for PeptideFileInputControl from a HDX-MS state specification.

Source code in pyhdx/web/utils.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def load_state(
    file_input: PeptideFileInputControl,
    hdx_spec: Dict,
    data_dir: Path,
    states: Optional[List[str]] = None,
    names: Optional[List[str]] = None,
) -> None:
    """Loader method for `PeptideFileInputControl` from a HDX-MS state specification."""

    input_files = [data_dir / f_dict["filename"] for f_dict in hdx_spec["data_files"].values()]
    file_input.widgets["input_files"].filename = [f.name for f in input_files]
    f_bytes = [f.read_bytes() for f in input_files]
    file_input.input_files = f_bytes

    # file_input._read_files() # triggered by setting input files
    state_spec = hdx_spec["states"]
    states = states or list(state_spec.keys())
    names = names or states

    for state, name in zip(states, names):
        peptide_spec = state_spec[state]["peptides"]
        data_spec = hdx_spec["data_files"]

        file_input.fd_file = data_spec[peptide_spec["FD_control"]["data_file"]]["filename"]
        file_input.fd_state = peptide_spec["FD_control"]["state"]
        file_input.fd_exposure = (
            peptide_spec["FD_control"]["exposure"]["value"]
            * time_factors[peptide_spec["FD_control"]["exposure"]["unit"]]
        )

        if "ND_control" in peptide_spec:
            file_input.nd_file = data_spec[peptide_spec["ND_control"]["data_file"]]["filename"]
            file_input.nd_state = peptide_spec["ND_control"]["state"]
            file_input.nd_exposure = (
                peptide_spec["ND_control"]["exposure"]["value"]
                * time_factors[peptide_spec["ND_control"]["exposure"]["unit"]]
            )

        file_input.exp_file = data_spec[peptide_spec["experiment"]["data_file"]]["filename"]
        file_input.exp_state = peptide_spec["experiment"]["state"]

        # Set experiment exposures if specified, otherwise leave default selection
        try:
            exp_vals = peptide_spec["experiment"]["exposure"]["values"]
            f = time_factors[peptide_spec["experiment"]["exposure"]["unit"]]
            file_input.exp_exposures = [v * f for v in exp_vals]
        except KeyError:
            pass

        file_input.measurement_name = name

        metadata = state_spec[state]["metadata"]
        file_input.pH = metadata["pH"]

        file_input.temperature = (
            metadata["temperature"]["value"]
            + temperature_offsets[metadata["temperature"]["unit"].lower()]
        )

        file_input.d_percentage = metadata["d_percentage"]

        if "n_term" in metadata:
            file_input.n_term = metadata["n_term"]
        if "c_term" in metadata:
            file_input.c_term = metadata["c_term"]
        if "sequence" in metadata:
            file_input.sequence = metadata["sequence"]

        file_input._add_single_dataset_spec()