kraken2生成的biom文件处理

使用kraken2软件做宏基因组或扩增子项目物种注释项目的时候,如果后续不使用bracken则可能会遇到一个问题,结果生成的biom文件中taxonomy并不是最终结果,比如下面这样 可以用脚本来处理,删...

使用kraken2软件做宏基因组或扩增子项目物种注释项目的时候,如果后续不使用bracken则可能会遇到一个问题,结果生成的biom文件中taxonomy并不是最终结果,比如下面这样

attachments-2023-05-QhAmIyGx6477134a8dbb4.png

可以用脚本来处理,删除未具体命名的分类级别

import argparse
from biom import load_table, Table
import numpy as np

def main():
    # Parse command-line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--input', help='Input biom file', required=True)
    parser.add_argument('-o', '--output', help='Output biom file', required=True)
    args = parser.parse_args()

    # Load the BIOM table
    table = load_table(args.input)

    # Get the OTU IDs and the data
    otu_ids = table.ids(axis='observation')
    data = table.matrix_data

    # Get the taxonomy metadata
    metadata = table.metadata(axis='observation')

    new_metadata = []
    for otu_id, meta in zip(otu_ids, metadata):
        if meta is not None and 'taxonomy' in meta:
            # Filter out unnamed taxonomic ranks
            taxonomy = [level for level in meta['taxonomy'] if not level.endswith('__')]
            meta['taxonomy'] = taxonomy
        new_metadata.append(meta)

    # Create a new BIOM table with the new metadata
    new_table = Table(data, otu_ids, table.ids(), new_metadata, table.metadata())

    # Write the new BIOM table to a file
    with open(args.output, 'w') as f:
        f.write(new_table.to_json("Filtered Taxonomy"))

if __name__ == "__main__":
    main()
  • 发表于 2023-05-31 17:33
  • 阅读 ( 703 )
  • 分类:宏基因组

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
xun
xun

电路元件工程师

66 篇文章

作家榜 »

  1. omicsgene 654 文章
  2. 安生水 325 文章
  3. Daitoue 167 文章
  4. 生物女学霸 120 文章
  5. 红橙子 78 文章
  6. CORNERSTONE 72 文章
  7. rzx 67 文章
  8. xun 66 文章