/usr/lib/python3/dist-packages/bioblend/galaxy/histories/__init__.py is in python3-bioblend 0.7.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """
Contains possible interactions with the Galaxy Histories
"""
import os
import re
import time
import six
import bioblend
from bioblend.galaxy.client import Client
class HistoryClient(Client):
def __init__(self, galaxy_instance):
self.module = 'histories'
super(HistoryClient, self).__init__(galaxy_instance)
def create_history(self, name=None):
"""
Create a new history, optionally setting the ``name``.
:type name: str
:param name: Optional name for new history
:rtype: dict
:return: Dictionary containing information about newly created history
"""
payload = {}
if name is not None:
payload['name'] = name
return Client._post(self, payload)
def get_histories(self, history_id=None, name=None, deleted=False):
"""
Get all histories or filter the specific one(s) via the provided
``name`` or ``history_id``. Provide only one argument, ``name`` or
``history_id``, but not both.
If ``deleted`` is set to ``True``, return histories that have been
deleted.
:type history_id: str
:param history_id: Encoded history ID to filter on
:type name: str
:param name: Name of history to filter on
:rtype: list
:return: Return a list of history element dicts. If more than one
history matches the given ``name``, return the list of all the
histories with the given name
"""
if history_id is not None and name is not None:
raise ValueError('Provide only one argument between name or history_id, but not both')
histories = Client._get(self, deleted=deleted)
if history_id is not None:
history = next((_ for _ in histories if _['id'] == history_id), None)
histories = [history] if history is not None else []
elif name is not None:
histories = [_ for _ in histories if _['name'] == name]
return histories
def show_history(self, history_id, contents=False, deleted=None, visible=None, details=None, types=None):
"""
Get details of a given history. By default, just get the history meta
information.
:type history_id: str
:param history_id: Encoded history ID to filter on
:type contents: bool
:param contents: When ``True``, the complete list of datasets in the
given history.
:type deleted: str
:param deleted: Used when contents=True, includes deleted datasets in
history dataset list
:type visible: str
:param visible: Used when contents=True, includes only visible datasets
in history dataset list
:type details: str
:param details: Used when contents=True, includes dataset details. Set
to 'all' for the most information
:type types: str
:param types: ???
:rtype: dict
:return: details of the given history
"""
params = {}
if contents:
if details:
params['details'] = details
if deleted is not None:
params['deleted'] = deleted
if visible is not None:
params['visible'] = visible
if types is not None:
params['types'] = types.join(",")
return Client._get(self, id=history_id, contents=contents, params=params)
def delete_dataset(self, history_id, dataset_id):
"""
Mark corresponding dataset as deleted.
:type history_id: str
:param history_id: Encoded history ID
:type dataset_id: str
:param dataset_id: Encoded dataset ID
"""
url = self.gi._make_url(self, history_id, contents=True)
# Append the dataset_id to the base history contents URL
url = '/'.join([url, dataset_id])
Client._delete(self, payload={}, url=url)
def delete_dataset_collection(self, history_id, dataset_collection_id):
"""
Mark corresponding dataset collection as deleted.
:type history_id: str
:param history_id: Encoded history ID
:type dataset_collection_id: str
:param dataset_collection_id: Encoded dataset collection ID
"""
url = self.gi._make_url(self, history_id, contents=True)
# Append the dataset_id to the base history contents URL
url = '/'.join([url, "dataset_collections", dataset_collection_id])
Client._delete(self, payload={}, url=url)
def show_dataset(self, history_id, dataset_id):
"""
Get details about a given history dataset.
:type history_id: str
:param history_id: Encoded history ID
:type dataset_id: str
:param dataset_id: Encoded dataset ID
"""
url = self.gi._make_url(self, history_id, contents=True)
# Append the dataset_id to the base history contents URL
url = '/'.join([url, dataset_id])
return Client._get(self, url=url)
def show_dataset_collection(self, history_id, dataset_collection_id):
"""
Get details about a given history dataset collection.
:type history_id: str
:param history_id: Encoded history ID
:type dataset_collection_id: str
:param dataset_collection_id: Encoded dataset collection ID
"""
url = self.gi._make_url(self, history_id, contents=True)
url = '/'.join([url, "dataset_collections", dataset_collection_id])
return Client._get(self, url=url)
def show_matching_datasets(self, history_id, name_filter=None):
"""
Get dataset details for matching datasets within a history.
:type history_id: str
:param history_id: Encoded history ID
:type name_filter: str
:param name_filter: Only datasets whose name matches the
``name_filter`` regular expression will be
returned; use plain strings for exact matches and
None to match all datasets in the history
"""
if isinstance(name_filter, six.string_types):
name_filter = re.compile(name_filter + '$')
return [self.show_dataset(history_id, h['id'])
for h in self.show_history(history_id, contents=True)
if name_filter is None or name_filter.match(h['name'])]
def show_dataset_provenance(self, history_id, dataset_id, follow=False):
"""
Get details related to how dataset was created (``id``, ``job_id``,
``tool_id``, ``stdout``, ``stderr``, ``parameters``, ``inputs``,
etc...).
:type history_id: str
:param history_id: Encoded history ID
:type dataset_id: str
:param dataset_id: Encoded dataset ID
:type follow: bool
:param follow: If ``follow`` is ``True``, recursively fetch dataset
provenance information for all inputs and their inputs,
etc...
"""
url = self.gi._make_url(self, history_id, contents=True)
url = '/'.join([url, dataset_id, "provenance"])
return Client._get(self, url=url)
def update_history(self, history_id, name=None, annotation=None, **kwds):
"""
Update history metadata information. Some of the attributes that can be
modified are documented below.
:type history_id: str
:param history_id: Encoded history ID
:type name: str
:param name: Replace history name with the given string
:type annotation: str
:param annotation: Replace history annotation with given string
:type deleted: bool
:param deleted: Mark or unmark history as 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
:rtype: int
:return: status code
"""
kwds['name'] = name
kwds['annotation'] = annotation
return Client._put(self, kwds, id=history_id).status_code
def update_dataset(self, history_id, dataset_id, **kwds):
"""
Update history dataset metadata. Some of the attributes that can be
modified are documented below.
:type history_id: str
:param history_id: Encoded history ID
:type dataset_id: str
:param dataset_id: Id of the dataset
:type name: str
:param name: Replace history dataset name with the given string
: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
:rtype: int
:return: status code
"""
url = self.gi._make_url(self, history_id, contents=True)
# Append the dataset_id to the base history contents URL
url = '/'.join([url, dataset_id])
return Client._put(self, payload=kwds, url=url).status_code
def update_dataset_collection(self, history_id, dataset_collection_id, **kwds):
"""
Update history dataset collection metadata. Some of the attributes that
can be modified are documented below.
:type history_id: str
:param history_id: Encoded history ID
:type dataset_collection_id: str
:param dataset_collection_id: Encoded dataset_collection ID
:type name: str
:param name: Replace history dataset collection name with the given
string
:type deleted: bool
:param deleted: Mark or unmark history dataset collection as deleted
:type visible: bool
:param visible: Mark or unmark history dataset collection as visible
:rtype: int
:return: status code
"""
url = self.gi._make_url(self, history_id, contents=True)
url = '/'.join([url, "dataset_collections", dataset_collection_id])
return Client._put(self, payload=kwds, url=url).status_code
def create_history_tag(self, history_id, tag):
"""
Create history tag
:type history_id: str
:param history_id: Encoded history ID
:type tag: str
:param tag: Add tag to history
:rtype: dict
:return: A dictionary with information regarding the tag.
For example::
{'model_class':'HistoryTagAssociation', 'user_tname': 'NGS_PE_RUN', 'id': 'f792763bee8d277a', 'user_value': None}
"""
# empty payload since we are adding the new tag using the url
payload = {}
# creating the url
url = self.url
url = '/'.join([url, history_id, 'tags', tag])
return Client._post(self, payload, url=url)
def upload_dataset_from_library(self, history_id, lib_dataset_id):
"""
Upload a dataset into the history from a library. Requires the
library dataset ID, which can be obtained from the library
contents.
:type history_id: str
:param history_id: Encoded history ID
:type lib_dataset_id: str
:param lib_dataset_id: Encoded library dataset ID
"""
payload = {
'content': lib_dataset_id,
'source': 'library',
'from_ld_id': lib_dataset_id, # compatibility with old API
}
return Client._post(self, payload, id=history_id, contents=True)
def create_dataset_collection(self, history_id, collection_description):
"""
Create a new dataset collection
:type history_id: str
:param history_id: Encoded history ID
:type collection_description: str
:param collection_description: a description of the dataset collection
"""
try:
collection_description = collection_description.to_dict()
except AttributeError:
pass
payload = dict(
name=collection_description["name"],
type="dataset_collection",
collection_type=collection_description["collection_type"],
element_identifiers=collection_description["element_identifiers"],
)
return Client._post(self, payload, id=history_id, contents=True)
def download_dataset(self, history_id, dataset_id, file_path,
use_default_filename=True):
"""
Download a ``dataset_id`` from history with ``history_id`` to a
file on the local file system, saving it to ``file_path``.
Refer to ``bioblend.galaxy.dataset.DatasetClient.download_dataset()``
for the other available parameters.
"""
meta = self.show_dataset(history_id, dataset_id)
if use_default_filename:
file_local_path = os.path.join(file_path, meta['name'])
else:
file_local_path = file_path
return self.gi.datasets.download_dataset(dataset_id,
file_path=file_local_path,
use_default_filename=False)
def delete_history(self, history_id, purge=False):
"""
Delete a history.
:type history_id: str
:param history_id: Encoded history ID
:type purge: bool
:param purge: if ``True``, also purge (permanently delete) the history
.. note::
For the purge option to work, the Galaxy instance must have the
``allow_user_dataset_purge`` option set to ``True`` in the
``config/galaxy.ini`` configuration file.
"""
payload = {}
if purge is True:
payload['purge'] = purge
return Client._delete(self, payload, id=history_id)
def undelete_history(self, history_id):
"""
Undelete a history
:type history_id: str
:param history_id: Encoded history ID
"""
url = self.gi._make_url(self, history_id, deleted=True)
# Append the 'undelete' action to the history URL
url = '/'.join([url, 'undelete'])
return Client._post(self, payload={}, url=url)
def get_status(self, history_id):
"""
Returns the state of this history
:type history_id: str
:param history_id: Encoded history ID
:rtype: dict
:return: A dict documenting the current state of the history. Has the following keys:
'state' = This is the current state of the history, such as ok, error, new etc.
'state_details' = Contains individual statistics for various dataset states.
'percent_complete' = The overall number of datasets processed to completion.
"""
state = {}
history = self.show_history(history_id)
state['state'] = history['state']
if history.get('state_details') is not None:
state['state_details'] = history['state_details']
total_complete = sum(six.itervalues(history['state_details']))
if total_complete > 0:
state['percent_complete'] = 100 * history['state_details']['ok'] / total_complete
else:
state['percent_complete'] = 0
return state
def get_current_history(self):
"""
Deprecated method.
Just an alias for get_most_recently_used_history().
"""
return self.get_most_recently_used_history()
def get_most_recently_used_history(self):
"""
Returns the current user's most recently used history (not deleted).
"""
url = self.gi._make_url(self, None)
url = '/'.join([url, 'most_recently_used'])
return Client._get(self, url=url)
def export_history(self, history_id, gzip=True, include_hidden=False,
include_deleted=False, wait=False):
"""
Start a job to create an export archive for the given history.
:type history_id: str
:param history_id: history ID
:type gzip: bool
:param gzip: create .tar.gz archive if ``True``, else .tar
:type include_hidden: bool
:param include_hidden: whether to include hidden datasets
in the export
:type include_deleted: bool
:param include_deleted: whether to include deleted datasets
in the export
:type wait: bool
:param wait: if ``True``, block until the export is ready; else, return
immediately
:rtype: str
:return: ``jeha_id`` of the export, or empty if ``wait`` is ``False``
and the export is not ready.
"""
params = {
'gzip': gzip,
'include_hidden': include_hidden,
'include_deleted': include_deleted,
}
url = '%s/exports' % self.gi._make_url(self, history_id)
while True:
r = Client._put(self, {}, url=url, params=params)
if not wait or r.status_code == 200:
break
time.sleep(1)
contents = r.json()
if contents:
jeha_id = contents['download_url'].rsplit('/', 1)[-1]
else:
jeha_id = '' # export is not ready
return jeha_id
def download_history(self, history_id, jeha_id, outf,
chunk_size=bioblend.CHUNK_SIZE):
"""
Download a history export archive. Use :meth:`export_history`
to create an export.
:type history_id: str
:param history_id: history ID
:type jeha_id: str
:param jeha_id: jeha ID (this should be obtained via
:meth:`export_history`)
:type outf: file
:param outf: output file object, open for writing in binary mode
:type chunk_size: int
:param chunk_size: how many bytes at a time should be read into memory
"""
url = '%s/exports/%s' % (
self.gi._make_url(self, module_id=history_id), jeha_id)
r = self.gi.make_get_request(url, stream=True)
r.raise_for_status()
for chunk in r.iter_content(chunk_size):
outf.write(chunk)
|