catomatic 0.1.2__tar.gz → 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {catomatic-0.1.2/src/catomatic.egg-info → catomatic-0.1.3}/PKG-INFO +2 -2
- {catomatic-0.1.2 → catomatic-0.1.3}/setup.cfg +4 -2
- catomatic-0.1.2/src/catomatic/CatalogueBuilder.py → catomatic-0.1.3/src/catomatic/BinaryCatalogue.py +51 -355
- {catomatic-0.1.2 → catomatic-0.1.3}/src/catomatic/Ecoff.py +62 -22
- catomatic-0.1.3/src/catomatic/PiezoTools.py +202 -0
- catomatic-0.1.3/src/catomatic/RegressionCatalogue.py +758 -0
- catomatic-0.1.3/src/catomatic/__main__.py +52 -0
- catomatic-0.1.3/src/catomatic/cli.py +390 -0
- catomatic-0.1.2/src/catomatic/defence.py → catomatic-0.1.3/src/catomatic/defence_module.py +154 -9
- {catomatic-0.1.2 → catomatic-0.1.3/src/catomatic.egg-info}/PKG-INFO +2 -2
- {catomatic-0.1.2 → catomatic-0.1.3}/src/catomatic.egg-info/SOURCES.txt +6 -2
- catomatic-0.1.3/src/catomatic.egg-info/entry_points.txt +4 -0
- catomatic-0.1.2/src/catomatic.egg-info/entry_points.txt +0 -2
- {catomatic-0.1.2 → catomatic-0.1.3}/LICENSE +0 -0
- {catomatic-0.1.2 → catomatic-0.1.3}/README.md +0 -0
- {catomatic-0.1.2 → catomatic-0.1.3}/pyproject.toml +0 -0
- {catomatic-0.1.2 → catomatic-0.1.3}/src/catomatic/__init__.py +0 -0
- {catomatic-0.1.2 → catomatic-0.1.3}/src/catomatic.egg-info/dependency_links.txt +0 -0
- {catomatic-0.1.2 → catomatic-0.1.3}/src/catomatic.egg-info/not-zip-safe +0 -0
- {catomatic-0.1.2 → catomatic-0.1.3}/src/catomatic.egg-info/requires.txt +0 -0
- {catomatic-0.1.2 → catomatic-0.1.3}/src/catomatic.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: catomatic
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: A tool for automatically building catalogues of antibiotic resistance-associated variants
|
|
5
5
|
Home-page: https://github.com/fowler-lab/catomatic
|
|
6
6
|
Author: Dylan Adlard, Philip W Fowler
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[metadata]
|
|
2
2
|
name = catomatic
|
|
3
|
-
version = 0.1.
|
|
3
|
+
version = 0.1.3
|
|
4
4
|
author = Dylan Adlard, Philip W Fowler
|
|
5
5
|
author_email = philip.fowler@ndm.ox.ac.uk
|
|
6
6
|
description = A tool for automatically building catalogues of antibiotic resistance-associated variants
|
|
@@ -27,7 +27,9 @@ include_package_data = True
|
|
|
27
27
|
|
|
28
28
|
[options.entry_points]
|
|
29
29
|
console_scripts =
|
|
30
|
-
|
|
30
|
+
BuildBinaryCatalogue = catomatic.BinaryCatalogue:main_binary_builder
|
|
31
|
+
BuildRegressionCatalogue = catomatic.RegressionCatalogue:main_regression_builder
|
|
32
|
+
GenerateEcoff = catomatic.Ecoff:main_ecoff_generator
|
|
31
33
|
|
|
32
34
|
[egg_info]
|
|
33
35
|
tag_build =
|
catomatic-0.1.2/src/catomatic/CatalogueBuilder.py → catomatic-0.1.3/src/catomatic/BinaryCatalogue.py
RENAMED
|
@@ -4,13 +4,16 @@ import piezo
|
|
|
4
4
|
import argparse
|
|
5
5
|
import numpy as np
|
|
6
6
|
import pandas as pd
|
|
7
|
-
from .
|
|
7
|
+
from .PiezoTools import PiezoExporter
|
|
8
|
+
from .defence_module import validate_binary_init, validate_binary_build_inputs
|
|
8
9
|
from scipy.stats import norm, binomtest, fisher_exact
|
|
9
10
|
|
|
10
11
|
|
|
11
|
-
class
|
|
12
|
+
class BinaryBuilder(PiezoExporter):
|
|
12
13
|
"""
|
|
13
|
-
|
|
14
|
+
Builds a mutation catalogue compatible with Piezo in a standardized format.
|
|
15
|
+
|
|
16
|
+
Binary labels underpin a frequentist statistical approach.
|
|
14
17
|
|
|
15
18
|
Instantiation constructs the catalogue object.
|
|
16
19
|
|
|
@@ -29,22 +32,6 @@ class BuildCatalogue:
|
|
|
29
32
|
seed (list) optional): A list of predefined GARC neutral mutations with associated phenotypes
|
|
30
33
|
that are hardcoded prior to running the builder. Defaults to None.
|
|
31
34
|
|
|
32
|
-
test (str, optional): Type of statistical test to run for phenotyping. None (doesn't phenotype)
|
|
33
|
-
vs binomial (against a defined background) vs Fisher (against contingency
|
|
34
|
-
background). Defaults to none.
|
|
35
|
-
|
|
36
|
-
background (float, optional): Background rate between 0-1 for binomial test phenotyping. Deafults to None.
|
|
37
|
-
|
|
38
|
-
p (float, optional): Significance level at which to reject the null hypothesis during statistical testing.
|
|
39
|
-
Defaults to 0.95.
|
|
40
|
-
tails (str, optional): Whether to run a 1-tailed or 2-tailed test. Defaults to 'two'.
|
|
41
|
-
strict_unlock (bool, optional): If strict_unlock is true, statistical significance in the direction of
|
|
42
|
-
susceptiblity will be required for S classifications. If false, homogenous
|
|
43
|
-
susceptiblity is sufficient for S classifcations. Defaults to False
|
|
44
|
-
record_ids (bool, optional): If true, will track identifiers to which the mutations belong and were extracted
|
|
45
|
-
from - helpful for detailed interrogation, but gives long evidence objects.
|
|
46
|
-
Defaults to False
|
|
47
|
-
|
|
48
35
|
"""
|
|
49
36
|
|
|
50
37
|
def __init__(
|
|
@@ -53,48 +40,76 @@ class BuildCatalogue:
|
|
|
53
40
|
mutations,
|
|
54
41
|
FRS=None,
|
|
55
42
|
seed=None,
|
|
56
|
-
test=None,
|
|
57
|
-
background=None,
|
|
58
|
-
p=0.95,
|
|
59
|
-
tails="two",
|
|
60
|
-
strict_unlock=False,
|
|
61
|
-
record_ids=False,
|
|
62
43
|
):
|
|
63
44
|
samples = pd.read_csv(samples) if isinstance(samples, str) else samples
|
|
64
45
|
mutations = pd.read_csv(mutations) if isinstance(mutations, str) else mutations
|
|
65
46
|
|
|
66
47
|
# Run the validation function
|
|
67
|
-
|
|
68
|
-
samples, mutations, seed, FRS, test, background, p, tails, record_ids
|
|
69
|
-
)
|
|
48
|
+
validate_binary_init(samples, mutations, seed, FRS)
|
|
70
49
|
|
|
71
50
|
if FRS:
|
|
72
51
|
# Apply fraction read support thresholds to mutations to filter out irrelevant variants
|
|
73
52
|
mutations = mutations[(mutations.FRS >= FRS)]
|
|
74
53
|
|
|
54
|
+
self.samples = samples
|
|
55
|
+
self.mutations = mutations
|
|
56
|
+
|
|
75
57
|
# Instantiate attributes
|
|
76
58
|
self.catalogue = {}
|
|
77
59
|
self.entry = []
|
|
78
|
-
self.record_ids = record_ids
|
|
79
60
|
self.temp_ids = []
|
|
61
|
+
self.run_iter = True
|
|
62
|
+
self.seed = seed
|
|
63
|
+
|
|
64
|
+
def build(
|
|
65
|
+
self,
|
|
66
|
+
test=None,
|
|
67
|
+
background=None,
|
|
68
|
+
p=0.95,
|
|
69
|
+
tails="two",
|
|
70
|
+
strict_unlock=False,
|
|
71
|
+
record_ids=False,
|
|
72
|
+
):
|
|
73
|
+
"""
|
|
74
|
+
Args:
|
|
75
|
+
test (str, optional): Type of statistical test to run for phenotyping. None (doesn't phenotype)
|
|
76
|
+
vs binomial (against a defined background) vs Fisher (against contingency
|
|
77
|
+
background). Defaults to none.
|
|
78
|
+
|
|
79
|
+
background (float, optional): Background rate between 0-1 for binomial test phenotyping. Deafults to None.
|
|
80
|
+
|
|
81
|
+
p (float, optional): Significance level at which to reject the null hypothesis during statistical testing.
|
|
82
|
+
Defaults to 0.95.
|
|
83
|
+
tails (str, optional): Whether to run a 1-tailed or 2-tailed test. Defaults to 'two'.
|
|
84
|
+
strict_unlock (bool, optional): If strict_unlock is true, statistical significance in the direction of
|
|
85
|
+
susceptiblity will be required for S classifications. If false, homogenous
|
|
86
|
+
susceptiblity is sufficient for S classifcations. Defaults to False
|
|
87
|
+
record_ids (bool, optional): If true, will track identifiers to which the mutations belong and were extracted
|
|
88
|
+
from - helpful for detailed interrogation, but gives long evidence objects.
|
|
89
|
+
Defaults to False"""
|
|
90
|
+
|
|
91
|
+
validate_binary_build_inputs(test, background, p, tails, record_ids)
|
|
92
|
+
|
|
80
93
|
self.test = test
|
|
81
94
|
self.background = background
|
|
82
|
-
self.p = 1 - p
|
|
83
95
|
self.strict_unlock = strict_unlock
|
|
96
|
+
self.p = 1 - p
|
|
84
97
|
self.tails = tails
|
|
85
|
-
self.
|
|
86
|
-
|
|
87
|
-
if seed is not None:
|
|
98
|
+
self.record_ids = record_ids
|
|
99
|
+
|
|
100
|
+
if self.seed is not None:
|
|
88
101
|
# If there are seeded variants, hardcode them now
|
|
89
|
-
for i in seed:
|
|
102
|
+
for i in self.seed:
|
|
90
103
|
self.add_mutation(i, "S", {"seeded": "True"})
|
|
91
104
|
|
|
92
105
|
while self.run_iter:
|
|
93
106
|
# While there are susceptible solos, classify and remove them
|
|
94
|
-
self.classify(samples, mutations)
|
|
107
|
+
self.classify(self.samples, self.mutations)
|
|
95
108
|
|
|
96
109
|
# If no more susceptible solos, classify all R and U solos in one, final sweep
|
|
97
|
-
self.classify(samples, mutations)
|
|
110
|
+
self.classify(self.samples, self.mutations)
|
|
111
|
+
|
|
112
|
+
return self
|
|
98
113
|
|
|
99
114
|
def classify(self, samples, mutations):
|
|
100
115
|
"""
|
|
@@ -494,322 +509,3 @@ class BuildCatalogue:
|
|
|
494
509
|
with open(outfile, "w") as f:
|
|
495
510
|
json.dump(self.catalogue, f, indent=4)
|
|
496
511
|
|
|
497
|
-
def to_piezo(
|
|
498
|
-
self,
|
|
499
|
-
genbank_ref,
|
|
500
|
-
catalogue_name,
|
|
501
|
-
version,
|
|
502
|
-
drug,
|
|
503
|
-
wildcards,
|
|
504
|
-
outfile,
|
|
505
|
-
grammar="GARC1",
|
|
506
|
-
values="RUS",
|
|
507
|
-
public=True,
|
|
508
|
-
for_piezo=True,
|
|
509
|
-
json_dumps=True,
|
|
510
|
-
include_U=True,
|
|
511
|
-
):
|
|
512
|
-
"""
|
|
513
|
-
Exports a pizeo-compatible dataframe as a csv file.
|
|
514
|
-
|
|
515
|
-
Parameters:
|
|
516
|
-
genbank_ref (str): GenBank reference identifier.
|
|
517
|
-
catalogue_name (str): Name of the catalogue.
|
|
518
|
-
version (str): Version of the catalogue.
|
|
519
|
-
drug (str): Target drug associated with the mutations.
|
|
520
|
-
wildcards (dict): Piezo wildcard (default rules) mutations with phenotypes.
|
|
521
|
-
outfile: The path to the output csv file where the catalogue will be saved.
|
|
522
|
-
grammar (str, optional): Grammar used in the catalogue, default "GARC1" (no other grammar currently supported).
|
|
523
|
-
values (str, optional): Prediction values, default "RUS" representing each phenotype (no other values currently supported).
|
|
524
|
-
public (bool, optional): private or public call
|
|
525
|
-
for_piezo (bool, optional): Whether to include the missing phenotype placeholders (only piezo requires them)
|
|
526
|
-
|
|
527
|
-
"""
|
|
528
|
-
|
|
529
|
-
piezo_df = self.build_piezo(
|
|
530
|
-
genbank_ref,
|
|
531
|
-
catalogue_name,
|
|
532
|
-
version,
|
|
533
|
-
drug,
|
|
534
|
-
wildcards,
|
|
535
|
-
grammar,
|
|
536
|
-
values,
|
|
537
|
-
public,
|
|
538
|
-
for_piezo,
|
|
539
|
-
json_dumps,
|
|
540
|
-
include_U,
|
|
541
|
-
)
|
|
542
|
-
piezo_df.to_csv(outfile)
|
|
543
|
-
|
|
544
|
-
def build_piezo(
|
|
545
|
-
self,
|
|
546
|
-
genbank_ref,
|
|
547
|
-
catalogue_name,
|
|
548
|
-
version,
|
|
549
|
-
drug,
|
|
550
|
-
wildcards,
|
|
551
|
-
grammar="GARC1",
|
|
552
|
-
values="RUS",
|
|
553
|
-
public=True,
|
|
554
|
-
for_piezo=True,
|
|
555
|
-
json_dumps=False,
|
|
556
|
-
include_U=True,
|
|
557
|
-
):
|
|
558
|
-
"""
|
|
559
|
-
Builds a piezo-format catalogue df from the catalogue object.
|
|
560
|
-
|
|
561
|
-
Parameters:
|
|
562
|
-
genbank_ref (str): GenBank reference identifier.
|
|
563
|
-
catalogue_name (str): Name of the catalogue.
|
|
564
|
-
version (str): Version of the catalogue.
|
|
565
|
-
drug (str): Target drug associated with the mutations.
|
|
566
|
-
wildcards (dict or path): Piezo wildcard (default rules) mutations with phenotypes.
|
|
567
|
-
grammar (str, optional): Grammar used in the catalogue, default "GARC1" (no other grammar currently supported).
|
|
568
|
-
values (str, optional): Prediction values, default "RUS" representing each phenotype (no other values currently supported).
|
|
569
|
-
public (bool, optional): private or public call
|
|
570
|
-
for_piezo (bool, optional): Whether to include the missing phenotype placeholders (only piezo requires them)
|
|
571
|
-
json_dumps (bool, optional): Whether to dump evidence column into json object for piezo (e.g if in notebook, unnecessary)
|
|
572
|
-
include_U (bool, optional): Whether to add unclassified mutations to catalogue
|
|
573
|
-
|
|
574
|
-
Returns:
|
|
575
|
-
self: instance with piezo_catalogue set
|
|
576
|
-
"""
|
|
577
|
-
|
|
578
|
-
validate_build_piezo_inputs(
|
|
579
|
-
genbank_ref, catalogue_name, version, drug,
|
|
580
|
-
wildcards, grammar, values, public, for_piezo, json_dumps, include_U
|
|
581
|
-
)
|
|
582
|
-
|
|
583
|
-
# if user-called
|
|
584
|
-
if public:
|
|
585
|
-
# add piezo wildcards to the catalogue
|
|
586
|
-
if isinstance(wildcards, str):
|
|
587
|
-
# if a path is supplied, read from the file
|
|
588
|
-
with open(wildcards) as f:
|
|
589
|
-
wildcards = json.load(f)
|
|
590
|
-
[self.add_mutation(k, v["pred"], v["evid"]) for k, v in wildcards.items()]
|
|
591
|
-
# inlcude a placeholder for each phenotype if don't exist - piezo requires all R, U, S to parse
|
|
592
|
-
if for_piezo:
|
|
593
|
-
if not any(v["pred"] == "R" for v in self.catalogue.values()):
|
|
594
|
-
self.add_mutation("placeholder@R1R", "R", {})
|
|
595
|
-
if not any(v["pred"] == "S" for v in self.catalogue.values()):
|
|
596
|
-
self.add_mutation("placeholder@S1S", "S", {})
|
|
597
|
-
if (
|
|
598
|
-
not any(v["pred"] == "U" for v in self.catalogue.values())
|
|
599
|
-
or not include_U
|
|
600
|
-
):
|
|
601
|
-
self.add_mutation("placeholder@U1U", "U", {})
|
|
602
|
-
data = self.catalogue
|
|
603
|
-
if include_U == False:
|
|
604
|
-
data = {
|
|
605
|
-
k: v
|
|
606
|
-
for k, v in data.items()
|
|
607
|
-
if (v["pred"] != "U")
|
|
608
|
-
or (k == "placeholder@U1U")
|
|
609
|
-
or ("*" in k)
|
|
610
|
-
or ("del_0.0" in k)
|
|
611
|
-
}
|
|
612
|
-
else:
|
|
613
|
-
# if internal:
|
|
614
|
-
data = wildcards
|
|
615
|
-
|
|
616
|
-
columns = [
|
|
617
|
-
"GENBANK_REFERENCE",
|
|
618
|
-
"CATALOGUE_NAME",
|
|
619
|
-
"CATALOGUE_VERSION",
|
|
620
|
-
"CATALOGUE_GRAMMAR",
|
|
621
|
-
"PREDICTION_VALUES",
|
|
622
|
-
"DRUG",
|
|
623
|
-
"MUTATION",
|
|
624
|
-
"PREDICTION",
|
|
625
|
-
"SOURCE",
|
|
626
|
-
"EVIDENCE",
|
|
627
|
-
"OTHER",
|
|
628
|
-
]
|
|
629
|
-
# construct the catalogue dataframe in piezo-standardised format
|
|
630
|
-
piezo_catalogue = (
|
|
631
|
-
pd.DataFrame.from_dict(data, orient="index")
|
|
632
|
-
.reset_index()
|
|
633
|
-
.rename(
|
|
634
|
-
columns={
|
|
635
|
-
"index": "MUTATION",
|
|
636
|
-
"pred": "PREDICTION",
|
|
637
|
-
"evid": "EVIDENCE",
|
|
638
|
-
}
|
|
639
|
-
)
|
|
640
|
-
.assign(
|
|
641
|
-
GENBANK_REFERENCE=genbank_ref,
|
|
642
|
-
CATALOGUE_NAME=catalogue_name,
|
|
643
|
-
CATALOGUE_VERSION=version,
|
|
644
|
-
CATALOGUE_GRAMMAR=grammar,
|
|
645
|
-
PREDICTION_VALUES=values,
|
|
646
|
-
DRUG=drug,
|
|
647
|
-
SOURCE=json.dumps({}) if json_dumps else {},
|
|
648
|
-
EVIDENCE=lambda df: df["EVIDENCE"].apply(
|
|
649
|
-
json.dumps if json_dumps else lambda x: x
|
|
650
|
-
),
|
|
651
|
-
OTHER=json.dumps({}) if json_dumps else {},
|
|
652
|
-
)[columns]
|
|
653
|
-
)
|
|
654
|
-
|
|
655
|
-
if public:
|
|
656
|
-
# Create a temporary column for the order in self.entry
|
|
657
|
-
piezo_catalogue["order"] = piezo_catalogue["MUTATION"].apply(
|
|
658
|
-
lambda x: self.entry.index(x)
|
|
659
|
-
)
|
|
660
|
-
|
|
661
|
-
# Sort by PREDICTION and the temporary order column
|
|
662
|
-
piezo_catalogue["PREDICTION"] = pd.Categorical(
|
|
663
|
-
piezo_catalogue["PREDICTION"], categories=["S", "R", "U"], ordered=True
|
|
664
|
-
)
|
|
665
|
-
piezo_catalogue = piezo_catalogue.sort_values(by=["PREDICTION", "order"])
|
|
666
|
-
|
|
667
|
-
# Drop the temporary order column
|
|
668
|
-
piezo_catalogue = piezo_catalogue.drop(columns=["order"])
|
|
669
|
-
piezo_catalogue = piezo_catalogue[columns]
|
|
670
|
-
|
|
671
|
-
return piezo_catalogue
|
|
672
|
-
|
|
673
|
-
@staticmethod
|
|
674
|
-
def parse_opt():
|
|
675
|
-
parser = argparse.ArgumentParser(
|
|
676
|
-
description="Build a catalogue and optionally export to Piezo format."
|
|
677
|
-
)
|
|
678
|
-
parser.add_argument(
|
|
679
|
-
"--samples", required=True, type=str, help="Path to the samples file."
|
|
680
|
-
)
|
|
681
|
-
parser.add_argument(
|
|
682
|
-
"--mutations", required=True, type=str, help="Path to the mutations file."
|
|
683
|
-
)
|
|
684
|
-
parser.add_argument(
|
|
685
|
-
"--FRS",
|
|
686
|
-
type=float,
|
|
687
|
-
default=None,
|
|
688
|
-
help="Optional: Fraction Read Support threshold.",
|
|
689
|
-
)
|
|
690
|
-
parser.add_argument(
|
|
691
|
-
"--seed", nargs="+", help="Optional: List of seed mutations."
|
|
692
|
-
)
|
|
693
|
-
parser.add_argument(
|
|
694
|
-
"--test",
|
|
695
|
-
type=str,
|
|
696
|
-
choices=[None, "Binomial", "Fisher"],
|
|
697
|
-
default=None,
|
|
698
|
-
help="Optional: Type of statistical test to run.",
|
|
699
|
-
)
|
|
700
|
-
parser.add_argument(
|
|
701
|
-
"--background",
|
|
702
|
-
type=float,
|
|
703
|
-
default=None,
|
|
704
|
-
help="Optional: Background mutation rate for the binomial test.",
|
|
705
|
-
)
|
|
706
|
-
parser.add_argument(
|
|
707
|
-
"--p",
|
|
708
|
-
type=float,
|
|
709
|
-
default=0.95,
|
|
710
|
-
help="Significance level for statistical testing.",
|
|
711
|
-
)
|
|
712
|
-
parser.add_argument(
|
|
713
|
-
"--strict_unlock",
|
|
714
|
-
action="store_true",
|
|
715
|
-
help="Enforce strict unlocking for classifications.",
|
|
716
|
-
)
|
|
717
|
-
parser.add_argument(
|
|
718
|
-
"--record_ids",
|
|
719
|
-
action="store_true",
|
|
720
|
-
help="Whether to record UNIQUEIDS in the catalogue"
|
|
721
|
-
)
|
|
722
|
-
parser.add_argument(
|
|
723
|
-
"--to_json",
|
|
724
|
-
action="store_true",
|
|
725
|
-
help="Flag to trigger exporting the catalogue to JSON format.",
|
|
726
|
-
)
|
|
727
|
-
parser.add_argument(
|
|
728
|
-
"--outfile",
|
|
729
|
-
type=str,
|
|
730
|
-
help="Path to output file for exporting the catalogue. Used with --to_json or --to_piezo.",
|
|
731
|
-
)
|
|
732
|
-
parser.add_argument(
|
|
733
|
-
"--to_piezo",
|
|
734
|
-
action="store_true",
|
|
735
|
-
help="Flag to export catalogue to Piezo format.",
|
|
736
|
-
)
|
|
737
|
-
parser.add_argument(
|
|
738
|
-
"--genbank_ref", type=str, help="GenBank reference for the catalogue."
|
|
739
|
-
)
|
|
740
|
-
parser.add_argument("--catalogue_name", type=str, help="Name of the catalogue.")
|
|
741
|
-
parser.add_argument("--version", type=str, help="Version of the catalogue.")
|
|
742
|
-
parser.add_argument(
|
|
743
|
-
"--drug", type=str, help="Drug associated with the mutations."
|
|
744
|
-
)
|
|
745
|
-
parser.add_argument(
|
|
746
|
-
"--wildcards", type=str, help="JSON file with wildcard rules."
|
|
747
|
-
)
|
|
748
|
-
parser.add_argument(
|
|
749
|
-
"--grammar",
|
|
750
|
-
type=str,
|
|
751
|
-
default="GARC1",
|
|
752
|
-
help="Grammar used in the catalogue.",
|
|
753
|
-
)
|
|
754
|
-
parser.add_argument(
|
|
755
|
-
"--values",
|
|
756
|
-
type=str,
|
|
757
|
-
default="RUS",
|
|
758
|
-
help="Values used for predictions in the catalogue.",
|
|
759
|
-
)
|
|
760
|
-
parser.add_argument(
|
|
761
|
-
"--for_piezo",
|
|
762
|
-
action="store_true",
|
|
763
|
-
help="If not planning to use piezo, set to False to avoid placeholder rows being added",
|
|
764
|
-
)
|
|
765
|
-
return parser.parse_args()
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
def main():
|
|
769
|
-
args = BuildCatalogue.parse_opt()
|
|
770
|
-
catalogue = BuildCatalogue(
|
|
771
|
-
samples=args.samples,
|
|
772
|
-
mutations=args.mutations,
|
|
773
|
-
FRS=args.FRS,
|
|
774
|
-
seed=args.seed,
|
|
775
|
-
test=args.test,
|
|
776
|
-
background=args.background,
|
|
777
|
-
p=args.p,
|
|
778
|
-
strict_unlock=args.strict_unlock,
|
|
779
|
-
record_ids=args.record_ids
|
|
780
|
-
)
|
|
781
|
-
|
|
782
|
-
if args.to_json:
|
|
783
|
-
if not args.outfile:
|
|
784
|
-
print("Please specify an output file with --outfile when using --to_json")
|
|
785
|
-
exit(1)
|
|
786
|
-
catalogue.to_json(args.outfile)
|
|
787
|
-
|
|
788
|
-
if args.to_piezo:
|
|
789
|
-
if not all(
|
|
790
|
-
[
|
|
791
|
-
args.genbank_ref,
|
|
792
|
-
args.catalogue_name,
|
|
793
|
-
args.version,
|
|
794
|
-
args.drug,
|
|
795
|
-
args.wildcards,
|
|
796
|
-
args.outfile,
|
|
797
|
-
]
|
|
798
|
-
):
|
|
799
|
-
print("Missing required arguments for exporting to Piezo format.")
|
|
800
|
-
exit(1)
|
|
801
|
-
catalogue.to_piezo(
|
|
802
|
-
genbank_ref=args.genbank_ref,
|
|
803
|
-
catalogue_name=args.catalogue_name,
|
|
804
|
-
version=args.version,
|
|
805
|
-
drug=args.drug,
|
|
806
|
-
wildcards=args.wildcards,
|
|
807
|
-
outfile=args.outfile,
|
|
808
|
-
grammar=args.grammar,
|
|
809
|
-
values=args.values,
|
|
810
|
-
for_piezo=args.for_piezo,
|
|
811
|
-
)
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
if __name__ == "__main__":
|
|
815
|
-
main()
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import numpy as np
|
|
2
2
|
import pandas as pd
|
|
3
|
+
from scipy.stats import norm
|
|
3
4
|
from intreg.intreg import IntReg
|
|
4
|
-
from .
|
|
5
|
+
from .defence_module import validate_ecoff_inputs
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
class GenerateEcoff:
|
|
8
|
+
class EcoffGenerator:
|
|
9
9
|
"""
|
|
10
10
|
Generate ECOFF values for wild-type samples using interval regression.
|
|
11
11
|
"""
|
|
12
12
|
|
|
13
|
-
def __init__(
|
|
13
|
+
def __init__(
|
|
14
|
+
self, samples, mutations, dilution_factor=2, censored=True, tail_dilutions=1
|
|
15
|
+
):
|
|
14
16
|
"""
|
|
15
17
|
Initialize the ECOFF generator with sample and mutation data.
|
|
16
18
|
|
|
@@ -21,18 +23,26 @@ class GenerateEcoff:
|
|
|
21
23
|
censored (bool): Flag to indicate if censored data is used.
|
|
22
24
|
tail_dilutions (int): Number of dilutions to extend for interval tails if uncensored.
|
|
23
25
|
"""
|
|
26
|
+
|
|
27
|
+
samples = pd.read_csv(samples) if isinstance(samples, str) else samples
|
|
28
|
+
mutations = pd.read_csv(mutations) if isinstance(mutations, str) else mutations
|
|
29
|
+
|
|
24
30
|
# Run input validation
|
|
25
|
-
validate_ecoff_inputs(
|
|
31
|
+
validate_ecoff_inputs(
|
|
32
|
+
samples, mutations, dilution_factor, censored, tail_dilutions
|
|
33
|
+
)
|
|
26
34
|
|
|
27
35
|
# Merge data and flag mutants
|
|
28
36
|
self.df = pd.merge(samples, mutations, how="left", on=["UNIQUEID"])
|
|
29
37
|
self.flag_mutants()
|
|
38
|
+
self.filter_mutants()
|
|
30
39
|
|
|
31
40
|
# Set parameters
|
|
32
41
|
self.dilution_factor = dilution_factor
|
|
33
42
|
self.censored = censored
|
|
34
43
|
self.tail_dilutions = tail_dilutions
|
|
35
44
|
|
|
45
|
+
|
|
36
46
|
def flag_mutants(self):
|
|
37
47
|
"""
|
|
38
48
|
Identify and flag mutant samples based on the presence of mutations.
|
|
@@ -50,22 +60,28 @@ class GenerateEcoff:
|
|
|
50
60
|
# Mark as mutant if not in wild-type or synonymous sets
|
|
51
61
|
self.df["MUTANT"] = ~self.df["UNIQUEID"].isin(synonymous_ids | wt_ids)
|
|
52
62
|
|
|
53
|
-
def define_intervals(self, df):
|
|
63
|
+
def define_intervals(self, df=None):
|
|
54
64
|
"""
|
|
55
65
|
Define MIC intervals based on the dilution factor and censoring settings.
|
|
56
66
|
|
|
57
67
|
Args:
|
|
58
|
-
df (DataFrame): DataFrame containing MIC data.
|
|
68
|
+
df (DataFrame): DataFrame containing MIC data. If public access, can optionally supply a df to override the wt.
|
|
59
69
|
|
|
60
70
|
Returns:
|
|
61
71
|
tuple: Log-transformed lower and upper bounds for MIC intervals.
|
|
62
72
|
"""
|
|
73
|
+
|
|
74
|
+
if df is None:
|
|
75
|
+
df = self.wt_df
|
|
76
|
+
|
|
77
|
+
df.drop_duplicates(['UNIQUEID'], inplace=True, keep='first')
|
|
78
|
+
|
|
63
79
|
y_low = np.zeros(len(df.MIC))
|
|
64
80
|
y_high = np.zeros(len(df.MIC))
|
|
65
81
|
|
|
66
82
|
# Calculate tail dilution factor if not censored
|
|
67
83
|
if not self.censored:
|
|
68
|
-
tail_dilution_factor = self.dilution_factor
|
|
84
|
+
tail_dilution_factor = self.dilution_factor**self.tail_dilutions
|
|
69
85
|
|
|
70
86
|
# Process each MIC value and define intervals
|
|
71
87
|
for i, mic in enumerate(df.MIC):
|
|
@@ -76,7 +92,9 @@ class GenerateEcoff:
|
|
|
76
92
|
elif mic.startswith(">"): # Right-censored
|
|
77
93
|
upper_bound = float(mic[1:])
|
|
78
94
|
y_low[i] = upper_bound
|
|
79
|
-
y_high[i] =
|
|
95
|
+
y_high[i] = (
|
|
96
|
+
np.inf if self.censored else upper_bound * tail_dilution_factor
|
|
97
|
+
)
|
|
80
98
|
else: # Exact MIC value
|
|
81
99
|
mic_value = float(mic)
|
|
82
100
|
y_low[i] = mic_value / self.dilution_factor
|
|
@@ -110,29 +128,51 @@ class GenerateEcoff:
|
|
|
110
128
|
Returns:
|
|
111
129
|
OptimizeResult: The result of the optimization containing fitted parameters.
|
|
112
130
|
"""
|
|
113
|
-
# Filter out mutant samples
|
|
114
|
-
self.wt_df = self.df[self.df.MUTANT == False]
|
|
115
131
|
# Define and log-transform intervals
|
|
116
|
-
y_low, y_high = self.define_intervals(
|
|
132
|
+
y_low, y_high = self.define_intervals()
|
|
117
133
|
# Fit the model with log-transformed data
|
|
118
134
|
return IntReg(y_low, y_high).fit(method="L-BFGS-B", initial_params=None)
|
|
135
|
+
|
|
136
|
+
def filter_mutants(self, mutant=False):
|
|
137
|
+
"""
|
|
138
|
+
Filters for wt or mutant samples. Defaults to wt (which is the assumed arg for the class)
|
|
139
|
+
Allows one to switch to explicilty fitting mutants for testing and devs.
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
mutant (bool): whether to filter for mutants or wt. Default to False
|
|
143
|
+
"""
|
|
119
144
|
|
|
120
|
-
|
|
145
|
+
if mutant:
|
|
146
|
+
#filter for mutants (for testing and devs)
|
|
147
|
+
self.wt_df = self.df[self.df.MUTANT]
|
|
148
|
+
else:
|
|
149
|
+
self.wt_df = self.df[~self.df.MUTANT]
|
|
150
|
+
|
|
151
|
+
def generate(self, percentile=99, run_mutants=False):
|
|
121
152
|
"""
|
|
122
|
-
Calculate the ECOFF value based on the fitted model.
|
|
153
|
+
Calculate the ECOFF value based on the fitted model and a specified percentile.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
percentile (float): The desired percentile (e.g., 99 for 99th percentile).
|
|
123
157
|
|
|
124
158
|
Returns:
|
|
125
|
-
tuple: ECOFF in the original scale, the
|
|
159
|
+
tuple: ECOFF in the original scale, the specified percentile in the log-transformed scale,
|
|
126
160
|
mean (mu), standard deviation (sigma), and the model result.
|
|
127
161
|
"""
|
|
128
|
-
|
|
162
|
+
|
|
163
|
+
assert (
|
|
164
|
+
percentile > 0 and percentile < 100
|
|
165
|
+
), "percentile must be a float or integer between 0 and 100"
|
|
166
|
+
|
|
129
167
|
model = self.fit()
|
|
130
168
|
# Extract model parameters
|
|
131
169
|
mu, log_sigma = model.x
|
|
132
170
|
sigma = np.exp(log_sigma)
|
|
133
|
-
#
|
|
134
|
-
|
|
135
|
-
#
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
171
|
+
# Calulcate z-score for the given percentile
|
|
172
|
+
z_score = norm.ppf(percentile / 100)
|
|
173
|
+
# Calculate the percentile in log scale
|
|
174
|
+
z_percentile = mu + z_score * sigma
|
|
175
|
+
# Convert the percentile back to the original MIC scale
|
|
176
|
+
ecoff = self.dilution_factor**z_percentile
|
|
177
|
+
|
|
178
|
+
return ecoff, z_percentile, mu, sigma, model
|