17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 | def get_i2b2_table(
spark_session: SparkSession, db_name: str, db_source: str, table: str
) -> SparkDataFrame: # pragma: no cover
"""
Retrieve a Spark table in i2b2 and transform it to fit with OMOP standard.
Parameters
----------
db_name: str
Name of the database where the data is stored.
table: str
Name of the table to extract.
Returns
-------
df: Spark DataFrame
Spark DataFrame extracted from the i2b2 database given and converted to OMOP standard.
"""
table_name = i2b2_tables[db_source][table]
columns = i2b2_renaming[table]
if db_source == "cse":
columns.pop("i2b2_action", None)
query = ",".join(["{k} AS {v}".format(k=k, v=v) for k, v in columns.items()])
df = spark_session.sql(f"""SELECT {query} FROM {db_name}.{table_name}""")
# Special mapping for i2b2 :
# CIM10
if table == "condition_occurrence":
df = df.withColumn(
"condition_source_value",
F.split(F.col("condition_source_value"), ":").getItem(1),
)
# Retrieve UF
df = df.withColumn(
"care_site_source_value",
F.split(F.col("care_site_source_value"), ":").getItem(1),
)
# CCAM
elif table == "procedure_occurrence":
df = df.withColumn(
"procedure_source_value",
F.split(F.col("procedure_source_value"), ":").getItem(1),
)
# Visits
elif table == "visit_occurrence":
df = df.withColumn(
"visit_source_value",
mapping_dict(visit_type_mapping, "Non Renseigné")(
F.col("visit_source_value")
),
)
if db_source == "cse":
df = df.withColumn("row_status_source_value", F.lit("Actif"))
df = df.withColumn(
"visit_occurrence_source_value", df["visit_occurrence_id"]
)
else:
df = df.withColumn(
"row_status_source_value",
F.when(
F.col("row_status_source_value").isin([-1, -2]), "supprimé"
).otherwise("Actif"),
)
# Retrieve Hospital trigram
ufr = spark_session.sql(
f"SELECT * FROM {db_name}.{i2b2_tables[db_source]['visit_detail']}"
)
ufr = ufr.withColumn(
"care_site_id",
F.substring(F.split(F.col("concept_cd"), ":").getItem(1), 1, 3),
)
ufr = ufr.withColumnRenamed("encounter_num", "visit_occurrence_id")
ufr = ufr.drop_duplicates(subset=["visit_occurrence_id"])
ufr = ufr.select(["visit_occurrence_id", "care_site_id"])
df = df.join(ufr, how="inner", on=["visit_occurrence_id"])
# Patients
elif table == "person":
df = df.withColumn(
"gender_source_value",
mapping_dict(sex_cd_mapping, "Non Renseigné")(F.col("gender_source_value")),
)
# Documents
elif table == "note":
df = df.withColumn(
"note_class_source_value",
F.substring(F.col("note_class_source_value"), 4, 100),
)
if db_source == "cse":
df = df.withColumn("row_status_source_value", F.lit("Actif"))
else:
df = df.withColumn(
"row_status_source_value",
F.when(F.col("row_status_source_value") < 0, "SUPP").otherwise("Actif"),
)
# Hospital trigrams
elif table == "care_site":
df = df.withColumn("care_site_type_source_value", F.lit("Hôpital"))
df = df.withColumn(
"care_site_source_value",
F.split(F.col("care_site_source_value"), ":").getItem(1),
)
df = df.withColumn(
"care_site_id", F.substring(F.col("care_site_source_value"), 1, 3)
)
df = df.drop_duplicates(subset=["care_site_id"])
df = df.withColumn(
"care_site_short_name",
mapping_dict(dict_code_UFR, "Non Renseigné")(F.col("care_site_id")),
)
# UFR
elif table == "visit_detail":
df = df.withColumn(
"care_site_id", F.split(F.col("care_site_id"), ":").getItem(1)
)
df = df.withColumn("visit_detail_type_source_value", F.lit("PASS"))
df = df.withColumn("row_status_source_value", F.lit("Actif"))
# biology
elif table == "biology":
df = df.withColumn(
"biology_source_value", F.substring(F.col("biology_source_value"), 5, 20)
)
# fact_relationship
elif table == "fact_relationship":
# Retrieve UF information
df = df.withColumn(
"fact_id_1",
F.split(F.col("care_site_source_value"), ":").getItem(1),
)
df = df.withColumn("domain_concept_id_1", F.lit(57)) # Care_site domain
# Retrieve hospital information
df = df.withColumn("fact_id_2", F.substring(F.col("fact_id_1"), 1, 3))
df = df.withColumn("domain_concept_id_2", F.lit(57)) # Care_site domain
df = df.drop_duplicates(subset=["fact_id_1", "fact_id_2"])
# Only UF-Hospital relationships in i2b2
df = df.withColumn("relationship_concept_id", F.lit(46233688)) # Included in
return df
|