Ejemplo n.º 1
0
 def test_unipartite_motif_roles(self):
     for i in range(0, 2):
         result = motif_roles(self.test_filename_u[i],
                              motifsize=i + 2,
                              allroles=True)
         all_roles = result.nodes[result.nodes.keys()[0]].roles.keys()
         roles = [[result.nodes[n].roles[r] for r in all_roles]
                  for n in result.nodes]
         columns = [sum(x) for x in zip(*roles)]
         check_total_nodes = sum(columns) == 3 * 13 or 2 * 2
         check_columns = any(v == 0 for v in columns) == False
         check_rows = [sum(x) for x in roles] == [1] * len(result.nodes)
         self.assertTrue(check_columns and check_rows and check_total_nodes)
Ejemplo n.º 2
0
 def test_bipartite_motif_roles(self):
     for i in range(0, 5):
         result = motif_roles(self.test_filename_b[i],
                              motifsize=i + 2,
                              networktype="bipartite",
                              allroles=False)
         all_roles = result.nodes[result.nodes.keys()[0]].roles.keys()
         roles = [[result.nodes[n].roles[r] for r in all_roles]
                  for n in result.nodes]
         check_columns = any(v == 0
                             for v in [sum(x)
                                       for x in zip(*roles)]) == False
         check_rows = [sum(x) for x in roles] == [1] * len(result.nodes)
         self.assertTrue(check_columns and check_rows)
Ejemplo n.º 3
0
 def test_unipartite_link_roles(self):
     for i in range(0, 2):
         result = motif_roles(self.test_filename_u[i],
                              motifsize=i + 2,
                              links=True,
                              allroles=True)
         all_roles = result.links[result.links.keys()[0]].roles.keys()
         roles = [[result.links[n].roles[r] for r in all_roles]
                  for n in result.links]
         columns = [sum(x) for x in zip(*roles)]
         #Are there the number of links we think there are?
         check_total_links = sum(columns) == sum(
             links_per_motif[i + 2].values())
         check_columns = any(
             v == 0 for v in columns) == False  # Every link has a role?
         check_rows = [
             sum(x) for x in roles
         ] == [1] * len(result.links)  # Every link is in one position only
         self.assertTrue(check_columns and check_rows and check_total_links)
Ejemplo n.º 4
0
def dalig(options, args, r_alignment=True):

    #if options.degree==0 and options.normalization>0:
    #	sys.stderr.write("Not implemented yet the combination degree==0 and normalization>0.\n")

    if r_alignment:
        alignment, n1, n2 = read_alignment(args[2])
    else:
        alignment, n1, n2 = check_alignment(args[2])

    try:
        n1.remove("NULL")
    except KeyError:
        pass
    try:
        n2.remove("NULL")
    except KeyError:
        pass

    ##########THIS IS KIND OF SUPERSHIT.... BUT IT WORKS!
    if options.normalization > 0:
        if len(n1) <= len(n2):
            reduce_n1 = False
            #a_map={key:value for (value, key) in alignment}
            a_map = {}
            for (value, key) in alignment:
                a_map[key] = value
        else:
            reduce_n1 = True
            #a_map={key:value for (key, value) in alignment}
            a_map = {}
            for (key, value) in alignment:
                a_map[key] = value

        net1, net1type, n1 = read_network(args[0],
                                          n1,
                                          options.normalization,
                                          red=reduce_n1,
                                          a_map=a_map)
        net2, net2type, n2 = read_network(args[1],
                                          n2,
                                          options.normalization,
                                          red=reduce_n1 == False,
                                          a_map=a_map)

        alignment = rewrite_alignment(alignment, n1, n2, net1, net2)
    else:
        net1, net1type, n1 = read_network(args[0], n1)
        net2, net2type, n2 = read_network(args[1], n2)
    ##########################################################

    if alignment == []:
        return 'optimal = [ (NULL,NULL:0,0:1)] \noverlap = (0,0)\nEnergy = nan\nNormalized energy = 1'

    #Are the networks unipartite or bipartite?
    if options.bipartite:
        if net1type == False and net2type == False:
            unipartite = net1type
        else:
            print "You are comparing a unipartite network with a bipartite one. Both will be considered as unipartite"
            unipartite = True
    else:
        unipartite = True

    if unipartite:
        net1_roles = motif_roles(
            copy.deepcopy(net1),
            motifsize=2,
        )
        net1_roles2 = motif_roles(
            copy.deepcopy(net1),
            motifsize=3,
        )
        for i in net1_roles:
            net1_roles[i].update(net1_roles2[i])
    else:
        net1_roles = motif_roles(
            copy.deepcopy(net1),
            motifsize=2,
            networktype="bipartite",
        )
        for k in range(3, 5):
            net1_roles2 = motif_roles(
                copy.deepcopy(net1),
                motifsize=k,
                networktype="bipartite",
            )
            for i in net1_roles:
                net1_roles[i].update(net1_roles2[i])

    if unipartite:
        net2_roles = motif_roles(
            copy.deepcopy(net2),
            motifsize=2,
        )
        net2_roles2 = motif_roles(
            copy.deepcopy(net2),
            motifsize=3,
        )
        for i in net2_roles:
            net2_roles[i].update(net2_roles2[i])
    else:
        net2_roles = motif_roles(
            copy.deepcopy(net2),
            motifsize=2,
            networktype="bipartite",
        )
        for k in range(3, 5):
            net2_roles2 = motif_roles(
                copy.deepcopy(net2),
                motifsize=k,
                networktype="bipartite",
            )
            for i in net2_roles:
                net2_roles[i].update(net2_roles2[i])

    dalig_in = dalig_input(net1, net2, net1_roles, net2_roles, alignment)
    # get a random seed
    rnd_seed = random.randint(0, sys.maxint)

    # do we want to start from a random initial alignment?
    if options.palignment:
        aflag = "-a"
    else:
        aflag = ""

    # do we want to start from a random initial alignment?
    if options.neighbours:
        sflag = "-s"
    else:
        sflag = ""

    # call the dalig alignment code
    command = "GSL_RNG_SEED=%s PATH=$PATH:/home/bbr36/.local/bin dalig.x -k %s -l %s -n %s %s %s" % (
        rnd_seed, options.degree, options.cost_function, options.normalization,
        aflag, sflag)

    #dalig_out = tempfile.TemporaryFile()
    process = subprocess.Popen(
        command,
        bufsize=0,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True,
    )

    # write the network and role data to dalig
    process.stdin.write(dalig_in)
    #process.stdin.close()

    output = process.communicate()

    # print out and store the dalig stdout line by line as it comes
    #output=""
    #for line in iter(poutput[0].readline, ''):
    #	output+=line

    #process.wait()

    # get rid of the GSL seed info and any empty lines from stderr
    #dalig_stderr = [i for i in process.stderr.readlines() if "GSL_RNG_SEED" not in i and i != '']
    #if dalig_stderr:
    #	sys.stderr.write(''.join(dalig_stderr))

    return output[0]