def save_schema(): owm = OWM() for module in ( 'owmeta.neuron', 'owmeta.worm', 'owmeta.biology', 'owmeta.cell', 'owmeta.channel', 'owmeta.channelworm', 'owmeta.connection', 'owmeta.document', 'owmeta.evidence', 'owmeta.experiment', 'owmeta.muscle', 'owmeta.network', 'owmeta.plot', 'owmeta.website', 'owmeta.data_trans.bibtex', 'owmeta.data_trans.connections', 'owmeta.data_trans.context_merge', 'owmeta.data_trans.data_with_evidence_ds', 'owmeta.data_trans.neuron_data', 'owmeta.data_trans.wormatlas', 'owmeta.data_trans.wormbase', 'owmeta.sources', 'owmeta.translators', ): owm.save(module)
def setUp(self): self.testdir = tempfile.mkdtemp(prefix=__name__ + '.') self.startdir = os.getcwd() os.chdir(self.testdir) self.cut = OWM() self.cut.non_interactive = True self._default_conf = { 'rdf.store_conf': '$HERE/worm.db', IMPORTS_CONTEXT_KEY: 'http://example.org/imports' }
def test_save_imports(owm_project): modpath = owm_project.make_module('test_module') owm_project.writefile(p(modpath, 'monkey.py'), 'tests/test_modules/owmclitest04_monkey_giraffe.py') print(owm_project.sh('owm save test_module.monkey')) with OWM(owmdir=p(owm_project.testdir, '.owm')).connect() as conn: ctx = Context(ident=conn.conf[IMPORTS_CONTEXT_KEY], conf=conn.conf) trips = set(ctx.stored.rdf_graph().triples((None, None, None))) assert (URIRef(conn.conf[DEFAULT_CONTEXT_KEY]), CONTEXT_IMPORTS, URIRef('http://example.org/primate/monkey')) in trips assert (URIRef(conn.conf[DEFAULT_CONTEXT_KEY]), CONTEXT_IMPORTS, URIRef('http://example.org/ungulate/giraffe')) in trips
class BaseTest(unittest.TestCase): def setUp(self): self.testdir = tempfile.mkdtemp(prefix=__name__ + '.') self.startdir = os.getcwd() os.chdir(self.testdir) self.cut = OWM() self.cut.non_interactive = True self._default_conf = { 'rdf.store_conf': '$HERE/worm.db', IMPORTS_CONTEXT_KEY: 'http://example.org/imports' } def tearDown(self): os.chdir(self.startdir) shutil.rmtree(self.testdir) self.cut.disconnect() def _init_conf(self, conf=None): my_conf = dict(self._default_conf) if conf: my_conf.update(conf) os.mkdir('.owm') with open(p('.owm', 'owm.conf'), 'w') as f: json.dump(my_conf, f)
def add_bundle(owm_project, descriptor=None): owm_project.writefile( 'abundle.yml', descriptor or '''\ --- id: abundle description: I'm a description includes: ["http://example.org/test_ctx"] ''') with OWM(owmdir=p(owm_project.testdir, OD)).connect() as conn: with transaction.manager: graph = conn.conf['rdf.graph'] sg = graph.get_context('http://example.org/test_ctx') sg.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'), Literal('c'))) owm_project.sh('owm bundle register abundle.yml')
def build_matrix(self): conn = OWM().connect() ctx = conn(Context)(ident='http://openworm.org/data') net = ctx.stored(Worm).query().neuron_network() neurons = sorted(list(net.neuron()), key=lambda x: x.name()) neuron_names = [n.name() for n in neurons] n_neurons = len(neuron_names) neuron_ids = range(0, n_neurons) name2id = dict([(name, id) for name, id in zip(neuron_names, neuron_ids)]) neuron_neurotransmitters = [n.neurotransmitter() for n in neurons] def is_inhibitory(synapse): if synapse.synclass() is not None: return 'GABA' in synapse.synclass() else: return 'GABA' in synapse.pre_cell().neurotransmitter() connections = list(net.synapse()) exc_syn_matrix = np.zeros((n_neurons, n_neurons)) inh_syn_matrix = np.zeros((n_neurons, n_neurons)) gj_matrix = np.zeros((n_neurons, n_neurons)) for connection in connections: pre_cell = connection.pre_cell() post_cell = connection.post_cell() weight = connection.number() try: if connection.syntype() == 'send': #Chemical synapses if is_inhibitory(connection): inh_syn_matrix[name2id[pre_cell.name()], name2id[post_cell.name()]] += weight else: exc_syn_matrix[name2id[pre_cell.name()], name2id[post_cell.name()]] += weight elif connection.syntype() == 'gapJunction': gj_matrix[name2id[pre_cell.name()], name2id[post_cell.name()]] = weight except KeyError: pass # Non-neuron was found. # Save results in npy format np.save(self.root + self.exc_path, exc_syn_matrix) np.save(self.root + self.inh_path, inh_syn_matrix) np.save(self.root + self.gj_path, gj_matrix) return exc_syn_matrix, inh_syn_matrix, gj_matrix
def test_translator_list(owm_project): expected = URIRef('http://example.org/trans1') with OWM(owmdir=p(owm_project.testdir, '.owm')).connect() as conn: with transaction.manager: # Create data sources ctx = conn(Context)(ident='http://example.org/context') conn.mapper.process_class(DT1) DT1.definition_context.save(conn.rdf) conn.mapper.declare_python_class_registry_entry( DT1, DataTranslator) # Create a translator ctx(DT1)(ident=expected) ctx_id = conn.conf[DEFAULT_CONTEXT_KEY] main_ctx = conn(Context)(ident=ctx_id) main_ctx.add_import(ctx) main_ctx.save_imports() ctx.save() conn.mapper.save() # List translators assertRegexpMatches(owm_project.sh('owm -o table translator list'), re.compile(expected.n3(), flags=re.MULTILINE))
def owm(**kwargs): r = OWM(owmdir=p(res.testdir, '.owm'), **kwargs) r.userdir = p(res.test_homedir, '.owmeta') return r
def __init__(self): self.owm = OWM() self.ctx = self.owm.default_context.stored
class DSMethods(metaclass=OrderedClass): # Note: Keep the ordering of def __init__(self): self.owm = OWM() self.ctx = self.owm.default_context.stored def neurons(self): neurons = self.owm.translate( NeuronCSVDataTranslator(), data_sources=[NeuronCSVDataSource(key='neurons')], output_key='neurons') self.ctx(neurons).description( "Contains descriptions of C. elegans neurons and is the" " principle such list for OpenWorm") def wormatlas_cells(self): cells = self.owm.translate( WormAtlasCellListDataTranslator(), data_sources=[ WormAtlasCellListDataSource(key='cells'), DWEDS(key='neurons') ], output_key='cells') self.ctx(cells).description( "Lineage names and descriptions of C. elegans cells from Worm Atlas" ) def ion_channels(self): ion_channels = self.owm.translate( WormbaseIonChannelCSVTranslator(), data_sources=[WormbaseIonChannelCSVDataSource(key='ion_channels')], output_key='ion_channels') self.ctx(ion_channels).description( "Contains Channels and ExpressionPatterns") def wormbase_cells(self): wb_cells = self.owm.translate( CellWormBaseCSVTranslator(), data_sources=[ WormBaseCSVDataSource(key='wormbase_celegans_cells') ], output_key='wormbase_cells') self.ctx(wb_cells).description( 'Contains muscles, neurons, and other cells. Neuron data is partially' ' redundant to the Worm Atlas data source') def bently_expression(self): bently_expression = self.owm.translate( NeuronCSVDataTranslator(), data_sources=[NeuronCSVDataSource(key='bently_expression')], output_key='bently_expression') self.ctx(bently_expression).description( 'Adds receptor, neurotransmitter, and neuropeptide relationships for Neurons' ) def muscle_ion_channels(self): muscle_ion_channels = self.owm.translate( WormbaseTextMatchCSVTranslator(), data_sources=[ WormbaseTextMatchCSVDataSource(key='muscle_ion_channels') ], output_key='muscle_ion_channels') self.ctx(muscle_ion_channels).description( 'Adds relationships between muscles and ion channels') def neuron_ion_channels(self): neuron_ion_channels = self.owm.translate( WormbaseTextMatchCSVTranslator(), data_sources=[ WormbaseTextMatchCSVDataSource(key='neuron_ion_channels') ], output_key='neuron_ion_channels') self.ctx(neuron_ion_channels).description( 'Adds relationships between neurons and ion channels') def connectome(self): emmons_connectome = self.owm.translate( NeuronConnectomeCSVTranslator(), data_sources=[ConnectomeCSVDataSource(key='emmons')], named_data_sources=dict(muscles_source=DWEDS(key='wormbase_cells'), neurons_source=DWEDS(key='neurons')), output_key='connectome') self.ctx(emmons_connectome).description('C. elegans connectome') def synclass(self): connectome_synclass = self.owm.translate( NeuronConnectomeSynapseClassTranslator(), data_sources=[DWEDS(key='connectome')], named_data_sources=dict( neurotransmitter_source=ConnectomeCSVDataSource( key='connectome')), output_key='synclass') self.ctx(connectome_synclass).description( 'Adds inferred relationships between connections and neurotransmitters that' ' mediates communication') def openworm_data(self): combined_data_source = self.owm.translate( ContextMergeDataTranslator(), data_sources=[ DWEDS(key='neurons'), DWEDS(key='wormbase_cells'), DWEDS(key='ion_channels'), DWEDS(key='bently_expression'), DWEDS(key='muscle_ion_channels'), DWEDS(key='neuron_ion_channels'), DWEDS(key='connectome'), DWEDS(key='synclass'), DWEDS(key='cells'), ], output_identifier='http://openworm.org/data') def methods(self): return list(x for x in self.member_names if x not in ('save', 'methods') and not x.startswith('_') and isinstance(getattr(self, x), types.MethodType)) def save(self): self.ctx.save()
def fetch_ma_mapping(): """Fetch associations between monoamines and their receptors from Bentley data. :return: dict from full monoamine names (title case) to cognate receptor gene names (upper case). """ url = BASE_URL + "monoamine_receptor_expression.tsv" mapping = defaultdict(set) for row in csv.DictReader(decode_lines(urlopen(url)), delimiter='\t'): if int(row.get("excluded", 0)): continue mapping[row["monoamine"].title()].add(row["receptor"].upper()) return mapping with OWM('../.owm').connect() as conn: ctx = conn(Context)(ident="http://openworm.org/data").stored #Get the worm object. worm = ctx(Worm).query() #Extract the network object from the worm object. net = worm.neuron_network() cell_transmitter_mapping, receptor_cell_mapping = get_mappings() ma_t_r_mapping = fetch_ma_mapping() ma_edges = list( get_possible_extrasynaptic_edges(cell_transmitter_mapping, ma_t_r_mapping, receptor_cell_mapping)) np_t_r_mapping = fetch_np_mapping()