-
Notifications
You must be signed in to change notification settings - Fork 0
/
proteins2genome.py
72 lines (58 loc) · 2.05 KB
/
proteins2genome.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
from collections import defaultdict as d
from optparse import OptionParser, OptionGroup
# Author: Martin Kapun
######################################################### HELP #########################################################################
usage = "python %prog --input file --output file "
parser = OptionParser(usage=usage)
group = OptionGroup(parser, "< put description here >")
######################################################### CODE #########################################################################
parser.add_option("--input", dest="IN", help="Input file")
parser.add_option("--Tax", dest="TA", help="Output file")
parser.add_option("--TaxList", dest="TL", help="Output file")
parser.add_option("--FreqTH", dest="FT", help="Output file")
(options, args) = parser.parse_args()
parser.add_option_group(group)
TaxHash = d(str)
def load_data(x):
""" import data either from a gzipped or or uncrompessed file or from STDIN"""
import gzip
if x == "-":
y = sys.stdin
elif x.endswith(".gz"):
y = gzip.open(x, "rt", encoding="latin-1")
else:
y = open(x, "r", encoding="latin-1")
return y
for l in open(options.TL, "rt"):
a = l.rstrip().split(",")
if len(a) < 3:
continue
if options.TA not in l:
continue
ID = a[2].replace(" ", "_")
TaxHash[ID] = "_".join([a[0], ID])
Genes = d(int)
SeqHash = d(lambda: d(list))
for l in load_data(options.IN):
if l.startswith(">"):
ID = l.split("[")[-1][:-2].replace(" ", "_")
a = l.rstrip().split()
Gene = "_".join(a[1:-2])
Genes[Gene] += 1
continue
SeqHash[ID][Gene].append(l.rstrip())
for Gene, Count in list(Genes.items()):
if Count / len(SeqHash.keys()) < float(options.FT):
del Genes[Gene]
for ID, v in sorted(SeqHash.items()):
# print(ID)
if ID not in TaxHash:
continue
Seq = []
for Gene, Count in sorted(Genes.items()):
Seq.extend(v[Gene])
if Seq == []:
continue
print(">" + TaxHash[ID])
print("\n".join(Seq))