Exemplo n.º 1
0
def is_digraph_to_xgmml_correct(digraph: DiGraph, expected_xgmml: str) -> bool:
    """

    Args:
        test_case_quick:
        expected_xgmml: expected xgmml content.

    Returns:
        bool: True if the created xgmml is the same as the expected, False otherwise.

    """
    gml_system = XGMML(digraph, "test_graph")

    expected_xgmml_lines = [
        line.strip() for line in expected_xgmml.split("\n") if line.strip()
    ]
    produced_xgmml_lines = [
        line.strip() for line in gml_system.to_string().split("\n")
        if line.strip()
    ]

    if not sorted(produced_xgmml_lines) == sorted(expected_xgmml_lines):
        for i, line in enumerate(produced_xgmml_lines):
            if line not in expected_xgmml_lines:
                print("generated line: {}".format(line))
                print("expected line:  {}".format(expected_xgmml_lines[i]))

    return sorted(produced_xgmml_lines) == sorted(expected_xgmml_lines)
Exemplo n.º 2
0
def test_layout_remains_correct_when_adding_node() -> None:
    """
    Testing if the new graph gets the correct layout of the old graph if we add an additional node.

    Returns:
        None

    Raises:
        AssertionError: If the node coordinates of the new graph differ from expected coordinates.

    """
    test_case = Quick("""A_[b]_ppi+_B_[a]; ! A_[(c)]-{p}
                         C_p+_A_[(c)]; ! C_[d]--D_[c]
                         C_[d]_ppi+_D_[c]""")

    reg_graph = RegulatoryGraph(test_case.rxncon_system)
    gml_system = XGMML(reg_graph.to_graph(), "add_node")
    mapped_layout = map_layout2xgmml(gml_system.to_string(), NODE_LAYOUT_MANIPULATION)
    xmldoc_no_layout = minidom.parseString(mapped_layout)
    xmldoc_no_layout_info = _get_labels_and_coordinates_dict(xmldoc_no_layout)
    xmldoc_expected_layout = minidom.parse(NODE_LAYOUT_MANIPULATION)
    xmldoc_expected_layout_info = _get_labels_and_coordinates_dict(xmldoc_expected_layout)

    assert all(xmldoc_no_layout_info[no_layout_node] == xmldoc_expected_layout_info[no_layout_node]
               for no_layout_node in xmldoc_no_layout_info)
    assert all(xmldoc_no_layout_info[expected_layout_node] == xmldoc_expected_layout_info[expected_layout_node]
               for expected_layout_node in xmldoc_expected_layout_info)
Exemplo n.º 3
0
def test_layout_remains_correct_when_removing_node() -> None:
    """
    Testing if the new graph gets tie correct layout of the old graph if we remove a node.

    Returns:
        None

    Raises:
        AssertionError: If the node coordinates of the new graph differ from expected coordinates.

    """
    test_case = Quick("""A_[b]_ppi+_B_[a]""")

    reg_graph = SpeciesReactionGraph(test_case.rxncon_system)
    gml_system = XGMML(reg_graph.to_graph(), "remove_node")
    mapped_layout = map_layout2xgmml(gml_system.to_string(),
                                     NODE_LAYOUT_MANIPULATION)
    xmldoc_no_layout = minidom.parseString(mapped_layout)
    xmldoc_no_layout_info = _get_labels_and_coordinates_dict(xmldoc_no_layout)
    xmldoc_expected_layout = minidom.parse(NODE_LAYOUT_MANIPULATION)
    xmldoc_expected_layout_info = _get_labels_and_coordinates_dict(
        xmldoc_expected_layout)

    assert all(xmldoc_no_layout_info[no_layout_node] ==
               xmldoc_expected_layout_info[no_layout_node]
               for no_layout_node in xmldoc_no_layout_info)
def test_regulatory_graph_layout_remains() -> None:
    """
    Testing if the new graph gets the correct layout of the old graph.

    Returns:
        None

    Raises:
        AssertionError: If the node coordinates of the new graph differ from expected coordinates.

    """

    book = ExcelBook(PHEROMONE_XLS)
    reg_graph = RegulatoryGraph(book.rxncon_system)
    gml_system = XGMML(reg_graph.to_graph(), "pheromone_layout")
    mapped_layout = map_layout2xgmml(gml_system.to_string(), PHEROMONE_XGMML)
    xmldoc_no_layout = minidom.parseString(mapped_layout)
    xmldoc_no_layout_info = _get_labels_and_coordinates_dict(xmldoc_no_layout)
    xmldoc_expected_layout = minidom.parse(PHEROMONE_XGMML)
    xmldoc_expected_layout_info = _get_labels_and_coordinates_dict(
        xmldoc_expected_layout)
    assert all(xmldoc_no_layout_info[no_layout_node] ==
               xmldoc_expected_layout_info[no_layout_node]
               for no_layout_node in xmldoc_no_layout_info)
    assert all(xmldoc_no_layout_info[expected_layout_node] ==
               xmldoc_expected_layout_info[expected_layout_node]
               for expected_layout_node in xmldoc_expected_layout_info)
Exemplo n.º 5
0
def write_xgmml(excel_filename: str, output=None, layout_template_file=None):
    """
    creating the xgmml file from an excel input and writing it into a new file.

    Args:
        excel_filename: Name of the excel input file.
        output: Name of the new output.
        layout_template_file: Name of the layout template file.

    Returns:
        None

    """
    if not output:
        output = os.path.splitext(os.path.basename(excel_filename))[0]

    base_path = os.path.dirname(excel_filename)

    suffix = '_srg'
    if not output.endswith('.xgmml'):
        output = '{0}{1}.xgmml'.format(output, suffix)
    else:
        base_name = output.split('.xgmml')[0]
        output = "{0}{1}.{2}".format(base_name, suffix, 'xgmml')

    graph_filename = os.path.join(base_path, '{0}'.format(output))

    print('graph_filename: ', graph_filename)
    _file_path_existence(graph_filename)

    print('Reading in Excel file [{}] ...'.format(excel_filename))
    excel_book = ExcelBook(excel_filename)

    rxncon_system = excel_book.rxncon_system
    print(
        'Constructed rxncon system: [{} reactions], [{} contingencies]'.format(
            len(rxncon_system.reactions), len(rxncon_system.contingencies)))

    print('Generating reaction species graph output...')
    reg_system = SpeciesReactionGraph(rxncon_system)
    graph = reg_system.to_graph()

    if layout_template_file:
        print('Writing layout information from [{0}] to graph file [{1}] ...'.
              format(layout_template_file, graph_filename))
        gml_system = XGMML(graph, "{}".format(output))
        graph = map_layout2xgmml(gml_system.to_string(), layout_template_file)
        print('Writing reaction species graph file [{}] ...'.format(
            graph_filename))

        with open(graph_filename, "w") as graph_handle:
            graph_handle.write(graph)
    else:
        print('Writing reaction species graph file [{}] ...'.format(
            graph_filename))
        gml_system = XGMML(graph, "{}".format(output))
        gml_system.to_file(graph_filename)