cobramod.visualization.converter ================================ .. py:module:: cobramod.visualization.converter .. 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. - create_reaction: Returns a :class:`cobramod.visualization.items.Reaction`. - 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. Attributes ---------- .. autoapisummary:: cobramod.visualization.converter.debug_log cobramod.visualization.converter.Position Classes ------- .. autoapisummary:: cobramod.visualization.converter.EscherIntegration cobramod.visualization.converter.Node cobramod.visualization.converter.Reaction cobramod.visualization.converter.JsonDictionary Functions --------- .. autoapisummary:: cobramod.visualization.converter.get_mapping cobramod.visualization.converter.transpose Module Contents --------------- .. py:class:: EscherIntegration(map_name = None, map_json = None, reaction_data = None, reaction_scale = None, reaction_styles = None, never_ask_before_quit = False) Bases: :py:obj:`anywidget.AnyWidget` .. versionadded:: 1.3.0 An alternative Python integration for `Escher `_ . .. py:attribute:: reaction_styles :type: Optional[List[Any]] .. py:attribute:: map_name .. py:attribute:: map_json .. py:attribute:: reaction_scale .. py:attribute:: reaction_data :type: Optional[Dict[str, float]] .. py:attribute:: never_ask_before_quit .. py:property:: model_data .. py:property:: embedded_css .. py:property:: options .. py:method:: save_html(filepath) This method creates a standalone HTML file that contains all the data of the Escher map and loads it automatically when it is opened. :param filepath: The file in which the HTML file is to be saved. .. py:data:: debug_log .. py:class:: Node(node_type, x, y, label_x = None, label_y = None, bigg_id = '', name = '', node_is_primary = False) Bases: :py:obj:`collections.UserDict` Simple class that represent a node for the JSON schema of Escher. The node type can be either a 'metabolite', 'midmarker' or a 'multimarker'. Markers only need the arguments x and y. Check method :func:`cobramod.visualization.items.Node.__init__` to see all the corresponding arguments. .. py:method:: marker(node_type, x, y, **kwargs) Sets the attribute of the dictionary if the dictionary belongs to a midmarker or multimarker. Arguments specified in __init__. .. py:method:: metabolite(node_type, x, y, label_x, label_y, bigg_id = '', name = '', node_is_primary = False) Returns a dictionary with the arguments if node_type correspond to metabolite. Arguments specified in __init__, .. py:class:: Reaction(*args, **kwargs) Bases: :py:obj:`collections.UserDict` Simple class that represent a reaction for the JSON schema for Escher. A Reaction have the Segment that give the position of the reaction in the canvas. :keyword name: Name of the reaction. :kwtype name: str :keyword bigg_id: Identifier of the reactions. It does not have to be from the database BIGG. :kwtype bigg_id: str :keyword reversibility: If reaction is reversible :kwtype reversibility: bool :keyword label_y: Position in y-axis for the label. :kwtype label_y: float :keyword label_x: Position in x-axis for the label. :kwtype label_x: float :keyword gene_reaction_rule: Gene rules that specify involved genes. :kwtype gene_reaction_rule: str, optional :keyword genes: A list with the genes involved for that identifier. Each item has to be a dictionary with the keys 'bigg_id' and 'name' :kwtype genes: list, optional :keyword metabolites: A list with the metabolites involved. Each iteam has to be an dictionary with the keys 'bigg_id' and 'coefficient'. :kwtype metabolites: list, optional :keyword segments: dictionary with :func:`cobramod.visualization.items.Segment`. They represent the connections between nodes. :kwtype segments: PairDictionary, optional .. py:method:: add_metabolite(bigg_id, coefficient) Add the identifier for a metabolite and its corresponding coefficient into the 'metabolite' key. :param bigg_id: identifier of the metabolite. It does not have to be from BIGG. :type bigg_id: str :param coefficient: Coefficient of the metabolite for the reaction. :type coefficient: float .. py:method:: add_segment(identifier, from_node_id, to_node_id) Adds to the Reaction for given identifier a Segment. It will be visualized in Escher. :param identifier: Number-identifier for the Segment, :type identifier: str :param from_node_id: json data identifier, that represents the initial node for the segment. :type from_node_id: str :param to_node_id: json data identifier, that represents the last node for the segment. :type to_node_id: str .. py:function:: get_mapping(graph) Returns a matrix for the representation of given graph. :param graph: Dictionary with relationship between nodes. A node can have multiple edges, which should be presented as values. :type graph: dict Returns List[list]: Representation of matrix :raises KeyError: If keys in graph are missing .. py:function:: transpose(matrix) Returns a transpose matrix for given matrix. .. py:data:: Position .. 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:method:: get_canvas() .. py:method:: json_dump(indent = None) 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:: add_metabolite(x, y, label_x, label_y, bigg_id, name, node_is_primary) 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, y, node_type) 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:: map_metabolites(metabolite_dict, reaction, top_edge, left_edge, vertical) 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, metabolite_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, column, string, name, identifier, vertical) 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:: color_grading(color, min_max = None, quantile = False, max_steps = 100, n_steps = 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, vertical = False, color = None, min_max = None, quantile = False, max_steps = 100, n_steps = None, custom_integration = False, never_ask_before_quit = 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