@@ -1237,3 +1237,89 @@ def supplement_replay(
12371237 verbose = verbose ,
12381238 dump_recorded_metadata = dump_recorded_metadata
12391239 )
1240+
1241+
1242+ @tools .command (
1243+ name = 'annotation-create' ,
1244+ short_help = 'Add a new Annotation to a QIIME 2 Result.' ,
1245+ help = 'Attach an Annotation to an existing Artifact or Visualization.'
1246+ ' Supported for QIIME 2 archives of version 7.0+.' ,
1247+ cls = ToolCommand
1248+ )
1249+ @click .option (
1250+ '--input-path' ,
1251+ required = True ,
1252+ metavar = _COMBO_METAVAR ,
1253+ type = click .Path (exists = True , file_okay = True ,
1254+ dir_okay = False , readable = True ),
1255+ help = 'Path to the `.qza` or `.qzv` you want to annotate.'
1256+ )
1257+ @click .option (
1258+ '--annotation-type' ,
1259+ required = True ,
1260+ type = click .Choice (['Note' ], case_sensitive = True ),
1261+ help = 'Annotation type to create.'
1262+ )
1263+ @click .option (
1264+ '--name' ,
1265+ required = True ,
1266+ help = 'Name for your Annotation (must be unique for a given Result).'
1267+ )
1268+ @click .option (
1269+ '--text' ,
1270+ required = False ,
1271+ help = 'Inline text for the Annotation.'
1272+ ' Mutually exclusive with `--file`.'
1273+ )
1274+ @click .option (
1275+ '--file' ,
1276+ 'filepath' ,
1277+ required = False ,
1278+ type = click .Path (exists = True , file_okay = True ,
1279+ dir_okay = False , readable = True ),
1280+ help = 'Path to a text file whose contents will be added'
1281+ ' to your Annotation. Mutually exclusive with `--text`.'
1282+ )
1283+ @click .option (
1284+ '--output-path' ,
1285+ required = False ,
1286+ type = click .Path (file_okay = True , dir_okay = False , writable = True ),
1287+ help = 'Where to write the newly annotated result.'
1288+ '[default: overwrite input filepath]'
1289+ )
1290+ def annotation_create (input_path , annotation_type ,
1291+ name , text , filepath , output_path ):
1292+ """Load a Result, add an Annotation and save."""
1293+ import qiime2 .sdk
1294+ from qiime2 .core .annotate import Note
1295+ from q2cli .core .config import CONFIG
1296+ from q2cli .util import get_plugin_manager
1297+
1298+ get_plugin_manager ()
1299+ # enforce exactly one of `--text` or `--file`
1300+ if (text is None ) == (filepath is None ):
1301+ raise click .UsageError ('Exactly one of `--text` or `--file`'
1302+ ' must be provided.' )
1303+
1304+ result = qiime2 .sdk .Result .load (input_path )
1305+
1306+ # TODO: add support for additional annotation types post 7.0
1307+ if annotation_type == 'Note' :
1308+ annotation = Note (name = name , text = text , filepath = filepath )
1309+ else :
1310+ raise click .BadParameter ('Unsupported annotation type: '
1311+ f'{ annotation_type } ' )
1312+
1313+ try :
1314+ result .add_annotation (annotation )
1315+ except Exception as e :
1316+ click .echo (CONFIG .cfg_style ('error' , str (e )), err = True )
1317+ raise click .Abort ()
1318+
1319+ out_fp = output_path or input_path
1320+ result .save (out_fp )
1321+
1322+ click .echo (
1323+ CONFIG .cfg_style ('success' ,
1324+ f'Added { annotation_type } "{ name } " to { out_fp } ' )
1325+ )
0 commit comments