-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodeller_caller.py
200 lines (170 loc) · 7.4 KB
/
modeller_caller.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
#!/usr/bin/python3
# encoding: utf-8
'''
Provides functions to modellize the structure of a protein.
Created on Mar 12, 2016
@author: Lluís
'''
import argparse
import logging
import os
import ftplib
import urllib
import sys
from Bio import SeqIO
import modeller.scripts
import modeller.automodel
import plots
# Code from https://salilab.org/archives/modeller_usage/2015/msg00043.html
class _ShutUp(object):
"""Redirects the output of the stdout.
This supres the modeller output when importing the module"""
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
def __exit__(self, *args):
sys.stdout.close()
sys.stdout = self._stdout
class env_mod(modeller.environ):
"""Modified version to redirect the output and open it silently."""
def __init__(self):
"""Modify with the class created"""
with _ShutUp():
super(env_mod, self)
env = env_mod() # Some variables needed for the modeller
class modeller_caller(object):
"""Class that gets the environment variables"""
def __init__(self, env):
"""Set the environment variables as a property of the class."""
self.env = env
def _extract_id(self, header):
"""Returns the id of the sequence."""
return header.split("|")[-2].lower()
def convert_ali(self, fasta, pir):
"""An alignment in fasta format is converted to Modeller/pir format.
fasta is a file with an alignment in fasta format
pir is the output name of the file with the alignment in pir format
It downloads the pdb of the alignment to fetch the necessary data for
the pir format.
"""
assert pir != "output.pir" # Assumption
logging.captureWarnings(True)
aln = modeller.alignment(self.env)
aln.append(file=fasta, alignment_format="FASTA", remove_gaps=False)
aln.write(file="output.pir", alignment_format='PIR')
fasta_h = open(fasta, "r")
sequences = SeqIO.parse(fasta_h, "fasta")
values = []
for record in sequences:
pdb_id = self._extract_id(record.id)
values.append([len(record), pdb_id])
# print(record.id)
# Download the pdb to build the model
# modeller search for all the posible names of the file
try:
pdb = plots.pdb_download(pdb_id, os.getcwd())
except urllib.error.URLError:
pass
except ftplib.error_perm:
pass
# finally:
# parser = PDBParser(PERMISSIVE=1)
# structure = parser.get_structure(pdb_id, pdb)
# print(parser.get_trailer())
self.pir = pir # Set the pir as an attribute
# Convert the pir into a understandable pir format?
with open(pir, "w") as out:
with open("output.pir", "r") as fl:
records = fl.read()
records = records.split(">")
for n, record in enumerate(records, start=-1):
lines = record.split("\n")
if lines[0] == "":
continue
id_pdb = self._extract_id(lines[0])
lines[0] = ">"+lines[0].split(";")[0]+";"+id_pdb
fields = lines[1].split(":")
fields[0] = "structureX"
fields[1] = id_pdb
fields[2] = "1"
fields[3] = "A"
if values[n][1] == id_pdb.rstrip():
fields[4] = str(values[n][0])
else:
fields[4] = "500" # Default length of the sequence
fields_a = []
for field in fields[:]:
if field == "":
fields_a.append(".")
else:
fields_a.append(field)
lines[1] = ":".join(fields_a)
lines_o = "\n".join(lines)
out.write(lines_o)
os.remove("output.pir")
logging.captureWarnings(False)
def asses_energy(self, pdb_file, name=None):
"""Asses energy of a pdb.
Returns a matrix of energy for a plot.
pdb_file is the name of the file to analyze
name is the name of the output file with the energy profile"""
logging.captureWarnings(True)
env.libs.topology.read(file='$(LIB)/top_heav.lib') # read topology
env.libs.parameters.read(file='$(LIB)/par.lib') # read parameters
# Setting to read all the models (not sure if it is the right way)
mdl = modeller.scripts.complete_pdb(self.env, pdb_file,
model_segment=('FIRST:A', 'LAST:Z')
)
s = modeller.selection(mdl)
output_op = 'ENERGY_PROFILE NO_REPORT VERY_LONG GRADIENT'
if name is not None:
score = s.assess_dope(output=output_op,
file='{}.profile'.format(name),
normalize_profile=True,
smoothing_window=15)
else:
score = s.assess_normalized_dope(normalize_profile=True,
smoothing_window=15)
logging.captureWarnings(False)
return score
def modelize(self, alig_pir, known, seq):
"""Uses automodel to generate a model of the protein.
alig_pir is the alignment in pir format of the proteins.
known are the pdb structures known which are similar to the sequence.
it can be a list of pdb id which should be on the same folder and
in the alignment file
seq is the sequence we want to create the structure"""
logging.captureWarnings(True)
assesment = modeller.automodel.assess.DOPE
a = modeller.automodel.automodel(self.env, alnfile=alig_pir,
knowns=known, sequence=seq,
assess_methods=assesment)
a.starting_model = 1
a.ending_model = 5
a.make()
self.outputs = a.outputs
logging.captureWarnings(False)
return self.outputs
if __name__ == "__main__":
fmt = """%(asctime)s - %(filename)s - %(funcName)s - %(levelname)s
- %(message)s"""
logging.basicConfig(filename='modeller_caller.log', level=logging.DEBUG,
format=fmt)
msg = 'Creates models of the sequences'
args_helper = argparse.ArgumentDefaultsHelpFormatter
argparser = argparse.ArgumentParser(description=msg,
formatter_class=args_helper)
argparser.add_argument("seq", help="Name of the sequence to analyse")
argparser.add_argument("models", help="Models of the file")
argparser.add_argument("-pir", help="Name of the file in pir format")
argparser.add_argument("-fasta",
help="File with sequences in fasta format")
args = argparser.parse_args()
env = env_mod() # Some variables needed for the modeller
modeler = modeller_caller(env)
# Convert the fasta alignment in pir format
if not args.fasta and not args.pir:
raise argparser.error("Required a fasta or a pir alignment")
elif args.fasta:
modeler.convert_ali(args.fasta, args.pir)
modeler.modelize(args.pir, args.seq, args.models)