按分类法过滤 STRING PPI 转储

最近我需要按分类法 ID 过滤 STRING 蛋白质视图数据库转储(例如 protein.links.full.v9.05.txt.gz)。原始数据集太大(它有超过 6.7 亿条记录)。

为了以恒定内存过滤(毕竟,完整的 STRING 转储有 47GB 大),我创建了这个脚本,允许过滤匹配给定生物体(NCBI 分类法 ID)的二元 PPI,也允许过滤至少一个相互作用蛋白质属于给定生物体的二元 PPI。通常这对 STRING 来说没有真正区别。

输入和输出文件可以是明文或 gzip 压缩的(启用透明压缩/解压缩)。

示例:

filter_protein_links_example.sh
#过滤 Saccharomyces cerevisiae 中的两个相互作用蛋白质
./filter-protein-links.py protein.links.full.v9.05.txt.gz output.txt.gz --filter-organism=4932 --match-both

源代码:

filter_protein_links.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
filter-protein-links.py

按分类法过滤 STRING 蛋白质链接转储
"""
from __future__ import with_statement
import gzip

__author__ = "Uli Koehler"
__license__ = "Apache v2.0"
__copyright__ = "Copyright 2013, Uli Koehler"

def filterSTRINGFile(infilename, outfilename, organismFilter, mustMatchBothOrganisms=True):
    inOpenFunc = gzip.open if infilename.endswith(".gz") else open
    outOpenFunc = gzip.open if outfilename.endswith(".gz") else open
    allRecordsCounter = 0
    passCounter = 0
    with inOpenFunc(infilename) as infile, outOpenFunc(outfilename, "w") as outfile:
        for line in infile:
            if line.startswith("protein1"):
                outfile.write(line)
                continue #跳过标题行
            allRecordsCounter += 1
            if allRecordsCounter % 1000000 == 0:
                print ("Processed {} input records, {} records passed test...".format(
                        allRecordsCounter, passCounter))
            parts = line.split()
            #检查生物体
            organismA = parts[0].partition(".")[0]
            organismB = parts[1].partition(".")[0]
            if mustMatchBothOrganisms and (organismA != organismFilter or organismB != organismFilter):
                continue
            if (not mustMatchBothOrganisms) and (organismA != organismFilter and organismB != organismFilter):
                continue
            #所有测试通过,写入行
            passCounter += 1
            outfile.write(line)
    #打印最终统计
    print ("Processed {} input records, {} records passed test".format(allRecordsCounter, passCounter))

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--filter-organism", type=int, required=True, dest="filterOrganism", help="两个蛋白质必须匹配此生物体 ID")
    parser.add_argument("--match-both", action="store_true", dest="matchBoth", help="指定此标志")
    parser.add_argument("infile", help="输入文件(支持 .gz)")
    parser.add_argument("outfile", help="输出文件(支持 .gz)")
    args = parser.parse_args()
    filterSTRINGFile(args.infile, args.outfile, str(args.filterOrganism), args.matchBoth)

Check out similar posts by category: Allgemein