diff --git a/OpenWormData/scripts/insert_worm.py b/OpenWormData/scripts/insert_worm.py index c07ca7377..31ce005f5 100644 --- a/OpenWormData/scripts/insert_worm.py +++ b/OpenWormData/scripts/insert_worm.py @@ -67,7 +67,14 @@ def upload_ionchannels(): c.gene_name(gene_name) c.gene_WB_ID(gene_WB_ID) c.description(description) - c.expression_pattern(expression_pattern) + patterns = expression_pattern.split(r' | ') + regex = re.compile(r' *\[([^\]]+)\] *(.*) *') + + matches = [regex.match(pat) for pat in patterns] + patterns = [P.ExpressionPattern(wormbaseID=m.group(1), description=m.group(2)) for m in matches if m is not None] + for pat in patterns: + c.expression_pattern(pat) + c.save() print ("uploaded ion_channel") except Exception: @@ -77,33 +84,15 @@ def upload_ionchannels(): def upload_channelneuron_association(): print ("uploading the channel neuron association") try: - net = NETWORK with open(CHANNEL_NEURON_SOURCE, 'rb') as f: reader = csv.reader(f, delimiter='\t') - rows = 0 + heading = skip_to_header(reader) for row in reader: - neuronlist = [] - if rows < 3: - rows += 1 - continue - elif rows == 3: - heading = row - rows += 1 - continue - rows += 1 - - cols = 0 - for col in row: - if cols >= 0 and cols <= 101: - cols += 1 - else: - if col == '1' or col == '2': - neuronlist.append(heading[cols]) - cols += 1 + neuronlist = extract_neuron_names(heading, row) ch = P.Channel(name=str(row[0])) for neuron in neuronlist: n = P.Neuron(name=str(neuron)) - net.neuron(n) + NETWORK.neuron(n) ch.appearsIn(n) print ("uploaded channel neuron association") except Exception: @@ -113,39 +102,58 @@ def upload_channelneuron_association(): def upload_channelmuscle_association(): print ("uploading the channel muscle association") try: - w = WORM with open(CHANNEL_MUSCLE_SOURCE, 'rb') as f: reader = csv.reader(f, delimiter='\t') - rows = 0 + heading = skip_to_header(reader) for row in reader: - musclelist = [] - if rows < 3: - rows += 1 - continue - elif rows == 3: - heading = row - rows += 1 - continue - rows += 1 - - cols = 0 - for col in row: - if cols >= 0 and cols <= 6: - cols += 1 - else: - if col == '1' or col == '2': - musclelist.append(heading[cols]) - cols += 1 + musclelist = extract_muscle_names(heading, row) ch = P.Channel(name=str(row[0])) for muscle in musclelist: m = P.Muscle(name=str(muscle)) - w.muscle(m) + WORM.muscle(m) ch.appearsIn(m) print ("uploaded channel muscle association") except Exception: traceback.print_exc() +def extract_neuron_names(heading, row): + neuronlist = [] + cols = 0 + for col in row: + if cols >= 0 and cols <= 101: + cols += 1 + else: + if col == '1' or col == '2': + neuronlist.append(heading[cols]) + cols += 1 + return neuronlist + + +def extract_muscle_names(heading, row): + musclelist = [] + cols = 0 + for col in row: + if cols >= 0 and cols <= 6: + cols += 1 + else: + if col == '1' or col == '2': + musclelist.append(heading[cols]) + cols += 1 + return musclelist + + +def skip_to_header(reader): + heading = None + rows = 0 + for row in reader: + if rows == 3: + heading = row + break + rows += 1 + return heading + + def upload_muscles(): """ Upload muscles and the neurons that connect to them """ @@ -646,16 +654,16 @@ def do_insert(config="default.conf", logging=False): WORM.neuron_network(NETWORK) NETWORK.worm(WORM) - upload_neurons() - upload_muscles() + # upload_neurons() + # upload_muscles() upload_ionchannels() - upload_channelneuron_association() - upload_channelmuscle_association() - attach_neuromlfiles_to_channel() - upload_lineage_and_descriptions() - upload_connections() - upload_receptors_types_neurotransmitters_neuropeptides_innexins() - upload_additional_receptors_neurotransmitters_neuropeptides_innexins() + # upload_channelneuron_association() + # upload_channelmuscle_association() + # attach_neuromlfiles_to_channel() + # upload_lineage_and_descriptions() + # upload_connections() + # upload_receptors_types_neurotransmitters_neuropeptides_innexins() + # upload_additional_receptors_neurotransmitters_neuropeptides_innexins() print("Saving...") WORM.save() diff --git a/PyOpenWorm/__init__.py b/PyOpenWorm/__init__.py index 39713556e..c157dcbb2 100644 --- a/PyOpenWorm/__init__.py +++ b/PyOpenWorm/__init__.py @@ -80,7 +80,7 @@ from .my_neuroml import NeuroML from .connection import Connection from .experiment import Experiment -from .channel import Channel +from .channel import Channel, ExpressionPattern from .channelworm import ChannelModel, PatchClampExperiment from .plot import Plot @@ -114,6 +114,7 @@ "Connection", "Experiment", "Channel", + "ExpressionPattern", "ChannelModel", "PatchClampExperiment", "Plot"] @@ -270,6 +271,7 @@ def connect(configFile=False, SimpleProperty.register() Relationship.register() Channel.register() + ExpressionPattern.register() ChannelModel.register() Experiment.register() PatchClampExperiment.register() diff --git a/PyOpenWorm/channel.py b/PyOpenWorm/channel.py index 03fc9f91d..41f4124ae 100644 --- a/PyOpenWorm/channel.py +++ b/PyOpenWorm/channel.py @@ -1,3 +1,4 @@ +import rdflib as R from .pProperty import Property from .channelworm import ChannelModel from .dataObject import DataObject @@ -100,7 +101,10 @@ def __init__(self, name=False, **kwargs): Channel.DatatypeProperty('description', self) Channel.DatatypeProperty('gene_name', self) Channel.DatatypeProperty('gene_WB_ID', self) - Channel.DatatypeProperty('expression_pattern', self) + Channel.ObjectProperty('expression_pattern', + owner=self, + multiple=True, + value_type=ExpressionPattern) Channel.DatatypeProperty('neuroML_file', owner=self) Channel.DatatypeProperty('proteins', self, multiple=True) Channel.ObjectProperty('appearsIn', self, multiple=True, @@ -123,3 +127,29 @@ def identifier(self): else: # name is already set, so we can make an identifier from it return self.make_identifier(self.name.defined_values[0]) + + +class ExpressionPattern(DataObject): + def __init__(self, wormbaseID=None, description=None, **kwargs): + super(ExpressionPattern, self).__init__(**kwargs) + ExpressionPattern.DatatypeProperty('wormbaseID', owner=self) + ExpressionPattern.DatatypeProperty('wormbaseURL', owner=self) + ExpressionPattern.DatatypeProperty('description', owner=self) + + if wormbaseID: + self.wormbaseID(wormbaseID) + self.wormbaseURL(R.URIRef("http://www.wormbase.org/species/all/expr_pattern/"+wormbaseID)) + + if description: + self.description(description) + + @property + def defined(self): + return super(ExpressionPattern, self).defined \ + or self.wormbaseID.has_defined_value() + + def identifier(self): + if super(ExpressionPattern, self).defined: + return super(ExpressionPattern, self).identifier() + else: + return self.make_identifier(self.wormbaseID.defined_values[0])