File size: 18,577 Bytes
12ac14b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import csv
import sql_utils as su
from tqdm.notebook import tqdm_notebook


def create_characterization_dt_tables(connection):

    CharacterizationInfo_str = "CREATE TABLE IF NOT EXISTS " + \
        "CharacterizationInfo " + \
        "(ID INT, CharType VARCHAR(150), CharName VARCHAR(150), " + \
        "AssayType VARCHAR(150), Protocol TEXT, " + \
        "DesignDescription TEXT, AnalysisAndConclusion TEXT);"

    CharExpConfig_str = "CREATE TABLE IF NOT EXISTS " + \
        "CharExpConfig " + \
        "(ID INT, CharType VARCHAR(150), CharName VARCHAR(150), " + \
        "AssayType VARCHAR(150), ExpConfigTechnique TEXT, " + \
        "ExpConfigInstruments TEXT, ExpConfigDescription TEXT);"

    CharResultDescriptions_str = "CREATE TABLE IF NOT EXISTS " + \
        "CharResultDescriptions " + \
        "(ID INT, CharType VARCHAR(150), CharName VARCHAR(150), " + \
        "AssayType VARCHAR(150), CharResultDescription TEXT);"

    CharResultKeywords_str = "CREATE TABLE IF NOT EXISTS " + \
        "CharResultKeywords " + \
        "(ID INT, CharType VARCHAR(150), CharName VARCHAR(150), " + \
        "AssayType VARCHAR(150), CharResultKeyword VARCHAR(150)); "

    CharResultTables_str = "CREATE TABLE IF NOT EXISTS " + \
        "CharResultTables " + \
        "(ID INT, CharType VARCHAR(150), CharName VARCHAR(150), " + \
        "AssayType VARCHAR(150), CharTable TEXT); "

    table_creation_querys = [
        CharacterizationInfo_str,
        CharExpConfig_str,
        CharResultDescriptions_str,
        CharResultKeywords_str,
        CharResultTables_str
        ]

    for query in table_creation_querys:
        results = su.simple_querry(
            connection,
            query
            )
        if len(results) == 0:
            print("Table Exists")
        else:
            print(results)

    su.show_tables(connection)


def characterization_to_sql(
        characterization_dt,
        connection
):

    total_ids = len(characterization_dt)
    no_sample_list = []
    with tqdm_notebook(
        total=total_ids,
        desc='Processing',
        unit='ID'
    ) as progress_bar:
        cursor = connection.cursor()

        for ID in characterization_dt:
            sample_info = characterization_dt[ID]

            for CharType_info in sample_info:
                if 'type' not in CharType_info:
                    print('There is no characterization with your sample.')
                    no_sample_list.append([ID, CharType_info])
                    continue
                CharType = CharType_info['type']
                CharType_Info_Assay = CharType_info['charsByAssayType']
                for CharName in CharType_Info_Assay:
                    CharName_infos = CharType_Info_Assay[CharName]
                    for charname_info in CharName_infos:
                        displayableItems = charname_info['displayableItems']
                        # Create a cursor object
                        for item in displayableItems:
                            if item['name'] == 'Assay Type':
                                AssayType = item['value']

                            if item['name'] == 'Protocol':
                                Protocol = item['value']

                            if item['name'] == 'Design Description':
                                DesignDescription = item['value']

                            if item['name'] == 'Experiment Configurations':
                                exp_config_list = item['value']

                            if item['name'] == 'Characterization Results':
                                char_results = item['value']

                            if item['name'] == 'Analysis and Conclusion':
                                AnalysisAndConclusion = item['value']

                        n_row = len(exp_config_list[0]['Technique'])
                        for i in range(n_row):
                            ExpConfigTechnique = exp_config_list[
                                0
                            ]['Technique'][i]

                            ExpConfigInstruments = exp_config_list[
                                1
                            ]['Instruments'][i]

                            ExpConfigDescription = exp_config_list[
                                2
                            ]['Description'][i]

                            # Write to NanoEntDes
                            CharExpConfig_insert = (
                                ID,
                                CharType,
                                CharName,
                                AssayType,
                                ExpConfigTechnique,
                                ExpConfigInstruments,
                                ExpConfigDescription
                                )

                            # Execute a SELECT statement to check
                            # if the entry already exists

                            search_query = "SELECT COUNT(*) FROM " + \
                                "CharExpConfig" + \
                                " WHERE ID = ?" + \
                                " AND CharType = ?" + \
                                " AND CharName = ?" + \
                                " AND AssayType = ?" + \
                                " AND ExpConfigTechnique = ?" + \
                                " AND ExpConfigInstruments = ?;"

                            cursor.execute(
                                search_query,
                                CharExpConfig_insert[:6]
                            )
                            count = cursor.fetchone()[0]

                            # Check the count to determine
                            # if the entry exists
                            if count == 0:
                                # Entry does not exist, proceed
                                # with insertion
                                insert_query = "INSERT INTO " + \
                                    "CharExpConfig " + \
                                    "(ID, " + \
                                    "CharType, " + \
                                    "CharName, " + \
                                    "AssayType, " + \
                                    "ExpConfigTechnique, " + \
                                    "ExpConfigInstruments, " + \
                                    "ExpConfigDescription) " + \
                                    "VALUES (?, ?, ?, ?, ?, ?, ?)"
                                cursor.execute(
                                    insert_query,
                                    CharExpConfig_insert
                                )
                                connection.commit()

                        for char_result in char_results:
                            if 'Data and Conditions' in char_result:
                                table_list = char_result[
                                    'Data and Conditions'
                                ]
                                CharTable = ""
                                for item in table_list:
                                    tsc = ",".join(
                                        item[
                                            'value'
                                        ]
                                    )
                                    CharTable += tsc
                                    CharTable += ";"
                                    # Write to NanoEntDes
                                    CRTables_insert = (
                                        ID,
                                        CharType,
                                        CharName,
                                        AssayType,
                                        CharTable
                                        )

                                    # Execute a SELECT
                                    # statement to check
                                    # if the entry already exists

                                    search_query = "SELECT " + \
                                        "COUNT(*) FROM " + \
                                        "CharResultTables" + \
                                        " WHERE ID = ?" + \
                                        " AND CharType = ?" + \
                                        " AND CharName = ?" + \
                                        " AND AssayType = ?;"

                                    cursor.execute(
                                        search_query,
                                        CRTables_insert[:4]
                                    )
                                    count = cursor.fetchone()[0]

                                    # Check the count to determine
                                    # if the entry exists
                                    if count == 0:
                                        # Entry does not exist, proceed
                                        # with insertion
                                        insert_query = "INSERT " + \
                                            "INTO " + \
                                            "CharResultTables " + \
                                            "(ID, " + \
                                            "CharType, " + \
                                            "CharName, " + \
                                            "AssayType, " + \
                                            "CharTable) " + \
                                            "VALUES (?, ?, ?, ?, ?)"
                                        cursor.execute(
                                            insert_query,
                                            CRTables_insert
                                        )
                                        connection.commit()
                            if 'Files' in char_result:
                                file_list = char_result[
                                    'Files'
                                ]

                                for char_file in file_list:
                                    if 'description' in char_file:
                                        CRDes = char_file[
                                            'description'
                                        ]
                                    else:
                                        CRDes = "None"

                                    if 'keywordsString' in char_file:
                                        CRKWstr_ls = char_file[
                                            'keywordsString'
                                        ].split(",")
                                    else:
                                        CRKWstr_ls = list("None")

                                    CRDes_insert = (
                                        ID,
                                        CharType,
                                        CharName,
                                        AssayType,
                                        CRDes
                                        )

                                    # Execute a SELECT
                                    # statement to check
                                    # if the entry already exists

                                    search_query = "SELECT " + \
                                        "COUNT(*) FROM " + \
                                        "CharResultDescriptions" + \
                                        " WHERE ID = ?" + \
                                        " AND CharType = ?" + \
                                        " AND CharName = ?" + \
                                        " AND AssayType = ?;"

                                    cursor.execute(
                                        search_query,
                                        CRDes_insert[:4]
                                    )
                                    count = cursor.fetchone()[0]

                                    # Check the count to determine
                                    # if the entry exists
                                    if count == 0:
                                        # Entry does not exist, proceed
                                        # with insertion
                                        insert_query = "INSERT " + \
                                            "INTO " + \
                                            "CharResultDescriptions" +\
                                            " (ID, " + \
                                            "CharType, " + \
                                            "CharName, " + \
                                            "AssayType, " + \
                                            "CharResultDescription" + \
                                            ") " + \
                                            "VALUES (?, ?, ?, ?, ?)"

                                        cursor.execute(
                                            insert_query,
                                            CRDes_insert
                                        )
                                        connection.commit()

                                    for CRKW in CRKWstr_ls:
                                        CRKW_insert = (
                                            ID,
                                            CharType,
                                            CharName,
                                            AssayType,
                                            CRKW
                                        )

                                        # Execute a SELECT
                                        # statement to check
                                        # if the entry already exists

                                        search_query = "SELECT " + \
                                            "COUNT(*) FROM " + \
                                            "CharResultKeywords" + \
                                            " WHERE ID = ?" + \
                                            " AND CharType = ?" + \
                                            " AND CharName = ?" + \
                                            " AND AssayType = ?" + \
                                            " AND " + \
                                            "CharResultKeyword = ?;"

                                        cursor.execute(
                                            search_query,
                                            CRKW_insert
                                        )
                                        count = cursor.fetchone()[0]

                                        # Check the count to determine
                                        # if the entry exists
                                        if count == 0:
                                            # Entry does not exist,
                                            # proceed
                                            # with insertion
                                            insert_query = "INSERT " +\
                                                "INTO " + \
                                                "CharResultKeywords" +\
                                                " (ID, " + \
                                                "CharType, " + \
                                                "CharName, " + \
                                                "AssayType, " + \
                                                "CharResultKeyword" + \
                                                ") " + \
                                                "VALUES (?, ?, " + \
                                                "?, ?, ?)"

                                            cursor.execute(
                                                insert_query,
                                                CRKW_insert
                                            )
                                            connection.commit()

                        # Write to NanoEntDes
                        CharacterizationInfo_insert = (
                            ID,
                            CharType,
                            CharName,
                            AssayType,
                            Protocol,
                            DesignDescription,
                            AnalysisAndConclusion
                            )

                        # Execute a SELECT statement to check
                        # if the entry already exists

                        search_query = "SELECT COUNT(*) FROM " + \
                            "CharacterizationInfo" + \
                            " WHERE ID = ?" + \
                            " AND CharType = ?" + \
                            " AND CharName = ?" + \
                            " AND AssayType = ?" + \
                            " AND Protocol = ?;"

                        cursor.execute(
                            search_query,
                            CharacterizationInfo_insert[:5]
                        )
                        count = cursor.fetchone()[0]

                        # Check the count to determine if the entry exists
                        if count == 0:
                            # Entry does not exist, proceed with insertion
                            insert_query = "INSERT INTO " + \
                                "CharacterizationInfo " + \
                                "(ID, " + \
                                "CharType, " + \
                                "CharName, " + \
                                "AssayType, " + \
                                "Protocol, " + \
                                "DesignDescription, " + \
                                "AnalysisAndConclusion) " + \
                                "VALUES (?, ?, ?, ?, ?, ?, ?)"
                            cursor.execute(
                                insert_query,
                                CharacterizationInfo_insert
                            )
                            connection.commit()
            progress_bar.update(1)
    cursor.close()
    no_sample_csv = 'no_characterization.csv'
    # Specify the filename for the CSV file

    with open(no_sample_csv, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerows(no_sample_list)

    print(f"CSV file '{no_sample_csv}' has been created.")