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

12 cql cohort extraction #75

Merged
merged 16 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
log/
target/
output
ontology
juliangruendner marked this conversation as resolved.
Show resolved Hide resolved
dependencies
package.json
fhirpkg.lock.json
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
torch-data-store:
image: "samply/blaze:0.30"
environment:
BASE_URL: "http://torch-data-store:8080"
BASE_URL: "http://localhost:8082"
juliangruendner marked this conversation as resolved.
Show resolved Hide resolved
JAVA_TOOL_OPTIONS: "-Xmx2g"
LOG_LEVEL: "info"
ports:
Expand Down Expand Up @@ -30,7 +30,7 @@ services:
restart: unless-stopped
image: nginxinc/nginx-unprivileged:1.25.5-alpine
ports:
- ${PORT_TORCH_NGINX:-127.0.0.1:8080}:8080
- ${PORT_TORCH_NGINX:-127.0.0.1:80}:8080
volumes:
- ./nginx.conf.template:/etc/nginx/nginx.conf.template
- ./start-nginx.sh:/start-nginx.sh
Expand Down
108 changes: 108 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<Hapi.version>6.2.5</Hapi.version>
<ontology-tag>v3.0.0-test.9</ontology-tag>
</properties>

<dependencies>
Expand Down Expand Up @@ -140,8 +141,115 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
juliangruendner marked this conversation as resolved.
Show resolved Hide resolved
<scope>provided</scope>
</dependency>

<dependency>
<groupId>de.medizininformatik-initiative</groupId>
<artifactId>sq2cql</artifactId>
<version>v0.5.0-alpha.1</version>
</dependency>

</dependencies>

<profiles>
<profile>
<id>download-ontology</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>delete-ontology-directories</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>rm</executable>
<arguments>
<argument>-rf</argument>
<argument>ontology</argument>
</arguments>
</configuration>
</execution>

<execution>
<id>create-ontology-directory</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mkdir</executable>
<arguments>
<argument>ontology</argument>
</arguments>
</configuration>
</execution>

<execution>
<id>download-ontology-mapping</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>curl</executable>
<arguments>
<argument>-L</argument>
<argument>https://github.com/medizininformatik-initiative/fhir-ontology-generator/raw/${ontology-tag}/example/fdpg-ontology/mapping.zip</argument>
<argument>-o</argument>
<argument>ontology/mapping.zip</argument>
juliangruendner marked this conversation as resolved.
Show resolved Hide resolved
</arguments>
</configuration>
</execution>

<execution>
<id>unpack-ontology-mapping</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>unzip</executable>
<arguments>
<argument>-jod</argument>
<argument>ontology/</argument>
<argument>ontology/mapping.zip</argument>
</arguments>
</configuration>
</execution>

<execution>
<id>delete-zip-files</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>rm</executable>
<arguments>
<argument>ontology/mapping.zip</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
<finalName>torch</finalName>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,37 @@
import de.medizininformatikinitiative.torch.CdsStructureDefinitionHandler;
import de.medizininformatikinitiative.torch.DataStore;
import de.medizininformatikinitiative.torch.ResourceTransformer;
import de.medizininformatikinitiative.torch.cql.*;
import de.medizininformatikinitiative.torch.rest.CapabilityStatementController;
import de.medizininformatikinitiative.torch.util.ElementCopier;
import de.medizininformatikinitiative.torch.util.Redaction;
import de.medizininformatikinitiative.torch.util.ResultFileManager;
import de.numcodex.sq2cql.Translator;
import de.numcodex.sq2cql.model.Mapping;
import de.numcodex.sq2cql.model.MappingContext;
import de.numcodex.sq2cql.model.MappingTreeBase;
import de.numcodex.sq2cql.model.MappingTreeModuleRoot;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Map.entry;

@Configuration
public class AppConfig {
Expand Down Expand Up @@ -63,6 +79,81 @@ public DataStore dataStore(@Qualifier("fhirClient") WebClient client, FhirContex
return new DataStore(client, context);
}

@Value("${torch.mappingsFile}")
private String mappingsFile;

@Value("${torch.conceptTreeFile}")
private String conceptTreeFile;

@Lazy
@Bean
Translator createCqlTranslator(@Qualifier("translation") ObjectMapper jsonUtil) throws IOException {
var mappings = jsonUtil.readValue(new File(mappingsFile), Mapping[].class);
var mappingTreeBase = new MappingTreeBase(Arrays.stream(jsonUtil.readValue(new File(conceptTreeFile), MappingTreeModuleRoot[].class)).toList());

return Translator.of(MappingContext.of(
Stream.of(mappings)
.collect(Collectors.toMap(Mapping::key, Function.identity(), (a, b) -> a)),
mappingTreeBase,
Map.ofEntries(entry("http://fhir.de/CodeSystem/bfarm/icd-10-gm", "icd10"),
entry("mii.abide", "abide"),
entry("http://fhir.de/CodeSystem/bfarm/ops", "ops"),
entry("http://dicom.nema.org/resources/ontology/DCM", "dcm"),
entry("https://www.medizininformatik-initiative.de/fhir/core/modul-person/CodeSystem/Vitalstatus", "vitalstatus"),
entry("http://loinc.org", "loinc"),
entry("https://fhir.bbmri.de/CodeSystem/SampleMaterialType", "sample"),
entry("http://fhir.de/CodeSystem/bfarm/atc", "atc"),
entry("http://snomed.info/sct", "snomed"),
entry("http://terminology.hl7.org/CodeSystem/condition-ver-status", "cvs"),
entry("http://hl7.org/fhir/administrative-gender", "gender"),
entry("urn:oid:1.2.276.0.76.5.409", "urn409"),
entry(
"https://www.netzwerk-universitaetsmedizin.de/fhir/CodeSystem/ecrf-parameter-codes",
"numecrf"),
entry("urn:iso:std:iso:3166", "iso3166"),
entry("https://www.netzwerk-universitaetsmedizin.de/fhir/CodeSystem/frailty-score",
"frailtyscore"),
entry("http://terminology.hl7.org/CodeSystem/consentcategorycodes", "consentcategory"),
entry("urn:oid:2.16.840.1.113883.3.1937.777.24.5.3", "consent"),
entry("http://hl7.org/fhir/sid/icd-o-3", "icdo3"),
entry("http://hl7.org/fhir/consent-provision-type", "provisiontype"))));
}

@Qualifier("cql")
@Lazy
@Bean
QueryTranslator createCqlQueryTranslator(
Translator sq2cqlTranslator,
@Qualifier("translation") ObjectMapper jsonUtil) {
return new CqlQueryTranslator(sq2cqlTranslator, jsonUtil);
}


@Bean
FhirConnector createFhirConnector(@Value("${torch.fhir.url}") String fhirUrl) {
return new FhirConnector(fhirContext().newRestfulGenericClient(fhirUrl));
}

@Bean
FhirHelper createFhirHelper(FhirContext fhirContext) {
return new FhirHelper(fhirContext);
}

@Bean
CqlClient createCqlQueryClient(
FhirConnector fhirConnector,
FhirHelper fhirHelper
) {

return new CqlClient(fhirConnector, fhirHelper);
}

@Qualifier("translation")
@Bean
ObjectMapper createTranslationObjectMapper() {
return new ObjectMapper();
}

@Bean
public ElementCopier elementCopier(CdsStructureDefinitionHandler cds) {
return new ElementCopier(cds);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.medizininformatikinitiative.torch.cql;

import lombok.extern.slf4j.Slf4j;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.ListResource;
import org.hl7.fhir.r4.model.MeasureReport;
import org.hl7.fhir.r4.model.Parameters;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;


@Slf4j
public class CqlClient {
private final FhirConnector fhirConnector;
private final FhirHelper fhirHelper;

public CqlClient(FhirConnector fhirConnector,
FhirHelper fhirHelper) {
this.fhirConnector = Objects.requireNonNull(fhirConnector);
this.fhirHelper = fhirHelper;
}


public Mono<List<String>> getPatientListByCql(String cqlQuery) {

var libraryUri = "urn:uuid" + UUID.randomUUID();
var measureUri = "urn:uuid" + UUID.randomUUID();
MeasureReport measureReport;

try {
Bundle bundle = fhirHelper.createBundle(cqlQuery, libraryUri, measureUri);
fhirConnector.transmitBundle(bundle);

Parameters params = fhirHelper.getListExecutionParams();
params.setParameter("measure", measureUri);
measureReport = fhirConnector.evaluateMeasure(measureUri, params);

var subjectListId = measureReport.getGroupFirstRep().getPopulationFirstRep().getSubjectResults().getReferenceElement().getIdPart();
return Mono.just(fhirConnector.searchAndExtractIds(subjectListId));
juliangruendner marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
throw new RuntimeException(e);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.medizininformatikinitiative.torch.cql;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.medizininformatikinitiative.torch.model.ccdl.StructuredQuery;
import de.numcodex.sq2cql.Translator;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

/**
* A translator for translating a {@link de.medizininformatikinitiative.torch.model.ccdl.StructuredQuery} into its CQL representation.
*/
@RequiredArgsConstructor
public class CqlQueryTranslator implements QueryTranslator {

@NonNull
private final Translator translator;

@NonNull
private final ObjectMapper jsonUtil;

@Override
public String translate(StructuredQuery query) throws QueryTranslationException {
de.numcodex.sq2cql.model.structured_query.StructuredQuery structuredQuery;
try {
structuredQuery = jsonUtil.readValue(jsonUtil.writeValueAsString(query),
de.numcodex.sq2cql.model.structured_query.StructuredQuery.class);
} catch (JsonProcessingException e) {
throw new QueryTranslationException("cannot encode/decode structured query as JSON", e);
}

try {
return translator.toCql(structuredQuery).print();
} catch (Exception e) {
throw new QueryTranslationException("cannot translate structured query to CQL format", e);
}
}
}
Loading