forked from salilab/SOAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequences.py
422 lines (395 loc) · 14.7 KB
/
sequences.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""
A module for processing protein sequences in PIR format.
"""
from env import *
class pir(object):
"""
Process PIR files for use by MDT.
MDT calculates statistics from structures by looping through
a PIR file containing all sequences for the structures.
The last two fields in the PIR header (X-ray resolution and the r factor)
can be repurposed to define the error bar parameters associated
with each structure, used by MDT to calculate the error bar
on the position of each atom.
:Parameters:
- `path`: path to the PIR file.
"""
def __init__(self, path=''):
self.pirpath=path
def get_pir(self,pdbset):#X-xray, U-biological unit, T-transmembrane protein, M-membrane protein,S-single chain, W-non-membrane protein,1-single chain pir or none, 2-multiple chain pir, H-helical protein, B-beta protein
import pp
pirfiledirectory=runenv.pdbpirdir
pdbfile='pdb_'+pdbset+'.pir'
listoffiles=os.listdir(pirfiledirectory)
if pdbfile in listoffiles:
return pdbfile
pirlist=pdbset.split('_')
#if pirlist[-1].startswith('')
if re.search('_[0-9]{2}',pdbset[-3:]):
self.property_filter(pdbset[:-3])
os.chdir(pirfiledirectory)
pp.pir_sif('pdb_'+pdbset[:-3]+'.pir', pdbfile,si=int(pdbset[-2:]))
else:
self.property_filter(pdbset)
return pdbfile
def get_property_dict(self,pdbset):
#pdb.set_trace()
fc=pdbset.split('_')
propertydict={}
k=-1
for fi in fc:
k=k+1
if k==0:
propertydict['type']=fi #X-xray, S-single chain, M-Membrane, W-non-membrane, T-transmembrane, U-biological units
elif fi.find('ph')!=-1:
fic=fi.split('ph')
propertydict['ph']=[float(fic[0]), float(fic[1])]
continue
elif fi[-1]=='A':
propertydict['resolution']= float(fi[:-1])
continue
elif fi[-1]=='r':
propertydict['r']= float(fi[:-1])
continue
elif fi[-5:]=='rfree':
propertydict['rfree']= float(fi[:-5])
continue
elif fi[-1]=='p':
propertydict['similar']=[fi[:-3],float(fi[-3:-1])]
continue
else:
propertydict[fi]=fi
continue
return propertydict
def property_filter(self,pdbset):
pirfiledirectory=runenv.pdbpirdir
pdbfile='pdb_'+pdbset+'.pir'
listoffiles=os.listdir(pirfiledirectory)
if pdbfile in listoffiles:
return pdbfile
pirstring=self.get_pir_from_pdbdict(pdbset)
fh=open(runenv.pdbpirdir+'pdb_'+pdbset+'.pir','w')
fh.write(pirstring)
fh.close()
pir(path=runenv.pdbpirdir+'pdb_'+pdbset+'.pir').duplicate_check()
def get_pir_from_pdbdict(self,pdbset):
import pp
if pdbset.startswith('THAC'):
ep=pp.tmhpdb()
ppdict=self.get_property_dict(pdbset[4:])
elif pdbset.startswith('THMC'):
ep=pp.tmhmcpdb()
ppdict=self.get_property_dict(pdbset[4:])
elif pdbset.startswith('BIOU'):
ep=pp.bioupdb()
ppdict=self.get_property_dict(pdbset[4:])
else:
ppdict=self.get_property_dict(pdbset)
ep=pp.entirepdb()
return ep.get_pir(ppdict)
def make_pir_single_forautomodel(self):
code=self.pirpath
e=environ()
e.io.atom_files_directory=runenv.pdbdir
aln=alignment(e)
mdl=model(e)
mdl.read(file=code)
aln.append_model(mdl,align_codes=code,atom_files=code)
aln.append_model(mdl,align_codes=code+'decoys')
aln.write(file= code+'.pir',alignment_format='PIR')
def make_pir_fromlist(self,filelist):
code=self.pirpath
e=environ()
e.io.atom_files_directory=['./']
output = modfile.File(self.pirpath, 'w')
ndnlist=[]
fl=os.listdir('./')
bf=[f for f in fl if 'base' in f and (f.endswith('pdb') or f.endswith('.pdb.gz') or f.endswith('gz'))]
for f in filelist:
try:
#if 1:
mdl=model(e)
if os.path.isfile('needattachtobase'):
if os.path.isfile(f+'.pdb.gz'):
lf=gzip.open(f+'.pdb.gz').read()
else:
lf=open(f+'.pdb').read()
if 'gz' in bf[0]:
open('temp.pdb','w').write(gzip.open(bf[0]).read()+'\n'+lf)
else:
open('temp.pdb','w').write(open(bf[0]).read()+'\n'+lf)
mdl.read('temp.pdb')
else:
mdl.read(f)
aln=alignment(e)
aln.append_model(mdl,align_codes=f,atom_files=f)
aln.write(output,alignment_format='PIR')
ndnlist.append(f)
except Exception, error:
print os.listdir('./')
print error
print f
traceback.print_exc()
return ndnlist
def count_pir(self):
input=file(self.pirpath)
n=0;
pirhead=re.compile(">P1;(.*)$")
for line in input:
if pirhead.match(line):
n=n+1
return n
def build_dict(self):
pirhead=re.compile(">P1;(.+)$")
fh=open(self.pirpath,'r')
pirdict={}
code=''
codepir=''
for line in fh:
if pirhead.match(line):
pirdict[code]=codepir
code=pirhead.match(line).group(1)
codepir=''
codepir=codepir+line
pirdict[code]=codepir
self.pirdict=pirdict
fh.close()
def get_codelist(self):
input=file(self.pirpath)
n=0;
pirhead=re.compile(">P1;(.*)$")
codelist=[]
for line in input:
if pirhead.match(line):
rer=pirhead.match(line)
codelist.append(rer.group(1))
return codelist
def count_residue2(self,residuepower=2):
input=file(self.pirpath)
n=0;
pirhead=re.compile(">P1;(.*)$")
linematch=re.compile("\S+.*")
sly=0
county=0
for line in input:
if county:
if linematch.match(line):
numofr=numofr+len(line)-1
else:
n=n+numofr**residuepower
county=0
if sly==1:
sly=0
county=1
if pirhead.match(line):
sly=1;
numofr=0;
#print line
return n
def sep_pir(self, tardir, numoff,permin=100,residuepower=2):
pirfile=self.pirpath
bdir=tardir # input
totalcodenum=self.count_pir()
if totalcodenum<permin:
print os.system('cp '+pirfile+' '+tardir+'/pdbs1')
return 0
if float(totalcodenum)/numoff<permin:
numoff=int(float(totalcodenum)/permin)
input=file(pirfile)
tn=self.count_residue2(residuepower)
maxn=tn/numoff;#input
k=1;#input
tc=0;
n=0;
os.chdir(tardir)
out=file('pdbs'+str(k),"w")
pirhead=re.compile(">P1;(.*)$")
linematch=re.compile("\S+.*")
sly=0
county=0
for line in input:
if county:
if linematch.match(line):
numofr=numofr+len(line)-1
else:
n=n+numofr**residuepower
county=0
if sly==1:
sly=0
county=1
if pirhead.match(line):
if n > maxn:
n=0
k=k+1
out.flush()
out.close()
out=file('pdbs'+str(k),"w")
#print 'seperating'+ str(k)
sly=1
numofr=0;
tc=tc+1
out.write(line)
out.flush()
out.close()
def gen_pir(self, pirfiledirectory,pdbset):
print 'Generating pdb file'
env = environ()
aln = alignment(env)
pdbfile='pdb_'+pdbset+'.pir'
listoffiles=os.listdir(pirfiledirectory)
for filen in listoffiles:
if pdbfile==filen:
return pdbfile
for filen in listoffiles:
if re.match(filen[:-4],pdbfile):
if re.match('[0-9.]*A',pdbfile[len(filen)-3:-4]):
print filen
input = modfile.File(pirfiledirectory+filen, 'r')
output = modfile.File(pirfiledirectory+pdbfile, 'w')
while aln.read_one(input, alignment_format='PIR'):
print "Read code %s" % aln[0].code
if aln[0].resolution < float(pdbfile[len(filen)-3:-5]):
aln.write(output, alignment_format='PIR')
input.close()
output.close()
return pdbfile
print 'The program do not know how to generate the pdbpir file. Please generate it manually.'
return 0
def filter_pir(self,inputpirfile,outputpirfile,structuretype='structureX',structureresolution=99.0):
if (os.access(outputpirfile,os.F_OK)):
return 0
env = environ()
aln = alignment(env)
input = modfile.File(inputpirfile, 'r')
output = modfile.File(outputpirfile, 'w')
while aln.read_one(input, alignment_format='PIR'):
if aln[0].resolution < structureresolution and aln[0].prottyp==structuretype:
aln.write(output, alignment_format='PIR')
input.close()
output.close()
def sif_pir(self,inputpirfile,outputpirfile,si=30):
if (os.access(outputpirfile,os.F_OK)):
return 0
#log.verbose()
env = environ()
sdb = sequence_db(env, seq_database_file=inputpirfile,
seq_database_format='PIR',
chains_list='ALL', minmax_db_seq_len=(30, 3000),
clean_sequences=True)
sdb.filter(rr_file='${LIB}/blosum62.sim.mat', gap_penalties_1d=(-500, -50),
matrix_offset = -450, max_diff_res=30, seqid_cut=si,
output_grp_file=outputpirfile+'.grp', output_cod_file=outputpirfile+'.cod')
out = file(outputpirfile, "w")
codes = [line.rstrip('\r\n') for line in file(outputpirfile+'.cod')]
codes = dict.fromkeys(codes)
pirhead = re.compile(">P1;(.*)$")
printline = False
for line in file(inputpirfile):
m = pirhead.match(line)
if m:
printline = m.group(1) in codes
if printline:
out.write(line)
def gen_pir_new(self,pirfiledirectory,pdbset):
print 'Generating pdb file ' + pdbset
env = environ()
aln = alignment(env)
pdbfile='pdb_'+pdbset+'.pir'
listoffiles=os.listdir(pirfiledirectory)
for filen in listoffiles:
if pdbfile==filen:
return pdbfile
if pdbset[0]=='X':
sttype='structureX'
elif pdbset[0]=='N':
sttype='structureN'
else:
print 'unsupported pdb database file type (example X_2.2A_60): '+pdbset
if re.search('[0-9.]*A',pdbset):
stresolution=float(pdbset[2:5])
print stresolution
else:
stresolution=99.0
if not re.search('_[0-9]*$',pdbset):
pdb1name=pdbfile
else:
pdb1name=pdbfile[-7:]+'.pir'
filter_pir(pirfiledirectory+'pdball.pir',pirfiledirectory+pdbfile[0:10]+'.pir', structuretype=sttype,structureresolution=stresolution)
if re.search('_[0-9]*$',pdbset):
sic=int(pdbset[-2:])
sif_pir(pirfiledirectory+pdbfile[0:10]+'.pir', pdbfile,si=sic)
return pdbfile
def get_pir_fromlist(self,list,targetpath):
pirstring=''
self.build_dict()
k=0
for fn in list:
k=k+1
print str(k)+' outof '+str(len(list))
pirstring=pirstring+self.pirdict[fn]
out=open(targetpath,'w')
out.write(pirstring)
out.close()
def get_first_n(self,n,targetpath):
pirstring=''
self.build_dict()
k=0
for fn in self.pirdict.keys():
k=k+1
pirstring=pirstring+self.pirdict[fn]
if k>=n:
break
out=open(targetpath,'w')
out.write(pirstring)
out.close()
def filter_pir(self,xray=2,rfactor=0.25,phrange=[6.5,7.5],sid=60):
tp=self.pirpath[:-4]+'_'+str(xray)+'_'+str(rfactor)+'_'+str(phrange[0])+'-'+str(phrange[1])+'.pir'
if os.path.isfile(tp[:-4]+'_'+str(sid)+'.pir'):
return 0
self.build_dict()
pirstring=''
env=environ()
env.io.atom_files_directory=[runenv.opdbdir]
for key in self.pirdict:
if len(key)<4:
continue
print key
mdl=model(env)
mdl.read(file=key[0:4])
if mdl.rfactor>rfactor or mdl.resolution>xray or mdl.rfactor==-1 or mdl.resolution==-1:
continue
ph=get_ph(key[0:4])
if ph<phrange[0] or ph>phrange[1]:
print 'ph not in range '+str(ph)
continue
pirstring=pirstring+self.pirdict[key]
out=open(tp,'w')
out.write(pirstring)
out.close
import pp
pp.pir_sif(tp, tp[:-4]+'_'+str(sid)+'.pir',si=sid)
def clean_pdb(self):
env=environ()
os.chdir(runenv.basedir+'pdb/')
env.io.atom_files_directory =['/salilab/park2/database/pdb/divided/']
codelist=self.get_codelist()
for code in codelist:
mdl=model(env)
mdl.read(code[0:4])
mdl.write('pdb'+code[0:4]+'.ent')
print os.system('scp pdb'+code[0:4]+'.ent '+runenv.jobserver+':~/pdb/')
def duplicate_check(self):
codelist=self.get_codelist()
if len(set(codelist))<len(codelist):
raise Exception('Duplicate entries in pir file possbily due to bugs in codes to generate the pir file: '+self.pirpath)
def exclude(self,excludepirfile,resultpir):
po=pir(path=excludepirfile)
codelist=po.get_codelist()
codelist=[code.split('.')[1][:4].lower() for code in codelist]
codeset=set(codelist)
self.build_dict()
pirlist=[]
for code in self.pirdict:
if not code[:4].lower() in codeset:
pirlist.append(self.pirdict[code])
open(resultpir,'w').write('\n'.join(pirlist))