Skip to content

Instantly share code, notes, and snippets.

@mwatts15
Last active September 8, 2020 06:22
Show Gist options
  • Select an option

  • Save mwatts15/6544d7a1029ecbd73002 to your computer and use it in GitHub Desktop.

Select an option

Save mwatts15/6544d7a1029ecbd73002 to your computer and use it in GitHub Desktop.
YAROM version of insert_worm.py
import yarom as Y
import traceback
import sqlite3
SQLITE_DB_LOC = '../aux_data/celegans.db'
LINEAGE_LIST_LOC = '../aux_data/C. elegans Cell List - WormAtlas.tsv'
MUSCLE_LINEAGE_LIST_LOC = '../aux_data/muscle_names_to_lineage'
WORM = None
NETWORK = None
def print_evidence():
try:
conn = sqlite3.connect(SQLITE_DB_LOC)
cur = conn.cursor()
cur.execute("SELECT DISTINCT a.Entity, b.Entity, Citations FROM tblrelationship, tblentity a, tblentity b where EnID1=a.id and EnID2=b.id and Citations!='' ")
for r in cur.fetchall():
print(r)
except Exception:
traceback.print_exc()
finally:
conn.close()
def upload_muscles():
""" Upload muscles and the neurons that connect to them
"""
try:
conn = sqlite3.connect(SQLITE_DB_LOC)
cur = conn.cursor()
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():
muscle_name = str(r[1])
m = Y.Muscle(name=muscle_name)
WORM.muscle(m)
n = Y.Neuron(name=str(r[0]))
m.innervatedBy(n)
print("uploaded muscles")
except Exception:
traceback.print_exc()
finally:
conn.close()
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:
cell_data = open(LINEAGE_LIST_LOC, "r")
muscle_cell_data = open(MUSCLE_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 x in muscle_cell_data:
x = x.split(' ')
data[x[0]] = {"lineageName" : x[1].strip()}
def add_data_to_cell(n):
name = n.name.values[0].idl
if str(name) not in data:
print("couldn't find data for",n,name)
return
ccdata = data[str(name)]
if 'lineageName' in ccdata:
n.lineageName(ccdata['lineageName'])
if 'desc' in ccdata:
n.description(ccdata['desc'])
WORM.cell(n)
for n in NETWORK.neuron.values:
add_data_to_cell(n)
for m in WORM.muscle.values:
add_data_to_cell(m)
print ("uploaded lineage and descriptions")
except Exception as e:
traceback.print_exc()
def norn(x):
""" return the next or None of an iterator """
try:
return next(x)
except StopIteration:
return None
def upload_neurons():
try:
conn = sqlite3.connect(SQLITE_DB_LOC)
cur = conn.cursor()
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])
NETWORK.neuron(Y.Neuron(name=neuron_name))
print("uploaded neurons")
except Exception:
traceback.print_exc()
finally:
conn.close()
def upload_receptors_and_innexins():
try:
conn = sqlite3.connect(SQLITE_DB_LOC)
cur = conn.cursor()
# insert neurons.
# save
# get the receptor (361), neurotransmitters (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] = Y.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', 354)
for neur in neurons:
NETWORK.neuron(neurons[neur])
#second step, get the relationships between them and add them to the graph
print ("uploaded receptors and innexins")
except Exception:
traceback.print_exc()
finally:
conn.close()
def upload_types():
import csv
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 = Y.Neuron(name=name)
types = data[name]
for t in types:
n.type(t)
NETWORK.neuron(n)
print ("uploaded types")
def upload_synapses():
import xlrd
try:
#second step, 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 not in ('R', 'Rp', 'NMJ'):
#We're not going to include 'receives' since they're just the inverse of 'sends'
#Also omitting NMJ for the time being
pre = s.cell(row, 0).value
post = s.cell(row, 1).value
if s.cell(row, 2).value == 'EJ':
syntype = 'gapJunction'
else:
syntype = 'send'
num = int(s.cell(row, 3).value)
c = Y.Synapse(pre_cell=Y.Neuron(name=pre), post_cell=Y.Neuron(name=post), number=num, syntype=syntype)
NETWORK.synapse(c)
print('uploaded synapses')
except Exception as e:
traceback.print_exc()
def do_insert():
try:
upload_neurons()
upload_types()
upload_muscles()
upload_lineage_and_descriptions()
upload_synapses()
upload_receptors_and_innexins()
#infer()
print("Saving to disk...")
WORM.save()
print("DONE")
except:
traceback.print_exc()
finally:
Y.disconnect()
if __name__ == '__main__':
import sys
logging = False
if len(sys.argv) > 1 and sys.argv[1] == '-l':
logging = True
Y.connect('default_yarom.conf', do_logging=logging)
import celegans
Y.mapper.MappedClass.remap()
WORM = Y.Worm(key='C. elegans')
NETWORK = Y.Network(key=WORM.idl)
WORM.neuron_network(NETWORK)
do_insert()
MANAL AB.plpppppap
MVR19 ABprpppppaa
MVL10 Capaaaa
MVL12 Capaaap
MVL14 Capaapa
MVL16 Capaapp
MDL14 Capapaa
MDL18 Capapap
MVL19 Capappa
MVL22 Capappp
MDL12 Cappaaa
MDL17 Cappaap
MDL20 Cappapa
MDL24 Cappapp
MDL19 Capppaa
MDL21 Capppap
MDL23 Cappppd
MVL23 Cappppv
MVR10 Cppaaaa
MVR12 Cppaaap
MVR14 Cppaapa
MVR16 Cppaapp
MDR14 Cppapaa
MDR18 Cppapap
MVR20 Cppappa
MVR21 Cppappp
MDR12 Cpppaaa
MDR17 Cpppaap
MDR20 Cpppapa
MDR24 Cpppapp
MDR19 Cppppaa
MDR21 Cppppap
MDR23 Cpppppd
MVR24 Cpppppv
MVL06 Daaaa
MDL08 Daaap
MVL03 Daapa
MVL05 Daapp
MVL08 Dapaa
MDL10 Dapap
MDL07 Dappaa
MDL09 Dappap
MDL11 Dapppa
MDL13 Dapppp
MVR06 Dpaaa
MDR08 Dpaap
MVR03 Dpapa
MVR05 Dpapp
MVR08 Dppaa
MDR10 Dppap
MDR07 Dpppaa
MDR09 Dpppap
MDR11 Dppppa
MDR13 Dppppp
MDL15 M.dlaa
MDL16 M.dlap
MDL22 M.dlpp
MDR15 M.draa
MDR16 M.drap
MDR22 M.drpp
MVL07 MSaapppaa
MVL09 MSaapppap
MVL11 MSaappppa
MVL13 MSaappppp
MDL02 MSapaaap
MVL02 MSapapap
MDL01 MSapappa
MDL04 MSapappp
MVL01 MSappapp
MVL04 MSapppaa
MDL06 MSapppap
MDL03 MSappppa
MDL05 MSappppp
MVL15 MSpappal
MVR15 MSpappap
MVR07 MSpapppaa
MVR09 MSpapppap
MVR11 MSpappppa
MVR13 MSpappppp
MDR02 MSppaaap
MVR02 MSppapap
MDR01 MSppappa
MDR04 MSppappp
MVR01 MSpppapp
MVR04 MSppppaa
MDR06 MSppppap
MDR03 MSpppppa
MDR05 MSpppppp
MVL17 M.vlaa
MVL18 M.vlap
MVL20 M.vlpap
MVL21 M.vlpp
MVR17 M.vraa
MVR18 M.vrap
MVR22 M.vrpap
MVR23 M.vrpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment