forked from enormandeau/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblast_get_best_results.py
executable file
·152 lines (130 loc) · 4.55 KB
/
blast_get_best_results.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# best_blast_results.py
# Finding the best blast result
__authors__ = "Eric Normandeau"
__program_name__ = "best_blast_results"
__version_info__ = ('0', '0', '1')
__version__ = '.'.join(__version_info__)
__revision_date__ = "2010-05-18"
# Module imports
import getopt
import sys
import platform
import re
import itertools
import math
# Function definitions
def best_blast_results(in_file, out_file):
output = ""
penalty = dict([
("unnamed", 200),
("unknown", 30),
("uncharacterized", 30),
("hypothetical", 20),
("predicted", 10),
("similar to", 5),
("novel protein", 5)
])
with open(in_file) as f:
line_counter = 1
for line in f:
line_counter += 1
if line.strip() == "":
continue
data = line.strip().split("\t")
name = data.pop(0)
results = itertools.izip(*[itertools.islice(data, i, None, 3)
for i in range(3)])
best_result = ["NoID", "No hits found", 1]
for result in results:
if result[0].find("No hits found") >= 0:# or result[0] == "":
print("#####" + result[0])
continue
print(result[2], float(result[2]))
evalue = float(result[2])
best_evalue = float(best_result[2])
result = list(result)
if evalue == 0:
evalue = 1e-200
for k in penalty:
if k in result[1].lower():
evalue = evalue * math.pow(10, penalty[k])
if k in best_result[1].lower():
best_evalue = best_evalue * math.pow(10, penalty[k])
if evalue > 1:
evalue = 1
if evalue < best_evalue:
best_result = result
best_result[2] = str(best_result[2])
output += name + "\t" + "\t".join(best_result) + "\n"
with open(out_file, "w") as f:
f.write(output)
def help():
_plateform = platform.system()
name = __program_name__
text = """
%s(1) User Commands %s(1)
\033[1mNAME\033[0m
\t%s - Keep the best blast result for many contigs
\033[1mSYNOPSIS\033[0m
\t\033[1mpython %s.py \033[0m[\033[4mOPTION\033[0m]... [\033[4mFILE\033[0m]...
\033[1mDESCRIPTION\033[0m
\tKeep best blast result based on evalue.
\t%s takes a file with blast results and returns the best
\tblast result for each contig, based on the blast evalue and
\tmeaningfullness of the gene name. (eg: rejects gene names containing
\t"Unnamed", "Unknown"...)
\033[1mOPTIONS\033[0m
\t\033[1m-h, --help\033[0m
\t\tDisplay the manual of this program
\t\033[1m-i, --input\033[0m
\t\tInput file in tabulated text format
\t\tNote: There can be an indeterminate number of blast results
\t\tfor one contig, as long as they are presented in the following
\t\tformat:
\t\tInput format:
\t\tContigName GeneID GeneName Evalue ... [groups of 3 col.]
\t\tContig_1 NA6524.1 ACo-A 9.00E-042
\t\tContig_2 PG4412.1 Putative 3.20E-012
\t\t...
\t\tContig_134 XF0893.1 Zinc fing 1.50E-035
\t\033[1m-o, --output\033[0m
\t\tOutput file in tabulated text format
\033[1mAUTHORS\033[0m
\t%s
%s %s %s %s(1)
"""%(name, name, name, name, name, __authors__, name, __version__, __revision_date__, name)
if _plateform != 'Windows' and "this is cool":
print(text)
else:
remove = ["\033[1m","\033[0m","\033[4m"]
for i in remove:
text = text.replace(i, "")
print(text)
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help",
"input=", "output="])
except getopt.GetoptError, e:
print("Input error. Use -h for help")
sys.exit(0)
for option, value in opts:
if option in ('-h', '--help'):
help()
sys.exit(0)
elif option in ('-i', '--input'):
input_file = value
output_file = input_file.replace(".txt", "") + "_results.txt"
elif option in ('-o', '--output'):
output_file = value
try:
with open(input_file) as test:
pass
except:
print("Input Error: No ACE file specified or file not found.")
print("Use -h for help.")
sys.exit(0)
best_blast_results(input_file, output_file)
if __name__ == "__main__":
main()