You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遍历目录中所有.mcool文件多分辨率分析报错求助

问题解决:批量处理多分辨率.mcool文件

报错原因

报错AttributeError: 'list' object has no attribute 'fileops'核心原因有两点:

  1. 批量代码缺少必要的导入语句(cooler和pathlib.Path),若此前代码错误将cooler变量赋值为列表,会直接触发该错误;
  2. 适配cooler标准API:list_coolers方法直接归属cooler模块,而非cooler.fileops子模块。

修正后的批量处理代码

import cooler
from pathlib import Path

data_dir = "./input/"
# 嵌套字典存储结果:{文件名: {分辨率标识: cooler对象/计算结果}}
cooler_results = {}

pathlist = Path(data_dir).glob('**/*.mcool')
for path in pathlist:
    cool_file = str(path)
    # 获取当前mcool文件的所有分辨率路径
    res_paths = cooler.list_coolers(cool_file)
    # 提取分辨率数值
    resolutions = [p.rsplit("/", 1)[1] for p in res_paths]
    
    # 为当前文件创建子容器存储各分辨率数据
    file_specific_data = {}
    for res in resolutions:
        # 加载对应分辨率的cooler实例
        clr = cooler.Cooler(f'{cool_file}::resolutions/{res}')
        # 计算染色体起始位置
        chromstarts = []
        for chrom in clr.chromnames:
            print(f'文件: {path.name} | 分辨率: {res} | {chrom} : {clr.extent(chrom)}')
            chromstarts.append(clr.extent(chrom)[0])
        # 按需求存储为类似clr_1000000的标识
        file_specific_data[f'clr_{res}'] = clr
        file_specific_data[f'chromstarts_{res}'] = chromstarts
    
    cooler_results[path.name] = file_specific_data

# 示例:访问指定文件的指定分辨率结果
# print(cooler_results['A001C019.hg38.nodups.pairs.mcool']['clr_1000000'])
# print(cooler_results['A001C019.hg38.nodups.pairs.mcool']['chromstarts_1000000'])

关键修改说明

  • 补全依赖导入:添加import cooler和from pathlib import Path,确保模块正确加载;
  • 修正API调用:将cooler.fileops.list_coolers改为cooler.list_coolers,匹配cooler官方标准用法;
  • 替代动态变量:用嵌套字典替代全局动态变量clr_1000000,避免变量名冲突,同时更便于后续批量访问结果;
  • 增强输出可读性:打印时增加文件名和分辨率标识,方便区分不同文件的处理结果;
  • 逻辑结构化:将单文件的多分辨率数据统一存入子字典,代码逻辑更清晰易维护。

内容的提问来源于stack exchange,提问作者Anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 17:57:25