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

Add two parameters in ConceptAncestorCore() and pManualConceptAncestor() #1021

Open
wants to merge 3 commits into
base: master
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
49 changes: 29 additions & 20 deletions working/packages/vocabulary_pack/ConceptAncestorCore.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
CREATE OR REPLACE FUNCTION vocabulary_pack.ConceptAncestorCore (pVocabularies TEXT, pDebugLoops BOOLEAN)
CREATE OR REPLACE FUNCTION vocabulary_pack.ConceptAncestorCore (
pVocabularies TEXT,
pDebugLoops BOOLEAN,
pIncludeNonStandard BOOLEAN DEFAULT FALSE,
pIncludeInvalidReason BOOLEAN DEFAULT FALSE
)
RETURNS VOID AS
$BODY$
/*
Expand Down Expand Up @@ -38,13 +43,13 @@ BEGIN
JOIN relationship s ON s.relationship_id = r.relationship_id
AND s.defines_ancestry = 1
JOIN concept c1 ON c1.concept_id = r.concept_id_1
AND c1.invalid_reason IS NULL
AND (pIncludeInvalidReason OR c1.invalid_reason IS NULL)
AND (
c1.vocabulary_id = ANY (iVocabularies)
OR iVocabularies IS NULL
)
JOIN concept c2 ON c2.concept_id = r.concept_id_2
AND c2.invalid_reason IS NULL
AND (pIncludeInvalidReason OR c2.invalid_reason IS NULL)
AND (
c2.vocabulary_id = ANY (iVocabularies)
OR iVocabularies IS NULL
Expand Down Expand Up @@ -84,12 +89,12 @@ BEGIN
ca.levels_of_separation,
ARRAY [descendant_concept_id] AS full_path
FROM temporary_ca_base$ ca
WHERE EXISTS (
WHERE (pIncludeNonStandard OR EXISTS (
SELECT 1
FROM concept c_int
WHERE c_int.concept_id = ca.ancestor_concept_id
AND c_int.standard_concept IS NOT NULL --remove non-standard records in ancestor_concept_id
)
))
AND ca.ancestor_concept_id > cRecord.ancestor_concept_id_min
AND ca.ancestor_concept_id <= cRecord.ancestor_concept_id_max

Expand Down Expand Up @@ -120,23 +125,27 @@ BEGIN
hc.descendant_concept_id;
END LOOP;

--remove non-standard records in descendant_concept_id
DELETE
FROM temporary_ca$ ca
USING concept c
WHERE c.standard_concept IS NULL
AND c.concept_id = ca.descendant_concept_id;

IF NOT pIncludeNonStandard THEN
--remove non-standard records in descendant_concept_id
DELETE
FROM temporary_ca$ ca
USING concept c
WHERE c.standard_concept IS NULL
AND c.concept_id = ca.descendant_concept_id;
END IF;

IF pDebugLoops THEN
--in debug mode we preserve the temporary_ca$ for future use
RETURN;
END IF;

PERFORM FROM temporary_ca$ ca WHERE ca.ancestor_concept_id=ca.descendant_concept_id LIMIT 1;
IF FOUND THEN
--loops found, e.g. code1 'Subsumes' code2 'Subsumes' code1, which leads to the creation of an code1-code1 chain
RAISE EXCEPTION $$Loops in the ancestor were found, please run SELECT vocabulary_pack.GetAncestorLoops(pVocabularies=>'%'); For view chains you can also use SELECT * FROM vocabulary_pack.GetAncestorPath_in_DEV(ancestor_id, descendant_id)$$, pVocabularies;
END IF;
IF NOT (pIncludeNonStandard OR pIncludeInvalidReason) THEN
PERFORM FROM temporary_ca$ ca WHERE ca.ancestor_concept_id=ca.descendant_concept_id LIMIT 1;
IF FOUND THEN
--loops found, e.g. code1 'Subsumes' code2 'Subsumes' code1, which leads to the creation of an code1-code1 chain
RAISE EXCEPTION $$Loops in the ancestor were found, please run SELECT vocabulary_pack.GetAncestorLoops(pVocabularies=>'%'); For view chains you can also use SELECT * FROM vocabulary_pack.GetAncestorPath_in_DEV(ancestor_id, descendant_id)$$, pVocabularies;
END IF;
END IF;

--Add connections to self for those vocabs having at least one concept in the concept_relationship table
INSERT INTO temporary_ca$
Expand All @@ -149,10 +158,10 @@ BEGIN
SELECT c_int.vocabulary_id
FROM concept_relationship cr_int
JOIN concept c_int ON c_int.concept_id = cr_int.concept_id_1
WHERE cr_int.invalid_reason IS NULL
WHERE r_int.invalid_reason IS NULL
)
AND c.invalid_reason IS NULL
AND c.standard_concept IS NOT NULL;
AND (pIncludeInvalidReason OR c.invalid_reason IS NULL)
AND (pIncludeNonStandard OR c.standard_concept IS NOT NULL);

CREATE INDEX idx_tmp_ca$ ON temporary_ca$ (ancestor_concept_id, descendant_concept_id) INCLUDE (min_levels_of_separation, max_levels_of_separation) WITH (FILLFACTOR=100);
ANALYZE temporary_ca$;
Expand Down
18 changes: 15 additions & 3 deletions working/packages/vocabulary_pack/pManualConceptAncestor.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
CREATE OR REPLACE FUNCTION vocabulary_pack.pManualConceptAncestor (pVocabularies TEXT)
CREATE OR REPLACE FUNCTION vocabulary_pack.pManualConceptAncestor (
pVocabularies TEXT,
pIncludeNonStandard BOOLEAN DEFAULT FALSE,
pIncludeInvalidReason BOOLEAN DEFAULT FALSE
)
RETURNS VOID AS
$BODY$
/*
Expand All @@ -17,10 +21,18 @@ DECLARE
cStartTime TIMESTAMP:=CLOCK_TIMESTAMP();
cWorkTime NUMERIC;
BEGIN
PERFORM vocabulary_pack.ConceptAncestorCore(pVocabularies, FALSE);
PERFORM vocabulary_pack.ConceptAncestorCore(pVocabularies, FALSE, pIncludeNonStandard, pIncludeInvalidReason);

cWorkTime:=ROUND((EXTRACT(EPOCH FROM CLOCK_TIMESTAMP()-cStartTime)/60)::NUMERIC,1);
PERFORM devv5.SendMailHTML (iSmallCA_emails, 'Manual concept ancestor in '||UPPER(CURRENT_SCHEMA)||' [ok]', 'Manual concept ancestor in '||UPPER(CURRENT_SCHEMA)||' completed'||crlf||'Execution time: '||cWorkTime||' min');
PERFORM devv5.SendMailHTML (
iSmallCA_emails,
'Manual concept ancestor in '||UPPER(CURRENT_SCHEMA)||' [ok]',
'Manual concept ancestor in '||UPPER(CURRENT_SCHEMA)||' completed'||crlf||
CASE WHEN pIncludeNonStandard THEN 'incliding non-standard concepts' ELSE '' END||crlf||
CASE WHEN pIncludeInvalidReason THEN 'including invalid reason ' ELSE '' END||crlf||
'Execution time: '||cWorkTime||' min'

);

EXCEPTION WHEN OTHERS THEN
GET STACKED DIAGNOSTICS cRet = PG_EXCEPTION_CONTEXT, cRet2 = PG_EXCEPTION_DETAIL;
Expand Down