[ PROMPT_NODE_26728 ]
data-management
[ SKILL_DOCUMENTATION ]
# 数据管理
## 概述
Latch 通过云存储抽象 (LatchFile, LatchDir) 和用于组织实验数据的结构化注册表系统提供全面的数据管理。
## 云存储:LatchFile 和 LatchDir
### LatchFile
代表 Latch 云存储系统中的一个文件。
python
from latch.types import LatchFile
# 创建现有文件的引用
input_file = LatchFile("latch:///data/sample.fastq")
# 访问文件属性
file_path = input_file.local_path # 执行时的本地路径
file_remote = input_file.remote_path # 云存储路径
### LatchDir
代表 Latch 云存储系统中的一个目录。
python
from latch.types import LatchDir
# 创建目录引用
output_dir = LatchDir("latch:///results/experiment_1")
# 目录操作
all_files = output_dir.glob("*.bam") # 查找匹配模式的文件
subdirs = output_dir.iterdir() # 列出内容
### 路径格式
Latch 存储使用特殊的 URL 方案:
- **Latch 路径**: `latch:///path/to/file`
- **本地路径**: 在工作流执行期间自动解析
- **S3 路径**: 如果配置正确,可直接使用
### 文件传输
文件会在本地执行环境和云存储之间自动传输:
python
from latch import small_task
from latch.types import LatchFile
@small_task
def process_file(input_file: LatchFile) -> LatchFile:
# 文件自动下载到本地执行环境
local_path = input_file.local_path
# 处理文件
with open(local_path, 'r') as f:
data = f.read()
# 写入输出
output_path = "output.txt"
with open(output_path, 'w') as f:
f.write(processed_data)
# 自动上传回云存储
return LatchFile(output_path, "latch:///results/output.txt")
### Glob 模式
使用模式匹配查找文件:
python
from latch.types import LatchDir
data_dir = LatchDir("latch:///data")
# 查找所有 FASTQ 文件
fastq_files = data_dir.glob("**/*.fastq")
# 查找子目录中的文件
bam_files = data_dir.glob("alignments/**/*.bam")
# 多种模式
results = data_dir.glob("*.{bam,sam}")
## 注册表系统
注册表通过项目、表和记录提供结构化的数据组织。
### 注册表架构
账户/工作区
└── 项目
└── 表
└── 记录
### 使用项目
python
from latch.registry.project import Project
# 获取或创建项目
proje