Skip to content

Commit

Permalink
Merge branch 'model-msg' into 'dev'
Browse files Browse the repository at this point in the history
Tweak some model choosing exception messages

See merge request research/medaka!595
  • Loading branch information
cjw85 committed Oct 9, 2024
2 parents 60f96d7 + d90e821 commit 423481c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
15 changes: 8 additions & 7 deletions medaka/medaka.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __call__(self, parser, namespace, values, option_string=None):
try:
model_fp = medaka.models.resolve_model(val)
except Exception as e:
msg = "Error validating model from '--{}' argument: {}."
msg = "Error validating model from '--{}' argument: {}"
raise RuntimeError(msg.format(self.dest, str(e)))
setattr(namespace, f"{self.dest}_was_given", True)
setattr(namespace, self.dest, model_fp)
Expand All @@ -44,8 +44,9 @@ class AutoModel(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
variant, input_file = values
if variant not in {'consensus', 'variant', 'consensus_bacteria'}:
raise ValueError("'TYPE' must be one of 'consensus', 'variant',"
"or 'consensus_bacteria'.")
raise ValueError(
"'TYPE' must be one of 'consensus', 'variant',"
"or 'consensus_bacteria'.")
bacteria = 'bacteria' in variant
variant = variant == 'variant'
model = medaka.models.model_from_basecaller(
Expand Down Expand Up @@ -197,11 +198,11 @@ def _model_arg():
formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)
grp = parser.add_mutually_exclusive_group()
grp.add_argument('--model', action=ResolveModel,
default=medaka.options.default_models['consensus'],
help="Model to use. Can be a medaka model name or a basecaller model name suffixed with ':consensus' or ':variant'. For example 'dna_r10.4.1_e8.2_400bps_hac@v4.1.0:variant'.")
default=medaka.options.default_models['consensus'],
help="Model to use. Can be a medaka model name or a basecaller model name suffixed with ':consensus' or ':variant'. For example 'dna_r10.4.1_e8.2_400bps_hac@v4.1.0:variant'.")
grp.add_argument('--auto_model', nargs=2, action=AutoModel,
metavar=("TYPE", "INPUT"), dest='model',
help="Automatically choose model according to INPUT. TYPE should be one of 'consensus' or 'variant'.")
metavar=("TYPE", "INPUT"), dest='model',
help="Automatically choose model according to INPUT. TYPE should be one of 'consensus' or 'variant'.")
return parser


Expand Down
26 changes: 23 additions & 3 deletions medaka/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,22 @@ def resolve_model(model):
raise ex
except Exception:
logger.warning(err_msg)

extra_msg = ""
if "fast" in model:
extra_msg = (
" 'fast' basecalling models are not supported by medaka, "
"if you have used a 'fast' model to perform basecalling you "
"will need to first perform basecalling with a "
"'high accuracy' or 'super accuracy' model before using "
"medaka.")
raise ValueError(
f"Model {model} is not a known model or existant file.")
f"The model '{model}' is not a recognised basecaller model or "
"existant file. This could indicate a malformed input file (for "
"which medaka was unable to identify correctly the basecaller "
"meta-information) or simply be that the model is not supported "
f"by medaka.{extra_msg}")

else:
# check for model in model stores
for suffix in model_suffixes:
Expand Down Expand Up @@ -168,7 +182,7 @@ def model_from_basecaller(fname, variant=False, bacteria=False):
# replace with bacterial consensus model if needed
if bacteria and not variant:
if model in medaka.options.bact_methyl_compatible_models:
model = "r1041_e82_400bps_bacterial_methylation"
model = medaka.options.bact_methyl_model
else:
logger.warning(
"WARNING: --bacteria specified but input data was not "
Expand Down Expand Up @@ -210,7 +224,13 @@ def _model_from_fastq(fname):
"basecall_model_version_id=")[1].split()[0]
models.add(model)
except Exception:
pass
try:
# rogue non-spec conforming minknow versions
model = rec.comment.split(
"model_version_id=")[1].split()[0]
models.add(model)
except Exception:
pass
if len(models) > 1:
# filter out any models without an `@`. These are likely FPs of
# the search above (there are unversioned models whose name
Expand Down
1 change: 1 addition & 0 deletions medaka/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
'r1041_e82_400bps_bacterial_methylation',
]

bact_methyl_model = 'r1041_e82_400bps_bacterial_methylation'
bact_methyl_compatible_models = [
'r1041_e82_400bps_hac_v4.2.0', 'r1041_e82_400bps_sup_v4.2.0',
'r1041_e82_400bps_hac_v4.3.0', 'r1041_e82_400bps_sup_v4.3.0',
Expand Down

0 comments on commit 423481c

Please sign in to comment.