File size: 8,417 Bytes
44a9dbf
 
 
 
5472b6c
 
 
 
 
 
 
 
 
 
44a9dbf
5472b6c
 
 
 
 
ae03dcd
5472b6c
44a9dbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5472b6c
 
 
44a9dbf
 
 
 
 
 
 
 
 
 
 
 
5472b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44a9dbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5472b6c
 
 
44a9dbf
 
 
 
5472b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44a9dbf
5472b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44a9dbf
5472b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44a9dbf
 
 
 
 
 
c5fa079
5472b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44a9dbf
 
 
c5fa079
 
 
 
 
 
5472b6c
 
 
 
 
c5fa079
5472b6c
 
c5fa079
5472b6c
 
44a9dbf
 
5472b6c
44a9dbf
 
 
 
 
 
 
5472b6c
 
 
 
 
ae03dcd
 
 
 
 
 
5472b6c
44a9dbf
 
ae03dcd
 
 
 
 
 
 
 
 
44a9dbf
5472b6c
ae03dcd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import csv
import os
import argparse
import pandas as pd
from utilities.anonymize import (
    get_cities_list,
    get_abbreviation_dict_correction,
    reformat_to_report,
    anonymize_analyzer,
    anonymize_engine,
    add_space_to_comma_endpoint,
    get_list_not_deidentify,
    config_deidentify,
)
from utilities.translate import get_translation_dict_correction, translate_report
from utilities.convert import (
    convert_df_no_header,
    convert_df,
    convert_json,
    convert_list_phenogenius,
    convert_pdf_to_text,
)
from utilities.extract_hpo import add_biometrics, extract_hpo
from utilities.get_model import get_models, get_nlp_marian
import gc


def main():

    print("Code Starting")
    MarianText, _, _ = translate_report(
        Report,
        Last_name,
        First_name,
        nlp_fr,
        marian_fr_en,
        dict_correction,
        dict_abbreviation_correction,
    )
    MarianText_report = reformat_to_report(MarianText, nlp_fr)
    del MarianText

    print("Translation and De-identification")
    (
        MarianText_anonymize_report_analyze,
        analyzer_results_return,
        _,
        _,
    ) = anonymize_analyzer(
        MarianText_report, analyzer, proper_noun, Last_name, First_name
    )

    print(MarianText_anonymize_report_analyze)

    MarianText_anonymize_report_engine = anonymize_engine(
        MarianText_report, analyzer_results_return, engine, nlp_fr
    )

    MarianText_anonymize_report_engine_modif = pd.DataFrame(
        [x for x in MarianText_anonymize_report_engine.split("\n")]
    )

    MarianText_anonymize_report_engine_df = MarianText_anonymize_report_engine_modif
    with open(
        os.path.join(args.result_dir, "Reports", "")
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_translated_and_deindentified_report.txt",
        "w",
    ) as file:
        file.write(
            convert_df_no_header(MarianText_anonymize_report_engine_df).decode("utf-8")
        )
    print(
        "Text file created successfully : "
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_translated_and_deindentified_report.txt"
    )

    print("Summarization")

    MarianText_anonymized_reformat_space = add_space_to_comma_endpoint(
        MarianText_anonymize_report_engine, nlp_fr
    )
    MarianText_anonymized_reformat_biometrics, _ = add_biometrics(
        MarianText_anonymized_reformat_space, nlp_fr
    )
    clinphen, clinphen_unsafe = extract_hpo(MarianText_anonymized_reformat_biometrics)

    del MarianText_anonymize_report_engine
    del MarianText_anonymized_reformat_space
    del MarianText_anonymized_reformat_biometrics

    clinphen_unsafe_check_raw = clinphen_unsafe
    clinphen_unsafe_check_raw["To keep in list"] = False
    clinphen_unsafe_check_raw["Confidence on extraction"] = "low"

    del clinphen_unsafe

    clinphen["Confidence on extraction"] = "high"
    clinphen["To keep in list"] = True

    cols = [
        "HPO ID",
        "Phenotype name",
        "To keep in list",
        "No. occurrences",
        "Earliness (lower = earlier)",
        "Confidence on extraction",
        "Example sentence",
    ]
    clinphen_all = pd.concat([clinphen, clinphen_unsafe_check_raw]).reset_index()
    clinphen_all = clinphen_all[cols]

    clinphen_df = clinphen_all
    clinphen_df_without_low_confidence = clinphen_df[
        clinphen_df["To keep in list"] == True
    ]
    del clinphen
    del clinphen_unsafe_check_raw
    gc.collect()

    with open(
        os.path.join(args.result_dir, "TSV", "")
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_summarized_report.tsv",
        "w",
    ) as file:
        file.write(convert_df(clinphen_df).decode("utf-8"))
    print(
        "Tsv file created successfully : "
        + os.path.join(args.result_dir, "TSV", "")
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_summarized_report.tsv"
    )

    with open(
        os.path.join(args.result_dir, "JSON", "")
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_summarized_report.json",
        "w",
    ) as file:
        file.write(convert_json(clinphen_df_without_low_confidence))
    print(
        "JSON file created successfully : "
        + os.path.join(args.result_dir, "JSON", "")
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_summarized_report.json"
    )

    with open(
        os.path.join(args.result_dir, "TXT", "")
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_summarized_report.txt",
        "w",
    ) as file:
        file.write(convert_list_phenogenius(clinphen_df_without_low_confidence))
    print(
        "Text file created successfully : "
        + os.path.join(args.result_dir, "TXT", "")
        + Report_id
        + "_"
        + Last_name
        + "_"
        + First_name
        + "_summarized_report.txt"
    )


if __name__ == "__main__":

    print("Welcome to the Clinfly app")

    parser = argparse.ArgumentParser(description="Description of clinfly arguments")
    parser.add_argument(
        "--file",
        type=str,
        help="the input file which contains the visits informations",
        required=True,
    )
    parser.add_argument(
        "--language",
        choices=["fr", "es", "de"],
        type=str,
        help="The language of the input : fr, es , de",
        required=True,
    )
    parser.add_argument(
        "--model_dir",
        default=os.path.expanduser("~"),
        type=str,
        help="The directory where the models will be downloaded.",
    )
    parser.add_argument(
        "--result_dir",
        default="Results",
        type=str,
        help="The directory where the results will be placed.",
    )

    args = parser.parse_args()

    if not os.path.exists(args.model_dir):
        os.makedirs(args.model_dir)

    if not os.path.exists(args.result_dir):
        os.makedirs(args.result_dir)

    if not os.path.exists(os.path.join(args.result_dir, "Reports")):
        os.makedirs(os.path.join(args.result_dir, "Reports"))

    if not os.path.exists(os.path.join(args.result_dir, "TSV")):
        os.makedirs(os.path.join(args.result_dir, "TSV"))

    if not os.path.exists(os.path.join(args.result_dir, "JSON")):
        os.makedirs(os.path.join(args.result_dir, "JSON"))

    if not os.path.exists(os.path.join(args.result_dir, "TXT")):
        os.makedirs(os.path.join(args.result_dir, "TXT"))

    print("Language chosen :", args.language)
    models_status = get_models(args.language, args.model_dir)
    dict_correction = get_translation_dict_correction()
    dict_abbreviation_correction = get_abbreviation_dict_correction()
    proper_noun = get_list_not_deidentify()
    cities_list = get_cities_list()
    analyzer, engine = config_deidentify(cities_list)
    nlp_fr, marian_fr_en = get_nlp_marian(args.language)

    file_name = args.file
    Report_id: str
    Last_name: str
    First_name: str
    Report: str

    if os.path.isfile(args.file):
        with open(file_name, 'r') as fichier:
          for ligne in fichier:
            elements = ligne.strip().split('\t')
            Report_id, Last_name, First_name, text_or_link = elements
            print("Report_id:", Report_id)
            print("Last_name:", Last_name)
            print("First_name:", First_name)
            if os.path.exists(text_or_link):
                if text_or_link.lower().endswith('.pdf'):
                    print(f"Processing PDF file: {text_or_link}")
                    Report = convert_pdf_to_text(text_or_link)
                else:
                    print(f"Unsupported file type. Please provide a link to a PDF files.")
            else:
                Report = text_or_link
                print("Report:", Report)
            main()
            print()
    else:
        print("Input is not a file. Please provide a valid input.")