Skip to content

Commit

Permalink
Fetch instances for types that can be used as funders from KG (HumanB…
Browse files Browse the repository at this point in the history
  • Loading branch information
ehennestad committed Feb 27, 2024
1 parent d58595c commit 1e46c32
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
71 changes: 33 additions & 38 deletions server/kg-util/fetchCoreSchemaInstances.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,73 +21,68 @@ fs.mkdir(INSTANCE_OUTPUT_DIRECTORY, { recursive: true }, (err) => {
}
});

// config instanceSpecification should contain the following properties:
// - openMindsType // Rename to openMindsSchemaType e.g: "Person"
// - instanceProperties // Rename to openMindsSchemaProperties
// - space // Optional, defaults to "common"

// Todo: Support array (list) input for instanceSpecification

let fetchCoreSchemaInstances = async (instanceSpecification) => {
// typeSpecification object should contain the following properties:
// - openMindsType // short name for the openminds type, e.g: "Person"
// - typeProperties // list of properties to save for this type, e.g: ["familyName", "givenName"]
// - space // Optional, defaults to "common" (KG space to search for instances)

let fetchCoreSchemaInstances = async (typeSpecifications) => {
// Create request header with authorization and options
const requestOptions = await getRequestOptions();

return new Promise((resolve, reject) => {

// Define some api constants
const API_BASE_URL = "https://core.kg.ebrains.eu/";
const API_ENDPOINT = "v3/instances";

let spaceName = "common";
if (instanceSpecification.space != undefined) {
spaceName = instanceSpecification.space;
}

const QUERY_PARAMS = ["stage=RELEASED", `space=${spaceName}`, "type=https://openminds.ebrains.eu/core/"];

//const CORE_SCHEMAS = ["Person", "URL"]
const CORE_SCHEMAS = [instanceSpecification.openMindsType];
typeSpecifications.forEach(async (typeSpecification) => {
let spaceName = "common";
if (typeSpecification.space !== undefined) {
spaceName = typeSpecification.space;
}

// Loop through core schemas terms and fetch their instances
for (let i = 0; i < CORE_SCHEMAS.length; i++){
const QUERY_PARAMS = ["stage=RELEASED", `space=${spaceName}`, "type=https://openminds.ebrains.eu/core/"];
const TYPE_NAME = typeSpecification.openMindsType;

// Assemble Query URL
let queryUrl = API_BASE_URL + API_ENDPOINT + "?" + QUERY_PARAMS.join("&") + CORE_SCHEMAS[i];
let instanceName = CORE_SCHEMAS[i];

// Fetch instances
fetchInstance(queryUrl, requestOptions, instanceName, instanceSpecification.instanceProperties)
.then( (data) => resolve(data) )
.catch( (error) => reject(error) )
}
let queryUrl = `${API_BASE_URL}${API_ENDPOINT}?${QUERY_PARAMS.join("&")}${TYPE_NAME}`;

try {
// Fetch instances
const data = await fetchInstances(queryUrl, requestOptions, TYPE_NAME, typeSpecification.typeProperties);
resolve(data);
} catch (error) {
reject(error);
}
});
});
}

// function to get schema instances from kg api
function fetchInstance(apiQueryUrl, requestOptions, instanceName, propertyNames) {
function fetchInstances(apiQueryUrl, requestOptions, typeName, propertyNames) {

return new Promise((resolve, reject) => {
fetch(apiQueryUrl, requestOptions)
.then( response => {
if (response.status===200) {
return response.json()
} else {
console.log('Error fetching instances for ' + instanceName + '. Status code: ' + response.status);
throw new Error('Error fetching instances for ' + instanceName + '. Status code: ' + response.status)
console.log('Error fetching instances for ' + typeName + '. Status code: ' + response.status);
throw new Error('Error fetching instances for ' + typeName + '. Status code: ' + response.status)
reject()
}
}) // Get response promise
.then( data => parseAndSaveData(data, instanceName, propertyNames).then( (instances) => resolve(instances) ) )
.then( data => parseAndSaveData(data, typeName, propertyNames).then( (instances) => resolve(instances) ) )
.catch( error => {reject(error); console.log(error) } )
});
}

// Parse and save schema instances
function parseAndSaveData(data, instanceName, propertyNameList) {
function parseAndSaveData(data, typeName, propertyNameList) {
return new Promise((resolve, reject) => {

const schemaInstanceList = [];
const typeInstanceList = [];

for (let thisInstance of data.data){
let newInstance = {"identifier": thisInstance["@id"]};
Expand All @@ -98,21 +93,21 @@ function parseAndSaveData(data, instanceName, propertyNameList) {
newInstance[propertyNameList[i]] = thisInstance[vocabName];
}
}
schemaInstanceList.push( newInstance );
typeInstanceList.push( newInstance );
}

// Save results to json file
const jsonStr = JSON.stringify(schemaInstanceList, null, 2);
const jsonStr = JSON.stringify(typeInstanceList, null, 2);

const filename = instanceName + '.json';
const filename = typeName + '.json';
const filePath = path.join(INSTANCE_OUTPUT_DIRECTORY, filename);

fs.writeFile(filePath, jsonStr, (err) => {
if (err) {
console.error(err);
reject(err)
} else {
console.log('File with instances for ' + instanceName + ' written successfully');
console.log('File with instances for ' + typeName + ' written successfully');
resolve()
}
});
Expand Down
20 changes: 15 additions & 5 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ async function setup() {

var startTime = performance.now()

configObject = {
openMindsType: "Person",
instanceProperties: ["familyName", "givenName"]
}
configObject = [
{
openMindsType: "Person",
typeProperties: ["familyName", "givenName"]
},
{
openMindsType: "Organization",
typeProperties: ["fullName"]
},
{
openMindsType: "Consortium",
typeProperties: ["fullName"]
},
]
// Temporary to retrieve strains for the workbench:
// configObject = {
// openMindsType: "Strain",
// instanceProperties: ["name", "description"],
// typeProperties: ["name", "description"],
// space: "dataset"
// }

Expand Down

0 comments on commit 1e46c32

Please sign in to comment.