forked from ZhihaoXie/BacMet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bacmet_class_summary.py
52 lines (46 loc) · 1.66 KB
/
bacmet_class_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# FileName: bacmet_class_summary.py
# Author: Zhihao Xie \(#^o^)/
# Date: 2018/5/2 16:28
# Version: v1.0.0
# CopyRight: Copyright ©Zhihao Xie, All rights reserved.
# class summary of bacmat output table
import sys
import os
import re
from collections import OrderedDict
from numpy import unique
def main():
if len(sys.argv) < 2:
print("usage: python3 {} <bacmat_out_table> > output".format(sys.argv[0]))
sys.exit(1)
bacmat_out = os.path.abspath(sys.argv[1])
class_sum = OrderedDict()
with open(bacmat_out) as fh:
for line in fh:
if re.search(r"^\s*$|^Query", line):
continue
elif len(line) == 0:
break
else:
fields = line.strip().split("\t")
compounds = fields[6]
if re.search(r'\[.*\]', compounds):
compounds_class = re.findall('\[class:\s?(.+?)\]', compounds)
compounds_class = list(unique(compounds_class))
if len(compounds_class) > 0:
for i in compounds_class:
class_sum.setdefault(i, 0)
class_sum[i] += 1
else:
compounds = compounds.strip('"')
compounds = compounds.strip("'")
compounds = compounds.strip()
class_sum.setdefault(compounds, 0)
class_sum[compounds] += 1
print("Class\tCount")
for key in sorted(class_sum.keys()):
print(key, class_sum[key], sep="\t")
if __name__ == '__main__':
main()