fluxpy.illustrations.build_escher_map ===================================== .. py:module:: fluxpy.illustrations.build_escher_map .. autoapi-nested-parse:: JSON creator This module handles the convertion of strings into a proper object that can be used to create a JSON string, which can be later used in Escher. The main class of this module is :class:`cobramod.visualization.converter.JsonDictionary`. This class is able to parse and store data as JSON objects. To check the attributes for each JSON object, please read the documentation of :mod:`cobramod.visualization.items`. Important methods: - json_dump: The data can be parsed into a JSON. (str) - add_metabolite: Add metabolite-node into the JsonDictionary. - add_marker: Add a marker-node into the JsonDictionary. - add_reaction: Parses a reaction string and add the information into the JsonDictionary. - add_blank: Adds a empty reaction. This is useful for the extra space in the visualizations. - visualize: Saves Escher visualization as a HTML and return the Escher Builder. Classes ------- .. autoapisummary:: fluxpy.illustrations.build_escher_map.Position fluxpy.illustrations.build_escher_map.JsonDictionary Functions --------- .. autoapisummary:: fluxpy.illustrations.build_escher_map._convert_string fluxpy.illustrations.build_escher_map._color2np_rgb fluxpy.illustrations.build_escher_map._divide_values fluxpy.illustrations.build_escher_map._get_set fluxpy.illustrations.build_escher_map._get_last_number fluxpy.illustrations.build_escher_map._add_reaction_markers fluxpy.illustrations.build_escher_map.add_marker fluxpy.illustrations.build_escher_map.go_for_markers_for_a_reaction fluxpy.illustrations.build_escher_map.build_markers Module Contents --------------- .. py:class:: Position Bases: :py:obj:`tuple` .. py:attribute:: row .. py:attribute:: column .. py:function:: _convert_string(string: str) -> dict Converts a :class:`cobra.Reaction` reaction-string into a dictionary, and returns a dictionary with the corresponding participants and their coefficients. Examples: 'C00001_c + 2 C00002_c --> C00009_c + C00080_c + G11113_c' 'C00002_c + C00033_c <=> C00227_c + G11113_c' 'C00002_c + C00033_c <-- C00227_c + G11113_c' .. py:function:: _color2np_rgb(color: Union[str, List[int], None]) -> numpy.ndarray This function translates the rgb or string representation into a numpy array. The string representation must follow the css standard. :param color: The color to be translated. :returns: Numpy array with the rgb representation of the color or with the values [220,220,220] as standard return for incorrect inputs. .. py:function:: _divide_values(flux: List[float], min_max: Optional[List[float]] = None) -> Tuple[List[float], List[float], bool, bool] This function divides a list into two, one consisting of the positive values and one of the negative values. :param flux: Array that is to be divided into positive and negative values. :type flux: List[int] :param min_max: List consisting of two values. These values determine the maximum value and minimum value that are taken into account in the distribution. All values outside this interval are ignored. :type min_max: [int,int] :returns: This function returns two lists and two bools. The first return is the list consisting of positive values. The second return is the list with negative values. The third return is a bool that indicates whether min_max consists of two positive values. The fourth return describes whether min_max consists of two negative values. .. py:class:: JsonDictionary Bases: :py:obj:`collections.UserDict` Create a JsonDictionary object which can be used to parse information into JSON files to be later read by Escher. When creating the object, some keyword arguments may be passed. :keyword head: General information of the JSON. Keys included: map_name, map_id, map_description, homepage, schema. :kwtype head: dict, optional :keyword reactions: Dictionary with multiple :class:`cobramod.visualization.items.Reaction` where the key is the number of the reaction and the value the object. Defaults to empty dictionary. :kwtype reactions: dict, optional :keyword nodes: Dictionary with multiple :class:`cobramod.visualization.items.Node` where the key is the number of the Node and the value the corresponding object. Defaults to empty dictionary. :kwtype nodes: PairDictionary :keyword text_labels: Dictionary with the custom text inside the canvas. :kwtype text_labels: dict, optional :keyword canvas: x and y position, width and height of the white area in Escher. :kwtype canvas: dict, optional .. attribute:: CANVAS_WIDTH Width for the canvas. Defaults to 1500. :type: float .. attribute:: CANVAS_HEIGHT Height for the canvas. Defaults to 1500. :type: float .. attribute:: R_WIDTH Width of a reaction. Defaults to 350. :type: float .. attribute:: R_HEIGHT Height of a reaction. Defaults to 270. :type: float .. attribute:: reaction_data Dictionary with the solution to be visualized. Default to empty dictionary. :type: Dict[str, float] .. py:attribute:: CANVAS_WIDTH :type: float :value: 0 .. py:attribute:: CANVAS_HEIGHT :type: float :value: 0 .. py:attribute:: X :value: 0 .. py:attribute:: Y :value: 0 .. py:attribute:: R_WIDTH :type: float :value: 650 .. py:attribute:: R_HEIGHT :type: float :value: 450 .. py:attribute:: _overview .. py:attribute:: flux_solution :type: Dict[str, float] :value: None .. py:attribute:: graph :type: dict .. py:attribute:: reaction_strings .. py:attribute:: reaction_scale .. py:method:: get_canvas() -> dict .. py:method:: json_dump(indent: Optional[int] = None) -> str Returns a string that is the JSON representation of this class. :param indent: Defines the indentation for the JSON. Defaults to None. :type indent: int .. py:method:: _get_set(item: str) -> set Return a set for the keys of either the reactions, the nodes or segments for all reactions. Options for item: "nodes", "segments", "reactions". .. py:method:: _get_last_number(item: str) -> int Returns the largest number of the keys from either reactions, nodes, or segments from each reaction. Options for item: "nodes", "segments", "reactions". .. py:method:: add_metabolite(x: float, y: float, label_x: float, label_y: float, bigg_id: str, name: str, node_is_primary: bool) Add a metabolite-type node into the JsonDictionary. The key will be always the last node number. :param x: Position in x-axis for the node. :type x: float :param y: Position in y-axis fot the node. :type y: float :param label_x: Position of the label in the x-axis. :type label_x: float :param label_y: Position of the label in the y-axis. :type label_y: float :param bigg_id: Identifier of the metabolite. It does not have to be from BIGG. :type bigg_id: str :param name: Name of the label for the metabolite. :type name: str :param node_is_primary: True if node should represent a primary metabolite, i.e. Node is visually larger. :type node_is_primary: bool .. py:method:: add_marker(x: float, y: float, node_type: str) Add a marker-type node into the JsonDictionary. Node can be a midmarker or a multimarker. These markers are located in the middle of the reaction. A midmarker is located between two multimarkers. :param x: Position in x-axis for the node. :type x: float :param y: Position in y-axis for the node. :type y: float :param node_type: Type of marker. Options: 'midmarker' or 'multimarker'. :type node_type: str .. py:method:: _add_reaction_markers(identifier: str, top_edge: float, left_edge: float, vertical: bool) Add the corresponding midmarker and multimarkers into the JsonDictionary. Nodes will be added to the corresponding reaction data. Number for the nodes will not repeat themselves. If vertical is passed, then the reaction markers will be created in order to represent a vertical pathway. .. py:method:: _get_matrix_reactions(vertical: bool, position: int) -> list Return a List with the names of the reactions, which are located in given position. The position can be from a column or a row. Vertical defines the orientation. .. py:method:: _get_products(reactions: list) -> Dict[str, list] Returns a dictionary where keys are the reactions of given list and the values the products of the reactions. :param reactions: List with reaction identifiers. :type reactions: list :returns: Dictionary with products for given reactions. :rtype: dict .. py:method:: _find_shared(metabolite: str, products: Dict[str, list]) -> tuple Returns the node number of the metabolite and the reaction involved if found in given dictionary with products. :param metabolite: Identifier of the metabolite. :type metabolite: str :param products: Dictionary with reactions and their corresponding products as values. :type products: dict :returns: the node_number for the metabolite and the name of the involved reaction. :rtype: tuple .. py:method:: map_metabolites(metabolite_dict: dict, reaction: cobramod.visualization.items.Reaction, top_edge: float, left_edge: float, vertical: bool) Creates the metabolites from given dictionary and complements the :class:`cobramod.visualization.items.Reaction`. Moreover, it creates the corresponding metabolites-nodes for the JsonDictionary class. :param metabolite_dict: Dictionary with metabolites and their coefficients. :type metabolite_dict: dict :param reaction: Reaction that will include the metabolite. :type reaction: Reaction :param top_edge: Position for the top edge of the reaction-box. :type top_edge: float :param left_edge: Position for the left edge of the reaction-box. :type left_edge: float :param vertical: Defines if metabolite should be map vertically. :type vertical: bool .. py:method:: add_segments(reaction: cobramod.visualization.items.Reaction, metabolite_dict: dict) Add the segments to given Reaction. This will make the visuals in Escher. The information about the nodes of metabolites in located in the JsonDictionary. A reaction will have 2 + number of metabolite as its number of segments. :param metabolite_dict: Dictionary with metabolites and their coefficients. :type metabolite_dict: dict :param reaction: Reaction to extend. :type reaction: Reaction .. py:method:: add_reaction(row: int, column: int, string: str, name: str, identifier: str, vertical: bool) Parses and add given reaction string as a reaction for the JsonDictionary. It will automatically create all the necessary nodes and segments for the JSON. :param string: Reaction string to be parsed. :type string: str :param identifier: Identifier for the reaction. :type identifier: str :param row: Row number from the visualization matrix. :type row: int :param column: Column number of the visualization matrix. :type column: int :param name: The name of the reaction. :type name: str :param vertical: If reaction should be displayed vertically. :type vertical: bool .. py:method:: _reset() Resets the data of JsonDictionary to its initial state. This helps when calling the function multiple times. .. py:method:: color_grading(color: List[Union[str, List[int], None]], min_max: Optional[List[float]] = None, quantile: bool = False, max_steps: int = 100, n_steps: Optional[int] = None) Function that creates a color scale between two predefined colors. The number of color gradations corresponds to the number of fluxes. :param color: list of two colors. The first color defines the endpoint for positive values, the second for negative values. The colors must be passed in their rgb representation. :type color: list of str or list of list of int :param min_max: List consisting of two floats. The first int describes the minimum, the second the maximum. These two values are additionally added as data values and at the same time ensure that all values greater or less than these are ignored when creating the gradient. :type min_max: list of float,optional :param quantile: Defines whether quantiles are to be used for the creation of the gradient. Otherwise, the steps are equally distributed between the minimum and maximum. :type quantile: bool, optional :param n_steps: Sets the number of color steps. :type n_steps: int, optional :param max_steps: Sets the maximum number of color steps. :type max_steps: int, optional .. py:method:: visualize(filepath: Union[str, pathlib.Path], vertical: bool = False, color: Optional[List[Union[str, List[int], None]]] = None, min_max: Optional[List[float]] = None, quantile: bool = False, max_steps: int = 100, n_steps: Optional[int] = None, custom_integration: bool = False, never_ask_before_quit: bool = False) Saves the visualization of the JsonDictionary in given path as a HTML. Returns the builder for the JsonDictionary. If method is called in Jupyter or Qtconsole, it will show the embedded builder of the escher visualization. Else, it will open the default browser of the operating system and will load the previously saved HTML. .. note:: Blank spaces are removed from the reactions. :param filepath: Path, optional Path for the HTML. Defaults to "pathway.html" in the current working directory :param vertical: bool, optional Defines if pathway should be illustrated vertically. Defaults to False. :param color: list of str or list of lost of int, optional A list consisting of two entries. These define the endpoints for a color gradient. The entries can either be the colors as a list with three elements that define the RGB values or a string that defines the color name according to the css standard. If None is used here, no gradient will be generated. Defaults to None. Example: ["orange", "green"] or [[255, 165, 0], [0, 128, 0]] :param min_max: list of float, optional List consisting of two ints. The first int describes the minimum, the second the maximum. These two values are additionally added as data values and at the same time ensure that all values greater or less than these are ignored when creating the gradient. With Quantile = False, a data-independent gradient is created. :param quantile: bool, optional Defines whether quantiles are to be used for the creation of the gradient. Otherwise, the steps are equally distributed between the minimum and maximum. :param max_steps: int, optional Sets the maximum number of color steps. :param n_steps: int, optional Sets the maximum number of color steps. :returns: Escher builder object for the visualization :rtype: Builder .. seealso:: Color names according to the css standard: https://www.w3schools.com/cssref/css_colors.asp .. py:function:: _get_set(self, item: str) -> set Return a set for the keys of either the reactions, the nodes or segments for all reactions. Options for item: "nodes", "segments", "reactions". .. py:function:: _get_last_number(self, item: str) -> int Returns the largest number of the keys from either reactions, nodes, or segments from each reaction. Options for item: "nodes", "segments", "reactions". .. py:function:: _add_reaction_markers(self, identifier: str, top_edge: float, left_edge: float, vertical: bool) Add the corresponding midmarker and multimarkers into the JsonDictionary. Nodes will be added to the corresponding reaction data. Number for the nodes will not repeat themselves. If vertical is passed, then the reaction markers will be created in order to represent a vertical pathway. .. py:function:: add_marker(all_escher_nodes, x, y, node_type) .. py:function:: go_for_markers_for_a_reaction(all_escher_nodes, node, vertical=False) .. py:function:: build_markers(all_escher_nodes)