 	AND crs.relationship_id = 'Concept replaced by'
 	AND crs.invalid_reason IS NULL;
 
--- At the end, the three tables concept_stage, concept_relationship_stage and concept_synonym_stage should be ready to be fed into the generic_update.sql script+--13. Add manual mappings
+DO $_$
+BEGIN
+	PERFORM VOCABULARY_PACK.ProcessManualRelationships();
+END $_$;
+
+--14. Working with replacement mappings
+DO $_$
+BEGIN
+	PERFORM VOCABULARY_PACK.CheckReplacementMappings();
+END $_$;
+
+--15. Add mapping from deprecated to fresh concepts
+DO $_$
+BEGIN
+	PERFORM VOCABULARY_PACK.AddFreshMAPSTO();
+END $_$;
+
+--16. Deprecate 'Maps to' mappings to deprecated and upgraded concepts
+DO $_$
+BEGIN
+	PERFORM VOCABULARY_PACK.DeprecateWrongMAPSTO();
+END $_$;
+
+--17. Delete ambiguous 'Maps to' mappings
+DO $_$
+BEGIN
+	PERFORM VOCABULARY_PACK.DeleteAmbiguousMAPSTO();
+END $_$;	
+
+ --18. Build reverse relationship. This is necessary for next point
+INSERT INTO concept_relationship_stage (
+	concept_code_1,
+	concept_code_2,
+	vocabulary_id_1,
+	vocabulary_id_2,
+	relationship_id,
+	valid_start_date,
+	valid_end_date,
+	invalid_reason
+	)
+SELECT crs.concept_code_2,
+	crs.concept_code_1,
+	crs.vocabulary_id_2,
+	crs.vocabulary_id_1,
+	r.reverse_relationship_id,
+	crs.valid_start_date,
+	crs.valid_end_date,
+	crs.invalid_reason
+FROM concept_relationship_stage crs
+JOIN relationship r ON r.relationship_id = crs.relationship_id
+WHERE NOT EXISTS (
+		-- the inverse record
+		SELECT 1
+		FROM concept_relationship_stage i
+		WHERE crs.concept_code_1 = i.concept_code_2
+			AND crs.concept_code_2 = i.concept_code_1
+			AND crs.vocabulary_id_1 = i.vocabulary_id_2
+			AND crs.vocabulary_id_2 = i.vocabulary_id_1
+			AND r.reverse_relationship_id = i.relationship_id
+		);
+
+--19. Deprecate all relationships in concept_relationship that aren't exist in concept_relationship_stage
+INSERT INTO concept_relationship_stage (
+	concept_code_1,
+	concept_code_2,
+	vocabulary_id_1,
+	vocabulary_id_2,
+	relationship_id,
+	valid_start_date,
+	valid_end_date,
+	invalid_reason
+	)
+SELECT a.concept_code,
+	b.concept_code,
+	a.vocabulary_id,
+	b.vocabulary_id,
+	relationship_id,
+	r.valid_start_date,
+	CURRENT_DATE,
+	'D'
+FROM concept a
+JOIN concept_relationship r ON a.concept_id = concept_id_1
+	AND r.invalid_reason IS NULL
+JOIN concept b ON b.concept_id = concept_id_2
+WHERE 'HemOnc' IN (
+		a.vocabulary_id,
+		b.vocabulary_id
+		)
+	AND NOT EXISTS (
+		SELECT 1
+		FROM concept_relationship_stage crs_int
+		WHERE crs_int.concept_code_1 = a.concept_code
+			AND crs_int.concept_code_2 = b.concept_code
+			AND crs_int.vocabulary_id_1 = a.vocabulary_id
+			AND crs_int.vocabulary_id_2 = b.vocabulary_id
+			AND crs_int.relationship_id = r.relationship_id
+		);
+		-- At the end, the three tables concept_stage, concept_relationship_stage and concept_synonym_stage should be ready to be fed into the generic_update.sql script
+

		'Route',
		'Procedure',
		--added 30-Aug-2019
		'Modality'
		--need to be added, requires further analysis of relationships
		/*
		'BioCondition',
		'Condition',
		'Condition Class'
		*/
		)
	AND h.concept_name IS NOT NULL;

--4. Create concept_relationship_stage
INSERT INTO concept_relationship_stage

	r.vocabulary_id_1,
	r.vocabulary_id_2,
	r.relationship_id,
    case when
r.valid_start_date is null then (select latest_update from vocabulary WHERE vocabulary_id = 'HemOnc') else r.valid_start_date end,
   case when
r.valid_end_date is null then TO_DATE('20991231', 'yyyymmdd') else r.valid_end_date end,
	r.invalid_reason
FROM sources.hemonc_crs r
JOIN concept_stage cs ON cs.concept_code = r.concept_code_1
