def set_item_properties(self, entity, item_type, item_name, item_definition_uri, item_lab_ids, lab_id_select): sbol.TextProperty(entity, 'http://purl.org/dc/terms/title', '0', '1', item_name) time_stamp = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S-00') sbol.TextProperty(entity, 'http://purl.org/dc/terms/created', '0', '1', time_stamp) sbol.TextProperty(entity, 'http://purl.org/dc/terms/modified', '0', '1', time_stamp) if item_type in intent_parser_constants.ITEM_TYPES['collection']: return if len(item_definition_uri) > 0: if item_type == 'CHEBI': if not item_definition_uri.startswith( 'http://identifiers.org/chebi/CHEBI'): item_definition_uri = 'http://identifiers.org/chebi/CHEBI:' + item_definition_uri else: sbol.URIProperty(entity, 'http://www.w3.org/ns/prov#wasDerivedFrom', '0', '1', item_definition_uri) if len(item_lab_ids) > 0: lab_id_tag = lab_id_select.replace(' ', '_') tp = None for item_lab_id in item_lab_ids.split(','): if tp is None: tp = sbol.TextProperty(entity, 'http://sd2e.org#' + lab_id_tag, '0', '1', item_lab_id) else: tp.add(item_lab_id)
def test_validation_rules(self): md = sbol2.ModuleDefinition() with self.assertRaises(TypeError): # No validation rules, so `'AND'` is interpreted # as validation rules. tp = sbol2.TextProperty(md, 'http://example.com#logic', '0', '1', 'AND', 'initial_value') # Use an empty list to specify no validation rules tp = sbol2.TextProperty(md, 'http://example.com#logic', '0', '1', [], 'AND')
def create_synbiohub_entry(self, sbol_type, sbol_type_map, display_id, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag): document = sbol.Document() document.addNamespace('http://sd2e.org#', 'sd2') document.addNamespace('http://purl.org/dc/terms/', 'dcterms') document.addNamespace('http://www.w3.org/ns/prov#', 'prov') document.displayId = 'foo' document.version = '1' if sbol_type == 'component': if item_type == 'CHEBI': item_sbol_type = item_definition_uri else: item_sbol_type = sbol_type_map[item_type] component = sbol.ComponentDefinition(display_id, item_sbol_type) sbol.TextProperty(component, 'http://sd2e.org#stub_object', '0', '1', 'true') self.set_item_properties(component, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.addComponentDefinition(component) elif sbol_type == 'module': module = sbol.ModuleDefinition(display_id) sbol.TextProperty(module, 'http://sd2e.org#stub_object', '0', '1', 'true') module.roles = sbol_type_map[item_type] self.set_item_properties(module, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.addModuleDefinition(module) elif sbol_type == 'external': top_level = sbol.TopLevel('http://sd2e.org/types/#attribute', display_id) self.set_item_properties(top_level, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.add(top_level) elif sbol_type == 'collection': collection = sbol.Collection(display_id) self.set_item_properties(collection, item_type, item_name, item_definition_uri, item_lab_ids, item_lab_id_tag) document.addCollection(collection) else: raise IntentParserException( 'Failed to create a SynBioHub entry: %s as a supported sbol type in Intent Parser' % sbol_type) return document
def test_text_property_constructor(self): # Test None as parent object with self.assertRaises(AttributeError): sbol.TextProperty(None, sbol.SBOL_NAME, '0', '*', [], 'foo') # Test string as parent object with self.assertRaises(AttributeError): sbol.TextProperty('foo', sbol.SBOL_NAME, '0', '*', [], 'foo') # Test with object whose properties attribute is not a dict with self.assertRaises(TypeError): md = sbol.ModuleDefinition() md.properties = [] sbol.TextProperty(md, sbol.SBOL_NAME, '0', '*', [], 'foo')
def test_six_arg_constructor_none(self): cd = sbol2.ComponentDefinition() type_uri = 'http://example.com/test#testText' initial_value = 'omega' prop = sbol2.TextProperty(cd, type_uri, '0', '1', None, initial_value) self.assertEqual(initial_value, prop.value) self.assertEqual([], prop._validation_rules)
def test_five_arg_constructor_values(self): cd = sbol2.ComponentDefinition() type_uri = 'http://example.com/test#testText' initial_value = ['alpha', 'bravo', 'charlie'] prop = sbol2.TextProperty(cd, type_uri, '0', '*', initial_value) self.assertEqual(initial_value, prop.value) self.assertEqual([], prop._validation_rules)
def test_five_arg_constructor_validators(self): cd = sbol2.ComponentDefinition() type_uri = 'http://example.com/test#testText' validation_rules = [print] prop = sbol2.TextProperty(cd, type_uri, '0', '*', validation_rules) self.assertEqual([], prop.value) self.assertEqual(validation_rules, prop._validation_rules)
def test_init_store(self): # Ensure that property constructors initialize the parent # object's value store obj = sbol2.SBOLObject() type_uri = 'http://example.com#thing' obj.thing = sbol2.TextProperty(obj, type_uri, '0', '*') self.assertIn(type_uri, obj.properties) self.assertEqual([], obj.properties[type_uri]) self.assertEqual([], obj.thing)
def dataSource(self): self.obj.wasDerivedFrom = self.cell_val if "pubmed.ncbi.nlm.nih.gov/" in self.cell_val: if 'obo' not in self.doc_pref_terms: self.doc.addNamespace('http://purl.obolibrary.org/obo/', 'obo') self.doc_pref_terms.append('obo') self.obj.OBI_0001617 = sbol2.TextProperty( self.obj, 'http://purl.obolibrary.org/obo/OBI_0001617', 0, 1, []) self.obj.OBI_0001617 = self.cell_val.split(".gov/")[1].replace( "/", "")
def test_text_property_setting_single(self): md = sbol.ModuleDefinition() testing_uri = rdflib.URIRef(sbol.SBOL_URI + "#Testing") tp = sbol.TextProperty(md, testing_uri, '0', '1', []) # Test setting to string expected = 'foo' tp.value = expected self.assertEqual(tp.value, expected) # Test setting to None tp.value = None self.assertIsNone(tp.value) # Test integer with self.assertRaises(TypeError): tp.value = 3 # Test setting to list with self.assertRaises(TypeError): tp.value = ['foo', 'bar']
def test_text_property_setting_list(self): md = sbol.ModuleDefinition() testing_uri = rdflib.URIRef(sbol.SBOL_URI + "#Testing") tp = sbol.TextProperty(md, testing_uri, '0', '*', []) # Test setting to string expected = 'foo' tp.value = expected self.assertEqual(tp.value, [expected]) # Test setting to None with self.assertRaises(TypeError): tp.value = None # Test setting to list expected = ['foo', 'bar'] tp.value = expected self.assertEqual(tp.value, expected) # Test setting to list of integers with self.assertRaises(TypeError): tp.value = [1, 2, 3] # Test setting to empty list expected = [] tp.value = expected self.assertEqual(tp.value, [])
def create_sbh_stub(self, data): # Extract some fields from the form try: item_type = data['itemType'] item_name = data['commonName'] item_definition_uri = data['definitionURI'] item_display_id = data['displayId'] except Exception as e: return intent_parser_view.operation_failed( 'Form submission missing key: ' + str(e)) # Make sure Common Name was specified if len(item_name) == 0: return intent_parser_view.operation_failed( 'Common Name must be specified') # Sanitize the display id if len(item_display_id) > 0: display_id = self.sanitize_name_to_display_id(item_display_id) if display_id != item_display_id: return intent_parser_view.operation_failed( 'Illegal display_id') else: display_id = self.sanitize_name_to_display_id(item_name) # Derive document URL document_url = self.sbh_uri_prefix + display_id + '/1' # Make sure document does not already exist try: if self.sbh.exists(document_url): return intent_parser_view.operation_failed( '"' + display_id + '" already exists in SynBioHub') except: return intent_parser_view.operation_failed( 'Failed to access SynBioHub') # Look up sbol type uri sbol_type = None for sbol_type_key in intent_parser_constants.ITEM_TYPES: sbol_type_map = intent_parser_constants.ITEM_TYPES[sbol_type_key] if item_type in sbol_type_map: sbol_type = sbol_type_key break # Fix CHEBI URI if item_type == 'CHEBI': if len(item_definition_uri) == 0: item_definition_uri = sbol_type_map[item_type] else: if not item_definition_uri.startswith( 'http://identifiers.org/chebi/CHEBI'): item_definition_uri = 'http://identifiers.org/chebi/CHEBI:' + \ item_definition_uri # Create a dictionary entry for the item try: self.create_dictionary_entry(data, document_url, item_definition_uri) except Exception as e: return intent_parser_view.operation_failed(str(e)) # Create an entry in SynBioHub try: document = sbol.Document() document.addNamespace('http://sd2e.org#', 'sd2') document.addNamespace('http://purl.org/dc/terms/', 'dcterms') document.addNamespace('http://www.w3.org/ns/prov#', 'prov') if sbol_type == 'component': if item_type == 'CHEBI': item_sbol_type = item_definition_uri else: item_sbol_type = sbol_type_map[item_type] component = sbol.ComponentDefinition(display_id, item_sbol_type) sbol.TextProperty(component, 'http://sd2e.org#stub_object', '0', '1', 'true') self.set_item_properties(component, data) document.addComponentDefinition(component) elif sbol_type == 'module': module = sbol.ModuleDefinition(display_id) sbol.TextProperty(module, 'http://sd2e.org#stub_object', '0', '1', 'true') module.roles = sbol_type_map[item_type] self.set_item_properties(module, data) document.addModuleDefinition(module) elif sbol_type == 'external': top_level = sbol.TopLevel('http://sd2e.org/types/#attribute', display_id) self.set_item_properties(top_level, data) document.addTopLevel(top_level) elif sbol_type == 'collection': collection = sbol.Collection(display_id) self.set_item_properties(collection, data) document.addCollection(collection) else: raise Exception() self.sbh.submit(document, self.sbh_collection_uri, 3) paragraph_index = data['selectionStartParagraph'] offset = data['selectionStartOffset'] end_offset = data['selectionEndOffset'] action = intent_parser_view.link_text(paragraph_index, offset, end_offset, document_url) except Exception as e: self.logger.error(''.join( traceback.format_exception(etype=type(e), value=e, tb=e.__traceback__))) message = 'Failed to add "' + display_id + '" to SynBioHub' return intent_parser_view.operation_failed(message) return_info = { 'actions': [action], 'results': { 'operationSucceeded': True } } return return_info
def switch(self, sbol_term): """Switch statement that calls a different method based on the sbol_term. For example if the sbol_term is sbh_alteredSequence then the function sbh_alteredSequence() will be run. If there is no function with a name equal to the sbol_term then the add_new method is run. Args: sbol_term (str): String indicating the method of processing required by the cell_value Returns: Nothing is returned but the componentDefinition and sbol doc are updated according to the sbol_term and cell_value """ self.sbol_term_pref = sbol_term.split("_", 1)[0] try: self.sbol_term_suf = sbol_term.split("_", 1)[1] except IndexError: raise ValueError( f"The SBOL Term '{sbol_term}' (sheet name: {self.sheet}) does not appear to have an underscore" ) if self.parental_lookup: # switches the object being worked on self.obj = self.obj_dict[self.cell_val]['object'] self.cell_val = self.obj_uri # if not applicable then do nothing if sbol_term == "Not_applicable": pass # if a special function has been defined below then do something elif hasattr(self, self.sbol_term_suf): return getattr(self, self.sbol_term_suf)() # if it is an sbol term use standard pySBOL implementation # unless it is a top level object in which case the standard # implementations don't work elif self.sbol_term_pref == "sbol": if hasattr(self.obj, self.sbol_term_suf): # if the attribute is a list append the new value if isinstance(getattr(self.obj, self.sbol_term_suf), list): current = getattr(self.obj, self.sbol_term_suf) # if the cell_val is a list append the whole list if isinstance(self.cell_val, list): setattr(self.obj, self.sbol_term_suf, current + self.cell_val) else: setattr(self.obj, self.sbol_term_suf, current + [self.cell_val]) else: # no iteration over list as else suggests that the property # can't have multiple values try: current = getattr(self.obj, self.sbol_term_suf) if isinstance(current, type(None)): setattr(self.obj, self.sbol_term_suf, self.cell_val) else: if isinstance(self.cell_val, str) and isinstance( current, str): value = current + "\n" + self.cell_val logging.warning( f'The SBOL term {self.sbol_term_suf} for sheet:{self.sheet}, col: {self.sht_col}, row: {self.sht_row} is being concatenated {current} with {self.cell_val}' ) setattr(self.obj, self.sbol_term_suf, value) except AttributeError: raise ValueError( f"Can't set attribute {self.sbol_term_pref}_{self.sbol_term_suf} for sheet:{self.sheet}, col: {self.sht_col}, row: {self.sht_row}. It is likely an issue with plural e.g. not sbol_type but sbol_types" ) else: raise ValueError( f'This SBOL object ({type(self.obj)}) has no attribute {self.sbol_term_suf}. The column definitions sheet SBOL Term needs to be updated. (sheet:{self.sheet}, row: {self.sht_row}, col:{self.sht_col})' ) else: # logging.warning(f'This sbol term ({self.sbol_term}) has not yet been implemented so it has been added via the default method') # define a new namespace if needed if self.sbol_term_pref not in self.doc_pref_terms: self.doc.addNamespace(self.namespace_url, self.sbol_term_pref) self.doc_pref_terms.append(self.sbol_term_pref) # if type is uri make it a uri property if self.col_type == "URI": # * allows multiple instance of this property if not hasattr(self.obj, self.sbol_term_suf): setattr( self.obj, self.sbol_term_suf, sbol2.URIProperty( self.obj, f'{self.namespace_url}{self.sbol_term_suf}', '0', '*', [])) setattr(self.obj, self.sbol_term_suf, self.cell_val) else: if not isinstance(self.cell_val, list): self.cell_val = [self.cell_val] current = getattr(self.obj, self.sbol_term_suf) setattr(self.obj, self.sbol_term_suf, current + self.cell_val) # otherwise implement as text property else: # * allows multiple instance of this property if not hasattr(self.obj, self.sbol_term_suf): setattr( self.obj, self.sbol_term_suf, sbol2.TextProperty( self.obj, f'{self.namespace_url}{self.sbol_term_suf}', '0', '*')) setattr(self.obj, self.sbol_term_suf, str(self.cell_val)) else: if not isinstance(self.cell_val, list): self.cell_val = [self.cell_val] current = getattr(self.obj, self.sbol_term_suf) setattr(self.obj, self.sbol_term_suf, current + self.cell_val)
def test_four_arg_constructor(self): cd = sbol2.ComponentDefinition() type_uri = 'http://example.com/test#testText' prop = sbol2.TextProperty(cd, type_uri, '0', '1') self.assertEqual(None, prop.value) self.assertEqual([], prop._validation_rules)
def switch(self, rowobj, sbol_term, sbol_version): if sbol_version == 2: self.func_list = self.func_list2 exutil = exutil2 elif sbol_version == 3: self.func_list = self.func_list3 exutil = exutil3 else: raise ValueError( f"SBOL Version ({sbol_version}) given to switch has not been implemented yet" ) # split sbol term into prefix and suffix self.sbol_term = sbol_term self.sbol_term_pref = sbol_term.split("_", 1)[0] try: self.sbol_term_suf = sbol_term.split("_", 1)[1] except IndexError: raise ValueError( f"The SBOL Term '{sbol_term}' (sheet name: {self.sheet}) does not appear to have an underscore" ) # if not applicable then do nothing if sbol_term == "Not_applicable": pass # if a special function has been defined in excel-sbol-utils then use that elif self.sbol_term_suf in self.func_list: return getattr(exutil, self.sbol_term_suf)(rowobj) # if it is an sbol term use standard pySBOL implementation # unless it is a top level object in which case the standard # implementations don't work elif self.sbol_term_pref == "sbol": for col in rowobj.col_cell_dict: cell_val = rowobj.col_cell_dict[col] parental_lookup = rowobj.term_coldef_df[ rowobj.term_coldef_df['Column Name'] == col]['Parent Lookup'] if parental_lookup.values[0]: # switches the object being worked on rowobj.obj = rowobj.obj_dict[cell_val]['object'] cell_val = rowobj.obj_uri if hasattr(rowobj.obj, self.sbol_term_suf): # if the attribute is a list append the new value if isinstance(getattr(rowobj.obj, self.sbol_term_suf), list): current = getattr(rowobj.obj, self.sbol_term_suf) # if the col_cell_dict has multiple columns append each # if cell_val is a dict then mcol must have been given if isinstance(cell_val, dict): raise TypeError( f"A multicolumn value was unexpectedly given for sheet:{rowobj.sheet}, row:{rowobj.sht_row}, sbol term :{self.sbol_term}, sbol term dict: {rowobj.col_cell_dict}" ) # if the cell_val is a list append the whole list elif isinstance(cell_val, list): setattr(rowobj.obj, self.sbol_term_suf, current + cell_val) # otherwise append as a list object else: setattr(rowobj.obj, self.sbol_term_suf, current + [cell_val]) # if type sbol list then add by special append # rather than regular list append elif isinstance( getattr(rowobj.obj, self.sbol_term_suf), sbol3.refobj_property.ReferencedObjectList): # if cell_val is a dict then mcol must have been given if isinstance(cell_val, dict): raise TypeError( f"A multicolumn value was unexpectedly given for sheet:{rowobj.sheet}, row:{rowobj.sht_row}, sbol term :{self.sbol_term}, sbol term dict: {rowobj.col_cell_dict}" ) # else should be fine to append else: getattr(getattr(rowobj.obj, self.sbol_term_suf), 'append')(cell_val) else: # no iteration over list as else suggests that the property # can't have multiple values setattr(rowobj.obj, self.sbol_term_suf, cell_val) else: raise ValueError( f'This SBOL object ({type(rowobj.obj)}) has no attribute {self.sbol_term_suf} (sheet:{rowobj.sheet}, row:{rowobj.sht_row}, sbol term dict:{rowobj.col_cell_dict})' ) else: # logging.warning(f'This sbol term ({self.sbol_term}) has not yet been implemented so it has been added via the default method') # define a new namespace if needed for col in rowobj.col_cell_dict: cell_val = rowobj.col_cell_dict[col] col_coldef_df = rowobj.term_coldef_df[ rowobj.term_coldef_df['Column Name'] == col] if len(col_coldef_df) == 0: raise TypeError( f"A multicolumn value was unexpectedly given for sheet:{rowobj.sheet}, row:{rowobj.sht_row}, sbol term :{self.sbol_term}, sbol term dict: {rowobj.col_cell_dict}" ) parental_lookup = col_coldef_df['Parent Lookup'].values[0] if parental_lookup: # switches the object being worked on rowobj.obj = rowobj.obj_dict[cell_val]['object'] cell_val = rowobj.obj_uri namespace_url = col_coldef_df['Namespace URL'].values[0] if self.sbol_term_pref not in rowobj.doc_pref_terms: rowobj.doc.addNamespace(namespace_url, self.sbol_term_pref) rowobj.doc_pref_terms.append(self.sbol_term_pref) col_type = col_coldef_df['Type'].values[0] # if type is uri make it a uri property if col_type == "URI": # * allows multiple instance of this property if not hasattr(rowobj.obj, self.sbol_term_suf): if sbol_version == 2: setattr( rowobj.obj, self.sbol_term_suf, sbol2.URIProperty( rowobj.obj, f'{namespace_url}{self.sbol_term_suf}', '0', '*', [])) setattr(rowobj.obj, self.sbol_term_suf, cell_val) elif sbol_version == 3: setattr( rowobj.obj, self.sbol_term_suf, sbol3.URIProperty( rowobj.obj, f'{namespace_url}{self.sbol_term_suf}', '0', '*', initial_value=[cell_val])) else: if not isinstance(cell_val, list): cell_val = [cell_val] current = getattr(rowobj.obj, self.sbol_term_suf) setattr(rowobj.obj, self.sbol_term_suf, list(current) + cell_val) # otherwise implement as text property else: # * allows multiple instance of this property if not hasattr(rowobj.obj, self.sbol_term_suf): if sbol_version == 2: setattr( rowobj.obj, self.sbol_term_suf, sbol2.TextProperty( rowobj.obj, f'{namespace_url}{self.sbol_term_suf}', '0', '*')) setattr(rowobj.obj, self.sbol_term_suf, str(cell_val)) elif sbol_version == 3: setattr( rowobj.obj, self.sbol_term_suf, sbol3.TextProperty( rowobj.obj, f'{namespace_url}{self.sbol_term_suf}', '0', '*', initial_value=str(cell_val))) else: if not isinstance(cell_val, list): cell_val = [cell_val] current = getattr(rowobj.obj, self.sbol_term_suf) setattr(rowobj.obj, self.sbol_term_suf, list(current) + cell_val)