View Javadoc
1   package ca.uhn.fhir.jpa.term.loinc;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion;
24  import ca.uhn.fhir.jpa.entity.TermConcept;
25  import ca.uhn.fhir.jpa.term.IHapiTerminologyLoaderSvc;
26  import ca.uhn.fhir.jpa.term.IRecordHandler;
27  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
28  import org.apache.commons.csv.CSVRecord;
29  import org.hl7.fhir.r4.model.ConceptMap;
30  import org.hl7.fhir.r4.model.Enumerations;
31  import org.hl7.fhir.r4.model.ValueSet;
32  
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Properties;
36  
37  import static org.apache.commons.lang3.StringUtils.defaultString;
38  import static org.apache.commons.lang3.StringUtils.trim;
39  
40  public class LoincPartRelatedCodeMappingHandler extends BaseLoincHandler implements IRecordHandler {
41  
42  	public static final String LOINC_SCT_PART_MAP_ID = "loinc-parts-to-snomed-ct";
43  	public static final String LOINC_SCT_PART_MAP_URI = "http://loinc.org/cm/loinc-parts-to-snomed-ct";
44  	public static final String LOINC_SCT_PART_MAP_NAME = "LOINC Part Map to SNOMED CT";
45  	public static final String LOINC_RXNORM_PART_MAP_ID = "loinc-parts-to-rxnorm";
46  	public static final String LOINC_RXNORM_PART_MAP_URI = "http://loinc.org/cm/loinc-parts-to-rxnorm";
47  	public static final String LOINC_RXNORM_PART_MAP_NAME = "LOINC Part Map to RxNORM";
48  	public static final String LOINC_RADLEX_PART_MAP_ID = "loinc-parts-to-radlex";
49  	public static final String LOINC_RADLEX_PART_MAP_URI = "http://loinc.org/cm/loinc-parts-to-radlex";
50  	public static final String LOINC_RADLEX_PART_MAP_NAME = "LOINC Part Map to RADLEX";
51  	private static final String CM_COPYRIGHT = "This content from LOINC® is copyright © 1995 Regenstrief Institute, Inc. and the LOINC Committee, and available at no cost under the license at https://loinc.org/license/. The LOINC Part File, LOINC/SNOMED CT Expression Association and Map Sets File, RELMA database and associated search index files include SNOMED Clinical Terms (SNOMED CT®) which is used by permission of the International Health Terminology Standards Development Organisation (IHTSDO) under license. All rights are reserved. SNOMED CT® was originally created by The College of American Pathologists. “SNOMED” and “SNOMED CT” are registered trademarks of the IHTSDO. Use of SNOMED CT content is subject to the terms and conditions set forth in the SNOMED CT Affiliate License Agreement.  It is the responsibility of those implementing this product to ensure they are appropriately licensed and for more information on the license, including how to register as an Affiliate Licensee, please refer to http://www.snomed.org/snomed-ct/get-snomed-ct or info@snomed.org. Under the terms of the Affiliate License, use of SNOMED CT in countries that are not IHTSDO Members is subject to reporting and fee payment obligations. However, IHTSDO agrees to waive the requirements to report and pay fees for use of SNOMED CT content included in the LOINC Part Mapping and LOINC Term Associations for purposes that support or enable more effective use of LOINC. This material includes content from the US Edition to SNOMED CT, which is developed and maintained by the U.S. National Library of Medicine and is available to authorized UMLS Metathesaurus Licensees from the UTS Downloads site at https://uts.nlm.nih.gov.";
52  	private final Map<String, TermConcept> myCode2Concept;
53  	private final TermCodeSystemVersion myCodeSystemVersion;
54  	private final List<ConceptMap> myConceptMaps;
55  
56  	public LoincPartRelatedCodeMappingHandler(TermCodeSystemVersion theCodeSystemVersion, Map<String, TermConcept> theCode2concept, List<ValueSet> theValueSets, List<ConceptMap> theConceptMaps, Properties theUploadProperties) {
57  		super(theCode2concept, theValueSets, theConceptMaps, theUploadProperties);
58  		myCodeSystemVersion = theCodeSystemVersion;
59  		myCode2Concept = theCode2concept;
60  		myConceptMaps = theConceptMaps;
61  	}
62  
63  	@Override
64  	public void accept(CSVRecord theRecord) {
65  
66  		String partNumber = trim(theRecord.get("PartNumber"));
67  		String partName = trim(theRecord.get("PartName"));
68  		String partTypeName = trim(theRecord.get("PartTypeName"));
69  		String extCodeId = trim(theRecord.get("ExtCodeId"));
70  		// TODO: use hex code for ascii 160
71  		extCodeId = extCodeId.replace(" ", "");
72  		String extCodeDisplayName = trim(theRecord.get("ExtCodeDisplayName"));
73  		String extCodeSystem = trim(theRecord.get("ExtCodeSystem"));
74  		String mapType = trim(theRecord.get("MapType"));
75  		String contentOrigin = trim(theRecord.get("ContentOrigin"));
76  		String extCodeSystemVersion = trim(theRecord.get("ExtCodeSystemVersion"));
77  		String extCodeSystemCopyrightNotice = trim(theRecord.get("ExtCodeSystemCopyrightNotice"));
78  
79  		Enumerations.ConceptMapEquivalence equivalence;
80  		switch (trim(defaultString(mapType))) {
81  			case "":
82  			case "Exact":
83  				// 'equal' is more exact than 'equivalent' in the equivalence codes
84  				equivalence = Enumerations.ConceptMapEquivalence.EQUAL;
85  				break;
86  			case "LOINC broader":
87  				equivalence = Enumerations.ConceptMapEquivalence.NARROWER;
88  				break;
89  			case "LOINC narrower":
90  				equivalence = Enumerations.ConceptMapEquivalence.WIDER;
91  				break;
92  			default:
93  				throw new InternalErrorException("Unknown MapType '" + mapType + "' for PartNumber: " + partNumber);
94  		}
95  
96  		String loincPartMapId;
97  		String loincPartMapUri;
98  		String loincPartMapName;
99  		switch (extCodeSystem) {
100 			case IHapiTerminologyLoaderSvc.SCT_URI:
101 				loincPartMapId = LOINC_SCT_PART_MAP_ID;
102 				loincPartMapUri = LOINC_SCT_PART_MAP_URI;
103 				loincPartMapName = LOINC_SCT_PART_MAP_NAME;
104 				break;
105 			case "http://www.nlm.nih.gov/research/umls/rxnorm":
106 				loincPartMapId = LOINC_RXNORM_PART_MAP_ID;
107 				loincPartMapUri = LOINC_RXNORM_PART_MAP_URI;
108 				loincPartMapName = LOINC_RXNORM_PART_MAP_NAME;
109 				break;
110 			case "http://www.radlex.org":
111 				loincPartMapId = LOINC_RADLEX_PART_MAP_ID;
112 				loincPartMapUri = LOINC_RADLEX_PART_MAP_URI;
113 				loincPartMapName = LOINC_RADLEX_PART_MAP_NAME;
114 				break;
115 			default:
116 				throw new InternalErrorException("Don't know how to handle mapping to system: " + extCodeSystem);
117 		}
118 
119 		addConceptMapEntry(
120 			new ConceptMapping()
121 				.setConceptMapId(loincPartMapId)
122 				.setConceptMapUri(loincPartMapUri)
123 				.setConceptMapName(loincPartMapName)
124 				.setSourceCodeSystem(IHapiTerminologyLoaderSvc.LOINC_URI)
125 				.setSourceCode(partNumber)
126 				.setSourceDisplay(partName)
127 				.setTargetCodeSystem(extCodeSystem)
128 				.setTargetCode(extCodeId)
129 				.setTargetDisplay(extCodeDisplayName)
130 				.setTargetCodeSystemVersion(extCodeSystemVersion)
131 				.setEquivalence(equivalence)
132 				.setCopyright(extCodeSystemCopyrightNotice),
133 			CM_COPYRIGHT
134 		);
135 
136 	}
137 
138 }