Repository navigation
Expand file tree
/
Copy pathbuild_conversions.py
More file actions
216 lines (198 loc) · 6.43 KB
/
Copy pathbuild_conversions.py
File metadata and controls
216 lines (198 loc) · 6.43 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
###############################################################################
###############################################################################
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
###############################################################################
###############################################################################
import re
def conversion(conversion_file):
"""
Description: Generic dictionary creator for database conversions
Input: Conversion File
Return: Dictionary
"""
conv_dt = {}
conversion = open(conversion_file)
for line in conversion:
line = line.strip().split()
first = line[0].split(":")[1]
second = line[1].split(":")[1]
conv_dt[first] = second
return conv_dt
def conversion_nogf(conversion_file):
"""
Description: eggNog -> eggNog function dictionary creator for
database conversions
Input: Conversion File
Return: Dictionary
"""
conv_dt = {}
conversion = open(conversion_file)
for line in conversion:
line = line.strip().split("\t")
first = line[0][3:]
second = line[1]
conv_dt[first] = second
return conv_dt
def conversion_contig_blastnr(nr_file, lengths):
"""
Description: Generates NR BlastX Best Words conversion file and
the BlastX Top Hit conversion file
Input: BlastX NR File, Contig Length
Return: Dictionary
"""
if lengths == 0:
return {}
file = open(nr_file)
prev = ''
conv_dt = {}
contig_blasts = []
for line in file:
row = line.strip('\n').split('\t')
if prev == row[0]:
contig_blasts.append(line)
else:
if contig_blasts != []:
best = find_best_words(contig_blasts, lengths[prev])
conv_dt[best[0]] += [best[1]]
prev = row[0]
contig_blasts = [line]
conv_dt[row[0]] = [row[1], row[3]]
best = find_best_words(contig_blasts, lengths[prev])
conv_dt[best[0]] += [best[1]]
file.close()
return conv_dt
def find_best_words(contig_blasts, length):
"""
Description: Finds the Best Words match per contig
Input: Set of hits for contig, Contig Length
Return: Best Words Contig
"""
badwords = ['and', 'or', 'to' 'similar', 'protein', 'predicted', 'hypothetical', 'quality']
#stuff
weights = {}
words_per_contig = []
for line in contig_blasts:
line = line.strip('\n').split('\t')
percent_identity = float(line[2])
query_coverage = float(line[3]) / length
words = []
for word in line[12].split():
if '[' in word:
break
word = word.lower().replace(',','').replace(':','').replace('-like','')
if word in badwords:
continue
if words:
if (re.compile('^[a-zA-z]{1}$').match(word) or re.compile('^[0-9]+$').match(word)) or 'isoform' in words[-1] and 'x' in word:
words[-1] = words[-1] + ' ' + word
else:
words.append(word)
for word in words:
try:
weights[word] += percent_identity * query_coverage
except:
weights[word] = percent_identity * query_coverage
words_per_contig.append(words)
best = 0
best_index = 0
for index, line in enumerate(words_per_contig):
score = sum([weights[word] for word in line])
if best < score:
best = score
best_index = index
return contig_blasts[best_index].strip('\n').split('\t')
"""Question for Tessa: Need to account for duplicate contigs?"""
def conversion_contig_closest(conversion_file):
"""
Description: Contig -> Closest Hit dictionary creator for
database conversions
Input: Conversion File
Return: Dictionary
"""
conv_dt = {}
conversion = open(conversion_file)
prev = ""
for line in conversion:
line = line.strip().split("\t")
if prev == line[0]:
continue
first = line[0]
second = line[1]
###
#Blast Length
#third = line[14]
third = '.'
###
conv_dt[first] = (second, third)
prev = first
return conv_dt
def conversion_ez_path(conversion_file):
"""
Description: Enzyme -> Kegg Pathway dictionary creator for
database conversions
Input: Conversion File
Return: Dictionary
"""
conv_dt = {}
conversion = open(conversion_file)
for line in conversion:
line = line.strip().split("\t")
if line[2] == 'original':
first = line[0].split(":")[1]
second = line[1].split(":")[1]
conv_dt[first] = second.replace(",", ";")
return conv_dt
def conversion_goslim(conversion_file):
"""
Description: GO -> GO Slim dictionary creator for database
conversions
Input: Conversion File
Return: Dictionary
"""
conv_dt = {}
conversion = open(conversion_file)
for line in conversion:
if line[0:7] == "id: GO:":
conv_dt[line[4:].strip()] = line[4:].strip()
return conv_dt
def conversion_idmap(conversion_file):
"""
Description: Generic dictionary creator for IDMAP database
conversions
Input: Conversion File
Return: Dictionary
"""
conv_dt = {}
conversion = open(conversion_file)
for line in conversion:
line = line.strip("\n").split("\t")
conv_dt[line[0]] = line[2].replace(",", ";")
return conv_dt
def conversion_go_entrez(conversion_file):
"""
Description: SwissProt -> GO & Entrez dictionary creator for
database conversions
Input: Conversion File
Return: Dictionary
"""
conv_dt = {}
conversion = open(conversion_file)
for line in conversion:
line = line.split("\t")
temp = line[6].replace(" ", "")
conv_dt[line[0]] = (temp, line[2])
return conv_dt