__init__.py 7.7 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 Tiago de Paula Peixoto <tiago@skewed.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from .. import *
from openpyxl import load_workbook
import pandas

title = "Complete C. elegans neurons (2019)"
description = """Networks among neurons of both the adult male and adult hermaphrodite worms C. elegans, constructed from electron microscopy series, to include directed edges (chemical) and undirected (gap junction), and spanning including nodes for muscle and non-muscle end organs.

For chemical connections, directed edges go from pre-synaptic cell to post-synaptic cell. 

The 'connectivity' edge property corresponds to the total number of EM serial sections of connectivity, taking into account both the number of synapses and the sizes of synapses.

To provide complete coverage of the entire nervous system, the data are assembled from multiple animals and include connections added by extrapolation in gaps where no data were available. 

For 'synapse' data the edge property 'synapses' contain the number of synapses scored between each pair of cells. These networks differ from the others in not taking into account the sizes of synapses and in showing only connections scored on electron micrographs.  It does not include any connections inserted by extrapolation.  Thus there are cells showing no connection here that are connected in the the other matrices.  Also note, there are more edges in these networks than the total number of synapses scored (synapse lists, Supplementary Information 3).  This is because polyads are listed here more than once (once for each postsynaptic cell).
33
34

The '_corrected' networks correspond to corrected versions of the network made available in July 2020.
Tiago Peixoto's avatar
Tiago Peixoto committed
35
36
"""
tags = ['Biological', 'Connectome', 'Weighted']
37
url = 'https://wormwiring.org/pages/adjacency.html'
Tiago Peixoto's avatar
Tiago Peixoto committed
38
39
40
41
42
43
44
45
46
47
48
citation = [('Cook et al., "Whole-animal connectomes of both Caenorhabditis elegans sexes." Nature 571, 63-71 (2019)', 'https://www.nature.com/articles/s41586-019-1352-7')]
icon_hash = '5d1cfbf5f7b45cae742fd520'
ustream_license = None
upstream_prefix = 'https://wormwiring.org/si'
files = [('SI%205%20Connectome%20adjacency%20matrices.xlsx', 'male_chemical', None),
         ('SI%205%20Connectome%20adjacency%20matrices.xlsx', 'male_gap_junction', None),
         ('SI%205%20Connectome%20adjacency%20matrices.xlsx', 'hermaphrodite_chemical', None),
         ('SI%205%20Connectome%20adjacency%20matrices.xlsx', 'hermaphrodite_gap_junction', None),
         ('SI%202%20Synapse%20adjacency%20matrices.xlsx', 'male_chemical_synapse', None),
         ('SI%202%20Synapse%20adjacency%20matrices.xlsx', 'male_gap_junction_synapse', None),
         ('SI%202%20Synapse%20adjacency%20matrices.xlsx', 'hermaphrodite_chemical_synapse', None),
49
50
51
52
53
         ('SI%202%20Synapse%20adjacency%20matrices.xlsx', 'hermaphrodite_gap_junction_synapse', None),
         ('SI%205%20Connectome%20adjacency%20matrices,%20corrected%20July%202020.xlsx', 'male_chemical_corrected', None),
         ('SI%205%20Connectome%20adjacency%20matrices,%20corrected%20July%202020.xlsx', 'male_gap_junction_corrected', None),
         ('SI%205%20Connectome%20adjacency%20matrices,%20corrected%20July%202020.xlsx', 'hermaphrodite_chemical_corrected', None),
         ('SI%205%20Connectome%20adjacency%20matrices,%20corrected%20July%202020.xlsx', 'hermaphrodite_gap_junction_corrected', None)]
Tiago Peixoto's avatar
Tiago Peixoto committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

def fetch_upstream(force=False):
    return fetch_upstream_files(__name__.split(".")[-1], upstream_prefix, files,
                                force)

@cache_network()
@coerce_props()
@annotate()
def parse(alts=None):
    global files
    name = __name__.split(".")[-1]
    for fname, alt, fmt in files:
        if alts is not None and alt not in alts:
            continue
        with open_upstream_file(name, fname, "rb") as f:
            wb = load_workbook(f, read_only=True)
70
            if alt in ["male_chemical", "male_chemical_corrected"]:
Tiago Peixoto's avatar
Tiago Peixoto committed
71
                g = parse_connectome(wb["male chemical"])
72
            elif alt in ["hermaphrodite_chemical", "hermaphrodite_chemical_corrected"]:
Tiago Peixoto's avatar
Tiago Peixoto committed
73
                g = parse_connectome(wb["hermaphrodite chemical"])
74
            elif alt in ["male_gap_junction", "male_gap_junction_corrected"]:
Tiago Peixoto's avatar
Tiago Peixoto committed
75
76
77
78
79
                g = parse_connectome(wb["male gap jn asymmetric"])
                g.set_directed(False)
            elif alt == "hermaphrodite_gap_junction":
                g = parse_connectome(wb["herm gap jn asymmetric"])
                g.set_directed(False)
80
81
82
83
            elif alt == "hermaphrodite_gap_junction_corrected":
                g = parse_connectome(wb["hermaphrodite gap jn asymmetric"])
                g.set_directed(False)
            elif alt in ["male_chemical_synapse"]:
Tiago Peixoto's avatar
Tiago Peixoto committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
                g = parse_connectome(wb["male chem synapse adjacency"], moff=0)
                del g.vp["node_type"]
                del g.vp["node_subtype"]
            elif alt == "hermaphrodite_chemical_synapse":
                g = parse_connectome(wb["herm chem synapse adjacency"])
            elif alt == "male_gap_junction_synapse":
                g = parse_connectome(wb["male gap jn synapse adjacency"], moff=0)
                g.set_directed(False)
                del g.vp["node_type"]
                del g.vp["node_subtype"]
            elif alt == "hermaphrodite_gap_junction_synapse":
                g = parse_connectome(wb["herm gap jn synapse adjacency"])
                g.set_directed(False)
            if "synapse" in alt:
                g.ep.synapses = g.ep.connectivity
                del g.ep["connectivity"]
        yield alt, g

def parse_connectome(ws, moff=2):
    g = Graph()

    g.vp.node_type = g.new_vp("string")
    g.vp.node_subtype = g.new_vp("string")
    g.vp.name = g.new_vp("string")
    g.ep.connectivity = g.new_ep("int")

    m = pandas.DataFrame(ws.values).values

    if moff == 2:
        curr_type = None
        for i, t in enumerate(m[0,:]):
            if t is not None:
                curr_type = t
            else:
                m[0,i] = curr_type

        curr_type = None
        for i, t in enumerate(m[1,:]):
            if t is not None:
                curr_type = t
            else:
                m[1,i] = curr_type

    vs = {}
    for i, v in enumerate(m[moff,:]):
        if v is None:
            continue
        vs[v] = g.add_vertex()
        g.vp.name[vs[v]] = v
        v = vs[v]

        if moff == 2:
            try:
                int(m[0, i])
            except (ValueError, TypeError):
                if m[0, i] != None:
                    g.vp.node_type[v] = m[0, i]
            try:
                int(m[1, i])
            except (ValueError, TypeError):
                if m[1, i] != None:
                    g.vp.node_subtype[v] = m[1, i]

    for i in range(m[:,moff+1].shape[0]):
        if i < moff + 1:
            continue
        for j in range(m[moff+1,:].shape[0]):
            if j < moff + 1:
                continue
            if m[i, j] is None:
                continue
            try:
                int(m[i, j])
            except ValueError:
                continue
            v = vs[m[i,moff]]
            u = vs[m[moff,j]]
            e = g.add_edge(u, v)
            g.ep.connectivity[e] = m[i,j]

    return g