Search code examples
cytoscape

How do you import a network using a confusion matrix?


I have a confusion matrix (I think), which looks like this:

I want to make this network on cytoscape so I can run some modelling, but I am not sure how to.

Any ideas?


Solution

  • To my limited knowledge of Cytoscape, there are no straightforward ways to import a confusion matrix into Cytoscape. Maybe some applications do support it though.

    The most straightforward thing to do would be to convert your matrix to a suitable format, like using the following script for instance.
    Then you would be able to import the resulting CSV in Cytoscape directly, even by drag and dropping to the network panel.

    #!/usr/bin/python
    import sys
    
    DELIMITER = ","
    NO_INTERACTION_VALUE = "0"
    
    if len(sys.argv) != 2:
        print("Please use as follow sh ./matrix-to-list.py <file-to-convert>")
        sys.exit(1)
    
    to_convert_name = sys.argv[1]
    result_name = ".".join(to_convert_name.split(".")[:-1]) + "-converted.csv"
    output = []
    
    with open(to_convert_name, "r") as to_convert:
        rows = [[value.strip() for value in row.split(DELIMITER)] for row in to_convert.readlines()]
        ids = rows[0][1:]
        for i in range(len(rows) - 1):
            row = rows[i + 1]
            target = row[0]
            for j in range(i + 1):
                value = row[j + 1]
                if value != NO_INTERACTION_VALUE:
                    output.append(ids[j] + DELIMITER + target)
    
    with open(result_name, "w") as result_file:
        result_file.write("idA,idB\n")
        result_file.write("\n".join(output))