Galaxy生物信息分析平台的数据集对象清理

2023-07-29,,

由于微信不允许外部链接,你需要点击文章尾部左下角的 "阅读原文",才能访问文中链接。

Galaxy Project 是在云计算背景下诞生的一个生物信息学可视化分析开源项目。该项目由美国国家科学基金会(NSF)、美国国家人类基因组研究所(NHGRI)、哈克生命科学研究所(The Huck Institutes of the Life Sciences)、宾州州立大学网络科学研究所(The Institute for CyberScience at Penn State),以及约翰霍普金斯大学(Johns Hopkins University)提供支持,是目前生物医学研究领域最受欢迎的在线生物信息分析工具之一。

The Galaxy Project is supported in part by NSF, NHGRI, The Huck Institutes of the Life Sciences, The Institute for CyberScience at Penn State, and Johns Hopkins University.

从 2015 年起,Galaxy 源码从 bitbucket 转移至 GitHub 托管,截止 2018 年 10 月该项目在 GitHub 上有 506 个 star,192 个贡献者,51 个 release 发布版本。

基于 Galaxy Project admin 配置文档,今天我们来聊一聊其数据集删除、清理的一些细节。

1
背景

Galaxy 右侧栏历史面板中显示的  Histories,是作为记录保存在 Galaxy 数据库的 History 表中的。 在其他字段中,History 表包括以下内容:

| id | update_time | user_id | deleted | purged |

当用户第一次访问 Galaxy 主页时,会创建一个新的 Galaxy 会话,并将会话信息存储在浏览器 cookie 中。 这时候 Galaxy 将自动创建一个新的历史记录并将其与会话 cookie 相关联。 此时,会话和历史都没有与用户 ID 相关联,因为用户尚未登录。使用 Galaxy 不需要登录,但如果用户登录了,则其用户 ID 将与 Galaxy 会话、以前创建的历史记录相关联。 在这种情况下,下次用户访问 Galaxy 主页并登录时,历史记录面板将显示符合以下所有条件的历史记录:

与他们的用户 ID 相关联

包含其最新更新,先前存储的历史记录

未被删除的(history.deleted字段包含的值为 False)

当用户执行分析时,与数据集相关联的每一个条目将会添加到历史记录中。 这些条目称为 HistoryDatasetAssociation 对象。 与数据集有关的信息存储在 Galaxy 数据库的 Dataset 表中,而数据本身则存储在磁盘上。 Galaxy 配置文件中包含了一个 file_path 的属性,该属性指向存储数据集的磁盘的位置。 在其他列(字段)中,Dataset 表包括以下内容:

| id | update_time | deleted | purged | file_size |

在其他的列字段中,HistoryDatasetAssociation 表包含了以下内容:

| id | update_time | history_id | deleted | dataset_id |

每条 HistoryDatasetAssociation 记录都通过 HistoryDatasetAssociation.history_id 和 HistoryDatasetAssociation.dataset_id 列中的值将数据集 (Dataset) 与历史记录相关联。 任意多个的 HistoryDatasetAssociation 记录都可以指向一个基础的 Dataset 数据集 —— 这就是复制 histories,历史条目和 libraries 库的工作方式,这些都无需复制实际的文件内容。

在其他的列字段中,LibraryDatasetDatasetAssociation 表包含了以下内容:

| id | update_time | library_dataset_id | deleted | dataset_id |

每一条的 LibraryDatasetDatasetAssociation 记录都通过LibraryDatasetDatasetAssociation.library_dataset_id 和 LibraryDatasetDatasetAssociation.dataset_id 列中的值将 Dataset 数据集与可版本化的库数据集记录相关联。 任意数量的 LibraryDatasetDatasetAssociation 记录都可以指向一个基础的 Dataset 数据集。

2
清除不需要的历史,库和数据集

只有在所有 HistoryDatasetAssociations 和 LibraryDatasetDatasetAssociations 都标记为已删除时,一个数据集才能真正被删除(或清除)。

Galaxy 发行版中包含 6 个脚本可用于清除不需要的历史记录,库和数据集。 这些脚本位于 GALAXY_ROOT/scripts/cleanup_datasets 目录中并命名为:

delete_userless_histories.sh

purge_histories.sh

purge_datasets.sh

purge_folders.sh

purge_libraries.sh

delete_datasets.sh

请注意,我们应该在运行脚本之前激活 Galaxy 虚拟环境,如下所示。

source /path/to/galaxy/root/.venv/bin/activate
sh delete_userless_histories.sh
sh purge_histories.sh

这些脚本都在同一目录中执行一个名为 cleanup_datasets.py 的 Python 脚本,在命令行上发送不同的参数值。 如果需要,可以忽略这些脚本,并且可以手动执行 cleanup_datasets.py 脚本(使用 GALAXY_ROOT 作为工作目录),传入所需的参数值。 但是,我们也可以在 cron 中轻松配置这些脚本以自动执行。 脚本执行的顺序将影响结果; 为获得最佳效果,建议的运行顺序为:

    delete_userless_histories.sh

    purge_histories.sh

    purge_libraries.sh

    purge_folders.sh

    purge_datasets.sh

如果希望在删除外部容器(历史记录,库/库文件夹)之前删除数据集,则可以在 purge_datasets.sh 脚本之前使用 delete_datasets.sh 脚本。 此脚本可能需要一些时间才能完成。


Available Flags

以下是有关 cleanup_datasets.py 脚本可以执行的每个函数的更多详细信息。


Deleting Userless Histories

如上所述,使用 Galaxy 默认是不需要登录的,并且在许多情况下,用户执行分析而无需登录以“一次性”方式查看结果,而不关心保留分析以供以后查看。 在这种情况下,创建的历史记录没有关联的用户 ID。cleanup_dataset.py 脚本可用于删除在指定时间段内未更改的这些类型的历史记录。 删除无用户历史记录的命令如下:

python cleanup_datasets.py config/galaxy.ini -d 60 -1

请注意,传递给脚本的第一个参数是 Galaxy 的配置文件。这是脚本获取有关数据库连接和磁盘上数据文件位置的信息所必需的。-d 标志后面的值是从最后一次更新历史记录以来经过的天数(即,在此示例中,History.update_time 列中的值超过 60 天)。根据此条件,将检索所有不包含 user_id 列中的值且其 update_time 列值超过 60 天的历史记录。-1 标志告诉 cleanup_datasets.py 脚本执行名为 delete_userless_histories() 的方法,该方法包含在脚本中。此方法通过将 History.delete 列的值设置为 True 来删除 update_time 值早于指定天数的无用户历史记录。执行相同的命令,但提供 -i 标志不会写入任何更改;本程序执行后将提供有关要删除的历史记录的 info 信息。


Purging Deleted Histories

在历史记录的生命周期中,“已删除”阶段之后的阶段是“已清除”阶段,即历史记录生命周期的最后一个阶段。 清除历史记录时,将清除与历史记录关联的所有 HistoryDatasetAssociation 记录。 清除 HistoryDatasetAssociation 时,会将其标记为已删除。 另外,只有当同时清除了与该数据集的所有关联时,HDA 关联的数据集才会被标记为已删除(HDA 和 LDDA 都标记为已删除);当发生这种情况时,用户不再能够取消使用 HDA 和 LDDA —— 如果使用 “-r”,则会从磁盘中删除关联的文件和元数据文件(主数据集文件仍保留在磁盘上,并且可由管理员检索)。 清除历史记录和相关 HDA 的命令如下:

python cleanup_datasets.py config/galaxy.ini -d 60 -2 -r

在此示例中,-2 标志告诉 cleanup_dataset.py 脚本执行脚本中包含的 purge_histories() 的方法。此方法检索 History.deleted 列值为 TrueHistory.purged 列值为 False 且其 History.update_time 列值早于指定天数(在此示例中为 60 )的所有 History 记录。通过将 HistoryDatasetAssociation.deleted 列值设置为 True,也可以清除与历史记录关联的所有 HistoryDatasetAssociation 记录;轮询任何其他 DatasetAssociations(HDA / LDDA),如果它们都具有 HistoryDatasetAssociation.deleted==True,则 Dataset.deleted 列将被设置为 True。此示例中的 -r 标志告诉 cleanup_dataset.py 脚本从磁盘中删除与 HistoryDatasetAssociation 记录关联的文件(元数据等)(仅当 Dataset.deleted 设置为 True 时)。执行相同的命令但用 -i 替换 -r 标志将打印出所有历史记录和关联的 HistoryDatasetAssociation 记录,如果使用 -r 标志将清除这些记录。


Purging Deleted Datasets

当与数据集关联的所有历史记录和库记录都已经如上面所述,被删除了的时候,数据集记录将进入“已删除”阶段。

删除数据集记录生命周期的下一个阶段是“清除”阶段,即数据集记录生命周期的最后一个阶段。 清除数据集的命令类似于:

python cleanup_datasets.py config/galaxy.ini -d 60 -3 -r

在此示例中,-3 标志告诉 cleanup_dataset.py 脚本执行脚本中包含的 purge_datasets() 方法。 此方法检索 Dataset.deleted 列值为 TrueDataset.purged 为 False 且其 Dataset.update_time 列的值早于指定天数(本示例中为 60 )的所有 Dataset 记录。 通过将 Dataset.purged 列设置为 True 来清除数据集记录。 如上所述,此示例中的 -r 标志告诉 cleanup_dataset.py 脚本从磁盘中删除与数据集记录关联的数据文件。 执行相同的命令但用 -i 替换 -r 标志将打印出在使用 -r 标志时将被清除的所有数据集记录。


Purging Library Folders

Besides existing in user’s history, dataset association objects exist with in 
Library Folders
; they are known as 
LibraryDatasetDatasetAssociations 
(LDDAs). Purging a library folder is similar to purging a History. An example command is:

python cleanup_datasets.py config/galaxy.ini -d 60 -5 -r

In this example, the -5 flag tells the cleanup_dataset.py script to execute the purge_folders() method included in the script. This method retrieves all LibraryFolder records whose LibraryFolder.deleted column value is TrueLibraryFolder.purged column value is False and whose LibraryFolder.update_time column value is older than the specified number of days ( 60 in this example ). It works recursively on all subfolders and their contents. All LibraryDatasetDatasetAssociations records associated with the LibraryFolder record are also purged by setting the LibraryDatasetDatasetAssociation.deleted column value to True; any additional DatasetAssociations (HDA/LDDA) are polled and if they all have HistoryDatasetAssociation(LibraryDatasetDatasetAssociation).deleted==True, the Dataset.deleted column is set to True. The -r flag in this example tells the cleanup_dataset.py script to remove the files (metadata etc) associated with the LibraryDatasetDatasetAssociation record (but not the actual Dataset file) from disk (only if Dataset.deleted is to be set to True). Executing the same command but replacing the -r flag with -i will print out all of the LibraryFolders and associated LibraryDatasetDatasetAssociation records that will be purged if the -r flag is used.

As is the case with 
Histories
, the 
purge_datasets 
script will need to be used to remove actual 
Datasets 
from disk.


Purging Libraries

Purging a library is similar to purging a 
LibraryFolder
. All libraries which are not purged, but are deleted and exceed the modified date are retrieved. The library is marked as purged and 
purge_folder 
is called on the library’s root folder, deleting all contents. An example command is:

python cleanup_datasets.py config/galaxy.ini -d 60 -4 -r

In this example, the -4 flag tells the cleanup_dataset.py script to execute the purge_libraries() method included in the script. This method retrieves all Library records whose  Library.deleted column value is True,  Library.purged column value is False and whose LibraryFolder.update_time column value is older than the specified number of days ( 60 in this example ). The Library’s Root Folder is then obtained and all LibraryDatasetDatasetAssociations records associated with the LibraryFolder record are also purged by setting the LibraryDatasetDatasetAssociation.deleted column value to True; any additional DatasetAssociations (HDA/LDDA) are polled and if they all have HistoryDatasetAssociation(LibraryDatasetDatasetAssociation).deleted==True, the Dataset.deleted column is set to True. The -r flag in this example tells the cleanup_dataset.py script to remove the files (metadata etc) associated with the LibraryDatasetDatasetAssociation record (but not the actual Dataset file) from disk (only if Dataset.deleted is to be set to True). Executing the same command but replacing the -r flag with -i will print out all of the Libraries, LibraryFolders and associated LibraryDatasetDatasetAssociation records that will be purged if the -r flag is used.


Deleting Datasets / Purging Dataset Instances

有时,在清除数据集实例并将基础数据集标记为已删除之前,不希望等待包含的历史记录或库/库文件夹被删除。-6 标志用于此目的。 此脚本将查找未删除但与 DatasetAssociation 相关联的所有数据集记录,该 DatasetAssociation 被标记为已删除并根据指定的截止值进行更新。 如果所有关联都标记为已删除,则 Dataset 将标记为已删除,并清除每个 DatasetAssociation。 之后需要运行 purge_datasets.s脚本以从磁盘中删除基础数据集。

An example command is:

python cleanup_datasets.py config/galaxy.ini -d 60 -6 -r

更多关于 Galaxy (release_18.05) 的数据集对象清理与实际操作,可以点击文章左下角 "阅读原文" 登陆博客查看。

·end·

—如果喜欢,快分享给你的朋友们吧—

我们一起愉快的玩耍吧

本文分享自微信公众号 - 生信科技爱好者(bioitee)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

Galaxy生物信息分析平台的数据集对象清理的相关教程结束。

《Galaxy生物信息分析平台的数据集对象清理.doc》

下载本文的Word格式文档,以方便收藏与打印。