aboutsummaryrefslogtreecommitdiff
path: root/gn3/db/sample_data.py
blob: 8db40e3f9412660f106ac0f3e747368484670355 (plain)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
"""Module containing functions that work with sample data"""
from typing import Any, Tuple, Dict

import MySQLdb

from gn3.csvcmp import extract_strain_name
from gn3.csvcmp import parse_csv_column

_MAP = {
    "ProbeSetData": ("StrainId", "Id", "value"),
    "PublishData": ("StrainId", "Id", "value"),
    "ProbeSetSE": ("StrainId", "DataId", "error"),
    "PublishSE": ("StrainId", "DataId", "error"),
    "NStrain": ("StrainId", "DataId", "count"),
}


def __extract_actions(
    original_data: str, updated_data: str, csv_header: str
) -> Dict:
    """Return a dictionary containing elements that need to be deleted, inserted,
    or updated.

    """
    result: Dict[str, Any] = {
        "delete": {"data": [], "csv_header": []},
        "insert": {"data": [], "csv_header": []},
        "update": {"data": [], "csv_header": []},
    }
    strain_name = ""
    for _o, _u, _h in zip(
        original_data.strip().split(","),
        updated_data.strip().split(","),
        csv_header.strip().split(","),
    ):
        if _h == "Strain Name":
            strain_name = _o
        if _o == _u:  # No change
            continue
        if _o and _u == "x":  # Deletion
            result["delete"]["data"].append(_o)
            result["delete"]["csv_header"].append(_h)
        elif _o == "x" and _u:  # Insert
            result["insert"]["data"].append(_u)
            result["insert"]["csv_header"].append(_h)
        elif _o and _u:  # Update
            result["update"]["data"].append(_u)
            result["update"]["csv_header"].append(_h)
    for key, val in result.items():
        if not val["data"]:
            result[key] = None
        else:
            result[key]["data"] = f"{strain_name}," + ",".join(
                result[key]["data"]
            )
            result[key]["csv_header"] = "Strain Name," + ",".join(
                result[key]["csv_header"]
            )
    return result

def get_mrna_sample_data(
    conn: Any, probeset_id: str, dataset_name: str
) -> Dict:
    """Fetch a mRNA Assay (ProbeSet in the DB) trait's sample data and return it as a dict"""
    with conn.cursor() as cursor:
        cursor.execute("""
SELECT st.Name, ifnull(psd.value, 'x'), ifnull(psse.error, 'x'), ifnull(ns.count, 'x')
FROM ProbeSetFreeze psf
    JOIN ProbeSetXRef psx ON psx.ProbeSetFreezeId = psf.Id
    JOIN ProbeSet ps ON ps.Id = psx.ProbeSetId
    JOIN ProbeSetData psd ON psd.Id = psx.DataId
    JOIN Strain st ON psd.StrainId = st.Id
    LEFT JOIN ProbeSetSE psse ON psse.DataId = psd.Id AND psse.StrainId = psd.StrainId
    LEFT JOIN NStrain ns ON ns.DataId = psd.Id AND ns.StrainId = psd.StrainId
WHERE ps.Id = %s AND psf.Name= %s""", (probeset_id, dataset_name))

        sample_data = {}
        for data in cursor.fetchall():
            sample, value, error, n_cases = data
            sample_data[sample] = {
                'value': value,
                'error': error,
                'n_cases': n_cases
            }

        return sample_data

def get_mrna_csv_sample_data(
    conn: Any, probeset_id: str, dataset_name: str, sample_list: list
) -> str:
    """Fetch a mRNA Assay (ProbeSet in DB) trait and return it as a csv string"""
    with conn.cursor() as cursor:
        cursor.execute("""
SELECT DISTINCT st.Name, concat(st.Name, ',', ifnull(psd.value, 'x'), ',',
    ifnull(psse.error, 'x'), ',', ifnull(ns.count, 'x')) AS 'Data'
FROM ProbeSetFreeze psf
    JOIN ProbeSetXRef psx ON psx.ProbeSetFreezeId = psf.Id
    JOIN ProbeSet ps ON ps.Id = psx.ProbeSetId
    JOIN ProbeSetData psd ON psd.Id = psx.DataId
    JOIN Strain st ON psd.StrainId = st.Id
    LEFT JOIN ProbeSetSE psse ON psse.DataId = psd.Id AND psse.StrainId = psd.StrainId
    LEFT JOIN NStrain ns ON ns.DataId = psd.Id AND ns.StrainId = psd.StrainId
WHERE ps.Id = %s AND psf.Name= %s""", (probeset_id, dataset_name))

        if not (data := cursor.fetchall()):
            return "No Sample Data Found"

        # Get list of samples with data in the DB
        existing_samples = [el[0] for el in data]

        trait_csv = ["Strain Name,Value,SE,Count"]
        for sample in sample_list:
            if sample in existing_samples:
                trait_csv.append(data[existing_samples.index(sample)][1])
            else:
                trait_csv.append(sample + ",x,x,x")

        return "\n".join(trait_csv)

def get_pheno_sample_data(
    conn: Any, trait_name: int, phenotype_id: int
) -> Dict:
    """Fetch a phenotype (Publish in the DB) trait's sample data and return it as a dict"""
    with conn.cursor() as cursor:
        cursor.execute("""
SELECT st.Name, ifnull(pd.value, 'x'), ifnull(ps.error, 'x'), ifnull(ns.count, 'x')
FROM PublishFreeze pf JOIN PublishXRef px ON px.InbredSetId = pf.InbredSetId
     JOIN PublishData pd ON pd.Id = px.DataId JOIN Strain st ON pd.StrainId = st.Id
     LEFT JOIN PublishSE ps ON ps.DataId = pd.Id AND ps.StrainId = pd.StrainId
     LEFT JOIN NStrain ns ON ns.DataId = pd.Id AND ns.StrainId = pd.StrainId
WHERE px.Id = %s AND px.PhenotypeId = %s
ORDER BY st.Name""", (trait_name, phenotype_id))

        sample_data = {}
        for data in cursor.fetchall():
            sample, value, error, n_cases = data
            sample_data[sample] = {
                'value': value,
                'error': error,
                'n_cases': n_cases
            }

        return sample_data

def get_pheno_csv_sample_data(
    conn: Any, trait_name: int, group_id: int, sample_list: list
) -> str:
    """Fetch a phenotype (Publish in DB) trait and return it as a csv string"""
    with conn.cursor() as cursor:
        cursor.execute("""
SELECT DISTINCT st.Name, concat(st.Name, ',', ifnull(pd.value, 'x'), ',',
ifnull(ps.error, 'x'), ',', ifnull(ns.count, 'x')) AS 'Data'
FROM PublishFreeze pf JOIN PublishXRef px ON px.InbredSetId = pf.InbredSetId
     JOIN PublishData pd ON pd.Id = px.DataId JOIN Strain st ON pd.StrainId = st.Id
     LEFT JOIN PublishSE ps ON ps.DataId = pd.Id AND ps.StrainId = pd.StrainId
     LEFT JOIN NStrain ns ON ns.DataId = pd.Id AND ns.StrainId = pd.StrainId
WHERE px.Id = %s AND px.InbredSetId = %s ORDER BY st.Name""",
                       (trait_name, group_id))
        if not (data := cursor.fetchall()):
            return "No Sample Data Found"

        # Get list of samples with data in the DB
        existing_samples = [el[0] for el in data]

        trait_csv = ["Strain Name,Value,SE,Count"]
        for sample in sample_list:
            if sample in existing_samples:
                trait_csv.append(data[existing_samples.index(sample)][1])
            else:
                trait_csv.append(sample + ",x,x,x")

        return "\n".join(trait_csv)

def get_mrna_sample_data_ids(
    conn: Any, probeset_id: int, dataset_name: str, strain_name: str
) -> Tuple:
    """Get the strain_id, probesetdata_id and inbredset_id for a given strain"""
    strain_id, probesetdata_id, inbredset_id = None, None, None
    with conn.cursor() as cursor:
        cursor.execute("""
SELECT st.id, psd.Id, pf.InbredSetId
FROM ProbeFreeze pf
    JOIN ProbeSetFreeze psf ON psf.ProbeFreezeId = pf.Id
    JOIN ProbeSetXRef psx ON psx.ProbeSetFreezeId = psf.Id
    JOIN ProbeSet ps ON ps.Id = psx.ProbeSetId
    JOIN ProbeSetData psd ON psd.Id = psx.DataId
    JOIN Strain st ON psd.StrainId = st.Id
    LEFT JOIN ProbeSetSE psse ON psse.DataId = psd.Id AND psse.StrainId = psd.StrainId
    LEFT JOIN NStrain ns ON ns.DataId = psd.Id AND ns.StrainId = psd.StrainId
WHERE ps.Id = %s AND psf.Name= %s AND st.Name = %s""", (probeset_id, dataset_name, strain_name))
        if _result := cursor.fetchone():
            strain_id, probesetdata_id, inbredset_id = _result
        if not all([strain_id, probesetdata_id, inbredset_id]):
            # Applies for data to be inserted:
            cursor.execute("""
SELECT psx.DataId, pf.InbredSetId
FROM ProbeFreeze pf
    JOIN ProbeSetFreeze psf ON psf.ProbeFreezeId = pf.Id
    JOIN ProbeSetXRef psx ON psx.ProbeSetFreezeId = psf.Id
WHERE psx.ProbeSetId = %s AND
      psf.Name = %s""", (probeset_id, dataset_name))

            probesetdata_id, inbredset_id = cursor.fetchone()
            cursor.execute(
                "SELECT Id FROM Strain WHERE Name = %s", (strain_name,)
            )
            strain_id = cursor.fetchone()[0]
    return (strain_id, probesetdata_id, inbredset_id)

def get_pheno_sample_data_ids(
    conn: Any, publishxref_id: int, phenotype_id: int, strain_name: str
) -> Tuple:
    """Get the strain_id, publishdata_id and inbredset_id for a given strain"""
    strain_id, publishdata_id, inbredset_id = None, None, None
    with conn.cursor() as cursor:
        cursor.execute(
            "SELECT st.id, pd.Id, pf.InbredSetId "
            "FROM PublishData pd "
            "JOIN Strain st ON pd.StrainId = st.Id "
            "JOIN PublishXRef px ON px.DataId = pd.Id "
            "JOIN PublishFreeze pf ON pf.InbredSetId "
            "= px.InbredSetId WHERE px.Id = %s "
            "AND px.PhenotypeId = %s AND st.Name = %s",
            (publishxref_id, phenotype_id, strain_name),
        )
        if _result := cursor.fetchone():
            strain_id, publishdata_id, inbredset_id = _result
        if not all([strain_id, publishdata_id, inbredset_id]):
            # Applies for data to be inserted:
            cursor.execute(
                "SELECT DataId, InbredSetId FROM PublishXRef "
                "WHERE Id = %s AND PhenotypeId = %s",
                (publishxref_id, phenotype_id),
            )
            publishdata_id, inbredset_id = cursor.fetchone()
            cursor.execute(
                "SELECT Id FROM Strain WHERE Name = %s", (strain_name,)
            )
            strain_id = cursor.fetchone()[0]
    return (strain_id, publishdata_id, inbredset_id)


# pylint: disable=[R0913, R0914]
def update_sample_data(
    conn: Any,
    original_data: str,
    updated_data: str,
    csv_header: str,
    trait_info: dict
) -> int:
    """Given the right parameters, update sample-data from the relevant
    table."""

    def __update_data(conn, table, value):
        if value and value != "x":
            with conn.cursor() as cursor:
                sub_query = " = %s AND ".join(_MAP.get(table)[:2]) + " = %s"
                _val = _MAP.get(table)[-1]
                cursor.execute(
                    (f"UPDATE {table} SET {_val} = %s " f"WHERE {sub_query}"),
                    (value, strain_id, data_id),
                )
                conn.commit()
                return cursor.rowcount
        return 0

    def __update_case_attribute(
        conn, value, strain_id, case_attr, inbredset_id
    ):
        if value != "x":
            (id_, name) = parse_csv_column(case_attr)
            with conn.cursor() as cursor:
                if id_:
                    cursor.execute(
                        "UPDATE CaseAttributeXRefNew "
                        "SET Value = %s "
                        "WHERE StrainId = %s AND CaseAttributeId = %s "
                        "AND InbredSetId = %s",
                        (value, strain_id, id_, inbredset_id),
                    )
                else:
                    cursor.execute(
                        "UPDATE CaseAttributeXRefNew "
                        "SET Value = %s "
                        "WHERE StrainId = %s AND CaseAttributeId = "
                        "(SELECT CaseAttributeId FROM "
                        "CaseAttribute WHERE Name = %s) "
                        "AND InbredSetId = %s",
                        (value, strain_id, name, inbredset_id),
                    )
                conn.commit()
                return cursor.rowcount
        return 0

    if 'probeset_id' in trait_info and 'dataset_name' in trait_info: # If ProbeSet/mRNA Assay trait
        probeset_id, dataset_name = trait_info['probeset_id'], trait_info['dataset_name']
        data_type = "mrna"
    else: # If Publish/phenotype trait
        trait_name, phenotype_id = trait_info['trait_name'], trait_info['phenotype_id']
        data_type = "pheno"

    if data_type == "mrna":
        strain_id, data_id, inbredset_id = get_mrna_sample_data_ids(
            conn=conn,
            probeset_id=int(probeset_id),
            dataset_name=dataset_name,
            strain_name=extract_strain_name(csv_header, original_data),
        )
        none_case_attrs = {
            "Strain Name": lambda x: 0,
            "Value": lambda x: __update_data(conn, "ProbeSetData", x),
            "SE": lambda x: __update_data(conn, "ProbeSetSE", x),
            "Count": lambda x: __update_data(conn, "NStrain", x),
        }
    else:
        strain_id, data_id, inbredset_id = get_pheno_sample_data_ids(
            conn=conn,
            publishxref_id=int(trait_name),
            phenotype_id=phenotype_id,
            strain_name=extract_strain_name(csv_header, original_data),
        )
        none_case_attrs = {
            "Strain Name": lambda x: 0,
            "Value": lambda x: __update_data(conn, "PublishData", x),
            "SE": lambda x: __update_data(conn, "PublishSE", x),
            "Count": lambda x: __update_data(conn, "NStrain", x),
        }

    count = 0
    # try:
    __actions = __extract_actions(
        original_data=original_data,
        updated_data=updated_data,
        csv_header=csv_header,
    )

    if __actions.get("update"):
        _csv_header = __actions["update"]["csv_header"]
        _data = __actions["update"]["data"]
        # pylint: disable=[E1101]
        for header, value in zip(_csv_header.split(","), _data.split(",")):
            header = header.strip()
            value = value.strip()
            if header in none_case_attrs:
                count += none_case_attrs[header](value)
            else:
                count += __update_case_attribute(
                    conn=conn,
                    value=value,
                    strain_id=strain_id,
                    case_attr=header,
                    inbredset_id=inbredset_id,
                )

    if __actions.get("delete"):
        _rowcount = delete_sample_data(
            conn=conn,
            data=__actions["delete"]["data"],
            csv_header=__actions["delete"]["csv_header"],
            trait_info=trait_info
        )
        if _rowcount:
            count += 1
    if __actions.get("insert"):
        _rowcount = insert_sample_data(
            conn=conn,
            data=__actions["insert"]["data"],
            csv_header=__actions["insert"]["csv_header"],
            trait_info=trait_info
        )
        if _rowcount:
            count += 1
    # except Exception as _e:
    #     raise MySQLdb.Error(_e) from _e
    return count

def delete_sample_data(
    conn: Any, data: str, csv_header: str, trait_info: dict
) -> int:
    """Given the right parameters, delete sample-data from the relevant
    tables."""

    def __delete_data(conn, table):
        sub_query = " = %s AND ".join(_MAP.get(table)[:2]) + " = %s"
        with conn.cursor() as cursor:
            cursor.execute(
                (f"DELETE FROM {table} " f"WHERE {sub_query}"),
                (strain_id, data_id),
            )
            conn.commit()
            return cursor.rowcount

    def __delete_case_attribute(conn, strain_id, case_attr, inbredset_id):
        with conn.cursor() as cursor:
            (id_, name) = parse_csv_column(case_attr)
            if id_:
                cursor.execute(
                    "DELETE FROM CaseAttributeXRefNew "
                    "WHERE StrainId = %s AND CaseAttributeId = %s "
                    "AND InbredSetId = %s",
                    (strain_id, id_, inbredset_id),
                )
            else:
                cursor.execute(
                    "DELETE FROM CaseAttributeXRefNew "
                    "WHERE StrainId = %s AND CaseAttributeId = "
                    "(SELECT CaseAttributeId FROM "
                    "CaseAttribute WHERE Name = %s) "
                    "AND InbredSetId = %s",
                    (strain_id, name, inbredset_id),
                )
            conn.commit()
            return cursor.rowcount

    if 'probeset_id' in trait_info and 'dataset_name' in trait_info: # If ProbeSet/mRNA Assay trait
        probeset_id, dataset_name = trait_info['probeset_id'], trait_info['dataset_name']
        data_type = "mrna"
    else: # If Publish/phenotype trait
        trait_name, phenotype_id = trait_info['trait_name'], trait_info['phenotype_id']
        data_type = "pheno"

    if data_type == "mrna":
        strain_id, data_id, inbredset_id = get_mrna_sample_data_ids(
            conn=conn,
            probeset_id=int(probeset_id),
            dataset_name=dataset_name,
            strain_name=extract_strain_name(csv_header, data),
        )
        none_case_attrs: Dict[str, Any] = {
            "Strain Name": lambda: 0,
            "Value": lambda: __delete_data(conn, "ProbeSetData"),
            "SE": lambda: __delete_data(conn, "ProbeSetSE"),
            "Count": lambda: __delete_data(conn, "NStrain"),
        }
    else:
        strain_id, data_id, inbredset_id = get_pheno_sample_data_ids(
            conn=conn,
            publishxref_id=int(trait_name),
            phenotype_id=phenotype_id,
            strain_name=extract_strain_name(csv_header, data),
        )
        none_case_attrs = {
            "Strain Name": lambda: 0,
            "Value": lambda: __delete_data(conn, "PublishData"),
            "SE": lambda: __delete_data(conn, "PublishSE"),
            "Count": lambda: __delete_data(conn, "NStrain"),
        }

    count = 0

    try:
        for header in csv_header.split(","):
            header = header.strip()
            if header in none_case_attrs:
                count += none_case_attrs[header]()
            else:
                count += __delete_case_attribute(
                    conn=conn,
                    strain_id=strain_id,
                    case_attr=header,
                    inbredset_id=inbredset_id,
                )
    except Exception as _e:
        raise MySQLdb.Error(_e) from _e
    return count

# pylint: disable=[R0913, R0914]
def insert_sample_data(
    conn: Any, data: str, csv_header: str, trait_info: dict
) -> int:
    """Given the right parameters, insert sample-data to the relevant table."""

    def __insert_data(conn, table, value):
        if value and value != "x":
            with conn.cursor() as cursor:
                columns = ", ".join(_MAP.get(table))
                cursor.execute(
                    (
                        f"INSERT INTO {table} "
                        f"({columns}) "
                        f"VALUES (%s, %s, %s)"
                    ),
                    (strain_id, data_id, value),
                )
                conn.commit()
                return cursor.rowcount
        return 0

    def __insert_case_attribute(conn, case_attr, value):
        if value != "x":
            with conn.cursor() as cursor:
                (id_, name) = parse_csv_column(case_attr)
                if not id_:
                    cursor.execute(
                        "SELECT Id FROM CaseAttribute WHERE Name = %s",
                        (name,),
                    )
                    if case_attr_id := cursor.fetchone():
                        id_ = case_attr_id[0]

                cursor.execute(
                    "SELECT StrainId FROM "
                    "CaseAttributeXRefNew WHERE StrainId = %s "
                    "AND CaseAttributeId = %s "
                    "AND InbredSetId = %s",
                    (strain_id, id_, inbredset_id),
                )
                if (not cursor.fetchone()) and id_:
                    cursor.execute(
                        "INSERT INTO CaseAttributeXRefNew "
                        "(StrainId, CaseAttributeId, Value, InbredSetId) "
                        "VALUES (%s, %s, %s, %s)",
                        (strain_id, id_, value, inbredset_id),
                    )
                    row_count = cursor.rowcount
                    conn.commit()
                    return row_count
                conn.commit()
        return 0

    if 'probeset_id' in trait_info and 'dataset_name' in trait_info: # If ProbeSet/mRNA Assay trait
        probeset_id, dataset_name = trait_info['probeset_id'], trait_info['dataset_name']
        data_type = "mrna"
    else: # If Publish/phenotype trait
        trait_name, phenotype_id = trait_info['trait_name'], trait_info['phenotype_id']
        data_type = "pheno"

    if data_type == "mrna":
        strain_id, data_id, inbredset_id = get_mrna_sample_data_ids(
            conn=conn,
            probeset_id=int(probeset_id),
            dataset_name=dataset_name,
            strain_name=extract_strain_name(csv_header, data),
        )
        none_case_attrs = {
            "Strain Name": lambda _: 0,
            "Value": lambda x: __insert_data(conn, "ProbeSetData", x),
            "SE": lambda x: __insert_data(conn, "ProbeSetSE", x),
            "Count": lambda x: __insert_data(conn, "NStrain", x),
        }
    else:
        strain_id, data_id, inbredset_id = get_pheno_sample_data_ids(
            conn=conn,
            publishxref_id=int(trait_name),
            phenotype_id=phenotype_id,
            strain_name=extract_strain_name(csv_header, data),
        )
        none_case_attrs = {
            "Strain Name": lambda _: 0,
            "Value": lambda x: __insert_data(conn, "PublishData", x),
            "SE": lambda x: __insert_data(conn, "PublishSE", x),
            "Count": lambda x: __insert_data(conn, "NStrain", x),
        }

    try:
        count = 0

        # Check if the data already exists:
        with conn.cursor() as cursor:
            if data_type == "mrna":
                cursor.execute(
                    "SELECT Id FROM ProbeSetData where Id = %s "
                    "AND StrainId = %s",
                    (data_id, strain_id))
            else:
                cursor.execute(
                    "SELECT Id FROM PublishData where Id = %s "
                    "AND StrainId = %s",
                    (data_id, strain_id))
            data_exists = cursor.fetchone()

        if data_exists:  # Data already exists
            return count

        for header, value in zip(csv_header.split(","), data.split(",")):
            header = header.strip()
            value = value.strip()
            if header in none_case_attrs:
                count += none_case_attrs[header](value)
            else:
                count += __insert_case_attribute(
                    conn=conn, case_attr=header, value=value
                )
        return count
    except Exception as _e:
        raise MySQLdb.Error(_e) from _e