wwpdb.utils.seqdb-v2 0.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wwpdb/utils/seqdb_v2/BlastProcess.py +676 -0
- wwpdb/utils/seqdb_v2/FastaUtil.py +141 -0
- wwpdb/utils/seqdb_v2/FetchNcbiXml.py +140 -0
- wwpdb/utils/seqdb_v2/FetchUniProtEntry.py +96 -0
- wwpdb/utils/seqdb_v2/FetchUnpXml.py +323 -0
- wwpdb/utils/seqdb_v2/NcbiBlastService.py +176 -0
- wwpdb/utils/seqdb_v2/ReadNcbiBlastXml.py +233 -0
- wwpdb/utils/seqdb_v2/ReadNcbiSummary.py +128 -0
- wwpdb/utils/seqdb_v2/ReadNcbiXml.py +214 -0
- wwpdb/utils/seqdb_v2/ReadUnpBlastXml.py +266 -0
- wwpdb/utils/seqdb_v2/ReadUnpXml.py +722 -0
- wwpdb/utils/seqdb_v2/UnpBlastService.py +144 -0
- wwpdb/utils/seqdb_v2/__init__.py +5 -0
- wwpdb/utils/seqdb_v2/mmCIFUtil.py +64 -0
- wwpdb_utils_seqdb_v2-0.13.dist-info/METADATA +67 -0
- wwpdb_utils_seqdb_v2-0.13.dist-info/RECORD +17 -0
- wwpdb_utils_seqdb_v2-0.13.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
##
|
|
2
|
+
# File: BlastProcess.py
|
|
3
|
+
# Date: 25-April-2010
|
|
4
|
+
# Author: Zukang Feng
|
|
5
|
+
#
|
|
6
|
+
# Updates:
|
|
7
|
+
# 27-Apr-2010 jdw add method to run and save by entity to a named output file.
|
|
8
|
+
# 31-Dec-2012 jdw migrate taxonomy extensions to V2 and consolidate batch fetch of UniProt entries.
|
|
9
|
+
# 2-Jan-2013 jdw add additional fragment tracking items -
|
|
10
|
+
##
|
|
11
|
+
|
|
12
|
+
__author__ = "Zukang Feng"
|
|
13
|
+
__email__ = "zfeng@rcsb.rutgers.edu"
|
|
14
|
+
__version__ = "V0.001"
|
|
15
|
+
|
|
16
|
+
import getopt
|
|
17
|
+
import gzip
|
|
18
|
+
import operator
|
|
19
|
+
import os
|
|
20
|
+
import re
|
|
21
|
+
import sys
|
|
22
|
+
import time
|
|
23
|
+
import traceback
|
|
24
|
+
|
|
25
|
+
from mmcif.api.DataCategory import DataCategory
|
|
26
|
+
from mmcif.api.PdbxContainers import DataContainer
|
|
27
|
+
from mmcif.io.PdbxWriter import PdbxWriter
|
|
28
|
+
|
|
29
|
+
from wwpdb.utils.seqdb_v2.mmCIFUtil import mmCIFUtil
|
|
30
|
+
from wwpdb.utils.seqdb_v2.NcbiBlastService import NcbiBlastService
|
|
31
|
+
from wwpdb.utils.seqdb_v2.ReadNcbiBlastXml import ReadNcbiBlastXmlString
|
|
32
|
+
from wwpdb.utils.seqdb_v2.ReadUnpBlastXml import ReadUnpBlastXmlString
|
|
33
|
+
from wwpdb.utils.seqdb_v2.UnpBlastService import UnpBlastService
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class RunBlastPerSeq:
|
|
37
|
+
"""Run blast search and save result into cif file"""
|
|
38
|
+
|
|
39
|
+
def __init__(self, entityId=None, entityInfo=None, taxonomyData=None, verbose=False, log=sys.stderr):
|
|
40
|
+
self.__verbose = verbose
|
|
41
|
+
self.__lfh = log
|
|
42
|
+
self.__entity = entityId
|
|
43
|
+
self.__sequence = ""
|
|
44
|
+
self.__type = ""
|
|
45
|
+
self.__taxonomyData = taxonomyData
|
|
46
|
+
self.__result = []
|
|
47
|
+
self.__saveBlastResults = False
|
|
48
|
+
self.__blastFileNamePrefix = None
|
|
49
|
+
self.__blastPath = "."
|
|
50
|
+
if entityInfo:
|
|
51
|
+
self.__sequence = entityInfo["seq"]
|
|
52
|
+
self.__type = entityInfo["type"]
|
|
53
|
+
self.__fragmentList = entityInfo["fragments"]
|
|
54
|
+
|
|
55
|
+
def saveBlastResults(self, blastPath=".", blastFileNamePrefix="test"):
|
|
56
|
+
self.__saveBlastResults = True
|
|
57
|
+
self.__blastPath = blastPath
|
|
58
|
+
self.__blastFileNamePrefix = blastFileNamePrefix
|
|
59
|
+
|
|
60
|
+
def GetResult(self):
|
|
61
|
+
return self.__result
|
|
62
|
+
|
|
63
|
+
def Run(self):
|
|
64
|
+
"""Manage the reference sequence database search for each fragment of the input
|
|
65
|
+
sequence.
|
|
66
|
+
|
|
67
|
+
Return sorted results using taxonomy data if this is avalable.
|
|
68
|
+
"""
|
|
69
|
+
if (not self.__sequence) or (not self.__fragmentList):
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
for fragId, fragment in enumerate(self.__fragmentList):
|
|
73
|
+
indFrag = fragId + 1
|
|
74
|
+
start = int(fragment["beg"]) - 1
|
|
75
|
+
end = int(fragment["end"])
|
|
76
|
+
sequence = self.__sequence[start:end]
|
|
77
|
+
|
|
78
|
+
if self.__saveBlastResults:
|
|
79
|
+
bPath = os.path.join(
|
|
80
|
+
self.__blastPath,
|
|
81
|
+
self.__blastFileNamePrefix + "_seqdb-blast_E" + self.__entity + "_F " + str(indFrag) + ".xml",
|
|
82
|
+
)
|
|
83
|
+
else:
|
|
84
|
+
bPath = None
|
|
85
|
+
|
|
86
|
+
result = self._Run(sequence, self.__type, blastFilePath=bPath)
|
|
87
|
+
if not result:
|
|
88
|
+
continue
|
|
89
|
+
for match in result:
|
|
90
|
+
match["beg_seq_num"] = fragment["beg"]
|
|
91
|
+
match["end_seq_num"] = fragment["end"]
|
|
92
|
+
match["fragment_id"] = indFrag
|
|
93
|
+
|
|
94
|
+
#
|
|
95
|
+
# re-sorting result
|
|
96
|
+
#
|
|
97
|
+
taxids = {}
|
|
98
|
+
if "taxid" in fragment:
|
|
99
|
+
taxids = self._GetTaxonomyTree(fragment["taxid"])
|
|
100
|
+
auth_accession_code = None
|
|
101
|
+
if "accession" in fragment:
|
|
102
|
+
auth_accession_code = fragment["accession"]
|
|
103
|
+
self._SortResult(result, taxids, auth_accession_code)
|
|
104
|
+
|
|
105
|
+
self.__result.append(result)
|
|
106
|
+
|
|
107
|
+
def _Run(self, sequence, rtype, blastFilePath=None):
|
|
108
|
+
"""Internal method to execute the sequence search according to input polymer type."""
|
|
109
|
+
if self.__verbose:
|
|
110
|
+
self.__lfh.write("+INFO (RunBlastPerSeq._Run) Launch search for sequence = %s\n" % sequence)
|
|
111
|
+
self.__lfh.write("+INFO (RunBlastPerSeq._Run) Launch search for sequence type = %s\n" % rtype)
|
|
112
|
+
timeBegin = time.time()
|
|
113
|
+
blast_match_result = []
|
|
114
|
+
|
|
115
|
+
# run uniprot blast service for protein sequence
|
|
116
|
+
if rtype == "polypeptide":
|
|
117
|
+
service = UnpBlastService(sequence)
|
|
118
|
+
service.RunService()
|
|
119
|
+
# fetch the raw XML result from the Blast search
|
|
120
|
+
xmlResult = service.GetResult()
|
|
121
|
+
if blastFilePath is not None:
|
|
122
|
+
service.WriteResultFile(blastFilePath)
|
|
123
|
+
timeBlast = time.time()
|
|
124
|
+
if self.__verbose:
|
|
125
|
+
self.__lfh.write(
|
|
126
|
+
"+INFO (RunBlastPerSeq._Run) Blast search completed in %d seconds\n" % (timeBlast - timeBegin)
|
|
127
|
+
)
|
|
128
|
+
if xmlResult:
|
|
129
|
+
blastresult = ReadUnpBlastXmlString(xmlResult, verbose=self.__verbose, log=self.__lfh)
|
|
130
|
+
blast_match_result = blastresult.GetResult()
|
|
131
|
+
|
|
132
|
+
# run ncbi blast service for RNA sequence
|
|
133
|
+
elif rtype == "polyribonucleotide":
|
|
134
|
+
service = NcbiBlastService(sequence)
|
|
135
|
+
service.RunService()
|
|
136
|
+
# fetch the raw XML result from the Blast search
|
|
137
|
+
xmlResult = service.GetResult()
|
|
138
|
+
if blastFilePath is not None:
|
|
139
|
+
service.WriteResultFile(blastFilePath)
|
|
140
|
+
timeBlast = time.time()
|
|
141
|
+
if self.__verbose:
|
|
142
|
+
self.__lfh.write(
|
|
143
|
+
"+INFO (RunBlastPerSeq._Run) Blast search completed in %d seconds\n" % (timeBlast - timeBegin)
|
|
144
|
+
)
|
|
145
|
+
if xmlResult:
|
|
146
|
+
blastresult = ReadNcbiBlastXmlString(xmlResult)
|
|
147
|
+
blast_match_result = blastresult.GetResult()
|
|
148
|
+
|
|
149
|
+
else:
|
|
150
|
+
self.__lfh.write("+INFO (RunBlastPerSeq._Run) Search failed for unknown type = %s\n" % rtype)
|
|
151
|
+
|
|
152
|
+
timeProc = time.time()
|
|
153
|
+
if self.__verbose:
|
|
154
|
+
if self.__verbose:
|
|
155
|
+
self.__lfh.write(
|
|
156
|
+
"+INFO (RunBlastPerSeq._Run) Search processing completed in %d seconds\n" % (timeProc - timeBegin)
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
self.__lfh.write("+INFO (RunBlastPerSeq._Run) Result length = %d\n" % len(blast_match_result))
|
|
160
|
+
|
|
161
|
+
return blast_match_result
|
|
162
|
+
|
|
163
|
+
def _GetTaxonomyTree(self, taxid):
|
|
164
|
+
rdict = {}
|
|
165
|
+
rdict["id"] = taxid
|
|
166
|
+
if self.__taxonomyData: # noqa: SIM102
|
|
167
|
+
if taxid in self.__taxonomyData:
|
|
168
|
+
parent_id = self.__taxonomyData[taxid]
|
|
169
|
+
rdict["p_id"] = parent_id
|
|
170
|
+
if parent_id in self.__taxonomyData:
|
|
171
|
+
gparent_id = self.__taxonomyData[parent_id]
|
|
172
|
+
rdict["gp_id"] = gparent_id
|
|
173
|
+
return rdict
|
|
174
|
+
|
|
175
|
+
def _SortResult(self, result, taxids, auth_accession_code):
|
|
176
|
+
"""Add a sorting index to the dictionary of sequence correspondence results obtained from
|
|
177
|
+
the Blast search. The sort index is based on heurestic which includes sequence matching
|
|
178
|
+
metrics and taxonomy data.
|
|
179
|
+
|
|
180
|
+
"""
|
|
181
|
+
if not result:
|
|
182
|
+
return
|
|
183
|
+
|
|
184
|
+
sorting_list = []
|
|
185
|
+
for i in range(len(result)):
|
|
186
|
+
identity = (int(result[i]["identity"]) - int(result[i]["gaps"])) * 100 / int(result[i]["query_length"])
|
|
187
|
+
if result[i]["db_name"] == "SP":
|
|
188
|
+
identity += 2
|
|
189
|
+
if auth_accession_code and (
|
|
190
|
+
result[i]["db_code"] == auth_accession_code or result[i]["db_accession"] == auth_accession_code
|
|
191
|
+
):
|
|
192
|
+
identity += 1
|
|
193
|
+
target_taxids = {}
|
|
194
|
+
if "taxonomy_id" in result[i]:
|
|
195
|
+
target_taxids = self._GetTaxonomyTree(result[i]["taxonomy_id"])
|
|
196
|
+
taxid_match = 0
|
|
197
|
+
if taxids and target_taxids:
|
|
198
|
+
if taxids["id"] == target_taxids["id"]:
|
|
199
|
+
taxid_match = 3
|
|
200
|
+
elif ("p_id" in taxids and taxids["p_id"] == target_taxids["id"]) or (
|
|
201
|
+
"p_id" in target_taxids and target_taxids["p_id"] == taxids["id"]
|
|
202
|
+
):
|
|
203
|
+
taxid_match = 2
|
|
204
|
+
elif ("gp_id" in taxids and taxids["gp_id"] == target_taxids["id"]) or (
|
|
205
|
+
"gp_id" in target_taxids and target_taxids["gp_id"] == taxids["id"]
|
|
206
|
+
):
|
|
207
|
+
taxid_match = 1
|
|
208
|
+
identity = identity * 4 + taxid_match
|
|
209
|
+
ilist = [identity, i]
|
|
210
|
+
sorting_list.append(ilist)
|
|
211
|
+
sorting_list.sort(key=operator.itemgetter(0), reverse=True)
|
|
212
|
+
for i in range(len(sorting_list)):
|
|
213
|
+
result[sorting_list[i][1]]["sort_order"] = str(i + 1)
|
|
214
|
+
|
|
215
|
+
def WriteCifFile(self, EntryID, fileName):
|
|
216
|
+
"""Export the reference sequence matching data to a CIF data file."""
|
|
217
|
+
if not self.__result:
|
|
218
|
+
return
|
|
219
|
+
|
|
220
|
+
curContainer = DataContainer("match_entity")
|
|
221
|
+
t = DataCategory("info")
|
|
222
|
+
t.appendAttribute("struct_id")
|
|
223
|
+
t.appendAttribute("entity_id")
|
|
224
|
+
t.appendAttribute("sequence")
|
|
225
|
+
t.appendAttribute("fragment_count")
|
|
226
|
+
t.setValue(EntryID, "struct_id", 0)
|
|
227
|
+
t.setValue(self.__entity, "entity_id", 0)
|
|
228
|
+
t.setValue(self._FormatSequence(self.__sequence), "sequence", 0)
|
|
229
|
+
t.setValue(len(self.__fragmentList), "fragment_count", 0)
|
|
230
|
+
|
|
231
|
+
curContainer.append(t)
|
|
232
|
+
|
|
233
|
+
_table_items = []
|
|
234
|
+
_table_items.append("id")
|
|
235
|
+
_table_items.append("beg_seq_num")
|
|
236
|
+
_table_items.append("end_seq_num")
|
|
237
|
+
_table_items.append("sort_order")
|
|
238
|
+
_table_items.append("db_name")
|
|
239
|
+
_table_items.append("db_code")
|
|
240
|
+
_table_items.append("db_accession")
|
|
241
|
+
_table_items.append("match_length")
|
|
242
|
+
_table_items.append("queryFrom")
|
|
243
|
+
_table_items.append("queryTo")
|
|
244
|
+
_table_items.append("hitFrom")
|
|
245
|
+
_table_items.append("hitTo")
|
|
246
|
+
_table_items.append("identity")
|
|
247
|
+
_table_items.append("positive")
|
|
248
|
+
_table_items.append("gaps")
|
|
249
|
+
_table_items.append("alignLen")
|
|
250
|
+
_table_items.append("query")
|
|
251
|
+
_table_items.append("subject")
|
|
252
|
+
_table_items.append("midline")
|
|
253
|
+
_table_items.append("query_length")
|
|
254
|
+
_table_items.append("name")
|
|
255
|
+
_table_items.append("source_scientific")
|
|
256
|
+
_table_items.append("source_common")
|
|
257
|
+
_table_items.append("taxonomy_id")
|
|
258
|
+
_table_items.append("gene")
|
|
259
|
+
_table_items.append("synonyms")
|
|
260
|
+
_table_items.append("comments")
|
|
261
|
+
_table_items.append("keyword")
|
|
262
|
+
_table_items.append("ec")
|
|
263
|
+
_table_items.append("fragment_id")
|
|
264
|
+
|
|
265
|
+
t = DataCategory("match_entity")
|
|
266
|
+
for item in _table_items:
|
|
267
|
+
t.appendAttribute(item)
|
|
268
|
+
|
|
269
|
+
t1 = DataCategory("org_sequence")
|
|
270
|
+
t1.appendAttribute("id")
|
|
271
|
+
t1.appendAttribute("sequence")
|
|
272
|
+
|
|
273
|
+
row = 0
|
|
274
|
+
for fragment_result in self.__result:
|
|
275
|
+
for match in fragment_result:
|
|
276
|
+
t.setValue(str(row + 1), "id", row)
|
|
277
|
+
for item in _table_items:
|
|
278
|
+
if item in match:
|
|
279
|
+
t.setValue(str(match[item]), item, row)
|
|
280
|
+
|
|
281
|
+
t1.setValue(str(row + 1), "id", row)
|
|
282
|
+
if "sequence" in match:
|
|
283
|
+
seq = re.sub("[\t \n]", "", match["sequence"])
|
|
284
|
+
t1.setValue(self._FormatSequence(str(seq)), "sequence", row)
|
|
285
|
+
|
|
286
|
+
row += 1
|
|
287
|
+
|
|
288
|
+
curContainer.append(t)
|
|
289
|
+
curContainer.append(t1)
|
|
290
|
+
|
|
291
|
+
myDataList = []
|
|
292
|
+
myDataList.append(curContainer)
|
|
293
|
+
ofh = open(fileName, "w")
|
|
294
|
+
pdbxW = PdbxWriter(ofh)
|
|
295
|
+
pdbxW.write(myDataList)
|
|
296
|
+
ofh.close()
|
|
297
|
+
|
|
298
|
+
@staticmethod
|
|
299
|
+
def _FormatSequence(sequence):
|
|
300
|
+
num_per_line = 60
|
|
301
|
+
lines = int(len(sequence) / num_per_line)
|
|
302
|
+
x = len(sequence) % num_per_line
|
|
303
|
+
m = lines
|
|
304
|
+
if x:
|
|
305
|
+
m = lines + 1
|
|
306
|
+
|
|
307
|
+
seq = ""
|
|
308
|
+
for i in range(m):
|
|
309
|
+
n = num_per_line
|
|
310
|
+
if i == lines:
|
|
311
|
+
n = x
|
|
312
|
+
seq += sequence[i * num_per_line : i * num_per_line + n]
|
|
313
|
+
if i != (m - 1):
|
|
314
|
+
seq += "\n"
|
|
315
|
+
|
|
316
|
+
return seq
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
class BlastProcess:
|
|
320
|
+
"""Search reference sequence database for sequence correspondences for each polymer entity.
|
|
321
|
+
|
|
322
|
+
Obtain sequence search target from the the one-letter-code sequences stored in category
|
|
323
|
+
entity_poly.
|
|
324
|
+
|
|
325
|
+
Launch the Blast search for each entity and store processed results in CIF format data
|
|
326
|
+
file for each entity.
|
|
327
|
+
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
def __init__(self, cifFilePath=None, taxonomyFilePath=None, verbose=False, log=sys.stderr):
|
|
331
|
+
self.__verbose = verbose
|
|
332
|
+
self.__lfh = log
|
|
333
|
+
self.__saveBlastResults = False
|
|
334
|
+
self.__blastFileNamePrefix = None
|
|
335
|
+
self.__filePath = cifFilePath
|
|
336
|
+
self.__taxonomyFilePath = taxonomyFilePath
|
|
337
|
+
self.__taxonomyData = {}
|
|
338
|
+
self.__blastPath = "."
|
|
339
|
+
#
|
|
340
|
+
# Read input file and create dictionary of polymer sequence details
|
|
341
|
+
self.__cifObj = mmCIFUtil(filePath=self.__filePath)
|
|
342
|
+
self.__BlockID = self.__cifObj.GetBlockID()
|
|
343
|
+
self.__entitySeq = self._GetEntitySeq()
|
|
344
|
+
self.__taxFlag = True
|
|
345
|
+
if self.__taxFlag:
|
|
346
|
+
self.__taxonomyData = self._readTaxonomyData()
|
|
347
|
+
if self.__verbose:
|
|
348
|
+
self.__lfh.write("+INFO BlastProcess() taxonomy file path %s\n" % self.__taxonomyFilePath)
|
|
349
|
+
self.__lfh.write("+INFO BlastProcess() taxonomy data length %d\n" % len(self.__taxonomyData))
|
|
350
|
+
|
|
351
|
+
if self.__verbose:
|
|
352
|
+
self.__lfh.write("+INFO BlastProcess() cifFilePath %s\n" % self.__filePath)
|
|
353
|
+
self.__lfh.write("+INFO BlastProcess() taxonomyFilePath %s\n" % self.__taxonomyFilePath)
|
|
354
|
+
self.__lfh.write("+INFO BlastProcess() entity sequence dictionary %r\n" % self.__entitySeq)
|
|
355
|
+
|
|
356
|
+
def saveBlastResults(self, blastPath=".", blastFileNamePrefix="test"):
|
|
357
|
+
self.__saveBlastResults = True
|
|
358
|
+
self.__blastPath = blastPath
|
|
359
|
+
self.__blastFileNamePrefix = blastFileNamePrefix
|
|
360
|
+
|
|
361
|
+
def getPolymerEntityDetails(self):
|
|
362
|
+
"""Return a referenc to the internal dictionary containing polymer entity details."""
|
|
363
|
+
return self.__entitySeq
|
|
364
|
+
|
|
365
|
+
def _GetEntitySeq(self):
|
|
366
|
+
"""Read the entity_poly category and
|
|
367
|
+
|
|
368
|
+
Return a dictionary containing for each polymer entity:
|
|
369
|
+
|
|
370
|
+
['seq'] = one-letter-code sequence
|
|
371
|
+
['type'] = sequence type --
|
|
372
|
+
['fragments'] = list of dictionaries for each sequece fragment containing keys - beg (one based), end, & tax_id
|
|
373
|
+
|
|
374
|
+
"""
|
|
375
|
+
rdict = {}
|
|
376
|
+
|
|
377
|
+
dList = self.__cifObj.GetValue("entity_poly")
|
|
378
|
+
if not dList:
|
|
379
|
+
return rdict
|
|
380
|
+
|
|
381
|
+
for d in dList:
|
|
382
|
+
if "entity_id" not in d:
|
|
383
|
+
continue
|
|
384
|
+
|
|
385
|
+
sequence = ""
|
|
386
|
+
if "pdbx_seq_one_letter_code" in d:
|
|
387
|
+
sequence = d["pdbx_seq_one_letter_code"]
|
|
388
|
+
elif "ndb_seq_one_letter_code" in d:
|
|
389
|
+
sequence = d["ndb_seq_one_letter_code"]
|
|
390
|
+
|
|
391
|
+
sequence = self._CleanSequence(sequence)
|
|
392
|
+
if len(sequence) < 5:
|
|
393
|
+
continue
|
|
394
|
+
|
|
395
|
+
rtype = None
|
|
396
|
+
if "type" in d:
|
|
397
|
+
rtype = d["type"]
|
|
398
|
+
rtype = self._FindCorrectType(rtype)
|
|
399
|
+
|
|
400
|
+
if not rtype:
|
|
401
|
+
rtype = self._FindTypeFromSeq(sequence)
|
|
402
|
+
|
|
403
|
+
if rtype not in ["polyribonucleotide", "polypeptide"]:
|
|
404
|
+
continue
|
|
405
|
+
|
|
406
|
+
if rtype == "polyribonucleotide" and len(sequence) < 50:
|
|
407
|
+
continue
|
|
408
|
+
|
|
409
|
+
rdict[d["entity_id"]] = {}
|
|
410
|
+
rdict[d["entity_id"]]["seq"] = sequence
|
|
411
|
+
rdict[d["entity_id"]]["type"] = rtype
|
|
412
|
+
fragmentList = self._GetEntityProperty(d["entity_id"], len(sequence))
|
|
413
|
+
if fragmentList:
|
|
414
|
+
rdict[d["entity_id"]]["fragments"] = fragmentList
|
|
415
|
+
|
|
416
|
+
return rdict
|
|
417
|
+
|
|
418
|
+
def _GetEntityProperty(self, entityid, length):
|
|
419
|
+
pList = self._GetTaxID(entityid, length, "entity_src_gen", "pdbx_gene_src_ncbi_taxonomy_id")
|
|
420
|
+
if not pList:
|
|
421
|
+
pList = self._GetTaxID(entityid, length, "entity_src_nat", "pdbx_ncbi_taxonomy_id")
|
|
422
|
+
if not pList:
|
|
423
|
+
pList = self._GetTaxID(entityid, length, "pdbx_entity_src_syn", "ncbi_taxonomy_id")
|
|
424
|
+
|
|
425
|
+
if not pList:
|
|
426
|
+
rdict = {}
|
|
427
|
+
rdict["beg"] = "1"
|
|
428
|
+
rdict["end"] = str(length)
|
|
429
|
+
pList.append(rdict)
|
|
430
|
+
|
|
431
|
+
return pList
|
|
432
|
+
|
|
433
|
+
def _GetTaxID(self, entityid, length, category, item):
|
|
434
|
+
pList = []
|
|
435
|
+
|
|
436
|
+
dList = self.__cifObj.GetValue(category)
|
|
437
|
+
if not dList:
|
|
438
|
+
return pList
|
|
439
|
+
|
|
440
|
+
for d in dList:
|
|
441
|
+
if "entity_id" not in d:
|
|
442
|
+
continue
|
|
443
|
+
|
|
444
|
+
if d["entity_id"] != entityid:
|
|
445
|
+
continue
|
|
446
|
+
|
|
447
|
+
rdict = {}
|
|
448
|
+
if item in d:
|
|
449
|
+
rdict["taxid"] = d[item]
|
|
450
|
+
|
|
451
|
+
if "pdbx_beg_seq_num" in d and "pdbx_end_seq_num" in d:
|
|
452
|
+
rdict["beg"] = d["pdbx_beg_seq_num"]
|
|
453
|
+
rdict["end"] = d["pdbx_end_seq_num"]
|
|
454
|
+
else:
|
|
455
|
+
rdict["beg"] = "1"
|
|
456
|
+
rdict["end"] = str(length)
|
|
457
|
+
|
|
458
|
+
pList.append(rdict)
|
|
459
|
+
|
|
460
|
+
if pList:
|
|
461
|
+
if not self._checkFragments(length, pList):
|
|
462
|
+
pList = pList[0:1]
|
|
463
|
+
pList[0]["beg"] = "1"
|
|
464
|
+
pList[0]["end"] = str(length)
|
|
465
|
+
|
|
466
|
+
for fragment in pList:
|
|
467
|
+
if "taxid" in fragment:
|
|
468
|
+
self.__taxFlag = True
|
|
469
|
+
break
|
|
470
|
+
|
|
471
|
+
return pList
|
|
472
|
+
|
|
473
|
+
@staticmethod
|
|
474
|
+
def _checkFragments(length, pList):
|
|
475
|
+
if not pList:
|
|
476
|
+
return True
|
|
477
|
+
|
|
478
|
+
# check start number in first fragment >= 1
|
|
479
|
+
start = int(pList[0]["beg"])
|
|
480
|
+
if start < 1:
|
|
481
|
+
return False
|
|
482
|
+
|
|
483
|
+
# check end number in last fragment <= length of whole sequence
|
|
484
|
+
n = len(pList)
|
|
485
|
+
iend = int(pList[n - 1]["end"])
|
|
486
|
+
if iend > length:
|
|
487
|
+
return False
|
|
488
|
+
|
|
489
|
+
# check start number < end number in same fragment
|
|
490
|
+
for fragment in pList:
|
|
491
|
+
start = int(fragment["beg"])
|
|
492
|
+
end = int(fragment["end"])
|
|
493
|
+
if start >= end:
|
|
494
|
+
return False
|
|
495
|
+
|
|
496
|
+
if n < 2:
|
|
497
|
+
return True
|
|
498
|
+
|
|
499
|
+
# check start number in current fragment > end number in previous fragment
|
|
500
|
+
for i in range(1, n):
|
|
501
|
+
end = int(pList[i - 1]["end"])
|
|
502
|
+
start = int(pList[i]["beg"])
|
|
503
|
+
if end >= start:
|
|
504
|
+
return False
|
|
505
|
+
|
|
506
|
+
return True
|
|
507
|
+
|
|
508
|
+
def _readTaxonomyData(self):
|
|
509
|
+
"""Read the NCBI taxonomy data file 'nodes.dmp' and
|
|
510
|
+
return a dictionary of parent taxonomy id's.
|
|
511
|
+
|
|
512
|
+
d[tax_id]=parent_tax_id
|
|
513
|
+
|
|
514
|
+
"""
|
|
515
|
+
if not self.__taxonomyFilePath:
|
|
516
|
+
return
|
|
517
|
+
|
|
518
|
+
d = {}
|
|
519
|
+
try:
|
|
520
|
+
if self.__taxonomyFilePath[-3:] == ".gz":
|
|
521
|
+
f = gzip.open(self.__taxonomyFilePath, "r")
|
|
522
|
+
else:
|
|
523
|
+
f = open(self.__taxonomyFilePath)
|
|
524
|
+
data = f.read()
|
|
525
|
+
f.close()
|
|
526
|
+
#
|
|
527
|
+
# Python3 coming in as bytes from compressed file
|
|
528
|
+
tlist = data.decode("utf-8").split("\t|\n")
|
|
529
|
+
for line in tlist:
|
|
530
|
+
if not line:
|
|
531
|
+
continue
|
|
532
|
+
list1 = line.split("\t|\t")
|
|
533
|
+
d[list1[0]] = list1[1]
|
|
534
|
+
except Exception as e: # noqa: BLE001
|
|
535
|
+
traceback.print_exc(file=self.__lfh)
|
|
536
|
+
self.__lfh.write(
|
|
537
|
+
"+ERROR BlastProcess() failed to read taxonomy data file %s %s\n" % (str(e), self.__taxonomyFilePath)
|
|
538
|
+
)
|
|
539
|
+
return d
|
|
540
|
+
|
|
541
|
+
@staticmethod
|
|
542
|
+
def _CleanSequence(seq):
|
|
543
|
+
seq = seq.upper()
|
|
544
|
+
seq = re.sub("[\t \n]", "", seq)
|
|
545
|
+
seq = re.sub("\\(MSE\\)", "M", seq)
|
|
546
|
+
seq = re.sub("\\([A-Z]{2,3}\\)", "X", seq)
|
|
547
|
+
return seq
|
|
548
|
+
|
|
549
|
+
@staticmethod
|
|
550
|
+
def _FindCorrectType(ptype):
|
|
551
|
+
ptype = ptype.lower()
|
|
552
|
+
ptype = ptype.strip()
|
|
553
|
+
if not ptype:
|
|
554
|
+
return ptype
|
|
555
|
+
if ptype.find("polydeoxyribonucleotide") >= 0 or ptype.find("dna") >= 0:
|
|
556
|
+
return "polydeoxyribonucleotide"
|
|
557
|
+
if ptype == "polyribonucleotide" or ptype == "rna":
|
|
558
|
+
return "polyribonucleotide"
|
|
559
|
+
if ptype.find("polypeptide") >= 0 or ptype == "protein":
|
|
560
|
+
return "polypeptide"
|
|
561
|
+
if ptype == "?" or ptype == ".":
|
|
562
|
+
return ""
|
|
563
|
+
return ptype
|
|
564
|
+
|
|
565
|
+
@staticmethod
|
|
566
|
+
def _FindTypeFromSeq(seq):
|
|
567
|
+
if re.search("[DEFHIKLMNPQRSVWY]", seq):
|
|
568
|
+
return "polypeptide"
|
|
569
|
+
if re.search("[ACGU]", seq):
|
|
570
|
+
return "polyribonucleotide"
|
|
571
|
+
return ""
|
|
572
|
+
|
|
573
|
+
def Run(self, entityId=None):
|
|
574
|
+
"""Execute the reference sequence search and return processed correspondence results -
|
|
575
|
+
|
|
576
|
+
If entityId is specified then results are returned in a dictionary for each polymer
|
|
577
|
+
entity.
|
|
578
|
+
|
|
579
|
+
Otherwise, the all polymer entities are processed and the results are written
|
|
580
|
+
to CIF data files for each entity.
|
|
581
|
+
|
|
582
|
+
The output file name convention is <Entry_id>.<entity_id>.info.cif
|
|
583
|
+
|
|
584
|
+
"""
|
|
585
|
+
hitlist = {}
|
|
586
|
+
if not self.__entitySeq:
|
|
587
|
+
return hitlist
|
|
588
|
+
|
|
589
|
+
if entityId:
|
|
590
|
+
if entityId in self.__entitySeq:
|
|
591
|
+
perseq = RunBlastPerSeq(
|
|
592
|
+
entityId=entityId,
|
|
593
|
+
entityInfo=self.__entitySeq[entityId],
|
|
594
|
+
taxonomyData=self.__taxonomyData,
|
|
595
|
+
verbose=self.__verbose,
|
|
596
|
+
log=self.__lfh,
|
|
597
|
+
)
|
|
598
|
+
if self.__saveBlastResults:
|
|
599
|
+
perseq.saveBlastResults(blastPath=self.__blastPath, blastFileNamePrefix=self.__blastFileNamePrefix)
|
|
600
|
+
perseq.Run()
|
|
601
|
+
hitlist = perseq.GetResult()
|
|
602
|
+
return hitlist
|
|
603
|
+
|
|
604
|
+
for k, v in self.__entitySeq.items():
|
|
605
|
+
perseq = RunBlastPerSeq(
|
|
606
|
+
entityId=k, entityInfo=v, taxonomyData=self.__taxonomyData, verbose=self.__verbose, log=self.__lfh
|
|
607
|
+
)
|
|
608
|
+
if self.__saveBlastResults:
|
|
609
|
+
perseq.saveBlastResults(blastPath=self.__blastPath, blastFileNamePrefix=self.__blastFileNamePrefix)
|
|
610
|
+
|
|
611
|
+
perseq.Run()
|
|
612
|
+
perseq.WriteCifFile(self.__BlockID, self.__BlockID + "." + k + ".info.cif")
|
|
613
|
+
return hitlist
|
|
614
|
+
|
|
615
|
+
def RunAndSave(self, entityId=None, fName=None):
|
|
616
|
+
"""Execute the reference sequence search and return processed correspondence results in
|
|
617
|
+
the named file.
|
|
618
|
+
|
|
619
|
+
This is the entry point for wwDPB API plugin.
|
|
620
|
+
|
|
621
|
+
"""
|
|
622
|
+
if entityId is None or fName is None:
|
|
623
|
+
return False
|
|
624
|
+
|
|
625
|
+
eId = str(entityId)
|
|
626
|
+
if eId not in self.__entitySeq:
|
|
627
|
+
return False
|
|
628
|
+
|
|
629
|
+
perseq = RunBlastPerSeq(
|
|
630
|
+
entityId=eId,
|
|
631
|
+
entityInfo=self.__entitySeq[eId],
|
|
632
|
+
taxonomyData=self.__taxonomyData,
|
|
633
|
+
verbose=self.__verbose,
|
|
634
|
+
log=self.__lfh,
|
|
635
|
+
)
|
|
636
|
+
if self.__saveBlastResults:
|
|
637
|
+
perseq.saveBlastResults(blastPath=self.__blastPath, blastFileNamePrefix=self.__blastFileNamePrefix)
|
|
638
|
+
|
|
639
|
+
perseq.Run()
|
|
640
|
+
perseq.WriteCifFile(self.__BlockID, str(fName))
|
|
641
|
+
return True
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
def main(argv): # pragma: no cover
|
|
645
|
+
opts, _args = getopt.getopt(argv, "i:e:t:", ["ciffile=", "entity=", "taxonomy="])
|
|
646
|
+
|
|
647
|
+
ciffile = None
|
|
648
|
+
entity = None
|
|
649
|
+
taxfile = None
|
|
650
|
+
for opt, arg in opts:
|
|
651
|
+
if opt in ("-i", "--ciffile"):
|
|
652
|
+
ciffile = arg
|
|
653
|
+
elif opt in ("-e", "--entity"):
|
|
654
|
+
entity = arg
|
|
655
|
+
elif opt in ("-t", "--taxonomy"):
|
|
656
|
+
taxfile = arg
|
|
657
|
+
|
|
658
|
+
if ciffile:
|
|
659
|
+
try:
|
|
660
|
+
process = BlastProcess(cifFilePath=ciffile, taxonomyFilePath=taxfile)
|
|
661
|
+
resultlist = process.Run(entityId=entity)
|
|
662
|
+
for ii, rst in enumerate(resultlist):
|
|
663
|
+
sys.stdout.write("%d: %r\n" % (ii, rst))
|
|
664
|
+
|
|
665
|
+
except Exception as exc: # noqa: BLE001
|
|
666
|
+
sys.stderr.write(exc)
|
|
667
|
+
traceback.print_exc(file=sys.stderr)
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
if __name__ == "__main__": # pragma: no cover
|
|
671
|
+
try:
|
|
672
|
+
main(sys.argv[1:])
|
|
673
|
+
sys.exit(0)
|
|
674
|
+
except Exception as exc: # noqa: BLE001
|
|
675
|
+
sys.stderr.write("Exception: %s\n" % str(exc))
|
|
676
|
+
sys.exit(1)
|