Skip to content

controllers

ColorTransformControl

Bases: PyHDXControlPanel

This controller allows users classify 'mapping' datasets and assign them colors.

Coloring can be either in discrete categories or as a continuous custom color map.

Source code in pyhdx/web/controllers.py
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
class ColorTransformControl(PyHDXControlPanel):
    """
    This controller allows users classify 'mapping' datasets and assign them colors.

    Coloring can be either in discrete categories or as a continuous custom color map.
    """

    _type = "color_transform"

    header = "Color Transform"

    # todo unify name for target field (target_data set)
    # When coupling param with the same name together there should be an option to exclude this behaviour
    quantity = param.Selector(label="Target Quantity")  # todo refactor cmapopt / color transform??
    # fit_ID = param.Selector()  # generalize selecting widgets based on selected table
    # quantity = param.Selector(label='Quantity')  # this is the lowest-level quantity of the multiindex df (filter??)

    current_color_transform = param.String()

    mode = param.Selector(
        default="Colormap",
        objects=["Colormap", "Continuous", "Discrete"],
        doc="Choose color mode (interpolation between selected colors).",
    )
    num_colors = param.Integer(
        2,
        bounds=(1, 10),
        label="Number of colours",
        doc="Number of classification colors.",
    )
    library = param.Selector(
        default="pyhdx_default",
        objects=["pyhdx_default", "user_defined", "matplotlib", "colorcet"],
    )
    colormap = param.Selector()
    otsu_thd = param.Action(
        lambda self: self._action_otsu(),
        label="Otsu",
        doc="Automatically perform thresholding based on Otsu's method.",
    )
    linear_thd = param.Action(
        lambda self: self._action_linear(),
        label="Linear",
        doc="Automatically perform thresholding by creating equally spaced sections.",
    )
    # log_space = param.Boolean(False,
    #                          doc='Boolean to set whether to apply colors in log space or not.')
    # apply = param.Action(lambda self: self._action_apply())
    no_coverage = param.Color(default="#8c8c8c", doc="Color to use for regions of no coverage")

    live_preview = param.Boolean(default=True, doc="Toggle live preview on/off", precedence=-1)

    color_transform_name = param.String("", doc="Name for the color transform to add")
    apply_colormap = param.Action(
        lambda self: self._action_apply_colormap(), label="Update color transform"
    )

    # show_thds = param.Boolean(True, label='Show Thresholds', doc='Toggle to show/hide threshold lines.')
    values = param.List(default=[], precedence=-1)
    colors = param.List(default=[], precedence=-1)

    def __init__(self, parent, **param):
        super(ColorTransformControl, self).__init__(
            parent, _excluded=["otsu_thd", "num_colors"], **param
        )

        # https://discourse.holoviz.org/t/based-on-a-select-widget-update-a-second-select-widget-then-how-to-link-the-latter-to-a-reactive-plot/917/8
        # update to proplot cmaps?
        self._bounds = True  # set to False to disable updating bounds on thresholds
        cc_cmaps = sorted(colorcet.cm.keys())
        mpl_cmaps = sorted(set(plt.colormaps()) - set("cet_" + cmap for cmap in cc_cmaps))

        self._pyhdx_cmaps = {}  # Dict of pyhdx default colormaps
        f_cmap, f_norm = CMAP_NORM_DEFAULTS["foldedness"]
        self._user_cmaps = {"lily_blue": f_cmap}
        cmap_opts = [opt for opt in self.opts.values() if isinstance(opt, CmapOpts)]
        # quantity (column name / cmap_field): (cmap, norm)
        self.quantity_mapping: dict[str, (Colormap, Normalize)] = {}
        for opt in cmap_opts:
            cmap, norm = opt.cmap, opt.norm_scaled
            self._pyhdx_cmaps[cmap.name] = cmap
            # field = {"dG": "dG"}.get(opt.field, opt.field)  # no longer needed
            self.quantity_mapping[opt.field] = (cmap, norm)

        self.cmap_options = {
            "matplotlib": mpl_cmaps,  # list or dicts
            "colorcet": cc_cmaps,
            "pyhdx_default": self._pyhdx_cmaps,
            "user_defined": self._user_cmaps,
        }

        self._update_num_colors()
        self._update_num_values()
        self._update_library()

        # these are rfu, drfu, d_uptake, dg, ddg,
        quantity_options = [opt.field for opt in self.opts.values() if isinstance(opt, CmapOpts)]
        self.param["quantity"].objects = quantity_options
        if self.quantity is None:
            self.quantity = quantity_options[0]

        self.update_box()

    @property
    def own_widget_names(self):
        """returns a list of names of widgets in self.widgets to be laid out in controller card"""

        initial_widgets = []
        for name in self.param:
            precedence = self.param[name].precedence
            if (precedence is None or precedence > 0) and name not in self._excluded + ["name"]:
                initial_widgets.append(name)

        # todo control color / value fields with param.add_parameter function
        widget_names = initial_widgets + [f"value_{i}" for i in range(len(self.values))]
        if self.mode != "Colormap":
            widget_names += [f"color_{i}" for i in range(len(self.colors))]
        return widget_names

    def make_dict(self):
        return self.generate_widgets(
            num_colors=pn.widgets.IntInput,
            current_color_transform=pn.widgets.StaticText,
        )

    def get_selected_data(self):
        # todo rfu residues peptides should be expanded one more dim to add quantity column level on top
        if self.quantity is None:
            self.parent.logger.info("No quantity selected to derive colormap thresholds from")
            return None

        # get the table key corresponding to the selected cmap quantity
        table_key = next(iter((k for k in self.src.tables.keys() if k.startswith(self.quantity))))

        table = self.src.get_table(table_key)
        if table is None:
            self.parent.logger.info(f"Table corresponding to {self.quantity!r} is empty")
            return None

        if isinstance(table.columns, pd.MultiIndex):
            df = table.xs(self.quantity, level=-1, axis=1)
        else:
            df = table[self.quantity]

        opt = self.opts[TABLE_INFO[self.quantity]["cmap_opt"]]

        return df * opt.sclf

    def get_values(self):
        """return numpy array with only the values from selected dataframe, nan omitted"""

        array = self.get_selected_data().to_numpy().flatten()
        values = array[~np.isnan(array)]

        return values

    def _action_otsu(self):
        if self.num_colors <= 1:
            return
        values = self.get_values()  # todo check for no values
        if not values.size:
            return

        # func = np.log if self.log_space else lambda x: x  # this can have NaN when in log space
        func = lambda x: x
        thds = threshold_multiotsu(func(values), classes=self.num_colors)
        widgets = [widget for name, widget in self.widgets.items() if name.startswith("value")]
        for thd, widget in zip(thds[::-1], widgets):  # Values from high to low
            widget.start = None
            widget.end = None
            widget.value = thd  # np.exp(thd) if self.log_space else thd
        self._update_bounds()

    def _action_linear(self):
        i = 1 if self.mode == "Discrete" else 0
        values = self.get_values()
        if not values.size:
            return

        # if self.log_space:
        #     thds = np.logspace(np.log(np.min(values)), np.log(np.max(values)),
        #                        num=self.num_colors + i, endpoint=True, base=np.e)
        # else:
        thds = np.linspace(np.min(values), np.max(values), num=self.num_colors + i, endpoint=True)

        widgets = [widget for name, widget in self.widgets.items() if name.startswith("value")]
        for thd, widget in zip(thds[i : self.num_colors][::-1], widgets):
            # Remove bounds, set values, update bounds
            widget.start = None
            widget.end = None
            widget.value = thd
        self._update_bounds()

    def _action_apply_colormap(self):
        if self.quantity is None:
            return

        cmap, norm = self.get_cmap_and_norm()
        if cmap and norm:
            cmap.name = self.color_transform_name
            # with # aggregate and execute at once: #            with param.parameterized.discard_events(opt): ??

            opt = self.opts[TABLE_INFO[self.quantity]["cmap_opt"]]
            opt.cmap = cmap
            opt.norm_scaled = norm  # perhaps setter for norm such that externally it behaves as a rescaled thingy?

            self.quantity_mapping[TABLE_INFO[self.quantity]["cmap_field"]] = (
                cmap,
                norm,
            )
            self._user_cmaps[cmap.name] = cmap

    @param.depends("colormap", "values", "colors", watch=True)
    def _preview_updated(self):
        if self.live_preview:
            pass
            # self._action_apply_colormap()

    @param.depends("quantity", watch=True)
    def _quantity_updated(self):
        cmap, norm = self.quantity_mapping[TABLE_INFO[self.quantity]["cmap_field"]]

        # with .. # todo accumulate events?

        preview = self.live_preview

        self.live_preview = False
        self.mode = "Colormap"

        lib = "pyhdx_default" if cmap.name in self._pyhdx_cmaps.keys() else "user_defined"
        self.library = lib
        self.no_coverage = to_hex(cmap.get_bad(), keep_alpha=False)
        self.colormap = cmap.name
        self.color_transform_name = cmap.name
        self.current_color_transform = cmap.name

        thds = [norm.vmax, norm.vmin]
        widgets = [widget for name, widget in self.widgets.items() if name.startswith("value")]
        self._bounds = False  # todo decorator
        for i, (thd, widget) in enumerate(zip(thds, widgets)):
            # Remove bounds, set values, update bounds
            widget.start = None
            widget.end = None
            widget.value = thd
            self.values[i] = thd

        self.param.trigger("values")
        self._bounds = True
        self._update_bounds()
        self.live_preview = preview

    def get_cmap_and_norm(self):
        norm_klass = mpl.colors.Normalize

        # if not self.log_space else mpl.colors.LogNorm
        # if self.colormap_name in self.cmaps['pyhdx_default']: # todo change
        #     self.parent.logger.info(f"Colormap name {self.colormap_name} already exists")
        #     return None, None

        if len(self.values) < 1:
            return None, None

        if self.mode == "Discrete":
            if len(self.values) != len(self.colors) - 1:
                return None, None
            cmap = mpl.colors.ListedColormap(self.colors[::-1])
            values = self.get_values()
            thds = sorted([values.min()] + self.values + [values.max()])

            norm = mpl.colors.BoundaryNorm(
                thds, self.num_colors, extend="neither"
            )  # todo refactor values to thd_values

        elif self.mode == "Continuous":
            norm = norm_klass(vmin=np.min(self.values), vmax=np.max(self.values), clip=True)
            positions = norm(self.values[::-1])
            cmap = mpl.colors.LinearSegmentedColormap.from_list(
                "custom_cmap", list(zip(positions, self.colors))
            )

        elif self.mode == "Colormap":
            norm = norm_klass(vmin=np.min(self.values), vmax=np.max(self.values), clip=True)
            if self.library == "matplotlib":
                cmap = mpl.cm.get_cmap(self.colormap)
            elif self.library == "colorcet":
                cmap = getattr(colorcet, "m_" + self.colormap)
            elif self.library == "pyhdx_default":
                cmap = self._pyhdx_cmaps[self.colormap]
            elif self.library == "user_defined":
                try:
                    cmap = self._user_cmaps[self.colormap]
                except KeyError:
                    return None, None

        cmap.name = self.color_transform_name
        self.current_color_transform = self.color_transform_name
        cmap.set_bad(self.no_coverage)

        return cmap, norm

    @param.depends("library", watch=True)
    def _update_library(self):
        collection = self.cmap_options[self.library]
        options = collection if isinstance(collection, list) else list(collection.keys())
        self.param["colormap"].objects = options
        if (
            self.colormap is None or self.colormap not in options
        ) and options:  # todo how can it not be in options?
            self.colormap = options[0]

    @param.depends("mode", watch=True)
    def _mode_updated(self):
        if self.mode == "Discrete":
            self._excluded = ["library", "colormap"]
        #        self.num_colors = max(3, self.num_colors)
        #        self.param['num_colors'].bounds = (3, None)
        elif self.mode == "Continuous":
            self._excluded = ["library", "colormap", "otsu_thd"]
        #      self.param['num_colors'].bounds = (2, None)
        elif self.mode == "Colormap":
            self._excluded = ["otsu_thd", "num_colors"]
            self.num_colors = 2

        # todo adjust add/ remove color widgets methods
        self.param.trigger("num_colors")
        self.update_box()

    @param.depends("num_colors", watch=True)
    def _update_num_colors(self):
        while len(self.colors) != self.num_colors:
            if len(self.colors) > self.num_colors:
                self._remove_color()
            elif len(self.colors) < self.num_colors:
                self._add_color()
        self.param.trigger("colors")

    @param.depends("num_colors", watch=True)
    def _update_num_values(self):
        diff = 1 if self.mode == "Discrete" else 0
        while len(self.values) != self.num_colors - diff:
            if len(self.values) > self.num_colors - diff:
                self._remove_value()
            elif len(self.values) < self.num_colors - diff:
                self._add_value()

        if self.num_colors >= 5:
            self.widgets["otsu_thd"].disabled = True
        else:
            self.widgets["otsu_thd"].disabled = False

        self._update_bounds()
        self.param.trigger("values")
        self.update_box()

    def _add_value(self):
        # value widgets are ordered in decreasing order, ergo next value widget
        # starts with default value of previous value -1
        try:
            first_value = self.values[-1]
        except IndexError:
            first_value = 0

        default = float(first_value - 1)
        self.values.append(default)

        name = f"Threshold {len(self.values)}"
        key = f"value_{len(self.values) - 1}"  # values already populated, first name starts at 1
        widget = pn.widgets.FloatInput(name=name, value=default)
        self.widgets[key] = widget
        widget.param.watch(self._value_event, ["value"])

    def _remove_value(self):
        key = f"value_{len(self.values) - 1}"
        widget = self.widgets.pop(key)
        self.values.pop()

        [widget.param.unwatch(watcher) for watcher in widget.param._watchers]
        del widget

    def _add_color(self):
        try:
            default = DEFAULT_CLASS_COLORS[len(self.colors)]
        except IndexError:
            default = "#" + "".join(np.random.choice(list("0123456789abcdef"), 6))

        self.colors.append(default)

        key = f"color_{len(self.colors) - 1}"
        widget = pn.widgets.ColorPicker(value=default)

        self.widgets[key] = widget

        widget.param.watch(self._color_event, ["value"])

    def _remove_color(self):
        key = f"color_{len(self.colors) - 1}"
        widget = self.widgets.pop(key)
        self.colors.pop()
        [widget.param.unwatch(watcher) for watcher in widget.param._watchers]
        del widget

    def _color_event(self, *events):
        for event in events:
            idx = list(self.widgets.values()).index(event.obj)
            key = list(self.widgets.keys())[idx]
            widget_index = int(key.split("_")[1])
            # idx = list(self.colors_widgets).index(event.obj)
            self.colors[widget_index] = event.new

        self.param.trigger("colors")

        # todo param trigger colors????

    def _value_event(self, *events):
        """triggers when a single value gets changed"""
        for event in events:
            idx = list(self.widgets.values()).index(event.obj)
            key = list(self.widgets.keys())[idx]
            widget_index = int(key.split("_")[1])
            self.values[widget_index] = event.new

        self._update_bounds()
        self.param.trigger("values")

    def _update_bounds(self):
        # for i, widget in enumerate(self.values_widgets.values()):
        if not self._bounds:  # temporary fix to turn on/off bounds (perhaps should be a decorator)
            return
        for i in range(len(self.values)):
            widget = self.widgets[f"value_{i}"]
            if i > 0:
                key = f"value_{i-1}"
                prev_value = float(self.widgets[key].value)
                widget.end = np.nextafter(prev_value, prev_value - 1)
            else:
                widget.end = None

            if i < len(self.values) - 1:
                key = f"value_{i+1}"
                next_value = float(self.widgets[key].value)
                widget.start = np.nextafter(next_value, next_value + 1)
            else:
                widget.start = None

own_widget_names property

returns a list of names of widgets in self.widgets to be laid out in controller card

get_values()

return numpy array with only the values from selected dataframe, nan omitted

Source code in pyhdx/web/controllers.py
1769
1770
1771
1772
1773
1774
1775
def get_values(self):
    """return numpy array with only the values from selected dataframe, nan omitted"""

    array = self.get_selected_data().to_numpy().flatten()
    values = array[~np.isnan(array)]

    return values

DUptakeFitControl

Bases: PyHDXControlPanel

Source code in pyhdx/web/controllers.py
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
class DUptakeFitControl(PyHDXControlPanel):
    _type = "d_uptake_fit"

    header = "D-Uptake fit"

    repeats = param.Integer(default=25, bounds=(1, 100), doc="Number of fitting repeats")

    bounds = param.Boolean(default=True, doc="Toggle to use bounds [0 - 1]")

    r1 = param.Number(
        default=1,
        bounds=(0, None),
        doc="Value of the regularizer along residue axis.",
    )

    fit_name = param.String("D_uptake_fit_1", doc="Name for the fit result")

    _fit_names = param.List([], doc="List of current and future guess names", precedence=-1)

    do_fit = param.Action(
        lambda self: self._action_fit(),
        label="Do Fitting",
        doc="Start D-uptake fit",
    )

    def make_dict(self):
        widgets = self.generate_widgets(
            r1=pn.widgets.FloatInput,
            repeats=pn.widgets.IntInput,
        )

        widgets["pbar"] = ASyncProgressBar()

        return widgets

    def _action_fit(self):
        if len(self.src.hdxm_objects) == 0:
            self.parent.logger.info("No datasets loaded")
            return

        if self.fit_name in self._fit_names:
            self.parent.logger.info(f"D-uptake fit with name {self._fit_names} already in use")
            return

        self._fit_names.append(self.fit_name)
        self.parent.logger.info("Started D-uptake fit")
        self.param["do_fit"].constant = True
        self.widgets["do_fit"].loading = True

        user_dict = self.sources["metadata"].get("user_settings")
        user_dict["d_uptake_fit"][self.fit_name] = self.get_user_settings()
        async_execute(self._fit_d_uptake)

    def get_user_settings(self) -> dict:
        """
        Returns a dictionary with the current user settings.
        """
        keys = ["bounds", "r1"]
        d = {k: getattr(self, k) for k in keys}

        return d

    async def _fit_d_uptake(self):
        name = self.fit_name
        num_samples = len(self.src.hdxm_objects)
        guess = None

        self.widgets["pbar"].num_tasks = num_samples
        async with Client(cfg.cluster.scheduler_address, asynchronous=True) as client:
            futures = []
            for hdxm in self.src.hdxm_objects.values():
                future = client.submit(
                    fit_d_uptake,
                    hdxm,
                    guess,
                    self.r1,
                    self.bounds,
                    self.repeats,
                    False,
                    "worker_client",
                )
                futures.append(future)

            await self.widgets["pbar"].run(futures)
            results = await asyncio.gather(*futures)

        result_obj = DUptakeFitResultSet(list(results))
        self.src.add(result_obj, name)

        self.param["do_fit"].constant = False
        self.widgets["do_fit"].loading = False

        self.parent.logger.info(f"Finished D-uptake fit {name}")

get_user_settings()

Returns a dictionary with the current user settings.

Source code in pyhdx/web/controllers.py
932
933
934
935
936
937
938
939
def get_user_settings(self) -> dict:
    """
    Returns a dictionary with the current user settings.
    """
    keys = ["bounds", "r1"]
    d = {k: getattr(self, k) for k in keys}

    return d

DifferentialControl

Bases: PyHDXControlPanel

Source code in pyhdx/web/controllers.py
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
class DifferentialControl(PyHDXControlPanel):
    _type = "diff"

    header = "Differential HDX"

    reference_state = param.Selector(doc="Which of the states to use as reference")

    comparison_name = param.String(default="comparison_1", doc="Name for the comparison table")

    add_comparison = param.Action(lambda self: self._action_add_comparison())

    def __init__(self, parent, **params):
        super().__init__(parent, **params)
        self.widgets["add_comparison"].disabled = True
        self.src.param.watch(self._source_updated, "hdxm_objects")
        self._df = None
        self._source_updated()  # todo trs source does not trigger updated when init

    @property
    def _layout(self):
        layout = []
        # These are 'blind' transform and serve only to provide selection in this controller;
        # they are not coupled to any 'view'
        if "ddG_fit_select" in self.transforms:
            layout.append(("transforms.ddG_fit_select", None))
        if "dduptake_fit_select" in self.transforms:
            layout.append(("transforms.dduptake_fit_select", None))
        layout.append(("self", None))

        return layout

    # def get(self):
    #     print("remove this")
    #     df = self.transforms["ddG_fit_select"].get()
    #     return df

    def _source_updated(self, *events):
        # Triggered when hdxm objects are added
        options = self.src.names
        if len(options) >= 2:
            self.widgets["add_comparison"].disabled = False

        self.param["reference_state"].objects = options
        if self.reference_state is None and options:
            self.reference_state = options[0]

    def _action_add_comparison(self):
        current_df = self.src.get_table("drfu")
        if current_df is not None and self.comparison_name in current_df.columns.get_level_values(
            level=0
        ):
            self.parent.logger.info(f"Comparison name {self.comparison_name!r} already exists")
            return

        user_dict = self.sources["metadata"].get("user_settings")
        user_dict["differential_HDX"][self.comparison_name] = self.get_user_settings()

        # RFU only app has no dGs,
        if "ddG_fit_select" in self.transforms:
            self.add_ddG_comparison()
        if "dduptake_fit_select" in self.transforms:
            self.add_dd_uptake_comparison()
        self.add_drfu_comparison()

        self.parent.logger.info(f"Successfully added comparison set {self.comparison_name!r}")
        self.src.updated = True

    def add_ddG_comparison(self):
        dG_df = self.transforms["ddG_fit_select"].get()
        if dG_df is None:
            return

        reference = dG_df[self.reference_state]["dG"]
        test = dG_df.xs("dG", axis=1, level=1).drop(self.reference_state, axis=1)
        # todo repeated code in plot.ddG_scatter_figure
        ddG = test.subtract(reference, axis=0)

        names = ["comparison_name", "comparison_state", "quantity"]
        columns = pd.MultiIndex.from_product(
            [[self.comparison_name], ddG.columns, ["ddG"]], names=names
        )
        ddG.columns = columns

        cov_ref = dG_df[self.reference_state, "covariance"] ** 2
        cov_test = dG_df.xs("covariance", axis=1, level=1).drop(self.reference_state, axis=1) ** 2
        cov = cov_test.add(cov_ref, axis=0).pow(0.5)
        columns = pd.MultiIndex.from_product(
            [[self.comparison_name], cov.columns, ["covariance"]], names=names
        )
        cov.columns = columns

        combined = pd.concat([ddG, cov], axis=1)

        categories = list(combined.columns.unique(level=1))
        combined.columns = multiindex_astype(combined.columns, 1, "category")
        combined.columns = multiindex_set_categories(combined.columns, 1, categories, ordered=True)

        self.src._add_table(combined, "ddG")

        # self.parent.sources['main'].param.trigger('tables')  #todo check/remove tables trigger

    def add_drfu_comparison(self):
        rfu_df = self.src.get_table("rfu")
        names = ["comparison_name", "comparison_state", "exposure", "quantity"]

        # Take rfu entries from df, to calculate drfu
        reference_rfu = rfu_df.xs(key=(self.reference_state, "rfu"), level=[0, 2], axis=1)
        test_rfu = rfu_df.drop(self.reference_state, axis=1, level=0).xs("rfu", level=2, axis=1)

        drfu = test_rfu.sub(reference_rfu, level="exposure").dropna(how="all", axis=1)

        # Expand multiindex level and set 'comparison_state' level as category
        columns = pd.MultiIndex.from_tuples(
            [(self.comparison_name, *cols, "drfu") for cols in drfu.columns],
            names=names,
        )
        drfu.columns = columns
        categories = list(drfu.columns.unique(level=1))
        drfu.columns = multiindex_astype(drfu.columns, 1, "category")
        drfu.columns = multiindex_set_categories(drfu.columns, 1, categories, ordered=True)

        reference_rfu_sd = rfu_df.xs(key=(self.reference_state, "rfu_sd"), level=[0, 2], axis=1)
        test_rfu_sd = rfu_df.drop(self.reference_state, axis=1, level=0).xs(
            "rfu_sd", level=2, axis=1
        )

        drfu_sd = ((test_rfu_sd**2).add((reference_rfu_sd**2))).pow(0.5).dropna(how="all", axis=1)

        # Expand multiindex level and set 'comparison_state' level as category
        columns = pd.MultiIndex.from_tuples(
            [(self.comparison_name, *cols, "drfu_sd") for cols in drfu_sd.columns],
            names=names,
        )
        drfu_sd.columns = columns
        categories = list(drfu_sd.columns.unique(level=1))
        drfu_sd.columns = multiindex_astype(drfu_sd.columns, 1, "category")
        drfu_sd.columns = multiindex_set_categories(drfu_sd.columns, 1, categories, ordered=True)

        combined = pd.concat([drfu, drfu_sd], axis=1).sort_index(axis=1)

        # TODO should be public
        self.src._add_table(combined, "drfu")

    def add_dd_uptake_comparison(self):
        d_uptake_df = self.transforms["dduptake_fit_select"].get()
        if d_uptake_df is None:
            return

        reference_d_uptake = d_uptake_df.xs(
            key=(self.reference_state, "d_uptake"), level=[0, 2], axis=1
        )
        test_d_uptake = d_uptake_df.drop(self.reference_state, axis=1, level=0).xs(
            "d_uptake", level=2, axis=1
        )

        dd_uptake = test_d_uptake.sub(reference_d_uptake, level="exposure").dropna(
            how="all", axis=1
        )

        names = ["comparison_name", "comparison_state", "exposure", "quantity"]
        columns = pd.MultiIndex.from_tuples(
            [(self.comparison_name, *cols, "dd_uptake") for cols in dd_uptake.columns],
            names=names,
        )

        dd_uptake.columns = fix_multiindex_dtypes(columns)

        self.src._add_table(dd_uptake, "dd_uptake")

    def get_user_settings(self) -> dict:
        """
        Returns a dictionary with the current user settings.
        """

        d = {"reference_state": self.reference_state}

        return d

get_user_settings()

Returns a dictionary with the current user settings.

Source code in pyhdx/web/controllers.py
1611
1612
1613
1614
1615
1616
1617
1618
def get_user_settings(self) -> dict:
    """
    Returns a dictionary with the current user settings.
    """

    d = {"reference_state": self.reference_state}

    return d

FileExportControl

Bases: PyHDXControlPanel

This controller allows users to export and download datasets.

All datasets can be exported as .txt tables. 'Mappable' datasets (with r_number column) can be exported as .pml pymol script, which colors protein structures based on their 'color' column.

Source code in pyhdx/web/controllers.py
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
class FileExportControl(PyHDXControlPanel):
    """
    <outdated docstring>
    This controller allows users to export and download datasets.

    All datasets can be exported as .txt tables.
    'Mappable' datasets (with r_number column) can be exported as .pml pymol script, which colors protein structures
    based on their 'color' column.

    """

    _type = "file_export"

    header = "File Export"

    table = param.Selector(label="Target dataset", doc="Name of the dataset to export")

    export_format = param.Selector(
        default="csv",
        objects=["csv", "pprint"],
        doc="Format of the exported tables."
        "'csv' is machine-readable, 'pprint' is human-readable format",
    )

    def __init__(self, parent, **param):
        super(FileExportControl, self).__init__(parent, **param)
        self.sources["main"].param.watch(
            self._tables_updated, ["tables", "updated"]
        )  # todo make up your mind: trigger tables or updated?

    def make_dict(self):
        widgets = self.generate_widgets()

        widgets["export_tables"] = pn.widgets.FileDownload(
            label="Download table", callback=self.table_export_callback
        )
        widgets["export_pml"] = pn.widgets.FileDownload(
            label="Download pml scripts",
            callback=self.pml_export_callback,
        )
        widgets["export_colors"] = pn.widgets.FileDownload(
            label="Download colors",
            callback=self.color_export_callback,
        )

        widgets["divider"] = pn.layout.Divider()

        widgets["download_state_spec"] = pn.widgets.FileDownload(
            label="Download HDX spec",
            callback=self.hdx_spec_callback,
        )

        widgets["download_config"] = pn.widgets.FileDownload(
            label="Download config",
            callback=self.config_callback,
        )

        widgets["download_user_settings"] = pn.widgets.FileDownload(
            label="Download user settings",
            callback=self.user_settings_callback,
        )

        widgets["download_log"] = pn.widgets.FileDownload(
            label="Download log",
            callback=self.log_callback,
        )

        widget_order = [
            "table",
            "export_format",
            "export_tables",
            "export_pml",
            "export_colors",
            "divider",
            "download_state_spec",
            "download_config",
            "download_user_settings",
            "download_log",
        ]
        final_widgets = {w: widgets[w] for w in widget_order}

        return final_widgets

    def _tables_updated(self, *events):
        options = list(self.sources["main"].tables.keys())
        self.param["table"].objects = options
        if not self.table and options:
            self.table = options[0]

    @property
    def _layout(self):
        return [("self", None)]

    @param.depends("table", "export_format", watch=True)
    def _table_updated(self):
        ext = ".csv" if self.export_format == "csv" else ".txt"
        self.widgets["export_tables"].filename = self.table + ext

        # currently only r_number indexed tables are in TABLE_INFO
        if self.table in TABLE_INFO:
            self.widgets["export_pml"].disabled = False
            self.widgets["export_colors"].disabled = False
            self.widgets["export_pml"].filename = self.table + "_pml_scripts.zip"
            self.widgets["export_colors"].filename = self.table + "_colors" + ext
        else:
            self.widgets["export_pml"].disabled = True
            self.widgets["export_colors"].disabled = True

    @pn.depends("table")
    def table_export_callback(self):
        if self.table:
            df = self.sources["main"].tables[self.table]
            io = dataframe_to_stringio(df, fmt=self.export_format)
            return io
        else:
            return None

    @pn.depends("table")
    def pml_export_callback(self):
        if self.table:
            # todo check if table is valid for pml conversion

            color_df = self.get_color_df()

            bio = BytesIO()
            with zipfile.ZipFile(bio, "w") as pml_zip:
                for col_name in color_df.columns:
                    name = (
                        "_".join(str(col) for col in col_name)
                        if isinstance(col_name, tuple)
                        else str(col_name)
                    )
                    colors = color_df[col_name]
                    pml_script = series_to_pymol(colors)  # todo refactor pd_series_to_pymol?
                    pml_zip.writestr(name + ".pml", pml_script)

            bio.seek(0)
            return bio

    def get_color_df(self) -> pd.Dataframe:
        df = self.sources["main"].tables[self.table]
        opt = self.opts[TABLE_INFO[self.table]["cmap_opt"]]
        field = TABLE_INFO[self.table]["cmap_field"]
        cmap = opt.cmap
        norm = opt.norm
        df = df.xs(field, level=-1, axis=1)

        color_df = apply_cmap(df, cmap, norm)

        return color_df

    @pn.depends("table")
    def color_export_callback(self):
        if self.table:
            df = self.get_color_df()
            io = dataframe_to_stringio(df, fmt=self.export_format)
            return io
        else:
            return None

    def hdx_spec_callback(self) -> StringIO:
        timestamp = self.parent.session_time.strftime("%Y%m%d%H%M")
        self.widgets["download_state_spec"].filename = f"PyHDX_hdx_spec_{timestamp}.yaml"

        sio = self.parent.hdx_spec_callback()
        return sio

    def config_callback(self) -> StringIO:
        timestamp = self.parent.session_time.strftime("%Y%m%d%H%M")
        self.widgets["download_config"].filename = f"PyHDX_config_{timestamp}.yaml"

        sio = self.parent.config_callback()
        return sio

    def user_settings_callback(self) -> StringIO:
        timestamp = self.parent.session_time.strftime("%Y%m%d%H%M")
        self.widgets["download_user_settings"].filename = f"PyHDX_config_{timestamp}.yaml"

        sio = self.parent.user_settings_callback()
        return sio

    def log_callback(self) -> StringIO:
        timestamp = self.parent.session_time.strftime("%Y%m%d%H%M")
        self.widgets["download_log"].filename = f"PyHDX_log_{timestamp}.txt"

        sio = self.parent.log_callback()
        return sio

FitControl

Bases: PyHDXControlPanel

This controller allows users to execute PyTorch fitting of the global data set.

Currently, repeated fitting overrides the old result.

Source code in pyhdx/web/controllers.py
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
class FitControl(PyHDXControlPanel):
    """
    This controller allows users to execute PyTorch fitting of the global data set.

    Currently, repeated fitting overrides the old result.
    """

    _type = "fit"

    header = "ΔG Fit"

    initial_guess = param.Selector(doc="Name of dataset to use for initial guesses.")

    guess_mode = param.Selector(
        default="One-to-one",
        objects=["One-to-one", "One-to-many"],
        doc="Use initial guesses for each protein state (one-to-one) or use one initial"
        "guess for all protein states (one-to-many)",
    )

    guess_state = param.Selector(
        doc="Which protein state to use for initial guess when using one-to-many guesses"
    )

    fit_mode = param.Selector(default="Single", objects=["Batch", "Single"])

    stop_loss = param.Number(
        STOP_LOSS,
        bounds=(0, None),
        doc="Threshold loss difference below which to stop fitting.",
    )

    stop_patience = param.Integer(
        PATIENCE,
        bounds=(1, None),
        doc="Number of epochs where stop loss should be satisfied before stopping.",
    )

    learning_rate = param.Number(
        optimizer_defaults["SGD"]["lr"],
        bounds=(0, None),
        doc="Learning rate parameter for optimization.",
    )

    momentum = param.Number(
        optimizer_defaults["SGD"]["momentum"],
        bounds=(0, None),
        doc="Stochastic Gradient Descent momentum",
    )

    nesterov = param.Boolean(
        optimizer_defaults["SGD"]["nesterov"],
        doc="Use Nesterov type of momentum for SGD",
    )

    epochs = param.Integer(EPOCHS, bounds=(1, None), doc="Maximum number of epochs (iterations.")

    r1 = param.Number(
        R1,
        bounds=(0, None),
        label="Regularizer 1 (peptide axis)",
        doc="Value of the regularizer along residue axis.",
    )

    r2 = param.Number(
        R2,
        bounds=(0, None),
        label="Regularizer 2 (sample axis)",
        doc="Value of the regularizer along sample axis.",
    )

    reference = param.Selector(
        None,
        allow_None=True,
        label="R2 reference",
        doc="Select reference state to use in batch fitting",
    )

    fit_name = param.String("Gibbs_fit_1", doc="Name for for the fit result")

    _fit_names = param.List([], precedence=-1, doc="List of names of completed and running fits")

    do_fit = param.Action(
        lambda self: self._action_fit(),
        constant=True,
        label="Do Fitting",
        doc="Start global fitting",
    )

    def __init__(self, parent, **params):
        self.pbar1 = ASyncProgressBar()  # tqdm?
        super(FitControl, self).__init__(parent, **params)

        self.src.param.watch(self._source_updated, ["updated"])
        self._mode_updated()  # Initialize excluded widgets
        self._current_jobs = 0
        self._max_jobs = 2  # todo config

    def make_dict(self):
        widgets = self.generate_widgets()
        # widgets['pbar_col'] = pn.layout.Column()
        widgets["pbar"] = ASyncProgressBar()
        # widgets['progress'] = CallbackProgress()

        return widgets

    def _source_updated(self, *events):
        rate_objects = list(self.src.rate_results.keys())
        if rate_objects:
            self.param["do_fit"].constant = False

        fit_objects = list(self.src.dG_fits.keys())
        self.param["initial_guess"].objects = rate_objects + fit_objects
        if not self.initial_guess and rate_objects:
            self.initial_guess = rate_objects[0]

        hdxm_objects = list(self.src.hdxm_objects.keys())
        self.param["reference"].objects = [None] + hdxm_objects
        self.param["guess_state"].objects = hdxm_objects
        if not self.guess_state and hdxm_objects:
            self.guess_state = hdxm_objects[0]

        self._mode_updated()

    @param.depends("guess_mode", "fit_mode", watch=True)
    def _mode_updated(self):
        excluded = []
        if not (self.fit_mode == "Batch" and len(self.src.hdxm_objects) > 1):
            excluded += ["r2", "reference"]
        if self.guess_mode == "One-to-one":
            excluded += ["guess_state"]
        self._excluded = excluded
        self.update_box()

    # @param.depends("fit_mode", watch=True)
    # def _fit_mode_updated(self):
    #     if self.fit_mode == "Batch" and len(self.src.hdxm_objects) > 1:
    #         # self.param['r2'].constant = False
    #         self._excluded = []
    #     else:
    #         # self.param['r2'].constant = True
    #         self._excluded = ["r2", "reference"]
    #
    #     self.update_box()

    def _action_fit(self):
        if self.fit_name in self._fit_names:
            self.parent.logger.info(f"Fit result with name {self.fit_name} already in use")
            return
        self._fit_names.append(self.fit_name)
        self.parent.logger.info("Started PyTorch fit")

        user_dict = self.sources["metadata"].get("user_settings")
        user_dict["dG_fit"][self.fit_name] = self.get_user_settings()

        self._current_jobs += 1
        # if self._current_jobs >= self._max_jobs:
        #     self.widgets['do_fit'].constant = True

        self.widgets["do_fit"].loading = True

        self.parent.logger.info(f"Current number of active jobs: {self._current_jobs}")

        if self.fit_mode == "Batch":
            async_execute(self._batch_fit)
        else:
            async_execute(self._single_fit)

    def get_guesses(self):
        ...

        # initial guesses are rates
        if self.initial_guess in self.src.rate_results:
            rates_df = self.src.get_table("rates")

            if self.guess_mode == "One-to-one":
                sub_df = rates_df.xs((self.initial_guess, "rate"), level=[0, 2], axis=1)
                gibbs_guess = self.src.hdx_set.guess_deltaG(sub_df)
            elif self.guess_mode == "One-to-many":
                hdxm = self.src.hdxm_objects[self.guess_state]
                rates_series = rates_df[(self.initial_guess, self.guess_state, "rate")]
                gibbs_guess = hdxm.guess_deltaG(rates_series)

        # intial guess are dG values from previous fit
        elif self.initial_guess in self.src.dG_fits:
            dG_df = self.src.get_table("dG")

            if self.guess_mode == "One-to-one":
                gibbs_guess = dG_df.xs((self.initial_guess, "_dG"), level=[0, 2], axis=1)
            elif self.guess_mode == "One-to-many":
                gibbs_guess = dG_df[(self.initial_guess, self.guess_state, "_dG")]

        else:
            self.parent.logger.debug(f"Initial guess {self.initial_guess!r} not found")

        return gibbs_guess

    async def _single_fit(self):
        name = self.fit_name

        # data_objs = self.src.hdxm_objects.values()
        # rates_df = self.src.rate_results[self.initial_guess].output
        gibbs_guesses = (
            self.get_guesses()
        )  # returns either DataFrame or Series depending on guess mode
        futures = []

        async with Client(cfg.cluster.scheduler_address, asynchronous=True) as client:
            for protein_state, hdxm in self.src.hdxm_objects.items():
                if isinstance(gibbs_guesses, pd.Series):
                    guess = gibbs_guesses
                else:
                    guess = gibbs_guesses[protein_state]

                future = client.submit(fit_gibbs_global, hdxm, guess, **self.fit_kwargs)
                futures.append(future)

            self.widgets["pbar"].num_tasks = len(futures)
            await self.widgets["pbar"].run(futures)

            results = await asyncio.gather(*futures)

        result = TorchFitResultSet(results)
        self.src.add(result, name)
        self._current_jobs -= 1
        self.widgets["pbar"].active = False
        self.widgets["do_fit"].loading = False
        self.parent.logger.info(f"Finished PyTorch fit: {name}")

    async def _batch_fit(self):
        self.widgets["pbar"].active = True
        name = self.fit_name
        hdx_set = self.src.hdx_set
        gibbs_guess = self.get_guesses()
        async with Client(cfg.cluster.scheduler_address, asynchronous=True) as client:
            future = client.submit(fit_gibbs_global_batch, hdx_set, gibbs_guess, **self.fit_kwargs)
            result = await future

        self.src.add(result, name)

        self._current_jobs -= 1
        self.widgets["pbar"].active = False
        self.widgets["do_fit"].loading = False
        self.parent.logger.info(f"Finished PyTorch fit: {name}")
        self.parent.logger.info(
            f"Finished fitting in {len(result.losses)} epochs, final mean squared residuals is {result.mse_loss:.2f}"
        )
        self.parent.logger.info(
            f"Total loss: {result.total_loss:.2f}, regularization loss: {result.reg_loss:.2f} "
            f"({result.regularization_percentage:.1f}%)"
        )

    @property
    def fit_kwargs(self):
        fit_kwargs = dict(
            r1=self.r1,
            lr=self.learning_rate,
            momentum=self.momentum,
            nesterov=self.nesterov,
            epochs=self.epochs,
            patience=self.stop_patience,
            stop_loss=self.stop_loss,
        )
        # callbacks=[self.widgets['progress'].callback])
        if self.fit_mode == "Batch":
            fit_kwargs["r2"] = self.r2
            fit_kwargs["r2_reference"] = self.reference

        return fit_kwargs

    def get_user_settings(self) -> dict:
        """
        Returns a dictionary with the current user settings.
        """

        d = {"initial_guess": self.initial_guess, "guess_mode": self.guess_mode}

        if self.guess_mode == "One-to-many":
            d["guess_state"] = self.guess_state
        d["fit_mode"] = self.fit_mode

        d.update(self.fit_kwargs)

        return d

get_user_settings()

Returns a dictionary with the current user settings.

Source code in pyhdx/web/controllers.py
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
def get_user_settings(self) -> dict:
    """
    Returns a dictionary with the current user settings.
    """

    d = {"initial_guess": self.initial_guess, "guess_mode": self.guess_mode}

    if self.guess_mode == "One-to-many":
        d["guess_state"] = self.guess_state
    d["fit_mode"] = self.fit_mode

    d.update(self.fit_kwargs)

    return d

InitialGuessControl

Bases: PyHDXControlPanel

This controller allows users to derive initial guesses for D-exchange rate from peptide uptake data.

Source code in pyhdx/web/controllers.py
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
class InitialGuessControl(PyHDXControlPanel):
    """
    This controller allows users to derive initial guesses for D-exchange rate from peptide uptake data.
    """

    _type = "initial_guess"

    header = "Initial Guesses"
    fitting_model = param.Selector(
        default="Half-life (λ)",
        objects=["Half-life (λ)", "Association"],
        doc="Choose method for determining initial guesses.",
    )
    dataset = param.Selector(
        default="", doc="Dataset to apply bounds to", label="Dataset (for bounds)"
    )
    global_bounds = param.Boolean(default=False, doc="Set bounds globally across all datasets")
    lower_bound = param.Number(0.0, doc="Lower bound for association model fitting")

    upper_bound = param.Number(0.0, doc="Upper bound for association model fitting")

    guess_name = param.String(default="Guess_1", doc="Name for the initial guesses")

    _guess_names = param.List([], doc="List of current and future guess names", precedence=-1)

    do_fit1 = param.Action(
        lambda self: self._action_fit(),
        label="Calculate Guesses",
        doc="Start initial guess fitting",
        constant=True,
    )

    bounds = param.Dict({}, doc="Dictionary which stores rate fitting bounds", precedence=-1)

    def __init__(self, parent, **params):
        _excluded = ["lower_bound", "upper_bound", "global_bounds", "dataset"]
        super(InitialGuessControl, self).__init__(parent, _excluded=_excluded, **params)
        self.src.param.watch(
            self._parent_hdxm_objects_updated, ["hdxm_objects"]
        )  # todo refactor ( to what?)

        self.update_box()

    def make_dict(self):
        widgets = self.generate_widgets(
            lower_bound=pn.widgets.FloatInput, upper_bound=pn.widgets.FloatInput
        )

        widgets["pbar"] = ASyncProgressBar()

        return widgets

    @param.depends("fitting_model", watch=True)
    def _fitting_model_updated(self):
        if self.fitting_model == "Half-life (λ)":
            self._excluded = ["dataset", "lower_bound", "upper_bound", "global_bounds"]

        elif self.fitting_model in ["Association", "Dissociation"]:
            self._excluded = []

        self.update_box()

    @param.depends("global_bounds", watch=True)
    def _global_bounds_updated(self):
        if self.global_bounds:
            self.param["dataset"].constant = True
        else:
            self.param["dataset"].constant = False

    @param.depends("dataset", watch=True)
    def _dataset_updated(self):
        lower, upper = self.bounds[self.dataset]
        self.lower_bound = lower
        self.upper_bound = upper

    @param.depends("lower_bound", "upper_bound", watch=True)
    def _bounds_updated(self):
        if not self.global_bounds:
            self.bounds[self.dataset] = (self.lower_bound, self.upper_bound)

    def _parent_hdxm_objects_updated(self, *events):
        if len(self.src.hdxm_objects) > 0:
            self.param["do_fit1"].constant = False

        # keys to remove:
        for k in self.bounds.keys() - self.src.hdxm_objects.keys():
            self.bounds.pop(k)
        # keys to add:
        for k in self.src.hdxm_objects.keys() - self.bounds.keys():
            self.bounds[k] = get_bounds(self.src.hdxm_objects[k].timepoints)

        options = list(self.src.hdxm_objects.keys())
        self.param["dataset"].objects = options
        if not self.dataset:
            self.dataset = options[0]

    def add_fit_result(self, future):
        name = self._guess_names.pop(future.key)

        results = future.result()
        result_obj = RatesFitResult(results)
        self.src.add(result_obj, name)

        self.param["do_fit1"].constant = False
        self.widgets["do_fit1"].loading = False

    def _action_fit(self):
        # Checking if data is available, should always be the case as button is locked before
        if len(self.src.hdxm_objects) == 0:
            self.parent.logger.info("No datasets loaded")
            return

        if self.guess_name in self._guess_names:
            self.parent.logger.info(f"Guess with name {self.guess_name} already in use")
            return

        self._guess_names.append(self.guess_name)
        self.parent.logger.info("Started initial guess fit")
        self.param["do_fit1"].constant = True
        self.widgets["do_fit1"].loading = True

        user_dict = self.sources["metadata"].get("user_settings")
        user_dict["initial_guess"][self.guess_name] = self.get_user_settings()

        if self.fitting_model.lower() in ["association", "dissociation"]:
            loop = asyncio.get_running_loop()
            loop.create_task(self._fit_rates(self.guess_name))

        # this is practically instantaneous and does not require dask
        elif self.fitting_model == "Half-life (λ)":
            results = map(fit_rates_half_time_interpolate, self.src.hdxm_objects.values())

            result_obj = RatesFitResult(list(results))
            self.src.add(result_obj, self.guess_name)

            self.param["do_fit1"].constant = False
            self.widgets["do_fit1"].loading = False
            self.parent.logger.info(f"Finished initial guess fit {self.guess_name}")

    async def _fit_rates(self, name):
        num_samples = len(self.src.hdxm_objects)

        self.widgets["pbar"].num_tasks = num_samples
        async with Client(cfg.cluster.scheduler_address, asynchronous=True) as client:
            if self.global_bounds:
                bounds = [(self.lower_bound, self.upper_bound)] * num_samples
            else:
                bounds = self.bounds.values()
            futures = []
            for hdxm, bound in zip(self.src.hdxm_objects.values(), bounds):
                future = client.submit(
                    fit_rates_weighted_average, hdxm, bound, client="worker_client"
                )
                futures.append(future)

            await self.widgets["pbar"].run(futures)

            results = await asyncio.gather(*futures)

        result_obj = RatesFitResult(list(results))
        self.src.add(result_obj, name)

        self.param["do_fit1"].constant = False
        self.widgets["do_fit1"].loading = False
        self.parent.logger.info(f"Finished initial guess fit {name}")

    def get_user_settings(self) -> dict:
        """
        Returns a dictionary with the current user settings.
        """

        d = {"fitting_model": self.fitting_model}
        if self.fitting_model in ["association", "dissociation"]:
            d["global_bounds"] = self.global_bounds
            if self.global_bounds:
                d["bounds"] = [self.lower_bound, self.upper_bound]
            else:
                d["bounds"] = self.bounds

        return d

get_user_settings()

Returns a dictionary with the current user settings.

Source code in pyhdx/web/controllers.py
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
def get_user_settings(self) -> dict:
    """
    Returns a dictionary with the current user settings.
    """

    d = {"fitting_model": self.fitting_model}
    if self.fitting_model in ["association", "dissociation"]:
        d["global_bounds"] = self.global_bounds
        if self.global_bounds:
            d["bounds"] = [self.lower_bound, self.upper_bound]
        else:
            d["bounds"] = self.bounds

    return d

MWEControl

Bases: ControlPanel

Temporary controller for use in the MWE app

Source code in pyhdx/web/controllers.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class MWEControl(ControlPanel):
    """Temporary controller for use in the MWE app"""

    _type = "mwe"

    new_data = param.Action(lambda self: self._action_new_data())

    @property
    def src(self):
        return self.sources["main"]

    def _action_new_data(self):
        df = pd.DataFrame(
            {
                "x": np.random.normal(loc=3, scale=2, size=100),
                "y": np.random.normal(loc=2, scale=0.3, size=100),
                "yerr": np.random.uniform(0.2, 1.0, size=100),
            }
        )

        self.src.tables["test_data"] = df
        self.src.param.trigger("updated")

PeptideFileInputControl

Bases: PyHDXControlPanel

This controller allows users to input .csv file (Currently only DynamX format) of 'state' peptide uptake data. Users can then choose how to correct for back-exchange and which 'state' and exposure times should be used for analysis.

Source code in pyhdx/web/controllers.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
class PeptideFileInputControl(PyHDXControlPanel):
    """
    This controller allows users to input .csv file (Currently only DynamX format) of 'state' peptide uptake data.
    Users can then choose how to correct for back-exchange and which 'state' and exposure times should be used for
    analysis.

    """

    _type = "peptide_file_input"

    header = "Peptide Input"

    input_mode = param.Selector(default="Manual", objects=["Manual", "Batch", "Database"])

    input_files_label = param.String("Input files:")

    input_files = param.List(doc="HDX input files. Currently only supports DynamX format")

    batch_file_label = param.String("Batch file (yaml)")

    batch_file = param.Parameter(doc="Batch file input:")

    dataset_id = param.Selector(
        label="Dataset ID", doc="Dataset ID to load from hdxms-datasets database"
    )

    nd_control = param.Boolean(
        default=False, precedence=-1, doc="Whether to allow users to input a ND control"
    )

    show_pH = param.Boolean(default=True, precedence=-1)

    show_temperature = param.Boolean(default=True, precedence=-1)

    show_d_percentage = param.Boolean(default=True, precedence=-1)

    fd_file = param.Selector(doc="File used for FD control", label="FD File")

    fd_state = param.Selector(doc="State used to normalize uptake", label="FD State")

    fd_exposure = param.Selector(doc="Exposure used to normalize uptake", label="FD Exposure")

    nd_file = param.Selector(doc="File used for ND control", label="ND File")

    nd_state = param.Selector(doc="State used to normalize uptake", label="ND State")

    nd_exposure = param.Selector(doc="Exposure used to normalize uptake", label="ND Exposure")

    exp_file = param.Selector(doc="File with experiment peptides", label="Exp File")

    exp_state = param.Selector(doc="State for selected experiment", label="Experiment State")

    exp_exposures = param.ListSelector(
        default=[],
        objects=[""],
        label="Experiment Exposures",
        doc="Selected exposure time to use",
    )

    d_percentage = param.Number(
        90.0,
        bounds=(0, 100),
        doc="Percentage of deuterium in the labelling buffer",
        label="Deuterium percentage",
    )

    temperature = param.Number(
        293.15,
        bounds=(273.15, 373.15),
        doc="Temperature of the D-labelling reaction",
        label="Temperature (K)",
    )

    pH = param.Number(
        7.5,
        bounds=(2.0, 14.0),
        doc="pH of the D-labelling reaction, as read from pH meter",
        label="pH read",
    )

    n_term = param.Integer(
        1,
        doc="Index of the n terminal residue in the protein. Can be set to negative values to "
        "accommodate for purification tags. Used in the determination of intrinsic rate of exchange",
    )

    c_term = param.Integer(
        0,
        bounds=(0, None),
        doc="Index of the c terminal residue in the protein. Used for generating pymol export script"
        "and determination of intrinsic rate of exchange for the C-terminal residue",
    )

    sequence = param.String("", doc="Optional FASTA protein sequence")

    measurement_name = param.String(doc="Label for the current HDX measurement")

    add_dataset_button = param.Action(  # -> refactor measurement
        lambda self: self._add_single_dataset_spec(),
        label="Add measurement",
        doc="Add single HDX measurement specification for loading",
    )

    hdxm_list = param.ListSelector(
        label="HDX Measurements", doc="Lists added HDX-MS measurements", constant=True
    )

    load_dataset_button = param.Action(
        lambda self: self._action_load_datasets(),
        label="Load dataset",
        doc="Parse specified HDX measurements apply back-exchange correction",
    )

    def __init__(self, parent, **params):
        excluded = ["batch_file", "batch_file_label"]
        super(PeptideFileInputControl, self).__init__(parent, _excluded=excluded, **params)
        self._update_mode()
        self.update_box()

        # Dictionary with current input files
        self.data_files: dict[str, DataFile] = {}
        # Dict with all files, keeps files after clearing input
        self.data_file_history: dict[str, DataFile] = {}

        # Dictionary of accumulated HDX state specifications:
        self.state_spec = {}
        # Dictionary of accumulated HDX data file specifications
        self.data_spec = {}

        # create database dir if it does not exist
        cfg.database_dir.mkdir(parents=True, exist_ok=True)
        self.data_vault = DataVault(cache_dir=cfg.database_dir)

        self.param["dataset_id"].objects = self.data_vault.datasets
        if self.data_vault.datasets:
            self.dataset_id = self.data_vault.datasets[0]

    def make_dict(self):
        text_area = pn.widgets.TextAreaInput(
            name="Sequence (optional)",
            placeholder="Enter sequence in FASTA format",
            max_length=10000,
            width=300,
            height=100,
            height_policy="fixed",
            width_policy="fixed",
        )
        widgets = self.generate_widgets(
            input_files_label=pn.widgets.StaticText(value=self.input_files_label),
            input_files=pn.widgets.FileInput(multiple=True, name="Input files"),
            batch_file_label=pn.widgets.StaticText(value=self.batch_file_label),
            batch_file=pn.widgets.FileInput(name="Batch yaml file", accept=".yaml"),
            be_percent=pn.widgets.FloatInput,
            pH=pn.widgets.FloatInput,
            temperature=pn.widgets.FloatInput,
            d_percentage=pn.widgets.FloatInput,
            sequence=text_area,
        )

        # Add hdx spec download button
        download = pn.widgets.FileDownload(
            label="Download HDX spec", callback=self.spec_download_callback
        )
        widgets["download_spec_button"] = download

        widget_order = [
            "input_mode",
            "input_files_label",
            "input_files",
            "batch_file_label",
            "batch_file",
            "dataset_id",
            "fd_file",
            "fd_state",
            "fd_exposure",
            "nd_file",
            "nd_state",
            "nd_exposure",
            "exp_file",
            "exp_state",
            "exp_exposures",
            "d_percentage",
            "temperature",
            "pH",
            "n_term",
            "c_term",
            "sequence",
            "measurement_name",
            "add_dataset_button",
            "download_spec_button",
            "hdxm_list",
            "load_dataset_button",
        ]

        sorted_widgets = {k: widgets[k] for k in widget_order}

        return sorted_widgets

    def spec_download_callback(self) -> StringIO:
        timestamp = self.parent.session_time.strftime("%Y%m%d%H%M")
        self.widgets["download_spec_button"].filename = f"PyHDX_hdx_spec_{timestamp}.yaml"

        sio = self.parent.hdx_spec_callback()
        return sio

    @param.depends("input_mode", watch=True)
    def _update_mode(self):
        excluded = set()

        # dictionary of widgets/params needed per input mode setting
        # inverse to find which widgets to exclude

        widget_dict = {
            "Manual": {
                "input_files_label",
                "input_files",
                "fd_file",
                "fd_state",
                "fd_exposure",
                "nd_state",
                "nd_exposure",
                "exp_file",
                "exp_state",
                "exp_exposures",
                "drop_first",
                "d_percentage",
                "pH",
                "temperature",
                "n_term",
                "c_term",
                "sequence",
                "add_dataset_button",
                "measurement_name",
                "download_spec_button",
            },
            "Batch": {"input_files_label", "input_files", "batch_file", "batch_file_label"},
            "Database": {
                "dataset_id",
            },
        }

        # widget_dict.pop(self.input_mode)
        excluded = (
            set.union(*(v for k, v in widget_dict.items() if k != self.input_mode))
            - widget_dict[self.input_mode]
        )
        #
        #
        # if self.input_mode == "Manual":
        #     excluded |= {"batch_file", "batch_file_label"}
        # elif self.input_mode == "Batch":
        #     excluded |= {
        #         "fd_file",
        #         "fd_state",
        #         "fd_exposure",
        #         "nd_state",
        #         "nd_exposure",
        #         "exp_file",
        #         "exp_state",
        #         "exp_exposures",
        #         "drop_first",
        #         "d_percentage",
        #         "pH",
        #         "temperature",
        #         "n_term",
        #         "c_term",
        #         "sequence",
        #         "add_dataset_button",
        #         "measurement_name",
        #         "download_spec_button",
        #     }
        # elif self.input_mode == "Database":
        #     excluded |= {
        #         "fd_file",
        #         "fd_state",
        #         "fd_exposure",
        #         "nd_state",
        #         "nd_exposure",
        #         "exp_file",
        #         "exp_state",
        #         "exp_exposures",
        #         "drop_first",
        #         "d_percentage",
        #         "pH",
        #         "temperature",
        #         "n_term",
        #         "c_term",
        #         "sequence",
        #         "add_dataset_button",
        #         "measurement_name",
        #         "download_spec_button",
        #     }

        # RFU mode input takes additional ND control
        if not self.nd_control:
            excluded |= {"nd_file", "nd_state", "nd_exposure"}

        # 'main' input mode takes additional HD labelling experiment parameters
        if not self.show_pH:
            excluded |= {"pH"}
        if not self.show_temperature:
            excluded |= {"temperature"}
        if not self.show_d_percentage:
            excluded |= {"d_percentage"}

        self._excluded = list(excluded)
        self.update_box()

    @param.depends("input_files", watch=True)
    def _read_files(self):
        if self.input_files:
            self.data_files = {
                name: DataFile(
                    name=name,
                    filepath_or_buffer=StringIO(byte_content.decode("UTF-8")),
                    format="DynamX",
                )
                for name, byte_content in zip(
                    self.widgets["input_files"].filename, self.input_files
                )
            }

            lens = [len(data_file.data) for data_file in self.data_files.values()]

            self.parent.logger.info(
                f'Loaded {len(self.input_files)} file{"s" if len(self.input_files) > 1 else ""} with a total '
                f"of {sum(lens)} peptides"
            )
        else:
            self.data_files = {}

        self.data_file_history |= self.data_files

        self.c_term = 0

        self._update_fd_file()
        self._update_fd_state()
        self._update_fd_exposure()

        self._update_nd_file()
        self._update_nd_state()
        self._update_nd_exposure()

        self._update_exp_file()
        self._update_exp_state()
        self._update_exp_exposure()

    def _update_fd_file(self):
        objects = list(self.data_files.keys())
        self.param["fd_file"].objects = objects
        self.fd_file = objects[0]

    @param.depends("fd_file", watch=True)
    def _update_fd_state(self):
        if self.data_files:
            states = list(self.data_files[self.fd_file].data["state"].unique())
            self.param["fd_state"].objects = states
            self.fd_state = states[0]
        else:
            self.param["fd_state"].objects = []

    @param.depends("fd_state", watch=True)
    def _update_fd_exposure(self):
        if self.data_files:
            df = self.data_files[self.fd_file].data
            # Get peptides only which belong to selected state
            fd_entries = df[df["state"] == self.fd_state]
            exposures = list(np.unique(fd_entries["exposure"]))
        else:
            exposures = []
        self.param["fd_exposure"].objects = exposures
        if exposures:
            self.fd_exposure = exposures[0]

    def _update_nd_file(self):
        objects = list(self.data_files.keys())
        self.param["nd_file"].objects = objects
        self.nd_file = objects[0]

    @param.depends("nd_file", watch=True)
    def _update_nd_state(self):
        if self.data_files:
            states = list(self.data_files[self.nd_file].data["state"].unique())
            self.param["nd_state"].objects = states
            self.nd_state = states[0]
        else:
            self.param["nd_state"].objects = []

    @param.depends("nd_state", watch=True)
    def _update_nd_exposure(self):
        if self.data_files:
            df = self.data_files[self.nd_file].data
            # Get peptides only which belong to selected state
            nd_entries = df[df["state"] == self.nd_state]
            exposures = list(np.unique(nd_entries["exposure"]))
        else:
            exposures = []
        self.param["nd_exposure"].objects = exposures
        if exposures:
            self.nd_exposure = exposures[0]

    def _update_exp_file(self):
        objects = list(self.data_files.keys())
        self.param["exp_file"].objects = objects
        self.exp_file = objects[0]

    @param.depends("exp_file", "fd_state", "fd_exposure", "nd_state", "nd_exposure", watch=True)
    def _update_exp_state(self):
        """Find the peptides which are both in the FD and ND states datasets, then from there determine the states which are present in the experiment dataset"""
        if self.exp_file not in self.data_files:
            self.param["exp_state"].objects = []
            return

        # IF self.has_nd... etc
        fd_spec = {"state": self.fd_state, "exposure": {"value": self.fd_exposure, "unit": "s"}}
        nd_spec = {"state": self.nd_state, "exposure": {"value": self.nd_exposure, "unit": "s"}}

        # Get the peptides which are in both the FD and ND states
        dataframes = [self.data_files[self.exp_file].data]
        dataframes.append(filter_peptides(self.data_files[self.fd_file].data, **fd_spec))
        dataframes.append(filter_peptides(self.data_files[self.fd_file].data, **nd_spec))

        intersected = dataframe_intersection(dataframes, by=["start", "stop"])
        states = list(np.unique(intersected[0]["state"]))

        self.param["exp_state"].objects = states

        # todo probably its best to clear all child selectors and then redo everything
        if self.exp_state in states:
            self._update_exp_exposure()

        elif states:
            self.exp_state = states[0]

    @param.depends("exp_state", watch=True)
    def _update_exp_exposure(self):
        if self.exp_file in self.data_files:
            df = self.data_files[self.exp_file].data
            exp_entries = df[df["state"] == self.exp_state]
            exposures = list(np.unique(exp_entries["exposure"]))
            exposures.sort()
        else:
            exposures = []

        self.param["exp_exposures"].objects = exposures
        self.exp_exposures = [e for e in exposures if e != 0.0]

        # Set default measurmenet name to the name of the state
        self.measurement_name = self.exp_state

        if not self.c_term and exposures:
            self.c_term = int(np.max(exp_entries["end"]))

    @property
    def hdx_spec(self) -> dict[str, Any]:
        return {"data_files": self.data_spec, "states": self.state_spec}

    # triggered from 'add measurement' button
    def _add_single_dataset_spec(self):
        """Adds the specifications of a single HDX Measurement to the `state_spec` / `data_spec` dictionaries"""
        if not self.data_files:
            self.parent.logger.info("No data loaded")
            return
        elif self.measurement_name in self.src.hdxm_objects.keys():
            self.parent.logger.info(f"Dataset name {self.measurement_name} already in use")
            return

        metadata = {}
        peptide_spec = {}

        exp_spec = {
            "state": self.exp_state,
            "exposure": {"values": self.exp_exposures, "unit": "s"},
        }

        peptide_spec["experiment"] = exp_spec

        df = self.data_files[self.exp_file].data
        peptides = filter_peptides(df, **exp_spec)
        corrected = correct_d_uptake(peptides)  # remove this step when _sequence field is removed
        exp_spec["data_file"] = self.exp_file

        try:
            verify_sequence(corrected, self.sequence, self.n_term, self.c_term)
        except ValueError as e:
            self.parent.logger.info(f"Cannot add dataset: {e}")
            return

        # Add the data file to the data spec
        if self.exp_file not in self.data_spec:
            self.data_spec[self.exp_file] = {
                "filename": self.exp_file,
                "format": "DynamX",
            }

        # Add the controls
        fd_spec = {
            "data_file": self.fd_file,
            "state": self.fd_state,
            "exposure": {"value": self.fd_exposure, "unit": "s"},
        }
        peptide_spec["FD_control"] = fd_spec
        if self.nd_control:
            nd_spec = {
                "data_file": self.nd_file,
                "state": self.nd_state,
                "exposure": {"value": self.nd_exposure, "unit": "s"},
            }
            peptide_spec["ND_control"] = nd_spec

        # Optionally add ph/temperature/d_percentage if this was input by the user
        if self.show_pH:
            metadata["pH"] = self.pH
        if self.show_temperature:
            metadata["temperature"] = {"value": self.temperature, "unit": "K"}
        if self.show_d_percentage:
            metadata["d_percentage"] = self.d_percentage

        metadata["n_term"] = self.n_term
        metadata["c_term"] = self.c_term
        if self.sequence:
            metadata["sequence"] = self.sequence

        self.state_spec[self.measurement_name] = {
            "peptides": peptide_spec,
            "metadata": metadata,
        }

        obj = self.param["hdxm_list"].objects or []
        self.param["hdxm_list"].objects = obj + [self.measurement_name]

    def _action_load_datasets(self) -> None:
        """Load all specified HDX measurements"""
        if self.input_mode == "Manual":
            data_src = self.data_file_history
            dataset = HDXDataSet(
                data_id=uuid.uuid4().hex, data_files=data_src, hdx_spec=self.hdx_spec
            )
        elif self.input_mode == "Batch":
            if self.hdxm_list:
                self.parent.logger.info("Cannot add data in batch after manually inputting data")
                return

            hdx_spec = yaml.safe_load(self.batch_file.decode("UTF-8"))

            # Convert loaded data_files to data src with correct keys
            data_src = {}
            for data_file, data_file_spec in hdx_spec["data_files"].items():
                data_src[data_file] = self.data_files[data_file_spec["filename"]]

            # store state spec for export
            self.state_spec = hdx_spec["states"]
            self.data_spec = hdx_spec["data_files"]

            dataset = HDXDataSet(
                data_id=uuid.uuid4().hex, data_files=data_src, hdx_spec=self.hdx_spec
            )
            self.param["hdxm_list"].objects = dataset.states
        elif self.input_mode == "Database":
            if self.dataset_id is None:
                return
            dataset = self.data_vault.load_dataset(self.dataset_id)
            self.param["hdxm_list"].objects = dataset.states
            self.parent.logger.info(f"Loaded dataset {dataset.data_id} from hdxms database")

            try:
                authors = ", ".join([author["name"] for author in dataset.metadata["authors"]])
                self.parent.logger.info(f"Author(s): {authors}")
            except KeyError:
                pass

            publications = dataset.metadata.get("publications", [])
            if publications:
                for pub in publications:
                    try:
                        pub_str = pub["title"]
                        if "DOI" in pub:
                            pub_str += f' ([{pub["DOI"]}](https://doi.org/{pub["DOI"]}))'
                        elif "URL" in pub:
                            pub_str += f' ([URL]({pub["URL"]}))'
                        self.parent.logger.info("Publication: " + pub_str)
                    except (KeyError, TypeError):
                        pass
        else:
            raise ValueError("Invalid input mode")

        # Disable input and changing config settings after loading data
        self.widgets["load_dataset_button"].disabled = True
        try:
            config_ctrl = self.parent.control_panels["GlobalSettingsControl"]
            drop_first = config_ctrl.drop_first
            config_ctrl.widgets["drop_first"].disabled = True
            # config_ctrl.widgets["weight_exponent"].disabled = True
        except KeyError:
            drop_first = 2

        for state in dataset.states:
            hdxm = HDXMeasurement.from_dataset(dataset, state, drop_first=drop_first)
            self.src.add(hdxm, state)
            self.parent.logger.info(
                f"Loaded experiment peptides state {hdxm.state} "
                f"({hdxm.Nt} timepoints, {len(hdxm.coverage)} peptides each)"
            )
            self.parent.logger.info(
                f"Average coverage: {hdxm.coverage.percent_coverage:.3}%, "
                f"Redundancy: {hdxm.coverage.redundancy:.2}"
            )

PeptidePropertiesControl

Bases: ControlPanel

Control panel for properties of the peptide for simulating D-uptake

Source code in pyhdx/web/controllers.py
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
class PeptidePropertiesControl(ControlPanel):
    """
    Control panel for properties of the peptide for simulating D-uptake
    """

    header = "Peptide controls"

    _type = "peptide"

    updated = param.Event(
        doc="Trigger for layout to listen to when widgets are updated",
        precedence=-1,
    )

    fasta_sequence = param.String(
        default="KLGPLTAGHH",
        doc="FASTA input for peptide sequence. First amino acid is truncated.",
    )

    temperature = param.Number(
        293.15,
        bounds=(0, 373.15),
        doc="Temperature of the D-labelling reaction",
        label="Temperature (K)",
    )

    pH = param.Number(
        7.5,
        doc="pH of the D-labelling reaction, as read from pH meter",
        label="pH read",
    )

    reload_btn = param.Action(
        lambda self: self._action_reload(),
        label="Reload peptide",
        doc="Reload with new peptide sequence",
    )

    dG = param.Array(
        default=np.array(DG_DEFAULT),
        doc="ΔG values of exchange (kJ/mol). List should be one shorter than input sequence",
    )

    k_open = param.Array(
        default=np.array(K_OPEN_DEFAULT),
        doc="Linderstrøm-Lang opening rates. Values are Log10, units s⁻¹",
    )

    k_close = param.Array(
        default=np.array([]),
        doc="Linderstrøm-Lang closing rates. Values are Log10, units s⁻¹",
    )

    dependent_variable = param.Selector(
        default="k_close",
        objects=["k_open", "k_close", "dG"],
        doc="Select which kinetic parameter should be fixed and derived from the other two",
    )

    def __init__(self, parent, **params) -> None:
        super().__init__(parent, **params)
        self._excluded = ["dG", "k_open", "k_close"]
        with param.edit_constant(self):
            self.k_close = self._get_k_close(self.dG, self.k_open)
        self.model = PeptideUptakeModel(list(self.fasta_sequence), self.temperature, self.pH)
        self.update_k_int_data()

        self.widgets = self.make_dict()  # this is the second trigger of make_dict
        self.update_box()
        self.update_d_uptake()

    @property
    def _layout(self):
        return [("self", self.own_widget_names), ("views.aa_uptake", "y")]

    def update_k_int_data(self):
        data_dict = {"aa": list(self.model.peptide), "k_int": self.model.k_int}
        df = pd.DataFrame(data_dict)
        self.src.add_table("k_int", df)

    def _action_reload(self):
        self.model = PeptideUptakeModel(list(self.fasta_sequence), self.temperature, self.pH)
        self.update_k_int_data()

        self.dependent_variable = "k_close"
        with param.parameterized.batch_call_watchers(self):
            self.dG = np.array(
                DG_DEFAULT[: len(self.model)] + [35.0] * (len(self.model) - len(DG_DEFAULT))
            )
            self.k_open = np.array(
                K_OPEN_DEFAULT[: len(self.model)] + [2.0] * (len(self.model) - len(K_OPEN_DEFAULT))
            )

        with param.edit_constant(self):
            self.k_close = self._get_k_close(self.dG, self.k_open)
        self.widgets = self.make_dict()
        self.updated = True
        self.update_box()
        self.update_d_uptake()

    def make_dict(self):
        dG_limits = {"start": 10, "end": 50}  # kJ/mol
        k_open_limits = {"start": -2, "end": 4}  # Log10
        k_close_limits = {
            k: self._get_k_close(dG_limits[k], k_open_limits[k]) for k in dG_limits.keys()
        }

        model = getattr(self, "model", None)
        names = model.peptide if model is not None else []

        widget_spec = dict(
            widget_type=CompositeFloatSliders,
            orientation="vertical",
            names=names,
        )

        widgets = self.generate_widgets(
            temperature=pn.widgets.FloatInput,
            dG={**widget_spec, **dG_limits},
            k_open={**widget_spec, **k_open_limits},
            k_close={**widget_spec, "disabled": True, **k_close_limits},
        )

        return widgets

    @param.depends("dG", watch=True)
    def value_updated(self):
        if self.dependent_variable == "dG":
            return
        elif self.dependent_variable == "k_open":
            self.k_open = self._get_k_open(self.dG, self.k_close)
            self.update_d_uptake()
        elif self.dependent_variable == "k_close":
            self.k_close = self._get_k_close(self.dG, self.k_open)
            self.update_d_uptake()

    @param.depends("k_open", watch=True)
    def k_open_updated(self):
        if self.dependent_variable == "k_open":
            return
        elif self.dependent_variable == "dG":
            self.dG = self._get_dG(self.k_open, self.k_close)
            self.update_d_uptake()
        elif self.dependent_variable == "k_close":
            self.k_close = self._get_k_close(self.dG, self.k_open)
            self.update_d_uptake()

    @param.depends("k_close", watch=True)
    def k_close_updated(self):
        if self.dependent_variable == "k_close":
            return
        elif self.dependent_variable == "dG":
            self.dG = self._get_dG(self.k_open, self.k_close)
            self.update_d_uptake()
        elif self.dependent_variable == "k_open":
            self.k_open = self._get_k_open(self.dG, self.k_close)
            self.update_d_uptake()

    @param.depends("dependent_variable", watch=True)
    def _fixed_quantity_updated(self):
        widget_keys = ["dG", "k_open", "k_close"]

        widget_keys.remove(self.dependent_variable)
        self.widgets[self.dependent_variable].disabled = True
        for widget_key in widget_keys:
            self.widgets[widget_key].disabled = False

    def update_d_uptake(self):
        time = np.logspace(-2, 6, num=250)

        d_uptake = self.model.eval_analytical(time, 10.0**self.k_open, 10.0**self.k_close)

        cols = [f"aa_{i}" for i in range(len(self.model))]
        idx = pd.Index(time, name="time")
        df = pd.DataFrame(d_uptake, index=idx, columns=cols)
        df["sum"] = df.sum(axis=1)

        self.src.add_table("d_uptake", df)
        self.src.updated = True

    # TODO input can also be numpy arrays
    def _get_k_open(self, dG: npt.ArrayLike, k_close: npt.ArrayLike) -> npt.ArrayLike:
        return k_close - dG * 1e3 / (np.log(10) * R * self.temperature)

    def _get_k_close(self, dG: npt.ArrayLike, k_open: npt.ArrayLike) -> npt.ArrayLike:
        return k_open + dG * 1e3 / (np.log(10) * R * self.temperature)

    def _get_dG(self, k_open: npt.ArrayLike, k_close: npt.ArrayLike) -> npt.ArrayLike:
        return np.log(10) * (k_close - k_open) * 1e-3 * R * self.temperature

    # move to base class?
    @property
    def src(self):
        return self.sources["main"]

    def update_limits(self):
        ...