Ejemplo n.º 1
0
def color_consensuses_table_cells(jsonified_partial_consensustable, jsonified_consensus_tree):
    if not jsonified_partial_consensustable or not jsonified_consensus_tree:
        return []
    partial_consensustable_data = tools.unjsonify_df(jsonified_partial_consensustable)
    consensustree_data = tools.unjsonify_builtin_types(jsonified_consensus_tree)
    tree = consensustree.dict_to_tree(consensustree_data)

    return consensustable.get_cells_styling(tree, partial_consensustable_data)
Ejemplo n.º 2
0
def update_current_tree_state(jsonified_full_consensustree):
    if not jsonified_full_consensustree:
        return []
    full_consensustree_data = json.loads(jsonified_full_consensustree)
    full_consensus_tree = consensustree.dict_to_tree(full_consensustree_data)
    current_consensustree_data = consensustree.tree_to_dict(
        full_consensus_tree)
    return json.dumps(current_consensustree_data)
Ejemplo n.º 3
0
def update_current_tree_state(jsonified_full_consensustree):
    if not jsonified_full_consensustree:
        return []
    full_consensustree_data = tools.unjsonify_builtin_types(
        jsonified_full_consensustree)
    full_consensus_tree = consensustree.dict_to_tree(full_consensustree_data)
    current_consensustree_data = consensustree.tree_to_dict(
        full_consensus_tree)
    return tools.jsonify_builtin_types(current_consensustree_data)
Ejemplo n.º 4
0
def update_partial_table_data(jsonified_full_consensustable: str, jsonified_tree: str, slider_value: float):
    if not jsonified_full_consensustable or not jsonified_tree:
        return []
    full_consensustable_data = tools.unjsonify_df(jsonified_full_consensustable)
    full_consensustree_data = tools.unjsonify_builtin_types(jsonified_tree)
    full_consensustree_tree = consensustree.dict_to_tree(full_consensustree_data)
    table_without_consensuses_smaller_than_slider = consensustable.remove_smaller_than_slider(full_consensustable_data,
                                                                                              full_consensustree_tree,
                                                                                              slider_value)
    return tools.jsonify_df(table_without_consensuses_smaller_than_slider)
Ejemplo n.º 5
0
def to_consensustree_graph(jsonified_current_consensustree, leaf_info,
                           jsonified_full_consensustable):
    if not jsonified_current_consensustree or not jsonified_full_consensustable:
        return {}
    current_consensustree_data = json.loads(jsonified_current_consensustree)
    current_consensustree_tree = consensustree.dict_to_tree(
        current_consensustree_data)
    full_consensustable_data = pd.read_json(jsonified_full_consensustable)
    return consensustree.get_consensustree_graph(current_consensustree_tree,
                                                 leaf_info,
                                                 full_consensustable_data)
Ejemplo n.º 6
0
def update_partial_table_data(jsonified_full_consensustable: str, jsonified_tree: str,
                              slider_value: float):
    if not jsonified_full_consensustable or not jsonified_tree:
        return []
    full_consensustable_data = pd.read_json(jsonified_full_consensustable)
    full_consensustree_data = json.loads(jsonified_tree)
    full_consensustree_tree = consensustree.dict_to_tree(full_consensustree_data)
    table_without_consensuses_smaller_than_slider = consensustable.remove_smaller_than_slider(
        full_consensustable_data,
        full_consensustree_tree,
        slider_value)
    return table_without_consensuses_smaller_than_slider.to_json()
Ejemplo n.º 7
0
def to_consensus_node_details_table(tree_click_data,
                                    jsonified_full_consensustable,
                                    jsonified_consensustree):
    if not jsonified_full_consensustable or not tree_click_data:
        return []
    clicked_node = tree_click_data['points'][0]
    node_id = clicked_node['pointIndex']
    full_consensustable = tools.unjsonify_df(jsonified_full_consensustable)
    consensustree_data = tools.unjsonify_builtin_types(jsonified_consensustree)
    tree = consensustree.dict_to_tree(consensustree_data)
    node_details_df = consensustable.get_consensus_details_df(
        node_id, full_consensustable, tree)
    return tools.jsonify_df(node_details_df)
Ejemplo n.º 8
0
def to_consensustree_graph(jsonified_current_consensustree, slider_value,
                           leaf_info, jsonified_full_consensustable):
    if not jsonified_current_consensustree or not jsonified_full_consensustable:
        return {}
    current_consensustree_data = tools.unjsonify_builtin_types(
        jsonified_current_consensustree)
    current_consensustree_tree = consensustree.dict_to_tree(
        current_consensustree_data)
    full_consensustable_data = tools.unjsonify_df(
        jsonified_full_consensustable)
    return consensustree.get_consensustree_graph(current_consensustree_tree,
                                                 slider_value, leaf_info,
                                                 full_consensustable_data)
Ejemplo n.º 9
0
def update_consensus_table(jsonified_partial_consensustable, jsonified_consensus_tree):
    if not jsonified_partial_consensustable:
        # return [], [{}], []
        return [{"ID": 0}, {"ID": 1}], [{"name": i, "id": i} for i in ["ID"]], []
    partial_consensustable_data = pd.read_json(jsonified_partial_consensustable)
    consensustable_columns = [{"name": i, "id": i} for i in partial_consensustable_data.columns]
    consensustable_content = partial_consensustable_data.to_dict("rows")

    if not jsonified_consensus_tree:
        return consensustable_content, consensustable_columns, []
    consensustree_data = json.loads(jsonified_consensus_tree)
    tree = consensustree.dict_to_tree(consensustree_data)
    color_cells = consensustable.get_cells_styling(tree, partial_consensustable_data)
    return consensustable_content, consensustable_columns, color_cells
Ejemplo n.º 10
0
    def get_sankey_diagram(self, hidde, zoom_out, slider_values, highlight_seq,
                           click_data, checklist, threshold,
                           consensustable_data, consensustree_data):
        if not self.sequences:
            raise PreventUpdate()

        # RANGE START / END
        range_start = min(self.column_dict[slider_values[0]])
        range_end = max(self.column_dict[slider_values[1]]
                        ) if slider_values[1] in self.column_dict else len(
                            self.diagram)

        label = []
        source = []
        target = []
        value = []
        link_color = []
        colors = dict(A="#FF9AA2", C="#B5EAD7", G="#C7CEEA", T="#FFDAC1")

        # FILTER SEQUENCES (AFFINITY TREE)
        if click_data:
            tree_node_id = click_data['points'][0]['pointIndex']
            full_consensustable = pd.read_json(consensustable_data)
            consensustree_data = json.loads(consensustree_data)
            tree = consensustree.dict_to_tree(consensustree_data)
            node_details_df = consensustable.get_consensus_details_df(
                tree_node_id, full_consensustable, tree)
            filtered_sequences = node_details_df["SEQID"].tolist()
            diagram_filtered = self.construct_diagram(sequences_values=[
                self.sequences[seq] for seq in filtered_sequences
            ])

            for i in range(len(self.column_dict)):
                if i not in diagram_filtered:
                    diagram_filtered[i] = dict(
                        base="",
                        sources={},
                        targets={},
                    )
        else:
            tree_node_id = None
            filtered_sequences = list(self.sequences.keys())
            diagram_filtered = copy.deepcopy(self.diagram)

        if zoom_out:  # EXTREAME ZOOM-OUT
            checklist = [1, 2]
            threshold = len(self.sequences) * 0.2
            range_start = 0
            range_end = len(self.column_dict) - 1

        if 3 in checklist:
            sequences_values = [
                self.sequences[seq] for seq in filtered_sequences
            ]
            new_sequences_values = [
                self._remove_snp(self.consensus_sequence, sequence)
                for sequence in sequences_values
            ]
            diagram_filtered = self.construct_diagram(
                sequences_values=new_sequences_values)
            for i in range(len(self.diagram)):
                if i not in diagram_filtered:
                    diagram_filtered[i] = dict(
                        base="",
                        sources={},
                        targets={},
                    )

        if 2 in checklist and threshold > 0:  # WEAK CONNECTIONS
            weak_nodes = list()
            for node_id in sorted(
                    diagram_filtered.keys())[range_start:range_end + 1]:
                node = diagram_filtered[node_id]
                if node["sources"] and (all([
                        x in weak_nodes for x in node["sources"].keys()
                ]) or all([x <= threshold for x in node["sources"].values()])):
                    weak_nodes.append(node_id)
                    diagram_filtered[node_id]["sources"] = {}
                    diagram_filtered[node_id]["targets"] = {}
                else:
                    diagram_filtered[node_id]["sources"] = {
                        key: value
                        for key, value in node["sources"].items()
                        if value > threshold
                    }
                    diagram_filtered[node_id]["targets"] = {
                        key: value
                        for key, value in node["targets"].items()
                        if value > threshold
                    }

            for node_id in sorted(diagram_filtered.keys())[range_end -
                                                           1:range_start:-1]:
                node = diagram_filtered[node_id]
                if node["targets"] and all(
                    [x in weak_nodes for x in node["targets"].keys()]):
                    weak_nodes.append(node_id)
                    diagram_filtered[node_id]["sources"] = {}
                    diagram_filtered[node_id]["targets"] = {}
                else:
                    diagram_filtered[node_id]["sources"] = {
                        key: value
                        for key, value in node["sources"].items()
                        if key not in weak_nodes
                    }
                    diagram_filtered[node_id]["targets"] = {
                        key: value
                        for key, value in node["targets"].items()
                        if key not in weak_nodes
                    }

        if 1 in checklist:  # CONCAT VERTICLES
            diagram_filtered, diagram_reorganization = self._bound_vertices(
                diagram_filtered, range_start, range_end)
        else:
            diagram_reorganization = dict()

        if highlight_seq and highlight_seq in filtered_sequences:  # HIGHLIGHT SEQUENCE
            highlight_seq_nodes = [
                node_id for node_id in self.sequences[highlight_seq]
                if node_id not in diagram_reorganization.keys()
            ]
        else:
            highlight_seq_nodes = []

        for node_id in sorted(diagram_filtered.keys())[range_start:range_end +
                                                       1]:
            label.append(diagram_filtered[node_id]["base"])
            for t in diagram_filtered[node_id]["targets"]:
                if t <= range_end:
                    source.append(node_id - range_start)
                    target.append(t - range_start)
                    value.append(diagram_filtered[node_id]["targets"][t])
                    link_color.append("#D3D3D3")

                    # HIGHLIGHT SEQUENCE
                    if highlight_seq_nodes and node_id in highlight_seq_nodes:
                        s_id = highlight_seq_nodes.index(node_id)
                        if highlight_seq_nodes[s_id + 1] == t:
                            value[-1] -= 1
                            source.append(node_id - range_start)
                            target.append(t - range_start)
                            value.append(1)
                            link_color.append("#342424")

        colors = dict(A="#FF9AA2", C="#B5EAD7", G="#C7CEEA", T="#FFDAC1")
        fig = go.Figure(
            data=go.Sankey(
                arrangement="snap",
                node=dict(label=[
                    l if len(l) < 5 else f"{l[0]}...{l[-1]}({len(l)})"
                    for l in label
                ],
                          pad=10,
                          color=[
                              colors[l] if l in colors else "gray"
                              for l in label
                          ]),
                link=dict(source=source,
                          target=target,
                          value=value,
                          color=link_color)),
            layout=dict(
                # height=300,
                # width=1600
            ))

        return fig, str(tree_node_id)