예제 #1
0
def main(argList):
    # Parse the arguments, which either come from the command line or a list
    # provided by the Python code calling this function
    parser = CreateParser()
    (opts, args) = parser.parse_args(argList)

    print "Parameters: %s" % opts

    if opts.networkFile == "None":
        raise RuntimeError("Must specify an network filename")

    if opts.pathwaySource == "load" and (opts.pathwayPath == "None"
                                         or opts.pathwayListFile == "None"):
        raise RuntimeError(
            "Must specify pathwayPath and pathwayListFile when loading pathways"
        )

    # Create the output path if needed
    if not os.path.exists(opts.outPath):
        print "Creating output directory %s" % opts.outPath
        os.makedirs(opts.outPath)

    # Load the interaction network
    network = NetworkUtil.LoadNetwork(opts.networkFile, weight=True)

    # Load or generate the pathways
    if opts.pathwaySource == "load":
        pathways = LoadPathways(opts.pathwayPath, opts.pathwayListFile)
    elif opts.pathwaySource == "generate":
        pathways = GeneratePathways(network, opts.numPathways, opts.branching,
                                    opts.depth, opts.outPath, opts.name)
    else:
        # Shouldn't be able to get to this case
        raise RuntimeError("%s is not a recognized pathway source" %
                           opts.pathwaySource)

    # Sample from the pathways
    CreateSamples(pathways, opts.samples, opts.fraction,
                  opts.outPath, opts.name, opts.noise, opts.sampleGroups,
                  set(network.nodes()))
def Evaluate(network2Pathway, outFileName, fraction, noise, weightedNetworks=False):
    with open(outFileName, "w") as outFile:
        npSum = 0
        nrSum = 0
        epSum = 0
        erSum = 0
        outFile.write("Steiner forest\tPathway\tTrue prizes\tNoisy prizes\tForest nodes\tPathway nodes\tIntersection nodes\tNode precision\tNode recall\tForest edges\tPathway edges\tIntersection edges\tEdge precision\tEdge recall\n")
        # The name forestFile assumes the networks to evaluate are Steiner forests, but they can
        # be any network        
        for forestFile, pathwayFile in network2Pathway:
            # For each Steiner forest, compute the precision and recall with respect to the original pathway
            forest = NetworkUtil.LoadNetwork(forestFile, weight=weightedNetworks)
            # Remove the artificial node if the forest is not empty
            if "DUMMY" in forest:
                forest.remove_node("DUMMY")
            # NetworkUtil.LoadNetwork only works for the simple format used when writing synthetic
            # pathways.  LoadGraphiteNetwork works for the simple format and the graphite edge list.
            pathway = NetworkUtil.LoadGraphiteNetwork(pathwayFile)
            intersection = NetworkUtil.Intersection(forest, pathway)
            if forest.order() == 0:
                nPrecision = 0
            else:
                nPrecision = float(intersection.order())/forest.order()
            npSum += nPrecision
            nRecall = float(intersection.order())/pathway.order()
            nrSum += nRecall
            if forest.size() == 0:
                ePrecision = 0
            else:
                ePrecision = float(intersection.size())/forest.size()
            epSum += ePrecision
            eRecall = float(intersection.size())/pathway.size()
            erSum += eRecall
            truePrizes = int(math.ceil(fraction*pathway.order()))
            noisyPrizes = int(math.ceil(noise*truePrizes))
            outFile.write("%s\t%s\t%d\t%d\t%d\t%d\t%d\t%f\t%f\t%d\t%d\t%d\t%f\t%f\n" % (os.path.basename(forestFile), os.path.basename(pathwayFile), truePrizes, noisyPrizes, forest.order(), pathway.order(), intersection.order(), nPrecision, nRecall, forest.size(), pathway.size(), intersection.size(), ePrecision, eRecall))
        # Write the average node/edge precision/recall
        outFile.write("Average\t\t\t\t\t\t\t%f\t%f\t\t\t\t%f\t%f\n" % (npSum/len(network2Pathway), nrSum/len(network2Pathway), epSum/len(network2Pathway), erSum/len(network2Pathway)))