File: wrappers.py

package info (click to toggle)
python-bioblend 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,096 kB
  • sloc: python: 7,596; sh: 219; makefile: 158
file content (1901 lines) | stat: -rw-r--r-- 63,473 bytes parent folder | download | duplicates (2)
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
# pylint: disable=W0622,E1101

"""
A basic object-oriented interface for Galaxy entities.
"""

import abc
import json
from collections.abc import (
    Mapping,
    Sequence,
)
from typing import (
    Any,
    Callable,
    cast,
    ClassVar,
    Dict,
    Generic,
    IO,
    Iterable,
    Iterator,
    List,
    Optional,
    Set,
    Tuple,
    Type,
    TYPE_CHECKING,
    TypeVar,
    Union,
)

from typing_extensions import Literal

import bioblend
from bioblend.galaxy.workflows import InputsBy
from bioblend.util import abstractclass

if TYPE_CHECKING:
    from . import client
    from .galaxy_instance import GalaxyInstance

__all__ = (
    "Wrapper",
    "Step",
    "Workflow",
    "LibraryContentInfo",
    "HistoryContentInfo",
    "DatasetContainer",
    "History",
    "Library",
    "Folder",
    "Dataset",
    "HistoryDatasetAssociation",
    "DatasetCollection",
    "HistoryDatasetCollectionAssociation",
    "LibraryDatasetDatasetAssociation",
    "LibraryDataset",
    "Tool",
    "Job",
    "LibraryPreview",
    "HistoryPreview",
    "WorkflowPreview",
)

WrapperSubtype = TypeVar("WrapperSubtype", bound="Wrapper")


@abstractclass
class Wrapper:
    """
    Abstract base class for Galaxy entity wrappers.

    Wrapper instances wrap deserialized JSON dictionaries such as the
    ones obtained by the Galaxy web API, converting key-based access to
    attribute-based access (e.g., ``library['name'] -> library.name``).

    Dict keys that are converted to attributes are listed in the
    ``BASE_ATTRS`` class variable: this is the 'stable' interface.
    Note that the wrapped dictionary is accessible via the ``wrapped``
    attribute.
    """

    BASE_ATTRS: Tuple[str, ...] = ("id",)
    gi: Optional["GalaxyInstance"]
    id: str
    is_modified: bool
    wrapped: dict
    _cached_parent: Optional["Wrapper"]

    def __init__(
        self, wrapped: Dict[str, Any], parent: Optional["Wrapper"] = None, gi: Optional["GalaxyInstance"] = None
    ) -> None:
        """
        :type wrapped: dict
        :param wrapped: JSON-serializable dictionary

        :type parent: :class:`Wrapper`
        :param parent: the parent of this wrapper

        :type gi: :class:`GalaxyInstance`
        :param gi: the GalaxyInstance through which we can access this wrapper
        """
        if not isinstance(wrapped, Mapping):
            raise TypeError("wrapped object must be a mapping type")
        # loads(dumps(x)) is a bit faster than deepcopy and allows type checks
        try:
            dumped = json.dumps(wrapped)
        except (TypeError, ValueError):
            raise ValueError("wrapped object must be JSON-serializable")
        object.__setattr__(self, "wrapped", json.loads(dumped))
        for k in self.BASE_ATTRS:
            object.__setattr__(self, k, self.wrapped.get(k))
        object.__setattr__(self, "_cached_parent", parent)
        object.__setattr__(self, "is_modified", False)
        object.__setattr__(self, "gi", gi)

    @property
    def parent(self) -> Optional["Wrapper"]:
        """
        The parent of this wrapper.
        """
        return self._cached_parent

    @property
    def is_mapped(self) -> bool:
        """
        ``True`` if this wrapper is mapped to an actual Galaxy entity.
        """
        return self.id is not None

    def unmap(self) -> None:
        """
        Disconnect this wrapper from Galaxy.
        """
        object.__setattr__(self, "id", None)

    def clone(self: WrapperSubtype) -> WrapperSubtype:
        """
        Return an independent copy of this wrapper.
        """
        return self.__class__(self.wrapped)

    def touch(self) -> None:
        """
        Mark this wrapper as having been modified since its creation.
        """
        object.__setattr__(self, "is_modified", True)
        if self.parent:
            self.parent.touch()

    def to_json(self) -> str:
        """
        Return a JSON dump of this wrapper.
        """
        return json.dumps(self.wrapped)

    @classmethod
    def from_json(cls: Type[WrapperSubtype], jdef: str) -> WrapperSubtype:
        """
        Build a new wrapper from a JSON dump.
        """
        return cls(json.loads(jdef))

    # FIXME: things like self.x[0] = 'y' do NOT call self.__setattr__
    def __setattr__(self, name: str, value: str) -> None:
        if name not in self.wrapped:
            raise AttributeError("can't set attribute")
        self.wrapped[name] = value
        object.__setattr__(self, name, value)
        self.touch()

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({self.wrapped!r})"


class Step(Wrapper):
    """
    Workflow step.

    Steps are the main building blocks of a Galaxy workflow. A step can be: an
    input (type ``data_collection_input``, ``data_input`` or
    ``parameter_input``), a computational tool (type ``tool``), a subworkflow
    (type ``subworkflow``) or a pause (type ``pause``).
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "input_steps",
        "name",
        "tool_id",
        "tool_inputs",
        "tool_version",
        "type",
    )
    input_steps: Dict[str, Dict]
    type: str
    tool_id: Optional[str]
    tool_inputs: Dict
    tool_version: Optional[str]

    def __init__(self, step_dict: Dict[str, Any], parent: Wrapper) -> None:
        super().__init__(step_dict, parent=parent, gi=parent.gi)
        try:
            stype = step_dict["type"]
        except KeyError:
            raise ValueError("not a step dict")
        if stype not in {"data_collection_input", "data_input", "parameter_input", "pause", "subworkflow", "tool"}:
            raise ValueError(f"Unknown step type: {stype!r}")


class InvocationStep(Wrapper):
    """
    Invocation step.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "action",
        "job_id",
        "order_index",
        "state",
        "update_time",
        "workflow_step_id",
        "workflow_step_label",
        "workflow_step_uuid",
    )
    action: Optional[object]
    gi: "GalaxyInstance"
    job_id: str
    order_index: int
    state: str
    update_time: str
    workflow_step_id: str
    workflow_step_label: str
    workflow_step_uuid: str

    @property
    def parent(self) -> Wrapper:
        ret = super().parent
        assert ret is not None
        return ret

    def __init__(self, wrapped: Dict[str, Any], parent: Wrapper, gi: "GalaxyInstance") -> None:
        super().__init__(wrapped, parent, gi)

    def refresh(self) -> "InvocationStep":
        """
        Re-fetch the attributes pertaining to this object.

        :return: self
        """
        step_dict = self.gi.gi.invocations.show_invocation_step(self.parent.id, self.id)
        self.__init__(step_dict, parent=self.parent, gi=self.gi)  # type: ignore[misc]
        return self

    def get_outputs(self) -> Dict[str, "HistoryDatasetAssociation"]:
        """
        Get the output datasets of the invocation step

        :rtype: dict of `HistoryDatasetAssociation`
        :return: dictionary mapping output names to history datasets
        """
        if not hasattr(self, "outputs"):
            self.refresh()
        return {name: self.gi.datasets.get(out_dict["id"]) for name, out_dict in self.wrapped["outputs"].items()}

    def get_output_collections(self) -> Dict[str, "HistoryDatasetCollectionAssociation"]:
        """
        Get the output dataset collections of the invocation step

        :rtype: dict of `HistoryDatasetCollectionAssociation`
        :return: dictionary mapping output names to history dataset collections
        """
        if not hasattr(self, "output_collections"):
            self.refresh()
        return {
            name: self.gi.dataset_collections.get(out_coll_dict["id"])
            for name, out_coll_dict in self.wrapped["output_collections"].items()
        }


class Workflow(Wrapper):
    """
    Workflows represent ordered sequences of computations on Galaxy.

    A workflow defines a sequence of steps that produce one or more
    results from an input dataset.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "deleted",
        "inputs",
        "latest_workflow_uuid",
        "name",
        "owner",
        "published",
        "steps",
        "tags",
    )
    dag: Dict[str, Set[str]]
    deleted: bool
    input_labels_to_ids: Dict[str, Set[str]]
    inputs: Dict[str, Dict]
    inv_dag: Dict[str, Set[str]]
    missing_ids: List
    name: str
    owner: str
    POLLING_INTERVAL = 10  # for output state monitoring
    published: bool
    sink_ids: Set[str]
    source_ids: Set[str]
    steps: Dict[str, Step]
    tags: List[str]
    tool_labels_to_ids: Dict[str, Set[str]]

    def __init__(self, wf_dict: Dict[str, Any], gi: Optional["GalaxyInstance"] = None) -> None:
        super().__init__(wf_dict, gi=gi)
        if gi:
            tools_list_by_id = [t.id for t in gi.tools.get_previews()]
        else:
            tools_list_by_id = []
        missing_ids = []
        tool_labels_to_ids: Dict[str, Set[str]] = {}
        for k, v in self.steps.items():
            step_dict = cast(Dict[str, Any], v)
            # convert step ids to str for consistency with outer keys
            step_dict["id"] = str(step_dict["id"])
            for i in step_dict["input_steps"].values():
                i["source_step"] = str(i["source_step"])
            step = Step(step_dict, self)
            self.steps[k] = step
            if step.type == "tool":
                if not step.tool_inputs or step.tool_id not in tools_list_by_id:
                    missing_ids.append(k)
                assert step.tool_id
                tool_labels_to_ids.setdefault(step.tool_id, set()).add(step.id)
        input_labels_to_ids: Dict[str, Set[str]] = {}
        for id_, d in self.inputs.items():
            input_labels_to_ids.setdefault(d["label"], set()).add(id_)
        object.__setattr__(self, "input_labels_to_ids", input_labels_to_ids)
        object.__setattr__(self, "tool_labels_to_ids", tool_labels_to_ids)
        dag, inv_dag = self._get_dag()
        heads, tails = set(dag), set(inv_dag)
        object.__setattr__(self, "dag", dag)
        object.__setattr__(self, "inv_dag", inv_dag)
        object.__setattr__(self, "source_ids", heads - tails)
        assert (
            set(self.inputs) == self.data_collection_input_ids | self.data_input_ids | self.parameter_input_ids
        ), f"inputs is {self.inputs!r}, while data_collection_input_ids is {self.data_collection_input_ids!r}, data_input_ids is {self.data_input_ids!r} and parameter_input_ids is {self.parameter_input_ids!r}"
        object.__setattr__(self, "sink_ids", tails - heads)
        object.__setattr__(self, "missing_ids", missing_ids)

    def _get_dag(self) -> Tuple[Dict[str, Set[str]], Dict[str, Set[str]]]:
        """
        Return the workflow's DAG.

        For convenience, this method computes a 'direct' (step =>
        successors) and an 'inverse' (step => predecessors)
        representation of the same DAG.

        For instance, a workflow with a single tool *c*, two inputs
        *a, b* and three outputs *d, e, f* is represented by (direct)::

          {'a': {'c'}, 'b': {'c'}, 'c': {'d', 'e', 'f'}}

        and by (inverse)::

          {'c': {'a', 'b'}, 'd': {'c'}, 'e': {'c'}, 'f': {'c'}}
        """
        dag: Dict[str, Set[str]] = {}
        inv_dag: Dict[str, Set[str]] = {}
        for s in self.steps.values():
            assert isinstance(s, Step)
            for i in s.input_steps.values():
                head, tail = i["source_step"], s.id
                dag.setdefault(head, set()).add(tail)
                inv_dag.setdefault(tail, set()).add(head)
        return dag, inv_dag

    def sorted_step_ids(self) -> List[str]:
        """
        Return a topological sort of the workflow's DAG.
        """
        ids: List[str] = []
        source_ids = self.source_ids.copy()
        inv_dag = {k: v.copy() for k, v in self.inv_dag.items()}
        while source_ids:
            head = source_ids.pop()
            ids.append(head)
            for tail in self.dag.get(head, set()):
                incoming = inv_dag[tail]
                incoming.remove(head)
                if not incoming:
                    source_ids.add(tail)
        return ids

    @property
    def data_input_ids(self) -> Set[str]:
        """
        Return the ids of data input steps for this workflow.
        """
        return {id_ for id_, s in self.steps.items() if s.type == "data_input"}

    @property
    def data_collection_input_ids(self) -> Set[str]:
        """
        Return the ids of data collection input steps for this workflow.
        """
        return {id_ for id_, s in self.steps.items() if s.type == "data_collection_input"}

    @property
    def parameter_input_ids(self) -> Set[str]:
        """
        Return the ids of parameter input steps for this workflow.
        """
        return {id_ for id_, s in self.steps.items() if s.type == "parameter_input"}

    @property
    def tool_ids(self) -> Set[str]:
        """
        Return the ids of tool steps for this workflow.
        """
        return {id_ for id_, s in self.steps.items() if s.type == "tool"}

    @property
    def input_labels(self) -> Set[str]:
        """
        Return the labels of this workflow's input steps.
        """
        return set(self.input_labels_to_ids)

    @property
    def is_runnable(self) -> bool:
        """
        Return True if the workflow can be run on Galaxy.

        A workflow is considered runnable on a Galaxy instance if all
        of the tools it uses are installed in that instance.
        """
        return not self.missing_ids

    @staticmethod
    def _convert_input_map(input_map: Dict[str, Any]) -> Dict[str, Any]:
        """
        Convert ``input_map`` to the format required by the Galaxy web API.

        :type input_map: dict
        :param input_map: a mapping to datasets or dataset collections

        :rtype: dict
        :return: a mapping in the format required by the Galaxy web API.
        """
        ret = {}
        for key, value in input_map.items():
            if isinstance(
                value,
                (
                    HistoryDatasetAssociation,
                    HistoryDatasetCollectionAssociation,
                    LibraryDatasetDatasetAssociation,
                    LibraryDataset,
                ),
            ):
                ret[key] = {"id": value.id, "src": value.SRC}
            else:
                ret[key] = value
        return ret

    # I think we should deprecate this method - NS
    def preview(self) -> "WorkflowPreview":
        assert self.gi is not None
        try:
            return [_ for _ in self.gi.workflows.get_previews(published=True) if _.id == self.id][0]
        except IndexError:
            raise ValueError(f"no object for id {self.id}")

    def export(self) -> Dict[str, Any]:
        """
        Export a re-importable representation of the workflow.

        :rtype: dict
        :return: a JSON-serializable dump of the workflow
        """
        assert self.gi is not None
        return self.gi.gi.workflows.export_workflow_dict(self.id)

    def delete(self) -> None:
        """
        Delete this workflow.

        .. warning::
          Deleting a workflow is irreversible - all of the data from
          the workflow will be permanently deleted.
        """
        assert self.gi is not None
        self.gi.workflows.delete(id_=self.id)
        self.unmap()

    def invoke(
        self,
        inputs: Optional[Dict[str, Any]] = None,
        params: Optional[Dict[str, Any]] = None,
        history: Optional[Union[str, "History"]] = None,
        import_inputs_to_history: bool = False,
        replacement_params: Optional[Dict[str, Any]] = None,
        allow_tool_state_corrections: bool = True,
        inputs_by: Optional[InputsBy] = None,
        parameters_normalized: bool = False,
    ) -> "Invocation":
        """
        Invoke the workflow. This will cause a workflow to be scheduled
        and return an object describing the workflow invocation.

        :type inputs: dict
        :param inputs: A mapping of workflow inputs to datasets and dataset collections.
                       The datasets source can be a LibraryDatasetDatasetAssociation (``ldda``),
                       LibraryDataset (``ld``), HistoryDatasetAssociation (``hda``), or
                       HistoryDatasetCollectionAssociation (``hdca``).

                       The map must be in the following format:
                       ``{'<input_index>': dataset or collection}``
                       (e.g. ``{'2': HistoryDatasetAssociation()}``)

                       This map may also be indexed by the UUIDs of the workflow steps,
                       as indicated by the ``uuid`` property of steps returned from the
                       Galaxy API. Alternatively workflow steps may be addressed by
                       the label that can be set in the workflow editor. If using
                       uuid or label you need to also set the ``inputs_by`` parameter
                       to ``step_uuid`` or ``name``.

        :type params: dict
        :param params: A mapping of non-datasets tool parameters (see below)

        :type history: History or str
        :param history: The history in which to store the workflow output, or
          the name of a new history to create. If ``None``, a new 'Unnamed
          history' is created.

        :type import_inputs_to_history: bool
        :param import_inputs_to_history: If ``True``, used workflow inputs will
          be imported into the history. If ``False``, only workflow outputs will
          be visible in the given history.

        :type allow_tool_state_corrections: bool
        :param allow_tool_state_corrections: If True, allow Galaxy to fill in
          missing tool state when running workflows. This may be useful for
          workflows using tools that have changed over time or for workflows
          built outside of Galaxy with only a subset of inputs defined.

        :type replacement_params: dict
        :param replacement_params: pattern-based replacements for post-job
          actions (see below)

        :type inputs_by: str
        :param inputs_by: Determines how inputs are referenced. Can be
          "step_index|step_uuid" (default), "step_index", "step_id", "step_uuid", or "name".

        :type parameters_normalized: bool
        :param parameters_normalized: Whether Galaxy should normalize ``params``
          to ensure everything is referenced by a numeric step ID. Default is
          ``False``, but when setting ``params`` for a subworkflow, ``True`` is
          required.

        :rtype: Invocation
        :return: the workflow invocation

        The ``params`` dict should be specified as follows::

          {STEP_ID: PARAM_DICT, ...}

        where PARAM_DICT is::

          {PARAM_NAME: VALUE, ...}

        For backwards compatibility, the following (deprecated) format is
        also supported for ``params``::

          {TOOL_ID: PARAM_DICT, ...}

        in which case PARAM_DICT affects all steps with the given tool id.
        If both by-tool-id and by-step-id specifications are used, the
        latter takes precedence.

        Finally (again, for backwards compatibility), PARAM_DICT can also
        be specified as::

          {'param': PARAM_NAME, 'value': VALUE}

        Note that this format allows only one parameter to be set per step.

        For a ``repeat`` parameter, the names of the contained parameters needs
        to be specified as ``<repeat name>_<repeat index>|<param name>``, with
        the repeat index starting at 0. For example, if the tool XML contains::

          <repeat name="cutoff" title="Parameters used to filter cells" min="1">
              <param name="name" type="text" value="n_genes" label="Name of param...">
                  <option value="n_genes">n_genes</option>
                  <option value="n_counts">n_counts</option>
              </param>
              <param name="min" type="float" min="0" value="0" label="Min value"/>
          </repeat>

        then the PARAM_DICT should be something like::

          {...
           "cutoff_0|name": "n_genes",
           "cutoff_0|min": "2",
           "cutoff_1|name": "n_counts",
           "cutoff_1|min": "4",
           ...}

        At the time of this writing, it is not possible to change the number of
        times the contained parameters are repeated. Therefore, the parameter
        indexes can go from 0 to n-1, where n is the number of times the
        repeated element was added when the workflow was saved in the Galaxy UI.

        The ``replacement_params`` dict should map parameter names in
        post-job actions (PJAs) to their runtime values. For
        instance, if the final step has a PJA like the following::

          {'RenameDatasetActionout_file1': {'action_arguments': {'newname': '${output}'},
                                            'action_type': 'RenameDatasetAction',
                                            'output_name': 'out_file1'}}

        then the following renames the output dataset to 'foo'::

          replacement_params = {'output': 'foo'}

        see also `this email thread
        <http://lists.bx.psu.edu/pipermail/galaxy-dev/2011-September/006875.html>`_.

        .. warning::
          Historically, workflow invocation consumed a ``dataset_map``
          data structure that was indexed by unencoded workflow step IDs. These
          IDs would not be stable across Galaxy instances. The new ``inputs``
          property is instead indexed by either the ``order_index`` property
          (which is stable across workflow imports) or the step UUID which is
          also stable.
        """
        assert self.gi is not None
        if not self.is_mapped:
            raise RuntimeError("workflow is not mapped to a Galaxy object")
        if not self.is_runnable:
            missing_tools_str = ", ".join(f"{self.steps[step_id].tool_id}[{step_id}]" for step_id in self.missing_ids)
            raise RuntimeError(f"workflow has missing tools: {missing_tools_str}")

        inv_dict = self.gi.gi.workflows.invoke_workflow(
            workflow_id=self.id,
            inputs=self._convert_input_map(inputs or {}),
            params=params,
            history_id=history.id if isinstance(history, History) else None,
            history_name=history if isinstance(history, str) else None,
            import_inputs_to_history=import_inputs_to_history,
            replacement_params=replacement_params,
            allow_tool_state_corrections=allow_tool_state_corrections,
            inputs_by=inputs_by,
            parameters_normalized=parameters_normalized,
        )
        return self.gi.invocations.get(inv_dict["id"])


class Invocation(Wrapper):
    """
    Invocation of a workflow.
    This causes the steps of a workflow to be executed in sequential order.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "history_id",
        "state",
        "update_time",
        "uuid",
        "workflow_id",
    )
    gi: "GalaxyInstance"
    history_id: str
    state: str
    steps: List[InvocationStep]
    update_time: str
    uuid: str
    workflow_id: str

    def __init__(self, inv_dict: Dict[str, Any], gi: "GalaxyInstance") -> None:
        super().__init__(inv_dict, gi=gi)
        self.steps = [InvocationStep(step, parent=self, gi=gi) for step in inv_dict["steps"]]
        self.inputs = [{**v, "label": k} for k, v in inv_dict["inputs"].items()]

    def sorted_step_ids(self) -> List[str]:
        """
        Get the step IDs sorted based on this order index.

        :rtype: list of str
        :param: sorted step IDs
        """
        return [step.id for step in sorted(self.steps, key=lambda step: step.order_index)]

    def step_states(self) -> Set[str]:
        """
        Get the set of step states for this invocation.

        :rtype: set
        :param: step states
        """
        return {step.state for step in self.steps}

    def number_of_steps(self) -> int:
        """
        Get the number of steps for this invocation.

        :rtype: int
        :param: number of steps
        """
        return len(self.steps)

    def sorted_steps_by(
        self,
        indices: Optional[Iterable[int]] = None,
        states: Optional[Iterable[Union[str, None]]] = None,
        step_ids: Optional[Iterable[str]] = None,
    ) -> List[InvocationStep]:
        """
        Get steps for this invocation, or get a subset by specifying
        optional parameters for filtering.

        :type indices: list of int
        :param indices: return steps that have matching order_index

        :type states: list of str
        :param states: return steps that have matching states

        :type step_ids: list of str
        :param step_ids: return steps that have matching step_ids

        :rtype: list of InvocationStep
        :param: invocation steps
        """
        steps: Union[List[InvocationStep], filter] = self.steps
        if indices is not None:
            steps = filter(lambda step: step.order_index in indices, steps)
        if states is not None:
            steps = filter(lambda step: step.state in states, steps)
        if step_ids is not None:
            steps = filter(lambda step: step.id in step_ids, steps)
        return sorted(steps, key=lambda step: step.order_index)

    def cancel(self) -> None:
        """
        Cancel this invocation.

        .. note::
          On success, this method updates the Invocation object's internal variables.
        """
        inv_dict = self.gi.gi.invocations.cancel_invocation(self.id)
        self.__init__(inv_dict, gi=self.gi)  # type: ignore[misc]

    def refresh(self) -> "Invocation":
        """
        Re-fetch the attributes pertaining to this object.

        :return: self
        """
        inv_dict = self.gi.gi.invocations.show_invocation(self.id)
        self.__init__(inv_dict, gi=self.gi)  # type: ignore[misc]
        return self

    def run_step_actions(self, steps: List[InvocationStep], actions: List[object]) -> None:
        """
        Run actions for active steps of this invocation.

        :type steps: list of InvocationStep
        :param steps: list of steps to run actions on

        :type actions: list of objects
        :param actions: list of actions to run

        .. note::
          On success, this method updates the Invocation object's internal step variables.
        """
        if not len(steps) == len(actions):
            raise RuntimeError(
                f"Different number of ``steps`` ({len(steps)}) and ``actions`` ({len(actions)}) in ``{self}.run_step_actions()``"
            )
        step_dict_list = [
            self.gi.gi.invocations.run_invocation_step_action(self.id, step.id, action)
            for step, action in zip(steps, actions)
        ]
        for step, step_dict in zip(steps, step_dict_list):
            step.__init__(step_dict, parent=self, gi=self.gi)  # type: ignore[misc]

    def summary(self) -> Dict[str, Any]:
        """
        Get a summary for this invocation.

        :rtype: dict
        :param: invocation summary
        """
        return self.gi.gi.invocations.get_invocation_summary(self.id)

    def step_jobs_summary(self) -> List[Dict[str, Any]]:
        """
        Get a summary for this invocation's step jobs.

        :rtype: list of dicts
        :param: step job summaries
        """
        return self.gi.gi.invocations.get_invocation_step_jobs_summary(self.id)

    def report(self) -> Dict[str, Any]:
        """
        Get a dictionary containing a Markdown report for this invocation.

        :rtype: dict
        :param: invocation report
        """
        return self.gi.gi.invocations.get_invocation_report(self.id)

    def save_report_pdf(self, file_path: str, chunk_size: int = bioblend.CHUNK_SIZE) -> None:
        """
        Download a PDF report for this invocation.

        :type file_path: str
        :param file_path: path to save the report

        :type chunk_size: int
        :param chunk_size: chunk size in bytes for reading remote data
        """
        self.gi.gi.invocations.get_invocation_report_pdf(self.id, file_path, chunk_size)

    def biocompute_object(self) -> Dict[str, Any]:
        """
        Get a BioCompute object for this invocation.

        :rtype: dict
        :param: BioCompute object
        """
        return self.gi.gi.invocations.get_invocation_biocompute_object(self.id)

    def wait(self, maxwait: float = 12000, interval: float = 3, check: bool = True) -> None:
        """
        Wait for this invocation to reach a terminal state.

        :type maxwait: float
        :param maxwait: upper limit on waiting time

        :type interval: float
        :param interval: polling interval in secconds

        :type check: bool
        :param check: if ``true``, raise an error if the terminal state is not 'scheduled'

        .. note::
          On success, this method updates the Invocation object's internal variables.
        """
        inv_dict = self.gi.gi.invocations.wait_for_invocation(self.id, maxwait=maxwait, interval=interval, check=check)
        self.__init__(inv_dict, gi=self.gi)  # type: ignore[misc]


DatasetSubtype = TypeVar("DatasetSubtype", bound="Dataset")


class Dataset(Wrapper, metaclass=abc.ABCMeta):
    """
    Abstract base class for Galaxy datasets.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "data_type",
        "file_ext",
        "file_name",
        "file_size",
        "genome_build",
        "misc_info",
        "name",
        "state",
    )
    container: "DatasetContainer"
    genome_build: str
    gi: "GalaxyInstance"
    misc_info: str
    name: str
    POLLING_INTERVAL = 1  # for state monitoring
    state: str

    def __init__(self, ds_dict: Dict[str, Any], container: "DatasetContainer", gi: "GalaxyInstance") -> None:
        super().__init__(ds_dict, gi=gi)
        object.__setattr__(self, "container", container)

    @property
    @abc.abstractmethod
    def _stream_url(self) -> str:
        """
        Return the URL to stream this dataset.
        """

    def get_stream(self, chunk_size: int = bioblend.CHUNK_SIZE) -> Iterator[bytes]:
        """
        Open dataset for reading and return an iterator over its contents.

        :type chunk_size: int
        :param chunk_size: read this amount of bytes at a time
        """
        kwargs: Dict[str, Any] = {"stream": True}
        if isinstance(self, LibraryDataset):
            kwargs["params"] = {"ld_ids%5B%5D": self.id}
        r = self.gi.gi.make_get_request(self._stream_url, **kwargs)
        if isinstance(self, LibraryDataset) and r.status_code == 500:
            # compatibility with older Galaxy releases
            kwargs["params"] = {"ldda_ids%5B%5D": self.id}
            r = self.gi.gi.make_get_request(self._stream_url, **kwargs)
        r.raise_for_status()
        return r.iter_content(chunk_size)  # FIXME: client can't close r

    def peek(self, chunk_size: int = bioblend.CHUNK_SIZE) -> bytes:
        """
        Open dataset for reading and return the first chunk.

        See :meth:`.get_stream` for param info.
        """
        try:
            return next(self.get_stream(chunk_size=chunk_size))
        except StopIteration:
            return b""

    def download(self, file_object: IO[bytes], chunk_size: int = bioblend.CHUNK_SIZE) -> None:
        """
        Open dataset for reading and save its contents to ``file_object``.

        :type file_object: file
        :param file_object: output file object

        See :meth:`.get_stream` for info on other params.
        """
        for chunk in self.get_stream(chunk_size=chunk_size):
            file_object.write(chunk)

    def get_contents(self, chunk_size: int = bioblend.CHUNK_SIZE) -> bytes:
        """
        Open dataset for reading and return its **full** contents.

        See :meth:`.get_stream` for param info.
        """
        return b"".join(self.get_stream(chunk_size=chunk_size))

    def refresh(self: DatasetSubtype) -> DatasetSubtype:
        """
        Re-fetch the attributes pertaining to this object.

        :return: self
        """
        gi_client = getattr(self.gi.gi, self.container.API_MODULE)
        ds_dict = gi_client.show_dataset(self.container.id, self.id)
        self.__init__(ds_dict, self.container, self.gi)  # type: ignore[misc]
        return self

    def wait(self, polling_interval: float = POLLING_INTERVAL, break_on_error: bool = True) -> None:
        """
        Wait for this dataset to come out of the pending states.

        :type polling_interval: float
        :param polling_interval: polling interval in seconds

        :type break_on_error: bool
        :param break_on_error: if ``True``, raise a RuntimeError exception if
          the dataset ends in the 'error' state.

        .. warning::

          This is a blocking operation that can take a very long time. Also,
          note that this method does not return anything; however, this dataset
          is refreshed (possibly multiple times) during the execution.
        """
        self.gi._wait_datasets([self], polling_interval=polling_interval, break_on_error=break_on_error)


class HistoryDatasetAssociation(Dataset):
    """
    Maps to a Galaxy ``HistoryDatasetAssociation``.
    """

    BASE_ATTRS = Dataset.BASE_ATTRS + ("annotation", "deleted", "purged", "tags", "visible")
    SRC = "hda"
    annotation: str
    deleted: bool
    purged: bool

    @property
    def _stream_url(self) -> str:
        base_url = self.gi.gi.histories._make_url(module_id=self.container.id, contents=True)
        return f"{base_url}/{self.id}/display"

    def get_stream(self, chunk_size: int = bioblend.CHUNK_SIZE) -> Iterator[bytes]:
        """
        Open dataset for reading and return an iterator over its contents.

        :type chunk_size: int
        :param chunk_size: read this amount of bytes at a time
        """
        _, _, r = self.gi.gi.datasets._initiate_download(
            self.id,
            stream_content=True,
        )
        return r.iter_content(chunk_size)  # FIXME: client can't close r

    def update(self, **kwargs: Any) -> "HistoryDatasetAssociation":
        """
        Update this history dataset metadata. Some of the attributes that can be
        modified are documented below.

        :type name: str
        :param name: Replace history dataset name with the given string

        :type genome_build: str
        :param genome_build: Replace history dataset genome build (dbkey)

        :type annotation: str
        :param annotation: Replace history dataset annotation with given string

        :type deleted: bool
        :param deleted: Mark or unmark history dataset as deleted

        :type visible: bool
        :param visible: Mark or unmark history dataset as visible
        """
        res = self.gi.gi.histories.update_dataset(self.container.id, self.id, **kwargs)
        # Refresh also the history because the dataset may have been (un)deleted
        self.container.refresh()
        self.__init__(res, self.container, gi=self.gi)  # type: ignore[misc]
        return self

    def delete(self, purge: bool = False) -> None:
        """
        Delete this history dataset.

        :type purge: bool
        :param purge: if ``True``, also purge (permanently delete) the dataset

        .. note::
          The ``purge`` option works only if the Galaxy instance has the
          ``allow_user_dataset_purge`` option set to ``true`` in the
          ``config/galaxy.yml`` configuration file.
        """
        self.gi.gi.histories.delete_dataset(self.container.id, self.id, purge=purge)
        self.container.refresh()
        self.refresh()


DatasetCollectionSubtype = TypeVar("DatasetCollectionSubtype", bound="DatasetCollection")


class DatasetCollection(Wrapper, metaclass=abc.ABCMeta):
    """
    Abstract base class for Galaxy dataset collections.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "collection_type",
        "deleted",
        "name",
        "state",
    )
    container: Union["DatasetCollection", "History"]
    API_MODULE = "dataset_collections"
    collection_type: str
    deleted: bool
    gi: "GalaxyInstance"

    def __init__(
        self, dsc_dict: Dict[str, Any], container: Union["DatasetCollection", "History"], gi: "GalaxyInstance"
    ) -> None:
        super().__init__(dsc_dict, gi=gi)
        object.__setattr__(self, "container", container)

    def refresh(self: DatasetCollectionSubtype) -> DatasetCollectionSubtype:
        """
        Re-fetch the attributes pertaining to this object.

        :return: self
        """
        gi_client = getattr(self.gi.gi, self.container.API_MODULE)
        dsc_dict = gi_client.show_dataset_collection(self.container.id, self.id)
        self.__init__(dsc_dict, self.container, self.gi)  # type: ignore[misc]
        return self

    @abc.abstractmethod
    def delete(self) -> None:
        """
        Delete this dataset collection.
        """


class HistoryDatasetCollectionAssociation(DatasetCollection):
    """
    Maps to a Galaxy ``HistoryDatasetCollectionAssociation``.
    """

    BASE_ATTRS = DatasetCollection.BASE_ATTRS + ("tags", "visible", "elements")
    SRC = "hdca"
    elements: List[Dict]

    def delete(self) -> None:
        self.gi.gi.histories.delete_dataset_collection(self.container.id, self.id)
        self.container.refresh()
        self.refresh()


@abstractclass
class LibRelatedDataset(Dataset):
    """
    Base class for LibraryDatasetDatasetAssociation and LibraryDataset classes.
    """

    @property
    def _stream_url(self) -> str:
        base_url = self.gi.gi.libraries._make_url()
        return f"{base_url}/datasets/download/uncompressed"


class LibraryDatasetDatasetAssociation(LibRelatedDataset):
    """
    Maps to a Galaxy ``LibraryDatasetDatasetAssociation``.
    """

    BASE_ATTRS = LibRelatedDataset.BASE_ATTRS + ("deleted",)
    SRC = "ldda"


class LibraryDataset(LibRelatedDataset):
    """
    Maps to a Galaxy ``LibraryDataset``.
    """

    SRC = "ld"
    file_name: str

    def delete(self, purged: bool = False) -> None:
        """
        Delete this library dataset.

        :type purged: bool
        :param purged: if ``True``, also purge (permanently delete) the dataset
        """
        self.gi.gi.libraries.delete_library_dataset(self.container.id, self.id, purged=purged)
        self.container.refresh()
        self.refresh()

    def update(self, **kwargs: Any) -> "LibraryDataset":
        """
        Update this library dataset metadata. Some of the attributes that can be
        modified are documented below.

        :type name: str
        :param name: Replace history dataset name with the given string

        :type genome_build: str
        :param genome_build: Replace history dataset genome build (dbkey)
        """
        res = self.gi.gi.libraries.update_library_dataset(self.id, **kwargs)
        self.container.refresh()
        self.__init__(res, self.container, gi=self.gi)  # type: ignore[misc]
        return self


@abstractclass
class ContentInfo(Wrapper):
    """
    Instances of this class wrap dictionaries obtained by getting
    ``/api/{histories,libraries}/<ID>/contents`` from Galaxy.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "name",
        "type",
    )


class LibraryContentInfo(ContentInfo):
    """
    Instances of this class wrap dictionaries obtained by getting
    ``/api/libraries/<ID>/contents`` from Galaxy.
    """


class HistoryContentInfo(ContentInfo):
    """
    Instances of this class wrap dictionaries obtained by getting
    ``/api/histories/<ID>/contents`` from Galaxy.
    """

    BASE_ATTRS = ContentInfo.BASE_ATTRS + ("deleted", "state", "visible")


DatasetContainerSubtype = TypeVar("DatasetContainerSubtype", bound="DatasetContainer")


class DatasetContainer(Wrapper, Generic[DatasetSubtype], metaclass=abc.ABCMeta):
    """
    Abstract base class for dataset containers (histories and libraries).
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "deleted",
        "name",
    )
    API_MODULE: str
    CONTENT_INFO_TYPE: Type[ContentInfo]
    DS_TYPE: ClassVar[Callable]
    content_infos: List[ContentInfo]
    deleted: bool
    gi: "GalaxyInstance"
    name: str
    obj_gi_client: "client.ObjDatasetContainerClient"

    def __init__(
        self,
        c_dict: Dict[str, Any],
        content_infos: Optional[List[ContentInfo]] = None,
        gi: Optional["GalaxyInstance"] = None,
    ) -> None:
        """
        :type content_infos: list of :class:`ContentInfo`
        :param content_infos: info objects for the container's contents
        """
        super().__init__(c_dict, gi=gi)
        if content_infos is None:
            content_infos = []
        object.__setattr__(self, "content_infos", content_infos)
        object.__setattr__(self, "obj_gi_client", getattr(self.gi, self.API_MODULE))

    @property
    def dataset_ids(self) -> List[str]:
        """
        Return the ids of the contained datasets.
        """
        return [_.id for _ in self.content_infos if _.type == "file"]

    # I think we should deprecate this method - NS
    def preview(self) -> "DatasetContainerPreview":
        getf = self.obj_gi_client.get_previews
        # self.state could be stale: check both regular and deleted containers
        try:
            p = [_ for _ in getf() if _.id == self.id][0]
        except IndexError:
            try:
                p = [_ for _ in getf(deleted=True) if _.id == self.id][0]
            except IndexError:
                raise ValueError(f"no object for id {self.id}")
        return p

    def refresh(self: DatasetContainerSubtype) -> DatasetContainerSubtype:
        """
        Re-fetch the attributes pertaining to this object.

        :return: self
        """
        fresh = self.obj_gi_client.get(self.id)
        self.__init__(fresh.wrapped, content_infos=fresh.content_infos, gi=self.gi)  # type: ignore[misc]
        return self

    def get_dataset(self, ds_id: str) -> DatasetSubtype:
        """
        Retrieve the dataset corresponding to the given id.

        :type ds_id: str
        :param ds_id: dataset id

        :rtype: :class:`~.HistoryDatasetAssociation` or
          :class:`~.LibraryDataset`
        :return: the dataset corresponding to ``ds_id``
        """
        gi_client = getattr(self.gi.gi, self.API_MODULE)
        ds_dict = gi_client.show_dataset(self.id, ds_id)
        return self.DS_TYPE(ds_dict, self, gi=self.gi)

    def get_datasets(self, name: Optional[str] = None) -> List[DatasetSubtype]:
        """
        Get all datasets contained inside this dataset container.

        :type name: str
        :param name: return only datasets with this name

        :rtype: list of :class:`~.HistoryDatasetAssociation` or list of
          :class:`~.LibraryDataset`
        :return: datasets with the given name contained inside this
          container

        .. note::
          when filtering library datasets by name, specify their full
          paths starting from the library's root folder, e.g.,
          ``/seqdata/reads.fastq``.  Full paths are available through
          the ``content_infos`` attribute of
          :class:`~.Library` objects.
        """
        if name is None:
            ds_ids = self.dataset_ids
        else:
            ds_ids = [_.id for _ in self.content_infos if _.name == name]
        return [self.get_dataset(_) for _ in ds_ids]

    @abc.abstractmethod
    def delete(self) -> None:
        """
        Delete this dataset container.
        """


class History(DatasetContainer[HistoryDatasetAssociation]):
    """
    Maps to a Galaxy history.
    """

    BASE_ATTRS = DatasetContainer.BASE_ATTRS + (
        "annotation",
        "published",
        "state",
        "state_ids",
        "state_details",
        "tags",
    )
    DS_TYPE = HistoryDatasetAssociation
    DSC_TYPE = HistoryDatasetCollectionAssociation
    CONTENT_INFO_TYPE = HistoryContentInfo
    API_MODULE = "histories"
    annotation: str
    published: bool
    tags: List[str]

    def update(self, **kwargs: Any) -> "History":
        """
        Update history metadata information. Some of the attributes that can be
        modified are documented below.

        :type name: str
        :param name: Replace history name with the given string

        :type annotation: str
        :param annotation: Replace history annotation with the given string

        :type deleted: bool
        :param deleted: Mark or unmark history as deleted

        :type purged: bool
        :param purged: If True, mark history as purged (permanently deleted).

        :type published: bool
        :param published: Mark or unmark history as published

        :type importable: bool
        :param importable: Mark or unmark history as importable

        :type tags: list
        :param tags: Replace history tags with the given list
        """
        # TODO: wouldn't it be better if name and annotation were attributes?
        self.gi.gi.histories.update_history(self.id, **kwargs)
        self.refresh()
        return self

    def delete(self, purge: bool = False) -> None:
        """
        Delete this history.

        :type purge: bool
        :param purge: if ``True``, also purge (permanently delete) the history

        .. note::
          The ``purge`` option works only if the Galaxy instance has the
          ``allow_user_dataset_purge`` option set to ``true`` in the
          ``config/galaxy.yml`` configuration file.
        """
        self.gi.histories.delete(id_=self.id, purge=purge)
        self.refresh()
        self.unmap()

    def import_dataset(self, lds: LibraryDataset) -> HistoryDatasetAssociation:
        """
        Import a dataset into the history from a library.

        :type lds: :class:`~.LibraryDataset`
        :param lds: the library dataset to import

        :rtype: :class:`~.HistoryDatasetAssociation`
        :return: the imported history dataset
        """
        if not self.is_mapped:
            raise RuntimeError("history is not mapped to a Galaxy object")
        if not isinstance(lds, LibraryDataset):
            raise TypeError("lds is not a LibraryDataset")
        res = self.gi.gi.histories.upload_dataset_from_library(self.id, lds.id)
        if not isinstance(res, Mapping):
            raise RuntimeError(f"upload_dataset_from_library: unexpected reply: {res!r}")
        self.refresh()
        return self.get_dataset(res["id"])

    def upload_file(self, path: str, **kwargs: Any) -> HistoryDatasetAssociation:
        """
        Upload the file specified by ``path`` to this history.

        :type path: str
        :param path: path of the file to upload

        See :meth:`~bioblend.galaxy.tools.ToolClient.upload_file` for
        the optional parameters.

        :rtype: :class:`~.HistoryDatasetAssociation`
        :return: the uploaded dataset
        """
        out_dict = self.gi.gi.tools.upload_file(path, self.id, **kwargs)
        self.refresh()
        return self.get_dataset(out_dict["outputs"][0]["id"])

    upload_dataset = upload_file

    def upload_from_ftp(self, path: str, **kwargs: Any) -> HistoryDatasetAssociation:
        """
        Upload the file specified by ``path`` from the user's FTP directory to
        this history.

        :type path: str
        :param path: path of the file in the user's FTP directory

        See :meth:`~bioblend.galaxy.tools.ToolClient.upload_file` for
        the optional parameters.

        :rtype: :class:`~.HistoryDatasetAssociation`
        :return: the uploaded dataset
        """
        out_dict = self.gi.gi.tools.upload_from_ftp(path, self.id, **kwargs)
        self.refresh()
        return self.get_dataset(out_dict["outputs"][0]["id"])

    def paste_content(self, content: str, **kwargs: Any) -> HistoryDatasetAssociation:
        """
        Upload a string to a new dataset in this history.

        :type content: str
        :param content: content of the new dataset to upload

        See :meth:`~bioblend.galaxy.tools.ToolClient.upload_file` for
        the optional parameters (except file_name).

        :rtype: :class:`~.HistoryDatasetAssociation`
        :return: the uploaded dataset
        """
        out_dict = self.gi.gi.tools.paste_content(content, self.id, **kwargs)
        self.refresh()
        return self.get_dataset(out_dict["outputs"][0]["id"])

    def export(
        self,
        gzip: bool = True,
        include_hidden: bool = False,
        include_deleted: bool = False,
        wait: bool = False,
        maxwait: Optional[int] = None,
    ) -> str:
        """
        Start a job to create an export archive for this history.  See
        :meth:`~bioblend.galaxy.histories.HistoryClient.export_history`
        for parameter and return value info.
        """
        return self.gi.gi.histories.export_history(
            self.id,
            gzip=gzip,
            include_hidden=include_hidden,
            include_deleted=include_deleted,
            wait=wait,
            maxwait=maxwait,
        )

    def download(self, jeha_id: str, outf: IO[bytes], chunk_size: int = bioblend.CHUNK_SIZE) -> None:
        """
        Download an export archive for this history.  Use :meth:`export`
        to create an export and get the required ``jeha_id``.  See
        :meth:`~bioblend.galaxy.histories.HistoryClient.download_history`
        for parameter and return value info.
        """
        return self.gi.gi.histories.download_history(self.id, jeha_id, outf, chunk_size=chunk_size)

    def create_dataset_collection(
        self, collection_description: bioblend.galaxy.dataset_collections.CollectionDescription
    ) -> "HistoryDatasetCollectionAssociation":
        """
        Create a new dataset collection in the history by providing a collection description.

        :type collection_description: bioblend.galaxy.dataset_collections.CollectionDescription
        :param collection_description: a description of the dataset collection

        :rtype: :class:`~.HistoryDatasetCollectionAssociation`
        :return: the new dataset collection
        """
        dataset_collection = self.gi.gi.histories.create_dataset_collection(self.id, collection_description)
        self.refresh()
        return self.get_dataset_collection(dataset_collection["id"])

    def get_dataset_collection(self, dsc_id: str) -> "HistoryDatasetCollectionAssociation":
        """
        Retrieve the dataset collection corresponding to the given id.

        :type dsc_id: str
        :param dsc_id: dataset collection id

        :rtype: :class:`~.HistoryDatasetCollectionAssociation`
        :return: the dataset collection corresponding to ``dsc_id``
        """
        dsc_dict = self.gi.gi.histories.show_dataset_collection(self.id, dsc_id)
        return self.DSC_TYPE(dsc_dict, self, gi=self.gi)


class Library(DatasetContainer[LibraryDataset]):
    """
    Maps to a Galaxy library.
    """

    BASE_ATTRS = DatasetContainer.BASE_ATTRS + ("description", "synopsis")
    DS_TYPE = LibraryDataset
    CONTENT_INFO_TYPE = LibraryContentInfo
    API_MODULE = "libraries"
    description: str
    synopsis: str

    @property
    def folder_ids(self) -> List[str]:
        """
        Return the ids of the contained folders.
        """
        return [_.id for _ in self.content_infos if _.type == "folder"]

    def delete(self) -> None:
        """
        Delete this library.
        """
        self.gi.libraries.delete(id_=self.id)
        self.refresh()
        self.unmap()

    def _pre_upload(self, folder: Optional["Folder"]) -> Optional[str]:
        """
        Return the id of the given folder, after sanity checking.
        """
        if not self.is_mapped:
            raise RuntimeError("library is not mapped to a Galaxy object")
        return None if folder is None else folder.id

    def upload_data(self, data: str, folder: Optional["Folder"] = None, **kwargs: Any) -> LibraryDataset:
        """
        Upload data to this library.

        :type data: str
        :param data: dataset contents

        :type folder: :class:`~.Folder`
        :param folder: a folder object, or ``None`` to upload to the root folder

        :rtype: :class:`~.LibraryDataset`
        :return: the dataset object that represents the uploaded content

        Optional keyword arguments: ``file_type``, ``dbkey``.
        """
        fid = self._pre_upload(folder)
        res = self.gi.gi.libraries.upload_file_contents(self.id, data, folder_id=fid, **kwargs)
        self.refresh()
        return self.get_dataset(res[0]["id"])

    def upload_from_url(self, url: str, folder: Optional["Folder"] = None, **kwargs: Any) -> LibraryDataset:
        """
        Upload data to this library from the given URL.

        :type url: str
        :param url: URL from which data should be read

        See :meth:`.upload_data` for info on other params.
        """
        fid = self._pre_upload(folder)
        res = self.gi.gi.libraries.upload_file_from_url(self.id, url, folder_id=fid, **kwargs)
        self.refresh()
        return self.get_dataset(res[0]["id"])

    def upload_from_local(self, path: str, folder: Optional["Folder"] = None, **kwargs: Any) -> LibraryDataset:
        """
        Upload data to this library from a local file.

        :type path: str
        :param path: local file path from which data should be read

        See :meth:`.upload_data` for info on other params.
        """
        fid = self._pre_upload(folder)
        res = self.gi.gi.libraries.upload_file_from_local_path(self.id, path, folder_id=fid, **kwargs)
        self.refresh()
        return self.get_dataset(res[0]["id"])

    def upload_from_galaxy_fs(
        self,
        paths: Union[str, Iterable[str]],
        folder: Optional["Folder"] = None,
        link_data_only: Literal["copy_files", "link_to_files"] = "copy_files",
        **kwargs: Any,
    ) -> List[LibraryDataset]:
        """
        Upload data to this library from filesystem paths on the server.

        :type paths: str or :class:`~collections.abc.Iterable` of str
        :param paths: server-side file paths from which data should be read

        :type link_data_only: str
        :param link_data_only: either 'copy_files' (default) or
          'link_to_files'. Setting to 'link_to_files' symlinks instead of
          copying the files

        :rtype: list of :class:`~.LibraryDataset`
        :return: the dataset objects that represent the uploaded content

        See :meth:`.upload_data` for info on other params.

        .. note::
          This method works only if the Galaxy instance has the
          ``allow_path_paste`` option set to ``true`` in the
          ``config/galaxy.yml`` configuration file.
        """
        fid = self._pre_upload(folder)
        if isinstance(paths, str):
            paths = (paths,)
        paths = "\n".join(paths)
        res = self.gi.gi.libraries.upload_from_galaxy_filesystem(
            self.id, paths, folder_id=fid, link_data_only=link_data_only, **kwargs
        )
        if res is None:
            raise RuntimeError("upload_from_galaxy_filesystem: no reply")
        if not isinstance(res, Sequence):
            raise RuntimeError(f"upload_from_galaxy_filesystem: unexpected reply: {res!r}")
        self.refresh()
        return [self.get_dataset(ds_info["id"]) for ds_info in res]

    def copy_from_dataset(
        self, hda: HistoryDatasetAssociation, folder: Optional["Folder"] = None, message: str = ""
    ) -> LibraryDataset:
        """
        Copy a history dataset into this library.

        :type hda: :class:`~.HistoryDatasetAssociation`
        :param hda: history dataset to copy into the library

        See :meth:`.upload_data` for info on other params.
        """
        fid = self._pre_upload(folder)
        res = self.gi.gi.libraries.copy_from_dataset(self.id, hda.id, folder_id=fid, message=message)
        self.refresh()
        return self.get_dataset(res["library_dataset_id"])

    def create_folder(
        self, name: str, description: Optional[str] = None, base_folder: Optional["Folder"] = None
    ) -> "Folder":
        """
        Create a folder in this library.

        :type name: str
        :param name: folder name

        :type description: str
        :param description: optional folder description

        :type base_folder: :class:`~.Folder`
        :param base_folder: parent folder, or ``None`` to create in the root
          folder

        :rtype: :class:`~.Folder`
        :return: the folder just created
        """
        bfid = None if base_folder is None else base_folder.id
        res = self.gi.gi.libraries.create_folder(self.id, name, description=description, base_folder_id=bfid)
        self.refresh()
        return self.get_folder(res[0]["id"])

    def get_folder(self, f_id: str) -> "Folder":
        """
        Retrieve the folder corresponding to the given id.

        :rtype: :class:`~.Folder`
        :return: the folder corresponding to ``f_id``
        """
        f_dict = self.gi.gi.libraries.show_folder(self.id, f_id)
        return Folder(f_dict, self, gi=self.gi)

    @property
    def root_folder(self) -> "Folder":
        """
        The root folder of this library.

        :rtype: :class:`~.Folder`
        :return: the root folder of this library
        """
        return self.get_folder(self.gi.gi.libraries._get_root_folder_id(self.id))


class Folder(Wrapper):
    """
    Maps to a folder in a Galaxy library.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "deleted",
        "description",
        "item_count",
        "name",
    )
    container: Library
    description: str
    gi: "GalaxyInstance"
    name: str
    _cached_parent: Optional["Folder"]

    def __init__(self, f_dict: Dict[str, Any], container: Library, gi: "GalaxyInstance") -> None:
        super().__init__(f_dict, gi=gi)
        object.__setattr__(self, "container", container)

    @property
    def parent(self) -> Optional["Folder"]:
        """
        The parent folder of this folder. The parent of the root folder is
        ``None``.

        :rtype: :class:`~.Folder`
        :return: the parent of this folder
        """
        if self._cached_parent is None:
            object.__setattr__(self, "_cached_parent", self._get_parent())
        return self._cached_parent

    def _get_parent(self) -> Optional["Folder"]:
        """
        Return the parent folder of this folder.
        """
        parent_id = self.wrapped["parent_id"]
        if parent_id is None:
            return None
        return self.container.get_folder(parent_id)

    def refresh(self) -> "Folder":
        """
        Re-fetch the attributes pertaining to this object.

        :return: self
        """
        f_dict = self.gi.gi.libraries.show_folder(self.container.id, self.id)
        self.__init__(f_dict, self.container, gi=self.gi)  # type: ignore[misc]
        return self


class Tool(Wrapper):
    """
    Maps to a Galaxy tool.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "name",
        "version",
    )
    gi: "GalaxyInstance"
    POLLING_INTERVAL = 10  # for output state monitoring

    def run(
        self, inputs: Dict[str, Any], history: History, wait: bool = False, polling_interval: float = POLLING_INTERVAL
    ) -> List[HistoryDatasetAssociation]:
        """
        Execute this tool in the given history with inputs from dict
        ``inputs``.

        :type inputs: dict
        :param inputs: dictionary of input datasets and parameters for
          the tool (see below)

        :type history: :class:`History`
        :param history: the history where to execute the tool

        :type wait: bool
        :param wait: whether to wait while the returned datasets are
          in a pending state

        :type polling_interval: float
        :param polling_interval: polling interval in seconds

        :rtype: list of :class:`HistoryDatasetAssociation`
        :return: list of output datasets

        The ``inputs`` dict should contain input datasets and parameters
        in the (largely undocumented) format used by the Galaxy API.
        Some examples can be found in `Galaxy's API test suite
        <https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy_test/api/test_tools.py>`_.
        The value of an input dataset can also be a :class:`Dataset`
        object, which will be automatically converted to the needed
        format.
        """
        for k, v in inputs.items():
            if isinstance(v, Dataset):
                inputs[k] = {"src": v.SRC, "id": v.id}  # type: ignore
        out_dict = self.gi.gi.tools.run_tool(history.id, self.id, inputs)
        outputs = [history.get_dataset(_["id"]) for _ in out_dict["outputs"]]
        if wait:
            self.gi._wait_datasets(outputs, polling_interval=polling_interval)
        return outputs


class Job(Wrapper):
    """
    Maps to a Galaxy job.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + ("state",)


DatasetContainerPreviewSubtype = TypeVar("DatasetContainerPreviewSubtype", bound="DatasetContainerPreview")


@abstractclass
class DatasetContainerPreview(Wrapper):
    """
    Abstract base class for dataset container (history and library) 'previews'.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "deleted",
        "name",
    )


class LibraryPreview(DatasetContainerPreview):
    """
    Models Galaxy library 'previews'.

    Instances of this class wrap dictionaries obtained by getting
    ``/api/libraries`` from Galaxy.
    """


class HistoryPreview(DatasetContainerPreview):
    """
    Models Galaxy history 'previews'.

    Instances of this class wrap dictionaries obtained by getting
    ``/api/histories`` from Galaxy.
    """

    BASE_ATTRS = DatasetContainerPreview.BASE_ATTRS + (
        "annotation",
        "published",
        "purged",
        "tags",
    )


class WorkflowPreview(Wrapper):
    """
    Models Galaxy workflow 'previews'.

    Instances of this class wrap dictionaries obtained by getting
    ``/api/workflows`` from Galaxy.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "deleted",
        "latest_workflow_uuid",
        "name",
        "number_of_steps",
        "owner",
        "published",
        "show_in_tool_panel",
        "tags",
    )


class InvocationPreview(Wrapper):
    """
    Models Galaxy invocation 'previews'.

    Instances of this class wrap dictionaries obtained by getting
    ``/api/invocations`` from Galaxy.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + (
        "history_id",
        "id",
        "state",
        "update_time",
        "uuid",
        "workflow_id",
    )
    history_id: str
    state: str
    update_time: str
    uuid: str
    workflow_id: str


class JobPreview(Wrapper):
    """
    Models Galaxy job 'previews'.

    Instances of this class wrap dictionaries obtained by getting
    ``/api/jobs`` from Galaxy.
    """

    BASE_ATTRS = Wrapper.BASE_ATTRS + ("state",)