Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adapt synth-parser to ontology changes #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 196 additions & 72 deletions example/1-Synth.json

Large diffs are not rendered by default.

36 changes: 32 additions & 4 deletions synth-converter/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ pub struct Batch {
pub batch_id: String,
#[serde(rename = "Actions")]
pub actions: Vec<Action>,
#[serde(rename = "batchName")]
pub batch_name: Option<String>,
#[serde(rename = "ReactionType")]
pub reaction_type: Option<String>,
#[serde(rename = "OptimizationType")]
pub optimization_type: Option<String>,
#[serde(rename = "Link")]
pub link: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -26,9 +34,8 @@ pub struct Action {
pub container_info: Option<ContainerInfo>,
#[serde(rename = "speedShaker")]
pub speed_shaker: Option<Observation>,
#[serde(flatten)]
#[serde(rename = "hasContainerPositionAndQuantity")]
pub has_container_position_and_quantity: Option<Vec<ContainerPosition>>,
pub has_container_position_and_quantity: Option<Vec<ContainerPositionQuantityItem>>,
#[serde(rename = "dispenseState")]
pub dispense_state: Option<String>,
#[serde(rename = "dispenseType")]
Expand Down Expand Up @@ -66,6 +73,14 @@ pub struct ContainerInfo {
pub struct Observation {
pub value: f64,
pub unit: String,
#[serde(rename = "errorMargin")]
pub error_margin: Option<ErrorMargin>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorMargin {
pub value: f64,
pub unit: String,
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -92,6 +107,9 @@ pub struct SampleItem {
pub internal_bar_code: String,
#[serde(rename = "expectedDatum")]
pub expected_datum: Option<Observation>,
#[serde(rename = "measuredQuantity")]
pub measured_quantity: Option<Observation>,
pub concentration: Option<Observation>,
#[serde(rename = "physicalState")]
pub physical_state: String,
#[serde(rename = "hasChemical")]
Expand All @@ -105,14 +123,24 @@ pub struct Chemical {
#[serde(rename = "chemicalName")]
pub chemical_name: String,
#[serde(rename = "CASNumber")]
pub cas_number: String,
pub cas_number: Option<String>,
#[serde(rename = "molecularMass")]
pub molecular_mass: Observation,
pub smiles: String,
#[serde(rename = "swissCatNumber")]
pub swiss_cat_number: Option<String>,
#[serde(rename = "Inchi")]
pub inchi: String,
pub keywords: Option<String>,
#[serde(rename = "molecularFormula")]
pub molecular_formula: String,
pub density: Option<Observation>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ContainerPosition {
pub struct ContainerPositionQuantityItem {
#[serde(rename = "containerID")]
pub container_id: String,
pub position: String,
pub quantity: Observation,
}
1 change: 1 addition & 0 deletions synth-converter/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use anyhow::{Context, Result};
pub fn json_to_rdf(input_content: &str, fmt: &str) -> Result<String> {
// Parse JSON into a Batch object
let batch = parse_json(input_content).context("Failed to parse JSON input")?;
print!("{:?}", batch);

// Build the RDF graph
let mut graph_builder = GraphBuilder::new().context("Failed to initialize GraphBuilder")?;
Expand Down
134 changes: 113 additions & 21 deletions synth-converter/src/graph/graph_builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{
batch::{
Action, ActionName, Batch, Chemical, ContainerInfo, ContainerPosition, Observation, Sample,
SampleItem,
Action, ActionName, Batch, Chemical, ContainerInfo, ContainerPositionQuantityItem,
Observation, Sample, SampleItem, ErrorMargin
},
graph::{
namespaces::{alloqual, allores, cat, obo, purl, qudt, schema},
namespaces::{alloqual, allores, cat, obo, purl, qudt, schema, unit},
utils::generate_bnode_term,
},
rdf::rdf_serializers::{serialize_graph_to_jsonld, serialize_graph_to_turtle},
Expand Down Expand Up @@ -84,36 +84,81 @@ impl GraphBuilder {
self.graph
.insert(&observation_term, qudt::value, observation.value)?;

if let Some(error_margin) = &observation.error_margin {
self.insert_an_error_margin(&observation_term, &cat::errorMargin, error_margin)?;
}

Ok(())
}

fn insert_a_density(
&mut self,
subject: &SimpleTerm,
property_term: &NsTerm<'_>,
observation: &Observation,
) -> Result<()> {
let density_term = generate_bnode_term();

self.graph
.insert(subject, property_term, &density_term)?;
self.graph
.insert(&density_term, qudt::unit, unit::MilliGM)?;
self.graph
.insert(&density_term, qudt::value, observation.value)?;

Ok(())
}

fn insert_an_error_margin(
&mut self,
subject: &SimpleTerm,
property_term: &NsTerm<'_>,
error_margin: &ErrorMargin,
) -> Result<()> {
let error_margin_term = generate_bnode_term();

self.graph
.insert(subject, property_term, &error_margin_term)?;
self.graph
.insert(&error_margin_term, qudt::unit, error_margin.unit.as_str())?;
self.graph
.insert(&error_margin_term, qudt::value, error_margin.value)?;

Ok(())
}

fn insert_a_container_position(
fn insert_a_container_position_and_quantity(
&mut self,
subject: &SimpleTerm,
container_position: &ContainerPosition,
container_position_quantity_item: &ContainerPositionQuantityItem,
) -> Result<()> {
let container_position_term = generate_bnode_term();
let container_position_quantity_item_term = generate_bnode_term();

self.graph.insert(
subject,
cat::hasContainerPositionAndQuantity,
&container_position_term,
&container_position_quantity_item_term,
)?;
self.graph.insert(
&container_position_term,
&container_position_quantity_item_term,
rdf::type_,
cat::ContainerPositionAndQuantity,
)?;
self.graph.insert(
&container_position_term,
&container_position_quantity_item_term,
allores::AFR_0002240,
container_position.position.as_str(),
container_position_quantity_item.position.as_str(),
)?;
self.graph.insert(
&container_position_quantity_item_term,
cat::containerID,
container_position_quantity_item.container_id.as_str(),
)?;

self.insert_an_observation(
&container_position_term,
&container_position_quantity_item_term,
&qudt::quantity,
&container_position.quantity,
&container_position_quantity_item.quantity,
)?;

Ok(())
Expand All @@ -136,8 +181,10 @@ impl GraphBuilder {
cat::chemicalName,
chemical.chemical_name.as_str(),
)?;
self.graph
.insert(&chemical_term, cat::casNumber, chemical.cas_number.as_str())?;
if let Some(cas_number) = &chemical.cas_number {
self.graph
.insert(&chemical_term, cat::casNumber, cas_number.as_str())?;
}
self.graph.insert(
&chemical_term,
allores::AFR_0002295,
Expand All @@ -147,6 +194,27 @@ impl GraphBuilder {
self.graph
.insert(&chemical_term, allores::AFR_0002294, &*molecular_mass)?;

self.graph
.insert(&chemical_term, allores::AFR_0002296, chemical.inchi.as_str())?;
self.graph
.insert(&chemical_term, allores::AFR_0001952, chemical.molecular_formula.as_str())?;
if let Some(swiss_cat_number) = &chemical.swiss_cat_number {
self.graph
.insert(&chemical_term, cat::swissCatNumber, swiss_cat_number.as_str())?;
}
if let Some(keywords) = &chemical.keywords {
self.graph
.insert(&chemical_term, schema::keywords, keywords.as_str())?;
}

if let Some(density) = &chemical.density {
self.insert_a_density(
&chemical_term,
&obo::PATO_0001019,
density,
)?;
}

Ok(())
}

Expand All @@ -164,6 +232,15 @@ impl GraphBuilder {
self.insert_an_observation(&sample_item_term, &cat::expectedDatum, expected_datum)?;
}

if let Some(measured_quantity) = &sample_item.measured_quantity {
self.insert_an_observation(&sample_item_term, &cat::measuredQuantity, measured_quantity)?;
}

if let Some(concentration) = &sample_item.measured_quantity {
self.insert_an_observation(&sample_item_term, &allores::AFR_0002036, concentration)?;
}


self.graph
.insert(&sample_item_term, cat::role, sample_item.role.as_str())?;
self.graph.insert(
Expand Down Expand Up @@ -220,12 +297,27 @@ impl GraphBuilder {

ActionName::setTemperatureAction => {
self.graph
.insert(subject, rdf::type_, cat::setTemperatureAction)?;
.insert(subject, rdf::type_, cat::SetTemperatureAction)?;
}

ActionName::setPressureAction => {
self.graph
.insert(subject, rdf::type_, cat::SetPressureAction)?;
}

ActionName::shakeAction => {
self.graph
.insert(subject, rdf::type_, cat::ShakeAction)?;
}

ActionName::setVacuumAction => {
self.graph
.insert(subject, rdf::type_, cat::SetVacuumAction)?;
}

_ => {
ActionName::filtrateAction => {
self.graph
.insert(subject, rdf::type_, allores::AFRE_0000001)?;
.insert(subject, rdf::type_, cat::FiltrateAction)?;
}
}

Expand Down Expand Up @@ -259,7 +351,7 @@ impl GraphBuilder {
)?;
self.graph.insert(
&action_term,
cat::localEquipmentName,
cat::subEquipmentName,
action.sub_equipment_name.as_str(),
)?;

Expand Down Expand Up @@ -299,9 +391,9 @@ impl GraphBuilder {
.insert(&action_term, alloqual::AFQ_0000111, dispense_state.as_str())?;
}

if let Some(container_positions) = &action.has_container_position_and_quantity {
for container_position in container_positions {
self.insert_a_container_position(&action_term, container_position)?;
if let Some(container_position_and_quantities) = &action.has_container_position_and_quantity {
for container_position_quantity_item in container_position_and_quantities {
self.insert_a_container_position_and_quantity(&action_term, container_position_quantity_item)?;
}
}

Expand Down
5 changes: 4 additions & 1 deletion synth-converter/src/graph/namespaces/allores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use sophia_api::namespace;
namespace! {
"http://purl.allotrope.org/ontologies/result#",
AFR_0002240,
AFR_0002296,
AFR_0002295,
AFR_0002294,
AFR_0002464,
AFRE_0000001,
AFX_0000622,
AFR_0002423,
AFR_0001606,
AFR_0001723
AFR_0001723,
AFR_0001952,
AFR_0002036
}
lazy_static! {
pub static ref ns: Namespace<&'static str> = Namespace::new(PREFIX.as_str()).unwrap();
Expand Down
38 changes: 23 additions & 15 deletions synth-converter/src/graph/namespaces/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@ use sophia::api::ns::Namespace;
use sophia_api::namespace;
namespace! {
"http://example.org/cat#",
containerID,
containerBarcode,
hasContainerPositionAndQuantity,
AddAction,
Batch,
ContainerPositionAndQuantity,
has_chemical,
chemicalName,
casNumber,
FiltrateAction,
Sample,
hasSample,
role,
SetPressureAction,
SetTemperatureAction,
SetVacuumAction,
ShakeAction,
casNumber,
chemicalName,
containerBarcode,
containerID,
dispenseType,
errorMargin,
expectedDatum,
hasBatch,
hasContainerPositionAndQuantity,
hasSample,
has_chemical,
internalBarCode,
vialShape,
AddAction,
measuredQuantity,
role,
setTemperatureAction,
hasBatch,
Batch,
localEquipmentName,
speedInRPM,
subEquipmentName,
swissCatNumber,
temperatureShakerShape,
temperatureTumbleStirrerShape,
speedInRPM,
dispenseType
vialShape
}
lazy_static! {
pub static ref ns: Namespace<&'static str> = Namespace::new(PREFIX.as_str()).unwrap();
Expand Down
1 change: 1 addition & 0 deletions synth-converter/src/graph/namespaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod obo;
pub mod purl;
pub mod qudt;
pub mod schema;
pub mod unit;
3 changes: 2 additions & 1 deletion synth-converter/src/graph/namespaces/obo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use sophia::api::ns::Namespace;
use sophia_api::namespace;
namespace! {
"http://purl.obolibrary.org/obo/",
CHEBI_25367
CHEBI_25367,
PATO_0001019
}
lazy_static! {
pub static ref ns: Namespace<&'static str> = Namespace::new(PREFIX.as_str()).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion synth-converter/src/graph/namespaces/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use sophia::api::ns::Namespace;
use sophia_api::namespace;
namespace! {
"https://schema.org/",
name
name,
keywords
}
lazy_static! {
pub static ref ns: Namespace<&'static str> = Namespace::new(PREFIX.as_str()).unwrap();
Expand Down
Loading