Example #1
0
def weighted_motif_stats(mfinderi, motif_stats, stoufferIDs, fweight,
                         allmotifs):

    results = cmfinder.motif_participation(mfinderi)
    CI = {}

    r_l = results.l
    members = cmfinder.intArray(mfinderi.MotifSize)
    while (r_l != None):
        motif = cmfinder.get_motif(r_l.p)
        id = int(motif.id)
        idx = 0

        CI[id] = np.zeros(motif_stats.motifs[id].real)

        am_l = motif.all_members.l

        while (am_l != None):

            cmfinder.get_motif_members(am_l.p, members, mfinderi.MotifSize)
            py_members = [int(members[i]) for i in xrange(mfinderi.MotifSize)]

            weight = fweight([
                motif_stats.links[x].weight
                for x in permutations(py_members, 2) if x in motif_stats.links
            ])

            CI[id][idx] = weight
            idx += 1

            am_l = am_l.next

        r_l = r_l.next

    cmfinder.res_tbl_mem_free_single(results)

    for motif_id in motif_stats.motifs:
        if motif_stats.motifs[motif_id].real > 0:
            motif_stats.motifs[motif_id].mean_weight, motif_stats.motifs[
                motif_id].sd_weight, motif_stats.motifs[
                    motif_id].median_weight, motif_stats.motifs[
                        motif_id].thirdq_weight, motif_stats.motifs[
                            motif_id].firstq_weight = confidence_interval(
                                CI[motif_id])

    if stoufferIDs:
        motif_stats.use_stouffer_IDs()

    return motif_stats
Example #2
0
def role_stats(mfinderi, roles, links, networktype, stoufferIDs, allroles,
               fweight):

    results = cmfinder.motif_participation(mfinderi)

    possible_roles = set([])
    actual_roles = set([])
    if networktype == "unipartite":
        for m, r in UNIPARTITE_ROLES[mfinderi.MotifSize]:
            possible_roles.update([tuple([m] + list(x)) for x in r])
    elif networktype == "bipartite":
        for m, r in BIPARTITE_ROLES[mfinderi.MotifSize]:
            possible_roles.update([tuple([m] + list(x)) for x in r])

    if links:
        possible_linkroles = set([])
        actual_linkroles = set([])
        if networktype == "unipartite":
            for m, l in UNIPARTITE_LINKS_ROLES[mfinderi.MotifSize]:
                possible_linkroles.update([tuple([m] + list(x)) for x in l])
        elif networktype == "bipartite":
            for m, l in BIPARTITE_LINKS_ROLES[mfinderi.MotifSize]:
                possible_linkroles.update([tuple([m] + list(x)) for x in l])

    r_l = results.l
    members = cmfinder.intArray(mfinderi.MotifSize)
    while (r_l != None):
        motif = cmfinder.get_motif(r_l.p)
        id = int(motif.id)

        am_l = motif.all_members.l
        while (am_l != None):
            cmfinder.get_motif_members(am_l.p, members, mfinderi.MotifSize)
            py_members = [int(members[i]) for i in xrange(mfinderi.MotifSize)]

            py_motif = [
                x for x in permutations(py_members, 2) if x in roles.links
            ]

            if roles.weighted:
                weight_motif = fweight(
                    [roles.links[x].weight for x in py_motif])
                weight_i = [
                    fweight([
                        roles.links[x].weight for x in py_motif
                        if x[0] == m or x[1] == m
                    ]) for m in py_members
                ]
                weight = weight_motif / float(sum(weight_i))

            for idm, m in enumerate(py_members):
                npred, nprey = 0, 0
                for othernode in py_members:
                    if (othernode, m) in py_motif:
                        npred += 1
                    if (m, othernode) in py_motif:
                        nprey += 1

                key = (id, npred, nprey)

                # if the node's in and out degrees are insufficient to discern its role
                # we will add the degrees of the nodes it interacts with (its neighbors)
                if key not in possible_roles:
                    if npred > 0:
                        connected_to = set([
                            othernode for othernode in py_members
                            if othernode != m and (othernode, m) in py_motif
                        ])
                        npreys = [
                            sum([(i, j) in py_motif for j in py_members
                                 if j != i]) for i in connected_to
                        ]
                        npreys.sort()
                        key = tuple(list(key) + [tuple(npreys)])
                    else:
                        connected_to = set([
                            othernode for othernode in py_members
                            if othernode != m and (m, othernode) in py_motif
                        ])
                        npreds = [
                            sum([(j, i) in py_motif for j in py_members
                                 if j != i]) for i in connected_to
                        ]
                        npreds.sort()
                        key = tuple(list(key) + [tuple(npreds)])

                if key not in possible_roles:
                    print >> sys.stderr, key
                    print >> sys.stderr, "Apparently there is a role you aren't accounting for in roles.py."
                    sys.exit()

                try:
                    roles.nodes[m].roles[key] += 1
                except KeyError:
                    roles.nodes[m].roles[key] = 1

                if roles.weighted:
                    try:
                        roles.nodes[m].weighted_roles[
                            key] += weight_i[idm] * weight
                    except KeyError:
                        roles.nodes[m].weighted_roles[
                            key] = weight_i[idm] * weight

                actual_roles.add(key)

                if links:
                    for n in py_members:
                        if n != m and (n, m) in roles.links:
                            npred1, npred2, nprey1, nprey2 = 0, 0, 0, 0
                            for othernode in py_members:
                                if (othernode, n) in py_motif:
                                    npred1 += 1
                                if (n, othernode) in py_motif:
                                    nprey1 += 1
                                if (othernode, m) in py_motif:
                                    npred2 += 1
                                if (m, othernode) in py_motif:
                                    nprey2 += 1

                            key = (id, (npred1, nprey1), (npred2, nprey2))

                            if key not in possible_linkroles:
                                key = (id, (npred2, nprey2), (npred1, nprey1))

                            if key not in possible_linkroles:
                                print >> sys.stderr, key
                                print >> sys.stderr, "Apparently there is a role you aren't accounting for in roles.py."

                            try:
                                roles.links[(n, m)].roles[key] += 1
                            except KeyError:
                                roles.links[(n, m)].roles[key] = 1

                            if roles.weighted:
                                try:
                                    roles.links[(
                                        n,
                                        m)].weighted_roles[key] += weight_motif
                                except KeyError:
                                    roles.links[(
                                        n,
                                        m)].weighted_roles[key] = weight_motif

                            actual_linkroles.add(key)

            am_l = am_l.next

        r_l = r_l.next

    cmfinder.res_tbl_mem_free_single(results)

    if not allroles:
        possible_roles = actual_roles
        if links:
            possible_linkroles = actual_linkroles

    for n in roles.nodes:
        for r in possible_roles:
            try:
                x = roles.nodes[n].roles[r]
            except KeyError:
                roles.nodes[n].roles[r] = 0

            if roles.weighted:
                try:
                    x = roles.nodes[n].weighted_roles[r]
                except KeyError:
                    roles.nodes[n].weighted_roles[r] = 0

    if links:
        for n in roles.links:
            for r in possible_linkroles:
                try:
                    x = roles.links[n].roles[r]
                except KeyError:
                    roles.links[n].roles[r] = 0

                if roles.weighted:
                    try:
                        x = roles.links[n].weighted_roles[r]
                    except KeyError:
                        roles.links[n].weighted_roles[r] = 0

    if stoufferIDs:
        roles.use_stouffer_IDs()

    return roles
Example #3
0
def participation_stats(mfinderi, participation, links, stoufferIDs, allmotifs,
                        fweight):

    results = cmfinder.motif_participation(mfinderi)

    r_l = results.l
    members = cmfinder.intArray(mfinderi.MotifSize)
    while (r_l != None):
        motif = cmfinder.get_motif(r_l.p)
        id = int(motif.id)

        am_l = motif.all_members.l
        while (am_l != None):
            cmfinder.get_motif_members(am_l.p, members, mfinderi.MotifSize)
            py_members = [int(members[i]) for i in xrange(mfinderi.MotifSize)]

            if participation.weighted:

                weight = fweight([
                    participation.links[x].weight
                    for x in permutations(py_members, 2)
                    if x in participation.links
                ])

                for m in py_members:
                    try:
                        participation.nodes[m].motifs[id] += 1
                    except KeyError:
                        participation.nodes[m].motifs[id] = 1
                    try:
                        participation.nodes[m].weighted_motifs[id] += weight
                    except KeyError:
                        participation.nodes[m].weighted_motifs[id] = weight
            else:
                for m in py_members:
                    try:
                        participation.nodes[m].motifs[id] += 1
                    except KeyError:
                        participation.nodes[m].motifs[id] = 1

            if links:
                for m in permutations(py_members, 2):
                    if m in participation.links:
                        try:
                            participation.links[m].motifs[id] += 1
                        except KeyError:
                            participation.links[m].motifs[id] = 1

                        if participation.weighted:
                            try:
                                participation.links[m].weighted_motifs[
                                    id] += weight
                            except KeyError:
                                participation.links[m].weighted_motifs[
                                    id] = weight

            am_l = am_l.next

        r_l = r_l.next

    cmfinder.res_tbl_mem_free_single(results)

    possible_motifs = set(participation.motifs.keys())

    for r in possible_motifs:
        for n in participation.nodes:
            try:
                x = participation.nodes[n].motifs[r]
            except KeyError:
                participation.nodes[n].motifs[r] = 0

            if participation.weighted:
                try:
                    x = participation.nodes[n].weighted_motifs[r]
                except KeyError:
                    participation.nodes[n].weighted_motifs[r] = 0

        if links:
            for n in participation.links:
                try:
                    x = participation.links[n].motifs[r]
                except KeyError:
                    participation.links[n].motifs[r] = 0

                if participation.weighted:
                    try:
                        x = participation.links[n].weighted_motifs[r]
                    except KeyError:
                        participation.links[n].weighted_motifs[r] = 0

    if stoufferIDs:
        participation.use_stouffer_IDs()

    return participation