class WormbaseTextMatchCSVDataSource(DSMixin, CSVDataSource): class_context = CONTEXT initial_cell_column = Informational( 'Initial Cell Column', description='The index of the first column with a cell name', multiple=False) cell_type = Informational('Cell Type', description='The type of cell to be produced', multiple=False)
class NeuronCSVDataSource(DSMixin, CSVDataSource): class_context = CONTEXT bibtex_files = Informational( display_name='BibTeX files', description= 'List of BibTeX files that are referenced in the csv file by entry ID')
class EvidenceDataSource(DSMixin, DataSource): class_context = SCI_CTX context_property = Informational(display_name='Context', property_name='evidence_context', property_type='ObjectProperty', description='The context') def __init__(self, *args, **kwargs): super(EvidenceDataSource, self).__init__(*args, **kwargs) self.context = _EvidenceContext.contextualize(self.context)(maker=self, imported=(CONTEXT,)) self.context_property(self.evidence_context.rdf_object) def commit_augment(self): saved_contexts = set([]) self.data_context.save_context(inline_imports=True, saved_contexts=saved_contexts) self.context.save_context(inline_imports=True, saved_contexts=saved_contexts) self.context.save_imports()
class ModelDBDataSource(ModelDB, DataSource): ''' A data source of a single ModelDB record ''' class_context = CONTEXT accession_id = Informational(display_name='Accession ID', description='Accession ID within ModelDB', multiple=False) download_url = Informational(display_name='Download URL', description='URL for downloading', multiple=False) base_directory = Informational( display_name='Base Model Directory', Description='Base directory of the downloaded model archive') key_property = 'accession_id' def __getattr__(self, name): # Tries to load the property from the graph if name.startswith(DATAOBJECT_PROPERTY_NAME_PREFIX): # Skip attributes that we *really* shouldn't be able to resolve return super().__getattr__(name) cd_type = ModelDBPropertyClassDescription.contextualize( self.context).query try: pclass = cd_type(local_id=name).resolve_class() return self.attach_property(pclass) except ClassResolutionFailed: raise AttributeError(name) def download(self, dest, base_url='https://senselab.med.yale.edu', session=None): ''' Download model files. Note that if the destination file already exists, it will be overwritten without warning if the operating system permits that. Parameters ---------- dest : str Destination file for the download. base_url : str, optional Base URL for the download session : requests.session.Session, optional Session to use for the download. If not provided, a new session will be created ''' if session is None: session = requests.Session() response = session.get(base_url + self.download_url(), stream=True) with open(dest, 'wb') as out: copyfileobj(response.raw, out) @classmethod def scrape_to_datasource(cls, accession, session=None, init_params=None): ''' Scrape a ModelDB page into a `owmeta_core.datasource.DataSource` Parameters ---------- accession : str or int Accession number or URL for the model session : requests.Session Session to use for getting the document. Optional. init_params : dict, optional Additional parameters to the DataSource created Returns ------- ModelDBDataSource The datasource created from the indicated ModelDB page See Also -------- `scrape` shares some parameters ''' data = scrape(accession, session) if init_params is None: init_params = dict() res = cls(**init_params) for d in data: if d['id'] == 'accession_id': for v in d['values']: res.accession_id(v) continue elif d['id'] == 'download_url': for v in d['values']: res.download_url(v) continue prop_class = create_property_class(d['id'], d['display_name']) prop = res.attach_property(prop_class) for v in d['values']: prop(v) return res
class C(self.DS1): q = Informational(also=self.DS1.a) p = Informational(also=self.DS1.a)
def test_default_property_type(self): inf = Informational() self.assertEqual(inf.property_type, 'DatatypeProperty')
class C(self.DS1): q = Informational(also=self.DS1.a, default_value='Q') p = Informational(also=self.DS1.a)
class DS1(DataSource): a = Informational(default_value='A')
class DS2(DS1): b = Informational() a = 'D'
class C(DataSource): q = Informational()
def test_default_display_name(self): inf = Informational(name='test') self.assertEqual(inf.display_name, 'test')
class C(self.DS1): q = Informational(also=self.DS1.a, default_value='R') a = 'M'
def test_default_multiple(self): inf = Informational() self.assertTrue(inf.multiple)
class DataWithEvidenceDataSource(DSMixin, DataSource): ''' A data source that has an "evidence context" containing statements which support those in its "data context". The data source also has a combined context which imports both the data and evidence contexts. ''' class_context = SCI_CTX evidence_context_property = Informational( display_name='Evidence context', property_name='evidence_context', property_type='ObjectProperty', multiple=False, description='The context in which evidence' ' for the "Data context" is defined') data_context_property = Informational( display_name='Data context', property_name='data_context', property_type='ObjectProperty', multiple=False, description='The context in which primary data' ' for this data source is defined') combined_context_property = Informational( display_name='Combined context', property_name='combined_context', property_type='ObjectProperty', multiple=False, description='Context importing both the data and evidence contexts') def __init__(self, *args, **kwargs): super(DataWithEvidenceDataSource, self).__init__(*args, **kwargs) self.__ad_hoc_contexts = dict() self.data_context = _DataContext.contextualize(self.context)( maker=self, imported=(CONTEXT, )) self.evidence_context = _EvidenceContext.contextualize(self.context)( maker=self, imported=(CONTEXT, )) self.combined_context = _CombinedContext.contextualize(self.context)( maker=self, imported=(self.data_context, self.evidence_context)) if not type(self).query_mode: if not self.data_context_property.has_defined_value(): self.data_context_property(self.data_context.rdf_object) if not self.evidence_context_property.has_defined_value(): self.evidence_context_property( self.evidence_context.rdf_object) if not self.combined_context_property.has_defined_value(): self.combined_context_property( self.combined_context.rdf_object) def data_context_for(self, **kwargs): ctx = self.context_for(**kwargs) self.data_context.add_import(ctx) return ctx def context_for(self, ident=None, **kwargs): key = "&".join(k + "=" + getattr(kwargs[k], 'identifier', str(kwargs[k])) for k in sorted(kwargs.keys())) res = self.__ad_hoc_contexts.get(key) if res is None: if ident: ctxid = ident else: ctxid = self.identifier + '/context_for?' + key self.__ad_hoc_contexts[key] = Context.contextualize( self.context)(ident=ctxid) res = self.__ad_hoc_contexts[key] return res def commit_augment(self): for ctx in self.__ad_hoc_contexts.values(): ctx.save() self.evidence_context.save() self.data_context.save() self.combined_context.save() for ctx in self.__ad_hoc_contexts.values(): ctx.save_imports(transitive=False) self.evidence_context.save_imports(transitive=False) self.data_context.save_imports(transitive=False) self.combined_context.save_imports(transitive=False)