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

Obsolete and replace terms #27

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 3 additions & 2 deletions src/ontology/catalog-v001.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

<!-- <uri name="http://purl.obolibrary.org/obo/dron/dron-upper.owl" uri="components/dron-upper.owl"/>-->


<uri name="http://purl.obolibrary.org/obo/dron/dron-obsolete.owl" uri="components/dron-obsolete.owl"/>

</group>
</catalog>
</catalog>
3 changes: 2 additions & 1 deletion src/ontology/dron-edit.owl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Ontology(<http://purl.obolibrary.org/obo/dron.owl>
Import(<http://purl.obolibrary.org/obo/dron/dron-ingredient.owl>)
Import(<http://purl.obolibrary.org/obo/dron/dron-ndc.owl>)
Import(<http://purl.obolibrary.org/obo/dron/dron-rxnorm.owl>)
Import(<http://purl.obolibrary.org/obo/dron/dron-obsolete.owl>)
Import(<http://purl.obolibrary.org/obo/dron/imports/apollo_sv_import.owl>)
Import(<http://purl.obolibrary.org/obo/dron/imports/bfo_import.owl>)
Import(<http://purl.obolibrary.org/obo/dron/imports/chebi_import.owl>)
Expand Down Expand Up @@ -2160,4 +2161,4 @@ AnnotationAssertion(<http://purl.obolibrary.org/obo/IAO_0000412> <http://purl.ob


AnnotationAssertion(<http://purl.obolibrary.org/obo/IAO_0000117> <http://purl.obolibrary.org/obo/DRON_00723856> "ciara lusnia")
)
)
15 changes: 11 additions & 4 deletions src/ontology/dron.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# In essence we merge rxnorm, dron-ingredient, all imports and the edit file
# (no NDC) and the run a regular full release.

LITE_ARTEFACTS=$(COMPONENTSDIR)/dron-rxnorm.owl $(COMPONENTSDIR)/dron-ingredient.owl $(IMPORT_OWL_FILES)
LITE_ARTEFACTS=$(COMPONENTSDIR)/dron-rxnorm.owl $(COMPONENTSDIR)/dron-ingredient.owl $(COMPONENTSDIR)/dron-obsolete.owl $(IMPORT_OWL_FILES)
$(TMPDIR)/dron-edit_lite.owl: $(SRC) $(LITE_ARTEFACTS)
$(ROBOT) remove --input $(SRC) --select imports \
merge $(patsubst %, -i %, $(LITE_ARTEFACTS)) --output [email protected] && mv [email protected] $@
Expand Down Expand Up @@ -61,13 +61,18 @@ update-labels: $(TMPDIR)/dron.db $(TMPDIR)/chebi.db $(TMPDIR)/rxnorm.db $(SCRIPT
sqlite3 < $(word 4,$^)
cd $(TEMPLATEDIR) && sqlite3 ../ontology/$< < ../ontology/$(word 5,$^)

# Report common problems.
$(TMPDIR)/problems.db: $(TMPDIR)/dron.db $(TMPDIR)/chebi.db $(TMPDIR)/rxnorm.db $(SCRIPTSDIR)/report-problems.sql
sqlite3 $@ < $(word 4,$^)

# Convert DrOn template tables to LDTab format tables.
$(TMPDIR)/ldtab.db: $(TMPDIR)/dron.db $(SCRIPTSDIR)/prefix.tsv $(SCRIPTSDIR)/convert-dron-ldtab.sql | $(TMPDIR)/ldtab.jar
$(eval DB=$@)
rm -f $@
$(LDTAB) init $(DB) --table dron_ingredient
$(LDTAB) init $(DB) --table dron_rxnorm
$(LDTAB) init $(DB) --table dron_ndc
$(LDTAB) init $(DB) --table dron_obsolete
$(LDTAB) prefix $@ $(word 2,$^)
sqlite3 $@ < $(word 3,$^)

Expand All @@ -81,7 +86,7 @@ $(COMPONENTSDIR)/%.owl: $(COMPONENTSDIR)/%.ttl
$(ROBOT) convert -i $< -o $@

# Override the all_components task.
all_components: $(COMPONENTSDIR)/dron-ingredient.ttl $(COMPONENTSDIR)/dron-rxnorm.ttl $(COMPONENTSDIR)/dron-ndc.ttl $(COMPONENTSDIR)/dron-ingredient.owl $(COMPONENTSDIR)/dron-rxnorm.owl $(COMPONENTSDIR)/dron-ndc.owl
all_components: $(COMPONENTSDIR)/dron-ingredient.ttl $(COMPONENTSDIR)/dron-rxnorm.ttl $(COMPONENTSDIR)/dron-ndc.ttl $(COMPONENTSDIR)/dron-obsolete.ttl $(COMPONENTSDIR)/dron-ingredient.owl $(COMPONENTSDIR)/dron-rxnorm.owl $(COMPONENTSDIR)/dron-ndc.owl $(COMPONENTSDIR)/dron-obsolete.owl

###################################
#### Create templates from OWL ####
Expand All @@ -99,17 +104,19 @@ $(TMPDIR)/reverse/:
$(TMPDIR)/reverse/dron-%.owl: components.bk/dron-%.owl | $(TMPDIR)/reverse/
$(ROBOT) convert -i $< -o $@

$(TMPDIR)/reverse.db: $(SCRIPTSDIR)/prefix.tsv $(SCRIPTSDIR)/create-dron-tables.sql $(SCRIPTSDIR)/convert-ldtab-dron.sql $(TMPDIR)/reverse/dron-ingredient.owl $(TMPDIR)/reverse/dron-rxnorm.owl $(TMPDIR)/reverse/dron-ndc.owl | $(TMPDIR)/ldtab.jar
$(TMPDIR)/reverse.db: $(SCRIPTSDIR)/prefix.tsv $(SCRIPTSDIR)/create-dron-tables.sql $(SCRIPTSDIR)/convert-ldtab-dron.sql $(TMPDIR)/reverse/dron-ingredient.owl $(TMPDIR)/reverse/dron-rxnorm.owl $(TMPDIR)/reverse/dron-ndc.owl $(TMPDIR)/reverse/dron-obsolete.owl | $(TMPDIR)/ldtab.jar
$(eval DB=$@)
rm -f $(DB)
$(LDTAB) init $(DB) --table dron_ingredient
$(LDTAB) init $(DB) --table dron_rxnorm
$(LDTAB) init $(DB) --table dron_ndc
$(LDTAB) init $(DB) --table dron_obsolete
$(LDTAB) prefix $(DB) $<
sqlite3 $(DB) < $(word 2,$^)
$(LDTAB) import $(DB) --table dron_ingredient $(TMPDIR)/reverse/dron-ingredient.owl
$(LDTAB) import $(DB) --table dron_rxnorm $(TMPDIR)/reverse/dron-rxnorm.owl
$(LDTAB) import $(DB) --table dron_ndc $(TMPDIR)/reverse/dron-ndc.owl
$(LDTAB) import $(DB) --table dron_obsolete $(TMPDIR)/reverse/dron-obsolete.owl

.PHONY: reverse
reverse: $(TMPDIR)/reverse.db $(SCRIPTSDIR)/save-dron-tables.sql
Expand Down Expand Up @@ -143,4 +150,4 @@ $(TMPDIR)/%.owl.diff: $(TMPDIR)/reverse/%.owl $(COMPONENTSDIR)/%.owl
-diff -u $^ > $@

.PHONY: roundtrip
roundtrip: $(COMPONENTSDIR)/dron-ingredient.ttl $(COMPONENTSDIR)/dron-rxnorm.ttl $(COMPONENTSDIR)/dron-ndc.ttl $(TMPDIR)/dron-ingredient.owl.diff $(TMPDIR)/dron-ingredient.tsv.diff $(TMPDIR)/dron-rxnorm.owl.diff $(TMPDIR)/dron-rxnorm.tsv.diff $(TMPDIR)/dron-ndc.owl.diff $(TMPDIR)/dron-ndc.tsv.diff
roundtrip: $(COMPONENTSDIR)/dron-ingredient.ttl $(COMPONENTSDIR)/dron-rxnorm.ttl $(COMPONENTSDIR)/dron-ndc.ttl $(COMPONENTSDIR)/dron-obsolete.ttl $(TMPDIR)/dron-ingredient.owl.diff $(TMPDIR)/dron-ingredient.tsv.diff $(TMPDIR)/dron-rxnorm.owl.diff $(TMPDIR)/dron-rxnorm.tsv.diff $(TMPDIR)/dron-ndc.owl.diff $(TMPDIR)/dron-ndc.tsv.diff $(TMPDIR)/dron-obsolete.owl.diff $(TMPDIR)/dron-obsolete.tsv.diff
35 changes: 35 additions & 0 deletions src/scripts/convert-dron-ldtab.sql
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,38 @@ FROM (
UNION
SELECT curie, clinical_drug AS drug FROM dron.ndc_clinical_drug
);


-- ## Obsolete Terms

INSERT OR IGNORE INTO dron_obsolete(subject, predicate, object)
SELECT
curie AS subject,
'rdf:type' AS predicate,
type AS object
FROM obsolete;

INSERT OR IGNORE INTO dron_obsolete(subject, predicate, object, datatype)
SELECT
curie AS subject,
'rdfs:label' AS predicate,
label AS object,
'xsd:string' AS datatype
FROM obsolete;

INSERT OR IGNORE INTO dron_obsolete(subject, predicate, object, datatype)
SELECT
curie AS subject,
'owl:deprecated' AS predicate,
'true' AS object,
'xsd:boolean' AS datatype
FROM obsolete;

INSERT OR IGNORE INTO dron_obsolete(subject, predicate, object)
SELECT
curie AS subject,
'IAO:0100001' AS predicate,
replaced_by AS object
FROM obsolete
WHERE replaced_by IS NOT NULL
AND replaced_by != '';
19 changes: 19 additions & 0 deletions src/scripts/convert-ldtab-dron.sql
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,22 @@ UNION
SELECT rxcui FROM clinical_drug
UNION
SELECT rxcui FROM branded_drug;


-- ## dron-obsolete

INSERT OR IGNORE INTO obsolete(curie, label, type)
SELECT
a.subject AS curie,
b.object AS label,
a.object AS type
FROM dron_obsolete AS a
LEFT JOIN dron_obsolete AS b ON a.subject = b.subject
WHERE a.predicate = 'rdf:type'
AND b.predicate = 'rdfs:label';

UPDATE obsolete
SET replaced_by = object
FROM dron_obsolete
WHERE obsolete.curie = dron_obsolete.subject
AND dron_obsolete.predicate = 'IAO:0100001';
6 changes: 6 additions & 0 deletions src/scripts/create-dron-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ CREATE TABLE ndc_clinical_drug (
FOREIGN KEY (clinical_drug) REFERENCES clinical_drug(curie)
);

CREATE TABLE obsolete (
curie TEXT PRIMARY KEY,
label TEXT NOT NULL, -- UNIQUE,
type TEXT NOT NULL,
replaced_by TEXT
);

-- Create triggers for automatically assigning DRON IDs.
-- First create a table with one row to track the current ID.
Expand Down
1 change: 1 addition & 0 deletions src/scripts/load-dron-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ PRAGMA foreign_keys = ON;
.import --skip 1 ../templates/branded_drug_excipient.tsv branded_drug_excipient
.import --skip 1 ../templates/ndc_branded_drug.tsv ndc_branded_drug
.import --skip 1 ../templates/ndc_clinical_drug.tsv ndc_clinical_drug
.import --skip 1 ../templates/obsolete.tsv obsolete
29 changes: 29 additions & 0 deletions src/scripts/obsolete-dron-ingredients.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh
#
# Add DRON ingredients to obsolete.tsv
# if they match ChEBI labels.

fail() {
echo "ERROR: " "$@"
exit 1
}

ROOT=$(git rev-parse --show-toplevel) || fail "Not a git repository"
DIR="${ROOT}/src/ontology"
SCRIPTSDIR="${ROOT}/src/scripts"
TEMPLATEDIR="${ROOT}/src/templates"
DB="${DIR}/tmp/dron.db"

# Load template TSVs into SQLite.
cd "${DIR}" || fail "Could not cd to ${DIR}"
make tmp/dron.db || fail "Could not make tmp/dron.db"
make tmp/chebi.db || fail "Could not make tmp/chebi.db"

# Find DRON ingredients matching ChEBI labels.
sqlite3 "${DB}" < "${SCRIPTSDIR}/obsolete-dron-ingredients.sql" \
|| fail "Could not run query for ingredients"

# Save obsolete.tsv.
cd "${TEMPLATEDIR}" \
&& sqlite3 "${DB}" < "${SCRIPTSDIR}/save-dron-tables.sql" \
|| fail "Could not save updated obsolete.tsv"
18 changes: 18 additions & 0 deletions src/scripts/obsolete-dron-ingredients.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ATTACH 'tmp/dron.db' AS dron;
ATTACH 'tmp/chebi.db' AS chebi;

CREATE INDEX IF NOT EXISTS dron.idx_ingredient_label ON ingredient(label);
WITH labels AS (
SELECT subject, object AS label
FROM chebi.chebi
WHERE predicate = 'rdfs:label'
)
INSERT OR IGNORE INTO dron.obsolete
SELECT
i.curie AS curie,
'obsolete ' || i.label AS label,
'owl:Class' AS type,
l.subject AS replaced_by
FROM dron.ingredient AS i
JOIN labels AS l ON l.label = i.label
WHERE i.curie LIKE 'DRON:%';
43 changes: 43 additions & 0 deletions src/scripts/obsolete-terms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh
#
# Obsolete a DRON term and replace it with another term.

fail() {
echo "ERROR: " "$@"
exit 1
}

ROOT=$(git rev-parse --show-toplevel) || fail "Not a git repository"
DIR="${ROOT}/src/ontology"
SCRIPTSDIR="${ROOT}/src/scripts"
TEMPLATEDIR="${ROOT}/src/templates"
DB="${DIR}/tmp/dron.db"

# Replace one term ID with another ID throughout.
replace() {
OBSOLETE="$1"
REPLACEMENT="$2"
if [ -n "${REPLACEMENT}" ]; then
< "${SCRIPTSDIR}/obsolete-terms.sql" \
sed "s/OBSOLETE/${OBSOLETE}/g" \
| sed "s/REPLACEMENT/${REPLACEMENT}/g" \
| sqlite3 "${DB}" \
|| fail "Could not obsolete ${OBSOLETE}"
fi
}

# Load template TSVs into SQLite.
cd "${DIR}" || fail "Could not cd to ${DIR}"
make tmp/dron.db || fail "Could not make ${DB}"

# Iterate over the rows of obsolete.tsv and replace.
tail -n+2 "${TEMPLATEDIR}/obsolete.tsv" \
| cut -f1,4 \
| while read -r p; do
replace $p
done

# Save templates TSVs.
cd "${TEMPLATEDIR}" \
&& sqlite3 "${DB}" < "${SCRIPTSDIR}/save-dron-tables.sql" \
|| fail "Could not save updated templates"
110 changes: 110 additions & 0 deletions src/scripts/obsolete-terms.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
PRAGMA foreign_keys = ON;

BEGIN TRANSACTION;

-- obsolete ingredient

INSERT OR IGNORE INTO ingredient
SELECT
'REPLACEMENT' AS curie,
label AS label,
rxcui AS rxcui
FROM ingredient
WHERE curie = 'OBSOLETE';

UPDATE ingredient_disposition
SET ingredient = 'REPLACEMENT'
WHERE ingredient = 'OBSOLETE';

UPDATE clinical_drug_form_ingredient
SET ingredient = 'REPLACEMENT'
WHERE ingredient = 'OBSOLETE';

UPDATE clinical_drug_strength
SET ingredient = 'REPLACEMENT'
WHERE ingredient = 'OBSOLETE';

UPDATE branded_drug_excipient
SET ingredient = 'REPLACEMENT'
WHERE ingredient = 'OBSOLETE';

DELETE FROM ingredient
WHERE curie = 'OBSOLETE';

-- obsolete clinical_drug_form

INSERT OR IGNORE INTO clinical_drug_form
SELECT
'REPLACEMENT' AS curie,
label AS label,
parent AS parent,
rxcui AS rxcui
FROM clinical_drug_form
WHERE curie = 'OBSOLETE';

UPDATE clinical_drug_form_ingredient
SET clinical_drug_form = 'REPLACEMENT'
WHERE clinical_drug_form = 'OBSOLETE';

UPDATE clinical_drug_form_disposition
SET clinical_drug_form = 'REPLACEMENT'
WHERE clinical_drug_form = 'OBSOLETE';

UPDATE clinical_drug
SET clinical_drug_form = 'REPLACEMENT'
WHERE clinical_drug_form = 'OBSOLETE';

DELETE FROM clinical_drug_form
WHERE curie = 'OBSOLETE';

-- obsolete clinical_drug

INSERT OR IGNORE INTO clinical_drug
SELECT
'REPLACEMENT' AS curie,
label AS label,
clinical_drug_form AS clinical_drug_form,
rxcui AS rxcui
FROM clinical_drug
WHERE curie = 'OBSOLETE';

UPDATE clinical_drug_strength
SET clinical_drug = 'REPLACEMENT'
WHERE clinical_drug = 'OBSOLETE';

UPDATE branded_drug
SET clinical_drug = 'REPLACEMENT'
WHERE clinical_drug = 'OBSOLETE';

UPDATE ndc_clinical_drug
SET clinical_drug = 'REPLACEMENT'
WHERE clinical_drug = 'OBSOLETE';

DELETE FROM clinical_drug
WHERE curie = 'OBSOLETE';

-- obsolete branded_drug

INSERT OR IGNORE INTO branded_drug
SELECT
'REPLACEMENT' AS curie,
label AS label,
clinical_drug AS clinical_drug,
rxcui AS rxcui
FROM branded_drug
WHERE curie = 'OBSOLETE';

UPDATE branded_drug_excipient
SET branded_drug = 'REPLACEMENT'
WHERE branded_drug = 'OBSOLETE';

UPDATE ndc_branded_drug
SET branded_drug = 'REPLACEMENT'
WHERE branded_drug = 'OBSOLETE';

DELETE FROM branded_drug
WHERE curie = 'OBSOLETE';

-- done

COMMIT;
Loading
Loading