def upload_neurons(): try: conn = sqlite3.connect(SQLITE_DB_LOC) cur = conn.cursor() ev = P.Evidence(title="C. elegans sqlite database") w = P.Worm() n = P.Network() w.neuron_network(n) # insert neurons. # save cur.execute(""" SELECT DISTINCT a.Entity FROM tblrelationship, tblentity a, tblentity b where EnID1=a.id and Relation = '1515' and EnID2='1' """) for r in cur.fetchall(): neuron_name = str(r[0]) n.neuron(P.Neuron(name=neuron_name)) ev.asserts(n) ev.save() #second step, get the relationships between them and add them to the graph print("uploaded neurons") except Exception: traceback.print_exc() finally: conn.close()
def do_insert(config="default.conf", logging=False): if config: if isinstance(config, P.Configure): pass elif isinstance(config, str): config = P.Configure.open(config) elif isinstance(config, dict): config = P.Configure().copy(config) else: raise Exception("Invalid configuration object " + str(config)) P.connect(conf=config, do_logging=logging) try: w = P.Worm() net = P.Network() w.neuron_network(net) w.save() upload_neurons() upload_muscles() upload_lineage_and_descriptions() upload_synapses() upload_receptors_and_innexins() upload_types() serialize_as_n3() #infer() except: traceback.print_exc() finally: P.disconnect()
def upload_muscles(): """ Upload muscles and the neurons that connect to them """ try: ev = P.Evidence(title="C. elegans sqlite database") conn = sqlite3.connect(SQLITE_DB_LOC) cur = conn.cursor() w = P.Worm() cur.execute(""" SELECT DISTINCT a.Entity, b.Entity FROM tblrelationship innervatedBy, tblentity b, tblentity a, tblrelationship type_b WHERE innervatedBy.EnID1=a.id and innervatedBy.Relation = '1516' and innervatedBy.EnID2=b.id and type_b.EnID1 = b.id and type_b.Relation='1515' and type_b.EnID2='1519' """) # 1519 is the for r in cur.fetchall(): neuron_name = str(r[0]) muscle_name = str(r[1]) m = P.Muscle(muscle_name) n = P.Neuron(neuron_name) w.muscle(m) m.innervatedBy(n) ev.asserts(w) ev.save() #second step, get the relationships between them and add them to the graph print("uploaded muscles") except Exception: traceback.print_exc() finally: conn.close()
def infer(): from rdflib import Graph from FuXi.Rete.RuleStore import SetupRuleStore from FuXi.Rete.Util import generateTokenSet from FuXi.Horn.HornRules import HornFromN3 try: w = P.Worm() semnet = w.rdf #fetches the entire worm.db graph rule_store, rule_graph, network = SetupRuleStore(makeNetwork=True) closureDeltaGraph = Graph() network.inferredFacts = closureDeltaGraph #build a network of rules for rule in HornFromN3('testrules.n3'): network.buildNetworkFromClause(rule) network.feedFactsToAdd(generateTokenSet(semnet)) # apply rules to original facts to infer new facts # combine original facts with inferred facts for x in closureDeltaGraph: w.rdf.add(x) ###uncomment next 4 lines to print inferred facts to human-readable file (demo purposes) #inferred_facts = closureDeltaGraph.serialize(format='n3') #format inferred facts to notation 3 #inferred = open('what_was_inferred.n3', 'w') #inferred.write(inferred_facts) #inferred.close() except Exception, e: traceback.print_exc()
def test_connection_content_matches(self): """ This test verifies that the content of each connection matches the content in the source. """ ignored_cells = ['hyp', 'intestine'] synapse_tuples = set() # set of tuple representation of synapses csv_tuples = set() # set of tuple representation of csv file synapses = PyOpenWorm.Worm().get_neuron_network().synapses() for synapse in synapses: if synapse.syntype() == 'gapJunction': syn_type = 'chemical' else: syn_type = 'electrical' syn_tuple = (synapse.pre_cell(), synapse.post_cell(), synapse.number(), syn_type) synapse_tuples.add(syn_tuple) # read csv file row by row with open('OpenWormData/aux_data/herm_full_edgelist.csv', 'rb') as csvfile: edge_reader = csv.reader(csvfile) edge_reader.next() # skip header row for row in edge_reader: source, target, weight, syn_type = map(str.strip, row) # ignore rows where source or target is 'hyp' or 'intestine' if source in ignored_cells or target in ignored_cells: continue csv_tuple = (source, target, weight, syn_type) csv_tuples.add(csv_tuple) self.assertTrue(csv_tuples.issubset(synapse_tuples))
def upload_types(): import csv ev = P.Evidence(title="neurons.csv") w = P.Worm() net = w.neuron_network.one() data = dict() for x in csv.reader(open('../aux_data/neurons.csv'), delimiter=';'): types = [] name = x[0] types_string = x[1] if 'sensory' in (types_string.lower()): types.append('sensory') if 'interneuron' in (types_string.lower()): types.append('interneuron') if 'motor' in (types_string.lower()): types.append('motor') data[name] = types for name in data: n = P.Neuron(name=name) types = data[name] for t in types: n.type(t) net.neuron(n) ev.asserts(net) ev.save() print("uploaded types")
def __init__(self): logger.info("Initialising OpenWormReader") P.connect() self.net = P.Worm().get_neuron_network() self.all_connections = self.net.synapses() self.neuron_connections = set(filter(lambda x: x.termination() == u'neuron', self.all_connections)) self.muscle_connections = set(filter(lambda x: x.termination() == u'muscle', self.all_connections)) logger.info("Finished initialising OpenWormReader")
def test_correct_muscle_number(self): """ This test verifies that the worm model has exactly 158 muscles. 95 body wall muscles, 37 Pharynx muscles, 26 other muscles See counts on row 3 here: https://docs.google.com/spreadsheets/d/1NDx9LRF_B2phR5w4HlEtxJzxx1ZIPT2gA0ZmNmozjos/edit#gid=1 """ muscles = PyOpenWorm.Worm().muscles() self.assertEqual(158, len(muscles))
def test_correct_neuron_number(self): """ This test verifies that the worm model has exactly 302 neurons. """ # FIXME: Test execution is not properly isolated -- it fails if # test_compare_to_xls fails. Other conditions may cause # it to pass net = PyOpenWorm.Worm().get_neuron_network() self.assertEqual(302, len(set(net.neuron_names())))
def test_verify_muslces_have_evidence(self): """ For each muscle in PyOpenWorm, verify that there is supporting evidence""" muscles = list(P.Worm().muscles()) muscle_evcheck = [] for mobj in muscles: hasEvidence = len(get_supporting_evidence(mobj)) muscle_evcheck.append(hasEvidence) self.assertTrue(0 not in muscle_evcheck)
def test_verify_connections_have_evidence(self): """ For each connection in PyOpenWorm, verify that there is supporting evidence. """ net = P.Worm().get_neuron_network() connections = list(net.synapses()) evcheck = [] for c in connections: has_evidence = len(get_supporting_evidence(c)) evcheck.append(has_evidence) self.assertTrue(0 not in evcheck)
def _get_cell_info(cells): import PyOpenWorm as P print("Connecting to the PyOpenWorm database...") P.connect() #Get the worm object. worm = P.Worm() #Extract the network object from the worm object. net = worm.neuron_network() #Go through our list and get the neuron object associated with each name. #Store these in another list. some_neurons = [P.Neuron(name) for name in cells] all_neuron_info = collections.OrderedDict() all_muscle_info = collections.OrderedDict() for neuron in some_neurons: print("=====Checking properties of: %s"%neuron) print neuron.triples() print neuron.__class__ short = ') %s'%neuron.name() if 'motor' in neuron.type(): short = 'Mo%s'%short if 'sensory' in neuron.type(): short = 'Se%s'%short if 'interneuron' in neuron.type(): short = 'In%s'%short if _is_muscle(neuron.name()): short = 'Mu%s'%short short = '(%s'%short if 'GABA' in neuron.neurotransmitter(): short = '- %s'%short elif len(neuron.neurotransmitter())==0: short = '? %s'%short else: short = '+ %s'%short info = (neuron, neuron.type(), neuron.receptor(), neuron.neurotransmitter(), short) #print dir(neuron) if _is_muscle(neuron.name()): all_muscle_info[neuron.name()] = info else: all_neuron_info[neuron.name()] = info return all_neuron_info, all_muscle_info
def test_correct_number_unique_neurons(self): """ This test verifies that the worm model has exactly 300 unique neurons making connections. """ synapses = PyOpenWorm.Worm().get_neuron_network().synapses() unique_neurons = set() # set of unique neurons for synapse in synapses: unique_neurons.add( synapse.pre_cell()) # set won't count duplicates self.assertEqual(300, len(unique_neurons))
def test_number_neuron_to_muscle(self): """ This test verifies that the worm model has exactly 1111 neuron to muscle connections. """ synapses = PyOpenWorm.Worm().get_neuron_network().synapses() count = 0 for synapse in synapses: if synapse.termination() == 'muscle': count += 1 self.assertEqual(1111, count)
def identifier(self, *args, **kwargs): ident = DataObject.identifier(self, *args, **kwargs) if 'query' in kwargs and kwargs['query'] == True: if not DataObject._is_variable(ident): return ident owners = self.getOwners(P.Worm().neuron_network.link) data = [] for x in owners: ident = x.identifier(query=True) # XXX: Query is set to true so a fixed identifier isn't generated randomly if not DataObject._is_variable(ident): data.append(ident) data = sorted(data) return self.make_identifier(data)
def upload_receptors_and_innexins(): try: conn = sqlite3.connect(SQLITE_DB_LOC) cur = conn.cursor() w = P.Worm() n = P.Network() w.neuron_network(n) # insert neurons. # save # get the receptor (354) and innexin (355) cur.execute(""" SELECT DISTINCT a.Entity, b.Entity FROM tblrelationship q, tblrelationship r, tblentity a, tblentity b where q.EnID1=a.id and q.Relation = '1515' and q.EnID2='1' and r.EnID1=a.id and r.Relation = '354' and r.EnID2=b.id """) for r in cur.fetchall(): neuron_name = str(r[0]) receptor = str(r[1]) neur = P.Neuron(name=neuron_name) neur.receptor(receptor) n.neuron(neur) cur.execute(""" SELECT DISTINCT a.Entity, b.Entity FROM tblrelationship q, tblrelationship r, tblentity a, tblentity b where q.EnID1=a.id and q.Relation = '1515' and q.EnID2='1' and r.EnID1=a.id and r.Relation = '355' and r.EnID2=b.id """) for r in cur.fetchall(): neuron_name = str(r[0]) innexin = str(r[1]) neur = P.Neuron(name=neuron_name) neur.innexin(innexin) n.neuron(neur) n.save() #second step, get the relationships between them and add them to the graph except Exception: traceback.print_exc() finally: conn.close()
def upload_receptors_and_innexins(): try: conn = sqlite3.connect(SQLITE_DB_LOC) cur = conn.cursor() w = P.Worm() n = w.neuron_network() # insert neurons. # save # get the receptor (361), neurotransmitters (313), neuropeptides (354) and innexins (355) neurons = dict() def add_data_to_neuron(data_kind, relation_code): cur.execute(""" SELECT DISTINCT a.Entity, b.Entity FROM tblrelationship q, tblrelationship r, tblentity a, tblentity b where q.EnID1=a.id and q.Relation = '1515' and q.EnID2='1' and r.EnID1=a.id and r.Relation = '{}' and r.EnID2=b.id """.format(relation_code)) for r in cur.fetchall(): name = str(r[0]) data = str(r[1]) if not (name in neurons): neurons[name] = P.Neuron(name=name) getattr(neurons[name], data_kind)(data) add_data_to_neuron('receptor', 361) add_data_to_neuron('innexin', 355) add_data_to_neuron('neurotransmitter', 313) add_data_to_neuron('neuropeptide', 354) for neur in neurons: n.neuron(neurons[neur]) n.save() #second step, get the relationships between them and add them to the graph print( "uploaded receptors, innexins, neurotransmitters and neuropeptides" ) except Exception: traceback.print_exc() finally: conn.close()
def test_unconnected_neurons(self): """ This test verifies that there are exactly 2 unconnected neurons, i.e., CANL and CANR, in the new connectome. """ # In previous tests, there is a check for exactly 302 neurons in total. # There is also a test for exactly 300 unique neurons making connections. # That means it should be enough to check that the set {CANL, CANR} and # the set of neurons making connections are disjoint. synapses = PyOpenWorm.Worm().get_neuron_network().synapses() connected_neurons = set() unconnected_neurons = {'CANL', 'CANR'} for synapse in synapses: connected_neurons.add(synapse.pre_cell()) self.assertTrue(connected_neurons.isdisjoint(unconnected_neurons))
def test_worm_get_semantic_net(self): g0 = PyOpenWorm.Worm().get_semantic_net() self.assertTrue(isinstance(g0, rdflib.ConjunctiveGraph)) qres = g0.query(""" SELECT ?subLabel #we want to get out the labels associated with the objects WHERE { GRAPH ?g { #Each triple is in its own sub-graph to enable provenance # match all subjects that have the 'is a' (1515) property pointing to 'muscle' (1519) ?subject <http://openworm.org/entities/1515> <http://openworm.org/entities/1519> . } #Triples that have the label are in the main graph only ?subject rdfs:label ?subLabel #for the subject, look up their plain text label. } """) list = [] for r in qres.result: list.append(str(r[0])) self.assertTrue('MDL08' in list)
def do_insert(config="default.conf", logging=False): global SQLITE_EVIDENCE global WORM global NETWORK if config: if isinstance(config, P.Configure): pass elif isinstance(config, str): config = P.Configure.open(config) elif isinstance(config, dict): config = P.Configure().copy(config) else: raise Exception("Invalid configuration object " + str(config)) P.connect(conf=config, do_logging=logging) SQLITE_EVIDENCE = P.Evidence(key="C_elegans_SQLite_DB", title="C. elegans sqlite database") try: WORM = P.Worm() NETWORK = P.Network() WORM.neuron_network(NETWORK) NETWORK.worm(WORM) upload_neurons() upload_muscles() upload_lineage_and_descriptions() upload_connections() upload_receptors_types_neurotransmitters_neuropeptides_innexins() upload_additional_receptors_neurotransmitters_neuropeptides_innexins() print("Saving...") WORM.save() #infer() print("Serializing...") serialize_as_n3() except Exception: traceback.print_exc() finally: P.disconnect()
def get(self,request, format=None): neurons=[] muscles=[] for neuron in P.Neuron().load(): neurons.append(str(neuron.name())) for muscle in P.Muscle().load(): muscles.append(str(muscle.name())) cellname = self.request.query_params.get('cellname', None) if cellname==None: Channels=[] else: if cellname in neurons: net = P.Worm().get_neuron_network() neuron = net.aneuron(cellname) serializer = NeuronDetailSerializer(NeuronDetail(neuron)) elif cellname in muscles: muscle = P.Muscle(cellname) serializer = MuscleDetailSerializer(MuscleDetail(muscle)) print serializer.data return Response(serializer.data)
def read_data(include_nonconnected_cells=False): print_("Initialising OpenWormReader") P.connect() net = P.Worm().get_neuron_network() all_connections = net.synapses() print_("Finished initialising OpenWormReader") conns = [] cells = [] cell_names = get_cells_in_model(net) for s in all_connections: pre = str(s.pre_cell().name()) post = str(s.post_cell().name()) if isinstance(s.post_cell(), P.Neuron) and pre in cell_names and post in cell_names: syntype = str(s.syntype()) syntype = syntype[0].upper() + syntype[1:] num = int(s.number()) synclass = str(s.synclass()) ci = ConnectionInfo(pre, post, num, syntype, synclass) conns.append(ci) if pre not in cells: cells.append(pre) if post not in cells: cells.append(post) print_("Total cells %i (%i with connections)" % (len(cell_names), len(cells))) print_("Total connections found %i " % len(conns)) P.disconnect() if include_nonconnected_cells: return cell_names, conns else: return cells, conns
def upload_synapses(): try: conn = sqlite3.connect(SQLITE_DB_LOC) cur = conn.cursor() w = P.Worm() n = P.Network() w.neuron_network(n) #second step, get the relationships between them and add them to the graph cur.execute("SELECT DISTINCT a.Entity, b.Entity, Weight, Relation FROM tblrelationship, tblentity a, tblentity b where EnID1=a.id and EnID2=b.id and (Relation = '356' OR Relation = '357')") for r in cur.fetchall(): #all items are numbers -- need to be converted to a string first = str(r[0]) second = str(r[1]) third = str(r[2]) syntype = str(r[3]) if syntype == '356': syntype = 'send' else: syntype = 'gapjunction' try: weight = int(third) # NMJs have negative weights. we only want the synaptic connections if weight < 0: syntype = 'gapjunction' weight = -1 * weight except: weight = None if weight: c = P.Connection(pre_cell=first, post_cell=second, number=weight, syntype=syntype) n.synapse(c) e = P.Evidence(author='*****@*****.**') e.asserts(w) e.save() except Exception, e: traceback.print_exc()
def upload_lineage_and_descriptions(): """ Upload lineage names and descriptions pulled from the WormAtlas master cell list Assumes that Neurons and Muscles have already been added """ # XXX: This wants a way to insert cells, then later, to insert neurons from the same set # and have the later recoginzed as the former. Identifier matching limits us here. It would # be best to establish owl:sameAs links to the super class (Cell) from the subclass (Neuron) # at the sub-class insert and have a reasoner relate # the two sets of inserts. cells = dict() try: w = P.Worm() net = w.neuron_network.one() ev = P.Evidence(uri="http://www.wormatlas.org/celllist.htm") # insert neurons. # save cell_data = open(LINEAGE_LIST_LOC, "r") # Skip headers next(cell_data) cell_name_counters = dict() data = dict() for x in cell_data: j = [x.strip().strip("\"") for x in x.split("\t")] name = j[0] lineageName = j[1] desc = j[2] # XXX: These renaming choices are arbitrary and may be inappropriate if name == "DB1/3": name = "DB1" elif name == "DB3/1": name = "DB3" elif name == "AVFL/R": if lineageName[0] == "W": name = "AVFL" elif lineageName[0] == "P": name = "AVFR" if name in cell_name_counters: while (name in cell_name_counters): cell_name_counters[name] += 1 name = name + "("+ str(cell_name_counters[name]) +")" else: cell_name_counters[name] = 0 data[name] = {"lineageName" : lineageName, "desc": desc} for n in net.neuron(): name = n.name.one() neuron_data = data[str(name)] n.lineageName(neuron_data['lineageName']) n.description(neuron_data['desc']) w.cell(n) ev.asserts(w) ev.save() except Exception, e: traceback.print_exc()
def test_worm_get_network(self): self.assertTrue( isinstance(PyOpenWorm.Worm().get_neuron_network(), PyOpenWorm.Network))
def test_worm_muscles(self): self.assertTrue('MDL08' in PyOpenWorm.Worm().muscles()) self.assertTrue('MDL15' in PyOpenWorm.Worm().muscles())
from __future__ import print_function from __future__ import absolute_import import PyOpenWorm as P import os print(os.getcwd()) P.connect('default.conf') def get_names(it): res = [] for x in it: res.append(x.name()) return res w = P.Worm() net = w.neuron_network() print("Retrieving names...") inter = get_names(net.interneurons()) motor = get_names(net.motor()) sensory = get_names(net.sensory()) print("Calculating combinations...") sensmot = set(sensory) & set(motor) sensint = set(sensory) & set(inter) motint = set(motor) & set(inter) sens_only = set(sensory) - set(motor) - set(inter) motor_only = set(motor) - set(sensory) - set(inter) inter_only = set(inter) - set(sensory) - set(motor) tri = motint & set(sensory) motint_no_tri = motint - tri
""" How to get a particular Neuron's gap junctions from the database. """ from __future__ import absolute_import from __future__ import print_function import PyOpenWorm as P #Connect to existing database. P.connect('default.conf') #Put the Worm's Network object in a variable. net = P.Worm().get_neuron_network() #Put a particular Neuron object in a variable ('AVAL' in this example). aval = net.aneuron('AVAL') print("Getting all Connections to/from AVAL, and printing the gap junctions") #We could also put them into an array or do other things with them other than print. num_gjs = 0 for c in aval.connection(): #the `one()` returns a string like "gapJunction" instead of "syntype=gapJunction" if c.syntype.one() == 'gapJunction': num_gjs += 1 print(num_gjs, c)
def __init__(self): logger.info("Initialising OpenWormReader") P.connect() self.net = P.Worm().get_neuron_network() self.all_connections = self.net.synapses() logger.info("Finished initialising OpenWormReader")
def upload_synapses(): import re search_string = re.compile(r'\w+[0]+[1-9]+') replace_string = re.compile(r'[0]+') def normalize(name): # normalize neuron names to match those used at other points # see #137 for elaboration # if there are zeroes in the middle of a name, remove them if re.match(search_string, name): name = replace_string.sub('', name) return name import xlrd try: w = P.Worm() n = P.Network() w.neuron_network(n) combining_dict = {} # Get synapses and gap junctions and add them to the graph s = xlrd.open_workbook('../aux_data/NeuronConnect.xls').sheets()[0] for row in range(1, s.nrows): if s.cell(row, 2).value in ('S', 'Sp', 'EJ'): #We're not going to include 'receives' ('R', 'Rp') since they're just the inverse of 'sends' #Also omitting 'NMJ' for the time being (no model in db) pre = normalize(s.cell(row, 0).value) post = normalize(s.cell(row, 1).value) num = int(s.cell(row, 3).value) if s.cell(row, 2).value == 'EJ': syntype = 'gapJunction' elif s.cell(row, 2).value in ('S', 'Sp'): syntype = 'send' # Add them to a dict to make sure Sends ('S') and Send-polys ('Sp') are summed. # keying by connection pairs as a string (e.g. 'SDQL,AVAL,send'). # values are lists of the form [pre, post, number, syntype]. string_key = '{},{},{}'.format(pre, post, syntype) if string_key in combining_dict.keys(): # if key already there, add to number num += combining_dict[string_key][2] combining_dict[string_key] = [pre, post, num, syntype] for entry in combining_dict: pre, post, num, syntype = combining_dict[entry] c = P.Connection(pre_cell=pre, post_cell=post, number=num, syntype=syntype) n.synapse(c) e = P.Evidence( uri='http://www.wormatlas.org/neuronalwiring.html#Connectivitydata' ) e.asserts(n) e.save() print('uploaded synapses') except Exception, e: traceback.print_exc()