File size: 11,720 Bytes
a3f3d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-

"""Module to handle pdb files."""

import os
from . import logger
import re
import gzip
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from io import StringIO
import json

_name = "PDB"

class Pdb:
    """
    Pdb parser. Initialized by:
    1. pdb filename
    2. gzipped pdb filename
    3. 4-letter pdb code
    """

    def __init__(self,*args, **kwargs):
        self.file_name = None
        self.pdb_code = None
        self.dir = os.getcwd()
        self.loc = os.path.join(self.dir, "input_pdb")
        self.codification = {"ALA" : 'A', "CYS" : 'C', "ASP" : 'D', "GLU" : 'E', "PHE" : 'F', "GLY" : 'G', "HIS" : 'H',
                             "ILE" : 'I', "LYS" : 'K', "LEU" : 'L', "MET" : 'M', "MSE" : 'M', "ASN" : 'N', "PYL" : 'O',
                             "PRO" : 'P', "GLN" : 'Q', "ARG" : 'R', "SER" : 'S', "THR" : 'T', "SEC" : 'U', "VAL" : 'V',
                             "TRP" : 'W', "5HP" : 'E', "ABA" : 'A', "AIB" : 'A', "BMT" : 'T', "CEA" : 'C', "CGU" : 'E',
                             "CME" : 'C', "CRO" : 'X', "CSD" : 'C', "CSO" : 'C', "CSS" : 'C', "CSW" : 'C', "CSX" : 'C',
                             "CXM" : 'M', "DAL" : 'A', "DAR" : 'R', "DCY" : 'C', "DGL" : 'E', "DGN" : 'Q', "DHI" : 'H',
                             "DIL" : 'I', "DIV" : 'V', "DLE" : 'L', "DLY" : 'K', "DPN" : 'F', "DPR" : 'P', "DSG" : 'N',
                             "DSN" : 'S', "DSP" : 'D', "DTH" : 'T', "DTR" : 'X', "DTY" : 'Y', "DVA" : 'V', "FME" : 'M',
                             "HYP" : 'P', "KCX" : 'K', "LLP" : 'K', "MLE" : 'L', "MVA" : 'V', "NLE" : 'L', "OCS" : 'C',
                             "ORN" : 'A', "PCA" : 'E', "PTR" : 'Y', "SAR" : 'G', "SEP" : 'S', "STY" : 'Y', "TPO" : 'T',
                             "TPQ" : 'F', "TYS" : 'Y', "TYR" : 'Y' }
        keys = list(self.codification.keys())
        self.sequences = {}
        self.onlycalfa = ""
        self.allatoms = ""
        self.chain = ""
        self.canumber = 0
        self.allnumber = 0

        if args and len(args) == 1:
            if args[0] is None: raise logger.AggrescanError("No pdb code/file provided. Quitting.",
                                                            module_name=_name)
            if os.path.isfile(args[0]):
                self.file_name = args[0]
            else:
                self.pdb_code = args[0]
        if kwargs:
            self.loc = kwargs['output']
            try:
                self.chain = kwargs['chain']
            except KeyError:
                pass

        if self.file_name:
            try:
                self.handler = gzip.GzipFile(filename=self.file_name)
                self.data = self.handler.readlines()
                logger.debug(module_name=_name, msg="Reading %s" % os.path.abspath(self.file_name))
            except IOError:
                try:
                    self.handler = open(self.file_name)
                    self.data = self.handler.readlines()
                    logger.debug(module_name=_name, msg="Reading %s" % os.path.abspath(self.file_name))
                except IOError:
                    raise logger.AggrescanError("Couldnt open specified filename %s. Quitting.' % os.path.abspath(self.file_name)",
                                                module_name=_name)
        elif self.pdb_code:
            self.handler = self.download_pdb()
            self.data = self.handler.readlines()

        seq = re.compile(r"^ATOM.{9}CA..(?P<seqid>.{3}).(?P<chain>.{1})(?P<resid>.{4})")  # TODO zle dla alternatywnych
        if self.chain != '':
            atm = re.compile(r"^ATOM.{9}(.{2}).( |A).{4}" + self.chain + "(?P<resid>.{4})(?P<x>.{12})(?P<y>.{8})(?P<z>.{8})")
        else:
            atm = re.compile(r"^ATOM.{9}(.{2}).( |A).{5}(?P<resid>.{4})(?P<x>.{12})(?P<y>.{8})(?P<z>.{8})")

        ter = re.compile(r'^END|^TER')
        mod = re.compile(r"^ENDMDL")
        self.trajectory = []
        self.sequence = ""

        lines = self.data
        end = len(lines) - 1
        counter = 0
        self._chainsOrder(lines)
        self._resIndexes(lines)
        self.mutatedata = {}

        for line in lines:
            line = re.sub(r'^HETATM(.{11})MSE(.*$)', r'ATOM  \1MET\2', line)
            localData = atm.match(line)
            data_seq = seq.match(line)

            if data_seq:
                seqid = data_seq.groups()[0].strip()
                chainid = data_seq.groups()[1].strip()
                resid = data_seq.groups()[2].strip()

                if seqid in keys:
                    s = self.codification[seqid]
                else:
                    s = "X"
                self.sequence += s

                # add to mutate page
                if chainid in list(self.mutatedata.keys()):
                    self.mutatedata[chainid].append({'chain': chainid,
                                                     'resname': s,
                                                     'residx': resid})
                else:
                    self.mutatedata[chainid] = [{'chain': chainid,
                                                 'resname': s,
                                                 'residx': resid}]

                if chainid in list(self.sequences.keys()):
                    self.sequences[chainid] += s
                else:
                    self.sequences[chainid] = s

            if localData:
                self.allnumber += 1
                self.allatoms += line
                dg = localData.groups()
                if dg[0] == 'CA':
                    self.onlycalfa += line
                    self.canumber += 1

            if counter == end:
                self.onlycalfa += line
                self.allatoms += line
            if ter.match(line):
                if self.chain:
                    if line[21] == self.chain:
                        self.onlycalfa += line
                        self.allatoms += line
                else:
                    self.onlycalfa += line
                    self.allatoms += line

            if (mod.match(line) and len(self.onlycalfa) > 1) or counter == end:
                break
            counter += 1
        self.handler.close()

    def _resIndexes(self, body):
        atm = re.compile(r"^ATOM.{9}CA..(?P<seqid>.{3}).(?P<chain>.{1})(?P<resid>.{4})")
        ter = re.compile(r'^END|^TER')
        mod = re.compile(r"^ENDMDL")
        self.numb = {}
        for chain in self.chains_order:
            self.numb[chain] = []

        for line in body:
            d = atm.match(line)
            if d:
                self.numb[d.group('chain').strip()].append(int(d.group('resid')))
            if mod.match(line):
                break

    def _chainsOrder(self, body):
        atm = re.compile(r"^ATOM.{9}CA..(?P<seqid>.{3}).(?P<chain>.{1})(?P<resid>.{4})")
        self.chains_order = []
        for line in body:
            d = atm.match(line)
            if d and d.group('chain') not in self.chains_order:
                self.chains_order.append(d.group('chain'))

    def isSingleChain(self):
        if self.chain != '' or len(list(self.sequences.keys())) == 1:
            return True
        else:
            return False

    def containsOnlyCA(self):
        if self.allnumber == self.canumber:
            return True
        else:
            return False

    def isBroken(self):
        brk = []
        if self.chain != '':
            indexes = self.numb[self.chain]
            first = indexes[0]
            for i in range(1, len(indexes)):
                if indexes[i] - 1 != first:
                    brk.append(str(first) + "-" + str(indexes[i]))
                first = indexes[i]
        else:
            for chain in list(self.sequences.keys()):
                indexes = self.numb[chain]
                first = indexes[0]
                for i in range(1, len(indexes)):
                    if indexes[i] - 1 != first:
                        brk.append(str(first) + "-" + str(indexes[i]))
                    first = indexes[i]
        if len(brk) > 0:
            return ", ".join(brk)
        return False

    def getResIndexes(self):
        t = [str(i) for i in self.numb[self.chain]]
        return ",".join(t)

    def getBody(self):
        return self.allatoms

    def containsChain(self, chain):
        if chain in list(self.sequences.keys()):
            return True

    def getSequenceNoHTML(self):
        if self.chain != '':
            return self.sequences[self.chain]
        else:
            out = ""
            for k in list(self.sequences.keys()):
                out += "".join(self.sequences[k])
            return out

    def getSequence(self):
        if self.chain != '':
            return "<strong>" + self.chain + "</strong>: " + self.sequences[self.chain]
        else:
            out = ""
            for k in list(self.sequences.keys()):
                out += "<strong>" + k + "</strong>: "
                out += "".join(self.sequences[k])
                out += "<br>"
            return out

    def getChainIdxResname(self):
        if self.chain == '':
            return json.dumps(self.mutatedata)
        else:
            return json.dumps({self.chain: self.mutatedata[self.chain]})

    def savePdbFile(self,path=''):
        if path:
            logger.to_file(filename=path, content=self.allatoms, allow_err=True)
        else:
            logger.to_file(filename=self.loc, content=self.allatoms, allow_err=True)

    def getPath(self):
        if os.path.isfile(self.loc):
            return self.loc
        else:
            raise logger.AggrescanError("Location for pdb file requested at: %s. The file was not found." % self.loc,
                                        module_name=_name)

    def download_pdb(self):
        try:
            gz_string = urlopen('http://www.rcsb.org/pdb/files/' + self.pdb_code.lower() + '.pdb.gz').read()
        except HTTPError as e:
            raise logger.AggrescanError("Could not download the pdb file. %s is not a valid pdb code/file. " % self.pdb_code,
                                        module_name=_name)

        except URLError as e:
            raise logger.AggrescanError("Could not download the pdb file. Can't connect to the PDB database - quitting",
                                        module_name=_name)
        fileLike = StringIO(gz_string)
        logger.debug(module_name=_name, msg="Successfully downloaded %s" % self.pdb_code.lower() + '.pdb.gz')
        return gzip.GzipFile(fileobj=fileLike,mode="rb")

    def validate(self):
        logger.debug(module_name=_name,msg='Validating pdb file: %s' % self.loc)
        if self.chain != '' and not self.containsChain(self.chain):
            raise logger.AggrescanError("Selected chain: %s not found in the pdb file. Quitting." % self.chain,
                                        module_name=_name)
        seq = self.getSequence()
        seq = re.sub("<strong>\w+</strong>:", "", seq)
        seq = re.sub("<br>", "", seq)
        seq = seq.replace(" ", "")
        allowed_seq = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L',
                       'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                       'W', 'Y']
        if len(seq) < 4:
            raise logger.AggrescanError("Sequence too short (perhaps something went wrong with pdb parsing).",
                                        module_name=_name)
        for e in seq:
            if e not in allowed_seq:
                raise logger.AggrescanError("Not supported amino acid: %s found in pdb file. Quitting." % e,
                                             module_name=_name)



if __name__ == '__main__':
    pass