예제 #1
0
    def test_smartonfhir_collection(self):
        """ Pull a sample of the smartonfhir observation data and generate the results in """
        from fhirtordf.loaders.fhircollectionloader import FHIRCollection

        output_dir = os.path.join(self.test_output_directory,
                                  'smartonfhir_testdata', 'obs_sample')
        make_and_clear_directory(output_dir)
        collection = \
            FHIRCollection(self.fhir_ontology,
                           os.path.join(self.test_input_directory,  'smartonfhir_testdata', 'json', 'obs_sample.json'),
                           "https://sb-fhir-dstu2.smarthealthit.org/api/smartdstu2/open/")
        generated_files = []
        for entry in collection.entries:
            os.makedirs(os.path.join(output_dir, entry.resource_type),
                        exist_ok=True)
            with open(
                    os.path.join(output_dir, entry.resource_type,
                                 entry.resource_id + '.ttl'), 'w') as output:
                output.write(str(entry))
            generated_files.append(
                os.path.join(entry.resource_type, entry.resource_id + ".ttl"))
        self.assertEqual([
            'Observation/SMART-Observation-5-smokingstatus.ttl',
            'Observation/SMART-Observation-6-smokingstatus.ttl',
            'Observation/SMART-Observation-18-smokingstatus.ttl',
            'Observation/SMART-Observation-1098667-gestage.ttl',
            'Observation/SMART-Observation-1681-lab.ttl',
            'Observation/SMART-Observation-1682-lab.ttl',
            'Observation/SMART-Observation-1683-lab.ttl',
            'Observation/SMART-Observation-1684-lab.ttl',
            'Observation/SMART-Observation-1685-lab.ttl'
        ], generated_files)
        print("\n*****> Check outputs in {}".format(output_dir))
예제 #2
0
    def test_careplan_instancenum(self):
        # This is creating the correct parsing for this resource.  Once the FHIR resource is fixed, we should
        # pull this
        from fhirtordf.loaders.fhircollectionloader import FHIRCollection

        collection = FHIRCollection(self.fhir_ontology,
                                    os.path.join(self.test_input_directory, 'synthea_data',
                                                 'CarePlan.json'),
                                    "http://standardhealthrecord.org/fhir/")
        for entry in collection.entries:
            print(entry)
        self.assertTrue(False)
예제 #3
0
    def test_synthea_collection(self):
        from fhirtordf.loaders.fhircollectionloader import FHIRCollection

        output_dir = os.path.join(self.test_output_directory, 'synthea_data',
                                  'ttl')
        make_and_clear_directory(output_dir)

        collection = FHIRCollection(
            self.fhir_ontology,
            os.path.join(self.test_input_directory, 'synthea_data',
                         'Adams301_Keyshawn30_74.json'),
            "http://standardhealthrecord.org/fhir/")
        generated_files = []
        for entry in collection.entries:
            output_fname = os.path.join(output_dir, entry.resource_type,
                                        entry.resource_id + ".ttl")
            os.makedirs(os.path.join(output_dir, entry.resource_type),
                        exist_ok=True)
            with open(output_fname, 'w') as output:
                output.write(str(entry))

            generated_files.append(
                os.path.join(
                    entry.resource_type,
                    (entry.resource_id if entry.resource_type
                     not in ["CarePlan", "MedicationRequest"] else "<UUID>") +
                    ".ttl"))

        self.assertEqual([
            'Patient/526238ef-dec3-401d-a1c1-2974962df23f.ttl',
            'Organization/9c00d82c-8deb-4e3c-80ee-2e9221addbc0.ttl',
            'Encounter/ff4f9269-5036-45dc-b0fb-d8133ae13c76.ttl',
            'Condition/9eba3a2a-b040-4697-8b46-41f87f3bbe71.ttl',
            'Condition/e69d5da5-4408-44e5-9ed0-2e93aacae4fa.ttl',
            'Condition/ccbc87d7-8439-4558-9672-7d58c56aa56c.ttl',
            'Condition/0b901fad-ecc3-4135-9ee8-ff4534010c44.ttl',
            'Observation/1d52ae90-6721-476c-ad4c-ec978eac4a34.ttl',
            'DiagnosticReport/4d7445b4-d001-4679-8053-d564fddbb973.ttl',
            'CarePlan/<UUID>.ttl', 'MedicationRequest/<UUID>.ttl',
            'MedicationRequest/<UUID>.ttl', 'MedicationRequest/<UUID>.ttl',
            'MedicationRequest/<UUID>.ttl', 'MedicationRequest/<UUID>.ttl',
            'MedicationRequest/<UUID>.ttl', 'MedicationRequest/<UUID>.ttl',
            'MedicationRequest/<UUID>.ttl', 'MedicationRequest/<UUID>.ttl',
            'MedicationRequest/<UUID>.ttl', 'MedicationRequest/<UUID>.ttl',
            'MedicationRequest/<UUID>.ttl', 'MedicationRequest/<UUID>.ttl'
        ], generated_files)
        print("\n*****> Check outputs in {}".format(output_dir))
예제 #4
0
def fhir_json_to_rdf(
        json_fname: str,
        base_uri: str = "http://hl7.org/fhir/",
        target_graph: Optional[Graph] = None,
        add_ontology_header: bool = True,
        do_continuations: bool = True,
        replace_narrative_text: bool = False,
        metavoc: Optional[Union[Graph, FHIRMetaVoc]] = None) -> Graph:
    """
    Convert a FHIR JSON resource image to RDF
    :param json_fname: Name or URI of the file to convert
    :param base_uri: Base URI to use for relative references.
    :param target_graph:  If supplied, add RDF to this graph. If not, start with an empty graph.
    :param add_ontology_header:  True means add owl:Ontology declaration to output
    :param do_continuations: True means follow continuation records on bundles and queries
    :param replace_narrative_text: True means replace any narrative text longer than 120 characters with
                '<div xmlns="http://www.w3.org/1999/xhtml">(removed)</div>'
    :param metavoc: FHIR Metadata Vocabulary (fhir.ttl) graph
    :return: resulting graph
    """
    def check_for_continuation(data_: JsonObj) -> Optional[str]:
        if do_continuations and 'link' in data_ and isinstance(
                data_.link, list):
            for link_e in data_.link:
                if 'relation' in link_e and link_e.relation == 'next':
                    return link_e.url
        return None

    if target_graph is None:
        target_graph = Graph()

    if metavoc is None:
        metavoc = FHIRMetaVoc().g
    elif isinstance(metavoc, FHIRMetaVoc):
        metavoc = metavoc.g

    page_fname = json_fname
    while page_fname:
        data = load(page_fname)
        if 'resourceType' in data and data.resourceType != 'Bundle':
            FHIRResource(metavoc,
                         None,
                         base_uri,
                         data,
                         target=target_graph,
                         add_ontology_header=add_ontology_header,
                         replace_narrative_text=replace_narrative_text)
            page_fname = check_for_continuation(data)
        elif 'entry' in data and isinstance(
                data.entry, list) and 'resource' in data.entry[0]:
            FHIRCollection(metavoc,
                           None,
                           base_uri,
                           data,
                           target=target_graph,
                           add_ontology_header=add_ontology_header
                           if 'resourceType' in data else False,
                           replace_narrative_text=replace_narrative_text)
            page_fname = check_for_continuation(data)
        else:
            page_fname = None
            target_graph = None
    return target_graph